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

-- |
-- Module: Lightning.Protocol.BOLT3.Scripts
-- Copyright: (c) 2025 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Scripts and witnesses for BOLT #3 transaction outputs.

module Lightning.Protocol.BOLT3.Scripts (
    -- * Funding output
    funding_script
  , funding_witness

    -- * to_local output
  , to_local_script
  , to_local_witness_spend
  , to_local_witness_revoke

    -- * to_remote output
  , to_remote_witness_script
  , to_remote_script_pubkey
  , to_remote_witness

    -- * Anchor outputs
  , anchor_script
  , anchor_witness_owner
  , anchor_witness_anyone

    -- * Offered HTLC outputs
  , offered_htlc_script
  , offered_htlc_witness_preimage
  , offered_htlc_witness_revoke

    -- * Received HTLC outputs
  , received_htlc_script
  , received_htlc_witness_timeout
  , received_htlc_witness_revoke

    -- * HTLC transaction inputs
  , htlc_success_witness
  , htlc_timeout_witness
  , remote_htlc_sighash

    -- * P2WSH
  , to_p2wsh
  ) where

import Bitcoin.Prim.Tx (Witness(..))
import Bitcoin.Prim.Tx.Sighash (SighashType(..))
import qualified Crypto.Hash.RIPEMD160 as RIPEMD160
import qualified Crypto.Hash.SHA256 as SHA256
import Data.Bits ((.&.), shiftR)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Builder as BSB
import qualified Data.ByteString.Lazy as BSL
import Data.Word (Word8, Word16, Word32)
import Lightning.Protocol.BOLT1 (Point, PaymentPreimage)
import qualified Lightning.Protocol.BOLT1 as BOLT1
import Lightning.Protocol.BOLT3.Types

-- opcodes --------------------------------------------------------------------

op_0, op_1, op_2, op_16 :: Word8
op_0 :: Word8
op_0  = Word8
0x00
op_1 :: Word8
op_1  = Word8
0x51
op_2 :: Word8
op_2  = Word8
0x52
op_16 :: Word8
op_16 = Word8
0x60

op_if, op_notif, op_else, op_endif, op_drop, op_dup, op_ifdup :: Word8
op_if :: Word8
op_if    = Word8
0x63
op_notif :: Word8
op_notif = Word8
0x64
op_else :: Word8
op_else  = Word8
0x67
op_endif :: Word8
op_endif = Word8
0x68
op_ifdup :: Word8
op_ifdup = Word8
0x73
op_drop :: Word8
op_drop  = Word8
0x75
op_dup :: Word8
op_dup   = Word8
0x76

op_swap, op_size, op_equal, op_equalverify, op_hash160 :: Word8
op_swap :: Word8
op_swap        = Word8
0x7c
op_size :: Word8
op_size        = Word8
0x82
op_equal :: Word8
op_equal       = Word8
0x87
op_equalverify :: Word8
op_equalverify = Word8
0x88
op_hash160 :: Word8
op_hash160     = Word8
0xa9

op_checksig, op_checksigverify, op_checkmultisig :: Word8
op_checksig :: Word8
op_checksig       = Word8
0xac
op_checksigverify :: Word8
op_checksigverify = Word8
0xad
op_checkmultisig :: Word8
op_checkmultisig  = Word8
0xae

op_checklocktimeverify, op_checksequenceverify :: Word8
op_checklocktimeverify :: Word8
op_checklocktimeverify = Word8
0xb1
op_checksequenceverify :: Word8
op_checksequenceverify = Word8
0xb2

-- helpers --------------------------------------------------------------------

op :: Word8 -> BSB.Builder
op :: Word8 -> Builder
op = Word8 -> Builder
BSB.word8
{-# INLINE op #-}

-- Push data of at most 75 bytes (all pushes here are keys, hashes or
-- short numbers).
push :: BS.ByteString -> BSB.Builder
push :: ByteString -> Builder
push ByteString
bs = Word8 -> Builder
BSB.word8 (Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
bs)) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
BSB.byteString ByteString
bs
{-# INLINE push #-}

push_point :: Point -> BSB.Builder
push_point :: Point -> Builder
push_point = ByteString -> Builder
push (ByteString -> Builder)
-> (Point -> ByteString) -> Point -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Point -> ByteString
BOLT1.un_point
{-# INLINE push_point #-}

-- Push a non-negative number, minimally encoded.
push_num :: Word32 -> BSB.Builder
push_num :: Word32 -> Builder
push_num Word32
n
  | Word32
n Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Word32
0    = Word8 -> Builder
op Word8
op_0
  | Word32
n Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word32
16   = Word8 -> Builder
op (Word8
0x50 Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word32 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
n)
  | Bool
otherwise = ByteString -> Builder
push ([Word8] -> ByteString
BS.pack ([Word8] -> [Word8]
forall {a}. (Bits a, Num a) => [a] -> [a]
signed (Word32 -> [Word8]
le Word32
n)))
  where
    -- little-endian magnitude
    le :: Word32 -> [Word8]
    le :: Word32 -> [Word8]
le !Word32
x
      | Word32
x Word32 -> Word32 -> Bool
forall a. Eq a => a -> a -> Bool
== Word32
0    = []
      | Bool
otherwise = Word32 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word32
x Word32 -> Word32 -> Word32
forall a. Bits a => a -> a -> a
.&. Word32
0xff) Word8 -> [Word8] -> [Word8]
forall a. a -> [a] -> [a]
: Word32 -> [Word8]
le (Word32
x Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`shiftR` Int
8)

    -- append a sign byte if the top bit is set
    signed :: [a] -> [a]
signed [a]
bs = case [a] -> [a]
forall a. [a] -> [a]
reverse [a]
bs of
      (a
h : [a]
_) | a
h a -> a -> a
forall a. Bits a => a -> a -> a
.&. a
0x80 a -> a -> Bool
forall a. Eq a => a -> a -> Bool
/= a
0 -> [a]
bs [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> [a
0x00]
      [a]
_ -> [a]
bs

push_delay :: Word16 -> BSB.Builder
push_delay :: Word16 -> Builder
push_delay = Word32 -> Builder
push_num (Word32 -> Builder) -> (Word16 -> Word32) -> Word16 -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE push_delay #-}

script :: BSB.Builder -> Script
script :: Builder -> Script
script = ByteString -> Script
Script (ByteString -> Script)
-> (Builder -> ByteString) -> Builder -> Script
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LazyByteString -> ByteString
BSL.toStrict (LazyByteString -> ByteString)
-> (Builder -> LazyByteString) -> Builder -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> LazyByteString
BSB.toLazyByteString
{-# INLINE script #-}

hash160 :: BS.ByteString -> BS.ByteString
hash160 :: ByteString -> ByteString
hash160 = ByteString -> ByteString
RIPEMD160.hash (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
SHA256.hash
{-# INLINE hash160 #-}

-- Append a witness script to a witness stack.
with_script :: [BS.ByteString] -> Script -> Witness
with_script :: [ByteString] -> Script -> Witness
with_script [ByteString]
items (Script ByteString
s) = [ByteString] -> Witness
Witness ([ByteString]
items [ByteString] -> [ByteString] -> [ByteString]
forall a. Semigroup a => a -> a -> a
<> [ByteString
s])
{-# INLINE with_script #-}

-- P2WSH ----------------------------------------------------------------------

-- | The P2WSH scriptPubKey for a witness script:
--   @0 \<SHA256(script)\>@.
to_p2wsh :: Script -> Script
to_p2wsh :: Script -> Script
to_p2wsh (Script ByteString
s) = Builder -> Script
script (Word8 -> Builder
op Word8
op_0 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
push (ByteString -> ByteString
SHA256.hash ByteString
s))

-- funding output -------------------------------------------------------------

-- | The funding output's witness script:
--
--   @2 \<pubkey1\> \<pubkey2\> 2 OP_CHECKMULTISIG@
--
--   where @pubkey1@ is the lexicographically lesser of the two funding
--   pubkeys. The argument order doesn't matter.
funding_script :: FundingPubkey -> FundingPubkey -> Script
funding_script :: FundingPubkey -> FundingPubkey -> Script
funding_script (FundingPubkey Point
a) (FundingPubkey Point
b) =
  let (Point
lo, Point
hi) = if Point
a Point -> Point -> Bool
forall a. Ord a => a -> a -> Bool
<= Point
b then (Point
a, Point
b) else (Point
b, Point
a)
  in  Builder -> Script
script (Builder -> Script) -> Builder -> Script
forall a b. (a -> b) -> a -> b
$
           Word8 -> Builder
op Word8
op_2 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Point -> Builder
push_point Point
lo Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Point -> Builder
push_point Point
hi
        Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_2 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_checkmultisig

-- | The witness spending the funding output, given each party's
--   funding pubkey and signature (DER-encoded, with sighash byte):
--
--   @0 \<signature_for_pubkey1\> \<signature_for_pubkey2\> \<script\>@
--
--   The signatures are ordered to match the pubkeys in
--   'funding_script', so the argument order doesn't matter.
funding_witness
  :: FundingPubkey -> BS.ByteString
  -> FundingPubkey -> BS.ByteString
  -> Witness
funding_witness :: FundingPubkey
-> ByteString -> FundingPubkey -> ByteString -> Witness
funding_witness pa :: FundingPubkey
pa@(FundingPubkey Point
a) ByteString
sa pb :: FundingPubkey
pb@(FundingPubkey Point
b) ByteString
sb =
  let (ByteString
s1, ByteString
s2) = if Point
a Point -> Point -> Bool
forall a. Ord a => a -> a -> Bool
<= Point
b then (ByteString
sa, ByteString
sb) else (ByteString
sb, ByteString
sa)
  in  [ByteString] -> Script -> Witness
with_script [ByteString
BS.empty, ByteString
s1, ByteString
s2] (FundingPubkey -> FundingPubkey -> Script
funding_script FundingPubkey
pa FundingPubkey
pb)

-- to_local output ------------------------------------------------------------

-- | The @to_local@ output's witness script:
--
--   @
--   OP_IF
--       \<revocationpubkey\>
--   OP_ELSE
--       \<to_self_delay\> OP_CHECKSEQUENCEVERIFY OP_DROP
--       \<local_delayedpubkey\>
--   OP_ENDIF
--   OP_CHECKSIG
--   @
--
--   HTLC-success and HTLC-timeout transaction outputs use the same
--   script.
to_local_script
  :: RevocationPubkey
  -> ToSelfDelay
  -> LocalDelayedPubkey
  -> Script
to_local_script :: RevocationPubkey -> ToSelfDelay -> LocalDelayedPubkey -> Script
to_local_script
  (RevocationPubkey Point
rev)
  (ToSelfDelay Word16
delay)
  (LocalDelayedPubkey Point
delayed) = Builder -> Script
script (Builder -> Script) -> Builder -> Script
forall a b. (a -> b) -> a -> b
$
       Word8 -> Builder
op Word8
op_if
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Point -> Builder
push_point Point
rev
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_else
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word16 -> Builder
push_delay Word16
delay
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_checksequenceverify
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_drop
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Point -> Builder
push_point Point
delayed
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_endif
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_checksig

-- | The witness for the owner's delayed spend of a @to_local@ (or
--   HTLC transaction) output, given its witness script:
--
--   @\<local_delayedsig\> \<\> \<script\>@
--
--   The spending input's @nSequence@ must be @to_self_delay@.
to_local_witness_spend :: BS.ByteString -> Script -> Witness
to_local_witness_spend :: ByteString -> Script -> Witness
to_local_witness_spend ByteString
sig = [ByteString] -> Script -> Witness
with_script [ByteString
sig, ByteString
BS.empty]

-- | The witness for the revocation spend of a @to_local@ (or HTLC
--   transaction) output, given its witness script:
--
--   @\<revocation_sig\> 1 \<script\>@
to_local_witness_revoke :: BS.ByteString -> Script -> Witness
to_local_witness_revoke :: ByteString -> Script -> Witness
to_local_witness_revoke ByteString
sig = [ByteString] -> Script -> Witness
with_script [ByteString
sig, Word8 -> ByteString
BS.singleton Word8
0x01]

-- to_remote output -----------------------------------------------------------

-- | The @to_remote@ output's witness script under 'Anchors':
--
--   @\<remotepubkey\> OP_CHECKSIGVERIFY 1 OP_CHECKSEQUENCEVERIFY@
--
--   (Under 'StaticRemotekey' the output is P2WPKH, with no witness
--   script; see 'to_remote_script_pubkey'.)
to_remote_witness_script :: RemotePubkey -> Script
to_remote_witness_script :: RemotePubkey -> Script
to_remote_witness_script (RemotePubkey Point
pk) = Builder -> Script
script (Builder -> Script) -> Builder -> Script
forall a b. (a -> b) -> a -> b
$
     Point -> Builder
push_point Point
pk
  Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_checksigverify
  Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_1
  Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_checksequenceverify

-- | The @to_remote@ output's scriptPubKey: P2WPKH to @remotepubkey@
--   under 'StaticRemotekey', and P2WSH of 'to_remote_witness_script'
--   under 'Anchors'.
to_remote_script_pubkey :: RemotePubkey -> CommitmentFormat -> Script
to_remote_script_pubkey :: RemotePubkey -> CommitmentFormat -> Script
to_remote_script_pubkey pk :: RemotePubkey
pk@(RemotePubkey Point
p) CommitmentFormat
fmt = case CommitmentFormat
fmt of
  CommitmentFormat
StaticRemotekey -> Builder -> Script
script (Word8 -> Builder
op Word8
op_0 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
push (ByteString -> ByteString
hash160 (Point -> ByteString
BOLT1.un_point Point
p)))
  CommitmentFormat
Anchors         -> Script -> Script
to_p2wsh (RemotePubkey -> Script
to_remote_witness_script RemotePubkey
pk)

-- | The witness spending a @to_remote@ output: @\<remote_sig\>
--   \<remotepubkey\>@ (P2WPKH) under 'StaticRemotekey', and
--   @\<remote_sig\> \<script\>@ under 'Anchors', where the spending
--   input's @nSequence@ must be 1.
to_remote_witness
  :: BS.ByteString -> RemotePubkey -> CommitmentFormat -> Witness
to_remote_witness :: ByteString -> RemotePubkey -> CommitmentFormat -> Witness
to_remote_witness ByteString
sig pk :: RemotePubkey
pk@(RemotePubkey Point
p) CommitmentFormat
fmt = case CommitmentFormat
fmt of
  CommitmentFormat
StaticRemotekey -> [ByteString] -> Witness
Witness [ByteString
sig, Point -> ByteString
BOLT1.un_point Point
p]
  CommitmentFormat
Anchors         -> [ByteString] -> Script -> Witness
with_script [ByteString
sig] (RemotePubkey -> Script
to_remote_witness_script RemotePubkey
pk)

-- anchor outputs -------------------------------------------------------------

-- | An anchor output's witness script, for the funding pubkey of the
--   party it belongs to:
--
--   @
--   \<funding_pubkey\> OP_CHECKSIG OP_IFDUP
--   OP_NOTIF
--       OP_16 OP_CHECKSEQUENCEVERIFY
--   OP_ENDIF
--   @
anchor_script :: FundingPubkey -> Script
anchor_script :: FundingPubkey -> Script
anchor_script (FundingPubkey Point
pk) = Builder -> Script
script (Builder -> Script) -> Builder -> Script
forall a b. (a -> b) -> a -> b
$
     Point -> Builder
push_point Point
pk
  Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_checksig
  Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_ifdup
  Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_notif
  Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_16
  Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_checksequenceverify
  Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_endif

-- | The witness for an anchor's owner to spend it:
--   @\<sig\> \<script\>@.
anchor_witness_owner :: BS.ByteString -> FundingPubkey -> Witness
anchor_witness_owner :: ByteString -> FundingPubkey -> Witness
anchor_witness_owner ByteString
sig FundingPubkey
pk = [ByteString] -> Script -> Witness
with_script [ByteString
sig] (FundingPubkey -> Script
anchor_script FundingPubkey
pk)

-- | The witness for anyone to sweep an anchor, 16 blocks after the
--   commitment transaction confirms: @\<\> \<script\>@.
anchor_witness_anyone :: FundingPubkey -> Witness
anchor_witness_anyone :: FundingPubkey -> Witness
anchor_witness_anyone FundingPubkey
pk = [ByteString] -> Script -> Witness
with_script [ByteString
BS.empty] (FundingPubkey -> Script
anchor_script FundingPubkey
pk)

-- offered HTLC outputs -------------------------------------------------------

-- csv suffix for anchors HTLC scripts
htlc_csv :: CommitmentFormat -> BSB.Builder
htlc_csv :: CommitmentFormat -> Builder
htlc_csv CommitmentFormat
fmt = case CommitmentFormat
fmt of
  CommitmentFormat
StaticRemotekey -> Builder
forall a. Monoid a => a
mempty
  CommitmentFormat
Anchors         -> Word8 -> Builder
op Word8
op_1 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_checksequenceverify Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_drop
{-# INLINE htlc_csv #-}

-- | An offered HTLC output's witness script:
--
--   @
--   OP_DUP OP_HASH160 \<RIPEMD160(SHA256(revocationpubkey))\> OP_EQUAL
--   OP_IF
--       OP_CHECKSIG
--   OP_ELSE
--       \<remote_htlcpubkey\> OP_SWAP OP_SIZE 32 OP_EQUAL
--       OP_NOTIF
--           OP_DROP 2 OP_SWAP \<local_htlcpubkey\> 2 OP_CHECKMULTISIG
--       OP_ELSE
--           OP_HASH160 \<RIPEMD160(payment_hash)\> OP_EQUALVERIFY
--           OP_CHECKSIG
--       OP_ENDIF
--   OP_ENDIF
--   @
--
--   Under 'Anchors', @1 OP_CHECKSEQUENCEVERIFY OP_DROP@ precedes the
--   final @OP_ENDIF@.
offered_htlc_script
  :: RevocationPubkey
  -> RemoteHtlcPubkey
  -> LocalHtlcPubkey
  -> BOLT1.PaymentHash
  -> CommitmentFormat
  -> Script
offered_htlc_script :: RevocationPubkey
-> RemoteHtlcPubkey
-> LocalHtlcPubkey
-> PaymentHash
-> CommitmentFormat
-> Script
offered_htlc_script
  (RevocationPubkey Point
rev)
  (RemoteHtlcPubkey Point
remote)
  (LocalHtlcPubkey Point
local)
  PaymentHash
ph
  CommitmentFormat
fmt = Builder -> Script
script (Builder -> Script) -> Builder -> Script
forall a b. (a -> b) -> a -> b
$
       Word8 -> Builder
op Word8
op_dup Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_hash160
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
push (ByteString -> ByteString
hash160 (Point -> ByteString
BOLT1.un_point Point
rev))
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_equal
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_if
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   Word8 -> Builder
op Word8
op_checksig
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_else
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   Point -> Builder
push_point Point
remote Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_swap Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_size
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   ByteString -> Builder
push (Word8 -> ByteString
BS.singleton Word8
32) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_equal
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   Word8 -> Builder
op Word8
op_notif
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>     Word8 -> Builder
op Word8
op_drop Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_2 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_swap
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>     Point -> Builder
push_point Point
local Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_2 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_checkmultisig
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   Word8 -> Builder
op Word8
op_else
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>     Word8 -> Builder
op Word8
op_hash160
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>     ByteString -> Builder
push (ByteString -> ByteString
RIPEMD160.hash (PaymentHash -> ByteString
BOLT1.un_payment_hash PaymentHash
ph))
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>     Word8 -> Builder
op Word8
op_equalverify Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_checksig
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   Word8 -> Builder
op Word8
op_endif
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   CommitmentFormat -> Builder
htlc_csv CommitmentFormat
fmt
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_endif

-- | The witness for the remote party to claim an offered HTLC output
--   with the payment preimage, given its witness script:
--
--   @\<remotehtlcsig\> \<payment_preimage\> \<script\>@
--
--   Under 'Anchors' the spending input's @nSequence@ must be 1.
offered_htlc_witness_preimage
  :: BS.ByteString -> PaymentPreimage -> Script -> Witness
offered_htlc_witness_preimage :: ByteString -> PaymentPreimage -> Script -> Witness
offered_htlc_witness_preimage ByteString
sig PaymentPreimage
pre =
  [ByteString] -> Script -> Witness
with_script [ByteString
sig, PaymentPreimage -> ByteString
BOLT1.un_payment_preimage PaymentPreimage
pre]

-- | The witness for the revocation spend of an offered HTLC output,
--   given its witness script:
--
--   @\<revocation_sig\> \<revocationpubkey\> \<script\>@
offered_htlc_witness_revoke
  :: BS.ByteString -> RevocationPubkey -> Script -> Witness
offered_htlc_witness_revoke :: ByteString -> RevocationPubkey -> Script -> Witness
offered_htlc_witness_revoke ByteString
sig (RevocationPubkey Point
rev) =
  [ByteString] -> Script -> Witness
with_script [ByteString
sig, Point -> ByteString
BOLT1.un_point Point
rev]

-- received HTLC outputs ------------------------------------------------------

-- | A received HTLC output's witness script:
--
--   @
--   OP_DUP OP_HASH160 \<RIPEMD160(SHA256(revocationpubkey))\> OP_EQUAL
--   OP_IF
--       OP_CHECKSIG
--   OP_ELSE
--       \<remote_htlcpubkey\> OP_SWAP OP_SIZE 32 OP_EQUAL
--       OP_IF
--           OP_HASH160 \<RIPEMD160(payment_hash)\> OP_EQUALVERIFY
--           2 OP_SWAP \<local_htlcpubkey\> 2 OP_CHECKMULTISIG
--       OP_ELSE
--           OP_DROP \<cltv_expiry\> OP_CHECKLOCKTIMEVERIFY OP_DROP
--           OP_CHECKSIG
--       OP_ENDIF
--   OP_ENDIF
--   @
--
--   Under 'Anchors', @1 OP_CHECKSEQUENCEVERIFY OP_DROP@ precedes the
--   final @OP_ENDIF@.
received_htlc_script
  :: RevocationPubkey
  -> RemoteHtlcPubkey
  -> LocalHtlcPubkey
  -> BOLT1.PaymentHash
  -> CltvExpiry
  -> CommitmentFormat
  -> Script
received_htlc_script :: RevocationPubkey
-> RemoteHtlcPubkey
-> LocalHtlcPubkey
-> PaymentHash
-> CltvExpiry
-> CommitmentFormat
-> Script
received_htlc_script
  (RevocationPubkey Point
rev)
  (RemoteHtlcPubkey Point
remote)
  (LocalHtlcPubkey Point
local)
  PaymentHash
ph
  (CltvExpiry Word32
expiry)
  CommitmentFormat
fmt = Builder -> Script
script (Builder -> Script) -> Builder -> Script
forall a b. (a -> b) -> a -> b
$
       Word8 -> Builder
op Word8
op_dup Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_hash160
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
push (ByteString -> ByteString
hash160 (Point -> ByteString
BOLT1.un_point Point
rev))
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_equal
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_if
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   Word8 -> Builder
op Word8
op_checksig
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_else
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   Point -> Builder
push_point Point
remote Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_swap Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_size
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   ByteString -> Builder
push (Word8 -> ByteString
BS.singleton Word8
32) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_equal
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   Word8 -> Builder
op Word8
op_if
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>     Word8 -> Builder
op Word8
op_hash160
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>     ByteString -> Builder
push (ByteString -> ByteString
RIPEMD160.hash (PaymentHash -> ByteString
BOLT1.un_payment_hash PaymentHash
ph))
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>     Word8 -> Builder
op Word8
op_equalverify
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>     Word8 -> Builder
op Word8
op_2 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_swap Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Point -> Builder
push_point Point
local
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>     Word8 -> Builder
op Word8
op_2 Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_checkmultisig
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   Word8 -> Builder
op Word8
op_else
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>     Word8 -> Builder
op Word8
op_drop Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word32 -> Builder
push_num Word32
expiry
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>     Word8 -> Builder
op Word8
op_checklocktimeverify Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_drop
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>     Word8 -> Builder
op Word8
op_checksig
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   Word8 -> Builder
op Word8
op_endif
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>   CommitmentFormat -> Builder
htlc_csv CommitmentFormat
fmt
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word8 -> Builder
op Word8
op_endif

-- | The witness for the remote party to time out a received HTLC
--   output, given its witness script:
--
--   @\<remotehtlcsig\> \<\> \<script\>@
--
--   Under 'Anchors' the spending input's @nSequence@ must be 1.
received_htlc_witness_timeout :: BS.ByteString -> Script -> Witness
received_htlc_witness_timeout :: ByteString -> Script -> Witness
received_htlc_witness_timeout ByteString
sig = [ByteString] -> Script -> Witness
with_script [ByteString
sig, ByteString
BS.empty]

-- | The witness for the revocation spend of a received HTLC output,
--   given its witness script:
--
--   @\<revocation_sig\> \<revocationpubkey\> \<script\>@
received_htlc_witness_revoke
  :: BS.ByteString -> RevocationPubkey -> Script -> Witness
received_htlc_witness_revoke :: ByteString -> RevocationPubkey -> Script -> Witness
received_htlc_witness_revoke ByteString
sig (RevocationPubkey Point
rev) =
  [ByteString] -> Script -> Witness
with_script [ByteString
sig, Point -> ByteString
BOLT1.un_point Point
rev]

-- HTLC transaction inputs ----------------------------------------------------

-- | The witness for an HTLC-success transaction's input, given both
--   HTLC signatures, the payment preimage and the received HTLC
--   output's witness script:
--
--   @0 \<remotehtlcsig\> \<localhtlcsig\> \<payment_preimage\> \<script\>@
--
--   See 'remote_htlc_sighash' for the signatures' sighash types.
htlc_success_witness
  :: BS.ByteString    -- ^ remotehtlcsig
  -> BS.ByteString    -- ^ localhtlcsig
  -> PaymentPreimage
  -> Script           -- ^ the received HTLC output's witness script
  -> Witness
htlc_success_witness :: ByteString -> ByteString -> PaymentPreimage -> Script -> Witness
htlc_success_witness ByteString
remote ByteString
local PaymentPreimage
pre =
  [ByteString] -> Script -> Witness
with_script [ByteString
BS.empty, ByteString
remote, ByteString
local, PaymentPreimage -> ByteString
BOLT1.un_payment_preimage PaymentPreimage
pre]

-- | The witness for an HTLC-timeout transaction's input, given both
--   HTLC signatures and the offered HTLC output's witness script:
--
--   @0 \<remotehtlcsig\> \<localhtlcsig\> \<\> \<script\>@
--
--   See 'remote_htlc_sighash' for the signatures' sighash types.
htlc_timeout_witness
  :: BS.ByteString    -- ^ remotehtlcsig
  -> BS.ByteString    -- ^ localhtlcsig
  -> Script           -- ^ the offered HTLC output's witness script
  -> Witness
htlc_timeout_witness :: ByteString -> ByteString -> Script -> Witness
htlc_timeout_witness ByteString
remote ByteString
local =
  [ByteString] -> Script -> Witness
with_script [ByteString
BS.empty, ByteString
remote, ByteString
local, ByteString
BS.empty]

-- | The sighash type of @remotehtlcsig@: the signature on an
--   HTLC-success or HTLC-timeout transaction that the commitment's
--   non-owner sends in @commitment_signed@.
--
--   Under 'Anchors' it is @SIGHASH_SINGLE|SIGHASH_ANYONECANPAY@, so
--   that the owner can add inputs and outputs to pay the (otherwise
--   zero) fee; under 'StaticRemotekey' it is @SIGHASH_ALL@. The
--   owner's own signature, @localhtlcsig@, always uses @SIGHASH_ALL@.
--
--   >>> remote_htlc_sighash Anchors
--   SIGHASH_SINGLE_ANYONECANPAY
remote_htlc_sighash :: CommitmentFormat -> SighashType
remote_htlc_sighash :: CommitmentFormat -> SighashType
remote_htlc_sighash CommitmentFormat
fmt = case CommitmentFormat
fmt of
  CommitmentFormat
StaticRemotekey -> SighashType
SIGHASH_ALL
  CommitmentFormat
Anchors         -> SighashType
SIGHASH_SINGLE_ANYONECANPAY