@@ -41,7 +41,7 @@ use crate::ln::script::{self, ShutdownScript};
41
41
use crate::ln::channel_state::{ChannelShutdownState, CounterpartyForwardingInfo, InboundHTLCDetails, InboundHTLCStateDetails, OutboundHTLCDetails, OutboundHTLCStateDetails};
42
42
use crate::ln::channelmanager::{self, OpenChannelMessage, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, PaymentClaimDetails, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
43
43
use crate::ln::chan_utils::{
44
- CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, htlc_success_tx_weight,
44
+ CounterpartyCommitmentSecrets, HTLCOutputInCommitment, htlc_success_tx_weight,
45
45
htlc_timeout_tx_weight, ChannelPublicKeys, CommitmentTransaction,
46
46
HolderCommitmentTransaction, ChannelTransactionParameters,
47
47
CounterpartyChannelTransactionParameters, MAX_HTLCS,
@@ -2036,8 +2036,7 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
2036
2036
) -> Result<CommitmentTransaction, ChannelError> where L::Target: Logger {
2037
2037
let funding_script = self.funding().get_funding_redeemscript();
2038
2038
2039
- let keys = self.context().build_holder_transaction_keys(&self.funding(), holder_commitment_point.current_point());
2040
- let initial_commitment_tx = self.context().build_commitment_transaction(self.funding(), holder_commitment_point.transaction_number(), &keys, true, false, logger).tx;
2039
+ 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;
2041
2040
let trusted_tx = initial_commitment_tx.trust();
2042
2041
let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
2043
2042
let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.funding().get_value_satoshis());
@@ -2074,8 +2073,7 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
2074
2073
}
2075
2074
};
2076
2075
let context = self.context();
2077
- let counterparty_keys = context.build_remote_transaction_keys(self.funding());
2078
- let counterparty_initial_commitment_tx = context.build_commitment_transaction(self.funding(), context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
2076
+ 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;
2079
2077
let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
2080
2078
let counterparty_initial_bitcoin_tx = counterparty_trusted_tx.built_transaction();
2081
2079
@@ -3460,7 +3458,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3460
3458
/// generated by the peer which proposed adding the HTLCs, and thus we need to understand both
3461
3459
/// which peer generated this transaction and "to whom" this transaction flows.
3462
3460
#[inline]
3463
- fn build_commitment_transaction<L: Deref>(&self, funding: &FundingScope, commitment_number: u64, keys : &TxCreationKeys , local: bool, generated_by_local: bool, logger: &L) -> CommitmentStats
3461
+ 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
3464
3462
where L::Target: Logger
3465
3463
{
3466
3464
let mut included_dust_htlcs: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::new();
@@ -3662,7 +3660,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3662
3660
if local { funding.channel_transaction_parameters.as_holder_broadcastable() }
3663
3661
else { funding.channel_transaction_parameters.as_counterparty_broadcastable() };
3664
3662
let tx = CommitmentTransaction::new(commitment_number,
3665
- &keys. per_commitment_point,
3663
+ &per_commitment_point,
3666
3664
value_to_a as u64,
3667
3665
value_to_b as u64,
3668
3666
feerate_per_kw,
@@ -3688,32 +3686,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3688
3686
}
3689
3687
}
3690
3688
3691
- #[inline]
3692
- /// Creates a set of keys for build_commitment_transaction to generate a transaction which our
3693
- /// counterparty will sign (ie DO NOT send signatures over a transaction created by this to
3694
- /// our counterparty!)
3695
- /// The result is a transaction which we can revoke broadcastership of (ie a "local" transaction)
3696
- /// TODO Some magic rust shit to compile-time check this?
3697
- fn build_holder_transaction_keys(&self, funding: &FundingScope, per_commitment_point: PublicKey) -> TxCreationKeys {
3698
- let delayed_payment_base = &funding.get_holder_pubkeys().delayed_payment_basepoint;
3699
- let htlc_basepoint = &funding.get_holder_pubkeys().htlc_basepoint;
3700
- let counterparty_pubkeys = funding.get_counterparty_pubkeys();
3701
-
3702
- TxCreationKeys::derive_new(&self.secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
3703
- }
3704
-
3705
- #[inline]
3706
- /// Creates a set of keys for build_commitment_transaction to generate a transaction which we
3707
- /// will sign and send to our counterparty.
3708
- /// If an Err is returned, it is a ChannelError::Close (for get_funding_created)
3709
- fn build_remote_transaction_keys(&self, funding: &FundingScope) -> TxCreationKeys {
3710
- let revocation_basepoint = &funding.get_holder_pubkeys().revocation_basepoint;
3711
- let htlc_basepoint = &funding.get_holder_pubkeys().htlc_basepoint;
3712
- let counterparty_pubkeys = funding.get_counterparty_pubkeys();
3713
-
3714
- 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)
3715
- }
3716
-
3717
3689
pub fn get_feerate_sat_per_1000_weight(&self) -> u32 {
3718
3690
self.feerate_per_kw
3719
3691
}
@@ -4497,9 +4469,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
4497
4469
SP::Target: SignerProvider,
4498
4470
L::Target: Logger
4499
4471
{
4500
- let counterparty_keys = self.build_remote_transaction_keys(funding);
4501
4472
let counterparty_initial_commitment_tx = self.build_commitment_transaction(
4502
- funding, self.cur_counterparty_commitment_transaction_number, &counterparty_keys , false, false, logger).tx;
4473
+ funding, self.cur_counterparty_commitment_transaction_number, &self.counterparty_cur_commitment_point.unwrap() , false, false, logger).tx;
4503
4474
match self.holder_signer {
4504
4475
// TODO (taproot|arik): move match into calling method for Taproot
4505
4476
ChannelSignerType::Ecdsa(ref ecdsa) => {
@@ -5499,9 +5470,7 @@ impl<SP: Deref> FundedChannel<SP> where
5499
5470
5500
5471
let funding_script = self.funding.get_funding_redeemscript();
5501
5472
5502
- let keys = self.context.build_holder_transaction_keys(&self.funding, self.holder_commitment_point.current_point());
5503
-
5504
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.holder_commitment_point.transaction_number(), &keys, true, false, logger);
5473
+ let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.holder_commitment_point.transaction_number(), &self.holder_commitment_point.current_point(), true, false, logger);
5505
5474
let commitment_txid = {
5506
5475
let trusted_tx = commitment_stats.tx.trust();
5507
5476
let bitcoin_tx = trusted_tx.built_transaction();
@@ -5569,19 +5538,20 @@ impl<SP: Deref> FundedChannel<SP> where
5569
5538
5570
5539
let mut nondust_htlc_sources = Vec::with_capacity(htlcs_cloned.len());
5571
5540
let mut htlcs_and_sigs = Vec::with_capacity(htlcs_cloned.len());
5541
+ let holder_keys = commitment_stats.tx.trust().keys();
5572
5542
for (idx, (htlc, mut source_opt)) in htlcs_cloned.drain(..).enumerate() {
5573
5543
if let Some(_) = htlc.transaction_output_index {
5574
5544
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.feerate_per_kw,
5575
5545
self.funding.get_counterparty_selected_contest_delay().unwrap(), &htlc, &self.context.channel_type,
5576
- &keys .broadcaster_delayed_payment_key, &keys .revocation_key);
5546
+ &holder_keys .broadcaster_delayed_payment_key, &holder_keys .revocation_key);
5577
5547
5578
- let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &keys );
5548
+ let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &holder_keys );
5579
5549
let htlc_sighashtype = if self.context.channel_type.supports_anchors_zero_fee_htlc_tx() { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
5580
5550
let htlc_sighash = hash_to_message!(&sighash::SighashCache::new(&htlc_tx).p2wsh_signature_hash(0, &htlc_redeemscript, htlc.to_bitcoin_amount(), htlc_sighashtype).unwrap()[..]);
5581
5551
log_trace!(logger, "Checking HTLC tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} in channel {}.",
5582
- log_bytes!(msg.htlc_signatures[idx].serialize_compact()[..]), log_bytes!(keys .countersignatory_htlc_key.to_public_key().serialize()),
5552
+ log_bytes!(msg.htlc_signatures[idx].serialize_compact()[..]), log_bytes!(holder_keys .countersignatory_htlc_key.to_public_key().serialize()),
5583
5553
encode::serialize_hex(&htlc_tx), log_bytes!(htlc_sighash[..]), encode::serialize_hex(&htlc_redeemscript), &self.context.channel_id());
5584
- if let Err(_) = self.context.secp_ctx.verify_ecdsa(&htlc_sighash, &msg.htlc_signatures[idx], &keys .countersignatory_htlc_key.to_public_key()) {
5554
+ if let Err(_) = self.context.secp_ctx.verify_ecdsa(&htlc_sighash, &msg.htlc_signatures[idx], &holder_keys .countersignatory_htlc_key.to_public_key()) {
5585
5555
return Err(ChannelError::close("Invalid HTLC tx signature from peer".to_owned()));
5586
5556
}
5587
5557
if !separate_nondust_htlc_sources {
@@ -6263,8 +6233,7 @@ impl<SP: Deref> FundedChannel<SP> where
6263
6233
// Before proposing a feerate update, check that we can actually afford the new fee.
6264
6234
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
6265
6235
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
6266
- let keys = self.context.build_holder_transaction_keys(&self.funding, self.holder_commitment_point.current_point());
6267
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.holder_commitment_point.transaction_number(), &keys, true, true, logger);
6236
+ 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);
6268
6237
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;
6269
6238
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
6270
6239
if holder_balance_msat < buffer_fee_msat + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
@@ -6577,8 +6546,7 @@ impl<SP: Deref> FundedChannel<SP> where
6577
6546
self.holder_commitment_point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
6578
6547
}
6579
6548
let funding_signed = if self.context.signer_pending_funding && !self.funding.is_outbound() {
6580
- let counterparty_keys = self.context.build_remote_transaction_keys(&self.funding);
6581
- 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;
6549
+ 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;
6582
6550
self.context.get_funding_signed_msg(&self.funding.channel_transaction_parameters, logger, counterparty_initial_commitment_tx)
6583
6551
} else { None };
6584
6552
// Provide a `channel_ready` message if we need to, but only if we're _not_ still pending
@@ -8514,8 +8482,7 @@ impl<SP: Deref> FundedChannel<SP> where
8514
8482
-> (Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>, CommitmentTransaction)
8515
8483
where L::Target: Logger
8516
8484
{
8517
- let counterparty_keys = self.context.build_remote_transaction_keys(&self.funding);
8518
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
8485
+ 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);
8519
8486
let counterparty_commitment_tx = commitment_stats.tx;
8520
8487
8521
8488
#[cfg(any(test, fuzzing))]
@@ -8546,8 +8513,7 @@ impl<SP: Deref> FundedChannel<SP> where
8546
8513
#[cfg(any(test, fuzzing))]
8547
8514
self.build_commitment_no_state_update(logger);
8548
8515
8549
- let counterparty_keys = self.context.build_remote_transaction_keys(&self.funding);
8550
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
8516
+ 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);
8551
8517
let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
8552
8518
8553
8519
match &self.context.holder_signer {
@@ -8575,6 +8541,7 @@ impl<SP: Deref> FundedChannel<SP> where
8575
8541
&counterparty_commitment_txid, encode::serialize_hex(&self.funding.get_funding_redeemscript()),
8576
8542
log_bytes!(signature.serialize_compact()[..]), &self.context.channel_id());
8577
8543
8544
+ let counterparty_keys = commitment_stats.tx.trust().keys();
8578
8545
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
8579
8546
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
8580
8547
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)),
@@ -9030,8 +8997,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
9030
8997
9031
8998
/// Only allowed after [`FundingScope::channel_transaction_parameters`] is set.
9032
8999
fn get_funding_created_msg<L: Deref>(&mut self, logger: &L) -> Option<msgs::FundingCreated> where L::Target: Logger {
9033
- let counterparty_keys = self.context.build_remote_transaction_keys(&self.funding);
9034
- 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;
9000
+ 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;
9035
9001
let signature = match &self.context.holder_signer {
9036
9002
// TODO (taproot|arik): move match into calling method for Taproot
9037
9003
ChannelSignerType::Ecdsa(ecdsa) => {
@@ -11557,7 +11523,7 @@ mod tests {
11557
11523
use bitcoin::secp256k1::Message;
11558
11524
use crate::sign::{ChannelDerivationParameters, HTLCDescriptor, ecdsa::EcdsaChannelSigner};
11559
11525
use crate::types::payment::PaymentPreimage;
11560
- use crate::ln::channel::{ HTLCOutputInCommitment ,TxCreationKeys} ;
11526
+ use crate::ln::channel::HTLCOutputInCommitment;
11561
11527
use crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint};
11562
11528
use crate::ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
11563
11529
use crate::util::logger::Logger;
@@ -11622,11 +11588,8 @@ mod tests {
11622
11588
// We can't just use build_holder_transaction_keys here as the per_commitment_secret is not
11623
11589
// derived from a commitment_seed, so instead we copy it here and call
11624
11590
// build_commitment_transaction.
11625
- let delayed_payment_base = &chan.context.holder_signer.as_ref().pubkeys().delayed_payment_basepoint;
11626
11591
let per_commitment_secret = SecretKey::from_slice(&<Vec<u8>>::from_hex("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100").unwrap()[..]).unwrap();
11627
11592
let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
11628
- let htlc_basepoint = &chan.context.holder_signer.as_ref().pubkeys().htlc_basepoint;
11629
- let keys = TxCreationKeys::derive_new(&secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint);
11630
11593
11631
11594
macro_rules! test_commitment {
11632
11595
( $counterparty_sig_hex: expr, $sig_hex: expr, $tx_hex: expr, $($remain:tt)* ) => {
@@ -11647,7 +11610,7 @@ mod tests {
11647
11610
$( { $htlc_idx: expr, $counterparty_htlc_sig_hex: expr, $htlc_sig_hex: expr, $htlc_tx_hex: expr } ), *
11648
11611
} ) => { {
11649
11612
let (commitment_tx, htlcs): (_, Vec<HTLCOutputInCommitment>) = {
11650
- let mut commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &keys , true, false, &logger);
11613
+ let mut commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &per_commitment_point , true, false, &logger);
11651
11614
11652
11615
let htlcs = commitment_stats.htlcs_included.drain(..)
11653
11616
.filter_map(|(htlc, _)| if htlc.transaction_output_index.is_some() { Some(htlc) } else { None })
@@ -11695,6 +11658,7 @@ mod tests {
11695
11658
let remote_signature = Signature::from_der(&<Vec<u8>>::from_hex($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
11696
11659
11697
11660
let ref htlc = htlcs[$htlc_idx];
11661
+ let keys = commitment_tx.trust().keys();
11698
11662
let mut htlc_tx = chan_utils::build_htlc_transaction(&unsigned_tx.txid, chan.context.feerate_per_kw,
11699
11663
chan.funding.get_counterparty_selected_contest_delay().unwrap(),
11700
11664
&htlc, $opt_anchors, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
0 commit comments