@@ -29,9 +29,9 @@ use bitcoin::locktime::absolute::LockTime;
29
29
use crate::ln::types::{ChannelId, PaymentPreimage, PaymentHash};
30
30
use crate::ln::features::{ChannelTypeFeatures, InitFeatures};
31
31
use crate::ln::interactivetxs::{
32
- AbortReason, estimate_input_weight, get_output_weight, HandleTxCompleteResult ,
33
- InteractiveTxConstructor, InteractiveTxMessageSend, InteractiveTxMessageSendResult ,
34
- OutputOwned, TX_COMMON_FIELDS_WEIGHT,
32
+ AbortReason, ConstructedTransaction, estimate_input_weight, get_output_weight ,
33
+ HandleTxCompleteResult, InteractiveTxConstructor, InteractiveTxMessageSend ,
34
+ InteractiveTxMessageSendResult, OutputOwned, SharedOwnedOutput , TX_COMMON_FIELDS_WEIGHT,
35
35
};
36
36
use crate::ln::msgs;
37
37
use crate::ln::msgs::DecodeError;
@@ -64,7 +64,6 @@ use crate::sync::Mutex;
64
64
use crate::sign::type_resolver::ChannelSignerType;
65
65
66
66
use super::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint, RevocationBasepoint};
67
- use super::interactivetxs::SharedOwnedOutput;
68
67
69
68
#[cfg(test)]
70
69
pub struct ChannelValueStat {
@@ -3822,7 +3821,7 @@ pub(super) struct DualFundingChannelContext {
3822
3821
/// The amount in satoshis we will be contributing to the channel.
3823
3822
pub our_funding_satoshis: u64,
3824
3823
/// The amount in satoshis our counterparty will be contributing to the channel.
3825
- pub their_funding_satoshis: u64,
3824
+ pub their_funding_satoshis: Option< u64> ,
3826
3825
/// The funding transaction locktime suggested by the initiator. If set by us, it is always set
3827
3826
/// to the current block height to align incentives against fee-sniping.
3828
3827
pub funding_tx_locktime: LockTime,
@@ -4613,6 +4612,105 @@ impl<SP: Deref> Channel<SP> where
4613
4612
Ok(())
4614
4613
}
4615
4614
4615
+ pub fn commitment_signed_initial_v2<L: Deref>(
4616
+ &mut self, msg: &msgs::CommitmentSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
4617
+ ) -> Result<ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>, ChannelError>
4618
+ where L::Target: Logger
4619
+ {
4620
+ if !matches!(self.context.channel_state, ChannelState::FundingNegotiated) {
4621
+ return Err(ChannelError::Close(
4622
+ (
4623
+ "Received initial commitment_signed before funding transaction constructed!".to_owned(),
4624
+ ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
4625
+ )));
4626
+ }
4627
+ if self.context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
4628
+ self.context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
4629
+ self.context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
4630
+ panic!("Should not have advanced channel commitment tx numbers prior to funding_created");
4631
+ }
4632
+ let dual_funding_channel_context = self.dual_funding_channel_context.as_mut().ok_or(
4633
+ ChannelError::Close(("Have no context for dual-funded channel".to_owned(), ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) }))
4634
+ )?;
4635
+
4636
+ let funding_script = self.context.get_funding_redeemscript();
4637
+
4638
+ let counterparty_keys = self.context.build_remote_transaction_keys();
4639
+ let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
4640
+ let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
4641
+ let counterparty_initial_bitcoin_tx = counterparty_trusted_tx.built_transaction();
4642
+
4643
+ log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
4644
+ &self.context.channel_id(), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
4645
+
4646
+ let holder_signer = self.context.build_holder_transaction_keys();
4647
+ let initial_commitment_tx = self.context.build_commitment_transaction(
4648
+ self.context.holder_commitment_point.transaction_number(), &holder_signer, true, false, logger
4649
+ ).tx;
4650
+ {
4651
+ let trusted_tx = initial_commitment_tx.trust();
4652
+ let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
4653
+ let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.context.channel_value_satoshis);
4654
+ // They sign our commitment transaction, allowing us to broadcast the tx if we wish.
4655
+ if let Err(_) = self.context.secp_ctx.verify_ecdsa(&sighash, &msg.signature, &self.context.get_counterparty_pubkeys().funding_pubkey) {
4656
+ return Err(ChannelError::Close(
4657
+ (
4658
+ "Invalid funding_signed signature from peer".to_owned(),
4659
+ ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
4660
+ )));
4661
+ }
4662
+ }
4663
+
4664
+ let holder_commitment_tx = HolderCommitmentTransaction::new(
4665
+ initial_commitment_tx,
4666
+ msg.signature,
4667
+ Vec::new(),
4668
+ &self.context.get_holder_pubkeys().funding_pubkey,
4669
+ self.context.counterparty_funding_pubkey()
4670
+ );
4671
+
4672
+ self.context.holder_signer.as_ref().validate_holder_commitment(&holder_commitment_tx, Vec::new())
4673
+ .map_err(|_| ChannelError::Close(
4674
+ (
4675
+ "Failed to validate our commitment".to_owned(),
4676
+ ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
4677
+ )))?;
4678
+
4679
+ let funding_redeemscript = self.context.get_funding_redeemscript();
4680
+ let funding_txo = self.context.get_funding_txo().unwrap();
4681
+ let funding_txo_script = funding_redeemscript.to_p2wsh();
4682
+ let obscure_factor = get_commitment_transaction_number_obscure_factor(&self.context.get_holder_pubkeys().payment_point, &self.context.get_counterparty_pubkeys().payment_point, self.context.is_outbound());
4683
+ let shutdown_script = self.context.shutdown_scriptpubkey.clone().map(|script| script.into_inner());
4684
+ let mut monitor_signer = signer_provider.derive_channel_signer(self.context.channel_value_satoshis, self.context.channel_keys_id);
4685
+ monitor_signer.provide_channel_parameters(&self.context.channel_transaction_parameters);
4686
+ let channel_monitor = ChannelMonitor::new(self.context.secp_ctx.clone(), monitor_signer,
4687
+ shutdown_script, self.context.get_holder_selected_contest_delay(),
4688
+ &self.context.destination_script, (funding_txo, funding_txo_script),
4689
+ &self.context.channel_transaction_parameters,
4690
+ funding_redeemscript.clone(), self.context.channel_value_satoshis,
4691
+ obscure_factor,
4692
+ holder_commitment_tx, best_block, self.context.counterparty_node_id, self.context.channel_id());
4693
+
4694
+ channel_monitor.provide_initial_counterparty_commitment_tx(
4695
+ counterparty_initial_bitcoin_tx.txid, Vec::new(),
4696
+ self.context.cur_counterparty_commitment_transaction_number,
4697
+ self.context.counterparty_cur_commitment_point.unwrap(),
4698
+ counterparty_initial_commitment_tx.feerate_per_kw(),
4699
+ counterparty_initial_commitment_tx.to_broadcaster_value_sat(),
4700
+ counterparty_initial_commitment_tx.to_countersignatory_value_sat(), logger);
4701
+
4702
+ assert!(!self.context.channel_state.is_monitor_update_in_progress()); // We have no had any monitor(s) yet to fail update!
4703
+ self.context.holder_commitment_point.advance(&self.context.holder_signer, &self.context.secp_ctx, logger);
4704
+ self.context.cur_counterparty_commitment_transaction_number -= 1;
4705
+
4706
+ log_info!(logger, "Received initial commitment_signed from peer for channel {}", &self.context.channel_id());
4707
+
4708
+ let need_channel_ready = self.check_get_channel_ready(0, logger).is_some();
4709
+ self.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
4710
+
4711
+ Ok(channel_monitor)
4712
+ }
4713
+
4616
4714
pub fn commitment_signed<L: Deref>(&mut self, msg: &msgs::CommitmentSigned, logger: &L) -> Result<Option<ChannelMonitorUpdate>, ChannelError>
4617
4715
where L::Target: Logger
4618
4716
{
@@ -7848,7 +7946,8 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
7848
7946
&mut self, msg: &msgs::AcceptChannel, default_limits: &ChannelHandshakeLimits,
7849
7947
their_features: &InitFeatures
7850
7948
) -> Result<(), ChannelError> {
7851
- self.context.do_accept_channel_checks(default_limits, their_features, &msg.common_fields, msg.channel_reserve_satoshis)
7949
+ self.context.do_accept_channel_checks(
7950
+ default_limits, their_features, &msg.common_fields, msg.channel_reserve_satoshis)
7852
7951
}
7853
7952
7854
7953
/// Handles a funding_signed message from the remote end.
@@ -8300,7 +8399,7 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
8300
8399
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
8301
8400
dual_funding_context: DualFundingChannelContext {
8302
8401
our_funding_satoshis: funding_satoshis,
8303
- their_funding_satoshis: 0 ,
8402
+ their_funding_satoshis: None ,
8304
8403
funding_tx_locktime,
8305
8404
funding_feerate_sat_per_1000_weight,
8306
8405
our_funding_inputs: funding_inputs,
@@ -8370,6 +8469,28 @@ impl<SP: Deref> OutboundV2Channel<SP> where SP::Target: SignerProvider {
8370
8469
require_confirmed_inputs: None,
8371
8470
}
8372
8471
}
8472
+
8473
+ pub fn funding_tx_constructed<L: Deref>(
8474
+ mut self, transaction: ConstructedTransaction, est_block: BestBlock, signer_provider: &SP, logger: &L
8475
+ ) -> Result<(Channel<SP>, msgs::CommitmentSigned), (Self, ChannelError)>
8476
+ where
8477
+ L::Target: Logger
8478
+ {
8479
+ let res = get_initial_commitment_signed(&mut self.context, transaction, est_block,
8480
+ signer_provider, logger);
8481
+ let commitment_signed = match res {
8482
+ Ok(commitment_signed) => commitment_signed,
8483
+ Err(err) => return Err((self, err)),
8484
+ };
8485
+
8486
+ let channel = Channel {
8487
+ context: self.context,
8488
+ dual_funding_channel_context: Some(self.dual_funding_context),
8489
+ interactive_tx_constructor: self.interactive_tx_constructor,
8490
+ };
8491
+
8492
+ Ok((channel, commitment_signed))
8493
+ }
8373
8494
}
8374
8495
8375
8496
// A not-yet-funded inbound (from counterparty) channel using V2 channel establishment.
@@ -8456,7 +8577,7 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
8456
8577
unfunded_context: UnfundedChannelContext { unfunded_channel_age_ticks: 0 },
8457
8578
dual_funding_context: DualFundingChannelContext {
8458
8579
our_funding_satoshis: funding_satoshis,
8459
- their_funding_satoshis: msg.common_fields.funding_satoshis,
8580
+ their_funding_satoshis: Some( msg.common_fields.funding_satoshis) ,
8460
8581
funding_tx_locktime: LockTime::from_consensus(msg.locktime),
8461
8582
funding_feerate_sat_per_1000_weight: msg.funding_feerate_sat_per_1000_weight,
8462
8583
our_funding_inputs: funding_inputs,
@@ -8535,6 +8656,28 @@ impl<SP: Deref> InboundV2Channel<SP> where SP::Target: SignerProvider {
8535
8656
pub fn get_accept_channel_v2_message(&self) -> msgs::AcceptChannelV2 {
8536
8657
self.generate_accept_channel_v2_message()
8537
8658
}
8659
+
8660
+ pub fn funding_tx_constructed<L: Deref>(
8661
+ mut self, transaction: ConstructedTransaction, est_block: BestBlock, signer_provider: &SP, logger: &L
8662
+ ) -> Result<(Channel<SP>, msgs::CommitmentSigned), (Self, ChannelError)>
8663
+ where
8664
+ L::Target: Logger
8665
+ {
8666
+ let res = get_initial_commitment_signed(&mut self.context, transaction, est_block,
8667
+ signer_provider, logger);
8668
+ let commitment_signed = match res {
8669
+ Ok(commitment_signed) => commitment_signed,
8670
+ Err(err) => return Err((self, err)),
8671
+ };
8672
+
8673
+ let channel = Channel {
8674
+ context: self.context,
8675
+ dual_funding_channel_context: Some(self.dual_funding_context),
8676
+ interactive_tx_constructor: self.interactive_tx_constructor,
8677
+ };
8678
+
8679
+ Ok((channel, commitment_signed))
8680
+ }
8538
8681
}
8539
8682
8540
8683
// Unfunded channel utilities
@@ -8562,6 +8705,84 @@ fn get_initial_channel_type(config: &UserConfig, their_features: &InitFeatures)
8562
8705
ret
8563
8706
}
8564
8707
8708
+ fn get_initial_counterparty_commitment_signature<SP:Deref, L: Deref>(
8709
+ context: &mut ChannelContext<SP>, logger: &L
8710
+ ) -> Result<Signature, ChannelError>
8711
+ where
8712
+ SP::Target: SignerProvider,
8713
+ L::Target: Logger
8714
+ {
8715
+ let counterparty_keys = context.build_remote_transaction_keys();
8716
+ let counterparty_initial_commitment_tx = context.build_commitment_transaction(
8717
+ context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
8718
+ match context.holder_signer {
8719
+ // TODO (taproot|arik): move match into calling method for Taproot
8720
+ ChannelSignerType::Ecdsa(ref ecdsa) => {
8721
+ Ok(ecdsa.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), Vec::new(), &context.secp_ctx)
8722
+ .map_err(|_| ChannelError::Close(
8723
+ (
8724
+ "Failed to get signatures for new commitment_signed".to_owned(),
8725
+ ClosureReason::HolderForceClosed { broadcasted_latest_txn: Some(false) },
8726
+ )))?.0)
8727
+ },
8728
+ // TODO (taproot|arik)
8729
+ #[cfg(taproot)]
8730
+ _ => todo!(),
8731
+ }
8732
+ }
8733
+
8734
+ fn get_initial_commitment_signed<SP:Deref, L: Deref>(
8735
+ context: &mut ChannelContext<SP>, transaction: ConstructedTransaction, est_block: BestBlock, signer_provider: &SP, logger: &L
8736
+ ) -> Result<msgs::CommitmentSigned, ChannelError>
8737
+ where
8738
+ SP::Target: SignerProvider,
8739
+ L::Target: Logger
8740
+ {
8741
+ if !matches!(
8742
+ context.channel_state, ChannelState::NegotiatingFunding(flags)
8743
+ if flags == (NegotiatingFundingFlags::OUR_INIT_SENT | NegotiatingFundingFlags::THEIR_INIT_SENT)) {
8744
+ panic!("Tried to get a funding_created messsage at a time other than immediately after initial handshake completion (or tried to get funding_created twice)");
8745
+ }
8746
+ if context.commitment_secrets.get_min_seen_secret() != (1 << 48) ||
8747
+ context.cur_counterparty_commitment_transaction_number != INITIAL_COMMITMENT_NUMBER ||
8748
+ context.holder_commitment_point.transaction_number() != INITIAL_COMMITMENT_NUMBER {
8749
+ panic!("Should not have advanced channel commitment tx numbers prior to initial commitment_signed");
8750
+ }
8751
+
8752
+ let funding_redeemscript = context.get_funding_redeemscript().to_p2wsh();
8753
+ let funding_outpoint_index = transaction.outputs().enumerate().find_map(
8754
+ |(idx, output)| {
8755
+ if output.tx_out().script_pubkey == funding_redeemscript { Some(idx as u16) } else { None }
8756
+ }).expect("funding transaction contains funding output");
8757
+ let funding_txo = OutPoint { txid: transaction.txid(), index: funding_outpoint_index };
8758
+ context.channel_transaction_parameters.funding_outpoint = Some(funding_txo);
8759
+ context.holder_signer.as_mut().provide_channel_parameters(&context.channel_transaction_parameters);
8760
+
8761
+ let signature = match get_initial_counterparty_commitment_signature(context, logger) {
8762
+ Ok(res) => res,
8763
+ Err(e) => {
8764
+ log_error!(logger, "Got bad signatures: {:?}!", e);
8765
+ context.channel_transaction_parameters.funding_outpoint = None;
8766
+ return Err(e);
8767
+ }
8768
+ };
8769
+
8770
+ if context.signer_pending_funding {
8771
+ log_trace!(logger, "Counterparty commitment signature ready for funding_created message: clearing signer_pending_funding");
8772
+ context.signer_pending_funding = false;
8773
+ }
8774
+
8775
+ log_info!(logger, "Generated commitment_signed for peer for channel {}", &context.channel_id());
8776
+
8777
+ Ok(msgs::CommitmentSigned {
8778
+ channel_id: context.channel_id.clone(),
8779
+ htlc_signatures: vec![],
8780
+ signature,
8781
+ #[cfg(taproot)]
8782
+ partial_signature_with_nonce: None,
8783
+ })
8784
+ }
8785
+
8565
8786
const SERIALIZATION_VERSION: u8 = 4;
8566
8787
const MIN_SERIALIZATION_VERSION: u8 = 3;
8567
8788
0 commit comments