@@ -1180,13 +1180,6 @@ impl HolderCommitmentTransaction {
11801180 let dummy_key = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
11811181 let dummy_sig = sign ( & secp_ctx, & secp256k1:: Message :: from_digest ( [ 42 ; 32 ] ) , & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
11821182
1183- let keys = TxCreationKeys {
1184- per_commitment_point : dummy_key. clone ( ) ,
1185- revocation_key : RevocationKey :: from_basepoint ( & secp_ctx, & RevocationBasepoint :: from ( dummy_key) , & dummy_key) ,
1186- broadcaster_htlc_key : HtlcKey :: from_basepoint ( & secp_ctx, & HtlcBasepoint :: from ( dummy_key) , & dummy_key) ,
1187- countersignatory_htlc_key : HtlcKey :: from_basepoint ( & secp_ctx, & HtlcBasepoint :: from ( dummy_key) , & dummy_key) ,
1188- broadcaster_delayed_payment_key : DelayedPaymentKey :: from_basepoint ( & secp_ctx, & DelayedPaymentBasepoint :: from ( dummy_key) , & dummy_key) ,
1189- } ;
11901183 let channel_pubkeys = ChannelPublicKeys {
11911184 funding_pubkey : dummy_key. clone ( ) ,
11921185 revocation_basepoint : RevocationBasepoint :: from ( dummy_key) ,
@@ -1208,7 +1201,7 @@ impl HolderCommitmentTransaction {
12081201 for _ in 0 ..htlcs. len ( ) {
12091202 counterparty_htlc_sigs. push ( dummy_sig) ;
12101203 }
1211- let inner = CommitmentTransaction :: new_with_auxiliary_htlc_data ( 0 , 0 , 0 , dummy_key . clone ( ) , dummy_key . clone ( ) , keys , 0 , htlcs, & channel_parameters. as_counterparty_broadcastable ( ) ) ;
1204+ let inner = CommitmentTransaction :: new_with_auxiliary_htlc_data ( 0 , & dummy_key , 0 , 0 , 0 , htlcs, & channel_parameters. as_counterparty_broadcastable ( ) , & secp_ctx ) ;
12121205 htlcs. sort_by_key ( |htlc| htlc. 0 . transaction_output_index ) ;
12131206 HolderCommitmentTransaction {
12141207 inner,
@@ -1518,12 +1511,13 @@ impl CommitmentTransaction {
15181511 /// Only include HTLCs that are above the dust limit for the channel.
15191512 ///
15201513 /// This is not exported to bindings users due to the generic though we likely should expose a version without
1521- pub fn new_with_auxiliary_htlc_data < T > ( commitment_number : u64 , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , broadcaster_funding_key : PublicKey , countersignatory_funding_key : PublicKey , keys : TxCreationKeys , feerate_per_kw : u32 , htlcs_with_aux : & mut Vec < ( HTLCOutputInCommitment , T ) > , channel_parameters : & DirectedChannelTransactionParameters ) -> CommitmentTransaction {
1514+ pub fn new_with_auxiliary_htlc_data < T > ( commitment_number : u64 , per_commitment_point : & PublicKey , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , feerate_per_kw : u32 , htlcs_with_aux : & mut Vec < ( HTLCOutputInCommitment , T ) > , channel_parameters : & DirectedChannelTransactionParameters , secp_ctx : & Secp256k1 < secp256k1 :: All > ) -> CommitmentTransaction {
15221515 let to_broadcaster_value_sat = Amount :: from_sat ( to_broadcaster_value_sat) ;
15231516 let to_countersignatory_value_sat = Amount :: from_sat ( to_countersignatory_value_sat) ;
1517+ let keys = TxCreationKeys :: from_channel_static_keys ( per_commitment_point, channel_parameters. broadcaster_pubkeys ( ) , channel_parameters. countersignatory_pubkeys ( ) , secp_ctx) ;
15241518
15251519 // Sort outputs and populate output indices while keeping track of the auxiliary data
1526- let ( outputs, htlcs) = Self :: internal_build_outputs ( & keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters, & broadcaster_funding_key , & countersignatory_funding_key ) . unwrap ( ) ;
1520+ let ( outputs, htlcs) = Self :: internal_build_outputs ( & keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters) ;
15271521
15281522 let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( commitment_number, channel_parameters) ;
15291523 let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
@@ -1552,19 +1546,19 @@ impl CommitmentTransaction {
15521546 self
15531547 }
15541548
1555- fn internal_rebuild_transaction ( & self , keys : & TxCreationKeys , channel_parameters : & DirectedChannelTransactionParameters , broadcaster_funding_key : & PublicKey , countersignatory_funding_key : & PublicKey ) -> Result < BuiltCommitmentTransaction , ( ) > {
1549+ fn internal_rebuild_transaction ( & self , keys : & TxCreationKeys , channel_parameters : & DirectedChannelTransactionParameters ) -> BuiltCommitmentTransaction {
15561550 let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( self . commitment_number , channel_parameters) ;
15571551
15581552 let mut htlcs_with_aux = self . htlcs . iter ( ) . map ( |h| ( h. clone ( ) , ( ) ) ) . collect ( ) ;
1559- let ( outputs, _) = Self :: internal_build_outputs ( keys, self . to_broadcaster_value_sat , self . to_countersignatory_value_sat , & mut htlcs_with_aux, channel_parameters, broadcaster_funding_key , countersignatory_funding_key ) ? ;
1553+ let ( outputs, _) = Self :: internal_build_outputs ( keys, self . to_broadcaster_value_sat , self . to_countersignatory_value_sat , & mut htlcs_with_aux, channel_parameters) ;
15601554
15611555 let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
15621556 let txid = transaction. compute_txid ( ) ;
15631557 let built_transaction = BuiltCommitmentTransaction {
15641558 transaction,
15651559 txid
15661560 } ;
1567- Ok ( built_transaction)
1561+ built_transaction
15681562 }
15691563
15701564 fn make_transaction ( obscured_commitment_transaction_number : u64 , txins : Vec < TxIn > , outputs : Vec < TxOut > ) -> Transaction {
@@ -1580,17 +1574,20 @@ impl CommitmentTransaction {
15801574 // - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
15811575 // caller needs to have sorted together with the HTLCs so it can keep track of the output index
15821576 // - building of a bitcoin transaction during a verify() call, in which case T is just ()
1583- fn internal_build_outputs < T > ( keys : & TxCreationKeys , to_broadcaster_value_sat : Amount , to_countersignatory_value_sat : Amount , htlcs_with_aux : & mut Vec < ( HTLCOutputInCommitment , T ) > , channel_parameters : & DirectedChannelTransactionParameters , broadcaster_funding_key : & PublicKey , countersignatory_funding_key : & PublicKey ) -> Result < ( Vec < TxOut > , Vec < HTLCOutputInCommitment > ) , ( ) > {
1584- let countersignatory_pubkeys = channel_parameters. countersignatory_pubkeys ( ) ;
1577+ fn internal_build_outputs < T > ( keys : & TxCreationKeys , to_broadcaster_value_sat : Amount , to_countersignatory_value_sat : Amount , htlcs_with_aux : & mut Vec < ( HTLCOutputInCommitment , T ) > , channel_parameters : & DirectedChannelTransactionParameters ) -> ( Vec < TxOut > , Vec < HTLCOutputInCommitment > ) {
1578+ let countersignatory_payment_point = & channel_parameters. countersignatory_pubkeys ( ) . payment_point ;
1579+ let countersignatory_funding_key = & channel_parameters. countersignatory_pubkeys ( ) . funding_pubkey ;
1580+ let broadcaster_funding_key = & channel_parameters. broadcaster_pubkeys ( ) . funding_pubkey ;
1581+ let channel_type = channel_parameters. channel_type_features ( ) ;
15851582 let contest_delay = channel_parameters. contest_delay ( ) ;
15861583
15871584 let mut txouts: Vec < ( TxOut , Option < & mut HTLCOutputInCommitment > ) > = Vec :: new ( ) ;
15881585
15891586 if to_countersignatory_value_sat > Amount :: ZERO {
1590- let script = if channel_parameters . channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( ) {
1591- get_to_countersigner_keyed_anchor_redeemscript ( & countersignatory_pubkeys . payment_point ) . to_p2wsh ( )
1587+ let script = if channel_type . supports_anchors_zero_fee_htlc_tx ( ) {
1588+ get_to_countersigner_keyed_anchor_redeemscript ( countersignatory_payment_point ) . to_p2wsh ( )
15921589 } else {
1593- ScriptBuf :: new_p2wpkh ( & Hash160 :: hash ( & countersignatory_pubkeys . payment_point . serialize ( ) ) . into ( ) )
1590+ ScriptBuf :: new_p2wpkh ( & Hash160 :: hash ( & countersignatory_payment_point . serialize ( ) ) . into ( ) )
15941591 } ;
15951592 txouts. push ( (
15961593 TxOut {
@@ -1616,7 +1613,7 @@ impl CommitmentTransaction {
16161613 ) ) ;
16171614 }
16181615
1619- if channel_parameters . channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( ) {
1616+ if channel_type . supports_anchors_zero_fee_htlc_tx ( ) {
16201617 if to_broadcaster_value_sat > Amount :: ZERO || !htlcs_with_aux. is_empty ( ) {
16211618 let anchor_script = get_keyed_anchor_redeemscript ( broadcaster_funding_key) ;
16221619 txouts. push ( (
@@ -1642,7 +1639,7 @@ impl CommitmentTransaction {
16421639
16431640 let mut htlcs = Vec :: with_capacity ( htlcs_with_aux. len ( ) ) ;
16441641 for ( htlc, _) in htlcs_with_aux {
1645- let script = get_htlc_redeemscript ( & htlc, & channel_parameters . channel_type_features ( ) , & keys) ;
1642+ let script = get_htlc_redeemscript ( htlc, channel_type , keys) ;
16461643 let txout = TxOut {
16471644 script_pubkey : script. to_p2wsh ( ) ,
16481645 value : htlc. to_bitcoin_amount ( ) ,
@@ -1674,7 +1671,7 @@ impl CommitmentTransaction {
16741671 }
16751672 outputs. push ( out. 0 ) ;
16761673 }
1677- Ok ( ( outputs, htlcs) )
1674+ ( outputs, htlcs)
16781675 }
16791676
16801677 fn internal_build_inputs ( commitment_number : u64 , channel_parameters : & DirectedChannelTransactionParameters ) -> ( u64 , Vec < TxIn > ) {
@@ -1753,14 +1750,14 @@ impl CommitmentTransaction {
17531750 ///
17541751 /// An external validating signer must call this method before signing
17551752 /// or using the built transaction.
1756- pub fn verify < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , channel_parameters : & DirectedChannelTransactionParameters , broadcaster_keys : & ChannelPublicKeys , countersignatory_keys : & ChannelPublicKeys , secp_ctx : & Secp256k1 < T > ) -> Result < TrustedCommitmentTransaction , ( ) > {
1753+ pub fn verify < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , channel_parameters : & DirectedChannelTransactionParameters , secp_ctx : & Secp256k1 < T > ) -> Result < TrustedCommitmentTransaction , ( ) > {
17571754 // This is the only field of the key cache that we trust
1758- let per_commitment_point = self . keys . per_commitment_point ;
1759- let keys = TxCreationKeys :: from_channel_static_keys ( & per_commitment_point, broadcaster_keys , countersignatory_keys , secp_ctx) ;
1755+ let per_commitment_point = & self . keys . per_commitment_point ;
1756+ let keys = TxCreationKeys :: from_channel_static_keys ( per_commitment_point, channel_parameters . broadcaster_pubkeys ( ) , channel_parameters . countersignatory_pubkeys ( ) , secp_ctx) ;
17601757 if keys != self . keys {
17611758 return Err ( ( ) ) ;
17621759 }
1763- let tx = self . internal_rebuild_transaction ( & keys, channel_parameters, & broadcaster_keys . funding_pubkey , & countersignatory_keys . funding_pubkey ) ? ;
1760+ let tx = self . internal_rebuild_transaction ( & keys, channel_parameters) ;
17641761 if self . built . transaction != tx. transaction || self . built . txid != tx. txid {
17651762 return Err ( ( ) ) ;
17661763 }
@@ -1967,8 +1964,8 @@ pub fn get_commitment_transaction_number_obscure_factor(
19671964mod tests {
19681965 use super :: { CounterpartyCommitmentSecrets , ChannelPublicKeys } ;
19691966 use crate :: chain;
1970- use crate :: ln:: chan_utils:: { get_htlc_redeemscript, get_to_countersigner_keyed_anchor_redeemscript, CommitmentTransaction , TxCreationKeys , ChannelTransactionParameters , CounterpartyChannelTransactionParameters , HTLCOutputInCommitment } ;
1971- use bitcoin:: secp256k1:: { PublicKey , SecretKey , Secp256k1 } ;
1967+ use crate :: ln:: chan_utils:: { get_htlc_redeemscript, get_to_countersigner_keyed_anchor_redeemscript, CommitmentTransaction , ChannelTransactionParameters , CounterpartyChannelTransactionParameters , HTLCOutputInCommitment } ;
1968+ use bitcoin:: secp256k1:: { self , PublicKey , SecretKey , Secp256k1 } ;
19721969 use crate :: util:: test_utils;
19731970 use crate :: sign:: { ChannelSigner , SignerProvider } ;
19741971 use bitcoin:: { Network , Txid , ScriptBuf , CompressedPublicKey } ;
@@ -1983,13 +1980,12 @@ mod tests {
19831980
19841981 struct TestCommitmentTxBuilder {
19851982 commitment_number : u64 ,
1986- holder_funding_pubkey : PublicKey ,
1987- counterparty_funding_pubkey : PublicKey ,
1988- keys : TxCreationKeys ,
1983+ per_commitment_point : PublicKey ,
19891984 feerate_per_kw : u32 ,
19901985 htlcs_with_aux : Vec < ( HTLCOutputInCommitment , ( ) ) > ,
19911986 channel_parameters : ChannelTransactionParameters ,
19921987 counterparty_pubkeys : ChannelPublicKeys ,
1988+ secp_ctx : Secp256k1 :: < secp256k1:: All > ,
19931989 }
19941990
19951991 impl TestCommitmentTxBuilder {
@@ -2014,32 +2010,23 @@ mod tests {
20142010 channel_type_features : ChannelTypeFeatures :: only_static_remote_key ( ) ,
20152011 channel_value_satoshis : 3000 ,
20162012 } ;
2017- let directed_parameters = channel_parameters. as_holder_broadcastable ( ) ;
2018- let keys = TxCreationKeys :: from_channel_static_keys (
2019- & per_commitment_point, directed_parameters. broadcaster_pubkeys ( ) ,
2020- directed_parameters. countersignatory_pubkeys ( ) , & secp_ctx,
2021- ) ;
20222013 let htlcs_with_aux = Vec :: new ( ) ;
20232014
20242015 Self {
20252016 commitment_number : 0 ,
2026- holder_funding_pubkey : holder_pubkeys. funding_pubkey ,
2027- counterparty_funding_pubkey : counterparty_pubkeys. funding_pubkey ,
2028- keys,
2017+ per_commitment_point,
20292018 feerate_per_kw : 1 ,
20302019 htlcs_with_aux,
20312020 channel_parameters,
20322021 counterparty_pubkeys,
2022+ secp_ctx,
20332023 }
20342024 }
20352025
20362026 fn build ( & mut self , to_broadcaster_sats : u64 , to_countersignatory_sats : u64 ) -> CommitmentTransaction {
20372027 CommitmentTransaction :: new_with_auxiliary_htlc_data (
2038- self . commitment_number , to_broadcaster_sats, to_countersignatory_sats,
2039- self . holder_funding_pubkey . clone ( ) ,
2040- self . counterparty_funding_pubkey . clone ( ) ,
2041- self . keys . clone ( ) , self . feerate_per_kw ,
2042- & mut self . htlcs_with_aux , & self . channel_parameters . as_holder_broadcastable ( )
2028+ self . commitment_number , & self . per_commitment_point , to_broadcaster_sats, to_countersignatory_sats, self . feerate_per_kw ,
2029+ & mut self . htlcs_with_aux , & self . channel_parameters . as_holder_broadcastable ( ) , & self . secp_ctx
20432030 )
20442031 }
20452032 }
@@ -2087,7 +2074,7 @@ mod tests {
20872074 builder. channel_parameters . channel_type_features = ChannelTypeFeatures :: only_static_remote_key ( ) ;
20882075 builder. htlcs_with_aux = vec ! [ ( received_htlc. clone( ) , ( ) ) , ( offered_htlc. clone( ) , ( ) ) ] ;
20892076 let tx = builder. build ( 3000 , 0 ) ;
2090- let keys = & builder . keys . clone ( ) ;
2077+ let keys = tx . trust ( ) . keys ( ) ;
20912078 assert_eq ! ( tx. built. transaction. output. len( ) , 3 ) ;
20922079 assert_eq ! ( tx. built. transaction. output[ 0 ] . script_pubkey, get_htlc_redeemscript( & received_htlc, & ChannelTypeFeatures :: only_static_remote_key( ) , & keys) . to_p2wsh( ) ) ;
20932080 assert_eq ! ( tx. built. transaction. output[ 1 ] . script_pubkey, get_htlc_redeemscript( & offered_htlc, & ChannelTypeFeatures :: only_static_remote_key( ) , & keys) . to_p2wsh( ) ) ;
0 commit comments