@@ -2129,6 +2129,43 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
2129
2129
next_local_nonce: None,
2130
2130
})
2131
2131
}
2132
+
2133
+ /// Only allowed after [`Self::channel_transaction_parameters`] is set.
2134
+ fn get_funding_signed_msg<L: Deref>(&mut self, logger: &L) -> (CommitmentTransaction, Option<msgs::FundingSigned>) where L::Target: Logger {
2135
+ let counterparty_keys = self.build_remote_transaction_keys();
2136
+ let counterparty_initial_commitment_tx = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number + 1, &counterparty_keys, false, false, logger).tx;
2137
+
2138
+ let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
2139
+ let counterparty_initial_bitcoin_tx = counterparty_trusted_tx.built_transaction();
2140
+ log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
2141
+ &self.channel_id(), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
2142
+
2143
+ match &self.holder_signer {
2144
+ // TODO (arik): move match into calling method for Taproot
2145
+ ChannelSignerType::Ecdsa(ecdsa) => {
2146
+ let funding_signed = ecdsa.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.secp_ctx)
2147
+ .map(|(signature, _)| msgs::FundingSigned {
2148
+ channel_id: self.channel_id(),
2149
+ signature,
2150
+ #[cfg(taproot)]
2151
+ partial_signature_with_nonce: None,
2152
+ })
2153
+ .ok();
2154
+
2155
+ if funding_signed.is_none() {
2156
+ log_trace!(logger, "Counterparty commitment signature not available for funding_signed message; setting signer_pending_funding");
2157
+ self.signer_pending_funding = true;
2158
+ } else if self.signer_pending_funding {
2159
+ log_trace!(logger, "Counterparty commitment signature available for funding_signed message; clearing signer_pending_funding");
2160
+ self.signer_pending_funding = false;
2161
+ }
2162
+
2163
+ // We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
2164
+ (counterparty_initial_commitment_tx, funding_signed)
2165
+ }
2166
+ }
2167
+ }
2168
+
2132
2169
}
2133
2170
2134
2171
// Internal utility functions for channels
@@ -3957,8 +3994,12 @@ impl<SP: Deref> Channel<SP> where
3957
3994
let commitment_update = if self.context.signer_pending_commitment_update {
3958
3995
self.get_last_commitment_update_for_send(logger).ok()
3959
3996
} else { None };
3960
- let funding_signed = None;
3961
- let channel_ready = None;
3997
+ let funding_signed = if self.context.signer_pending_funding && !self.context.is_outbound() {
3998
+ self.context.get_funding_signed_msg(logger).1
3999
+ } else { None };
4000
+ let channel_ready = if funding_signed.is_some() {
4001
+ self.check_get_channel_ready(0)
4002
+ } else { None };
3962
4003
let funding_created = if self.context.signer_pending_funding && self.context.is_outbound() {
3963
4004
self.context.get_funding_created_msg(logger)
3964
4005
} else { None };
@@ -6730,41 +6771,22 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6730
6771
self.generate_accept_channel_message()
6731
6772
}
6732
6773
6733
- fn funding_created_signature <L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<( CommitmentTransaction, CommitmentTransaction, Option<Signature>) , ChannelError> where L::Target: Logger {
6774
+ fn check_funding_created_signature <L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<CommitmentTransaction, ChannelError> where L::Target: Logger {
6734
6775
let funding_script = self.context.get_funding_redeemscript();
6735
6776
6736
6777
let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
6737
6778
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger).tx;
6738
- {
6739
- let trusted_tx = initial_commitment_tx.trust();
6740
- let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
6741
- let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.context.channel_value_satoshis);
6742
- // They sign the holder commitment transaction...
6743
- log_trace!(logger, "Checking funding_created tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} for channel {}.",
6744
- log_bytes!(sig.serialize_compact()[..]), log_bytes!(self.context.counterparty_funding_pubkey().serialize()),
6745
- encode::serialize_hex(&initial_commitment_bitcoin_tx.transaction), log_bytes!(sighash[..]),
6746
- encode::serialize_hex(&funding_script), &self.context.channel_id());
6747
- secp_check!(self.context.secp_ctx.verify_ecdsa(&sighash, &sig, self.context.counterparty_funding_pubkey()), "Invalid funding_created signature from peer".to_owned());
6748
- }
6749
-
6750
- let counterparty_keys = self.context.build_remote_transaction_keys();
6751
- let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
6752
-
6753
- let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
6754
- let counterparty_initial_bitcoin_tx = counterparty_trusted_tx.built_transaction();
6755
- log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
6756
- &self.context.channel_id(), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
6757
-
6758
- match &self.context.holder_signer {
6759
- // TODO (arik): move match into calling method for Taproot
6760
- ChannelSignerType::Ecdsa(ecdsa) => {
6761
- let counterparty_signature = ecdsa.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.context.secp_ctx)
6762
- .map(|(sig, _)| sig).ok();
6779
+ let trusted_tx = initial_commitment_tx.trust();
6780
+ let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
6781
+ let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.context.channel_value_satoshis);
6782
+ // They sign the holder commitment transaction...
6783
+ log_trace!(logger, "Checking funding_created tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} for channel {}.",
6784
+ log_bytes!(sig.serialize_compact()[..]), log_bytes!(self.context.counterparty_funding_pubkey().serialize()),
6785
+ encode::serialize_hex(&initial_commitment_bitcoin_tx.transaction), log_bytes!(sighash[..]),
6786
+ encode::serialize_hex(&funding_script), &self.context.channel_id());
6787
+ secp_check!(self.context.secp_ctx.verify_ecdsa(&sighash, &sig, self.context.counterparty_funding_pubkey()), "Invalid funding_created signature from peer".to_owned());
6763
6788
6764
- // We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
6765
- Ok((counterparty_initial_commitment_tx, initial_commitment_tx, counterparty_signature))
6766
- }
6767
- }
6789
+ Ok(initial_commitment_tx)
6768
6790
}
6769
6791
6770
6792
pub fn funding_created<L: Deref>(
@@ -6791,10 +6813,10 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6791
6813
let funding_txo = OutPoint { txid: msg.funding_txid, index: msg.funding_output_index };
6792
6814
self.context.channel_transaction_parameters.funding_outpoint = Some(funding_txo);
6793
6815
// This is an externally observable change before we finish all our checks. In particular
6794
- // funding_created_signature may fail.
6816
+ // check_funding_created_signature may fail.
6795
6817
self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
6796
6818
6797
- let (counterparty_initial_commitment_tx, initial_commitment_tx, sig_opt) = match self.funding_created_signature (&msg.signature, logger) {
6819
+ let initial_commitment_tx = match self.check_funding_created_signature (&msg.signature, logger) {
6798
6820
Ok(res) => res,
6799
6821
Err(ChannelError::Close(e)) => {
6800
6822
self.context.channel_transaction_parameters.funding_outpoint = None;
@@ -6803,7 +6825,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6803
6825
Err(e) => {
6804
6826
// The only error we know how to handle is ChannelError::Close, so we fall over here
6805
6827
// to make sure we don't continue with an inconsistent state.
6806
- panic!("unexpected error type from funding_created_signature {:?}", e);
6828
+ panic!("unexpected error type from check_funding_created_signature {:?}", e);
6807
6829
}
6808
6830
};
6809
6831
@@ -6821,6 +6843,13 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6821
6843
6822
6844
// Now that we're past error-generating stuff, update our local state:
6823
6845
6846
+ self.context.channel_state = ChannelState::FundingSent as u32;
6847
+ self.context.channel_id = funding_txo.to_channel_id();
6848
+ self.context.cur_counterparty_commitment_transaction_number -= 1;
6849
+ self.context.cur_holder_commitment_transaction_number -= 1;
6850
+
6851
+ let (counterparty_initial_commitment_tx, funding_signed) = self.context.get_funding_signed_msg(logger);
6852
+
6824
6853
let funding_redeemscript = self.context.get_funding_redeemscript();
6825
6854
let funding_txo_script = funding_redeemscript.to_v0_p2wsh();
6826
6855
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());
@@ -6837,39 +6866,22 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6837
6866
6838
6867
channel_monitor.provide_initial_counterparty_commitment_tx(
6839
6868
counterparty_initial_commitment_tx.trust().txid(), Vec::new(),
6840
- self.context.cur_counterparty_commitment_transaction_number,
6869
+ self.context.cur_counterparty_commitment_transaction_number + 1 ,
6841
6870
self.context.counterparty_cur_commitment_point.unwrap(), self.context.feerate_per_kw,
6842
6871
counterparty_initial_commitment_tx.to_broadcaster_value_sat(),
6843
6872
counterparty_initial_commitment_tx.to_countersignatory_value_sat(), logger);
6844
6873
6845
- self.context.channel_state = ChannelState::FundingSent as u32;
6846
- self.context.channel_id = funding_txo.to_channel_id();
6847
- self.context.cur_counterparty_commitment_transaction_number -= 1;
6848
- self.context.cur_holder_commitment_transaction_number -= 1;
6849
-
6850
- log_info!(logger, "Generated funding_signed for peer for channel {}", &self.context.channel_id());
6874
+ log_info!(logger, "{} funding_signed for peer for channel {}",
6875
+ if funding_signed.is_some() { "Generated" } else { "Waiting for signature on" }, &self.context.channel_id());
6851
6876
6852
6877
// Promote the channel to a full-fledged one now that we have updated the state and have a
6853
6878
// `ChannelMonitor`.
6854
6879
let mut channel = Channel {
6855
6880
context: self.context,
6856
6881
};
6857
- let channel_id = channel.context.channel_id.clone();
6858
6882
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
6859
6883
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
6860
6884
6861
- let funding_signed = if let Some(signature) = sig_opt {
6862
- Some(msgs::FundingSigned {
6863
- channel_id,
6864
- signature,
6865
- #[cfg(taproot)]
6866
- partial_signature_with_nonce: None,
6867
- })
6868
- } else {
6869
- channel.context.signer_pending_funding = true;
6870
- None
6871
- };
6872
-
6873
6885
Ok((channel, funding_signed, channel_monitor))
6874
6886
}
6875
6887
}
0 commit comments