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