@@ -14,6 +14,7 @@ use bitcoin::blockdata::script::{Script,Builder};
14
14
use bitcoin:: blockdata:: opcodes;
15
15
use bitcoin:: blockdata:: transaction:: { TxIn , TxOut , OutPoint , Transaction , EcdsaSighashType } ;
16
16
use bitcoin:: util:: sighash;
17
+ use bitcoin:: util:: address:: Payload ;
17
18
18
19
use bitcoin:: hashes:: { Hash , HashEngine } ;
19
20
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
@@ -25,11 +26,11 @@ use crate::ln::msgs::DecodeError;
25
26
use crate :: util:: ser:: { Readable , Writeable , Writer } ;
26
27
use crate :: util:: { byte_utils, transaction_utils} ;
27
28
28
- use bitcoin:: hash_types:: WPubkeyHash ;
29
29
use bitcoin:: secp256k1:: { SecretKey , PublicKey , Scalar } ;
30
30
use bitcoin:: secp256k1:: { Secp256k1 , ecdsa:: Signature , Message } ;
31
31
use bitcoin:: secp256k1:: Error as SecpError ;
32
32
use bitcoin:: { PackedLockTime , secp256k1, Sequence , Witness } ;
33
+ use bitcoin:: PublicKey as BitcoinPublicKey ;
33
34
34
35
use crate :: io;
35
36
use crate :: prelude:: * ;
@@ -41,13 +42,20 @@ use core::ops::Deref;
41
42
use crate :: chain;
42
43
use crate :: util:: crypto:: sign;
43
44
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.
49
54
pub ( 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 ;
51
59
52
60
/// Gets the weight for an HTLC-Success transaction.
53
61
#[ inline]
@@ -65,18 +73,24 @@ pub fn htlc_timeout_tx_weight(opt_anchors: bool) -> u64 {
65
73
if opt_anchors { HTLC_TIMEOUT_ANCHOR_TX_WEIGHT } else { HTLC_TIMEOUT_TX_WEIGHT }
66
74
}
67
75
76
+ /// Describes the type of HTLC claim as determined by analyzing the witness.
68
77
#[ 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.
70
80
OfferedTimeout ,
81
+ /// Claims an offered output on a commitment transaction through the success path.
71
82
OfferedPreimage ,
83
+ /// Claims an accepted output on a commitment transaction through the timeout path.
72
84
AcceptedTimeout ,
85
+ /// Claims an accepted output on a commitment transaction through the success path.
73
86
AcceptedPreimage ,
87
+ /// Claims an offered/accepted output on a commitment transaction through the revocation path.
74
88
Revocation ,
75
89
}
76
90
77
91
impl HTLCClaim {
78
92
/// 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 > {
80
94
debug_assert_eq ! ( OFFERED_HTLC_SCRIPT_WEIGHT_ANCHORS , MIN_ACCEPTED_HTLC_SCRIPT_WEIGHT ) ;
81
95
if witness. len ( ) < 2 {
82
96
return None ;
@@ -700,7 +714,7 @@ pub fn build_htlc_transaction(commitment_txid: &Txid, feerate_per_kw: u32, conte
700
714
701
715
/// Gets the witnessScript for the to_remote output when anchors are enabled.
702
716
#[ 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 {
704
718
Builder :: new ( )
705
719
. push_slice ( & payment_point. serialize ( ) [ ..] )
706
720
. push_opcode ( opcodes:: all:: OP_CHECKSIGVERIFY )
@@ -1284,7 +1298,7 @@ impl CommitmentTransaction {
1284
1298
let script = if opt_anchors {
1285
1299
get_to_countersignatory_with_anchors_redeemscript ( & countersignatory_pubkeys. payment_point ) . to_v0_p2wsh ( )
1286
1300
} else {
1287
- get_p2wpkh_redeemscript ( & countersignatory_pubkeys. payment_point )
1301
+ Payload :: p2wpkh ( & BitcoinPublicKey :: new ( countersignatory_pubkeys. payment_point ) ) . unwrap ( ) . script_pubkey ( )
1288
1302
} ;
1289
1303
txouts. push ( (
1290
1304
TxOut {
@@ -1590,25 +1604,21 @@ pub fn get_commitment_transaction_number_obscure_factor(
1590
1604
| ( ( res[ 31 ] as u64 ) << 0 * 8 )
1591
1605
}
1592
1606
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
-
1599
1607
#[ cfg( test) ]
1600
1608
mod tests {
1601
1609
use super :: CounterpartyCommitmentSecrets ;
1602
1610
use crate :: { hex, chain} ;
1603
1611
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 } ;
1605
1613
use bitcoin:: secp256k1:: { PublicKey , SecretKey , Secp256k1 } ;
1606
1614
use crate :: util:: test_utils;
1607
1615
use crate :: chain:: keysinterface:: { KeysInterface , BaseSign } ;
1608
1616
use bitcoin:: { Network , Txid } ;
1609
1617
use bitcoin:: hashes:: Hash ;
1610
1618
use crate :: ln:: PaymentHash ;
1611
1619
use bitcoin:: hashes:: hex:: ToHex ;
1620
+ use bitcoin:: util:: address:: Payload ;
1621
+ use bitcoin:: PublicKey as BitcoinPublicKey ;
1612
1622
1613
1623
#[ test]
1614
1624
fn test_anchors ( ) {
@@ -1648,7 +1658,7 @@ mod tests {
1648
1658
& mut htlcs_with_aux, & channel_parameters. as_holder_broadcastable ( )
1649
1659
) ;
1650
1660
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 ( ) ) ;
1652
1662
1653
1663
// Generate broadcaster and counterparty outputs as well as two anchors
1654
1664
let tx = CommitmentTransaction :: new_with_auxiliary_htlc_data (
0 commit comments