{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE BangPatterns #-}
module Lightning.Protocol.BOLT5.Spend (
spend_to_local
, spend_htlc_tx
, spend_htlc_output
, spend_remote_htlc_timeout
, spend_remote_htlc_preimage
, spend_revoked_to_local
, spend_revoked_htlc
, spend_revoked_htlc_output
, spend_anchor_owner
, spend_anchor_anyone
) where
import Bitcoin.Prim.Tx (OutPoint, Tx(..), TxIn(..), TxOut(..), Witness(..))
import Bitcoin.Prim.Tx.Sighash (SighashType(..))
import qualified Data.ByteString as BS
import Data.List.NonEmpty (NonEmpty(..))
import Data.Word (Word32, Word64)
import Lightning.Protocol.BOLT1 (PaymentHash, Satoshi)
import qualified Lightning.Protocol.BOLT1 as BOLT1
import Lightning.Protocol.BOLT3
import Lightning.Protocol.BOLT5.Types
spend_to_local
:: OutPoint
-> Satoshi
-> RevocationPubkey
-> ToSelfDelay
-> LocalDelayedPubkey
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_to_local :: OutPoint
-> Satoshi
-> RevocationPubkey
-> ToSelfDelay
-> LocalDelayedPubkey
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_to_local !OutPoint
op !Satoshi
value !RevocationPubkey
revpk delay :: ToSelfDelay
delay@(ToSelfDelay Word16
d) !LocalDelayedPubkey
delayedpk
!Script
dest !FeeratePerKw
feerate =
OutPoint
-> Satoshi
-> Script
-> Word64
-> Word32
-> Word32
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
sweep OutPoint
op Satoshi
value (RevocationPubkey -> ToSelfDelay -> LocalDelayedPubkey -> Script
to_local_script RevocationPubkey
revpk ToSelfDelay
delay LocalDelayedPubkey
delayedpk)
Word64
to_local_penalty_input_weight (Word16 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
d) Word32
0 Script
dest FeeratePerKw
feerate
spend_htlc_tx
:: HTLCContext
-> CommitmentKeys
-> Maybe SpendingTx
spend_htlc_tx :: HTLCContext -> CommitmentKeys -> Maybe SpendingTx
spend_htlc_tx !HTLCContext
ctx !CommitmentKeys
keys = do
htx <- HTLCContext -> Maybe HTLCTx
build_htlc_tx HTLCContext
ctx
let !h = HTLCContext -> HTLC
hc_htlc HTLCContext
ctx
pure SpendingTx
{ stx_tx = htlc_to_tx htx
, stx_input_script = htlc_script keys (hc_format ctx) h
, stx_input_value = BOLT1.msat_to_sat (htlc_amount_msat h)
, stx_sighash_type = SIGHASH_ALL
}
spend_htlc_output
:: OutPoint
-> Satoshi
-> RevocationPubkey
-> ToSelfDelay
-> LocalDelayedPubkey
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_htlc_output :: OutPoint
-> Satoshi
-> RevocationPubkey
-> ToSelfDelay
-> LocalDelayedPubkey
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_htlc_output = OutPoint
-> Satoshi
-> RevocationPubkey
-> ToSelfDelay
-> LocalDelayedPubkey
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_to_local
spend_remote_htlc_timeout
:: OutPoint
-> Satoshi
-> HTLC
-> CommitmentKeys
-> CommitmentFormat
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_remote_htlc_timeout :: OutPoint
-> Satoshi
-> HTLC
-> CommitmentKeys
-> CommitmentFormat
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_remote_htlc_timeout !OutPoint
op !Satoshi
value !HTLC
htlc !CommitmentKeys
keys !CommitmentFormat
fmt !Script
dest !FeeratePerKw
feerate =
let expiry :: CltvExpiry
expiry@(CltvExpiry Word32
e) = HTLC -> CltvExpiry
htlc_cltv_expiry HTLC
htlc
!script :: Script
script = RevocationPubkey
-> RemoteHtlcPubkey
-> LocalHtlcPubkey
-> PaymentHash
-> CltvExpiry
-> CommitmentFormat
-> Script
received_htlc_script (CommitmentKeys -> RevocationPubkey
ck_revocation_pubkey CommitmentKeys
keys)
(CommitmentKeys -> RemoteHtlcPubkey
ck_remote_htlc CommitmentKeys
keys) (CommitmentKeys -> LocalHtlcPubkey
ck_local_htlc CommitmentKeys
keys)
(HTLC -> PaymentHash
htlc_payment_hash HTLC
htlc) CltvExpiry
expiry CommitmentFormat
fmt
in OutPoint
-> Satoshi
-> Script
-> Word64
-> Word32
-> Word32
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
sweep OutPoint
op Satoshi
value Script
script
(CommitmentFormat -> HTLCOutputType -> Word64
htlc_output_type_weight CommitmentFormat
fmt (CltvExpiry -> HTLCOutputType
HTLCReceivedOutput CltvExpiry
expiry))
(CommitmentFormat -> Word32
remote_htlc_sequence CommitmentFormat
fmt) Word32
e Script
dest FeeratePerKw
feerate
spend_remote_htlc_preimage
:: OutPoint
-> Satoshi
-> HTLC
-> CommitmentKeys
-> CommitmentFormat
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_remote_htlc_preimage :: OutPoint
-> Satoshi
-> HTLC
-> CommitmentKeys
-> CommitmentFormat
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_remote_htlc_preimage !OutPoint
op !Satoshi
value !HTLC
htlc !CommitmentKeys
keys !CommitmentFormat
fmt !Script
dest !FeeratePerKw
feerate =
let !expiry :: CltvExpiry
expiry = HTLC -> CltvExpiry
htlc_cltv_expiry HTLC
htlc
!script :: Script
script = RevocationPubkey
-> RemoteHtlcPubkey
-> LocalHtlcPubkey
-> PaymentHash
-> CommitmentFormat
-> Script
offered_htlc_script (CommitmentKeys -> RevocationPubkey
ck_revocation_pubkey CommitmentKeys
keys)
(CommitmentKeys -> RemoteHtlcPubkey
ck_remote_htlc CommitmentKeys
keys) (CommitmentKeys -> LocalHtlcPubkey
ck_local_htlc CommitmentKeys
keys)
(HTLC -> PaymentHash
htlc_payment_hash HTLC
htlc) CommitmentFormat
fmt
in OutPoint
-> Satoshi
-> Script
-> Word64
-> Word32
-> Word32
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
sweep OutPoint
op Satoshi
value Script
script
(CommitmentFormat -> HTLCOutputType -> Word64
htlc_output_type_weight CommitmentFormat
fmt (CltvExpiry -> HTLCOutputType
HTLCOfferedOutput CltvExpiry
expiry))
(CommitmentFormat -> Word32
remote_htlc_sequence CommitmentFormat
fmt) Word32
0 Script
dest FeeratePerKw
feerate
spend_revoked_to_local
:: OutPoint
-> Satoshi
-> RevocationPubkey
-> ToSelfDelay
-> LocalDelayedPubkey
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_revoked_to_local :: OutPoint
-> Satoshi
-> RevocationPubkey
-> ToSelfDelay
-> LocalDelayedPubkey
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_revoked_to_local !OutPoint
op !Satoshi
value !RevocationPubkey
revpk !ToSelfDelay
delay !LocalDelayedPubkey
delayedpk !Script
dest
!FeeratePerKw
feerate =
OutPoint
-> Satoshi
-> Script
-> Word64
-> Word32
-> Word32
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
sweep OutPoint
op Satoshi
value (RevocationPubkey -> ToSelfDelay -> LocalDelayedPubkey -> Script
to_local_script RevocationPubkey
revpk ToSelfDelay
delay LocalDelayedPubkey
delayedpk)
Word64
to_local_penalty_input_weight Word32
0xFFFFFFFF Word32
0 Script
dest FeeratePerKw
feerate
spend_revoked_htlc
:: OutPoint
-> Satoshi
-> HTLCOutputType
-> RevocationPubkey
-> CommitmentKeys
-> CommitmentFormat
-> PaymentHash
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_revoked_htlc :: OutPoint
-> Satoshi
-> HTLCOutputType
-> RevocationPubkey
-> CommitmentKeys
-> CommitmentFormat
-> PaymentHash
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_revoked_htlc !OutPoint
op !Satoshi
value !HTLCOutputType
htype !RevocationPubkey
revpk !CommitmentKeys
keys !CommitmentFormat
fmt !PaymentHash
ph !Script
dest
!FeeratePerKw
feerate =
let !remote :: RemoteHtlcPubkey
remote = CommitmentKeys -> RemoteHtlcPubkey
ck_remote_htlc CommitmentKeys
keys
!local :: LocalHtlcPubkey
local = CommitmentKeys -> LocalHtlcPubkey
ck_local_htlc CommitmentKeys
keys
!script :: Script
script = case HTLCOutputType
htype of
HTLCOfferedOutput CltvExpiry
_ ->
RevocationPubkey
-> RemoteHtlcPubkey
-> LocalHtlcPubkey
-> PaymentHash
-> CommitmentFormat
-> Script
offered_htlc_script RevocationPubkey
revpk RemoteHtlcPubkey
remote LocalHtlcPubkey
local PaymentHash
ph CommitmentFormat
fmt
HTLCReceivedOutput CltvExpiry
expiry ->
RevocationPubkey
-> RemoteHtlcPubkey
-> LocalHtlcPubkey
-> PaymentHash
-> CltvExpiry
-> CommitmentFormat
-> Script
received_htlc_script RevocationPubkey
revpk RemoteHtlcPubkey
remote LocalHtlcPubkey
local PaymentHash
ph CltvExpiry
expiry CommitmentFormat
fmt
in OutPoint
-> Satoshi
-> Script
-> Word64
-> Word32
-> Word32
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
sweep OutPoint
op Satoshi
value Script
script (CommitmentFormat -> HTLCOutputType -> Word64
htlc_output_type_weight CommitmentFormat
fmt HTLCOutputType
htype)
Word32
0xFFFFFFFF Word32
0 Script
dest FeeratePerKw
feerate
spend_revoked_htlc_output
:: OutPoint
-> Satoshi
-> RevocationPubkey
-> ToSelfDelay
-> LocalDelayedPubkey
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_revoked_htlc_output :: OutPoint
-> Satoshi
-> RevocationPubkey
-> ToSelfDelay
-> LocalDelayedPubkey
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_revoked_htlc_output = OutPoint
-> Satoshi
-> RevocationPubkey
-> ToSelfDelay
-> LocalDelayedPubkey
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
spend_revoked_to_local
spend_anchor_owner
:: OutPoint
-> Satoshi
-> FundingPubkey
-> Script
-> SpendingTx
spend_anchor_owner :: OutPoint -> Satoshi -> FundingPubkey -> Script -> SpendingTx
spend_anchor_owner !OutPoint
op !Satoshi
value !FundingPubkey
fundpk !Script
dest =
Tx -> Script -> Satoshi -> SighashType -> SpendingTx
SpendingTx (OutPoint -> Word32 -> Script -> Satoshi -> Word32 -> Tx
mk_spending_tx OutPoint
op Word32
0xFFFFFFFE Script
dest Satoshi
value Word32
0)
(FundingPubkey -> Script
anchor_script FundingPubkey
fundpk) Satoshi
value SighashType
SIGHASH_ALL
spend_anchor_anyone
:: OutPoint
-> Satoshi
-> FundingPubkey
-> Script
-> SpendingTx
spend_anchor_anyone :: OutPoint -> Satoshi -> FundingPubkey -> Script -> SpendingTx
spend_anchor_anyone !OutPoint
op !Satoshi
value !FundingPubkey
fundpk !Script
dest =
Tx -> Script -> Satoshi -> SighashType -> SpendingTx
SpendingTx (OutPoint -> Word32 -> Script -> Satoshi -> Word32 -> Tx
mk_spending_tx OutPoint
op Word32
16 Script
dest Satoshi
value Word32
0)
(FundingPubkey -> Script
anchor_script FundingPubkey
fundpk) Satoshi
value SighashType
SIGHASH_ALL
sweep
:: OutPoint
-> Satoshi
-> Script
-> Word64
-> Word32
-> Word32
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
sweep :: OutPoint
-> Satoshi
-> Script
-> Word64
-> Word32
-> Word32
-> Script
-> FeeratePerKw
-> Maybe SpendingTx
sweep !OutPoint
op !Satoshi
value !Script
script !Word64
weight !Word32
sq !Word32
locktime !Script
dest !FeeratePerKw
feerate = do
let !fee :: Satoshi
fee = FeeratePerKw -> Word64 -> Satoshi
spending_fee FeeratePerKw
feerate (Word64
weight Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
penalty_tx_base_weight)
out <- Satoshi -> Satoshi -> Script -> Maybe Satoshi
sweep_value Satoshi
value Satoshi
fee Script
dest
pure (SpendingTx (mk_spending_tx op sq dest out locktime) script
value SIGHASH_ALL)
sweep_value :: Satoshi -> Satoshi -> Script -> Maybe Satoshi
sweep_value :: Satoshi -> Satoshi -> Script -> Maybe Satoshi
sweep_value !Satoshi
value !Satoshi
fee (Script ByteString
dest) = do
out <- Satoshi -> Satoshi -> Maybe Satoshi
BOLT1.sub_sat Satoshi
value Satoshi
fee
if BOLT1.un_satoshi out < dust_threshold dest
then Nothing
else Just out
dust_threshold :: BS.ByteString -> Word64
dust_threshold :: ByteString -> Word64
dust_threshold ByteString
s =
let !len :: Word64
len = Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
s) :: Word64
!len_size :: Word64
len_size
| Word64
len Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0xfd = Word64
1
| Word64
len Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
0xffff = Word64
3
| Bool
otherwise = Word64
5
!spend_size :: Word64
spend_size = if ByteString -> Bool
is_witness_program ByteString
s then Word64
67 else Word64
148
in Word64
3 Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* (Word64
8 Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
len_size Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
len Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
spend_size)
is_witness_program :: BS.ByteString -> Bool
is_witness_program :: ByteString -> Bool
is_witness_program ByteString
s = case ByteString -> [Word8]
BS.unpack (Int -> ByteString -> ByteString
BS.take Int
2 ByteString
s) of
[Word8
version, Word8
push_len] ->
(Word8
version Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x00 Bool -> Bool -> Bool
|| (Word8
version Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word8
0x51 Bool -> Bool -> Bool
&& Word8
version Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
0x60))
Bool -> Bool -> Bool
&& Word8
push_len Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word8
2 Bool -> Bool -> Bool
&& Word8
push_len Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
40
Bool -> Bool -> Bool
&& Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
push_len Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString -> Int
BS.length ByteString
s
[Word8]
_ -> Bool
False
remote_htlc_sequence :: CommitmentFormat -> Word32
remote_htlc_sequence :: CommitmentFormat -> Word32
remote_htlc_sequence CommitmentFormat
fmt = case CommitmentFormat
fmt of
CommitmentFormat
StaticRemotekey -> Word32
0
CommitmentFormat
Anchors -> Word32
1
htlc_script :: CommitmentKeys -> CommitmentFormat -> HTLC -> Script
htlc_script :: CommitmentKeys -> CommitmentFormat -> HTLC -> Script
htlc_script !CommitmentKeys
keys !CommitmentFormat
fmt !HTLC
h =
let !revpk :: RevocationPubkey
revpk = CommitmentKeys -> RevocationPubkey
ck_revocation_pubkey CommitmentKeys
keys
!remote :: RemoteHtlcPubkey
remote = CommitmentKeys -> RemoteHtlcPubkey
ck_remote_htlc CommitmentKeys
keys
!local :: LocalHtlcPubkey
local = CommitmentKeys -> LocalHtlcPubkey
ck_local_htlc CommitmentKeys
keys
!ph :: PaymentHash
ph = HTLC -> PaymentHash
htlc_payment_hash HTLC
h
in case HTLC -> HTLCDirection
htlc_direction HTLC
h of
HTLCDirection
HTLCOffered -> RevocationPubkey
-> RemoteHtlcPubkey
-> LocalHtlcPubkey
-> PaymentHash
-> CommitmentFormat
-> Script
offered_htlc_script RevocationPubkey
revpk RemoteHtlcPubkey
remote LocalHtlcPubkey
local PaymentHash
ph CommitmentFormat
fmt
HTLCDirection
HTLCReceived ->
RevocationPubkey
-> RemoteHtlcPubkey
-> LocalHtlcPubkey
-> PaymentHash
-> CltvExpiry
-> CommitmentFormat
-> Script
received_htlc_script RevocationPubkey
revpk RemoteHtlcPubkey
remote LocalHtlcPubkey
local PaymentHash
ph
(HTLC -> CltvExpiry
htlc_cltv_expiry HTLC
h) CommitmentFormat
fmt
mk_spending_tx
:: OutPoint
-> Word32
-> Script
-> Satoshi
-> Word32
-> Tx
mk_spending_tx :: OutPoint -> Word32 -> Script -> Satoshi -> Word32 -> Tx
mk_spending_tx !OutPoint
op !Word32
sq (Script ByteString
dest) !Satoshi
value !Word32
locktime = Tx
{ tx_version :: Word32
tx_version = Word32
2
, tx_inputs :: NonEmpty TxIn
tx_inputs = TxIn
{ txin_prevout :: OutPoint
txin_prevout = OutPoint
op
, txin_script_sig :: ByteString
txin_script_sig = ByteString
BS.empty
, txin_sequence :: Word32
txin_sequence = Word32
sq
, txin_witness :: Witness
txin_witness = [ByteString] -> Witness
Witness []
} TxIn -> [TxIn] -> NonEmpty TxIn
forall a. a -> [a] -> NonEmpty a
:| []
, tx_outputs :: NonEmpty TxOut
tx_outputs = TxOut
{ txout_value :: Word64
txout_value = Satoshi -> Word64
BOLT1.un_satoshi Satoshi
value
, txout_script_pubkey :: ByteString
txout_script_pubkey = ByteString
dest
} TxOut -> [TxOut] -> NonEmpty TxOut
forall a. a -> [a] -> NonEmpty a
:| []
, tx_locktime :: Word32
tx_locktime = Word32
locktime
}