ppad-bolt5-0.1.0: On-chain transaction handling per BOLT #5
Copyright(c) 2025 Jared Tobin
LicenseMIT
MaintainerJared Tobin <jared@ppad.tech>
Safe HaskellNone
LanguageHaskell2010

Lightning.Protocol.BOLT5

Description

On-chain transaction handling for the Lightning Network, per BOLT #5.

This library's scope is being redesigned, and it is not yet ready for release. It covers unilateral closes only partially: it identifies a local or remote commitment on chain, classifies its outputs, extracts preimages from HTLC spends, and builds unsigned, single-input spending transactions. It doesn't detect mutual or revoked closes, assemble witnesses, or attach fee inputs to anchor spends and zero-fee HTLC transactions.

Design

This is a stateless toolkit of pure functions. The caller manages channel state (which outputs are resolved, the current block height, etc.) and provides explicit inputs. Commitment transactions are those built by ppad-bolt3, and amounts are ppad-bolt1's. Functions produce unsigned SpendingTx values; the caller signs them and assembles witnesses with bolt3's witness functions.

Usage

import qualified Lightning.Protocol.BOLT1 as BOLT1
import Lightning.Protocol.BOLT3
import Lightning.Protocol.BOLT5

-- classify the outputs of our local commitment
let outputs = classify_local_commit_outputs commit keys delay fmt

-- for each unresolved output, build a spending transaction
case uo_type output of
  SpendToLocal delay revpk delayedpk ->
    spend_to_local (uo_outpoint output) (uo_value output)
      revpk delay delayedpk dest feerate
  ...
Synopsis

Close identification

data CloseType Source #

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.

Constructors

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.

Instances

Instances details
NFData CloseType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Methods

rnf :: CloseType -> () #

Generic CloseType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Associated Types

type Rep CloseType 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep CloseType = D1 ('MetaData "CloseType" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) ((C1 ('MetaCons "MutualClose" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "LocalCommitClose" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "RemoteCommitClose" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "RevokedCommitClose" 'PrefixI 'False) (U1 :: Type -> Type)))
Show CloseType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Eq CloseType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep CloseType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep CloseType = D1 ('MetaData "CloseType" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) ((C1 ('MetaCons "MutualClose" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "LocalCommitClose" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "RemoteCommitClose" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "RevokedCommitClose" 'PrefixI 'False) (U1 :: Type -> Type)))

identify_close Source #

Arguments

:: CommitmentTx

Our local commitment tx.

-> CommitmentTx

The remote commitment tx (current).

-> ByteString

Raw serialized transaction found on chain.

-> Maybe CloseType 

Identify the type of channel close from a transaction that spends the funding output.

Parses the on-chain transaction and compares its txid with the txids of the known local and remote commitment transactions. Comparing txids rather than bytes means the signed (segwit) serialization found on chain matches the unsigned commitment.

Returns Nothing if the bytes don't parse as a transaction or the transaction matches neither commitment. Mutual close and revoked commitment detection are left to the caller (e.g. comparing with the closing transaction, or checking a secret store for older commitment numbers).

Output classification

data UnresolvedOutput Source #

An unresolved commitment transaction output.

Instances

Instances details
NFData UnresolvedOutput Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Methods

rnf :: UnresolvedOutput -> () #

Generic UnresolvedOutput Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Associated Types

type Rep UnresolvedOutput 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep UnresolvedOutput = D1 ('MetaData "UnresolvedOutput" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) (C1 ('MetaCons "UnresolvedOutput" 'PrefixI 'True) (S1 ('MetaSel ('Just "uo_outpoint") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 OutPoint) :*: (S1 ('MetaSel ('Just "uo_value") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Satoshi) :*: S1 ('MetaSel ('Just "uo_type") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 OutputResolution))))
Show UnresolvedOutput Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Eq UnresolvedOutput Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep UnresolvedOutput Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep UnresolvedOutput = D1 ('MetaData "UnresolvedOutput" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) (C1 ('MetaCons "UnresolvedOutput" 'PrefixI 'True) (S1 ('MetaSel ('Just "uo_outpoint") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 OutPoint) :*: (S1 ('MetaSel ('Just "uo_value") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Satoshi) :*: S1 ('MetaSel ('Just "uo_type") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 OutputResolution))))

data OutputResolution Source #

How to resolve an output, per BOLT #5 rules.

Constructors

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.

Instances

Instances details
NFData OutputResolution Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Methods

rnf :: OutputResolution -> () #

Generic OutputResolution Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Associated Types

type Rep OutputResolution 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep OutputResolution = D1 ('MetaData "OutputResolution" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) (((C1 ('MetaCons "Resolved" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "SpendToLocal" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ToSelfDelay) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 RevocationPubkey) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 LocalDelayedPubkey)))) :+: (C1 ('MetaCons "SpendHTLCTimeout" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 HTLC) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 CommitmentKeys) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 CommitmentFormat))) :+: C1 ('MetaCons "SpendHTLCSuccess" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 HTLC) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 CommitmentKeys) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 CommitmentFormat))))) :+: ((C1 ('MetaCons "SpendHTLCTimeoutDirect" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 HTLC)) :+: C1 ('MetaCons "SpendHTLCPreimageDirect" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 HTLC))) :+: (C1 ('MetaCons "Revoke" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 RevocationPubkey)) :+: (C1 ('MetaCons "RevokeHTLC" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 RevocationPubkey) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 HTLCOutputType)) :+: C1 ('MetaCons "AnchorSpend" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 FundingPubkey))))))
Show OutputResolution Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Eq OutputResolution Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep OutputResolution Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep OutputResolution = D1 ('MetaData "OutputResolution" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) (((C1 ('MetaCons "Resolved" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "SpendToLocal" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ToSelfDelay) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 RevocationPubkey) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 LocalDelayedPubkey)))) :+: (C1 ('MetaCons "SpendHTLCTimeout" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 HTLC) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 CommitmentKeys) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 CommitmentFormat))) :+: C1 ('MetaCons "SpendHTLCSuccess" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 HTLC) :*: (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 CommitmentKeys) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 CommitmentFormat))))) :+: ((C1 ('MetaCons "SpendHTLCTimeoutDirect" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 HTLC)) :+: C1 ('MetaCons "SpendHTLCPreimageDirect" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 HTLC))) :+: (C1 ('MetaCons "Revoke" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 RevocationPubkey)) :+: (C1 ('MetaCons "RevokeHTLC" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 RevocationPubkey) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 HTLCOutputType)) :+: C1 ('MetaCons "AnchorSpend" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 FundingPubkey))))))

classify_local_commit_outputs Source #

Arguments

:: CommitmentTx

Our local commitment transaction.

-> CommitmentKeys

Derived keys for this commitment.

-> ToSelfDelay

Remote's to_self_delay (CSV delay for our outputs).

-> CommitmentFormat 
-> [UnresolvedOutput] 

Classify the outputs of our local commitment transaction.

Per BOLT #5: when our local commitment is on chain, we must resolve each output. to_local requires a CSV-delayed spend, to_remote is resolved by the commitment itself, HTLC outputs need second-stage transactions, and our anchor can be spent immediately.

Each HTLC output's HTLC is read from its bolt3 OutputHTLC, so the commitment must be the one bolt3 built for this state.

classify_remote_commit_outputs Source #

Arguments

:: CommitmentTx

The remote commitment transaction.

-> CommitmentKeys

Derived keys for this commitment (from the remote's point of view, so local and remote are swapped).

-> [UnresolvedOutput] 

Classify the outputs of the remote commitment transaction.

Per BOLT #5: when the remote commitment is on chain, there are no CSV delays on our outputs. We can spend HTLCs we offered directly after timeout, and HTLCs we received directly with the preimage.

The commitment, its keys and its HTLCs are from the remote's point of view (as bolt3 builds the remote's commitment): an HTLC the remote offered is one we received.

classify_revoked_commit_outputs Source #

Arguments

:: CommitmentTx

The revoked commitment transaction.

-> RevocationPubkey

Revocation pubkey (derived from the revealed secret).

-> [UnresolvedOutput] 

Classify the outputs of a revoked commitment transaction.

Per BOLT #5: when a revoked commitment is on chain, we can claim to_local and every HTLC output with the revocation key. to_remote (ours) and the anchors are marked Resolved.

Preimage extraction

extract_preimage_offered Source #

Arguments

:: PaymentHash

Payment hash of the HTLC.

-> Witness

Witness of the input spending the HTLC output.

-> Maybe PaymentPreimage 

Extract a payment preimage from the witness of an input spending an offered HTLC output.

When the remote party claims an offered HTLC on our local commitment, the witness (see bolt3's offered_htlc_witness_preimage) is:

<remotehtlcsig> <payment_preimage> <witness_script>

Returns the preimage only if it hashes to the given payment hash. Any other spend of the output, e.g. by revocation, yields Nothing.

extract_preimage_htlc_success Source #

Arguments

:: PaymentHash

Payment hash of the HTLC.

-> Witness

Witness of the HTLC-success transaction input.

-> Maybe PaymentPreimage 

Extract a payment preimage from the witness of an HTLC-success transaction's input.

When the remote party claims an HTLC it received on its commitment with an HTLC-success transaction, the witness (see bolt3's htlc_success_witness) is:

0 <remotehtlcsig> <localhtlcsig> <payment_preimage>
   <witness_script>

Returns the preimage only if it hashes to the given payment hash.

Timeout check

htlc_timed_out :: Word32 -> HTLC -> Bool Source #

Whether an HTLC has timed out at the given block height: whether the height is at least the HTLC's CLTV expiry.

>>> let Just amt = BOLT1.milli_satoshi 1000000
>>> let Just ph = BOLT1.payment_hash (BS.replicate 32 0xaa)
>>> let htlc = HTLC HTLCOffered amt ph (CltvExpiry 800000)
>>> htlc_timed_out 799999 htlc
False
>>> htlc_timed_out 800000 htlc
True

Spending transactions

data SpendingTx Source #

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.

Constructors

SpendingTx 

Fields

Instances

Instances details
NFData SpendingTx Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Methods

rnf :: SpendingTx -> () #

Generic SpendingTx Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Associated Types

type Rep SpendingTx 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep SpendingTx = D1 ('MetaData "SpendingTx" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) (C1 ('MetaCons "SpendingTx" 'PrefixI 'True) ((S1 ('MetaSel ('Just "stx_tx") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Tx) :*: S1 ('MetaSel ('Just "stx_input_script") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Script)) :*: (S1 ('MetaSel ('Just "stx_input_value") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Satoshi) :*: S1 ('MetaSel ('Just "stx_sighash_type") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 SighashType))))
Show SpendingTx Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Eq SpendingTx Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep SpendingTx Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep SpendingTx = D1 ('MetaData "SpendingTx" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) (C1 ('MetaCons "SpendingTx" 'PrefixI 'True) ((S1 ('MetaSel ('Just "stx_tx") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Tx) :*: S1 ('MetaSel ('Just "stx_input_script") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Script)) :*: (S1 ('MetaSel ('Just "stx_input_value") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Satoshi) :*: S1 ('MetaSel ('Just "stx_sighash_type") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 SighashType))))

Local commitment

spend_to_local Source #

Arguments

:: OutPoint

Outpoint of the to_local output.

-> Satoshi

Value of the to_local output.

-> RevocationPubkey 
-> ToSelfDelay 
-> LocalDelayedPubkey 
-> Script

Destination scriptPubKey.

-> FeeratePerKw 
-> Maybe SpendingTx 

Spend the to_local output of our local commitment tx.

The input's nSequence is the to_self_delay, so the transaction is valid only once the delay has passed. The caller signs with the local delayed privkey and uses bolt3's to_local_witness_spend.

Returns Nothing if the fee would exceed the output value, or leave an output below the destination's dust threshold.

spend_htlc_tx Source #

Arguments

:: HTLCContext 
-> CommitmentKeys

Keys for our local commitment (for the witness script).

-> Maybe SpendingTx 

Build the HTLC-timeout (for an offered HTLC) or HTLC-success (for a received HTLC) transaction spending an HTLC output of our local commitment, with bolt3's build_htlc_tx.

The sighash type is that of the local signature, which is always SIGHASH_ALL. The remote's htlc_signature uses bolt3's remote_htlc_sighash (SIGHASH_SINGLE|SIGHASH_ANYONECANPAY under Anchors). The witness is bolt3's htlc_timeout_witness or htlc_success_witness.

Under Anchors the transaction pays no fee; it must be combined with other inputs before broadcast.

Returns Nothing if bolt3 can't build the transaction (the fee exceeds the HTLC amount).

spend_htlc_output Source #

Arguments

:: OutPoint

Outpoint of the second-stage output.

-> Satoshi

Value of the second-stage output.

-> RevocationPubkey 
-> ToSelfDelay 
-> LocalDelayedPubkey 
-> Script

Destination scriptPubKey.

-> FeeratePerKw 
-> Maybe SpendingTx 

Spend the output of an HTLC-timeout or HTLC-success transaction after the CSV delay.

The output uses the to_local script, so this is spend_to_local. The caller signs with the local delayed privkey and uses bolt3's to_local_witness_spend.

Remote commitment

spend_remote_htlc_timeout Source #

Arguments

:: OutPoint

Outpoint of the HTLC output.

-> Satoshi

Value of the HTLC output.

-> HTLC

The HTLC being spent.

-> CommitmentKeys

Keys for the remote commitment.

-> CommitmentFormat 
-> Script

Destination scriptPubKey.

-> FeeratePerKw 
-> Maybe SpendingTx 

Spend an HTLC we offered directly, after it times out, from the remote commitment (where it is a received HTLC).

The locktime is the HTLC's CLTV expiry. The caller signs with our HTLC privkey and uses bolt3's received_htlc_witness_timeout.

Returns Nothing if the fee would exceed the output value, or leave an output below the destination's dust threshold.

spend_remote_htlc_preimage Source #

Arguments

:: OutPoint

Outpoint of the HTLC output.

-> Satoshi

Value of the HTLC output.

-> HTLC

The HTLC being spent.

-> CommitmentKeys

Keys for the remote commitment.

-> CommitmentFormat 
-> Script

Destination scriptPubKey.

-> FeeratePerKw 
-> Maybe SpendingTx 

Spend an HTLC we received directly with the preimage, from the remote commitment (where it is an offered HTLC).

The caller signs with our HTLC privkey and uses bolt3's offered_htlc_witness_preimage.

Returns Nothing if the fee would exceed the output value, or leave an output below the destination's dust threshold.

Revoked commitment

spend_revoked_to_local Source #

Arguments

:: OutPoint

Outpoint of the to_local output.

-> Satoshi

Value of the to_local output.

-> RevocationPubkey 
-> ToSelfDelay 
-> LocalDelayedPubkey 
-> Script

Destination scriptPubKey.

-> FeeratePerKw 
-> Maybe SpendingTx 

Spend a revoked to_local output with the revocation key.

The caller signs with the revocation privkey and uses bolt3's to_local_witness_revoke.

Returns Nothing if the fee would exceed the output value, or leave an output below the destination's dust threshold.

spend_revoked_htlc Source #

Arguments

:: OutPoint

Outpoint of the HTLC output.

-> Satoshi

Value of the HTLC output.

-> HTLCOutputType

Whether offered or received HTLC.

-> RevocationPubkey 
-> CommitmentKeys 
-> CommitmentFormat 
-> PaymentHash 
-> Script

Destination scriptPubKey.

-> FeeratePerKw 
-> Maybe SpendingTx 

Spend a revoked HTLC output with the revocation key.

The caller signs with the revocation privkey and uses bolt3's offered_htlc_witness_revoke or received_htlc_witness_revoke, according to the output type.

Returns Nothing if the fee would exceed the output value, or leave an output below the destination's dust threshold.

spend_revoked_htlc_output Source #

Arguments

:: OutPoint

Outpoint of the second-stage output.

-> Satoshi

Value of the second-stage output.

-> RevocationPubkey 
-> ToSelfDelay 
-> LocalDelayedPubkey 
-> Script

Destination scriptPubKey.

-> FeeratePerKw 
-> Maybe SpendingTx 

Spend the output of a revoked HTLC-timeout or HTLC-success transaction with the revocation key.

The output uses the to_local script, so this is spend_revoked_to_local. The caller signs with the revocation privkey and uses bolt3's to_local_witness_revoke.

Anchor outputs

spend_anchor_owner Source #

Arguments

:: OutPoint

Outpoint of the anchor output.

-> Satoshi

Value of the anchor output (330 sats).

-> FundingPubkey 
-> Script

Destination scriptPubKey.

-> SpendingTx 

Spend an anchor output as its owner (immediately).

The transaction pays no fee and has a single input: it must be combined with other inputs before broadcast. The caller signs with the funding privkey and uses bolt3's anchor_witness_owner.

spend_anchor_anyone Source #

Arguments

:: OutPoint

Outpoint of the anchor output.

-> Satoshi

Value of the anchor output (330 sats).

-> FundingPubkey 
-> Script

Destination scriptPubKey.

-> SpendingTx 

Spend an anchor output as anyone (16 blocks after the commitment confirms).

As spend_anchor_owner, the transaction pays no fee. The witness is bolt3's anchor_witness_anyone, which needs no signature.

Revoked outputs

data HTLCOutputType Source #

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.

Constructors

HTLCOfferedOutput !CltvExpiry

Offered HTLC output with CLTV expiry.

HTLCReceivedOutput !CltvExpiry

Received HTLC output with CLTV expiry.

Instances

Instances details
NFData HTLCOutputType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Methods

rnf :: HTLCOutputType -> () #

Generic HTLCOutputType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Associated Types

type Rep HTLCOutputType 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep HTLCOutputType = D1 ('MetaData "HTLCOutputType" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) (C1 ('MetaCons "HTLCOfferedOutput" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 CltvExpiry)) :+: C1 ('MetaCons "HTLCReceivedOutput" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 CltvExpiry)))
Show HTLCOutputType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Eq HTLCOutputType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep HTLCOutputType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep HTLCOutputType = D1 ('MetaData "HTLCOutputType" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) (C1 ('MetaCons "HTLCOfferedOutput" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 CltvExpiry)) :+: C1 ('MetaCons "HTLCReceivedOutput" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 CltvExpiry)))

htlc_output_type :: OutputType -> Maybe HTLCOutputType Source #

The HTLCOutputType of a bolt3 OutputType, or Nothing if it isn't an HTLC output.

>>> htlc_output_type OutputToLocal
Nothing

data RevokedOutput Source #

A revoked output that can be swept with a revocation key.

Instances

Instances details
NFData RevokedOutput Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Methods

rnf :: RevokedOutput -> () #

Generic RevokedOutput Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Associated Types

type Rep RevokedOutput 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep RevokedOutput = D1 ('MetaData "RevokedOutput" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) (C1 ('MetaCons "RevokedOutput" 'PrefixI 'True) (S1 ('MetaSel ('Just "ro_outpoint") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 OutPoint) :*: (S1 ('MetaSel ('Just "ro_value") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Satoshi) :*: S1 ('MetaSel ('Just "ro_type") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 RevokedOutputType))))
Show RevokedOutput Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Eq RevokedOutput Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep RevokedOutput Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep RevokedOutput = D1 ('MetaData "RevokedOutput" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) (C1 ('MetaCons "RevokedOutput" 'PrefixI 'True) (S1 ('MetaSel ('Just "ro_outpoint") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 OutPoint) :*: (S1 ('MetaSel ('Just "ro_value") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Satoshi) :*: S1 ('MetaSel ('Just "ro_type") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 RevokedOutputType))))

data RevokedOutputType Source #

What kind of revoked output is being swept.

Constructors

RevokedToLocal

Revoked to_local output.

RevokedHTLC !HTLCOutputType

Revoked HTLC output.

Instances

Instances details
NFData RevokedOutputType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Methods

rnf :: RevokedOutputType -> () #

Generic RevokedOutputType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Associated Types

type Rep RevokedOutputType 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep RevokedOutputType = D1 ('MetaData "RevokedOutputType" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) (C1 ('MetaCons "RevokedToLocal" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "RevokedHTLC" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 HTLCOutputType)))
Show RevokedOutputType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Eq RevokedOutputType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep RevokedOutputType Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep RevokedOutputType = D1 ('MetaData "RevokedOutputType" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.1.0-6lIeVQo7q3B6JOngQAJKdz" 'False) (C1 ('MetaCons "RevokedToLocal" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "RevokedHTLC" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 HTLCOutputType)))

Weights (Appendix A)

revoked_output_weight :: CommitmentFormat -> RevokedOutput -> Word64 Source #

Penalty input weight for a revoked output.

htlc_output_type_weight :: CommitmentFormat -> HTLCOutputType -> Word64 Source #

Penalty input weight for an HTLC output.

>>> htlc_output_type_weight Anchors (HTLCOfferedOutput (CltvExpiry 500))
410

to_local_penalty_witness_weight :: Word64 Source #

Expected weight of the to_local penalty transaction witness (160 weight units).

offered_htlc_penalty_witness_weight :: Word64 Source #

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

accepted_htlc_penalty_witness_weight :: Word64 Source #

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

offered_htlc_penalty_witness_weight_anchors :: Word64 Source #

Expected weight of the offered_htlc penalty transaction witness with option_anchors (246 weight units), whose witness script is 136 bytes.

accepted_htlc_penalty_witness_weight_anchors :: Word64 Source #

Expected weight of the accepted_htlc penalty transaction witness with option_anchors (252 weight units), whose witness script is 142 bytes.

to_local_penalty_input_weight :: Word64 Source #

Weight of a to_local penalty input (164 + 160 = 324).

offered_htlc_penalty_input_weight :: Word64 Source #

Weight of an offered_htlc penalty input (164 + 243 = 407).

accepted_htlc_penalty_input_weight :: Word64 Source #

Weight of an accepted_htlc penalty input (164 + 249 = 413).

offered_htlc_penalty_input_weight_anchors :: Word64 Source #

Weight of an offered_htlc penalty input with option_anchors (164 + 246 = 410).

accepted_htlc_penalty_input_weight_anchors :: Word64 Source #

Weight of an accepted_htlc penalty input with option_anchors (164 + 252 = 416).

to_remote_input_weight :: Word64 Source #

Weight of a to_remote P2WPKH input (108 + 164 = 272).

penalty_tx_base_weight :: Word64 Source #

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.

max_standard_weight :: Word64 Source #

Maximum standard transaction weight (400,000).

Fees

spending_fee :: FeeratePerKw -> Word64 -> Satoshi Source #

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