@@ -1968,8 +1968,7 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
1968
1968
) -> Result<CommitmentTransaction, ChannelError> where L::Target: Logger {
1969
1969
let funding_script = self.context().get_funding_redeemscript();
1970
1970
1971
- let keys = self.context().build_holder_transaction_keys(holder_commitment_point.current_point());
1972
- let initial_commitment_tx = self.context().build_commitment_transaction(self.funding(), holder_commitment_point.transaction_number(), &keys, true, false, logger).tx;
1971
+ 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;
1973
1972
let trusted_tx = initial_commitment_tx.trust();
1974
1973
let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
1975
1974
let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.funding().channel_value_satoshis);
@@ -2006,8 +2005,7 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
2006
2005
}
2007
2006
};
2008
2007
let context = self.context();
2009
- let counterparty_keys = context.build_remote_transaction_keys();
2010
- let counterparty_initial_commitment_tx = context.build_commitment_transaction(self.funding(), context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
2008
+ 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;
2011
2009
let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
2012
2010
let counterparty_initial_bitcoin_tx = counterparty_trusted_tx.built_transaction();
2013
2011
@@ -3412,7 +3410,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3412
3410
/// generated by the peer which proposed adding the HTLCs, and thus we need to understand both
3413
3411
/// which peer generated this transaction and "to whom" this transaction flows.
3414
3412
#[inline]
3415
- fn build_commitment_transaction<L: Deref>(&self, funding: &FundingScope, commitment_number: u64, keys : &TxCreationKeys , local: bool, generated_by_local: bool, logger: &L) -> CommitmentStats
3413
+ 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
3416
3414
where L::Target: Logger
3417
3415
{
3418
3416
let mut included_dust_htlcs: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::new();
@@ -3614,7 +3612,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3614
3612
if local { self.channel_transaction_parameters.as_holder_broadcastable() }
3615
3613
else { self.channel_transaction_parameters.as_counterparty_broadcastable() };
3616
3614
let tx = CommitmentTransaction::new(commitment_number,
3617
- &keys. per_commitment_point,
3615
+ &per_commitment_point,
3618
3616
value_to_a as u64,
3619
3617
value_to_b as u64,
3620
3618
feerate_per_kw,
@@ -3640,32 +3638,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3640
3638
}
3641
3639
}
3642
3640
3643
- #[inline]
3644
- /// Creates a set of keys for build_commitment_transaction to generate a transaction which our
3645
- /// counterparty will sign (ie DO NOT send signatures over a transaction created by this to
3646
- /// our counterparty!)
3647
- /// The result is a transaction which we can revoke broadcastership of (ie a "local" transaction)
3648
- /// TODO Some magic rust shit to compile-time check this?
3649
- fn build_holder_transaction_keys(&self, per_commitment_point: PublicKey) -> TxCreationKeys {
3650
- let delayed_payment_base = &self.get_holder_pubkeys().delayed_payment_basepoint;
3651
- let htlc_basepoint = &self.get_holder_pubkeys().htlc_basepoint;
3652
- let counterparty_pubkeys = self.get_counterparty_pubkeys();
3653
-
3654
- TxCreationKeys::derive_new(&self.secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
3655
- }
3656
-
3657
- #[inline]
3658
- /// Creates a set of keys for build_commitment_transaction to generate a transaction which we
3659
- /// will sign and send to our counterparty.
3660
- /// If an Err is returned, it is a ChannelError::Close (for get_funding_created)
3661
- fn build_remote_transaction_keys(&self) -> TxCreationKeys {
3662
- let revocation_basepoint = &self.get_holder_pubkeys().revocation_basepoint;
3663
- let htlc_basepoint = &self.get_holder_pubkeys().htlc_basepoint;
3664
- let counterparty_pubkeys = self.get_counterparty_pubkeys();
3665
-
3666
- 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)
3667
- }
3668
-
3669
3641
/// Gets the redeemscript for the funding transaction output (ie the funding transaction output
3670
3642
/// pays to get_funding_redeemscript().to_p2wsh()).
3671
3643
/// Panics if called before accept_channel/InboundV1Channel::new
@@ -4459,9 +4431,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
4459
4431
SP::Target: SignerProvider,
4460
4432
L::Target: Logger
4461
4433
{
4462
- let counterparty_keys = self.build_remote_transaction_keys();
4463
4434
let counterparty_initial_commitment_tx = self.build_commitment_transaction(
4464
- funding, self.cur_counterparty_commitment_transaction_number, &counterparty_keys , false, false, logger).tx;
4435
+ funding, self.cur_counterparty_commitment_transaction_number, &self.counterparty_cur_commitment_point.unwrap() , false, false, logger).tx;
4465
4436
match self.holder_signer {
4466
4437
// TODO (taproot|arik): move match into calling method for Taproot
4467
4438
ChannelSignerType::Ecdsa(ref ecdsa) => {
@@ -5454,9 +5425,7 @@ impl<SP: Deref> FundedChannel<SP> where
5454
5425
5455
5426
let funding_script = self.context.get_funding_redeemscript();
5456
5427
5457
- let keys = self.context.build_holder_transaction_keys(self.holder_commitment_point.current_point());
5458
-
5459
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.holder_commitment_point.transaction_number(), &keys, true, false, logger);
5428
+ 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);
5460
5429
let commitment_txid = {
5461
5430
let trusted_tx = commitment_stats.tx.trust();
5462
5431
let bitcoin_tx = trusted_tx.built_transaction();
@@ -5524,19 +5493,20 @@ impl<SP: Deref> FundedChannel<SP> where
5524
5493
5525
5494
let mut nondust_htlc_sources = Vec::with_capacity(htlcs_cloned.len());
5526
5495
let mut htlcs_and_sigs = Vec::with_capacity(htlcs_cloned.len());
5496
+ let holder_keys = TxCreationKeys::from_channel_static_keys(&self.holder_commitment_point.current_point(), self.context.get_holder_pubkeys(), self.context.get_counterparty_pubkeys(), &self.context.secp_ctx);
5527
5497
for (idx, (htlc, mut source_opt)) in htlcs_cloned.drain(..).enumerate() {
5528
5498
if let Some(_) = htlc.transaction_output_index {
5529
5499
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.feerate_per_kw,
5530
5500
self.context.get_counterparty_selected_contest_delay().unwrap(), &htlc, &self.context.channel_type,
5531
- &keys .broadcaster_delayed_payment_key, &keys .revocation_key);
5501
+ &holder_keys .broadcaster_delayed_payment_key, &holder_keys .revocation_key);
5532
5502
5533
- let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &keys );
5503
+ let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &holder_keys );
5534
5504
let htlc_sighashtype = if self.context.channel_type.supports_anchors_zero_fee_htlc_tx() { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
5535
5505
let htlc_sighash = hash_to_message!(&sighash::SighashCache::new(&htlc_tx).p2wsh_signature_hash(0, &htlc_redeemscript, htlc.to_bitcoin_amount(), htlc_sighashtype).unwrap()[..]);
5536
5506
log_trace!(logger, "Checking HTLC tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} in channel {}.",
5537
- log_bytes!(msg.htlc_signatures[idx].serialize_compact()[..]), log_bytes!(keys .countersignatory_htlc_key.to_public_key().serialize()),
5507
+ log_bytes!(msg.htlc_signatures[idx].serialize_compact()[..]), log_bytes!(holder_keys .countersignatory_htlc_key.to_public_key().serialize()),
5538
5508
encode::serialize_hex(&htlc_tx), log_bytes!(htlc_sighash[..]), encode::serialize_hex(&htlc_redeemscript), &self.context.channel_id());
5539
- if let Err(_) = self.context.secp_ctx.verify_ecdsa(&htlc_sighash, &msg.htlc_signatures[idx], &keys .countersignatory_htlc_key.to_public_key()) {
5509
+ if let Err(_) = self.context.secp_ctx.verify_ecdsa(&htlc_sighash, &msg.htlc_signatures[idx], &holder_keys .countersignatory_htlc_key.to_public_key()) {
5540
5510
return Err(ChannelError::close("Invalid HTLC tx signature from peer".to_owned()));
5541
5511
}
5542
5512
if !separate_nondust_htlc_sources {
@@ -6218,8 +6188,7 @@ impl<SP: Deref> FundedChannel<SP> where
6218
6188
// Before proposing a feerate update, check that we can actually afford the new fee.
6219
6189
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
6220
6190
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
6221
- let keys = self.context.build_holder_transaction_keys(self.holder_commitment_point.current_point());
6222
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.holder_commitment_point.transaction_number(), &keys, true, true, logger);
6191
+ 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);
6223
6192
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;
6224
6193
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
6225
6194
if holder_balance_msat < buffer_fee_msat + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
@@ -6532,8 +6501,7 @@ impl<SP: Deref> FundedChannel<SP> where
6532
6501
self.holder_commitment_point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
6533
6502
}
6534
6503
let funding_signed = if self.context.signer_pending_funding && !self.context.is_outbound() {
6535
- let counterparty_keys = self.context.build_remote_transaction_keys();
6536
- 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;
6504
+ 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;
6537
6505
self.context.get_funding_signed_msg(logger, counterparty_initial_commitment_tx)
6538
6506
} else { None };
6539
6507
// Provide a `channel_ready` message if we need to, but only if we're _not_ still pending
@@ -8469,8 +8437,7 @@ impl<SP: Deref> FundedChannel<SP> where
8469
8437
-> (Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>, CommitmentTransaction)
8470
8438
where L::Target: Logger
8471
8439
{
8472
- let counterparty_keys = self.context.build_remote_transaction_keys();
8473
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
8440
+ 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);
8474
8441
let counterparty_commitment_tx = commitment_stats.tx;
8475
8442
8476
8443
#[cfg(any(test, fuzzing))]
@@ -8501,8 +8468,7 @@ impl<SP: Deref> FundedChannel<SP> where
8501
8468
#[cfg(any(test, fuzzing))]
8502
8469
self.build_commitment_no_state_update(logger);
8503
8470
8504
- let counterparty_keys = self.context.build_remote_transaction_keys();
8505
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
8471
+ 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);
8506
8472
let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
8507
8473
8508
8474
match &self.context.holder_signer {
@@ -8528,7 +8494,8 @@ impl<SP: Deref> FundedChannel<SP> where
8528
8494
encode::serialize_hex(&commitment_stats.tx.trust().built_transaction().transaction),
8529
8495
&counterparty_commitment_txid, encode::serialize_hex(&self.context.get_funding_redeemscript()),
8530
8496
log_bytes!(signature.serialize_compact()[..]), &self.context.channel_id());
8531
-
8497
+
8498
+ let counterparty_keys = TxCreationKeys::from_channel_static_keys(&self.context.counterparty_cur_commitment_point.unwrap(), self.context.get_counterparty_pubkeys(), self.context.get_holder_pubkeys(), &self.context.secp_ctx);
8532
8499
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
8533
8500
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
8534
8501
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.feerate_per_kw, self.context.get_holder_selected_contest_delay(), htlc, &self.context.channel_type, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
@@ -8980,8 +8947,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
8980
8947
8981
8948
/// Only allowed after [`ChannelContext::channel_transaction_parameters`] is set.
8982
8949
fn get_funding_created_msg<L: Deref>(&mut self, logger: &L) -> Option<msgs::FundingCreated> where L::Target: Logger {
8983
- let counterparty_keys = self.context.build_remote_transaction_keys();
8984
- 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;
8950
+ 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;
8985
8951
let signature = match &self.context.holder_signer {
8986
8952
// TODO (taproot|arik): move match into calling method for Taproot
8987
8953
ChannelSignerType::Ecdsa(ecdsa) => {
@@ -11633,7 +11599,7 @@ mod tests {
11633
11599
$( { $htlc_idx: expr, $counterparty_htlc_sig_hex: expr, $htlc_sig_hex: expr, $htlc_tx_hex: expr } ), *
11634
11600
} ) => { {
11635
11601
let (commitment_tx, htlcs): (_, Vec<HTLCOutputInCommitment>) = {
11636
- let mut commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &keys , true, false, &logger);
11602
+ let mut commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &per_commitment_point , true, false, &logger);
11637
11603
11638
11604
let htlcs = commitment_stats.htlcs_included.drain(..)
11639
11605
.filter_map(|(htlc, _)| if htlc.transaction_output_index.is_some() { Some(htlc) } else { None })
0 commit comments