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

-- |
-- Module: Lightning.Protocol.BOLT5.Types
-- Copyright: (c) 2025 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Types for BOLT #5 on-chain transaction handling.

module Lightning.Protocol.BOLT5.Types (
    -- * Close identification
    CloseType(..)

    -- * Output classification
  , UnresolvedOutput(..)
  , OutputResolution(..)

    -- * HTLC output type
  , HTLCOutputType(..)
  , htlc_output_type
  , htlc_output_type_weight

    -- * Spending transactions
  , SpendingTx(..)

    -- * Revoked outputs
  , RevokedOutput(..)
  , RevokedOutputType(..)
  , revoked_output_weight

    -- * Weight constants (Appendix A)
  , to_local_penalty_witness_weight
  , offered_htlc_penalty_witness_weight
  , accepted_htlc_penalty_witness_weight
  , offered_htlc_penalty_witness_weight_anchors
  , accepted_htlc_penalty_witness_weight_anchors
  , to_local_penalty_input_weight
  , offered_htlc_penalty_input_weight
  , accepted_htlc_penalty_input_weight
  , offered_htlc_penalty_input_weight_anchors
  , accepted_htlc_penalty_input_weight_anchors
  , to_remote_input_weight
  , penalty_tx_base_weight
  , max_standard_weight

    -- * Fee calculation
  , spending_fee
  ) where

import Bitcoin.Prim.Tx (OutPoint, Tx)
import Bitcoin.Prim.Tx.Sighash (SighashType)
import Control.DeepSeq (NFData(..))
import Data.Word (Word64)
import GHC.Generics (Generic)
import Lightning.Protocol.BOLT1 (Satoshi)
import qualified Lightning.Protocol.BOLT1 as BOLT1
import Lightning.Protocol.BOLT3

-- close identification -----------------------------------------------

-- | What kind of close was detected on chain.
--
--   @identify_close@ produces only 'LocalCommitClose' and
--   'RemoteCommitClose'; recognising mutual and revoked closes is
--   left to the caller.
data CloseType
  = MutualClose
    -- ^ Cooperative closure agreed by both parties.
  | LocalCommitClose
    -- ^ Our commitment transaction was broadcast.
  | RemoteCommitClose
    -- ^ The remote party's commitment transaction was broadcast.
  | RevokedCommitClose
    -- ^ A revoked (outdated) commitment transaction was broadcast.
  deriving (CloseType -> CloseType -> Bool
(CloseType -> CloseType -> Bool)
-> (CloseType -> CloseType -> Bool) -> Eq CloseType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CloseType -> CloseType -> Bool
== :: CloseType -> CloseType -> Bool
$c/= :: CloseType -> CloseType -> Bool
/= :: CloseType -> CloseType -> Bool
Eq, Int -> CloseType -> ShowS
[CloseType] -> ShowS
CloseType -> String
(Int -> CloseType -> ShowS)
-> (CloseType -> String)
-> ([CloseType] -> ShowS)
-> Show CloseType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CloseType -> ShowS
showsPrec :: Int -> CloseType -> ShowS
$cshow :: CloseType -> String
show :: CloseType -> String
$cshowList :: [CloseType] -> ShowS
showList :: [CloseType] -> ShowS
Show, (forall x. CloseType -> Rep CloseType x)
-> (forall x. Rep CloseType x -> CloseType) -> Generic CloseType
forall x. Rep CloseType x -> CloseType
forall x. CloseType -> Rep CloseType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CloseType -> Rep CloseType x
from :: forall x. CloseType -> Rep CloseType x
$cto :: forall x. Rep CloseType x -> CloseType
to :: forall x. Rep CloseType x -> CloseType
Generic)

instance NFData CloseType

-- output classification ----------------------------------------------

-- | An unresolved commitment transaction output.
data UnresolvedOutput = UnresolvedOutput
  { UnresolvedOutput -> OutPoint
uo_outpoint :: !OutPoint
  , UnresolvedOutput -> Satoshi
uo_value    :: {-# UNPACK #-} !Satoshi
  , UnresolvedOutput -> OutputResolution
uo_type     :: !OutputResolution
  } deriving (UnresolvedOutput -> UnresolvedOutput -> Bool
(UnresolvedOutput -> UnresolvedOutput -> Bool)
-> (UnresolvedOutput -> UnresolvedOutput -> Bool)
-> Eq UnresolvedOutput
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UnresolvedOutput -> UnresolvedOutput -> Bool
== :: UnresolvedOutput -> UnresolvedOutput -> Bool
$c/= :: UnresolvedOutput -> UnresolvedOutput -> Bool
/= :: UnresolvedOutput -> UnresolvedOutput -> Bool
Eq, Int -> UnresolvedOutput -> ShowS
[UnresolvedOutput] -> ShowS
UnresolvedOutput -> String
(Int -> UnresolvedOutput -> ShowS)
-> (UnresolvedOutput -> String)
-> ([UnresolvedOutput] -> ShowS)
-> Show UnresolvedOutput
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UnresolvedOutput -> ShowS
showsPrec :: Int -> UnresolvedOutput -> ShowS
$cshow :: UnresolvedOutput -> String
show :: UnresolvedOutput -> String
$cshowList :: [UnresolvedOutput] -> ShowS
showList :: [UnresolvedOutput] -> ShowS
Show, (forall x. UnresolvedOutput -> Rep UnresolvedOutput x)
-> (forall x. Rep UnresolvedOutput x -> UnresolvedOutput)
-> Generic UnresolvedOutput
forall x. Rep UnresolvedOutput x -> UnresolvedOutput
forall x. UnresolvedOutput -> Rep UnresolvedOutput x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. UnresolvedOutput -> Rep UnresolvedOutput x
from :: forall x. UnresolvedOutput -> Rep UnresolvedOutput x
$cto :: forall x. Rep UnresolvedOutput x -> UnresolvedOutput
to :: forall x. Rep UnresolvedOutput x -> UnresolvedOutput
Generic)

instance NFData UnresolvedOutput

-- | How to resolve an output, per BOLT #5 rules.
data OutputResolution
  = Resolved
    -- ^ Nothing to do (e.g. to_remote on our local commitment).
  | SpendToLocal
      !ToSelfDelay !RevocationPubkey !LocalDelayedPubkey
    -- ^ Spend to_local after the CSV delay.
  | SpendHTLCTimeout
      !HTLC !CommitmentKeys !CommitmentFormat
    -- ^ Spend via an HTLC-timeout transaction (our commitment, an
    --   HTLC we offered).
  | SpendHTLCSuccess
      !HTLC !CommitmentKeys !CommitmentFormat
    -- ^ Spend via an HTLC-success transaction (our commitment, an
    --   HTLC we received).
  | SpendHTLCTimeoutDirect !HTLC
    -- ^ Spend the HTLC directly after it times out (the remote
    --   commitment, an HTLC we offered).
  | SpendHTLCPreimageDirect !HTLC
    -- ^ Spend the HTLC directly with the preimage (the remote
    --   commitment, an HTLC we received).
  | Revoke !RevocationPubkey
    -- ^ Spend a revoked to_local output with the revocation key.
  | RevokeHTLC !RevocationPubkey !HTLCOutputType
    -- ^ Spend a revoked HTLC output with the revocation key.
  | AnchorSpend !FundingPubkey
    -- ^ Spend our anchor output.
  deriving (OutputResolution -> OutputResolution -> Bool
(OutputResolution -> OutputResolution -> Bool)
-> (OutputResolution -> OutputResolution -> Bool)
-> Eq OutputResolution
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OutputResolution -> OutputResolution -> Bool
== :: OutputResolution -> OutputResolution -> Bool
$c/= :: OutputResolution -> OutputResolution -> Bool
/= :: OutputResolution -> OutputResolution -> Bool
Eq, Int -> OutputResolution -> ShowS
[OutputResolution] -> ShowS
OutputResolution -> String
(Int -> OutputResolution -> ShowS)
-> (OutputResolution -> String)
-> ([OutputResolution] -> ShowS)
-> Show OutputResolution
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OutputResolution -> ShowS
showsPrec :: Int -> OutputResolution -> ShowS
$cshow :: OutputResolution -> String
show :: OutputResolution -> String
$cshowList :: [OutputResolution] -> ShowS
showList :: [OutputResolution] -> ShowS
Show, (forall x. OutputResolution -> Rep OutputResolution x)
-> (forall x. Rep OutputResolution x -> OutputResolution)
-> Generic OutputResolution
forall x. Rep OutputResolution x -> OutputResolution
forall x. OutputResolution -> Rep OutputResolution x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. OutputResolution -> Rep OutputResolution x
from :: forall x. OutputResolution -> Rep OutputResolution x
$cto :: forall x. Rep OutputResolution x -> OutputResolution
to :: forall x. Rep OutputResolution x -> OutputResolution
Generic)

instance NFData OutputResolution

-- HTLC output type -------------------------------------------------

-- | The kind of an HTLC output, from the commitment owner's point of
--   view.
--
--   Unlike bolt3's 'OutputType', this can't describe a to_local,
--   to_remote or anchor output.
data HTLCOutputType
  = HTLCOfferedOutput {-# UNPACK #-} !CltvExpiry
    -- ^ Offered HTLC output with CLTV expiry.
  | HTLCReceivedOutput {-# UNPACK #-} !CltvExpiry
    -- ^ Received HTLC output with CLTV expiry.
  deriving (HTLCOutputType -> HTLCOutputType -> Bool
(HTLCOutputType -> HTLCOutputType -> Bool)
-> (HTLCOutputType -> HTLCOutputType -> Bool) -> Eq HTLCOutputType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HTLCOutputType -> HTLCOutputType -> Bool
== :: HTLCOutputType -> HTLCOutputType -> Bool
$c/= :: HTLCOutputType -> HTLCOutputType -> Bool
/= :: HTLCOutputType -> HTLCOutputType -> Bool
Eq, Int -> HTLCOutputType -> ShowS
[HTLCOutputType] -> ShowS
HTLCOutputType -> String
(Int -> HTLCOutputType -> ShowS)
-> (HTLCOutputType -> String)
-> ([HTLCOutputType] -> ShowS)
-> Show HTLCOutputType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HTLCOutputType -> ShowS
showsPrec :: Int -> HTLCOutputType -> ShowS
$cshow :: HTLCOutputType -> String
show :: HTLCOutputType -> String
$cshowList :: [HTLCOutputType] -> ShowS
showList :: [HTLCOutputType] -> ShowS
Show, (forall x. HTLCOutputType -> Rep HTLCOutputType x)
-> (forall x. Rep HTLCOutputType x -> HTLCOutputType)
-> Generic HTLCOutputType
forall x. Rep HTLCOutputType x -> HTLCOutputType
forall x. HTLCOutputType -> Rep HTLCOutputType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HTLCOutputType -> Rep HTLCOutputType x
from :: forall x. HTLCOutputType -> Rep HTLCOutputType x
$cto :: forall x. Rep HTLCOutputType x -> HTLCOutputType
to :: forall x. Rep HTLCOutputType x -> HTLCOutputType
Generic)

instance NFData HTLCOutputType

-- | The 'HTLCOutputType' of a bolt3 'OutputType', or 'Nothing' if
--   it isn't an HTLC output.
--
--   >>> htlc_output_type OutputToLocal
--   Nothing
htlc_output_type :: OutputType -> Maybe HTLCOutputType
htlc_output_type :: OutputType -> Maybe HTLCOutputType
htlc_output_type OutputType
t = case OutputType
t of
  OutputHTLC HTLC
h -> HTLCOutputType -> Maybe HTLCOutputType
forall a. a -> Maybe a
Just (HTLCOutputType -> Maybe HTLCOutputType)
-> HTLCOutputType -> Maybe HTLCOutputType
forall a b. (a -> b) -> a -> b
$ case HTLC -> HTLCDirection
htlc_direction HTLC
h of
    HTLCDirection
HTLCOffered  -> CltvExpiry -> HTLCOutputType
HTLCOfferedOutput (HTLC -> CltvExpiry
htlc_cltv_expiry HTLC
h)
    HTLCDirection
HTLCReceived -> CltvExpiry -> HTLCOutputType
HTLCReceivedOutput (HTLC -> CltvExpiry
htlc_cltv_expiry HTLC
h)
  OutputType
OutputToLocal      -> Maybe HTLCOutputType
forall a. Maybe a
Nothing
  OutputType
OutputToRemote     -> Maybe HTLCOutputType
forall a. Maybe a
Nothing
  OutputType
OutputLocalAnchor  -> Maybe HTLCOutputType
forall a. Maybe a
Nothing
  OutputType
OutputRemoteAnchor -> Maybe HTLCOutputType
forall a. Maybe a
Nothing
{-# INLINE htlc_output_type #-}

-- | Penalty input weight for an HTLC output.
--
--   >>> htlc_output_type_weight Anchors (HTLCOfferedOutput (CltvExpiry 500))
--   410
htlc_output_type_weight :: CommitmentFormat -> HTLCOutputType -> Word64
htlc_output_type_weight :: CommitmentFormat -> HTLCOutputType -> Word64
htlc_output_type_weight !CommitmentFormat
fmt !HTLCOutputType
t = case (CommitmentFormat
fmt, HTLCOutputType
t) of
  (CommitmentFormat
StaticRemotekey, HTLCOfferedOutput CltvExpiry
_)  ->
    Word64
offered_htlc_penalty_input_weight
  (CommitmentFormat
StaticRemotekey, HTLCReceivedOutput CltvExpiry
_) ->
    Word64
accepted_htlc_penalty_input_weight
  (CommitmentFormat
Anchors, HTLCOfferedOutput CltvExpiry
_)          ->
    Word64
offered_htlc_penalty_input_weight_anchors
  (CommitmentFormat
Anchors, HTLCReceivedOutput CltvExpiry
_)         ->
    Word64
accepted_htlc_penalty_input_weight_anchors
{-# INLINE htlc_output_type_weight #-}

-- spending transactions ----------------------------------------------

-- | An unsigned spending transaction, with what the caller needs to
--   sign its (single) input.
--
--   The caller signs the input's BIP143 sighash (ppad-tx's
--   @sighash_segwit@ over 'stx_tx', input 0, 'stx_input_script',
--   'stx_input_value' and 'stx_sighash_type') and assembles the
--   witness with bolt3's witness functions.
data SpendingTx = SpendingTx
  { SpendingTx -> Tx
stx_tx           :: !Tx
    -- ^ The unsigned transaction.
  , SpendingTx -> Script
stx_input_script :: !Script
    -- ^ Witness script of the output being spent.
  , SpendingTx -> Satoshi
stx_input_value  :: {-# UNPACK #-} !Satoshi
    -- ^ Value of the output being spent.
  , SpendingTx -> SighashType
stx_sighash_type :: !SighashType
    -- ^ Sighash type of the local signature.
  } deriving (SpendingTx -> SpendingTx -> Bool
(SpendingTx -> SpendingTx -> Bool)
-> (SpendingTx -> SpendingTx -> Bool) -> Eq SpendingTx
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SpendingTx -> SpendingTx -> Bool
== :: SpendingTx -> SpendingTx -> Bool
$c/= :: SpendingTx -> SpendingTx -> Bool
/= :: SpendingTx -> SpendingTx -> Bool
Eq, Int -> SpendingTx -> ShowS
[SpendingTx] -> ShowS
SpendingTx -> String
(Int -> SpendingTx -> ShowS)
-> (SpendingTx -> String)
-> ([SpendingTx] -> ShowS)
-> Show SpendingTx
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SpendingTx -> ShowS
showsPrec :: Int -> SpendingTx -> ShowS
$cshow :: SpendingTx -> String
show :: SpendingTx -> String
$cshowList :: [SpendingTx] -> ShowS
showList :: [SpendingTx] -> ShowS
Show, (forall x. SpendingTx -> Rep SpendingTx x)
-> (forall x. Rep SpendingTx x -> SpendingTx) -> Generic SpendingTx
forall x. Rep SpendingTx x -> SpendingTx
forall x. SpendingTx -> Rep SpendingTx x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. SpendingTx -> Rep SpendingTx x
from :: forall x. SpendingTx -> Rep SpendingTx x
$cto :: forall x. Rep SpendingTx x -> SpendingTx
to :: forall x. Rep SpendingTx x -> SpendingTx
Generic)

instance NFData SpendingTx where
  rnf :: SpendingTx -> ()
rnf (SpendingTx Tx
tx Script
s Satoshi
v SighashType
h) = Tx -> ()
forall a. NFData a => a -> ()
rnf Tx
tx () -> () -> ()
forall a b. a -> b -> b
`seq` Script -> ()
forall a. NFData a => a -> ()
rnf Script
s () -> () -> ()
forall a b. a -> b -> b
`seq` Satoshi -> ()
forall a. NFData a => a -> ()
rnf Satoshi
v () -> () -> ()
forall a b. a -> b -> b
`seq` SighashType
h SighashType -> () -> ()
forall a b. a -> b -> b
`seq` ()

-- revoked outputs ----------------------------------------------------

-- | A revoked output that can be swept with a revocation key.
data RevokedOutput = RevokedOutput
  { RevokedOutput -> OutPoint
ro_outpoint :: !OutPoint
  , RevokedOutput -> Satoshi
ro_value    :: {-# UNPACK #-} !Satoshi
  , RevokedOutput -> RevokedOutputType
ro_type     :: !RevokedOutputType
  } deriving (RevokedOutput -> RevokedOutput -> Bool
(RevokedOutput -> RevokedOutput -> Bool)
-> (RevokedOutput -> RevokedOutput -> Bool) -> Eq RevokedOutput
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RevokedOutput -> RevokedOutput -> Bool
== :: RevokedOutput -> RevokedOutput -> Bool
$c/= :: RevokedOutput -> RevokedOutput -> Bool
/= :: RevokedOutput -> RevokedOutput -> Bool
Eq, Int -> RevokedOutput -> ShowS
[RevokedOutput] -> ShowS
RevokedOutput -> String
(Int -> RevokedOutput -> ShowS)
-> (RevokedOutput -> String)
-> ([RevokedOutput] -> ShowS)
-> Show RevokedOutput
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RevokedOutput -> ShowS
showsPrec :: Int -> RevokedOutput -> ShowS
$cshow :: RevokedOutput -> String
show :: RevokedOutput -> String
$cshowList :: [RevokedOutput] -> ShowS
showList :: [RevokedOutput] -> ShowS
Show, (forall x. RevokedOutput -> Rep RevokedOutput x)
-> (forall x. Rep RevokedOutput x -> RevokedOutput)
-> Generic RevokedOutput
forall x. Rep RevokedOutput x -> RevokedOutput
forall x. RevokedOutput -> Rep RevokedOutput x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. RevokedOutput -> Rep RevokedOutput x
from :: forall x. RevokedOutput -> Rep RevokedOutput x
$cto :: forall x. Rep RevokedOutput x -> RevokedOutput
to :: forall x. Rep RevokedOutput x -> RevokedOutput
Generic)

instance NFData RevokedOutput

-- | What kind of revoked output is being swept.
data RevokedOutputType
  = RevokedToLocal
    -- ^ Revoked to_local output.
  | RevokedHTLC !HTLCOutputType
    -- ^ Revoked HTLC output.
  deriving (RevokedOutputType -> RevokedOutputType -> Bool
(RevokedOutputType -> RevokedOutputType -> Bool)
-> (RevokedOutputType -> RevokedOutputType -> Bool)
-> Eq RevokedOutputType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: RevokedOutputType -> RevokedOutputType -> Bool
== :: RevokedOutputType -> RevokedOutputType -> Bool
$c/= :: RevokedOutputType -> RevokedOutputType -> Bool
/= :: RevokedOutputType -> RevokedOutputType -> Bool
Eq, Int -> RevokedOutputType -> ShowS
[RevokedOutputType] -> ShowS
RevokedOutputType -> String
(Int -> RevokedOutputType -> ShowS)
-> (RevokedOutputType -> String)
-> ([RevokedOutputType] -> ShowS)
-> Show RevokedOutputType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> RevokedOutputType -> ShowS
showsPrec :: Int -> RevokedOutputType -> ShowS
$cshow :: RevokedOutputType -> String
show :: RevokedOutputType -> String
$cshowList :: [RevokedOutputType] -> ShowS
showList :: [RevokedOutputType] -> ShowS
Show, (forall x. RevokedOutputType -> Rep RevokedOutputType x)
-> (forall x. Rep RevokedOutputType x -> RevokedOutputType)
-> Generic RevokedOutputType
forall x. Rep RevokedOutputType x -> RevokedOutputType
forall x. RevokedOutputType -> Rep RevokedOutputType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. RevokedOutputType -> Rep RevokedOutputType x
from :: forall x. RevokedOutputType -> Rep RevokedOutputType x
$cto :: forall x. Rep RevokedOutputType x -> RevokedOutputType
to :: forall x. Rep RevokedOutputType x -> RevokedOutputType
Generic)

instance NFData RevokedOutputType

-- | Penalty input weight for a revoked output.
revoked_output_weight :: CommitmentFormat -> RevokedOutput -> Word64
revoked_output_weight :: CommitmentFormat -> RevokedOutput -> Word64
revoked_output_weight !CommitmentFormat
fmt !RevokedOutput
ro = case RevokedOutput -> RevokedOutputType
ro_type RevokedOutput
ro of
  RevokedOutputType
RevokedToLocal ->
    Word64
to_local_penalty_input_weight
  RevokedHTLC !HTLCOutputType
htype ->
    CommitmentFormat -> HTLCOutputType -> Word64
htlc_output_type_weight CommitmentFormat
fmt HTLCOutputType
htype
{-# INLINE revoked_output_weight #-}

-- weight constants (BOLT #5 Appendix A) ------------------------------

-- | Expected weight of the to_local penalty transaction witness
--   (160 weight units).
to_local_penalty_witness_weight :: Word64
to_local_penalty_witness_weight :: Word64
to_local_penalty_witness_weight = Word64
160

-- | Expected weight of the offered_htlc penalty transaction
--   witness (243 weight units), for a 133-byte witness script
--   (no option_anchors).
offered_htlc_penalty_witness_weight :: Word64
offered_htlc_penalty_witness_weight :: Word64
offered_htlc_penalty_witness_weight = Word64
243

-- | Expected weight of the accepted_htlc penalty transaction
--   witness (249 weight units), for a 139-byte witness script
--   (no option_anchors).
accepted_htlc_penalty_witness_weight :: Word64
accepted_htlc_penalty_witness_weight :: Word64
accepted_htlc_penalty_witness_weight = Word64
249

-- | Expected weight of the offered_htlc penalty transaction
--   witness with option_anchors (246 weight units), whose witness
--   script is 136 bytes.
offered_htlc_penalty_witness_weight_anchors :: Word64
offered_htlc_penalty_witness_weight_anchors :: Word64
offered_htlc_penalty_witness_weight_anchors = Word64
246

-- | Expected weight of the accepted_htlc penalty transaction
--   witness with option_anchors (252 weight units), whose witness
--   script is 142 bytes.
accepted_htlc_penalty_witness_weight_anchors :: Word64
accepted_htlc_penalty_witness_weight_anchors :: Word64
accepted_htlc_penalty_witness_weight_anchors = Word64
252

-- | Weight of a to_local penalty input (164 + 160 = 324).
to_local_penalty_input_weight :: Word64
to_local_penalty_input_weight :: Word64
to_local_penalty_input_weight = Word64
324

-- | Weight of an offered_htlc penalty input (164 + 243 = 407).
offered_htlc_penalty_input_weight :: Word64
offered_htlc_penalty_input_weight :: Word64
offered_htlc_penalty_input_weight = Word64
407

-- | Weight of an accepted_htlc penalty input (164 + 249 = 413).
accepted_htlc_penalty_input_weight :: Word64
accepted_htlc_penalty_input_weight :: Word64
accepted_htlc_penalty_input_weight = Word64
413

-- | Weight of an offered_htlc penalty input with option_anchors
--   (164 + 246 = 410).
offered_htlc_penalty_input_weight_anchors :: Word64
offered_htlc_penalty_input_weight_anchors :: Word64
offered_htlc_penalty_input_weight_anchors = Word64
410

-- | Weight of an accepted_htlc penalty input with option_anchors
--   (164 + 252 = 416).
accepted_htlc_penalty_input_weight_anchors :: Word64
accepted_htlc_penalty_input_weight_anchors :: Word64
accepted_htlc_penalty_input_weight_anchors = Word64
416

-- | Weight of a to_remote P2WPKH input (108 + 164 = 272).
to_remote_input_weight :: Word64
to_remote_input_weight :: Word64
to_remote_input_weight = Word64
272

-- | Base weight of a penalty transaction (4 * 53 + 2 = 214).
--
--   Non-witness: version (4) + input count (1) + output count (1) +
--   value (8) + script length (1) + P2WSH script (34) + locktime (4)
--   = 53 bytes. Witness header: 2 bytes.
--
--   The spending transactions here use it as their base weight
--   whatever their destination script.
penalty_tx_base_weight :: Word64
penalty_tx_base_weight :: Word64
penalty_tx_base_weight = Word64
214

-- | Maximum standard transaction weight (400,000).
max_standard_weight :: Word64
max_standard_weight :: Word64
max_standard_weight = Word64
400000

-- fee calculation ----------------------------------------------------

-- | The fee for a transaction of the given weight:
--
--   @fee = feerate_per_kw * weight / 1000@
--
--   rounded down, and saturating at 21 million BTC.
--
--   >>> spending_fee (FeeratePerKw 253) 324
--   Satoshi 81
spending_fee :: FeeratePerKw -> Word64 -> Satoshi
spending_fee :: FeeratePerKw -> Word64 -> Satoshi
spending_fee (FeeratePerKw !Word32
rate) !Word64
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
* Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
weight Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`quot` Integer
1000
      !m :: Word64
m = Satoshi -> Word64
BOLT1.un_satoshi Satoshi
BOLT1.max_satoshi
  in  case Word64 -> Maybe Satoshi
BOLT1.satoshi (Integer -> Word64
forall a. Num a => Integer -> a
fromInteger (Integer -> Integer -> Integer
forall a. Ord a => a -> a -> a
min Integer
f (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
m))) of
        Just Satoshi
s  -> Satoshi
s
        Maybe Satoshi
Nothing -> Satoshi
BOLT1.max_satoshi
{-# INLINE spending_fee #-}