@@ -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()))?;
@@ -4115,20 +4119,24 @@ impl<SP: Deref> Channel<SP> where
4115
4119
log_trace!(logger, "Proposing initial closing_signed for our counterparty with a fee range of {}-{} sat (with initial proposal {} sats)",
4116
4120
our_min_fee, our_max_fee, total_fee_satoshis);
4117
4121
4118
- let sig = self.context.holder_signer.as_ecdsa().unwrap()
4119
- .sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
4120
- .map_err(|()| ChannelError::Close("Failed to get signature for closing transaction.".to_owned()))?;
4122
+ match &self.context.holder_signer {
4123
+ ChannelSignerType::Ecdsa(ecdsa) => {
4124
+ let sig = ecdsa
4125
+ .sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
4126
+ .map_err(|()| ChannelError::Close("Failed to get signature for closing transaction.".to_owned()))?;
4121
4127
4122
- self.context.last_sent_closing_fee = Some((total_fee_satoshis, sig.clone()));
4123
- Ok((Some(msgs::ClosingSigned {
4124
- channel_id: self.context.channel_id,
4125
- fee_satoshis: total_fee_satoshis,
4126
- signature: sig,
4127
- fee_range: Some(msgs::ClosingSignedFeeRange {
4128
- min_fee_satoshis: our_min_fee,
4129
- max_fee_satoshis: our_max_fee,
4130
- }),
4131
- }), None))
4128
+ self.context.last_sent_closing_fee = Some((total_fee_satoshis, sig.clone()));
4129
+ Ok((Some(msgs::ClosingSigned {
4130
+ channel_id: self.context.channel_id,
4131
+ fee_satoshis: total_fee_satoshis,
4132
+ signature: sig,
4133
+ fee_range: Some(msgs::ClosingSignedFeeRange {
4134
+ min_fee_satoshis: our_min_fee,
4135
+ max_fee_satoshis: our_max_fee,
4136
+ }),
4137
+ }), None))
4138
+ }
4139
+ }
4132
4140
}
4133
4141
4134
4142
// Marks a channel as waiting for a response from the counterparty. If it's not received
@@ -4344,27 +4352,31 @@ impl<SP: Deref> Channel<SP> where
4344
4352
self.build_closing_transaction($new_fee, false)
4345
4353
};
4346
4354
4347
- let sig = self.context.holder_signer.as_ecdsa().unwrap()
4348
- .sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
4349
- .map_err(|_| ChannelError::Close("External signer refused to sign closing transaction".to_owned()))?;
4350
-
4351
- let signed_tx = if $new_fee == msg.fee_satoshis {
4352
- self.context.channel_state = ChannelState::ShutdownComplete as u32;
4353
- self.context.update_time_counter += 1;
4354
- let tx = self.build_signed_closing_transaction(&closing_tx, &msg.signature, &sig);
4355
- Some(tx)
4356
- } else { None };
4355
+ return match &self.context.holder_signer {
4356
+ ChannelSignerType::Ecdsa(ecdsa) => {
4357
+ let sig = ecdsa
4358
+ .sign_closing_transaction(&closing_tx, &self.context.secp_ctx)
4359
+ .map_err(|_| ChannelError::Close("External signer refused to sign closing transaction".to_owned()))?;
4357
4360
4358
- self.context.last_sent_closing_fee = Some((used_fee, sig.clone()));
4359
- return Ok((Some(msgs::ClosingSigned {
4360
- channel_id: self.context.channel_id,
4361
- fee_satoshis: used_fee,
4362
- signature: sig,
4363
- fee_range: Some(msgs::ClosingSignedFeeRange {
4364
- min_fee_satoshis: our_min_fee,
4365
- max_fee_satoshis: our_max_fee,
4366
- }),
4367
- }), signed_tx))
4361
+ let signed_tx = if $new_fee == msg.fee_satoshis {
4362
+ self.context.channel_state = ChannelState::ShutdownComplete as u32;
4363
+ self.context.update_time_counter += 1;
4364
+ let tx = self.build_signed_closing_transaction(&closing_tx, &msg.signature, &sig);
4365
+ Some(tx)
4366
+ } else { None };
4367
+
4368
+ self.context.last_sent_closing_fee = Some((used_fee, sig.clone()));
4369
+ Ok((Some(msgs::ClosingSigned {
4370
+ channel_id: self.context.channel_id,
4371
+ fee_satoshis: used_fee,
4372
+ signature: sig,
4373
+ fee_range: Some(msgs::ClosingSignedFeeRange {
4374
+ min_fee_satoshis: our_min_fee,
4375
+ max_fee_satoshis: our_max_fee,
4376
+ }),
4377
+ }), signed_tx))
4378
+ }
4379
+ }
4368
4380
}
4369
4381
}
4370
4382
@@ -4954,26 +4966,30 @@ impl<SP: Deref> Channel<SP> where
4954
4966
},
4955
4967
Ok(v) => v
4956
4968
};
4957
- let our_bitcoin_sig = match self.context.holder_signer.as_ecdsa().unwrap().sign_channel_announcement_with_funding_key(&announcement, &self.context.secp_ctx) {
4958
- Err(_) => {
4959
- log_error!(logger, "Signer rejected channel_announcement signing. Channel will not be announced!");
4960
- return None;
4961
- },
4962
- Ok(v) => v
4963
- };
4964
- let short_channel_id = match self.context.get_short_channel_id() {
4965
- Some(scid) => scid,
4966
- None => return None,
4967
- };
4969
+ match &self.context.holder_signer {
4970
+ ChannelSignerType::Ecdsa(ecdsa) => {
4971
+ let our_bitcoin_sig = match ecdsa.sign_channel_announcement_with_funding_key(&announcement, &self.context.secp_ctx) {
4972
+ Err(_) => {
4973
+ log_error!(logger, "Signer rejected channel_announcement signing. Channel will not be announced!");
4974
+ return None;
4975
+ },
4976
+ Ok(v) => v
4977
+ };
4978
+ let short_channel_id = match self.context.get_short_channel_id() {
4979
+ Some(scid) => scid,
4980
+ None => return None,
4981
+ };
4968
4982
4969
- self.context.announcement_sigs_state = AnnouncementSigsState::MessageSent;
4983
+ self.context.announcement_sigs_state = AnnouncementSigsState::MessageSent;
4970
4984
4971
- Some(msgs::AnnouncementSignatures {
4972
- channel_id: self.context.channel_id(),
4973
- short_channel_id,
4974
- node_signature: our_node_sig,
4975
- bitcoin_signature: our_bitcoin_sig,
4976
- })
4985
+ Some(msgs::AnnouncementSignatures {
4986
+ channel_id: self.context.channel_id(),
4987
+ short_channel_id,
4988
+ node_signature: our_node_sig,
4989
+ bitcoin_signature: our_bitcoin_sig,
4990
+ })
4991
+ }
4992
+ }
4977
4993
}
4978
4994
4979
4995
/// Signs the given channel announcement, returning a ChannelError::Ignore if no keys are
@@ -4988,15 +5004,19 @@ impl<SP: Deref> Channel<SP> where
4988
5004
4989
5005
let our_node_sig = node_signer.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelAnnouncement(&announcement))
4990
5006
.map_err(|_| ChannelError::Ignore("Failed to generate node signature for channel_announcement".to_owned()))?;
4991
- let our_bitcoin_sig = self.context.holder_signer.as_ecdsa().unwrap().sign_channel_announcement_with_funding_key(&announcement, &self.context.secp_ctx)
4992
- .map_err(|_| ChannelError::Ignore("Signer rejected channel_announcement".to_owned()))?;
4993
- Ok(msgs::ChannelAnnouncement {
4994
- node_signature_1: if were_node_one { our_node_sig } else { their_node_sig },
4995
- node_signature_2: if were_node_one { their_node_sig } else { our_node_sig },
4996
- bitcoin_signature_1: if were_node_one { our_bitcoin_sig } else { their_bitcoin_sig },
4997
- bitcoin_signature_2: if were_node_one { their_bitcoin_sig } else { our_bitcoin_sig },
4998
- contents: announcement,
4999
- })
5007
+ match &self.context.holder_signer {
5008
+ ChannelSignerType::Ecdsa(ecdsa) => {
5009
+ let our_bitcoin_sig = ecdsa.sign_channel_announcement_with_funding_key(&announcement, &self.context.secp_ctx)
5010
+ .map_err(|_| ChannelError::Ignore("Signer rejected channel_announcement".to_owned()))?;
5011
+ Ok(msgs::ChannelAnnouncement {
5012
+ node_signature_1: if were_node_one { our_node_sig } else { their_node_sig },
5013
+ node_signature_2: if were_node_one { their_node_sig } else { our_node_sig },
5014
+ bitcoin_signature_1: if were_node_one { our_bitcoin_sig } else { their_bitcoin_sig },
5015
+ bitcoin_signature_2: if were_node_one { their_bitcoin_sig } else { our_bitcoin_sig },
5016
+ contents: announcement,
5017
+ })
5018
+ }
5019
+ }
5000
5020
} else {
5001
5021
Err(ChannelError::Ignore("Attempted to sign channel announcement before we'd received announcement_signatures".to_string()))
5002
5022
}
@@ -5322,40 +5342,45 @@ impl<SP: Deref> Channel<SP> where
5322
5342
let counterparty_keys = self.context.build_remote_transaction_keys();
5323
5343
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
5324
5344
let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
5325
- let (signature, htlc_signatures);
5326
5345
5327
- {
5328
- let mut htlcs = Vec::with_capacity(commitment_stats.htlcs_included.len());
5329
- for &(ref htlc, _) in commitment_stats.htlcs_included.iter() {
5330
- htlcs.push(htlc);
5331
- }
5346
+ match &self.context.holder_signer {
5347
+ ChannelSignerType::Ecdsa(ecdsa) => {
5348
+ let (signature, htlc_signatures);
5332
5349
5333
- let res = self.context.holder_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&commitment_stats.tx, commitment_stats.preimages, &self.context.secp_ctx)
5334
- .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?;
5335
- signature = res.0;
5336
- htlc_signatures = res.1;
5350
+ {
5351
+ let mut htlcs = Vec::with_capacity(commitment_stats.htlcs_included.len());
5352
+ for &(ref htlc, _) in commitment_stats.htlcs_included.iter() {
5353
+ htlcs.push(htlc);
5354
+ }
5337
5355
5338
- log_trace!(logger, "Signed remote commitment tx {} (txid {}) with redeemscript {} -> {} in channel {}",
5339
- encode::serialize_hex(&commitment_stats.tx.trust().built_transaction().transaction),
5340
- &counterparty_commitment_txid, encode::serialize_hex(&self.context.get_funding_redeemscript()),
5341
- log_bytes!(signature.serialize_compact()[..]), log_bytes!(self.context.channel_id()));
5356
+ let res = ecdsa.sign_counterparty_commitment(&commitment_stats.tx, commitment_stats.preimages, &self.context.secp_ctx)
5357
+ .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?;
5358
+ signature = res.0;
5359
+ htlc_signatures = res.1;
5360
+
5361
+ log_trace!(logger, "Signed remote commitment tx {} (txid {}) with redeemscript {} -> {} in channel {}",
5362
+ encode::serialize_hex(&commitment_stats.tx.trust().built_transaction().transaction),
5363
+ &counterparty_commitment_txid, encode::serialize_hex(&self.context.get_funding_redeemscript()),
5364
+ log_bytes!(signature.serialize_compact()[..]), log_bytes!(self.context.channel_id()));
5365
+
5366
+ for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
5367
+ log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
5368
+ 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)),
5369
+ encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
5370
+ log_bytes!(counterparty_keys.broadcaster_htlc_key.serialize()),
5371
+ log_bytes!(htlc_sig.serialize_compact()[..]), log_bytes!(self.context.channel_id()));
5372
+ }
5373
+ }
5342
5374
5343
- for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
5344
- log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
5345
- 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)),
5346
- encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
5347
- log_bytes!(counterparty_keys.broadcaster_htlc_key.serialize()),
5348
- log_bytes!(htlc_sig.serialize_compact()[..]), log_bytes!(self.context.channel_id()));
5375
+ Ok((msgs::CommitmentSigned {
5376
+ channel_id: self.context.channel_id,
5377
+ signature,
5378
+ htlc_signatures,
5379
+ #[cfg(taproot)]
5380
+ partial_signature_with_nonce: None,
5381
+ }, (counterparty_commitment_txid, commitment_stats.htlcs_included)))
5349
5382
}
5350
5383
}
5351
-
5352
- Ok((msgs::CommitmentSigned {
5353
- channel_id: self.context.channel_id,
5354
- signature,
5355
- htlc_signatures,
5356
- #[cfg(taproot)]
5357
- partial_signature_with_nonce: None,
5358
- }, (counterparty_commitment_txid, commitment_stats.htlcs_included)))
5359
5384
}
5360
5385
5361
5386
/// Adds a pending outbound HTLC to this channel, and builds a new remote commitment
@@ -5725,8 +5750,13 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
5725
5750
fn get_funding_created_signature<L: Deref>(&mut self, logger: &L) -> Result<Signature, ChannelError> where L::Target: Logger {
5726
5751
let counterparty_keys = self.context.build_remote_transaction_keys();
5727
5752
let counterparty_initial_commitment_tx = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
5728
- Ok(self.context.holder_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.context.secp_ctx)
5729
- .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0)
5753
+ match &self.context.holder_signer {
5754
+ // TODO (arik): move match into calling method for Taproot
5755
+ ChannelSignerType::Ecdsa(ecdsa) => {
5756
+ Ok(ecdsa.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.context.secp_ctx)
5757
+ .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0)
5758
+ }
5759
+ }
5730
5760
}
5731
5761
5732
5762
/// Updates channel state with knowledge of the funding transaction's txid/index, and generates
@@ -6446,11 +6476,16 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
6446
6476
log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
6447
6477
log_bytes!(self.context.channel_id()), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
6448
6478
6449
- let counterparty_signature = self.context.holder_signer.as_ecdsa().unwrap().sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.context.secp_ctx)
6450
- .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0;
6479
+ match &self.context.holder_signer {
6480
+ // TODO (arik): move match into calling method for Taproot
6481
+ ChannelSignerType::Ecdsa(ecdsa) => {
6482
+ let counterparty_signature = ecdsa.sign_counterparty_commitment(&counterparty_initial_commitment_tx, Vec::new(), &self.context.secp_ctx)
6483
+ .map_err(|_| ChannelError::Close("Failed to get signatures for new commitment_signed".to_owned()))?.0;
6451
6484
6452
- // We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
6453
- Ok((counterparty_initial_bitcoin_tx.txid, initial_commitment_tx, counterparty_signature))
6485
+ // We sign "counterparty" commitment transaction, allowing them to broadcast the tx if they wish.
6486
+ Ok((counterparty_initial_bitcoin_tx.txid, initial_commitment_tx, counterparty_signature))
6487
+ }
6488
+ }
6454
6489
}
6455
6490
6456
6491
pub fn funding_created<L: Deref>(
@@ -6630,7 +6665,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
6630
6665
self.context.latest_monitor_update_id.write(writer)?;
6631
6666
6632
6667
let mut key_data = VecWriter(Vec::new());
6633
- self.context.holder_signer.as_ecdsa().unwrap().write(&mut key_data)?;
6668
+ // TODO: Introduce serialization distinction for non-ECDSA signers.
6669
+ self.context.holder_signer.as_ecdsa().expect("Only ECDSA signers may be serialized").write(&mut key_data)?;
6634
6670
assert!(key_data.0.len() < core::usize::MAX);
6635
6671
assert!(key_data.0.len() < core::u32::MAX as usize);
6636
6672
(key_data.0.len() as u32).write(writer)?;
0 commit comments