Skip to content

Commit 5fc075b

Browse files
committed
Implement key wrappers for Payment, Delayed and Htlc keys.
1 parent 415cbf0 commit 5fc075b

File tree

8 files changed

+329
-117
lines changed

8 files changed

+329
-117
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ 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;
39-
use crate::ln::chan_utils::{CommitmentTransaction, CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCClaim, ChannelTransactionParameters, HolderCommitmentTransaction, TxCreationKeys};
38+
use crate::ln::channel_keys::{DelayedPaymentKey, DelayedPaymentBasepoint, HtlcBasepoint};
39+
use crate::ln::chan_utils::{self,CommitmentTransaction, CounterpartyCommitmentSecrets, HTLCOutputInCommitment, HTLCClaim, ChannelTransactionParameters, HolderCommitmentTransaction, TxCreationKeys};
4040
use crate::ln::channelmanager::{HTLCSource, SentHTLCId};
4141
use crate::chain;
4242
use crate::chain::{BestBlock, WatchedOutput};
@@ -240,7 +240,7 @@ struct HolderSignedTx {
240240
revocation_key: PublicKey,
241241
a_htlc_key: PublicKey,
242242
b_htlc_key: PublicKey,
243-
delayed_payment_key: PublicKey,
243+
delayed_payment_key: DelayedPaymentKey,
244244
per_commitment_point: PublicKey,
245245
htlc_outputs: Vec<(HTLCOutputInCommitment, Option<Signature>, Option<HTLCSource>)>,
246246
to_self_value_sat: u64,
@@ -277,8 +277,8 @@ impl HolderSignedTx {
277277
/// justice or 2nd-stage preimage/timeout transactions.
278278
#[derive(Clone, PartialEq, Eq)]
279279
struct CounterpartyCommitmentParameters {
280-
counterparty_delayed_payment_base_key: PublicKey,
281-
counterparty_htlc_base_key: PublicKey,
280+
counterparty_delayed_payment_base_key: DelayedPaymentBasepoint,
281+
counterparty_htlc_base_key: HtlcBasepoint,
282282
on_counterparty_tx_csv: u16,
283283
}
284284

@@ -1162,8 +1162,8 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
11621162
let holder_commitment_tx = HolderSignedTx {
11631163
txid,
11641164
revocation_key: tx_keys.revocation_key,
1165-
a_htlc_key: tx_keys.broadcaster_htlc_key,
1166-
b_htlc_key: tx_keys.countersignatory_htlc_key,
1165+
a_htlc_key: tx_keys.broadcaster_htlc_key.to_public_key(),
1166+
b_htlc_key: tx_keys.countersignatory_htlc_key.to_public_key(),
11671167
delayed_payment_key: tx_keys.broadcaster_delayed_payment_key,
11681168
per_commitment_point: tx_keys.per_commitment_point,
11691169
htlc_outputs: Vec::new(), // There are never any HTLCs in the initial commitment transactions
@@ -2493,8 +2493,8 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
24932493
let mut new_holder_commitment_tx = HolderSignedTx {
24942494
txid,
24952495
revocation_key: tx_keys.revocation_key,
2496-
a_htlc_key: tx_keys.broadcaster_htlc_key,
2497-
b_htlc_key: tx_keys.countersignatory_htlc_key,
2496+
a_htlc_key: tx_keys.broadcaster_htlc_key.to_public_key(),
2497+
b_htlc_key: tx_keys.countersignatory_htlc_key.to_public_key(),
24982498
delayed_payment_key: tx_keys.broadcaster_delayed_payment_key,
24992499
per_commitment_point: tx_keys.per_commitment_point,
25002500
htlc_outputs,
@@ -2926,9 +2926,10 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
29262926
let revocation_pubkey = chan_utils::derive_public_revocation_key(
29272927
&self.onchain_tx_handler.secp_ctx, &their_per_commitment_point,
29282928
&self.holder_revocation_basepoint);
2929-
let delayed_key = chan_utils::derive_public_key(&self.onchain_tx_handler.secp_ctx,
2930-
&their_per_commitment_point,
2931-
&self.counterparty_commitment_params.counterparty_delayed_payment_base_key);
2929+
let delayed_payment_basepoint = DelayedPaymentBasepoint::from(self.counterparty_commitment_params.counterparty_delayed_payment_base_key);
2930+
let delayed_key = DelayedPaymentKey::from_basepoint(&self.onchain_tx_handler.secp_ctx,
2931+
&delayed_payment_basepoint,
2932+
&their_per_commitment_point);
29322933
let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey,
29332934
self.counterparty_commitment_params.on_counterparty_tx_csv, &delayed_key);
29342935

@@ -2992,7 +2993,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
29922993
let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
29932994
let per_commitment_point = PublicKey::from_secret_key(&self.onchain_tx_handler.secp_ctx, &per_commitment_key);
29942995
let revocation_pubkey = chan_utils::derive_public_revocation_key(&self.onchain_tx_handler.secp_ctx, &per_commitment_point, &self.holder_revocation_basepoint);
2995-
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);
2996+
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));
29962997

29972998
let revokeable_redeemscript = chan_utils::get_revokeable_redeemscript(&revocation_pubkey, self.counterparty_commitment_params.on_counterparty_tx_csv, &delayed_key);
29982999
let revokeable_p2wsh = revokeable_redeemscript.to_v0_p2wsh();
@@ -3106,9 +3107,9 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
31063107
if let Some(transaction) = tx {
31073108
let revocation_pubkey = chan_utils::derive_public_revocation_key(
31083109
&self.onchain_tx_handler.secp_ctx, &per_commitment_point, &self.holder_revocation_basepoint);
3109-
let delayed_key = chan_utils::derive_public_key(&self.onchain_tx_handler.secp_ctx,
3110-
&per_commitment_point,
3111-
&self.counterparty_commitment_params.counterparty_delayed_payment_base_key);
3110+
3111+
let delayed_key = DelayedPaymentKey::from_basepoint(&self.onchain_tx_handler.secp_ctx, &self.counterparty_commitment_params.counterparty_delayed_payment_base_key, &per_commitment_point);
3112+
31123113
let revokeable_p2wsh = chan_utils::get_revokeable_redeemscript(&revocation_pubkey,
31133114
self.counterparty_commitment_params.on_counterparty_tx_csv,
31143115
&delayed_key).to_v0_p2wsh();
@@ -4504,8 +4505,8 @@ mod tests {
45044505
use crate::chain::transaction::OutPoint;
45054506
use crate::sign::InMemorySigner;
45064507
use crate::ln::{PaymentPreimage, PaymentHash};
4507-
use crate::ln::chan_utils;
4508-
use crate::ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
4508+
use crate::ln::channel_keys::{DelayedPaymentBasepoint, DelayedPaymentKey, HtlcBasepoint};
4509+
use crate::ln::chan_utils::{self,HTLCOutputInCommitment, ChannelPublicKeys, ChannelTransactionParameters, HolderCommitmentTransaction, CounterpartyChannelTransactionParameters};
45094510
use crate::ln::channelmanager::{PaymentSendFailure, PaymentId, RecipientOnionFields};
45104511
use crate::ln::functional_test_utils::*;
45114512
use crate::ln::script::ShutdownScript;
@@ -4673,8 +4674,8 @@ mod tests {
46734674
funding_pubkey: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[44; 32]).unwrap()),
46744675
revocation_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()),
46754676
payment_point: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[46; 32]).unwrap()),
4676-
delayed_payment_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[47; 32]).unwrap()),
4677-
htlc_basepoint: PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[48; 32]).unwrap())
4677+
delayed_payment_basepoint: DelayedPaymentBasepoint::from(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[47; 32]).unwrap())),
4678+
htlc_basepoint: HtlcBasepoint::from(PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[48; 32]).unwrap()))
46784679
};
46794680
let funding_outpoint = OutPoint { txid: Txid::all_zeros(), index: u16::max_value() };
46804681
let channel_parameters = ChannelTransactionParameters {
@@ -4764,6 +4765,7 @@ mod tests {
47644765
let privkey = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
47654766
let pubkey = PublicKey::from_secret_key(&secp_ctx, &privkey);
47664767

4768+
use crate::ln::channel_keys::{HtlcKey, HtlcBasepoint};
47674769
macro_rules! sign_input {
47684770
($sighash_parts: expr, $idx: expr, $amount: expr, $weight: expr, $sum_actual_sigs: expr, $opt_anchors: expr) => {
47694771
let htlc = HTLCOutputInCommitment {
@@ -4773,7 +4775,7 @@ mod tests {
47734775
payment_hash: PaymentHash([1; 32]),
47744776
transaction_output_index: Some($idx as u32),
47754777
};
4776-
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) };
4778+
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, &HtlcKey::from_basepoint(&secp_ctx, &HtlcBasepoint::from(pubkey), &pubkey), &HtlcKey::from_basepoint(&secp_ctx, &HtlcBasepoint::from(pubkey), &pubkey), &pubkey) };
47774779
let sighash = hash_to_message!(&$sighash_parts.segwit_signature_hash($idx, &redeem_script, $amount, EcdsaSighashType::All).unwrap()[..]);
47784780
let sig = secp_ctx.sign_ecdsa(&sighash, &privkey);
47794781
let mut ser_sig = sig.serialize_der().to_vec();

lightning/src/chain/package.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ 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};
25-
use crate::ln::chan_utils;
24+
use crate::ln::chan_utils::{self,TxCreationKeys, HTLCOutputInCommitment};
25+
use crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint};
2626
use crate::ln::msgs::DecodeError;
2727
use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT, compute_feerate_sat_per_1000_weight, FEERATE_FLOOR_SATS_PER_KW};
2828
use crate::sign::WriteableEcdsaChannelSigner;
@@ -114,8 +114,8 @@ 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,
118-
counterparty_htlc_base_key: PublicKey,
117+
counterparty_delayed_payment_base_key: DelayedPaymentBasepoint,
118+
counterparty_htlc_base_key: HtlcBasepoint,
119119
per_commitment_key: SecretKey,
120120
weight: u64,
121121
amount: 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: HtlcBasepoint, 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,16 +160,16 @@ 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,
164-
counterparty_htlc_base_key: PublicKey,
163+
counterparty_delayed_payment_base_key: DelayedPaymentBasepoint,
164+
counterparty_htlc_base_key: HtlcBasepoint,
165165
per_commitment_key: SecretKey,
166166
weight: u64,
167167
amount: u64,
168168
htlc: HTLCOutputInCommitment,
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: HtlcBasepoint, 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,
208-
counterparty_htlc_base_key: PublicKey,
207+
counterparty_delayed_payment_base_key: DelayedPaymentBasepoint,
208+
counterparty_htlc_base_key: HtlcBasepoint,
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: HtlcBasepoint, 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,
286-
counterparty_htlc_base_key: PublicKey,
285+
counterparty_delayed_payment_base_key: DelayedPaymentBasepoint,
286+
counterparty_htlc_base_key: HtlcBasepoint,
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: HtlcBasepoint, htlc: HTLCOutputInCommitment, channel_type_features: ChannelTypeFeatures) -> Self {
293293
CounterpartyReceivedHTLCOutput {
294294
per_commitment_point,
295295
counterparty_delayed_payment_base_key,
@@ -1190,6 +1190,7 @@ mod tests {
11901190
use crate::chain::Txid;
11911191
use crate::ln::chan_utils::HTLCOutputInCommitment;
11921192
use crate::ln::{PaymentPreimage, PaymentHash};
1193+
use crate::ln::channel_keys::{DelayedPaymentBasepoint, HtlcBasepoint};
11931194

11941195
use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
11951196
use bitcoin::blockdata::script::Script;
@@ -1206,7 +1207,7 @@ mod tests {
12061207
{
12071208
let dumb_scalar = SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap();
12081209
let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
1209-
PackageSolvingData::RevokedOutput(RevokedOutput::build(dumb_point, dumb_point, dumb_point, dumb_scalar, 0, 0, $is_counterparty_balance_on_anchors))
1210+
PackageSolvingData::RevokedOutput(RevokedOutput::build(dumb_point, DelayedPaymentBasepoint::from(dumb_point), HtlcBasepoint::from(dumb_point), dumb_scalar, 0, 0, $is_counterparty_balance_on_anchors))
12101211
}
12111212
}
12121213
}
@@ -1218,7 +1219,7 @@ mod tests {
12181219
let dumb_point = PublicKey::from_secret_key(&$secp_ctx, &dumb_scalar);
12191220
let hash = PaymentHash([1; 32]);
12201221
let htlc = HTLCOutputInCommitment { offered: true, amount_msat: $amt, cltv_expiry: 0, payment_hash: hash, transaction_output_index: None };
1221-
PackageSolvingData::CounterpartyReceivedHTLCOutput(CounterpartyReceivedHTLCOutput::build(dumb_point, dumb_point, dumb_point, htlc, $opt_anchors))
1222+
PackageSolvingData::CounterpartyReceivedHTLCOutput(CounterpartyReceivedHTLCOutput::build(dumb_point, DelayedPaymentBasepoint::from(dumb_point), HtlcBasepoint::from(dumb_point), htlc, $opt_anchors))
12221223
}
12231224
}
12241225
}
@@ -1231,7 +1232,7 @@ mod tests {
12311232
let hash = PaymentHash([1; 32]);
12321233
let preimage = PaymentPreimage([2;32]);
12331234
let htlc = HTLCOutputInCommitment { offered: false, amount_msat: $amt, cltv_expiry: 1000, payment_hash: hash, transaction_output_index: None };
1234-
PackageSolvingData::CounterpartyOfferedHTLCOutput(CounterpartyOfferedHTLCOutput::build(dumb_point, dumb_point, dumb_point, preimage, htlc, $opt_anchors))
1235+
PackageSolvingData::CounterpartyOfferedHTLCOutput(CounterpartyOfferedHTLCOutput::build(dumb_point, DelayedPaymentBasepoint::from(dumb_point), HtlcBasepoint::from(dumb_point), preimage, htlc, $opt_anchors))
12351236
}
12361237
}
12371238
}

0 commit comments

Comments
 (0)