Skip to content

Commit 1d1d8cd

Browse files
committed
Implement DelayedPaymentBasepoint and DelayedPaymentKey wrappers to explicitly specify those types of keys in functions and structs allowing the language to verify what key is being used and enable idiomatic derivation of one key from another.
1 parent 1852715 commit 1d1d8cd

File tree

6 files changed

+137
-49
lines changed

6 files changed

+137
-49
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use bitcoin::{secp256k1, EcdsaSighashType};
3535
use crate::ln::channel::INITIAL_COMMITMENT_NUMBER;
3636
use crate::ln::{PaymentHash, PaymentPreimage};
3737
use crate::ln::msgs::DecodeError;
38-
use crate::ln::chan_utils;
38+
use crate::ln::chan_utils::{self, DelayedPaymentKey, DelayedPaymentBasepoint};
3939
use crate::ln::chan_utils::{CommitmentTransaction, CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCClaim, ChannelTransactionParameters, HolderCommitmentTransaction, TxCreationKeys};
4040
use crate::ln::channelmanager::{HTLCSource, SentHTLCId};
4141
use crate::chain;
@@ -239,7 +239,7 @@ struct HolderSignedTx {
239239
revocation_key: PublicKey,
240240
a_htlc_key: PublicKey,
241241
b_htlc_key: PublicKey,
242-
delayed_payment_key: PublicKey,
242+
delayed_payment_key: DelayedPaymentKey,
243243
per_commitment_point: PublicKey,
244244
htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>,
245245
to_self_value_sat: u64,
@@ -276,7 +276,7 @@ impl HolderSignedTx {
276276
/// justice or 2nd-stage preimage/timeout transactions.
277277
#[derive(Clone, PartialEq, Eq)]
278278
struct CounterpartyCommitmentParameters {
279-
counterparty_delayed_payment_base_key: PublicKey,
279+
counterparty_delayed_payment_base_key: DelayedPaymentBasepoint,
280280
counterparty_htlc_base_key: PublicKey,
281281
on_counterparty_tx_csv: u16,
282282
}
@@ -2923,9 +2923,10 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
29232923
let revocation_pubkey = chan_utils::derive_public_revocation_key(
29242924
&self.onchain_tx_handler.secp_ctx, &their_per_commitment_point,
29252925
&self.holder_revocation_basepoint);
2926-
let delayed_key = chan_utils::derive_public_key(&self.onchain_tx_handler.secp_ctx,
2927-
&their_per_commitment_point,
2928-
&self.counterparty_commitment_params.counterparty_delayed_payment_base_key);
2926+
let delayed_payment_basepoint = DelayedPaymentBasepoint::from(self.counterparty_commitment_params.counterparty_delayed_payment_base_key);
2927+
let delayed_key = DelayedPaymentKey::from_basepoint(&self.onchain_tx_handler.secp_ctx,
2928+
&delayed_payment_basepoint,
2929+
&their_per_commitment_point);
29292930
let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey,
29302931
self.counterparty_commitment_params.on_counterparty_tx_csv, &delayed_key);
29312932

@@ -2989,7 +2990,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
29892990
let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
29902991
let per_commitment_point = PublicKey::from_secret_key(&self.onchain_tx_handler.secp_ctx, &per_commitment_key);
29912992
let revocation_pubkey = chan_utils::derive_public_revocation_key(&self.onchain_tx_handler.secp_ctx, &per_commitment_point, &self.holder_revocation_basepoint);
2992-
let delayed_key = chan_utils::derive_public_key(&self.onchain_tx_handler.secp_ctx, &PublicKey::from_secret_key(&self.onchain_tx_handler.secp_ctx, &per_commitment_key), &self.counterparty_commitment_params.counterparty_delayed_payment_base_key);
2993+
let delayed_key = DelayedPaymentKey::from_basepoint(&self.onchain_tx_handler.secp_ctx, &self.counterparty_commitment_params.counterparty_delayed_payment_base_key, &PublicKey::from_secret_key(&self.onchain_tx_handler.secp_ctx, &per_commitment_key));
29932994

29942995
let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.counterparty_commitment_params.on_counterparty_tx_csv, &delayed_key);
29952996
let revokeable_p2wsh = revokeable_redeemscript.to_v0_p2wsh();
@@ -3103,9 +3104,9 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
31033104
if let Some(transaction) = tx {
31043105
let revocation_pubkey = chan_utils::derive_public_revocation_key(
31053106
&self.onchain_tx_handler.secp_ctx, &per_commitment_point, &self.holder_revocation_basepoint);
3106-
let delayed_key = chan_utils::derive_public_key(&self.onchain_tx_handler.secp_ctx,
3107-
&per_commitment_point,
3108-
&self.counterparty_commitment_params.counterparty_delayed_payment_base_key);
3107+
3108+
let delayed_key = DelayedPaymentKey::from_basepoint(&self.onchain_tx_handler.secp_ctx, &self.counterparty_commitment_params.counterparty_delayed_payment_base_key, &per_commitment_point);
3109+
31093110
let revokeable_p2wsh = chan_utils::get_revokeable_redeemscript(&revocation_pubkey,
31103111
self.counterparty_commitment_params.on_counterparty_tx_csv,
31113112
&delayed_key).to_v0_p2wsh();
@@ -4501,7 +4502,7 @@ mod tests {
45014502
use crate::chain::transaction::OutPoint;
45024503
use crate::sign::InMemorySigner;
45034504
use crate::ln::{PaymentPreimage, PaymentHash};
4504-
use crate::ln::chan_utils;
4505+
use crate::ln::chan_utils::{self, DelayedPaymentBasepoint, DelayedPaymentKey};
45054506
use crate::ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
45064507
use crate::ln::channelmanager::{PaymentSendFailure, PaymentId, RecipientOnionFields};
45074508
use crate::ln::functional_test_utils::*;
@@ -4670,7 +4671,7 @@ mod tests {
46704671
funding_pubkey: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[44; 32]).unwrap()),
46714672
revocation_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()),
46724673
payment_point: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap()),
4673-
delayed_payment_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[47; 32]).unwrap()),
4674+
delayed_payment_basepoint: DelayedPaymentBasepoint::from(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[47; 32]).unwrap())),
46744675
htlc_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[48; 32]).unwrap())
46754676
};
46764677
let funding_outpoint = OutPoint { txid: Txid::all_zeros(), index: u16::max_value() };
@@ -4770,7 +4771,7 @@ mod tests {
47704771
payment_hash: PaymentHash([1; 32]),
47714772
transaction_output_index: Some($idx as u32),
47724773
};
4773-
let redeem_script = if *$weight == WEIGHT_REVOKED_OUTPUT { chan_utils::get_revokeable_redeemscript(&pubkey, 256, &pubkey) } else { chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, $opt_anchors, &pubkey, &pubkey, &pubkey) };
4774+
let redeem_script = if *$weight == WEIGHT_REVOKED_OUTPUT { chan_utils::get_revokeable_redeemscript(&pubkey, 256, &DelayedPaymentKey::from_basepoint(&secp_ctx, &DelayedPaymentBasepoint::from(pubkey), &pubkey)) } else { chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, $opt_anchors, &pubkey, &pubkey, &pubkey) };
47744775
let sighash = hash_to_message!(&$sighash_parts.segwit_signature_hash($idx, &redeem_script, $amount, EcdsaSighashType::All).unwrap()[..]);
47754776
let sig = secp_ctx.sign_ecdsa(&sighash, &privkey);
47764777
let mut ser_sig = sig.serialize_der().to_vec();

lightning/src/chain/package.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use bitcoin::hash_types::Txid;
2121
use bitcoin::secp256k1::{SecretKey,PublicKey};
2222

2323
use crate::ln::PaymentPreimage;
24-
use crate::ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment};
24+
use crate::ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment, DelayedPaymentBasepoint};
2525
use crate::ln::chan_utils;
2626
use crate::ln::msgs::DecodeError;
2727
use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
@@ -114,7 +114,7 @@ const HIGH_FREQUENCY_BUMP_INTERVAL: u32 = 1;
114114
#[derive(Clone, PartialEq, Eq)]
115115
pub(crate) struct RevokedOutput {
116116
per_commitment_point: PublicKey,
117-
counterparty_delayed_payment_base_key: PublicKey,
117+
counterparty_delayed_payment_base_key: DelayedPaymentBasepoint,
118118
counterparty_htlc_base_key: PublicKey,
119119
per_commitment_key: SecretKey,
120120
weight: u64,
@@ -124,7 +124,7 @@ pub(crate) struct RevokedOutput {
124124
}
125125

126126
impl RevokedOutput {
127-
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, per_commitment_key: SecretKey, amount: u64, on_counterparty_tx_csv: u16, is_counterparty_balance_on_anchors: bool) -> Self {
127+
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: DelayedPaymentBasepoint, counterparty_htlc_base_key: PublicKey, per_commitment_key: SecretKey, amount: u64, on_counterparty_tx_csv: u16, is_counterparty_balance_on_anchors: bool) -> Self {
128128
RevokedOutput {
129129
per_commitment_point,
130130
counterparty_delayed_payment_base_key,
@@ -160,7 +160,7 @@ impl_writeable_tlv_based!(RevokedOutput, {
160160
#[derive(Clone, PartialEq, Eq)]
161161
pub(crate) struct RevokedHTLCOutput {
162162
per_commitment_point: PublicKey,
163-
counterparty_delayed_payment_base_key: PublicKey,
163+
counterparty_delayed_payment_base_key: DelayedPaymentBasepoint,
164164
counterparty_htlc_base_key: PublicKey,
165165
per_commitment_key: SecretKey,
166166
weight: u64,
@@ -169,7 +169,7 @@ pub(crate) struct RevokedHTLCOutput {
169169
}
170170

171171
impl RevokedHTLCOutput {
172-
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, per_commitment_key: SecretKey, amount: u64, htlc: HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures) -> Self {
172+
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: DelayedPaymentBasepoint, counterparty_htlc_base_key: PublicKey, per_commitment_key: SecretKey, amount: u64, htlc: HTLCOutputInCommitment, channel_type_features: &ChannelTypeFeatures) -> Self {
173173
let weight = if htlc.offered { weight_revoked_offered_htlc(channel_type_features) } else { weight_revoked_received_htlc(channel_type_features) };
174174
RevokedHTLCOutput {
175175
per_commitment_point,
@@ -204,15 +204,15 @@ impl_writeable_tlv_based!(RevokedHTLCOutput, {
204204
#[derive(Clone, PartialEq, Eq)]
205205
pub(crate) struct CounterpartyOfferedHTLCOutput {
206206
per_commitment_point: PublicKey,
207-
counterparty_delayed_payment_base_key: PublicKey,
207+
counterparty_delayed_payment_base_key: DelayedPaymentBasepoint,
208208
counterparty_htlc_base_key: PublicKey,
209209
preimage: PaymentPreimage,
210210
htlc: HTLCOutputInCommitment,
211211
channel_type_features: ChannelTypeFeatures,
212212
}
213213

214214
impl CounterpartyOfferedHTLCOutput {
215-
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, preimage: PaymentPreimage, htlc: HTLCOutputInCommitment, channel_type_features: ChannelTypeFeatures) -> Self {
215+
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: DelayedPaymentBasepoint, counterparty_htlc_base_key: PublicKey, preimage: PaymentPreimage, htlc: HTLCOutputInCommitment, channel_type_features: ChannelTypeFeatures) -> Self {
216216
CounterpartyOfferedHTLCOutput {
217217
per_commitment_point,
218218
counterparty_delayed_payment_base_key,
@@ -282,14 +282,14 @@ impl Readable for CounterpartyOfferedHTLCOutput {
282282
#[derive(Clone, PartialEq, Eq)]
283283
pub(crate) struct CounterpartyReceivedHTLCOutput {
284284
per_commitment_point: PublicKey,
285-
counterparty_delayed_payment_base_key: PublicKey,
285+
counterparty_delayed_payment_base_key: DelayedPaymentBasepoint,
286286
counterparty_htlc_base_key: PublicKey,
287287
htlc: HTLCOutputInCommitment,
288288
channel_type_features: ChannelTypeFeatures,
289289
}
290290

291291
impl CounterpartyReceivedHTLCOutput {
292-
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: PublicKey, counterparty_htlc_base_key: PublicKey, htlc: HTLCOutputInCommitment, channel_type_features: ChannelTypeFeatures) -> Self {
292+
pub(crate) fn build(per_commitment_point: PublicKey, counterparty_delayed_payment_base_key: DelayedPaymentBasepoint, counterparty_htlc_base_key: PublicKey, htlc: HTLCOutputInCommitment, channel_type_features: ChannelTypeFeatures) -> Self {
293293
CounterpartyReceivedHTLCOutput {
294294
per_commitment_point,
295295
counterparty_delayed_payment_base_key,
@@ -1200,6 +1200,7 @@ mod tests {
12001200
use crate::chain::Txid;
12011201
use crate::ln::chan_utils::HTLCOutputInCommitment;
12021202
use crate::ln::{PaymentPreimage, PaymentHash};
1203+
use crate::ln::chan_utils::DelayedPaymentBasepoint;
12031204

12041205
use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
12051206
use bitcoin::blockdata::script::Script;
@@ -1216,7 +1217,7 @@ mod tests {
12161217
{
12171218
let dumb_scalar = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
12181219
let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
1219-
PackageSolvingData::RevokedOutput(RevokedOutput::build(dumb_point, dumb_point, dumb_point, dumb_scalar, 0, 0, $is_counterparty_balance_on_anchors))
1220+
PackageSolvingData::RevokedOutput(RevokedOutput::build(dumb_point, DelayedPaymentBasepoint::from(dumb_point), dumb_point, dumb_scalar, 0, 0, $is_counterparty_balance_on_anchors))
12201221
}
12211222
}
12221223
}
@@ -1228,7 +1229,7 @@ mod tests {
12281229
let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
12291230
let hash = PaymentHash([1; 32]);
12301231
let htlc = HTLCOutputInCommitment { offered: true, amount_msat: $amt, cltv_expiry: 0, payment_hash: hash, transaction_output_index: None };
1231-
PackageSolvingData::CounterpartyReceivedHTLCOutput(CounterpartyReceivedHTLCOutput::build(dumb_point, dumb_point, dumb_point, htlc, $opt_anchors))
1232+
PackageSolvingData::CounterpartyReceivedHTLCOutput(CounterpartyReceivedHTLCOutput::build(dumb_point, DelayedPaymentBasepoint::from(dumb_point), dumb_point, htlc, $opt_anchors))
12321233
}
12331234
}
12341235
}
@@ -1241,7 +1242,7 @@ mod tests {
12411242
let hash = PaymentHash([1; 32]);
12421243
let preimage = PaymentPreimage([2;32]);
12431244
let htlc = HTLCOutputInCommitment { offered: false, amount_msat: $amt, cltv_expiry: 1000, payment_hash: hash, transaction_output_index: None };
1244-
PackageSolvingData::CounterpartyOfferedHTLCOutput(CounterpartyOfferedHTLCOutput::build(dumb_point, dumb_point, dumb_point, preimage, htlc, $opt_anchors))
1245+
PackageSolvingData::CounterpartyOfferedHTLCOutput(CounterpartyOfferedHTLCOutput::build(dumb_point, DelayedPaymentBasepoint::from(dumb_point), dumb_point, preimage, htlc, $opt_anchors))
12451246
}
12461247
}
12471248
}

lightning/src/events/bump_transaction.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::chain::chaininterface::{BroadcasterInterface, fee_for_weight};
1818
use crate::chain::ClaimId;
1919
use crate::io_extras::sink;
2020
use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
21-
use crate::ln::chan_utils;
21+
use crate::ln::chan_utils::{self, DelayedPaymentKey};
2222
use crate::ln::chan_utils::{
2323
ANCHOR_INPUT_WITNESS_WEIGHT, HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT,
2424
HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT, ChannelTransactionParameters, HTLCOutputInCommitment
@@ -186,9 +186,7 @@ impl HTLCDescriptor {
186186
let channel_params = self.channel_derivation_parameters.transaction_parameters.as_holder_broadcastable();
187187
let broadcaster_keys = channel_params.broadcaster_pubkeys();
188188
let counterparty_keys = channel_params.countersignatory_pubkeys();
189-
let broadcaster_delayed_key = chan_utils::derive_public_key(
190-
secp, &self.per_commitment_point, &broadcaster_keys.delayed_payment_basepoint
191-
);
189+
let broadcaster_delayed_key = DelayedPaymentKey::from_basepoint(&secp, &broadcaster_keys.delayed_payment_basepoint, &self.per_commitment_point);
192190
let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
193191
secp, &self.per_commitment_point, &counterparty_keys.revocation_basepoint
194192
);

0 commit comments

Comments
 (0)