@@ -20,7 +20,6 @@ use bitcoin::sighash::EcdsaSighashType;
2020use bitcoin:: transaction:: Version ;
2121
2222use bitcoin:: hashes:: { Hash , HashEngine } ;
23- use bitcoin:: hashes:: hash160:: Hash as Hash160 ;
2423use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
2524use bitcoin:: hashes:: ripemd160:: Hash as Ripemd160 ;
2625use bitcoin:: hash_types:: Txid ;
@@ -1135,7 +1134,13 @@ impl HolderCommitmentTransaction {
11351134 for _ in 0 ..htlcs. len ( ) {
11361135 counterparty_htlc_sigs. push ( dummy_sig) ;
11371136 }
1138- 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 ( ) ) ;
1137+ let channel_parameters = channel_parameters. as_counterparty_broadcastable ( ) ;
1138+ let counterparty_payment_script = get_counterparty_payment_script ( & channel_parameters. channel_type_features ( ) , & channel_parameters. countersignatory_pubkeys ( ) . payment_point ) ;
1139+ let counterparty_txout = TxOut {
1140+ script_pubkey : counterparty_payment_script,
1141+ value : Amount :: ZERO ,
1142+ } ;
1143+ let inner = CommitmentTransaction :: new_with_auxiliary_htlc_data ( 0 , 0 , counterparty_txout, dummy_key. clone ( ) , dummy_key. clone ( ) , keys, 0 , htlcs, & channel_parameters) ;
11391144 htlcs. sort_by_key ( |htlc| htlc. 0 . transaction_output_index ) ;
11401145 HolderCommitmentTransaction {
11411146 inner,
@@ -1445,12 +1450,12 @@ impl CommitmentTransaction {
14451450 /// Only include HTLCs that are above the dust limit for the channel.
14461451 ///
14471452 /// This is not exported to bindings users due to the generic though we likely should expose a version without
1448- 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 {
1453+ pub fn new_with_auxiliary_htlc_data < T > ( commitment_number : u64 , to_broadcaster_value_sat : u64 , to_countersignatory_txout : TxOut , 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 {
14491454 let to_broadcaster_value_sat = Amount :: from_sat ( to_broadcaster_value_sat) ;
1450- let to_countersignatory_value_sat = Amount :: from_sat ( to_countersignatory_value_sat ) ;
1455+ let to_countersignatory_value_sat = to_countersignatory_txout . value ;
14511456
14521457 // Sort outputs and populate output indices while keeping track of the auxiliary data
1453- 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 ( ) ;
1458+ let ( outputs, htlcs) = Self :: internal_build_outputs ( & keys, to_broadcaster_value_sat, to_countersignatory_txout , htlcs_with_aux, channel_parameters, & broadcaster_funding_key, & countersignatory_funding_key) . unwrap ( ) ;
14541459
14551460 let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( commitment_number, channel_parameters) ;
14561461 let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
@@ -1479,11 +1484,15 @@ impl CommitmentTransaction {
14791484 self
14801485 }
14811486
1482- fn internal_rebuild_transaction ( & self , keys : & TxCreationKeys , channel_parameters : & DirectedChannelTransactionParameters , broadcaster_funding_key : & PublicKey , countersignatory_funding_key : & PublicKey ) -> Result < BuiltCommitmentTransaction , ( ) > {
1487+ fn internal_rebuild_transaction ( & self , keys : & TxCreationKeys , channel_parameters : & DirectedChannelTransactionParameters , broadcaster_funding_key : & PublicKey , countersignatory_funding_key : & PublicKey , to_countersignatory_spk : ScriptBuf ) -> Result < BuiltCommitmentTransaction , ( ) > {
14831488 let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( self . commitment_number , channel_parameters) ;
14841489
1490+ let to_countersignatory_txout = TxOut {
1491+ script_pubkey : to_countersignatory_spk,
1492+ value : self . to_countersignatory_value_sat ,
1493+ } ;
14851494 let mut htlcs_with_aux = self . htlcs . iter ( ) . map ( |h| ( h. clone ( ) , ( ) ) ) . collect ( ) ;
1486- 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) ?;
1495+ let ( outputs, _) = Self :: internal_build_outputs ( keys, self . to_broadcaster_value_sat , to_countersignatory_txout , & mut htlcs_with_aux, channel_parameters, broadcaster_funding_key, countersignatory_funding_key) ?;
14871496
14881497 let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
14891498 let txid = transaction. compute_txid ( ) ;
@@ -1507,23 +1516,14 @@ impl CommitmentTransaction {
15071516 // - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
15081517 // caller needs to have sorted together with the HTLCs so it can keep track of the output index
15091518 // - building of a bitcoin transaction during a verify() call, in which case T is just ()
1510- 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 > ) , ( ) > {
1511- let countersignatory_pubkeys = channel_parameters. countersignatory_pubkeys ( ) ;
1519+ fn internal_build_outputs < T > ( keys : & TxCreationKeys , to_broadcaster_value_sat : Amount , to_countersignatory_txout : TxOut , htlcs_with_aux : & mut Vec < ( HTLCOutputInCommitment , T ) > , channel_parameters : & DirectedChannelTransactionParameters , broadcaster_funding_key : & PublicKey , countersignatory_funding_key : & PublicKey ) -> Result < ( Vec < TxOut > , Vec < HTLCOutputInCommitment > ) , ( ) > {
15121520 let contest_delay = channel_parameters. contest_delay ( ) ;
15131521
15141522 let mut txouts: Vec < ( TxOut , Option < & mut HTLCOutputInCommitment > ) > = Vec :: new ( ) ;
15151523
1516- if to_countersignatory_value_sat > Amount :: ZERO {
1517- let script = if channel_parameters. channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( ) {
1518- get_to_countersignatory_with_anchors_redeemscript ( & countersignatory_pubkeys. payment_point ) . to_p2wsh ( )
1519- } else {
1520- ScriptBuf :: new_p2wpkh ( & Hash160 :: hash ( & countersignatory_pubkeys. payment_point . serialize ( ) ) . into ( ) )
1521- } ;
1524+ if to_countersignatory_txout. value > Amount :: ZERO {
15221525 txouts. push ( (
1523- TxOut {
1524- script_pubkey : script. clone ( ) ,
1525- value : to_countersignatory_value_sat,
1526- } ,
1526+ to_countersignatory_txout. clone ( ) ,
15271527 None ,
15281528 ) )
15291529 }
@@ -1555,7 +1555,7 @@ impl CommitmentTransaction {
15551555 ) ) ;
15561556 }
15571557
1558- if to_countersignatory_value_sat > Amount :: ZERO || !htlcs_with_aux. is_empty ( ) {
1558+ if to_countersignatory_txout . value > Amount :: ZERO || !htlcs_with_aux. is_empty ( ) {
15591559 let anchor_script = get_anchor_redeemscript ( countersignatory_funding_key) ;
15601560 txouts. push ( (
15611561 TxOut {
@@ -1680,14 +1680,14 @@ impl CommitmentTransaction {
16801680 ///
16811681 /// An external validating signer must call this method before signing
16821682 /// or using the built transaction.
1683- pub fn verify < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , channel_parameters : & DirectedChannelTransactionParameters , broadcaster_keys : & ChannelPublicKeys , countersignatory_keys : & ChannelPublicKeys , secp_ctx : & Secp256k1 < T > ) -> Result < TrustedCommitmentTransaction , ( ) > {
1683+ pub fn verify < T : secp256k1:: Signing + secp256k1:: Verification > ( & self , channel_parameters : & DirectedChannelTransactionParameters , broadcaster_keys : & ChannelPublicKeys , countersignatory_keys : & ChannelPublicKeys , secp_ctx : & Secp256k1 < T > , to_countersignatory_spk : ScriptBuf ) -> Result < TrustedCommitmentTransaction , ( ) > {
16841684 // This is the only field of the key cache that we trust
16851685 let per_commitment_point = self . keys . per_commitment_point ;
16861686 let keys = TxCreationKeys :: from_channel_static_keys ( & per_commitment_point, broadcaster_keys, countersignatory_keys, secp_ctx) ;
16871687 if keys != self . keys {
16881688 return Err ( ( ) ) ;
16891689 }
1690- let tx = self . internal_rebuild_transaction ( & keys, channel_parameters, & broadcaster_keys. funding_pubkey , & countersignatory_keys. funding_pubkey ) ?;
1690+ let tx = self . internal_rebuild_transaction ( & keys, channel_parameters, & broadcaster_keys. funding_pubkey , & countersignatory_keys. funding_pubkey , to_countersignatory_spk ) ?;
16911691 if self . built . transaction != tx. transaction || self . built . txid != tx. txid {
16921692 return Err ( ( ) ) ;
16931693 }
@@ -1894,11 +1894,11 @@ pub fn get_commitment_transaction_number_obscure_factor(
18941894mod tests {
18951895 use super :: { CounterpartyCommitmentSecrets , ChannelPublicKeys } ;
18961896 use crate :: chain;
1897- use crate :: ln:: chan_utils:: { get_htlc_redeemscript, get_to_countersignatory_with_anchors_redeemscript, CommitmentTransaction , TxCreationKeys , ChannelTransactionParameters , CounterpartyChannelTransactionParameters , HTLCOutputInCommitment } ;
1897+ use crate :: ln:: chan_utils:: { get_counterparty_payment_script , get_htlc_redeemscript, get_to_countersignatory_with_anchors_redeemscript, CommitmentTransaction , TxCreationKeys , ChannelTransactionParameters , CounterpartyChannelTransactionParameters , HTLCOutputInCommitment } ;
18981898 use bitcoin:: secp256k1:: { PublicKey , SecretKey , Secp256k1 } ;
18991899 use crate :: util:: test_utils;
19001900 use crate :: sign:: { ChannelSigner , SignerProvider } ;
1901- use bitcoin:: { Network , Txid , ScriptBuf , CompressedPublicKey } ;
1901+ use bitcoin:: { Amount , TxOut , Network , Txid , ScriptBuf , CompressedPublicKey } ;
19021902 use bitcoin:: hashes:: Hash ;
19031903 use bitcoin:: hex:: FromHex ;
19041904 use crate :: types:: payment:: PaymentHash ;
@@ -1957,12 +1957,20 @@ mod tests {
19571957 }
19581958
19591959 fn build ( & mut self , to_broadcaster_sats : u64 , to_countersignatory_sats : u64 ) -> CommitmentTransaction {
1960+ let channel_parameters = self . channel_parameters . as_holder_broadcastable ( ) ;
1961+ let counterparty_payment_script = get_counterparty_payment_script ( & channel_parameters. channel_type_features ( ) , & channel_parameters. countersignatory_pubkeys ( ) . payment_point ) ;
1962+ let counterparty_txout = TxOut {
1963+ script_pubkey : counterparty_payment_script,
1964+ value : Amount :: from_sat ( to_countersignatory_sats) ,
1965+ } ;
19601966 CommitmentTransaction :: new_with_auxiliary_htlc_data (
1961- self . commitment_number , to_broadcaster_sats, to_countersignatory_sats,
1967+ self . commitment_number ,
1968+ to_broadcaster_sats,
1969+ counterparty_txout,
19621970 self . holder_funding_pubkey . clone ( ) ,
19631971 self . counterparty_funding_pubkey . clone ( ) ,
19641972 self . keys . clone ( ) , self . feerate_per_kw ,
1965- & mut self . htlcs_with_aux , & self . channel_parameters . as_holder_broadcastable ( )
1973+ & mut self . htlcs_with_aux , & channel_parameters,
19661974 )
19671975 }
19681976 }
0 commit comments