{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE BangPatterns #-}

-- |
-- Module: Lightning.Protocol.BOLT5.Spend
-- Copyright: (c) 2025 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Spending transaction construction for BOLT #5 on-chain
-- transaction handling.
--
-- All functions produce unsigned t'SpendingTx' values. The caller
-- signs (using the sighash metadata provided) and assembles the
-- witness with bolt3's witness functions.

module Lightning.Protocol.BOLT5.Spend (
    -- * Local commitment spends
    spend_to_local
  , spend_htlc_tx
  , spend_htlc_output

    -- * Remote commitment spends
  , spend_remote_htlc_timeout
  , spend_remote_htlc_preimage

    -- * Revoked commitment spends
  , spend_revoked_to_local
  , spend_revoked_htlc
  , spend_revoked_htlc_output

    -- * Anchor spends
  , 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

-- local commitment spends --------------------------------------------

-- | Spend the to_local output of our local commitment tx.
--
--   The input's nSequence is the to_self_delay, so the transaction
--   is valid only once the delay has passed. The caller signs with
--   the local delayed privkey and uses bolt3's
--   'to_local_witness_spend'.
--
--   Returns 'Nothing' if the fee would exceed the output value, or
--   leave an output below the destination's dust threshold.
spend_to_local
  :: OutPoint
  -- ^ Outpoint of the to_local output.
  -> Satoshi
  -- ^ Value of the to_local output.
  -> RevocationPubkey
  -> ToSelfDelay
  -> LocalDelayedPubkey
  -> Script
  -- ^ Destination scriptPubKey.
  -> 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

-- | Build the HTLC-timeout (for an offered HTLC) or HTLC-success
--   (for a received HTLC) transaction spending an HTLC output of
--   our local commitment, with bolt3's 'build_htlc_tx'.
--
--   The sighash type is that of the local signature, which is
--   always @SIGHASH_ALL@. The remote's @htlc_signature@ uses bolt3's
--   'remote_htlc_sighash' (@SIGHASH_SINGLE|SIGHASH_ANYONECANPAY@
--   under 'Anchors'). The witness is bolt3's 'htlc_timeout_witness'
--   or 'htlc_success_witness'.
--
--   Under 'Anchors' the transaction pays no fee; it must be
--   combined with other inputs before broadcast.
--
--   Returns 'Nothing' if bolt3 can't build the transaction (the fee
--   exceeds the HTLC amount).
spend_htlc_tx
  :: HTLCContext
  -> CommitmentKeys
  -- ^ Keys for our local commitment (for the witness script).
  -> 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 the output of an HTLC-timeout or HTLC-success
--   transaction after the CSV delay.
--
--   The output uses the to_local script, so this is
--   'spend_to_local'. The caller signs with the local delayed
--   privkey and uses bolt3's 'to_local_witness_spend'.
spend_htlc_output
  :: OutPoint
  -- ^ Outpoint of the second-stage output.
  -> Satoshi
  -- ^ Value of the second-stage output.
  -> RevocationPubkey
  -> ToSelfDelay
  -> LocalDelayedPubkey
  -> Script
  -- ^ Destination scriptPubKey.
  -> 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

-- remote commitment spends -------------------------------------------

-- | Spend an HTLC we offered directly, after it times out, from the
--   remote commitment (where it is a received HTLC).
--
--   The locktime is the HTLC's CLTV expiry. The caller signs with
--   our HTLC privkey and uses bolt3's
--   'received_htlc_witness_timeout'.
--
--   Returns 'Nothing' if the fee would exceed the output value, or
--   leave an output below the destination's dust threshold.
spend_remote_htlc_timeout
  :: OutPoint
  -- ^ Outpoint of the HTLC output.
  -> Satoshi
  -- ^ Value of the HTLC output.
  -> HTLC
  -- ^ The HTLC being spent.
  -> CommitmentKeys
  -- ^ Keys for the remote commitment.
  -> CommitmentFormat
  -> Script
  -- ^ Destination scriptPubKey.
  -> 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 an HTLC we received directly with the preimage, from the
--   remote commitment (where it is an offered HTLC).
--
--   The caller signs with our HTLC privkey and uses bolt3's
--   'offered_htlc_witness_preimage'.
--
--   Returns 'Nothing' if the fee would exceed the output value, or
--   leave an output below the destination's dust threshold.
spend_remote_htlc_preimage
  :: OutPoint
  -- ^ Outpoint of the HTLC output.
  -> Satoshi
  -- ^ Value of the HTLC output.
  -> HTLC
  -- ^ The HTLC being spent.
  -> CommitmentKeys
  -- ^ Keys for the remote commitment.
  -> CommitmentFormat
  -> Script
  -- ^ Destination scriptPubKey.
  -> 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

-- revoked commitment spends ------------------------------------------

-- | Spend a revoked to_local output with the revocation key.
--
--   The caller signs with the revocation privkey and uses bolt3's
--   'to_local_witness_revoke'.
--
--   Returns 'Nothing' if the fee would exceed the output value, or
--   leave an output below the destination's dust threshold.
spend_revoked_to_local
  :: OutPoint
  -- ^ Outpoint of the to_local output.
  -> Satoshi
  -- ^ Value of the to_local output.
  -> RevocationPubkey
  -> ToSelfDelay
  -> LocalDelayedPubkey
  -> Script
  -- ^ Destination scriptPubKey.
  -> 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 a revoked HTLC output with the revocation key.
--
--   The caller signs with the revocation privkey and uses bolt3's
--   'offered_htlc_witness_revoke' or 'received_htlc_witness_revoke',
--   according to the output type.
--
--   Returns 'Nothing' if the fee would exceed the output value, or
--   leave an output below the destination's dust threshold.
spend_revoked_htlc
  :: OutPoint
  -- ^ Outpoint of the HTLC output.
  -> Satoshi
  -- ^ Value of the HTLC output.
  -> HTLCOutputType
  -- ^ Whether offered or received HTLC.
  -> RevocationPubkey
  -> CommitmentKeys
  -> CommitmentFormat
  -> PaymentHash
  -> Script
  -- ^ Destination scriptPubKey.
  -> 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 the output of a revoked HTLC-timeout or HTLC-success
--   transaction with the revocation key.
--
--   The output uses the to_local script, so this is
--   'spend_revoked_to_local'. The caller signs with the revocation
--   privkey and uses bolt3's 'to_local_witness_revoke'.
spend_revoked_htlc_output
  :: OutPoint
  -- ^ Outpoint of the second-stage output.
  -> Satoshi
  -- ^ Value of the second-stage output.
  -> RevocationPubkey
  -> ToSelfDelay
  -> LocalDelayedPubkey
  -> Script
  -- ^ Destination scriptPubKey.
  -> 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

-- anchor spends ------------------------------------------------------

-- | Spend an anchor output as its owner (immediately).
--
--   The transaction pays no fee and has a single input: it must be
--   combined with other inputs before broadcast. The caller signs
--   with the funding privkey and uses bolt3's
--   'anchor_witness_owner'.
spend_anchor_owner
  :: OutPoint
  -- ^ Outpoint of the anchor output.
  -> Satoshi
  -- ^ Value of the anchor output (330 sats).
  -> FundingPubkey
  -> Script
  -- ^ Destination scriptPubKey.
  -> 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 an anchor output as anyone (16 blocks after the
--   commitment confirms).
--
--   As 'spend_anchor_owner', the transaction pays no fee. The
--   witness is bolt3's 'anchor_witness_anyone', which needs no
--   signature.
spend_anchor_anyone
  :: OutPoint
  -- ^ Outpoint of the anchor output.
  -> Satoshi
  -- ^ Value of the anchor output (330 sats).
  -> FundingPubkey
  -> Script
  -- ^ Destination scriptPubKey.
  -> 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

-- internal helpers ---------------------------------------------------

-- | A single-input, single-output sweep paying the input value, less
--   the fee for the given input weight, to the destination.
sweep
  :: OutPoint
  -> Satoshi      -- ^ input value
  -> Script       -- ^ input witness script
  -> Word64       -- ^ input weight
  -> Word32       -- ^ input nSequence
  -> Word32       -- ^ locktime
  -> Script       -- ^ destination scriptPubKey
  -> 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)

-- | Value of a sweep's single output: the input value less the fee.
--
--   'Nothing' if the fee exceeds the input value, or if the output
--   would be below the dust threshold of its script.
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, in satoshis, for an output paying to a script.
--
--   As Bitcoin Core computes it for spendable outputs at the default
--   dust relay feerate of 3 sat/vbyte: three times the size of the
--   output plus that of an input spending it (67 vbytes for a
--   witness program, 148 otherwise). This gives 546 sats for P2PKH,
--   540 for P2SH, 294 for P2WPKH, and 330 for P2WSH and P2TR.
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)

-- | Whether a script is a segwit witness program: a version opcode
--   (OP_0 or OP_1 to OP_16) followed by a single 2-40 byte push.
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

-- | The nSequence of a direct HTLC spend from the remote commitment:
--   1 under 'Anchors' (for the HTLC scripts' CSV), 0 otherwise.
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

-- | The witness script of an HTLC output of a commitment with the
--   given keys.
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

-- | Build a version 2, single-input, single-output transaction.
mk_spending_tx
  :: OutPoint     -- ^ input outpoint
  -> Word32       -- ^ input nSequence
  -> Script       -- ^ output scriptPubKey
  -> Satoshi      -- ^ output value
  -> Word32       -- ^ locktime
  -> 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
  }