@@ -3243,10 +3243,14 @@ impl<SP: Deref> Channel<SP> where
3243
3243
*self.context.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
3244
3244
}
3245
3245
3246
- self.context.holder_signer.as_ecdsa().unwrap().validate_counterparty_revocation(
3247
- self.context.cur_counterparty_commitment_transaction_number + 1,
3248
- &secret
3249
- ).map_err(|_| ChannelError::Close("Failed to validate revocation from peer".to_owned()))?;
3246
+ match &self.context.holder_signer {
3247
+ ChannelSignerType::Ecdsa(ecdsa) => {
3248
+ ecdsa.validate_counterparty_revocation(
3249
+ self.context.cur_counterparty_commitment_transaction_number + 1,
3250
+ &secret
3251
+ ).map_err(|_| ChannelError::Close("Failed to validate revocation from peer".to_owned()))?;
3252
+ }
3253
+ };
3250
3254
3251
3255
self.context.commitment_secrets.provide_secret(self.context.cur_counterparty_commitment_transaction_number + 1, msg.per_commitment_secret)
3252
3256
.map_err(|_| ChannelError::Close("Previous secrets did not match new one".to_owned()))?;
@@ -4091,20 +4095,24 @@ impl<SP: Deref> Channel<SP> where
4091
4095
log_trace!(logger, "Proposing initial closing_signed for our counterparty with a fee range of {}-{} sat (with initial proposal {} sats)",
4092
4096
our_min_fee, our_max_fee, total_fee_satoshis);
4093
4097
4094
- let sig = self.context.holder_signer.as_ecdsa().unwrap()
4095
- .sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
4096
- .map_err(|()| ChannelError::Close("Failed to get signature for closing transaction.".to_owned()))?;
4098
+ match &self.context.holder_signer {
4099
+ ChannelSignerType::Ecdsa(ecdsa) => {
4100
+ let sig = ecdsa
4101
+ .sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
4102
+ .map_err(|()| ChannelError::Close("Failed to get signature for closing transaction.".to_owned()))?;
4097
4103
4098
- self.context.last_sent_closing_fee = Some((total_fee_satoshis, sig.clone()));
4099
- Ok((Some(msgs::ClosingSigned {
4100
- channel_id: self.context.channel_id,
4101
- fee_satoshis: total_fee_satoshis,
4102
- signature: sig,
4103
- fee_range: Some(msgs::ClosingSignedFeeRange {
4104
- min_fee_satoshis: our_min_fee,
4105
- max_fee_satoshis: our_max_fee,
4106
- }),
4107
- }), None))
4104
+ self.context.last_sent_closing_fee = Some((total_fee_satoshis, sig.clone()));
4105
+ Ok((Some(msgs::ClosingSigned {
4106
+ channel_id: self.context.channel_id,
4107
+ fee_satoshis: total_fee_satoshis,
4108
+ signature: sig,
4109
+ fee_range: Some(msgs::ClosingSignedFeeRange {
4110
+ min_fee_satoshis: our_min_fee,
4111
+ max_fee_satoshis: our_max_fee,
4112
+ }),
4113
+ }), None))
4114
+ }
4115
+ }
4108
4116
}
4109
4117
4110
4118
// Marks a channel as waiting for a response from the counterparty. If it's not received
@@ -4320,27 +4328,31 @@ impl<SP: Deref> Channel<SP> where
4320
4328
self.build_closing_transaction($new_fee, false)
4321
4329
};
4322
4330
4323
- let sig = self.context.holder_signer.as_ecdsa().unwrap()
4324
- .sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
4325
- .map_err(|_| ChannelError::Close("External signer refused to sign closing transaction".to_owned()))?;
4326
-
4327
- let signed_tx = if $new_fee == msg.fee_satoshis {
4328
- self.context.channel_state = ChannelState::ShutdownComplete as u32;
4329
- self.context.update_time_counter += 1;
4330
- let tx = self.build_signed_closing_transaction(&closing_tx, &msg.signature, &sig);
4331
- Some(tx)
4332
- } else { None };
4331
+ return match &self.context.holder_signer {
4332
+ ChannelSignerType::Ecdsa(ecdsa) => {
4333
+ let sig = ecdsa
4334
+ .sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
4335
+ .map_err(|_| ChannelError::Close("External signer refused to sign closing transaction".to_owned()))?;
4333
4336
4334
- self.context.last_sent_closing_fee = Some((used_fee, sig.clone()));
4335
- return Ok((Some(msgs::ClosingSigned {
4336
- channel_id: self.context.channel_id,
4337
- fee_satoshis: used_fee,
4338
- signature: sig,
4339
- fee_range: Some(msgs::ClosingSignedFeeRange {
4340
- min_fee_satoshis: our_min_fee,
4341
- max_fee_satoshis: our_max_fee,
4342
- }),
4343
- }), signed_tx))
4337
+ let signed_tx = if $new_fee == msg.fee_satoshis {
4338
+ self.context.channel_state = ChannelState::ShutdownComplete as u32;
4339
+ self.context.update_time_counter += 1;
4340
+ let tx = self.build_signed_closing_transaction(&closing_tx, &msg.signature, &sig);
4341
+ Some(tx)
4342
+ } else { None };
4343
+
4344
+ self.context.last_sent_closing_fee = Some((used_fee, sig.clone()));
4345
+ Ok((Some(msgs::ClosingSigned {
4346
+ channel_id: self.context.channel_id,
4347
+ fee_satoshis: used_fee,
4348
+ signature: sig,
4349
+ fee_range: Some(msgs::ClosingSignedFeeRange {
4350
+ min_fee_satoshis: our_min_fee,
4351
+ max_fee_satoshis: our_max_fee,
4352
+ }),
4353
+ }), signed_tx))
4354
+ }
4355
+ }
4344
4356
}
4345
4357
}
4346
4358
@@ -4930,26 +4942,30 @@ impl<SP: Deref> Channel<SP> where
4930
4942
},
4931
4943
Ok(v) => v
4932
4944
};
4933
- let our_bitcoin_sig = match self.context.holder_signer.as_ecdsa().unwrap().sign_channel_announcement_with_funding_key(&announcement, &self.context.secp_ctx) {
4934
- Err(_) => {
4935
- log_error!(logger, "Signer rejected channel_announcement signing. Channel will not be announced!");
4936
- return None;
4937
- },
4938
- Ok(v) => v
4939
- };
4940
- let short_channel_id = match self.context.get_short_channel_id() {
4941
- Some(scid) => scid,
4942
- None => return None,
4943
- };
4945
+ match &self.context.holder_signer {
4946
+ ChannelSignerType::Ecdsa(ecdsa) => {
4947
+ let our_bitcoin_sig = match ecdsa.sign_channel_announcement_with_funding_key(&announcement, &self.context.secp_ctx) {
4948
+ Err(_) => {
4949
+ log_error!(logger, "Signer rejected channel_announcement signing. Channel will not be announced!");
4950
+ return None;
4951
+ },
4952
+ Ok(v) => v
4953
+ };
4954
+ let short_channel_id = match self.context.get_short_channel_id() {
4955
+ Some(scid) => scid,
4956
+ None => return None,
4957
+ };
4944
4958
4945
- self.context.announcement_sigs_state = AnnouncementSigsState::MessageSent;
4959
+ self.context.announcement_sigs_state = AnnouncementSigsState::MessageSent;
4946
4960
4947
- Some(msgs::AnnouncementSignatures {
4948
- channel_id: self.context.channel_id(),
4949
- short_channel_id,
4950
- node_signature: our_node_sig,
4951
- bitcoin_signature: our_bitcoin_sig,
4952
- })
4961
+ Some(msgs::AnnouncementSignatures {
4962
+ channel_id: self.context.channel_id(),
4963
+ short_channel_id,
4964
+ node_signature: our_node_sig,
4965
+ bitcoin_signature: our_bitcoin_sig,
4966
+ })
4967
+ }
4968
+ }
4953
4969
}
4954
4970
4955
4971
/// Signs the given channel announcement, returning a ChannelError::Ignore if no keys are
@@ -4964,15 +4980,19 @@ impl<SP: Deref> Channel<SP> where
4964
4980
4965
4981
let our_node_sig = node_signer.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelAnnouncement(&announcement))
4966
4982
.map_err(|_| ChannelError::Ignore("Failed to generate node signature for channel_announcement".to_owned()))?;
4967
- let our_bitcoin_sig = self.context.holder_signer.as_ecdsa().unwrap().sign_channel_announcement_with_funding_key(&announcement, &self.context.secp_ctx)
4968
- .map_err(|_| ChannelError::Ignore("Signer rejected channel_announcement".to_owned()))?;
4969
- Ok(msgs::ChannelAnnouncement {
4970
- node_signature_1: if were_node_one { our_node_sig } else { their_node_sig },
4971
- node_signature_2: if were_node_one { their_node_sig } else { our_node_sig },
4972
- bitcoin_signature_1: if were_node_one { our_bitcoin_sig } else { their_bitcoin_sig },
4973
- bitcoin_signature_2: if were_node_one { their_bitcoin_sig } else { our_bitcoin_sig },
4974
- contents: announcement,
4975
- })
4983
+ match &self.context.holder_signer {
4984
+ ChannelSignerType::Ecdsa(ecdsa) => {
4985
+ let our_bitcoin_sig = ecdsa.sign_channel_announcement_with_funding_key(&announcement, &self.context.secp_ctx)
4986
+ .map_err(|_| ChannelError::Ignore("Signer rejected channel_announcement".to_owned()))?;
4987
+ Ok(msgs::ChannelAnnouncement {
4988
+ node_signature_1: if were_node_one { our_node_sig } else { their_node_sig },
4989
+ node_signature_2: if were_node_one { their_node_sig } else { our_node_sig },
4990
+ bitcoin_signature_1: if were_node_one { our_bitcoin_sig } else { their_bitcoin_sig },
4991
+ bitcoin_signature_2: if were_node_one { their_bitcoin_sig } else { our_bitcoin_sig },
4992
+ contents: announcement,
4993
+ })
4994
+ }
4995
+ }
4976
4996
} else {
4977
4997
Err(ChannelError::Ignore("Attempted to sign channel announcement before we'd received announcement_signatures".to_string()))
4978
4998
}
@@ -5298,40 +5318,45 @@ impl<SP: Deref> Channel<SP> where
5298
5318
let counterparty_keys = self.context.build_remote_transaction_keys();
5299
5319
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
5300
5320
let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
5301
- let (signature, htlc_signatures);
5302
5321
5303
- {
5304
- let mut htlcs = Vec::with_capacity(commitment_stats.htlcs_included.len());
5305
- for &(ref htlc, _) in commitment_stats.htlcs_included.iter() {
5306
- htlcs.push(htlc);
5307
- }
5322
+ match &self.context.holder_signer {
5323
+ ChannelSignerType::Ecdsa(ecdsa) => {
5324
+ let (signature, htlc_signatures);
5308
5325
5309
- let res = self.context.holder_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&commitment_stats.tx, commitment_stats.preimages, &self.context.secp_ctx)
5310
- .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?;
5311
- signature = res.0;
5312
- htlc_signatures = res.1;
5326
+ {
5327
+ let mut htlcs = Vec::with_capacity(commitment_stats.htlcs_included.len());
5328
+ for &(ref htlc, _) in commitment_stats.htlcs_included.iter() {
5329
+ htlcs.push(htlc);
5330
+ }
5313
5331
5314
- log_trace!(logger, "Signed remote commitment tx {} (txid {}) with redeemscript {} -> {} in channel {}",
5315
- encode::serialize_hex(&commitment_stats.tx.trust().built_transaction().transaction),
5316
- &counterparty_commitment_txid, encode::serialize_hex(&self.context.get_funding_redeemscript()),
5317
- log_bytes!(signature.serialize_compact()[..]), log_bytes!(self.context.channel_id()));
5332
+ let res = ecdsa.sign_counterparty_commitment(&commitment_stats.tx, commitment_stats.preimages, &self.context.secp_ctx)
5333
+ .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?;
5334
+ signature = res.0;
5335
+ htlc_signatures = res.1;
5336
+
5337
+ log_trace!(logger, "Signed remote commitment tx {} (txid {}) with redeemscript {} -> {} in channel {}",
5338
+ encode::serialize_hex(&commitment_stats.tx.trust().built_transaction().transaction),
5339
+ &counterparty_commitment_txid, encode::serialize_hex(&self.context.get_funding_redeemscript()),
5340
+ log_bytes!(signature.serialize_compact()[..]), log_bytes!(self.context.channel_id()));
5341
+
5342
+ for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
5343
+ log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
5344
+ encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.feerate_per_kw, self.context.get_holder_selected_contest_delay(), htlc, &self.context.channel_type, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
5345
+ encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
5346
+ log_bytes!(counterparty_keys.broadcaster_htlc_key.serialize()),
5347
+ log_bytes!(htlc_sig.serialize_compact()[..]), log_bytes!(self.context.channel_id()));
5348
+ }
5349
+ }
5318
5350
5319
- for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
5320
- log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
5321
- encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.feerate_per_kw, self.context.get_holder_selected_contest_delay(), htlc, &self.context.channel_type, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
5322
- encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
5323
- log_bytes!(counterparty_keys.broadcaster_htlc_key.serialize()),
5324
- log_bytes!(htlc_sig.serialize_compact()[..]), log_bytes!(self.context.channel_id()));
5351
+ Ok((msgs::CommitmentSigned {
5352
+ channel_id: self.context.channel_id,
5353
+ signature,
5354
+ htlc_signatures,
5355
+ #[cfg(taproot)]
5356
+ partial_signature_with_nonce: None,
5357
+ }, (counterparty_commitment_txid, commitment_stats.htlcs_included)))
5325
5358
}
5326
5359
}
5327
-
5328
- Ok((msgs::CommitmentSigned {
5329
- channel_id: self.context.channel_id,
5330
- signature,
5331
- htlc_signatures,
5332
- #[cfg(taproot)]
5333
- partial_signature_with_nonce: None,
5334
- }, (counterparty_commitment_txid, commitment_stats.htlcs_included)))
5335
5360
}
5336
5361
5337
5362
/// Adds a pending outbound HTLC to this channel, and builds a new remote commitment
@@ -5701,8 +5726,13 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
5701
5726
fn get_funding_created_signature<L: Deref>(&mut self, logger: &L) -> Result<Signature, ChannelError> where L::Target: Logger {
5702
5727
let counterparty_keys = self.context.build_remote_transaction_keys();
5703
5728
let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
5704
- Ok(self.context.holder_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.context.secp_ctx)
5705
- .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0)
5729
+ match &self.context.holder_signer {
5730
+ // TODO (arik): move match into calling method for Taproot
5731
+ ChannelSignerType::Ecdsa(ecdsa) => {
5732
+ Ok(ecdsa.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.context.secp_ctx)
5733
+ .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0)
5734
+ }
5735
+ }
5706
5736
}
5707
5737
5708
5738
/// Updates channel state with knowledge of the funding transaction's txid/index, and generates
@@ -6422,11 +6452,16 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6422
6452
log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
6423
6453
log_bytes!(self.context.channel_id()), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
6424
6454
6425
- let counterparty_signature = self.context.holder_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.context.secp_ctx)
6426
- .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0;
6455
+ match &self.context.holder_signer {
6456
+ // TODO (arik): move match into calling method for Taproot
6457
+ ChannelSignerType::Ecdsa(ecdsa) => {
6458
+ let counterparty_signature = ecdsa.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.context.secp_ctx)
6459
+ .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0;
6427
6460
6428
- // We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
6429
- Ok((counterparty_initial_bitcoin_tx.txid, initial_commitment_tx, counterparty_signature))
6461
+ // We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
6462
+ Ok((counterparty_initial_bitcoin_tx.txid, initial_commitment_tx, counterparty_signature))
6463
+ }
6464
+ }
6430
6465
}
6431
6466
6432
6467
pub fn funding_created<L: Deref>(
@@ -6606,7 +6641,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
6606
6641
self.context.latest_monitor_update_id.write(writer)?;
6607
6642
6608
6643
let mut key_data = VecWriter(Vec::new());
6609
- self.context.holder_signer.as_ecdsa().unwrap().write(&mut key_data)?;
6644
+ // TODO: Introduce serialization distinction for non-ECDSA signers.
6645
+ self.context.holder_signer.as_ecdsa().expect("Only ECDSA signers may be serialized").write(&mut key_data)?;
6610
6646
assert!(key_data.0.len() < core::usize::MAX);
6611
6647
assert!(key_data.0.len() < core::u32::MAX as usize);
6612
6648
(key_data.0.len() as u32).write(writer)?;
0 commit comments