{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
module Lightning.Protocol.BOLT3.Tx (
CommitmentContext(..)
, CommitmentTx(..)
, CommitmentOutput(..)
, OutputType(..)
, build_commitment_tx
, HTLCContext(..)
, HTLCTx(..)
, build_htlc_tx
, ClosingTx(..)
, ClosingOutput(..)
, ClosingContext(..)
, ClosingOutputs(..)
, build_closing_tx
, LegacyClosingContext(..)
, build_legacy_closing_tx
, commitment_fee
, htlc_timeout_fee
, htlc_success_fee
, htlc_trim_threshold
, is_trimmed
, commitment_to_tx
, htlc_to_tx
, closing_to_tx
, encode_commitment_tx
, encode_htlc_tx
, encode_closing_tx
) where
import Bitcoin.Prim.Tx (OutPoint(..), TxId)
import qualified Bitcoin.Prim.Tx as BT
import Control.DeepSeq (NFData)
import Data.Bits ((.&.), (.|.), shiftR)
import qualified Data.ByteString as BS
import Data.List (sortBy)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.List.NonEmpty as NE
import Data.Maybe (fromMaybe)
import Data.Word (Word32, Word64)
import GHC.Generics (Generic)
import Lightning.Protocol.BOLT1 (Satoshi, MilliSatoshi)
import qualified Lightning.Protocol.BOLT1 as BOLT1
import Lightning.Protocol.BOLT3.Keys
import Lightning.Protocol.BOLT3.Scripts
import Lightning.Protocol.BOLT3.Types
data OutputType
= OutputToLocal
| OutputToRemote
| OutputLocalAnchor
| OutputRemoteAnchor
| OutputHTLC !HTLC
deriving (OutputType -> OutputType -> Bool
(OutputType -> OutputType -> Bool)
-> (OutputType -> OutputType -> Bool) -> Eq OutputType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OutputType -> OutputType -> Bool
== :: OutputType -> OutputType -> Bool
$c/= :: OutputType -> OutputType -> Bool
/= :: OutputType -> OutputType -> Bool
Eq, Int -> OutputType -> ShowS
[OutputType] -> ShowS
OutputType -> String
(Int -> OutputType -> ShowS)
-> (OutputType -> String)
-> ([OutputType] -> ShowS)
-> Show OutputType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OutputType -> ShowS
showsPrec :: Int -> OutputType -> ShowS
$cshow :: OutputType -> String
show :: OutputType -> String
$cshowList :: [OutputType] -> ShowS
showList :: [OutputType] -> ShowS
Show, (forall x. OutputType -> Rep OutputType x)
-> (forall x. Rep OutputType x -> OutputType) -> Generic OutputType
forall x. Rep OutputType x -> OutputType
forall x. OutputType -> Rep OutputType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. OutputType -> Rep OutputType x
from :: forall x. OutputType -> Rep OutputType x
$cto :: forall x. Rep OutputType x -> OutputType
to :: forall x. Rep OutputType x -> OutputType
Generic)
instance NFData OutputType
data CommitmentOutput = CommitmentOutput
{ CommitmentOutput -> Satoshi
co_value :: {-# UNPACK #-} !Satoshi
, CommitmentOutput -> Script
co_script :: !Script
, CommitmentOutput -> OutputType
co_type :: !OutputType
} deriving (CommitmentOutput -> CommitmentOutput -> Bool
(CommitmentOutput -> CommitmentOutput -> Bool)
-> (CommitmentOutput -> CommitmentOutput -> Bool)
-> Eq CommitmentOutput
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CommitmentOutput -> CommitmentOutput -> Bool
== :: CommitmentOutput -> CommitmentOutput -> Bool
$c/= :: CommitmentOutput -> CommitmentOutput -> Bool
/= :: CommitmentOutput -> CommitmentOutput -> Bool
Eq, Int -> CommitmentOutput -> ShowS
[CommitmentOutput] -> ShowS
CommitmentOutput -> String
(Int -> CommitmentOutput -> ShowS)
-> (CommitmentOutput -> String)
-> ([CommitmentOutput] -> ShowS)
-> Show CommitmentOutput
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CommitmentOutput -> ShowS
showsPrec :: Int -> CommitmentOutput -> ShowS
$cshow :: CommitmentOutput -> String
show :: CommitmentOutput -> String
$cshowList :: [CommitmentOutput] -> ShowS
showList :: [CommitmentOutput] -> ShowS
Show, (forall x. CommitmentOutput -> Rep CommitmentOutput x)
-> (forall x. Rep CommitmentOutput x -> CommitmentOutput)
-> Generic CommitmentOutput
forall x. Rep CommitmentOutput x -> CommitmentOutput
forall x. CommitmentOutput -> Rep CommitmentOutput x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CommitmentOutput -> Rep CommitmentOutput x
from :: forall x. CommitmentOutput -> Rep CommitmentOutput x
$cto :: forall x. Rep CommitmentOutput x -> CommitmentOutput
to :: forall x. Rep CommitmentOutput x -> CommitmentOutput
Generic)
instance NFData CommitmentOutput
data CommitmentContext = CommitmentContext
{ CommitmentContext -> OutPoint
cc_funding_outpoint :: {-# UNPACK #-} !OutPoint
, CommitmentContext -> CommitmentNumber
cc_commitment_number :: {-# UNPACK #-} !CommitmentNumber
, CommitmentContext -> PaymentBasepoint
cc_local_payment_bp :: !PaymentBasepoint
, CommitmentContext -> PaymentBasepoint
cc_remote_payment_bp :: !PaymentBasepoint
, CommitmentContext -> ToSelfDelay
cc_to_self_delay :: {-# UNPACK #-} !ToSelfDelay
, CommitmentContext -> DustLimit
cc_dust_limit :: {-# UNPACK #-} !DustLimit
, CommitmentContext -> FeeratePerKw
cc_feerate :: {-# UNPACK #-} !FeeratePerKw
, CommitmentContext -> CommitmentFormat
cc_format :: !CommitmentFormat
, CommitmentContext -> Bool
cc_is_funder :: !Bool
, CommitmentContext -> MilliSatoshi
cc_to_local_msat :: {-# UNPACK #-} !MilliSatoshi
, CommitmentContext -> MilliSatoshi
cc_to_remote_msat :: {-# UNPACK #-} !MilliSatoshi
, CommitmentContext -> [HTLC]
cc_htlcs :: ![HTLC]
, CommitmentContext -> CommitmentKeys
cc_keys :: !CommitmentKeys
} deriving (CommitmentContext -> CommitmentContext -> Bool
(CommitmentContext -> CommitmentContext -> Bool)
-> (CommitmentContext -> CommitmentContext -> Bool)
-> Eq CommitmentContext
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CommitmentContext -> CommitmentContext -> Bool
== :: CommitmentContext -> CommitmentContext -> Bool
$c/= :: CommitmentContext -> CommitmentContext -> Bool
/= :: CommitmentContext -> CommitmentContext -> Bool
Eq, Int -> CommitmentContext -> ShowS
[CommitmentContext] -> ShowS
CommitmentContext -> String
(Int -> CommitmentContext -> ShowS)
-> (CommitmentContext -> String)
-> ([CommitmentContext] -> ShowS)
-> Show CommitmentContext
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CommitmentContext -> ShowS
showsPrec :: Int -> CommitmentContext -> ShowS
$cshow :: CommitmentContext -> String
show :: CommitmentContext -> String
$cshowList :: [CommitmentContext] -> ShowS
showList :: [CommitmentContext] -> ShowS
Show, (forall x. CommitmentContext -> Rep CommitmentContext x)
-> (forall x. Rep CommitmentContext x -> CommitmentContext)
-> Generic CommitmentContext
forall x. Rep CommitmentContext x -> CommitmentContext
forall x. CommitmentContext -> Rep CommitmentContext x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CommitmentContext -> Rep CommitmentContext x
from :: forall x. CommitmentContext -> Rep CommitmentContext x
$cto :: forall x. Rep CommitmentContext x -> CommitmentContext
to :: forall x. Rep CommitmentContext x -> CommitmentContext
Generic)
instance NFData CommitmentContext
data CommitmentTx = CommitmentTx
{ CommitmentTx -> Word32
ctx_version :: {-# UNPACK #-} !Word32
, CommitmentTx -> Locktime
ctx_locktime :: {-# UNPACK #-} !Locktime
, CommitmentTx -> OutPoint
ctx_input_outpoint :: {-# UNPACK #-} !OutPoint
, CommitmentTx -> Sequence
ctx_input_sequence :: {-# UNPACK #-} !Sequence
, CommitmentTx -> NonEmpty CommitmentOutput
ctx_outputs :: !(NonEmpty CommitmentOutput)
, CommitmentTx -> Script
ctx_funding_script :: !Script
} deriving (CommitmentTx -> CommitmentTx -> Bool
(CommitmentTx -> CommitmentTx -> Bool)
-> (CommitmentTx -> CommitmentTx -> Bool) -> Eq CommitmentTx
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CommitmentTx -> CommitmentTx -> Bool
== :: CommitmentTx -> CommitmentTx -> Bool
$c/= :: CommitmentTx -> CommitmentTx -> Bool
/= :: CommitmentTx -> CommitmentTx -> Bool
Eq, Int -> CommitmentTx -> ShowS
[CommitmentTx] -> ShowS
CommitmentTx -> String
(Int -> CommitmentTx -> ShowS)
-> (CommitmentTx -> String)
-> ([CommitmentTx] -> ShowS)
-> Show CommitmentTx
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CommitmentTx -> ShowS
showsPrec :: Int -> CommitmentTx -> ShowS
$cshow :: CommitmentTx -> String
show :: CommitmentTx -> String
$cshowList :: [CommitmentTx] -> ShowS
showList :: [CommitmentTx] -> ShowS
Show, (forall x. CommitmentTx -> Rep CommitmentTx x)
-> (forall x. Rep CommitmentTx x -> CommitmentTx)
-> Generic CommitmentTx
forall x. Rep CommitmentTx x -> CommitmentTx
forall x. CommitmentTx -> Rep CommitmentTx x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CommitmentTx -> Rep CommitmentTx x
from :: forall x. CommitmentTx -> Rep CommitmentTx x
$cto :: forall x. Rep CommitmentTx x -> CommitmentTx
to :: forall x. Rep CommitmentTx x -> CommitmentTx
Generic)
instance NFData CommitmentTx
build_commitment_tx :: CommitmentContext -> Maybe CommitmentTx
build_commitment_tx :: CommitmentContext -> Maybe CommitmentTx
build_commitment_tx CommitmentContext
ctx = do
outputs <- [CommitmentOutput] -> Maybe (NonEmpty CommitmentOutput)
forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty ([CommitmentOutput] -> Maybe (NonEmpty CommitmentOutput))
-> ([CommitmentOutput] -> [CommitmentOutput])
-> [CommitmentOutput]
-> Maybe (NonEmpty CommitmentOutput)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [CommitmentOutput] -> [CommitmentOutput]
sort_outputs ([CommitmentOutput] -> Maybe (NonEmpty CommitmentOutput))
-> [CommitmentOutput] -> Maybe (NonEmpty CommitmentOutput)
forall a b. (a -> b) -> a -> b
$
[CommitmentOutput]
to_local [CommitmentOutput] -> [CommitmentOutput] -> [CommitmentOutput]
forall a. Semigroup a => a -> a -> a
<> [CommitmentOutput]
to_remote [CommitmentOutput] -> [CommitmentOutput] -> [CommitmentOutput]
forall a. Semigroup a => a -> a -> a
<> [CommitmentOutput]
local_anchor [CommitmentOutput] -> [CommitmentOutput] -> [CommitmentOutput]
forall a. Semigroup a => a -> a -> a
<> [CommitmentOutput]
remote_anchor [CommitmentOutput] -> [CommitmentOutput] -> [CommitmentOutput]
forall a. Semigroup a => a -> a -> a
<> [CommitmentOutput]
htlcs
pure CommitmentTx
{ ctx_version = 2
, ctx_locktime =
Locktime (0x20000000 .|. (fromIntegral obscured .&. 0xFFFFFF))
, ctx_input_outpoint = cc_funding_outpoint ctx
, ctx_input_sequence =
Sequence
(0x80000000 .|. (fromIntegral (obscured `shiftR` 24) .&. 0xFFFFFF))
, ctx_outputs = outputs
, ctx_funding_script =
funding_script (ck_local_funding keys) (ck_remote_funding keys)
}
where
!keys :: CommitmentKeys
keys = CommitmentContext -> CommitmentKeys
cc_keys CommitmentContext
ctx
!fmt :: CommitmentFormat
fmt = CommitmentContext -> CommitmentFormat
cc_format CommitmentContext
ctx
!rate :: FeeratePerKw
rate = CommitmentContext -> FeeratePerKw
cc_feerate CommitmentContext
ctx
!dust :: DustLimit
dust = CommitmentContext -> DustLimit
cc_dust_limit CommitmentContext
ctx
DustLimit Satoshi
dust_sat = DustLimit
dust
(!PaymentBasepoint
opener, !PaymentBasepoint
accepter)
| CommitmentContext -> Bool
cc_is_funder CommitmentContext
ctx = (CommitmentContext -> PaymentBasepoint
cc_local_payment_bp CommitmentContext
ctx, CommitmentContext -> PaymentBasepoint
cc_remote_payment_bp CommitmentContext
ctx)
| Bool
otherwise = (CommitmentContext -> PaymentBasepoint
cc_remote_payment_bp CommitmentContext
ctx, CommitmentContext -> PaymentBasepoint
cc_local_payment_bp CommitmentContext
ctx)
!obscured :: Word64
obscured =
PaymentBasepoint -> PaymentBasepoint -> CommitmentNumber -> Word64
obscured_commitment_number PaymentBasepoint
opener PaymentBasepoint
accepter (CommitmentContext -> CommitmentNumber
cc_commitment_number CommitmentContext
ctx)
untrimmed :: [HTLC]
untrimmed = (HTLC -> Bool) -> [HTLC] -> [HTLC]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (HTLC -> Bool) -> HTLC -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DustLimit -> FeeratePerKw -> CommitmentFormat -> HTLC -> Bool
is_trimmed DustLimit
dust FeeratePerKw
rate CommitmentFormat
fmt) (CommitmentContext -> [HTLC]
cc_htlcs CommitmentContext
ctx)
!fee :: Satoshi
fee = FeeratePerKw -> CommitmentFormat -> Word64 -> Satoshi
commitment_fee FeeratePerKw
rate CommitmentFormat
fmt (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([HTLC] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [HTLC]
untrimmed))
!deduction :: Satoshi
deduction = case CommitmentFormat
fmt of
CommitmentFormat
StaticRemotekey -> Satoshi
fee
CommitmentFormat
Anchors ->
Satoshi -> Maybe Satoshi -> Satoshi
forall a. a -> Maybe a -> a
fromMaybe Satoshi
BOLT1.max_satoshi (Satoshi -> Satoshi -> Maybe Satoshi
BOLT1.add_sat Satoshi
fee (Word64 -> Satoshi
sat Word64
660))
deduct :: Satoshi -> Satoshi
deduct Satoshi
s = Satoshi -> Maybe Satoshi -> Satoshi
forall a. a -> Maybe a -> a
fromMaybe (Word64 -> Satoshi
sat Word64
0) (Satoshi -> Satoshi -> Maybe Satoshi
BOLT1.sub_sat Satoshi
s Satoshi
deduction)
!local_sat :: Satoshi
local_sat = MilliSatoshi -> Satoshi
BOLT1.msat_to_sat (CommitmentContext -> MilliSatoshi
cc_to_local_msat CommitmentContext
ctx)
!remote_sat :: Satoshi
remote_sat = MilliSatoshi -> Satoshi
BOLT1.msat_to_sat (CommitmentContext -> MilliSatoshi
cc_to_remote_msat CommitmentContext
ctx)
(!Satoshi
to_local_sat, !Satoshi
to_remote_sat)
| CommitmentContext -> Bool
cc_is_funder CommitmentContext
ctx = (Satoshi -> Satoshi
deduct Satoshi
local_sat, Satoshi
remote_sat)
| Bool
otherwise = (Satoshi
local_sat, Satoshi -> Satoshi
deduct Satoshi
remote_sat)
to_local :: [CommitmentOutput]
to_local =
[ Satoshi -> Script -> OutputType -> CommitmentOutput
CommitmentOutput Satoshi
to_local_sat
(Script -> Script
to_p2wsh (RevocationPubkey -> ToSelfDelay -> LocalDelayedPubkey -> Script
to_local_script (CommitmentKeys -> RevocationPubkey
ck_revocation_pubkey CommitmentKeys
keys)
(CommitmentContext -> ToSelfDelay
cc_to_self_delay CommitmentContext
ctx) (CommitmentKeys -> LocalDelayedPubkey
ck_local_delayed CommitmentKeys
keys)))
OutputType
OutputToLocal
| Satoshi
to_local_sat Satoshi -> Satoshi -> Bool
forall a. Ord a => a -> a -> Bool
>= Satoshi
dust_sat ]
to_remote :: [CommitmentOutput]
to_remote =
[ Satoshi -> Script -> OutputType -> CommitmentOutput
CommitmentOutput Satoshi
to_remote_sat
(RemotePubkey -> CommitmentFormat -> Script
to_remote_script_pubkey (CommitmentKeys -> RemotePubkey
ck_remote_payment CommitmentKeys
keys) CommitmentFormat
fmt)
OutputType
OutputToRemote
| Satoshi
to_remote_sat Satoshi -> Satoshi -> Bool
forall a. Ord a => a -> a -> Bool
>= Satoshi
dust_sat ]
has_htlcs :: Bool
has_htlcs = Bool -> Bool
not ([HTLC] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [HTLC]
untrimmed)
anchor :: FundingPubkey -> OutputType -> CommitmentOutput
anchor FundingPubkey
pk OutputType
ty = Satoshi -> Script -> OutputType -> CommitmentOutput
CommitmentOutput Satoshi
anchor_output_value
(Script -> Script
to_p2wsh (FundingPubkey -> Script
anchor_script FundingPubkey
pk)) OutputType
ty
local_anchor :: [CommitmentOutput]
local_anchor = case CommitmentFormat
fmt of
CommitmentFormat
Anchors | Bool -> Bool
not ([CommitmentOutput] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [CommitmentOutput]
to_local) Bool -> Bool -> Bool
|| Bool
has_htlcs ->
[FundingPubkey -> OutputType -> CommitmentOutput
anchor (CommitmentKeys -> FundingPubkey
ck_local_funding CommitmentKeys
keys) OutputType
OutputLocalAnchor]
CommitmentFormat
_ -> []
remote_anchor :: [CommitmentOutput]
remote_anchor = case CommitmentFormat
fmt of
CommitmentFormat
Anchors | Bool -> Bool
not ([CommitmentOutput] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [CommitmentOutput]
to_remote) Bool -> Bool -> Bool
|| Bool
has_htlcs ->
[FundingPubkey -> OutputType -> CommitmentOutput
anchor (CommitmentKeys -> FundingPubkey
ck_remote_funding CommitmentKeys
keys) OutputType
OutputRemoteAnchor]
CommitmentFormat
_ -> []
htlcs :: [CommitmentOutput]
htlcs = (HTLC -> CommitmentOutput) -> [HTLC] -> [CommitmentOutput]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap HTLC -> CommitmentOutput
htlc_output [HTLC]
untrimmed
htlc_output :: HTLC -> CommitmentOutput
htlc_output HTLC
h =
let !script :: Script
script = case HTLC -> HTLCDirection
htlc_direction HTLC
h of
HTLCDirection
HTLCOffered -> 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
h) CommitmentFormat
fmt
HTLCDirection
HTLCReceived -> 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
h)
(HTLC -> CltvExpiry
htlc_cltv_expiry HTLC
h) CommitmentFormat
fmt
in Satoshi -> Script -> OutputType -> CommitmentOutput
CommitmentOutput (MilliSatoshi -> Satoshi
BOLT1.msat_to_sat (HTLC -> MilliSatoshi
htlc_amount_msat HTLC
h))
(Script -> Script
to_p2wsh Script
script) (HTLC -> OutputType
OutputHTLC HTLC
h)
sort_outputs :: [CommitmentOutput] -> [CommitmentOutput]
sort_outputs :: [CommitmentOutput] -> [CommitmentOutput]
sort_outputs = (CommitmentOutput -> CommitmentOutput -> Ordering)
-> [CommitmentOutput] -> [CommitmentOutput]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy CommitmentOutput -> CommitmentOutput -> Ordering
cmp where
cmp :: CommitmentOutput -> CommitmentOutput -> Ordering
cmp CommitmentOutput
a CommitmentOutput
b = Satoshi -> Satoshi -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (CommitmentOutput -> Satoshi
co_value CommitmentOutput
a) (CommitmentOutput -> Satoshi
co_value CommitmentOutput
b)
Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> Script -> Script -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (CommitmentOutput -> Script
co_script CommitmentOutput
a) (CommitmentOutput -> Script
co_script CommitmentOutput
b)
Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> Maybe CltvExpiry -> Maybe CltvExpiry -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (OutputType -> Maybe CltvExpiry
cltv (CommitmentOutput -> OutputType
co_type CommitmentOutput
a)) (OutputType -> Maybe CltvExpiry
cltv (CommitmentOutput -> OutputType
co_type CommitmentOutput
b))
cltv :: OutputType -> Maybe CltvExpiry
cltv OutputType
t = case OutputType
t of
OutputHTLC HTLC
h -> CltvExpiry -> Maybe CltvExpiry
forall a. a -> Maybe a
Just (HTLC -> CltvExpiry
htlc_cltv_expiry HTLC
h)
OutputType
_ -> Maybe CltvExpiry
forall a. Maybe a
Nothing
data HTLCContext = HTLCContext
{ HTLCContext -> TxId
hc_commitment_txid :: !TxId
, HTLCContext -> Word32
hc_output_index :: {-# UNPACK #-} !Word32
, HTLCContext -> HTLC
hc_htlc :: !HTLC
, HTLCContext -> ToSelfDelay
hc_to_self_delay :: {-# UNPACK #-} !ToSelfDelay
, HTLCContext -> FeeratePerKw
hc_feerate :: {-# UNPACK #-} !FeeratePerKw
, HTLCContext -> CommitmentFormat
hc_format :: !CommitmentFormat
, HTLCContext -> RevocationPubkey
hc_revocation_pubkey :: !RevocationPubkey
, HTLCContext -> LocalDelayedPubkey
hc_local_delayed :: !LocalDelayedPubkey
} deriving (HTLCContext -> HTLCContext -> Bool
(HTLCContext -> HTLCContext -> Bool)
-> (HTLCContext -> HTLCContext -> Bool) -> Eq HTLCContext
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HTLCContext -> HTLCContext -> Bool
== :: HTLCContext -> HTLCContext -> Bool
$c/= :: HTLCContext -> HTLCContext -> Bool
/= :: HTLCContext -> HTLCContext -> Bool
Eq, Int -> HTLCContext -> ShowS
[HTLCContext] -> ShowS
HTLCContext -> String
(Int -> HTLCContext -> ShowS)
-> (HTLCContext -> String)
-> ([HTLCContext] -> ShowS)
-> Show HTLCContext
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HTLCContext -> ShowS
showsPrec :: Int -> HTLCContext -> ShowS
$cshow :: HTLCContext -> String
show :: HTLCContext -> String
$cshowList :: [HTLCContext] -> ShowS
showList :: [HTLCContext] -> ShowS
Show, (forall x. HTLCContext -> Rep HTLCContext x)
-> (forall x. Rep HTLCContext x -> HTLCContext)
-> Generic HTLCContext
forall x. Rep HTLCContext x -> HTLCContext
forall x. HTLCContext -> Rep HTLCContext x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HTLCContext -> Rep HTLCContext x
from :: forall x. HTLCContext -> Rep HTLCContext x
$cto :: forall x. Rep HTLCContext x -> HTLCContext
to :: forall x. Rep HTLCContext x -> HTLCContext
Generic)
instance NFData HTLCContext
data HTLCTx = HTLCTx
{ HTLCTx -> Word32
htx_version :: {-# UNPACK #-} !Word32
, HTLCTx -> Locktime
htx_locktime :: {-# UNPACK #-} !Locktime
, HTLCTx -> OutPoint
htx_input_outpoint :: {-# UNPACK #-} !OutPoint
, HTLCTx -> Sequence
htx_input_sequence :: {-# UNPACK #-} !Sequence
, HTLCTx -> Satoshi
htx_output_value :: {-# UNPACK #-} !Satoshi
, HTLCTx -> Script
htx_output_script :: !Script
} deriving (HTLCTx -> HTLCTx -> Bool
(HTLCTx -> HTLCTx -> Bool)
-> (HTLCTx -> HTLCTx -> Bool) -> Eq HTLCTx
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HTLCTx -> HTLCTx -> Bool
== :: HTLCTx -> HTLCTx -> Bool
$c/= :: HTLCTx -> HTLCTx -> Bool
/= :: HTLCTx -> HTLCTx -> Bool
Eq, Int -> HTLCTx -> ShowS
[HTLCTx] -> ShowS
HTLCTx -> String
(Int -> HTLCTx -> ShowS)
-> (HTLCTx -> String) -> ([HTLCTx] -> ShowS) -> Show HTLCTx
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HTLCTx -> ShowS
showsPrec :: Int -> HTLCTx -> ShowS
$cshow :: HTLCTx -> String
show :: HTLCTx -> String
$cshowList :: [HTLCTx] -> ShowS
showList :: [HTLCTx] -> ShowS
Show, (forall x. HTLCTx -> Rep HTLCTx x)
-> (forall x. Rep HTLCTx x -> HTLCTx) -> Generic HTLCTx
forall x. Rep HTLCTx x -> HTLCTx
forall x. HTLCTx -> Rep HTLCTx x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HTLCTx -> Rep HTLCTx x
from :: forall x. HTLCTx -> Rep HTLCTx x
$cto :: forall x. Rep HTLCTx x -> HTLCTx
to :: forall x. Rep HTLCTx x -> HTLCTx
Generic)
instance NFData HTLCTx
build_htlc_tx :: HTLCContext -> Maybe HTLCTx
build_htlc_tx :: HTLCContext -> Maybe HTLCTx
build_htlc_tx HTLCContext
ctx = do
value <- Satoshi -> Satoshi -> Maybe Satoshi
BOLT1.sub_sat (MilliSatoshi -> Satoshi
BOLT1.msat_to_sat (HTLC -> MilliSatoshi
htlc_amount_msat HTLC
h)) Satoshi
fee
pure HTLCTx
{ htx_version = 2
, htx_locktime = locktime
, htx_input_outpoint =
OutPoint (hc_commitment_txid ctx) (hc_output_index ctx)
, htx_input_sequence = case fmt of
CommitmentFormat
StaticRemotekey -> Word32 -> Sequence
Sequence Word32
0
CommitmentFormat
Anchors -> Word32 -> Sequence
Sequence Word32
1
, htx_output_value = value
, htx_output_script = to_p2wsh $ to_local_script
(hc_revocation_pubkey ctx) (hc_to_self_delay ctx)
(hc_local_delayed ctx)
}
where
!h :: HTLC
h = HTLCContext -> HTLC
hc_htlc HTLCContext
ctx
!fmt :: CommitmentFormat
fmt = HTLCContext -> CommitmentFormat
hc_format HTLCContext
ctx
(!Satoshi
fee, !Locktime
locktime) = case HTLC -> HTLCDirection
htlc_direction HTLC
h of
HTLCDirection
HTLCOffered ->
let CltvExpiry Word32
e = HTLC -> CltvExpiry
htlc_cltv_expiry HTLC
h
in (FeeratePerKw -> CommitmentFormat -> Satoshi
htlc_timeout_fee (HTLCContext -> FeeratePerKw
hc_feerate HTLCContext
ctx) CommitmentFormat
fmt, Word32 -> Locktime
Locktime Word32
e)
HTLCDirection
HTLCReceived ->
(FeeratePerKw -> CommitmentFormat -> Satoshi
htlc_success_fee (HTLCContext -> FeeratePerKw
hc_feerate HTLCContext
ctx) CommitmentFormat
fmt, Word32 -> Locktime
Locktime Word32
0)
data ClosingOutput = ClosingOutput
{ ClosingOutput -> Satoshi
clo_value :: {-# UNPACK #-} !Satoshi
, ClosingOutput -> Script
clo_script :: !Script
} deriving (ClosingOutput -> ClosingOutput -> Bool
(ClosingOutput -> ClosingOutput -> Bool)
-> (ClosingOutput -> ClosingOutput -> Bool) -> Eq ClosingOutput
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ClosingOutput -> ClosingOutput -> Bool
== :: ClosingOutput -> ClosingOutput -> Bool
$c/= :: ClosingOutput -> ClosingOutput -> Bool
/= :: ClosingOutput -> ClosingOutput -> Bool
Eq, Int -> ClosingOutput -> ShowS
[ClosingOutput] -> ShowS
ClosingOutput -> String
(Int -> ClosingOutput -> ShowS)
-> (ClosingOutput -> String)
-> ([ClosingOutput] -> ShowS)
-> Show ClosingOutput
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ClosingOutput -> ShowS
showsPrec :: Int -> ClosingOutput -> ShowS
$cshow :: ClosingOutput -> String
show :: ClosingOutput -> String
$cshowList :: [ClosingOutput] -> ShowS
showList :: [ClosingOutput] -> ShowS
Show, (forall x. ClosingOutput -> Rep ClosingOutput x)
-> (forall x. Rep ClosingOutput x -> ClosingOutput)
-> Generic ClosingOutput
forall x. Rep ClosingOutput x -> ClosingOutput
forall x. ClosingOutput -> Rep ClosingOutput x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ClosingOutput -> Rep ClosingOutput x
from :: forall x. ClosingOutput -> Rep ClosingOutput x
$cto :: forall x. Rep ClosingOutput x -> ClosingOutput
to :: forall x. Rep ClosingOutput x -> ClosingOutput
Generic)
instance NFData ClosingOutput
data ClosingTx = ClosingTx
{ ClosingTx -> Word32
cltx_version :: {-# UNPACK #-} !Word32
, ClosingTx -> Locktime
cltx_locktime :: {-# UNPACK #-} !Locktime
, ClosingTx -> OutPoint
cltx_input_outpoint :: {-# UNPACK #-} !OutPoint
, ClosingTx -> Sequence
cltx_input_sequence :: {-# UNPACK #-} !Sequence
, ClosingTx -> NonEmpty ClosingOutput
cltx_outputs :: !(NonEmpty ClosingOutput)
} deriving (ClosingTx -> ClosingTx -> Bool
(ClosingTx -> ClosingTx -> Bool)
-> (ClosingTx -> ClosingTx -> Bool) -> Eq ClosingTx
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ClosingTx -> ClosingTx -> Bool
== :: ClosingTx -> ClosingTx -> Bool
$c/= :: ClosingTx -> ClosingTx -> Bool
/= :: ClosingTx -> ClosingTx -> Bool
Eq, Int -> ClosingTx -> ShowS
[ClosingTx] -> ShowS
ClosingTx -> String
(Int -> ClosingTx -> ShowS)
-> (ClosingTx -> String)
-> ([ClosingTx] -> ShowS)
-> Show ClosingTx
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ClosingTx -> ShowS
showsPrec :: Int -> ClosingTx -> ShowS
$cshow :: ClosingTx -> String
show :: ClosingTx -> String
$cshowList :: [ClosingTx] -> ShowS
showList :: [ClosingTx] -> ShowS
Show, (forall x. ClosingTx -> Rep ClosingTx x)
-> (forall x. Rep ClosingTx x -> ClosingTx) -> Generic ClosingTx
forall x. Rep ClosingTx x -> ClosingTx
forall x. ClosingTx -> Rep ClosingTx x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ClosingTx -> Rep ClosingTx x
from :: forall x. ClosingTx -> Rep ClosingTx x
$cto :: forall x. Rep ClosingTx x -> ClosingTx
to :: forall x. Rep ClosingTx x -> ClosingTx
Generic)
instance NFData ClosingTx
data ClosingOutputs
= CloserOutputOnly
| CloseeOutputOnly
| CloserAndCloseeOutputs
deriving (ClosingOutputs -> ClosingOutputs -> Bool
(ClosingOutputs -> ClosingOutputs -> Bool)
-> (ClosingOutputs -> ClosingOutputs -> Bool) -> Eq ClosingOutputs
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ClosingOutputs -> ClosingOutputs -> Bool
== :: ClosingOutputs -> ClosingOutputs -> Bool
$c/= :: ClosingOutputs -> ClosingOutputs -> Bool
/= :: ClosingOutputs -> ClosingOutputs -> Bool
Eq, Int -> ClosingOutputs -> ShowS
[ClosingOutputs] -> ShowS
ClosingOutputs -> String
(Int -> ClosingOutputs -> ShowS)
-> (ClosingOutputs -> String)
-> ([ClosingOutputs] -> ShowS)
-> Show ClosingOutputs
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ClosingOutputs -> ShowS
showsPrec :: Int -> ClosingOutputs -> ShowS
$cshow :: ClosingOutputs -> String
show :: ClosingOutputs -> String
$cshowList :: [ClosingOutputs] -> ShowS
showList :: [ClosingOutputs] -> ShowS
Show, (forall x. ClosingOutputs -> Rep ClosingOutputs x)
-> (forall x. Rep ClosingOutputs x -> ClosingOutputs)
-> Generic ClosingOutputs
forall x. Rep ClosingOutputs x -> ClosingOutputs
forall x. ClosingOutputs -> Rep ClosingOutputs x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ClosingOutputs -> Rep ClosingOutputs x
from :: forall x. ClosingOutputs -> Rep ClosingOutputs x
$cto :: forall x. Rep ClosingOutputs x -> ClosingOutputs
to :: forall x. Rep ClosingOutputs x -> ClosingOutputs
Generic)
instance NFData ClosingOutputs
data ClosingContext = ClosingContext
{ ClosingContext -> OutPoint
clc_funding_outpoint :: {-# UNPACK #-} !OutPoint
, ClosingContext -> MilliSatoshi
clc_closer_msat :: {-# UNPACK #-} !MilliSatoshi
, ClosingContext -> MilliSatoshi
clc_closee_msat :: {-# UNPACK #-} !MilliSatoshi
, ClosingContext -> Script
clc_closer_script :: !Script
, ClosingContext -> Script
clc_closee_script :: !Script
, ClosingContext -> Satoshi
clc_fee :: {-# UNPACK #-} !Satoshi
, ClosingContext -> Locktime
clc_locktime :: {-# UNPACK #-} !Locktime
, ClosingContext -> ClosingOutputs
clc_outputs :: !ClosingOutputs
} deriving (ClosingContext -> ClosingContext -> Bool
(ClosingContext -> ClosingContext -> Bool)
-> (ClosingContext -> ClosingContext -> Bool) -> Eq ClosingContext
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ClosingContext -> ClosingContext -> Bool
== :: ClosingContext -> ClosingContext -> Bool
$c/= :: ClosingContext -> ClosingContext -> Bool
/= :: ClosingContext -> ClosingContext -> Bool
Eq, Int -> ClosingContext -> ShowS
[ClosingContext] -> ShowS
ClosingContext -> String
(Int -> ClosingContext -> ShowS)
-> (ClosingContext -> String)
-> ([ClosingContext] -> ShowS)
-> Show ClosingContext
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ClosingContext -> ShowS
showsPrec :: Int -> ClosingContext -> ShowS
$cshow :: ClosingContext -> String
show :: ClosingContext -> String
$cshowList :: [ClosingContext] -> ShowS
showList :: [ClosingContext] -> ShowS
Show, (forall x. ClosingContext -> Rep ClosingContext x)
-> (forall x. Rep ClosingContext x -> ClosingContext)
-> Generic ClosingContext
forall x. Rep ClosingContext x -> ClosingContext
forall x. ClosingContext -> Rep ClosingContext x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ClosingContext -> Rep ClosingContext x
from :: forall x. ClosingContext -> Rep ClosingContext x
$cto :: forall x. Rep ClosingContext x -> ClosingContext
to :: forall x. Rep ClosingContext x -> ClosingContext
Generic)
instance NFData ClosingContext
build_closing_tx :: ClosingContext -> Maybe ClosingTx
build_closing_tx :: ClosingContext -> Maybe ClosingTx
build_closing_tx ClosingContext
ctx = do
closer_sat <-
Satoshi -> Satoshi -> Maybe Satoshi
BOLT1.sub_sat (MilliSatoshi -> Satoshi
BOLT1.msat_to_sat (ClosingContext -> MilliSatoshi
clc_closer_msat ClosingContext
ctx)) (ClosingContext -> Satoshi
clc_fee ClosingContext
ctx)
let !closer = Satoshi -> Script -> ClosingOutput
output Satoshi
closer_sat (ClosingContext -> Script
clc_closer_script ClosingContext
ctx)
!closee = Satoshi -> Script -> ClosingOutput
output (MilliSatoshi -> Satoshi
BOLT1.msat_to_sat (ClosingContext -> MilliSatoshi
clc_closee_msat ClosingContext
ctx))
(ClosingContext -> Script
clc_closee_script ClosingContext
ctx)
pure ClosingTx
{ cltx_version = 2
, cltx_locktime = clc_locktime ctx
, cltx_input_outpoint = clc_funding_outpoint ctx
, cltx_input_sequence = Sequence 0xFFFFFFFD
, cltx_outputs = case clc_outputs ctx of
ClosingOutputs
CloserOutputOnly -> ClosingOutput
closer ClosingOutput -> [ClosingOutput] -> NonEmpty ClosingOutput
forall a. a -> [a] -> NonEmpty a
:| []
ClosingOutputs
CloseeOutputOnly -> ClosingOutput
closee ClosingOutput -> [ClosingOutput] -> NonEmpty ClosingOutput
forall a. a -> [a] -> NonEmpty a
:| []
ClosingOutputs
CloserAndCloseeOutputs -> NonEmpty ClosingOutput -> NonEmpty ClosingOutput
sort_closing (ClosingOutput
closer ClosingOutput -> [ClosingOutput] -> NonEmpty ClosingOutput
forall a. a -> [a] -> NonEmpty a
:| [ClosingOutput
closee])
}
where
output :: Satoshi -> Script -> ClosingOutput
output Satoshi
amt spk :: Script
spk@(Script ByteString
s) = case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
s of
Just (Word8
0x6a, ByteString
_) -> Satoshi -> Script -> ClosingOutput
ClosingOutput (Word64 -> Satoshi
sat Word64
0) Script
spk
Maybe (Word8, ByteString)
_ -> Satoshi -> Script -> ClosingOutput
ClosingOutput Satoshi
amt Script
spk
data LegacyClosingContext = LegacyClosingContext
{ LegacyClosingContext -> OutPoint
lcc_funding_outpoint :: {-# UNPACK #-} !OutPoint
, LegacyClosingContext -> MilliSatoshi
lcc_local_msat :: {-# UNPACK #-} !MilliSatoshi
, LegacyClosingContext -> MilliSatoshi
lcc_remote_msat :: {-# UNPACK #-} !MilliSatoshi
, LegacyClosingContext -> Script
lcc_local_script :: !Script
, LegacyClosingContext -> Script
lcc_remote_script :: !Script
, LegacyClosingContext -> DustLimit
lcc_dust_limit :: {-# UNPACK #-} !DustLimit
, LegacyClosingContext -> Satoshi
lcc_fee :: {-# UNPACK #-} !Satoshi
, LegacyClosingContext -> Bool
lcc_is_funder :: !Bool
, LegacyClosingContext -> Bool
lcc_omit_local :: !Bool
} deriving (LegacyClosingContext -> LegacyClosingContext -> Bool
(LegacyClosingContext -> LegacyClosingContext -> Bool)
-> (LegacyClosingContext -> LegacyClosingContext -> Bool)
-> Eq LegacyClosingContext
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LegacyClosingContext -> LegacyClosingContext -> Bool
== :: LegacyClosingContext -> LegacyClosingContext -> Bool
$c/= :: LegacyClosingContext -> LegacyClosingContext -> Bool
/= :: LegacyClosingContext -> LegacyClosingContext -> Bool
Eq, Int -> LegacyClosingContext -> ShowS
[LegacyClosingContext] -> ShowS
LegacyClosingContext -> String
(Int -> LegacyClosingContext -> ShowS)
-> (LegacyClosingContext -> String)
-> ([LegacyClosingContext] -> ShowS)
-> Show LegacyClosingContext
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LegacyClosingContext -> ShowS
showsPrec :: Int -> LegacyClosingContext -> ShowS
$cshow :: LegacyClosingContext -> String
show :: LegacyClosingContext -> String
$cshowList :: [LegacyClosingContext] -> ShowS
showList :: [LegacyClosingContext] -> ShowS
Show, (forall x. LegacyClosingContext -> Rep LegacyClosingContext x)
-> (forall x. Rep LegacyClosingContext x -> LegacyClosingContext)
-> Generic LegacyClosingContext
forall x. Rep LegacyClosingContext x -> LegacyClosingContext
forall x. LegacyClosingContext -> Rep LegacyClosingContext x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. LegacyClosingContext -> Rep LegacyClosingContext x
from :: forall x. LegacyClosingContext -> Rep LegacyClosingContext x
$cto :: forall x. Rep LegacyClosingContext x -> LegacyClosingContext
to :: forall x. Rep LegacyClosingContext x -> LegacyClosingContext
Generic)
instance NFData LegacyClosingContext
build_legacy_closing_tx :: LegacyClosingContext -> Maybe ClosingTx
build_legacy_closing_tx :: LegacyClosingContext -> Maybe ClosingTx
build_legacy_closing_tx LegacyClosingContext
ctx = do
let !local_sat :: Satoshi
local_sat = MilliSatoshi -> Satoshi
BOLT1.msat_to_sat (LegacyClosingContext -> MilliSatoshi
lcc_local_msat LegacyClosingContext
ctx)
!remote_sat :: Satoshi
remote_sat = MilliSatoshi -> Satoshi
BOLT1.msat_to_sat (LegacyClosingContext -> MilliSatoshi
lcc_remote_msat LegacyClosingContext
ctx)
DustLimit Satoshi
dust = LegacyClosingContext -> DustLimit
lcc_dust_limit LegacyClosingContext
ctx
(l, r) <-
if LegacyClosingContext -> Bool
lcc_is_funder LegacyClosingContext
ctx
then (Satoshi -> (Satoshi, Satoshi))
-> Maybe Satoshi -> Maybe (Satoshi, Satoshi)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Satoshi
x -> (Satoshi
x, Satoshi
remote_sat)) (Satoshi -> Satoshi -> Maybe Satoshi
BOLT1.sub_sat Satoshi
local_sat Satoshi
fee)
else (Satoshi -> (Satoshi, Satoshi))
-> Maybe Satoshi -> Maybe (Satoshi, Satoshi)
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Satoshi
x -> (Satoshi
local_sat, Satoshi
x)) (Satoshi -> Satoshi -> Maybe Satoshi
BOLT1.sub_sat Satoshi
remote_sat Satoshi
fee)
outputs <- NE.nonEmpty $
[ ClosingOutput l (lcc_local_script ctx)
| not (lcc_omit_local ctx), l >= dust ]
<> [ ClosingOutput r (lcc_remote_script ctx) | r >= dust ]
pure ClosingTx
{ cltx_version = 2
, cltx_locktime = Locktime 0
, cltx_input_outpoint = lcc_funding_outpoint ctx
, cltx_input_sequence = Sequence 0xFFFFFFFF
, cltx_outputs = sort_closing outputs
}
where
!fee :: Satoshi
fee = LegacyClosingContext -> Satoshi
lcc_fee LegacyClosingContext
ctx
sort_closing :: NonEmpty ClosingOutput -> NonEmpty ClosingOutput
sort_closing :: NonEmpty ClosingOutput -> NonEmpty ClosingOutput
sort_closing = (ClosingOutput -> ClosingOutput -> Ordering)
-> NonEmpty ClosingOutput -> NonEmpty ClosingOutput
forall a. (a -> a -> Ordering) -> NonEmpty a -> NonEmpty a
NE.sortBy ((ClosingOutput -> ClosingOutput -> Ordering)
-> NonEmpty ClosingOutput -> NonEmpty ClosingOutput)
-> (ClosingOutput -> ClosingOutput -> Ordering)
-> NonEmpty ClosingOutput
-> NonEmpty ClosingOutput
forall a b. (a -> b) -> a -> b
$ \ClosingOutput
a ClosingOutput
b ->
Satoshi -> Satoshi -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (ClosingOutput -> Satoshi
clo_value ClosingOutput
a) (ClosingOutput -> Satoshi
clo_value ClosingOutput
b) Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> Script -> Script -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (ClosingOutput -> Script
clo_script ClosingOutput
a) (ClosingOutput -> Script
clo_script ClosingOutput
b)
weight_fee :: FeeratePerKw -> Integer -> Satoshi
weight_fee :: FeeratePerKw -> Integer -> Satoshi
weight_fee (FeeratePerKw Word32
rate) Integer
weight =
let !f :: Integer
f = Word32 -> Integer
forall a. Integral a => a -> Integer
toInteger Word32
rate Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
weight Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`quot` Integer
1000
in if Integer
f Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Satoshi -> Word64
BOLT1.un_satoshi Satoshi
BOLT1.max_satoshi)
then Satoshi
BOLT1.max_satoshi
else Word64 -> Satoshi
sat (Integer -> Word64
forall a. Num a => Integer -> a
fromInteger Integer
f)
commitment_fee :: FeeratePerKw -> CommitmentFormat -> Word64 -> Satoshi
commitment_fee :: FeeratePerKw -> CommitmentFormat -> Word64 -> Satoshi
commitment_fee FeeratePerKw
rate CommitmentFormat
fmt Word64
n = FeeratePerKw -> Integer -> Satoshi
weight_fee FeeratePerKw
rate (Integer
base Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
172 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
n) where
base :: Integer
base = case CommitmentFormat
fmt of
CommitmentFormat
StaticRemotekey -> Integer
724
CommitmentFormat
Anchors -> Integer
1124
htlc_timeout_fee :: FeeratePerKw -> CommitmentFormat -> Satoshi
htlc_timeout_fee :: FeeratePerKw -> CommitmentFormat -> Satoshi
htlc_timeout_fee FeeratePerKw
rate CommitmentFormat
fmt = case CommitmentFormat
fmt of
CommitmentFormat
StaticRemotekey -> FeeratePerKw -> Integer -> Satoshi
weight_fee FeeratePerKw
rate Integer
663
CommitmentFormat
Anchors -> Word64 -> Satoshi
sat Word64
0
htlc_success_fee :: FeeratePerKw -> CommitmentFormat -> Satoshi
htlc_success_fee :: FeeratePerKw -> CommitmentFormat -> Satoshi
htlc_success_fee FeeratePerKw
rate CommitmentFormat
fmt = case CommitmentFormat
fmt of
CommitmentFormat
StaticRemotekey -> FeeratePerKw -> Integer -> Satoshi
weight_fee FeeratePerKw
rate Integer
703
CommitmentFormat
Anchors -> Word64 -> Satoshi
sat Word64
0
htlc_trim_threshold
:: DustLimit
-> FeeratePerKw
-> CommitmentFormat
-> HTLCDirection
-> Satoshi
htlc_trim_threshold :: DustLimit
-> FeeratePerKw -> CommitmentFormat -> HTLCDirection -> Satoshi
htlc_trim_threshold (DustLimit Satoshi
dust) FeeratePerKw
rate CommitmentFormat
fmt HTLCDirection
dir =
let !fee :: Satoshi
fee = case HTLCDirection
dir of
HTLCDirection
HTLCOffered -> FeeratePerKw -> CommitmentFormat -> Satoshi
htlc_timeout_fee FeeratePerKw
rate CommitmentFormat
fmt
HTLCDirection
HTLCReceived -> FeeratePerKw -> CommitmentFormat -> Satoshi
htlc_success_fee FeeratePerKw
rate CommitmentFormat
fmt
in Satoshi -> Maybe Satoshi -> Satoshi
forall a. a -> Maybe a -> a
fromMaybe Satoshi
BOLT1.max_satoshi (Satoshi -> Satoshi -> Maybe Satoshi
BOLT1.add_sat Satoshi
dust Satoshi
fee)
is_trimmed :: DustLimit -> FeeratePerKw -> CommitmentFormat -> HTLC -> Bool
is_trimmed :: DustLimit -> FeeratePerKw -> CommitmentFormat -> HTLC -> Bool
is_trimmed DustLimit
dust FeeratePerKw
rate CommitmentFormat
fmt HTLC
h =
MilliSatoshi -> Satoshi
BOLT1.msat_to_sat (HTLC -> MilliSatoshi
htlc_amount_msat HTLC
h)
Satoshi -> Satoshi -> Bool
forall a. Ord a => a -> a -> Bool
< DustLimit
-> FeeratePerKw -> CommitmentFormat -> HTLCDirection -> Satoshi
htlc_trim_threshold DustLimit
dust FeeratePerKw
rate CommitmentFormat
fmt (HTLC -> HTLCDirection
htlc_direction HTLC
h)
unsigned_tx
:: Word32 -> Locktime -> OutPoint -> Sequence -> NonEmpty BT.TxOut
-> BT.Tx
unsigned_tx :: Word32 -> Locktime -> OutPoint -> Sequence -> NonEmpty TxOut -> Tx
unsigned_tx Word32
version (Locktime Word32
lt) OutPoint
prevout (Sequence Word32
sq) NonEmpty TxOut
outs = BT.Tx
{ tx_version :: Word32
BT.tx_version = Word32
version
, tx_inputs :: NonEmpty TxIn
BT.tx_inputs = OutPoint -> ByteString -> Word32 -> Witness -> TxIn
BT.TxIn OutPoint
prevout ByteString
BS.empty Word32
sq ([ByteString] -> Witness
BT.Witness []) TxIn -> [TxIn] -> NonEmpty TxIn
forall a. a -> [a] -> NonEmpty a
:| []
, tx_outputs :: NonEmpty TxOut
BT.tx_outputs = NonEmpty TxOut
outs
, tx_locktime :: Word32
BT.tx_locktime = Word32
lt
}
txout :: Satoshi -> Script -> BT.TxOut
txout :: Satoshi -> Script -> TxOut
txout Satoshi
v (Script ByteString
s) = Word64 -> ByteString -> TxOut
BT.TxOut (Satoshi -> Word64
BOLT1.un_satoshi Satoshi
v) ByteString
s
{-# INLINE txout #-}
commitment_to_tx :: CommitmentTx -> BT.Tx
commitment_to_tx :: CommitmentTx -> Tx
commitment_to_tx CommitmentTx
c = Word32 -> Locktime -> OutPoint -> Sequence -> NonEmpty TxOut -> Tx
unsigned_tx (CommitmentTx -> Word32
ctx_version CommitmentTx
c) (CommitmentTx -> Locktime
ctx_locktime CommitmentTx
c)
(CommitmentTx -> OutPoint
ctx_input_outpoint CommitmentTx
c) (CommitmentTx -> Sequence
ctx_input_sequence CommitmentTx
c)
((CommitmentOutput -> TxOut)
-> NonEmpty CommitmentOutput -> NonEmpty TxOut
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\CommitmentOutput
o -> Satoshi -> Script -> TxOut
txout (CommitmentOutput -> Satoshi
co_value CommitmentOutput
o) (CommitmentOutput -> Script
co_script CommitmentOutput
o)) (CommitmentTx -> NonEmpty CommitmentOutput
ctx_outputs CommitmentTx
c))
htlc_to_tx :: HTLCTx -> BT.Tx
htlc_to_tx :: HTLCTx -> Tx
htlc_to_tx HTLCTx
h = Word32 -> Locktime -> OutPoint -> Sequence -> NonEmpty TxOut -> Tx
unsigned_tx (HTLCTx -> Word32
htx_version HTLCTx
h) (HTLCTx -> Locktime
htx_locktime HTLCTx
h)
(HTLCTx -> OutPoint
htx_input_outpoint HTLCTx
h) (HTLCTx -> Sequence
htx_input_sequence HTLCTx
h)
(Satoshi -> Script -> TxOut
txout (HTLCTx -> Satoshi
htx_output_value HTLCTx
h) (HTLCTx -> Script
htx_output_script HTLCTx
h) TxOut -> [TxOut] -> NonEmpty TxOut
forall a. a -> [a] -> NonEmpty a
:| [])
closing_to_tx :: ClosingTx -> BT.Tx
closing_to_tx :: ClosingTx -> Tx
closing_to_tx ClosingTx
c = Word32 -> Locktime -> OutPoint -> Sequence -> NonEmpty TxOut -> Tx
unsigned_tx (ClosingTx -> Word32
cltx_version ClosingTx
c) (ClosingTx -> Locktime
cltx_locktime ClosingTx
c)
(ClosingTx -> OutPoint
cltx_input_outpoint ClosingTx
c) (ClosingTx -> Sequence
cltx_input_sequence ClosingTx
c)
((ClosingOutput -> TxOut)
-> NonEmpty ClosingOutput -> NonEmpty TxOut
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\ClosingOutput
o -> Satoshi -> Script -> TxOut
txout (ClosingOutput -> Satoshi
clo_value ClosingOutput
o) (ClosingOutput -> Script
clo_script ClosingOutput
o)) (ClosingTx -> NonEmpty ClosingOutput
cltx_outputs ClosingTx
c))
encode_commitment_tx :: CommitmentTx -> BS.ByteString
encode_commitment_tx :: CommitmentTx -> ByteString
encode_commitment_tx = Tx -> ByteString
BT.to_bytes_legacy (Tx -> ByteString)
-> (CommitmentTx -> Tx) -> CommitmentTx -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CommitmentTx -> Tx
commitment_to_tx
encode_htlc_tx :: HTLCTx -> BS.ByteString
encode_htlc_tx :: HTLCTx -> ByteString
encode_htlc_tx = Tx -> ByteString
BT.to_bytes_legacy (Tx -> ByteString) -> (HTLCTx -> Tx) -> HTLCTx -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HTLCTx -> Tx
htlc_to_tx
encode_closing_tx :: ClosingTx -> BS.ByteString
encode_closing_tx :: ClosingTx -> ByteString
encode_closing_tx = Tx -> ByteString
BT.to_bytes_legacy (Tx -> ByteString) -> (ClosingTx -> Tx) -> ClosingTx -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ClosingTx -> Tx
closing_to_tx