ppad-bolt5-0.0.1: 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 module implements the logic for handling channel closures:

  • Mutual close - cooperative closure agreed by both parties
  • Unilateral close - one party publishes their commitment transaction
  • Revoked transaction close - penalty for publishing old state

Design

This is a stateless toolkit of pure functions. The caller manages channel state (which outputs are resolved, current block height, etc.) and provides explicit inputs. Functions produce unsigned SpendingTx values; the caller signs and assembles witnesses using bolt3 constructors.

Usage

import Lightning.Protocol.BOLT3
import Lightning.Protocol.BOLT5

-- Classify outputs of our local commitment
let outputs = classify_local_commit_outputs
      commitTx keys delay features htlcs

-- For each unresolved output, construct spending tx
case uo_type output of
  SpendToLocal delay revpk delayedpk ->
    spend_to_local (uo_outpoint output)
      (uo_value output) revpk delay delayedpk
      destScript feerate
  ...
Synopsis

Types

Close identification

data CloseType Source #

What kind of close was detected on chain.

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
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.0.1-5N7Wen1sZDiBgqKJHrpe1Z" '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.0.1-5N7Wen1sZDiBgqKJHrpe1Z" '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)))

Output classification

data UnresolvedOutput Source #

An unresolved commitment transaction output.

Instances

Instances details
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.0.1-5N7Wen1sZDiBgqKJHrpe1Z" '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.0.1-5N7Wen1sZDiBgqKJHrpe1Z" '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

Already resolved (e.g. to_remote on local commit).

SpendToLocal !ToSelfDelay !RevocationPubkey !LocalDelayedPubkey

Spend to_local after CSV delay.

SpendHTLCTimeout !HTLC !CommitmentKeys !ChannelFeatures

Spend via HTLC-timeout second-stage tx (local commit, local offer).

SpendHTLCSuccess !HTLC !CommitmentKeys !ChannelFeatures

Spend via HTLC-success second-stage tx (local commit, remote offer).

SpendHTLCTimeoutDirect !HTLC

Spend HTLC directly after timeout (remote commit, local offer).

SpendHTLCPreimageDirect !HTLC

Spend HTLC directly with preimage (remote commit, remote offer).

Revoke !RevocationPubkey

Spend revoked to_local with revocation key.

RevokeHTLC !RevocationPubkey !OutputType

Spend revoked HTLC output with revocation key.

AnchorSpend !FundingPubkey

Spend anchor output.

Instances

Instances details
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.0.1-5N7Wen1sZDiBgqKJHrpe1Z" '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 ChannelFeatures))) :+: 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 ChannelFeatures))))) :+: ((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 OutputType)) :+: 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.0.1-5N7Wen1sZDiBgqKJHrpe1Z" '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 ChannelFeatures))) :+: 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 ChannelFeatures))))) :+: ((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 OutputType)) :+: C1 ('MetaCons "AnchorSpend" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 FundingPubkey))))))

Spending transactions

data SpendingTx Source #

Unsigned spending transaction, ready for caller to sign.

The caller uses bolt3 witness constructors to assemble the final witness after signing.

Constructors

SpendingTx 

Fields

Instances

Instances details
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.0.1-5N7Wen1sZDiBgqKJHrpe1Z" '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.0.1-5N7Wen1sZDiBgqKJHrpe1Z" '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))))

Penalty batching

data PenaltyContext Source #

Context for constructing batched penalty transactions.

Constructors

PenaltyContext 

Fields

Instances

Instances details
Generic PenaltyContext Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Associated Types

type Rep PenaltyContext 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep PenaltyContext = D1 ('MetaData "PenaltyContext" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.0.1-5N7Wen1sZDiBgqKJHrpe1Z" 'False) (C1 ('MetaCons "PenaltyContext" 'PrefixI 'True) ((S1 ('MetaSel ('Just "pc_outputs") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (NonEmpty UnresolvedOutput)) :*: S1 ('MetaSel ('Just "pc_revocation_key") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 RevocationPubkey)) :*: (S1 ('MetaSel ('Just "pc_destination") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Script) :*: S1 ('MetaSel ('Just "pc_feerate") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 FeeratePerKw))))
Show PenaltyContext Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

Eq PenaltyContext Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep PenaltyContext Source # 
Instance details

Defined in Lightning.Protocol.BOLT5.Types

type Rep PenaltyContext = D1 ('MetaData "PenaltyContext" "Lightning.Protocol.BOLT5.Types" "ppad-bolt5-0.0.1-5N7Wen1sZDiBgqKJHrpe1Z" 'False) (C1 ('MetaCons "PenaltyContext" 'PrefixI 'True) ((S1 ('MetaSel ('Just "pc_outputs") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (NonEmpty UnresolvedOutput)) :*: S1 ('MetaSel ('Just "pc_revocation_key") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 RevocationPubkey)) :*: (S1 ('MetaSel ('Just "pc_destination") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Script) :*: S1 ('MetaSel ('Just "pc_feerate") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 FeeratePerKw))))

Weight constants (Appendix A)

to_local_penalty_witness_weight :: Word64 Source #

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

offered_htlc_penalty_witness_weight :: Word64 Source #

Expected weight of the offered_htlc penalty transaction witness (243 bytes).

accepted_htlc_penalty_witness_weight :: Word64 Source #

Expected weight of the accepted_htlc penalty transaction witness (249 bytes).

to_local_penalty_input_weight :: Word64 Source #

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

offered_htlc_penalty_input_weight :: Word64 Source #

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

accepted_htlc_penalty_input_weight :: Word64 Source #

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

to_remote_input_weight :: Word64 Source #

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

penalty_tx_base_weight :: Word64 Source #

Base weight of a penalty transaction (4*53 + 2 = 214 bytes).

Non-witness: version(4) + input_count(1) + output_count(1) + value(8) + script_len(1) + p2wsh_script(34) + locktime(4) = 53 Witness header: 2 bytes.

max_standard_weight :: Word64 Source #

Maximum standard transaction weight (400,000 bytes).

Fee calculation

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

Calculate the fee for a spending transaction given its weight.

fee = feerate_per_kw * weight / 1000

Close identification

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.

Compares the on-chain transaction bytes against the known local and remote commitment transaction serializations (stripped/unsigned) to determine whether it's a local or remote commitment close.

Returns Nothing if the transaction doesn't match either commitment. Mutual close and revoked commitment detection require additional checks by the caller (e.g. comparing closing tx format, checking a secret store for older commitment numbers).

Output classification

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).

-> ChannelFeatures

Channel feature flags.

-> [HTLC]

HTLCs in this commitment.

-> [UnresolvedOutput] 

Classify outputs of our local commitment transaction.

Per BOLT #5: when we discover our local commitment 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 anchors can be spent immediately.

classify_remote_commit_outputs Source #

Arguments

:: CommitmentTx

The remote commitment transaction.

-> CommitmentKeys

Derived keys for this commitment (from remote's perspective, so local/remote are swapped).

-> ChannelFeatures

Channel feature flags.

-> [HTLC]

HTLCs in this commitment.

-> [UnresolvedOutput] 

Classify outputs of the remote commitment transaction.

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

classify_revoked_commit_outputs Source #

Arguments

:: CommitmentTx

The revoked commitment transaction.

-> CommitmentKeys

Derived keys for the revoked commitment.

-> RevocationPubkey

Revocation pubkey (derived from the revealed secret).

-> ChannelFeatures

Channel feature flags.

-> [HTLC]

HTLCs in the revoked commitment.

-> [UnresolvedOutput] 

Classify outputs of a revoked commitment transaction.

Per BOLT #5: when we discover a revoked commitment, we can claim everything using the revocation key. to_local is spent via revocation, HTLCs are spent via revocation, and we can also optionally sweep to_remote.

Preimage extraction

extract_preimage_offered :: Witness -> Maybe PaymentPreimage Source #

Extract a payment preimage from an offered HTLC witness.

When the remote party claims an offered HTLC on our local commitment, the witness contains the preimage. The witness stack for a preimage claim is:

<remotehtlcsig> <paymentPreimage>

The preimage is the second item (32 bytes) and must hash to the expected payment hash.

extract_preimage_htlc_success :: Witness -> Maybe PaymentPreimage Source #

Extract a payment preimage from an HTLC-success transaction witness.

When the remote party uses an HTLC-success tx on their commitment to claim a received HTLC, the witness contains the preimage. The witness stack is:

0 <remotehtlcsig> <localhtlcsig> <paymentPreimage>

The preimage is the fourth item (32 bytes).

Timeout check

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

Check if an HTLC has timed out at the given block height.

An HTLC has timed out when the current block height is equal to or greater than the HTLC's CLTV expiry.

Spending transaction construction

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.

Requires waiting for the CSV delay (to_self_delay) before broadcasting. The caller signs with the local delayed privkey and uses to_local_witness_spend from bolt3.

Returns Nothing if the fee would exceed the output value.

The input nSequence is set to the to_self_delay value.

spend_htlc_timeout Source #

Arguments

:: HTLCContext 
-> CommitmentKeys

Full commitment keys (needed for witness script).

-> SpendingTx 

Construct an HTLC-timeout second-stage transaction.

Used when we offered an HTLC on our local commitment and it has timed out. The bolt3 build_htlc_timeout_tx function constructs the HTLC-timeout tx; this wraps it as a SpendingTx with the witness script and sighash metadata.

spend_htlc_success Source #

Arguments

:: HTLCContext 
-> CommitmentKeys

Full commitment keys (needed for witness script).

-> SpendingTx 

Construct an HTLC-success second-stage transaction.

Used when we received an HTLC on our local commitment and have the preimage. The bolt3 build_htlc_success_tx function constructs the HTLC-success tx; this wraps it as a SpendingTx.

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 a second-stage HTLC output (HTLC-timeout or HTLC-success output) after the CSV delay.

The output of an HTLC-timeout or HTLC-success tx uses the same to_local script. The caller signs with the local delayed privkey and uses htlc_output_witness_spend.

Returns Nothing if the fee would exceed the output value.

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.

-> ChannelFeatures 
-> Script

Destination scriptPubKey.

-> FeeratePerKw 
-> Maybe SpendingTx 

Spend an offered HTLC directly after timeout on the remote commitment.

On the remote commitment, their received HTLCs (our offered) have timed out and we can sweep them directly.

Returns Nothing if the fee would exceed the output value.

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.

-> ChannelFeatures 
-> Script

Destination scriptPubKey.

-> FeeratePerKw 
-> Maybe SpendingTx 

Spend a received HTLC directly with preimage on the remote commitment.

On the remote commitment, their offered HTLCs (our received) can be claimed with the payment preimage.

Returns Nothing if the fee would exceed the output value.

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 using the revocation key.

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

Returns Nothing if the fee would exceed the output value.

spend_revoked_htlc Source #

Arguments

:: OutPoint

Outpoint of the HTLC output.

-> Satoshi

Value of the HTLC output.

-> OutputType

Whether offered or received HTLC.

-> RevocationPubkey 
-> CommitmentKeys 
-> ChannelFeatures 
-> PaymentHash 
-> Script

Destination scriptPubKey.

-> FeeratePerKw 
-> Maybe SpendingTx 

Spend a revoked HTLC output using the revocation key.

The caller signs with the revocation privkey and uses offered_htlc_witness_revoke or received_htlc_witness_revoke from bolt3, depending on the output type.

Returns Nothing if the output type is not an HTLC, or if the fee would exceed the output value.

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 a revoked second-stage HTLC output (HTLC-timeout or HTLC-success output) using the revocation key.

The output of a revoked HTLC-timeout/success tx uses the to_local script. The caller signs with the revocation privkey and uses htlc_output_witness_revoke.

Returns Nothing if the fee would exceed the output value.

spend_revoked_batch :: PenaltyContext -> Maybe SpendingTx Source #

Construct a batched penalty transaction spending multiple revoked outputs.

Per BOLT #5, up to 483 bidirectional HTLCs plus to_local can be resolved in a single penalty transaction (within the 400,000 weight limit). The caller signs each input with the revocation privkey. | Returns Nothing if the total fee would exceed the total input value.

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 the owner (immediately).

The caller signs with the funding privkey and uses anchor_witness_owner from bolt3.

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 (after 16 blocks).

Uses anchor_witness_anyone from bolt3 (empty signature).