@@ -40,7 +40,7 @@ use crate::ln::script::{self, ShutdownScript};
40
40
use crate::ln::channel_state::{ChannelShutdownState, CounterpartyForwardingInfo, InboundHTLCDetails, InboundHTLCStateDetails, OutboundHTLCDetails, OutboundHTLCStateDetails};
41
41
use crate::ln::channelmanager::{self, OpenChannelMessage, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, PaymentClaimDetails, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
42
42
use crate::ln::chan_utils::{
43
- CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, htlc_success_tx_weight,
43
+ CounterpartyCommitmentSecrets, HTLCOutputInCommitment, htlc_success_tx_weight,
44
44
htlc_timeout_tx_weight, ChannelPublicKeys, CommitmentTransaction,
45
45
HolderCommitmentTransaction, ChannelTransactionParameters,
46
46
CounterpartyChannelTransactionParameters, max_htlcs,
@@ -2045,8 +2045,7 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
2045
2045
) -> Result<CommitmentTransaction, ChannelError> where L::Target: Logger {
2046
2046
let funding_script = self.funding().get_funding_redeemscript();
2047
2047
2048
- let keys = self.context().build_holder_transaction_keys(&self.funding(), holder_commitment_point.current_point());
2049
- let initial_commitment_tx = self.context().build_commitment_transaction(self.funding(), holder_commitment_point.transaction_number(), &keys, true, false, logger).tx;
2048
+ let initial_commitment_tx = self.context().build_commitment_transaction(self.funding(), holder_commitment_point.transaction_number(), &holder_commitment_point.current_point(), true, false, logger).tx;
2050
2049
let trusted_tx = initial_commitment_tx.trust();
2051
2050
let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
2052
2051
let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.funding().get_value_satoshis());
@@ -2083,8 +2082,7 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
2083
2082
}
2084
2083
};
2085
2084
let context = self.context();
2086
- let counterparty_keys = context.build_remote_transaction_keys(self.funding());
2087
- let counterparty_initial_commitment_tx = context.build_commitment_transaction(self.funding(), context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
2085
+ let counterparty_initial_commitment_tx = context.build_commitment_transaction(self.funding(), context.cur_counterparty_commitment_transaction_number, &context.counterparty_cur_commitment_point.unwrap(), false, false, logger).tx;
2088
2086
let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
2089
2087
let counterparty_initial_bitcoin_tx = counterparty_trusted_tx.built_transaction();
2090
2088
@@ -3481,9 +3479,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3481
3479
{
3482
3480
let funding_script = funding.get_funding_redeemscript();
3483
3481
3484
- let keys = self.build_holder_transaction_keys(funding, holder_commitment_point.current_point());
3485
-
3486
- let commitment_stats = self.build_commitment_transaction(funding, holder_commitment_point.transaction_number(), &keys, true, false, logger);
3482
+ let commitment_stats = self.build_commitment_transaction(funding, holder_commitment_point.transaction_number(), &holder_commitment_point.current_point(), true, false, logger);
3487
3483
let commitment_txid = {
3488
3484
let trusted_tx = commitment_stats.tx.trust();
3489
3485
let bitcoin_tx = trusted_tx.built_transaction();
@@ -3551,19 +3547,20 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3551
3547
3552
3548
let mut nondust_htlc_sources = Vec::with_capacity(htlcs_cloned.len());
3553
3549
let mut htlcs_and_sigs = Vec::with_capacity(htlcs_cloned.len());
3550
+ let holder_keys = commitment_stats.tx.trust().keys();
3554
3551
for (idx, (htlc, mut source_opt)) in htlcs_cloned.drain(..).enumerate() {
3555
3552
if let Some(_) = htlc.transaction_output_index {
3556
3553
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.feerate_per_kw,
3557
3554
funding.get_counterparty_selected_contest_delay().unwrap(), &htlc, &self.channel_type,
3558
- &keys .broadcaster_delayed_payment_key, &keys .revocation_key);
3555
+ &holder_keys .broadcaster_delayed_payment_key, &holder_keys .revocation_key);
3559
3556
3560
- let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &self.channel_type, &keys );
3557
+ let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &self.channel_type, &holder_keys );
3561
3558
let htlc_sighashtype = if self.channel_type.supports_anchors_zero_fee_htlc_tx() { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
3562
3559
let htlc_sighash = hash_to_message!(&sighash::SighashCache::new(&htlc_tx).p2wsh_signature_hash(0, &htlc_redeemscript, htlc.to_bitcoin_amount(), htlc_sighashtype).unwrap()[..]);
3563
3560
log_trace!(logger, "Checking HTLC tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} in channel {}.",
3564
- log_bytes!(msg.htlc_signatures[idx].serialize_compact()[..]), log_bytes!(keys .countersignatory_htlc_key.to_public_key().serialize()),
3561
+ log_bytes!(msg.htlc_signatures[idx].serialize_compact()[..]), log_bytes!(holder_keys .countersignatory_htlc_key.to_public_key().serialize()),
3565
3562
encode::serialize_hex(&htlc_tx), log_bytes!(htlc_sighash[..]), encode::serialize_hex(&htlc_redeemscript), &self.channel_id());
3566
- if let Err(_) = self.secp_ctx.verify_ecdsa(&htlc_sighash, &msg.htlc_signatures[idx], &keys .countersignatory_htlc_key.to_public_key()) {
3563
+ if let Err(_) = self.secp_ctx.verify_ecdsa(&htlc_sighash, &msg.htlc_signatures[idx], &holder_keys .countersignatory_htlc_key.to_public_key()) {
3567
3564
return Err(ChannelError::close("Invalid HTLC tx signature from peer".to_owned()));
3568
3565
}
3569
3566
if !separate_nondust_htlc_sources {
@@ -3612,7 +3609,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3612
3609
/// generated by the peer which proposed adding the HTLCs, and thus we need to understand both
3613
3610
/// which peer generated this transaction and "to whom" this transaction flows.
3614
3611
#[inline]
3615
- fn build_commitment_transaction<L: Deref>(&self, funding: &FundingScope, commitment_number: u64, keys : &TxCreationKeys , local: bool, generated_by_local: bool, logger: &L) -> CommitmentStats
3612
+ fn build_commitment_transaction<L: Deref>(&self, funding: &FundingScope, commitment_number: u64, per_commitment_point : &PublicKey , local: bool, generated_by_local: bool, logger: &L) -> CommitmentStats
3616
3613
where L::Target: Logger
3617
3614
{
3618
3615
let mut included_dust_htlcs: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::new();
@@ -3814,7 +3811,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3814
3811
if local { funding.channel_transaction_parameters.as_holder_broadcastable() }
3815
3812
else { funding.channel_transaction_parameters.as_counterparty_broadcastable() };
3816
3813
let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(commitment_number,
3817
- &keys. per_commitment_point,
3814
+ per_commitment_point,
3818
3815
value_to_a as u64,
3819
3816
value_to_b as u64,
3820
3817
feerate_per_kw,
@@ -3840,32 +3837,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3840
3837
}
3841
3838
}
3842
3839
3843
- #[inline]
3844
- /// Creates a set of keys for build_commitment_transaction to generate a transaction which our
3845
- /// counterparty will sign (ie DO NOT send signatures over a transaction created by this to
3846
- /// our counterparty!)
3847
- /// The result is a transaction which we can revoke broadcastership of (ie a "local" transaction)
3848
- /// TODO Some magic rust shit to compile-time check this?
3849
- fn build_holder_transaction_keys(&self, funding: &FundingScope, per_commitment_point: PublicKey) -> TxCreationKeys {
3850
- let delayed_payment_base = &funding.get_holder_pubkeys().delayed_payment_basepoint;
3851
- let htlc_basepoint = &funding.get_holder_pubkeys().htlc_basepoint;
3852
- let counterparty_pubkeys = funding.get_counterparty_pubkeys();
3853
-
3854
- TxCreationKeys::derive_new(&self.secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
3855
- }
3856
-
3857
- #[inline]
3858
- /// Creates a set of keys for build_commitment_transaction to generate a transaction which we
3859
- /// will sign and send to our counterparty.
3860
- /// If an Err is returned, it is a ChannelError::Close (for get_funding_created)
3861
- fn build_remote_transaction_keys(&self, funding: &FundingScope) -> TxCreationKeys {
3862
- let revocation_basepoint = &funding.get_holder_pubkeys().revocation_basepoint;
3863
- let htlc_basepoint = &funding.get_holder_pubkeys().htlc_basepoint;
3864
- let counterparty_pubkeys = funding.get_counterparty_pubkeys();
3865
-
3866
- TxCreationKeys::derive_new(&self.secp_ctx, &self.counterparty_cur_commitment_point.unwrap(), &counterparty_pubkeys.delayed_payment_basepoint, &counterparty_pubkeys.htlc_basepoint, revocation_basepoint, htlc_basepoint)
3867
- }
3868
-
3869
3840
pub fn get_feerate_sat_per_1000_weight(&self) -> u32 {
3870
3841
self.feerate_per_kw
3871
3842
}
@@ -4648,9 +4619,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
4648
4619
SP::Target: SignerProvider,
4649
4620
L::Target: Logger
4650
4621
{
4651
- let counterparty_keys = self.build_remote_transaction_keys(funding);
4652
4622
let counterparty_initial_commitment_tx = self.build_commitment_transaction(
4653
- funding, self.cur_counterparty_commitment_transaction_number, &counterparty_keys , false, false, logger).tx;
4623
+ funding, self.cur_counterparty_commitment_transaction_number, &self.counterparty_cur_commitment_point.unwrap() , false, false, logger).tx;
4654
4624
match self.holder_signer {
4655
4625
// TODO (taproot|arik): move match into calling method for Taproot
4656
4626
ChannelSignerType::Ecdsa(ref ecdsa) => {
@@ -6363,8 +6333,7 @@ impl<SP: Deref> FundedChannel<SP> where
6363
6333
// Before proposing a feerate update, check that we can actually afford the new fee.
6364
6334
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
6365
6335
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
6366
- let keys = self.context.build_holder_transaction_keys(&self.funding, self.holder_commitment_point.current_point());
6367
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.holder_commitment_point.transaction_number(), &keys, true, true, logger);
6336
+ let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.holder_commitment_point.transaction_number(), &self.holder_commitment_point.current_point(), true, true, logger);
6368
6337
let buffer_fee_msat = commit_tx_fee_sat(feerate_per_kw, commitment_stats.num_nondust_htlcs + htlc_stats.on_holder_tx_outbound_holding_cell_htlcs_count as usize + CONCURRENT_INBOUND_HTLC_FEE_BUFFER as usize, self.context.get_channel_type()) * 1000;
6369
6338
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
6370
6339
if holder_balance_msat < buffer_fee_msat + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
@@ -6677,8 +6646,7 @@ impl<SP: Deref> FundedChannel<SP> where
6677
6646
self.holder_commitment_point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
6678
6647
}
6679
6648
let funding_signed = if self.context.signer_pending_funding && !self.funding.is_outbound() {
6680
- let counterparty_keys = self.context.build_remote_transaction_keys(&self.funding);
6681
- let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number + 1, &counterparty_keys, false, false, logger).tx;
6649
+ let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number + 1, &self.context.counterparty_cur_commitment_point.unwrap(), false, false, logger).tx;
6682
6650
self.context.get_funding_signed_msg(&self.funding.channel_transaction_parameters, logger, counterparty_initial_commitment_tx)
6683
6651
} else { None };
6684
6652
// Provide a `channel_ready` message if we need to, but only if we're _not_ still pending
@@ -8751,8 +8719,7 @@ impl<SP: Deref> FundedChannel<SP> where
8751
8719
-> (Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>, CommitmentTransaction)
8752
8720
where L::Target: Logger
8753
8721
{
8754
- let counterparty_keys = self.context.build_remote_transaction_keys(&self.funding);
8755
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
8722
+ let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number, &self.context.counterparty_cur_commitment_point.unwrap(), false, true, logger);
8756
8723
let counterparty_commitment_tx = commitment_stats.tx;
8757
8724
8758
8725
#[cfg(any(test, fuzzing))]
@@ -8783,8 +8750,7 @@ impl<SP: Deref> FundedChannel<SP> where
8783
8750
#[cfg(any(test, fuzzing))]
8784
8751
self.build_commitment_no_state_update(logger);
8785
8752
8786
- let counterparty_keys = self.context.build_remote_transaction_keys(&self.funding);
8787
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
8753
+ let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number, &self.context.counterparty_cur_commitment_point.unwrap(), false, true, logger);
8788
8754
let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
8789
8755
8790
8756
match &self.context.holder_signer {
@@ -8812,6 +8778,7 @@ impl<SP: Deref> FundedChannel<SP> where
8812
8778
&counterparty_commitment_txid, encode::serialize_hex(&self.funding.get_funding_redeemscript()),
8813
8779
log_bytes!(signature.serialize_compact()[..]), &self.context.channel_id());
8814
8780
8781
+ let counterparty_keys = commitment_stats.tx.trust().keys();
8815
8782
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
8816
8783
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
8817
8784
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.feerate_per_kw, self.funding.get_holder_selected_contest_delay(), htlc, &self.context.channel_type, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
@@ -9268,8 +9235,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
9268
9235
9269
9236
/// Only allowed after [`FundingScope::channel_transaction_parameters`] is set.
9270
9237
fn get_funding_created_msg<L: Deref>(&mut self, logger: &L) -> Option<msgs::FundingCreated> where L::Target: Logger {
9271
- let counterparty_keys = self.context.build_remote_transaction_keys(&self.funding);
9272
- let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
9238
+ let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number, &self.context.counterparty_cur_commitment_point.unwrap(), false, false, logger).tx;
9273
9239
let signature = match &self.context.holder_signer {
9274
9240
// TODO (taproot|arik): move match into calling method for Taproot
9275
9241
ChannelSignerType::Ecdsa(ecdsa) => {
@@ -11833,7 +11799,7 @@ mod tests {
11833
11799
use bitcoin::secp256k1::Message;
11834
11800
use crate::sign::{ChannelDerivationParameters, HTLCDescriptor, ecdsa::EcdsaChannelSigner};
11835
11801
use crate::types::payment::PaymentPreimage;
11836
- use crate::ln::channel::{ HTLCOutputInCommitment ,TxCreationKeys} ;
11802
+ use crate::ln::channel::HTLCOutputInCommitment;
11837
11803
use crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint};
11838
11804
use crate::ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
11839
11805
use crate::util::logger::Logger;
@@ -11901,11 +11867,6 @@ mod tests {
11901
11867
// build_commitment_transaction.
11902
11868
let per_commitment_secret = SecretKey::from_slice(&<Vec<u8>>::from_hex("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100").unwrap()[..]).unwrap();
11903
11869
let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
11904
- let directed_params = chan.funding.channel_transaction_parameters.as_holder_broadcastable();
11905
- let keys = TxCreationKeys::from_channel_static_keys(
11906
- &per_commitment_point, directed_params.broadcaster_pubkeys(),
11907
- directed_params.countersignatory_pubkeys(), &secp_ctx,
11908
- );
11909
11870
11910
11871
macro_rules! test_commitment {
11911
11872
( $counterparty_sig_hex: expr, $sig_hex: expr, $tx_hex: expr, $($remain:tt)* ) => {
@@ -11926,7 +11887,7 @@ mod tests {
11926
11887
$( { $htlc_idx: expr, $counterparty_htlc_sig_hex: expr, $htlc_sig_hex: expr, $htlc_tx_hex: expr } ), *
11927
11888
} ) => { {
11928
11889
let (commitment_tx, htlcs): (_, Vec<HTLCOutputInCommitment>) = {
11929
- let mut commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &keys , true, false, &logger);
11890
+ let mut commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &per_commitment_point , true, false, &logger);
11930
11891
11931
11892
let htlcs = commitment_stats.htlcs_included.drain(..)
11932
11893
.filter_map(|(htlc, _)| if htlc.transaction_output_index.is_some() { Some(htlc) } else { None })
@@ -11974,6 +11935,7 @@ mod tests {
11974
11935
let remote_signature = Signature::from_der(&<Vec<u8>>::from_hex($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
11975
11936
11976
11937
let ref htlc = htlcs[$htlc_idx];
11938
+ let keys = commitment_tx.trust().keys();
11977
11939
let mut htlc_tx = chan_utils::build_htlc_transaction(&unsigned_tx.txid, chan.context.feerate_per_kw,
11978
11940
chan.funding.get_counterparty_selected_contest_delay().unwrap(),
11979
11941
&htlc, $opt_anchors, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
0 commit comments