Skip to content

Commit 5588f53

Browse files
committed
Use match arms for signer types.
1 parent dd04e06 commit 5588f53

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()))?;
@@ -4115,20 +4119,24 @@ impl<SP: Deref> Channel<SP> where
41154119
log_trace!(logger, "Proposing initial closing_signed for our counterparty with a fee range of {}-{} sat (with initial proposal {} sats)",
41164120
our_min_fee, our_max_fee, total_fee_satoshis);
41174121

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

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+
}
41324140
}
41334141

41344142
// 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
43444352
self.build_closing_transaction($new_fee, false)
43454353
};
43464354

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

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+
}
43684380
}
43694381
}
43704382

@@ -4954,26 +4966,30 @@ impl<SP: Deref> Channel<SP> where
49544966
},
49554967
Ok(v) => v
49564968
};
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+
};
49684982

4969-
self.context.announcement_sigs_state = AnnouncementSigsState::MessageSent;
4983+
self.context.announcement_sigs_state = AnnouncementSigsState::MessageSent;
49704984

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+
}
49774993
}
49784994

49794995
/// Signs the given channel announcement, returning a ChannelError::Ignore if no keys are
@@ -4988,15 +5004,19 @@ impl<SP: Deref> Channel<SP> where
49885004

49895005
let our_node_sig = node_signer.sign_gossip_message(msgs::UnsignedGossipMessage::ChannelAnnouncement(&announcement))
49905006
.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+
}
50005020
} else {
50015021
Err(ChannelError::Ignore("Attempted to sign channel announcement before we'd received announcement_signatures".to_string()))
50025022
}
@@ -5322,40 +5342,45 @@ impl<SP: Deref> Channel<SP> where
53225342
let counterparty_keys = self.context.build_remote_transaction_keys();
53235343
let commitment_stats = self.context.build_commitment_transaction(self.context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, true, logger);
53245344
let counterparty_commitment_txid = commitment_stats.tx.trust().txid();
5325-
let (signature, htlc_signatures);
53265345

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

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

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

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)))
53495382
}
53505383
}
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)))
53595384
}
53605385

53615386
/// 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 {
57255750
fn get_funding_created_signature<L: Deref>(&mut self, logger: &L) -> Result<Signature, ChannelError> where L::Target: Logger {
57265751
let counterparty_keys = self.context.build_remote_transaction_keys();
57275752
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+
}
57305760
}
57315761

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

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

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+
}
64546489
}
64556490

64566491
pub fn funding_created<L: Deref>(
@@ -6630,7 +6665,8 @@ impl<SP: Deref> Writeable for Channel<SP> where SP::Target: SignerProvider {
66306665
self.context.latest_monitor_update_id.write(writer)?;
66316666

66326667
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)?;
66346670
assert!(key_data.0.len() < core::usize::MAX);
66356671
assert!(key_data.0.len() < core::u32::MAX as usize);
66366672
(key_data.0.len() as u32).write(writer)?;

0 commit comments

Comments
 (0)