@@ -2110,6 +2110,36 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
2110
2110
next_local_nonce: None,
2111
2111
})
2112
2112
}
2113
+
2114
+ /// Only allowed after [`Self::channel_transaction_parameters`] is set.
2115
+ fn get_funding_signed_msg<L: Deref>(&mut self, logger: &L) -> (CommitmentTransaction, Option<msgs::FundingSigned>) where L::Target: Logger {
2116
+ let counterparty_keys = self.build_remote_transaction_keys();
2117
+ let counterparty_initial_commitment_tx = self.build_commitment_transaction(self.cur_counterparty_commitment_transaction_number + 1, &counterparty_keys, false, false, logger).tx;
2118
+
2119
+ let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
2120
+ let counterparty_initial_bitcoin_tx = counterparty_trusted_tx.built_transaction();
2121
+ log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
2122
+ &self.channel_id(), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
2123
+
2124
+ match &self.holder_signer {
2125
+ // TODO (arik): move match into calling method for Taproot
2126
+ ChannelSignerType::Ecdsa(ecdsa) => {
2127
+ let funding_signed = ecdsa.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.secp_ctx)
2128
+ .map(|(signature, _)| msgs::FundingSigned {
2129
+ channel_id: self.channel_id(),
2130
+ signature,
2131
+ #[cfg(taproot)]
2132
+ partial_signature_with_nonce: None,
2133
+ })
2134
+ .ok();
2135
+ self.signer_pending_funding = funding_signed.is_none();
2136
+
2137
+ // We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
2138
+ (counterparty_initial_commitment_tx, funding_signed)
2139
+ }
2140
+ }
2141
+ }
2142
+
2113
2143
}
2114
2144
2115
2145
// Internal utility functions for channels
@@ -3935,7 +3965,9 @@ impl<SP: Deref> Channel<SP> where
3935
3965
let commitment_update = if self.context.signer_pending_commitment_update {
3936
3966
self.get_last_commitment_update_for_send(logger).ok()
3937
3967
} else { None };
3938
- let funding_signed = None;
3968
+ let funding_signed = if self.context.signer_pending_funding && !self.context.is_outbound() {
3969
+ self.context.get_funding_signed_msg(logger).1
3970
+ } else { None };
3939
3971
let funding_created = if self.context.signer_pending_funding && self.context.is_outbound() {
3940
3972
self.context.get_funding_created_msg(logger)
3941
3973
} else { None };
@@ -6661,41 +6693,22 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6661
6693
self.generate_accept_channel_message()
6662
6694
}
6663
6695
6664
- fn funding_created_signature <L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<( CommitmentTransaction, CommitmentTransaction, Option<Signature>) , ChannelError> where L::Target: Logger {
6696
+ fn check_funding_created_signature <L: Deref>(&mut self, sig: &Signature, logger: &L) -> Result<CommitmentTransaction, ChannelError> where L::Target: Logger {
6665
6697
let funding_script = self.context.get_funding_redeemscript();
6666
6698
6667
6699
let keys = self.context.build_holder_transaction_keys(self.context.cur_holder_commitment_transaction_number);
6668
6700
let initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_holder_commitment_transaction_number, &keys, true, false, logger).tx;
6669
- {
6670
- let trusted_tx = initial_commitment_tx.trust();
6671
- let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
6672
- let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.context.channel_value_satoshis);
6673
- // They sign the holder commitment transaction...
6674
- log_trace!(logger, "Checking funding_created tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} for channel {}.",
6675
- log_bytes!(sig.serialize_compact()[..]), log_bytes!(self.context.counterparty_funding_pubkey().serialize()),
6676
- encode::serialize_hex(&initial_commitment_bitcoin_tx.transaction), log_bytes!(sighash[..]),
6677
- encode::serialize_hex(&funding_script), &self.context.channel_id());
6678
- secp_check!(self.context.secp_ctx.verify_ecdsa(&sighash, &sig, self.context.counterparty_funding_pubkey()), "Invalid funding_created signature from peer".to_owned());
6679
- }
6680
-
6681
- let counterparty_keys = self.context.build_remote_transaction_keys();
6682
- let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
6683
-
6684
- let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
6685
- let counterparty_initial_bitcoin_tx = counterparty_trusted_tx.built_transaction();
6686
- log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
6687
- &self.context.channel_id(), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
6688
-
6689
- match &self.context.holder_signer {
6690
- // TODO (arik): move match into calling method for Taproot
6691
- ChannelSignerType::Ecdsa(ecdsa) => {
6692
- let counterparty_signature = ecdsa.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.context.secp_ctx)
6693
- .map(|(sig, _)| sig).ok();
6701
+ let trusted_tx = initial_commitment_tx.trust();
6702
+ let initial_commitment_bitcoin_tx = trusted_tx.built_transaction();
6703
+ let sighash = initial_commitment_bitcoin_tx.get_sighash_all(&funding_script, self.context.channel_value_satoshis);
6704
+ // They sign the holder commitment transaction...
6705
+ log_trace!(logger, "Checking funding_created tx signature {} by key {} against tx {} (sighash {}) with redeemscript {} for channel {}.",
6706
+ log_bytes!(sig.serialize_compact()[..]), log_bytes!(self.context.counterparty_funding_pubkey().serialize()),
6707
+ encode::serialize_hex(&initial_commitment_bitcoin_tx.transaction), log_bytes!(sighash[..]),
6708
+ encode::serialize_hex(&funding_script), &self.context.channel_id());
6709
+ secp_check!(self.context.secp_ctx.verify_ecdsa(&sighash, &sig, self.context.counterparty_funding_pubkey()), "Invalid funding_created signature from peer".to_owned());
6694
6710
6695
- // We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
6696
- Ok((counterparty_initial_commitment_tx, initial_commitment_tx, counterparty_signature))
6697
- }
6698
- }
6711
+ Ok(initial_commitment_tx)
6699
6712
}
6700
6713
6701
6714
pub fn funding_created<L: Deref>(
@@ -6722,10 +6735,10 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6722
6735
let funding_txo = OutPoint { txid: msg.funding_txid, index: msg.funding_output_index };
6723
6736
self.context.channel_transaction_parameters.funding_outpoint = Some(funding_txo);
6724
6737
// This is an externally observable change before we finish all our checks. In particular
6725
- // funding_created_signature may fail.
6738
+ // check_funding_created_signature may fail.
6726
6739
self.context.holder_signer.as_mut().provide_channel_parameters(&self.context.channel_transaction_parameters);
6727
6740
6728
- let (counterparty_initial_commitment_tx, initial_commitment_tx, sig_opt) = match self.funding_created_signature (&msg.signature, logger) {
6741
+ let initial_commitment_tx = match self.check_funding_created_signature (&msg.signature, logger) {
6729
6742
Ok(res) => res,
6730
6743
Err(ChannelError::Close(e)) => {
6731
6744
self.context.channel_transaction_parameters.funding_outpoint = None;
@@ -6734,7 +6747,7 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6734
6747
Err(e) => {
6735
6748
// The only error we know how to handle is ChannelError::Close, so we fall over here
6736
6749
// to make sure we don't continue with an inconsistent state.
6737
- panic!("unexpected error type from funding_created_signature {:?}", e);
6750
+ panic!("unexpected error type from check_funding_created_signature {:?}", e);
6738
6751
}
6739
6752
};
6740
6753
@@ -6750,6 +6763,13 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6750
6763
return Err((self, ChannelError::Close("Failed to validate our commitment".to_owned())));
6751
6764
}
6752
6765
6766
+ self.context.channel_state = ChannelState::FundingSent as u32;
6767
+ self.context.channel_id = funding_txo.to_channel_id();
6768
+ self.context.cur_counterparty_commitment_transaction_number -= 1;
6769
+ self.context.cur_holder_commitment_transaction_number -= 1;
6770
+
6771
+ let (counterparty_initial_commitment_tx, funding_signed) = self.context.get_funding_signed_msg(logger);
6772
+
6753
6773
// Now that we're past error-generating stuff, update our local state:
6754
6774
6755
6775
let funding_redeemscript = self.context.get_funding_redeemscript();
@@ -6768,16 +6788,11 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6768
6788
6769
6789
channel_monitor.provide_initial_counterparty_commitment_tx(
6770
6790
counterparty_initial_commitment_tx.trust().txid(), Vec::new(),
6771
- self.context.cur_counterparty_commitment_transaction_number,
6791
+ self.context.cur_counterparty_commitment_transaction_number + 1 ,
6772
6792
self.context.counterparty_cur_commitment_point.unwrap(), self.context.feerate_per_kw,
6773
6793
counterparty_initial_commitment_tx.to_broadcaster_value_sat(),
6774
6794
counterparty_initial_commitment_tx.to_countersignatory_value_sat(), logger);
6775
6795
6776
- self.context.channel_state = ChannelState::FundingSent as u32;
6777
- self.context.channel_id = funding_txo.to_channel_id();
6778
- self.context.cur_counterparty_commitment_transaction_number -= 1;
6779
- self.context.cur_holder_commitment_transaction_number -= 1;
6780
-
6781
6796
log_info!(logger, "Generated funding_signed for peer for channel {}", &self.context.channel_id());
6782
6797
6783
6798
// Promote the channel to a full-fledged one now that we have updated the state and have a
@@ -6789,18 +6804,6 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6789
6804
let need_channel_ready = channel.check_get_channel_ready(0).is_some();
6790
6805
channel.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
6791
6806
6792
- let funding_signed = if let Some(signature) = sig_opt {
6793
- Some(msgs::FundingSigned {
6794
- channel_id,
6795
- signature,
6796
- #[cfg(taproot)]
6797
- partial_signature_with_nonce: None,
6798
- })
6799
- } else {
6800
- channel.context.signer_pending_funding = true;
6801
- None
6802
- };
6803
-
6804
6807
Ok((channel, funding_signed, channel_monitor))
6805
6808
}
6806
6809
}
0 commit comments