@@ -20,7 +20,6 @@ use bitcoin::sighash::EcdsaSighashType;
20
20
use bitcoin:: transaction:: Version ;
21
21
22
22
use bitcoin:: hashes:: { Hash , HashEngine } ;
23
- use bitcoin:: hashes:: hash160:: Hash as Hash160 ;
24
23
use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
25
24
use bitcoin:: hashes:: ripemd160:: Hash as Ripemd160 ;
26
25
use bitcoin:: hash_types:: Txid ;
@@ -1135,7 +1134,13 @@ impl HolderCommitmentTransaction {
1135
1134
for _ in 0 ..htlcs. len ( ) {
1136
1135
counterparty_htlc_sigs. push ( dummy_sig) ;
1137
1136
}
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) ;
1139
1144
htlcs. sort_by_key ( |htlc| htlc. 0 . transaction_output_index ) ;
1140
1145
HolderCommitmentTransaction {
1141
1146
inner,
@@ -1445,12 +1450,12 @@ impl CommitmentTransaction {
1445
1450
/// Only include HTLCs that are above the dust limit for the channel.
1446
1451
///
1447
1452
/// 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 {
1449
1454
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 ;
1451
1456
1452
1457
// 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 ( ) ;
1454
1459
1455
1460
let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( commitment_number, channel_parameters) ;
1456
1461
let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
@@ -1479,11 +1484,15 @@ impl CommitmentTransaction {
1479
1484
self
1480
1485
}
1481
1486
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 , ( ) > {
1483
1488
let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( self . commitment_number , channel_parameters) ;
1484
1489
1490
+ let to_countersignatory_txout = TxOut {
1491
+ script_pubkey : to_countersignatory_spk,
1492
+ value : self . to_countersignatory_value_sat ,
1493
+ } ;
1485
1494
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) ?;
1487
1496
1488
1497
let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
1489
1498
let txid = transaction. compute_txid ( ) ;
@@ -1507,23 +1516,14 @@ impl CommitmentTransaction {
1507
1516
// - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
1508
1517
// caller needs to have sorted together with the HTLCs so it can keep track of the output index
1509
1518
// - 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 > ) , ( ) > {
1512
1520
let contest_delay = channel_parameters. contest_delay ( ) ;
1513
1521
1514
1522
let mut txouts: Vec < ( TxOut , Option < & mut HTLCOutputInCommitment > ) > = Vec :: new ( ) ;
1515
1523
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 {
1522
1525
txouts. push ( (
1523
- TxOut {
1524
- script_pubkey : script. clone ( ) ,
1525
- value : to_countersignatory_value_sat,
1526
- } ,
1526
+ to_countersignatory_txout. clone ( ) ,
1527
1527
None ,
1528
1528
) )
1529
1529
}
@@ -1555,7 +1555,7 @@ impl CommitmentTransaction {
1555
1555
) ) ;
1556
1556
}
1557
1557
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 ( ) {
1559
1559
let anchor_script = get_anchor_redeemscript ( countersignatory_funding_key) ;
1560
1560
txouts. push ( (
1561
1561
TxOut {
@@ -1680,14 +1680,14 @@ impl CommitmentTransaction {
1680
1680
///
1681
1681
/// An external validating signer must call this method before signing
1682
1682
/// 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 , ( ) > {
1684
1684
// This is the only field of the key cache that we trust
1685
1685
let per_commitment_point = self . keys . per_commitment_point ;
1686
1686
let keys = TxCreationKeys :: from_channel_static_keys ( & per_commitment_point, broadcaster_keys, countersignatory_keys, secp_ctx) ;
1687
1687
if keys != self . keys {
1688
1688
return Err ( ( ) ) ;
1689
1689
}
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 ) ?;
1691
1691
if self . built . transaction != tx. transaction || self . built . txid != tx. txid {
1692
1692
return Err ( ( ) ) ;
1693
1693
}
@@ -1894,11 +1894,11 @@ pub fn get_commitment_transaction_number_obscure_factor(
1894
1894
mod tests {
1895
1895
use super :: { CounterpartyCommitmentSecrets , ChannelPublicKeys } ;
1896
1896
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 } ;
1898
1898
use bitcoin:: secp256k1:: { PublicKey , SecretKey , Secp256k1 } ;
1899
1899
use crate :: util:: test_utils;
1900
1900
use crate :: sign:: { ChannelSigner , SignerProvider } ;
1901
- use bitcoin:: { Network , Txid , ScriptBuf , CompressedPublicKey } ;
1901
+ use bitcoin:: { Amount , TxOut , Network , Txid , ScriptBuf , CompressedPublicKey } ;
1902
1902
use bitcoin:: hashes:: Hash ;
1903
1903
use bitcoin:: hex:: FromHex ;
1904
1904
use crate :: types:: payment:: PaymentHash ;
@@ -1957,12 +1957,20 @@ mod tests {
1957
1957
}
1958
1958
1959
1959
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
+ } ;
1960
1966
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,
1962
1970
self . holder_funding_pubkey . clone ( ) ,
1963
1971
self . counterparty_funding_pubkey . clone ( ) ,
1964
1972
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,
1966
1974
)
1967
1975
}
1968
1976
}
0 commit comments