@@ -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
@@ -3483,9 +3481,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3483
3481
{
3484
3482
let funding_script = funding.get_funding_redeemscript();
3485
3483
3486
- let keys = self.build_holder_transaction_keys(funding, holder_commitment_point.current_point());
3487
-
3488
- let commitment_stats = self.build_commitment_transaction(funding, holder_commitment_point.transaction_number(), &keys, true, false, logger);
3484
+ let commitment_stats = self.build_commitment_transaction(funding, holder_commitment_point.transaction_number(), &holder_commitment_point.current_point(), true, false, logger);
3489
3485
let commitment_txid = {
3490
3486
let trusted_tx = commitment_stats.tx.trust();
3491
3487
let bitcoin_tx = trusted_tx.built_transaction();
@@ -3553,19 +3549,20 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3553
3549
3554
3550
let mut nondust_htlc_sources = Vec::with_capacity(htlcs_cloned.len());
3555
3551
let mut htlcs_and_sigs = Vec::with_capacity(htlcs_cloned.len());
3552
+ let holder_keys = commitment_stats.tx.trust().keys();
3556
3553
for (idx, (htlc, mut source_opt)) in htlcs_cloned.drain(..).enumerate() {
3557
3554
if let Some(_) = htlc.transaction_output_index {
3558
3555
let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, commitment_stats.feerate_per_kw,
3559
3556
funding.get_counterparty_selected_contest_delay().unwrap(), &htlc, &self.channel_type,
3560
- &keys .broadcaster_delayed_payment_key, &keys .revocation_key);
3557
+ &holder_keys .broadcaster_delayed_payment_key, &holder_keys .revocation_key);
3561
3558
3562
- let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &self.channel_type, &keys );
3559
+ let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &self.channel_type, &holder_keys );
3563
3560
let htlc_sighashtype = if self.channel_type.supports_anchors_zero_fee_htlc_tx() { EcdsaSighashType::SinglePlusAnyoneCanPay } else { EcdsaSighashType::All };
3564
3561
let htlc_sighash = hash_to_message!(&sighash::SighashCache::new(&htlc_tx).p2wsh_signature_hash(0, &htlc_redeemscript, htlc.to_bitcoin_amount(), htlc_sighashtype).unwrap()[..]);
3565
3562
log_trace!(logger, "Checking HTLC tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} in channel {}.",
3566
- log_bytes!(msg.htlc_signatures[idx].serialize_compact()[..]), log_bytes!(keys .countersignatory_htlc_key.to_public_key().serialize()),
3563
+ log_bytes!(msg.htlc_signatures[idx].serialize_compact()[..]), log_bytes!(holder_keys .countersignatory_htlc_key.to_public_key().serialize()),
3567
3564
encode::serialize_hex(&htlc_tx), log_bytes!(htlc_sighash[..]), encode::serialize_hex(&htlc_redeemscript), &self.channel_id());
3568
- if let Err(_) = self.secp_ctx.verify_ecdsa(&htlc_sighash, &msg.htlc_signatures[idx], &keys .countersignatory_htlc_key.to_public_key()) {
3565
+ if let Err(_) = self.secp_ctx.verify_ecdsa(&htlc_sighash, &msg.htlc_signatures[idx], &holder_keys .countersignatory_htlc_key.to_public_key()) {
3569
3566
return Err(ChannelError::close("Invalid HTLC tx signature from peer".to_owned()));
3570
3567
}
3571
3568
if !separate_nondust_htlc_sources {
@@ -3614,7 +3611,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3614
3611
/// generated by the peer which proposed adding the HTLCs, and thus we need to understand both
3615
3612
/// which peer generated this transaction and "to whom" this transaction flows.
3616
3613
#[inline]
3617
- fn build_commitment_transaction<L: Deref>(&self, funding: &FundingScope, commitment_number: u64, keys : &TxCreationKeys , local: bool, generated_by_local: bool, logger: &L) -> CommitmentStats
3614
+ 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
3618
3615
where L::Target: Logger
3619
3616
{
3620
3617
let mut included_dust_htlcs: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::new();
@@ -3816,7 +3813,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3816
3813
if local { funding.channel_transaction_parameters.as_holder_broadcastable() }
3817
3814
else { funding.channel_transaction_parameters.as_counterparty_broadcastable() };
3818
3815
let tx = CommitmentTransaction::new_with_auxiliary_htlc_data(commitment_number,
3819
- &keys. per_commitment_point,
3816
+ per_commitment_point,
3820
3817
value_to_a as u64,
3821
3818
value_to_b as u64,
3822
3819
feerate_per_kw,
@@ -3842,32 +3839,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3842
3839
}
3843
3840
}
3844
3841
3845
- #[inline]
3846
- /// Creates a set of keys for build_commitment_transaction to generate a transaction which our
3847
- /// counterparty will sign (ie DO NOT send signatures over a transaction created by this to
3848
- /// our counterparty!)
3849
- /// The result is a transaction which we can revoke broadcastership of (ie a "local" transaction)
3850
- /// TODO Some magic rust shit to compile-time check this?
3851
- fn build_holder_transaction_keys(&self, funding: &FundingScope, per_commitment_point: PublicKey) -> TxCreationKeys {
3852
- let delayed_payment_base = &funding.get_holder_pubkeys().delayed_payment_basepoint;
3853
- let htlc_basepoint = &funding.get_holder_pubkeys().htlc_basepoint;
3854
- let counterparty_pubkeys = funding.get_counterparty_pubkeys();
3855
-
3856
- TxCreationKeys::derive_new(&self.secp_ctx, &per_commitment_point, delayed_payment_base, htlc_basepoint, &counterparty_pubkeys.revocation_basepoint, &counterparty_pubkeys.htlc_basepoint)
3857
- }
3858
-
3859
- #[inline]
3860
- /// Creates a set of keys for build_commitment_transaction to generate a transaction which we
3861
- /// will sign and send to our counterparty.
3862
- /// If an Err is returned, it is a ChannelError::Close (for get_funding_created)
3863
- fn build_remote_transaction_keys(&self, funding: &FundingScope) -> TxCreationKeys {
3864
- let revocation_basepoint = &funding.get_holder_pubkeys().revocation_basepoint;
3865
- let htlc_basepoint = &funding.get_holder_pubkeys().htlc_basepoint;
3866
- let counterparty_pubkeys = funding.get_counterparty_pubkeys();
3867
-
3868
- 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)
3869
- }
3870
-
3871
3842
pub fn get_feerate_sat_per_1000_weight(&self) -> u32 {
3872
3843
self.feerate_per_kw
3873
3844
}
@@ -4650,9 +4621,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
4650
4621
SP::Target: SignerProvider,
4651
4622
L::Target: Logger
4652
4623
{
4653
- let counterparty_keys = self.build_remote_transaction_keys(funding);
4654
4624
let counterparty_initial_commitment_tx = self.build_commitment_transaction(
4655
- funding, self.cur_counterparty_commitment_transaction_number, &counterparty_keys , false, false, logger).tx;
4625
+ funding, self.cur_counterparty_commitment_transaction_number, &self.counterparty_cur_commitment_point.unwrap() , false, false, logger).tx;
4656
4626
match self.holder_signer {
4657
4627
// TODO (taproot|arik): move match into calling method for Taproot
4658
4628
ChannelSignerType::Ecdsa(ref ecdsa) => {
@@ -6365,8 +6335,7 @@ impl<SP: Deref> FundedChannel<SP> where
6365
6335
// Before proposing a feerate update, check that we can actually afford the new fee.
6366
6336
let dust_exposure_limiting_feerate = self.context.get_dust_exposure_limiting_feerate(&fee_estimator);
6367
6337
let htlc_stats = self.context.get_pending_htlc_stats(Some(feerate_per_kw), dust_exposure_limiting_feerate);
6368
- let keys = self.context.build_holder_transaction_keys(&self.funding, self.holder_commitment_point.current_point());
6369
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.holder_commitment_point.transaction_number(), &keys, true, true, logger);
6338
+ 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);
6370
6339
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;
6371
6340
let holder_balance_msat = commitment_stats.local_balance_msat - htlc_stats.outbound_holding_cell_msat;
6372
6341
if holder_balance_msat < buffer_fee_msat + self.funding.counterparty_selected_channel_reserve_satoshis.unwrap() * 1000 {
@@ -6679,8 +6648,7 @@ impl<SP: Deref> FundedChannel<SP> where
6679
6648
self.holder_commitment_point.try_resolve_pending(&self.context.holder_signer, &self.context.secp_ctx, logger);
6680
6649
}
6681
6650
let funding_signed = if self.context.signer_pending_funding && !self.funding.is_outbound() {
6682
- let counterparty_keys = self.context.build_remote_transaction_keys(&self.funding);
6683
- 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;
6651
+ 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;
6684
6652
self.context.get_funding_signed_msg(&self.funding.channel_transaction_parameters, logger, counterparty_initial_commitment_tx)
6685
6653
} else { None };
6686
6654
// Provide a `channel_ready` message if we need to, but only if we're _not_ still pending
@@ -8753,8 +8721,7 @@ impl<SP: Deref> FundedChannel<SP> where
8753
8721
-> (Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>, CommitmentTransaction)
8754
8722
where L::Target: Logger
8755
8723
{
8756
- let counterparty_keys = self.context.build_remote_transaction_keys(&self.funding);
8757
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
8724
+ 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);
8758
8725
let counterparty_commitment_tx = commitment_stats.tx;
8759
8726
8760
8727
#[cfg(any(test, fuzzing))]
@@ -8785,8 +8752,7 @@ impl<SP: Deref> FundedChannel<SP> where
8785
8752
#[cfg(any(test, fuzzing))]
8786
8753
self.build_commitment_no_state_update(logger);
8787
8754
8788
- let counterparty_keys = self.context.build_remote_transaction_keys(&self.funding);
8789
- let commitment_stats = self.context.build_commitment_transaction(&self.funding, self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
8755
+ 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);
8790
8756
let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
8791
8757
8792
8758
match &self.context.holder_signer {
@@ -8814,6 +8780,7 @@ impl<SP: Deref> FundedChannel<SP> where
8814
8780
&counterparty_commitment_txid, encode::serialize_hex(&self.funding.get_funding_redeemscript()),
8815
8781
log_bytes!(signature.serialize_compact()[..]), &self.context.channel_id());
8816
8782
8783
+ let counterparty_keys = commitment_stats.tx.trust().keys();
8817
8784
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
8818
8785
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
8819
8786
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)),
@@ -9270,8 +9237,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
9270
9237
9271
9238
/// Only allowed after [`FundingScope::channel_transaction_parameters`] is set.
9272
9239
fn get_funding_created_msg<L: Deref>(&mut self, logger: &L) -> Option<msgs::FundingCreated> where L::Target: Logger {
9273
- let counterparty_keys = self.context.build_remote_transaction_keys(&self.funding);
9274
- 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;
9240
+ 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;
9275
9241
let signature = match &self.context.holder_signer {
9276
9242
// TODO (taproot|arik): move match into calling method for Taproot
9277
9243
ChannelSignerType::Ecdsa(ecdsa) => {
@@ -11835,7 +11801,7 @@ mod tests {
11835
11801
use bitcoin::secp256k1::Message;
11836
11802
use crate::sign::{ChannelDerivationParameters, HTLCDescriptor, ecdsa::EcdsaChannelSigner};
11837
11803
use crate::types::payment::PaymentPreimage;
11838
- use crate::ln::channel::{ HTLCOutputInCommitment ,TxCreationKeys} ;
11804
+ use crate::ln::channel::HTLCOutputInCommitment;
11839
11805
use crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint};
11840
11806
use crate::ln::chan_utils::{ChannelPublicKeys, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
11841
11807
use crate::util::logger::Logger;
@@ -11903,11 +11869,6 @@ mod tests {
11903
11869
// build_commitment_transaction.
11904
11870
let per_commitment_secret = SecretKey::from_slice(&<Vec<u8>>::from_hex("1f1e1d1c1b1a191817161514131211100f0e0d0c0b0a09080706050403020100").unwrap()[..]).unwrap();
11905
11871
let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
11906
- let directed_params = chan.funding.channel_transaction_parameters.as_holder_broadcastable();
11907
- let keys = TxCreationKeys::from_channel_static_keys(
11908
- &per_commitment_point, directed_params.broadcaster_pubkeys(),
11909
- directed_params.countersignatory_pubkeys(), &secp_ctx,
11910
- );
11911
11872
11912
11873
macro_rules! test_commitment {
11913
11874
( $counterparty_sig_hex: expr, $sig_hex: expr, $tx_hex: expr, $($remain:tt)* ) => {
@@ -11928,7 +11889,7 @@ mod tests {
11928
11889
$( { $htlc_idx: expr, $counterparty_htlc_sig_hex: expr, $htlc_sig_hex: expr, $htlc_tx_hex: expr } ), *
11929
11890
} ) => { {
11930
11891
let (commitment_tx, htlcs): (_, Vec<HTLCOutputInCommitment>) = {
11931
- let mut commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &keys , true, false, &logger);
11892
+ let mut commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &per_commitment_point , true, false, &logger);
11932
11893
11933
11894
let htlcs = commitment_stats.htlcs_included.drain(..)
11934
11895
.filter_map(|(htlc, _)| if htlc.transaction_output_index.is_some() { Some(htlc) } else { None })
@@ -11976,6 +11937,7 @@ mod tests {
11976
11937
let remote_signature = Signature::from_der(&<Vec<u8>>::from_hex($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
11977
11938
11978
11939
let ref htlc = htlcs[$htlc_idx];
11940
+ let keys = commitment_tx.trust().keys();
11979
11941
let mut htlc_tx = chan_utils::build_htlc_transaction(&unsigned_tx.txid, chan.context.feerate_per_kw,
11980
11942
chan.funding.get_counterparty_selected_contest_delay().unwrap(),
11981
11943
&htlc, $opt_anchors, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
0 commit comments