@@ -14,6 +14,7 @@ use bitcoin::blockdata::script::{Script,Builder};
1414use bitcoin:: blockdata:: opcodes;
1515use bitcoin:: blockdata:: transaction:: { TxIn , TxOut , OutPoint , Transaction , EcdsaSighashType } ;
1616use bitcoin:: util:: sighash;
17+ use bitcoin:: util:: address:: Payload ;
1718
1819use bitcoin:: hashes:: { Hash , HashEngine } ;
1920use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
@@ -25,11 +26,11 @@ use crate::ln::msgs::DecodeError;
2526use crate :: util:: ser:: { Readable , Writeable , Writer } ;
2627use crate :: util:: { byte_utils, transaction_utils} ;
2728
28- use bitcoin:: hash_types:: WPubkeyHash ;
2929use bitcoin:: secp256k1:: { SecretKey , PublicKey , Scalar } ;
3030use bitcoin:: secp256k1:: { Secp256k1 , ecdsa:: Signature , Message } ;
3131use bitcoin:: secp256k1:: Error as SecpError ;
3232use bitcoin:: { PackedLockTime , secp256k1, Sequence , Witness } ;
33+ use bitcoin:: PublicKey as BitcoinPublicKey ;
3334
3435use crate :: io;
3536use crate :: prelude:: * ;
@@ -41,13 +42,20 @@ use core::ops::Deref;
4142use crate :: chain;
4243use crate :: util:: crypto:: sign;
4344
44- pub ( crate ) const MAX_HTLCS : u16 = 483 ;
45- pub ( crate ) const OFFERED_HTLC_SCRIPT_WEIGHT : usize = 133 ;
46- pub ( crate ) const OFFERED_HTLC_SCRIPT_WEIGHT_ANCHORS : usize = 136 ;
47- // The weight of `accepted_htlc_script` can vary in function of its CLTV argument value. We define a
48- // range that encompasses both its non-anchors and anchors variants.
45+ /// Maximum number of one-way in-flight HTLC (protocol-level value).
46+ pub const MAX_HTLCS : u16 = 483 ;
47+ /// The weight of a BIP141 witnessScript for a BOLT3's "offered HTLC output" on a commitment transaction, non-anchor variant.
48+ pub const OFFERED_HTLC_SCRIPT_WEIGHT : usize = 133 ;
49+ /// The weight of a BIP141 witnessScript for a BOLT3's "offered HTLC output" on a commitment transaction, anchor variant.
50+ pub const OFFERED_HTLC_SCRIPT_WEIGHT_ANCHORS : usize = 136 ;
51+
52+ /// The weight of a BIP141 witnessScript for a BOLT3's "received HTLC output" can vary in function of its CLTV argument value.
53+ /// We define a range that encompasses both its non-anchors and anchors variants.
4954pub ( crate ) const MIN_ACCEPTED_HTLC_SCRIPT_WEIGHT : usize = 136 ;
50- pub ( crate ) const MAX_ACCEPTED_HTLC_SCRIPT_WEIGHT : usize = 143 ;
55+ /// The weight of a BIP141 witnessScript for a BOLT3's "received HTLC output" can vary in function of its CLTV argument value.
56+ /// We define a range that encompasses both its non-anchors and anchors variants.
57+ /// This is the maximum post-anchor value.
58+ pub const MAX_ACCEPTED_HTLC_SCRIPT_WEIGHT : usize = 143 ;
5159
5260/// Gets the weight for an HTLC-Success transaction.
5361#[ inline]
@@ -65,18 +73,24 @@ pub fn htlc_timeout_tx_weight(opt_anchors: bool) -> u64 {
6573 if opt_anchors { HTLC_TIMEOUT_ANCHOR_TX_WEIGHT } else { HTLC_TIMEOUT_TX_WEIGHT }
6674}
6775
76+ /// Describes the type of HTLC claim as determined by analyzing the witness.
6877#[ derive( PartialEq , Eq ) ]
69- pub ( crate ) enum HTLCClaim {
78+ pub enum HTLCClaim {
79+ /// Claims an offered output on a commitment transaction through the timeout path.
7080 OfferedTimeout ,
81+ /// Claims an offered output on a commitment transaction through the success path.
7182 OfferedPreimage ,
83+ /// Claims an accepted output on a commitment transaction through the timeout path.
7284 AcceptedTimeout ,
85+ /// Claims an accepted output on a commitment transaction through the success path.
7386 AcceptedPreimage ,
87+ /// Claims an offered/accepted output on a commitment transaction through the revocation path.
7488 Revocation ,
7589}
7690
7791impl HTLCClaim {
7892 /// Check if a given input witness attempts to claim a HTLC.
79- pub ( crate ) fn from_witness ( witness : & Witness ) -> Option < Self > {
93+ pub fn from_witness ( witness : & Witness ) -> Option < Self > {
8094 debug_assert_eq ! ( OFFERED_HTLC_SCRIPT_WEIGHT_ANCHORS , MIN_ACCEPTED_HTLC_SCRIPT_WEIGHT ) ;
8195 if witness. len ( ) < 2 {
8296 return None ;
@@ -700,7 +714,7 @@ pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, conte
700714
701715/// Gets the witnessScript for the to_remote output when anchors are enabled.
702716#[ inline]
703- pub ( crate ) fn get_to_countersignatory_with_anchors_redeemscript ( payment_point : & PublicKey ) -> Script {
717+ pub fn get_to_countersignatory_with_anchors_redeemscript ( payment_point : & PublicKey ) -> Script {
704718 Builder :: new ( )
705719 . push_slice ( & payment_point. serialize ( ) [ ..] )
706720 . push_opcode ( opcodes:: all:: OP_CHECKSIGVERIFY )
@@ -1284,7 +1298,7 @@ impl CommitmentTransaction {
12841298 let script = if opt_anchors {
12851299 get_to_countersignatory_with_anchors_redeemscript ( & countersignatory_pubkeys. payment_point ) . to_v0_p2wsh ( )
12861300 } else {
1287- get_p2wpkh_redeemscript ( & countersignatory_pubkeys. payment_point )
1301+ Payload :: p2wpkh ( & BitcoinPublicKey :: new ( countersignatory_pubkeys. payment_point ) ) . unwrap ( ) . script_pubkey ( )
12881302 } ;
12891303 txouts. push ( (
12901304 TxOut {
@@ -1590,25 +1604,21 @@ pub fn get_commitment_transaction_number_obscure_factor(
15901604 | ( ( res[ 31 ] as u64 ) << 0 * 8 )
15911605}
15921606
1593- fn get_p2wpkh_redeemscript ( key : & PublicKey ) -> Script {
1594- Builder :: new ( ) . push_opcode ( opcodes:: all:: OP_PUSHBYTES_0 )
1595- . push_slice ( & WPubkeyHash :: hash ( & key. serialize ( ) ) [ ..] )
1596- . into_script ( )
1597- }
1598-
15991607#[ cfg( test) ]
16001608mod tests {
16011609 use super :: CounterpartyCommitmentSecrets ;
16021610 use crate :: { hex, chain} ;
16031611 use crate :: prelude:: * ;
1604- use crate :: ln:: chan_utils:: { get_htlc_redeemscript, get_to_countersignatory_with_anchors_redeemscript, get_p2wpkh_redeemscript , CommitmentTransaction , TxCreationKeys , ChannelTransactionParameters , CounterpartyChannelTransactionParameters , HTLCOutputInCommitment } ;
1612+ use crate :: ln:: chan_utils:: { get_htlc_redeemscript, get_to_countersignatory_with_anchors_redeemscript, CommitmentTransaction , TxCreationKeys , ChannelTransactionParameters , CounterpartyChannelTransactionParameters , HTLCOutputInCommitment } ;
16051613 use bitcoin:: secp256k1:: { PublicKey , SecretKey , Secp256k1 } ;
16061614 use crate :: util:: test_utils;
16071615 use crate :: chain:: keysinterface:: { KeysInterface , BaseSign } ;
16081616 use bitcoin:: { Network , Txid } ;
16091617 use bitcoin:: hashes:: Hash ;
16101618 use crate :: ln:: PaymentHash ;
16111619 use bitcoin:: hashes:: hex:: ToHex ;
1620+ use bitcoin:: util:: address:: Payload ;
1621+ use bitcoin:: PublicKey as BitcoinPublicKey ;
16121622
16131623 #[ test]
16141624 fn test_anchors ( ) {
@@ -1648,7 +1658,7 @@ mod tests {
16481658 & mut htlcs_with_aux, & channel_parameters. as_holder_broadcastable ( )
16491659 ) ;
16501660 assert_eq ! ( tx. built. transaction. output. len( ) , 2 ) ;
1651- assert_eq ! ( tx. built. transaction. output[ 1 ] . script_pubkey, get_p2wpkh_redeemscript ( & counterparty_pubkeys. payment_point) ) ;
1661+ assert_eq ! ( tx. built. transaction. output[ 1 ] . script_pubkey, Payload :: p2wpkh ( & BitcoinPublicKey :: new ( counterparty_pubkeys. payment_point) ) . unwrap ( ) . script_pubkey ( ) ) ;
16521662
16531663 // Generate broadcaster and counterparty outputs as well as two anchors
16541664 let tx = CommitmentTransaction :: new_with_auxiliary_htlc_data (
0 commit comments