bolt5

On-chain transaction handling for Lightning (docs.ppad.tech/bolt5).
git clone git://git.ppad.tech/bolt5.git
Log | Files | Refs | README | LICENSE

BOLT5.hs (3148B)


      1 {-# OPTIONS_HADDOCK prune #-}
      2 
      3 -- |
      4 -- Module: Lightning.Protocol.BOLT5
      5 -- Copyright: (c) 2025 Jared Tobin
      6 -- License: MIT
      7 -- Maintainer: Jared Tobin <jared@ppad.tech>
      8 --
      9 -- On-chain transaction handling for the Lightning Network, per
     10 -- [BOLT #5](https://github.com/lightning/bolts/blob/master/05-onchain.md).
     11 --
     12 -- This module implements the logic for handling channel closures:
     13 --
     14 -- * Mutual close - cooperative closure agreed by both parties
     15 -- * Unilateral close - one party publishes their commitment
     16 --   transaction
     17 -- * Revoked transaction close - penalty for publishing old state
     18 --
     19 -- = Design
     20 --
     21 -- This is a stateless toolkit of pure functions. The caller
     22 -- manages channel state (which outputs are resolved, current
     23 -- block height, etc.) and provides explicit inputs. Functions
     24 -- produce unsigned 'SpendingTx' values; the caller signs and
     25 -- assembles witnesses using bolt3 constructors.
     26 --
     27 -- = Usage
     28 --
     29 -- @
     30 -- import Lightning.Protocol.BOLT3
     31 -- import Lightning.Protocol.BOLT5
     32 --
     33 -- -- Classify outputs of our local commitment
     34 -- let outputs = classify_local_commit_outputs
     35 --       commitTx keys delay features htlcs
     36 --
     37 -- -- For each unresolved output, construct spending tx
     38 -- case uo_type output of
     39 --   SpendToLocal delay revpk delayedpk ->
     40 --     spend_to_local (uo_outpoint output)
     41 --       (uo_value output) revpk delay delayedpk
     42 --       destScript feerate
     43 --   ...
     44 -- @
     45 
     46 module Lightning.Protocol.BOLT5 (
     47     -- * Types
     48     -- ** Close identification
     49     CloseType(..)
     50 
     51     -- ** Output classification
     52   , UnresolvedOutput(..)
     53   , OutputResolution(..)
     54 
     55     -- ** HTLC output type
     56   , HTLCOutputType(..)
     57   , htlcOutputType
     58   , htlcOutputTypeWeight
     59 
     60     -- ** Spending transactions
     61   , SpendingTx(..)
     62 
     63     -- ** Penalty batching
     64   , RevokedOutput(..)
     65   , RevokedOutputType(..)
     66   , PenaltyContext(..)
     67   , revoked_output_weight
     68 
     69     -- * Weight constants (Appendix A)
     70   , to_local_penalty_witness_weight
     71   , offered_htlc_penalty_witness_weight
     72   , accepted_htlc_penalty_witness_weight
     73   , to_local_penalty_input_weight
     74   , offered_htlc_penalty_input_weight
     75   , accepted_htlc_penalty_input_weight
     76   , to_remote_input_weight
     77   , penalty_tx_base_weight
     78   , max_standard_weight
     79 
     80     -- * Fee calculation
     81   , spending_fee
     82 
     83     -- * Close identification
     84   , identify_close
     85 
     86     -- * Output classification
     87   , classify_local_commit_outputs
     88   , classify_remote_commit_outputs
     89   , classify_revoked_commit_outputs
     90 
     91     -- * Preimage extraction
     92   , extract_preimage_offered
     93   , extract_preimage_htlc_success
     94 
     95     -- * Timeout check
     96   , htlc_timed_out
     97 
     98     -- * Spending transaction construction
     99     -- ** Local commitment
    100   , spend_to_local
    101   , spend_htlc_timeout
    102   , spend_htlc_success
    103   , spend_htlc_output
    104 
    105     -- ** Remote commitment
    106   , spend_remote_htlc_timeout
    107   , spend_remote_htlc_preimage
    108 
    109     -- ** Revoked commitment
    110   , spend_revoked_to_local
    111   , spend_revoked_htlc
    112   , spend_revoked_htlc_output
    113   , spend_revoked_batch
    114 
    115     -- ** Anchor outputs
    116   , spend_anchor_owner
    117   , spend_anchor_anyone
    118   ) where
    119 
    120 import Lightning.Protocol.BOLT5.Types
    121 import Lightning.Protocol.BOLT5.Detect
    122 import Lightning.Protocol.BOLT5.Spend