Skip to content

Commit 8dc3262

Browse files
committed
Use match arms for signer types.
1 parent a2004cb commit 8dc3262

File tree

1 file changed

+135
-99
lines changed

1 file changed

+135
-99
lines changed

lightning/src/ln/channel.rs

Lines changed: 135 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -3243,10 +3243,14 @@ impl<SP: Deref> Channel<SP> where
32433243
*self.context.next_remote_commitment_tx_fee_info_cached.lock().unwrap() = None;
32443244
}
32453245

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+
};
32503254

32513255
self.context.commitment_secrets.provide_secret(self.context.cur_counterparty_commitment_transaction_number + 1, msg.per_commitment_secret)
32523256
.map_err(|_| ChannelError::Close("Previous secrets did not match new one".to_owned()))?;
@@ -4091,20 +4095,24 @@ impl<SP: Deref> Channel<SP> where
40914095
log_trace!(logger, "Proposing initial closing_signed for our counterparty with a fee range of {}-{} sat (with initial proposal {} sats)",
40924096
our_min_fee, our_max_fee, total_fee_satoshis);
40934097

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()))?;
40974103

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+
}
41084116
}
41094117

41104118
// 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
43204328
self.build_closing_transaction($new_fee, false)
43214329
};
43224330

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()))?;
43334336

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+
}
43444356
}
43454357
}
43464358

@@ -4930,26 +4942,30 @@ impl<SP: Deref> Channel<SP> where
49304942
},
49314943
Ok(v) => v
49324944
};
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+
};
49444958

4945-
self.context.announcement_sigs_state = AnnouncementSigsState::MessageSent;
4959+
self.context.announcement_sigs_state = AnnouncementSigsState::MessageSent;
49464960

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+
}
49534969
}
49544970

49554971
/// Signs the given channel announcement, returning a ChannelError::Ignore if no keys are
@@ -4964,15 +4980,19 @@ impl<SP: Deref> Channel<SP> where
49644980

49654981
let our_node_sig = node_signer.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelAnnouncement(&announcement))
49664982
.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+
}
49764996
} else {
49774997
Err(ChannelError::Ignore("Attempted to sign channel announcement before we'd received announcement_signatures".to_string()))
49784998
}
@@ -5298,40 +5318,45 @@ impl<SP: Deref> Channel<SP> where
52985318
let counterparty_keys = self.context.build_remote_transaction_keys();
52995319
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
53005320
let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
5301-
let (signature, htlc_signatures);
53025321

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);
53085325

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+
}
53135331

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+
}
53185350

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)))
53255358
}
53265359
}
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)))
53355360
}
53365361

53375362
/// 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 {
57015726
fn get_funding_created_signature<L: Deref>(&mut self, logger: &L) -> Result<Signature, ChannelError> where L::Target: Logger {
57025727
let counterparty_keys = self.context.build_remote_transaction_keys();
57035728
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+
}
57065736
}
57075737

57085738
/// 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 {
64226452
log_trace!(logger, "Initial counterparty tx for channel {} is: txid {} tx {}",
64236453
log_bytes!(self.context.channel_id()), counterparty_initial_bitcoin_tx.txid, encode::serialize_hex(&counterparty_initial_bitcoin_tx.transaction));
64246454

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;
64276460

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+
}
64306465
}
64316466

64326467
pub fn funding_created<L: Deref>(
@@ -6606,7 +6641,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
66066641
self.context.latest_monitor_update_id.write(writer)?;
66076642

66086643
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)?;
66106646
assert!(key_data.0.len() < core::usize::MAX);
66116647
assert!(key_data.0.len() < core::u32::MAX as usize);
66126648
(key_data.0.len() as u32).write(writer)?;

0 commit comments

Comments
 (0)