@@ -28,7 +28,7 @@ use bitcoin::secp256k1;
28
28
use crate::ln::types::{ChannelId, PaymentPreimage, PaymentHash};
29
29
use crate::ln::features::{ChannelTypeFeatures, InitFeatures};
30
30
use crate::ln::msgs;
31
- use crate::ln::msgs::DecodeError;
31
+ use crate::ln::msgs::{ClosingSigned, ClosingSignedFeeRange, DecodeError} ;
32
32
use crate::ln::script::{self, ShutdownScript};
33
33
use crate::ln::channel_state::{ChannelShutdownState, CounterpartyForwardingInfo, InboundHTLCDetails, InboundHTLCStateDetails, OutboundHTLCDetails, OutboundHTLCStateDetails};
34
34
use crate::ln::channelmanager::{self, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
@@ -5933,7 +5933,7 @@ impl<SP: Deref> Channel<SP> where
5933
5933
5934
5934
if !self.context.is_outbound() {
5935
5935
if let Some(msg) = &self.context.pending_counterparty_closing_signed.take() {
5936
- return self.closing_signed(fee_estimator, &msg);
5936
+ return self.closing_signed(fee_estimator, &msg, logger );
5937
5937
}
5938
5938
return Ok((None, None, None));
5939
5939
}
@@ -5951,27 +5951,11 @@ impl<SP: Deref> Channel<SP> where
5951
5951
log_trace!(logger, "Proposing initial closing_signed for our counterparty with a fee range of {}-{} sat (with initial proposal {} sats)",
5952
5952
our_min_fee, our_max_fee, total_fee_satoshis);
5953
5953
5954
- match &self.context.holder_signer {
5955
- ChannelSignerType::Ecdsa(ecdsa) => {
5956
- let sig = ecdsa
5957
- .sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
5958
- .map_err(|()| ChannelError::close("Failed to get signature for closing transaction.".to_owned()))?;
5959
-
5960
- self.context.last_sent_closing_fee = Some((total_fee_satoshis, sig.clone()));
5961
- Ok((Some(msgs::ClosingSigned {
5962
- channel_id: self.context.channel_id,
5963
- fee_satoshis: total_fee_satoshis,
5964
- signature: sig,
5965
- fee_range: Some(msgs::ClosingSignedFeeRange {
5966
- min_fee_satoshis: our_min_fee,
5967
- max_fee_satoshis: our_max_fee,
5968
- }),
5969
- }), None, None))
5970
- },
5971
- // TODO (taproot|arik)
5972
- #[cfg(taproot)]
5973
- _ => todo!()
5954
+ let closing_signed = self.get_closing_signed_msg(&closing_tx, false, total_fee_satoshis, our_min_fee, our_max_fee, logger);
5955
+ if closing_signed.is_none() {
5956
+ return Err(ChannelError::close("Failed to get signature for closing transaction.".to_owned()));
5974
5957
}
5958
+ Ok((closing_signed, None, None))
5975
5959
}
5976
5960
5977
5961
// Marks a channel as waiting for a response from the counterparty. If it's not received
@@ -6118,10 +6102,38 @@ impl<SP: Deref> Channel<SP> where
6118
6102
tx
6119
6103
}
6120
6104
6121
- pub fn closing_signed<F: Deref>(
6122
- &mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, msg: &msgs::ClosingSigned)
6105
+ fn get_closing_signed_msg<L: Deref>(
6106
+ &mut self, closing_tx: &ClosingTransaction, skip_remote_output: bool,
6107
+ fee_satoshis: u64, min_fee_satoshis: u64, max_fee_satoshis: u64, logger: &L
6108
+ ) -> Option<msgs::ClosingSigned>
6109
+ where L::Target: Logger
6110
+ {
6111
+ match &self.context.holder_signer {
6112
+ ChannelSignerType::Ecdsa(ecdsa) => {
6113
+ let fee_range = msgs::ClosingSignedFeeRange {
6114
+ min_fee_satoshis,
6115
+ max_fee_satoshis,
6116
+ };
6117
+ let sig = ecdsa.sign_closing_transaction(closing_tx, &self.context.secp_ctx).ok()?;
6118
+
6119
+ self.context.last_sent_closing_fee = Some((fee_satoshis, sig.clone()));
6120
+ Some(msgs::ClosingSigned {
6121
+ channel_id: self.context.channel_id,
6122
+ fee_satoshis,
6123
+ signature: sig,
6124
+ fee_range: Some(fee_range),
6125
+ })
6126
+ },
6127
+ // TODO (taproot|arik)
6128
+ #[cfg(taproot)]
6129
+ _ => todo!()
6130
+ }
6131
+ }
6132
+
6133
+ pub fn closing_signed<F: Deref, L: Deref>(
6134
+ &mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, msg: &msgs::ClosingSigned, logger: &L)
6123
6135
-> Result<(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>), ChannelError>
6124
- where F::Target: FeeEstimator
6136
+ where F::Target: FeeEstimator, L::Target: Logger
6125
6137
{
6126
6138
if !self.context.channel_state.is_both_sides_shutdown() {
6127
6139
return Err(ChannelError::close("Remote end sent us a closing_signed before both sides provided a shutdown".to_owned()));
@@ -6146,7 +6158,8 @@ impl<SP: Deref> Channel<SP> where
6146
6158
}
6147
6159
6148
6160
let funding_redeemscript = self.context.get_funding_redeemscript();
6149
- let (mut closing_tx, used_total_fee) = self.build_closing_transaction(msg.fee_satoshis, false);
6161
+ let mut skip_remote_output = false;
6162
+ let (mut closing_tx, used_total_fee) = self.build_closing_transaction(msg.fee_satoshis, skip_remote_output);
6150
6163
if used_total_fee != msg.fee_satoshis {
6151
6164
return Err(ChannelError::close(format!("Remote sent us a closing_signed with a fee other than the value they can claim. Fee in message: {}. Actual closing tx fee: {}", msg.fee_satoshis, used_total_fee)));
6152
6165
}
@@ -6157,7 +6170,8 @@ impl<SP: Deref> Channel<SP> where
6157
6170
Err(_e) => {
6158
6171
// The remote end may have decided to revoke their output due to inconsistent dust
6159
6172
// limits, so check for that case by re-checking the signature here.
6160
- closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
6173
+ skip_remote_output = true;
6174
+ closing_tx = self.build_closing_transaction(msg.fee_satoshis, skip_remote_output).0;
6161
6175
let sighash = closing_tx.trust().get_sighash_all(&funding_redeemscript, self.context.channel_value_satoshis);
6162
6176
secp_check!(self.context.secp_ctx.verify_ecdsa(&sighash, &msg.signature, self.context.counterparty_funding_pubkey()), "Invalid closing tx signature from peer".to_owned());
6163
6177
},
@@ -6204,50 +6218,36 @@ impl<SP: Deref> Channel<SP> where
6204
6218
let (closing_tx, used_fee) = if $new_fee == msg.fee_satoshis {
6205
6219
(closing_tx, $new_fee)
6206
6220
} else {
6207
- self.build_closing_transaction($new_fee, false)
6221
+ skip_remote_output = false;
6222
+ self.build_closing_transaction($new_fee, skip_remote_output)
6208
6223
};
6209
6224
6210
- return match &self.context.holder_signer {
6211
- ChannelSignerType::Ecdsa(ecdsa) => {
6212
- let sig = ecdsa
6213
- .sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
6214
- .map_err(|_| ChannelError::close("External signer refused to sign closing transaction".to_owned()))?;
6215
- let (signed_tx, shutdown_result) = if $new_fee == msg.fee_satoshis {
6216
- let shutdown_result = ShutdownResult {
6217
- closure_reason,
6218
- monitor_update: None,
6219
- dropped_outbound_htlcs: Vec::new(),
6220
- unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(),
6221
- channel_id: self.context.channel_id,
6222
- user_channel_id: self.context.user_id,
6223
- channel_capacity_satoshis: self.context.channel_value_satoshis,
6224
- counterparty_node_id: self.context.counterparty_node_id,
6225
- unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
6226
- channel_funding_txo: self.context.get_funding_txo(),
6227
- };
6228
- self.context.channel_state = ChannelState::ShutdownComplete;
6229
- self.context.update_time_counter += 1;
6230
- let tx = self.build_signed_closing_transaction(&closing_tx, &msg.signature, &sig);
6231
- (Some(tx), Some(shutdown_result))
6232
- } else {
6233
- (None, None)
6234
- };
6235
-
6236
- self.context.last_sent_closing_fee = Some((used_fee, sig.clone()));
6237
- Ok((Some(msgs::ClosingSigned {
6238
- channel_id: self.context.channel_id,
6239
- fee_satoshis: used_fee,
6240
- signature: sig,
6241
- fee_range: Some(msgs::ClosingSignedFeeRange {
6242
- min_fee_satoshis: our_min_fee,
6243
- max_fee_satoshis: our_max_fee,
6244
- }),
6245
- }), signed_tx, shutdown_result))
6246
- },
6247
- // TODO (taproot|arik)
6248
- #[cfg(taproot)]
6249
- _ => todo!()
6225
+ let closing_signed = self.get_closing_signed_msg(&closing_tx, skip_remote_output, used_fee, our_min_fee, our_max_fee, logger);
6226
+ if closing_signed.is_none() {
6227
+ return Err(ChannelError::close("Failed to get signature for closing transaction.".to_owned()));
6250
6228
}
6229
+ let (signed_tx, shutdown_result) = if $new_fee == msg.fee_satoshis {
6230
+ let shutdown_result = ShutdownResult {
6231
+ closure_reason,
6232
+ monitor_update: None,
6233
+ dropped_outbound_htlcs: Vec::new(),
6234
+ unbroadcasted_batch_funding_txid: self.context.unbroadcasted_batch_funding_txid(),
6235
+ channel_id: self.context.channel_id,
6236
+ user_channel_id: self.context.user_id,
6237
+ channel_capacity_satoshis: self.context.channel_value_satoshis,
6238
+ counterparty_node_id: self.context.counterparty_node_id,
6239
+ unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
6240
+ channel_funding_txo: self.context.get_funding_txo(),
6241
+ };
6242
+ self.context.channel_state = ChannelState::ShutdownComplete;
6243
+ self.context.update_time_counter += 1;
6244
+ let tx = closing_signed.as_ref().map(|ClosingSigned { signature, .. }|
6245
+ self.build_signed_closing_transaction(&closing_tx, &msg.signature, signature));
6246
+ (tx, Some(shutdown_result))
6247
+ } else {
6248
+ (None, None)
6249
+ };
6250
+ return Ok((closing_signed, signed_tx, shutdown_result))
6251
6251
}
6252
6252
}
6253
6253
0 commit comments