Skip to content

Commit 72c42ee

Browse files
committed
Cache HTLC per_commitment_point in descriptor
This allows us to obtain the HTLC input and output from its descriptor without needing to derive the `per_commitment_point` through the signer.
1 parent ae701a0 commit 72c42ee

File tree

5 files changed

+31
-45
lines changed

5 files changed

+31
-45
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,6 +2632,9 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
26322632
channel_parameters: self.onchain_tx_handler.channel_transaction_parameters.clone(),
26332633
commitment_txid: htlc.commitment_txid,
26342634
per_commitment_number: htlc.per_commitment_number,
2635+
per_commitment_point: self.onchain_tx_handler.signer.get_per_commitment_point(
2636+
htlc.per_commitment_number, &self.onchain_tx_handler.secp_ctx,
2637+
),
26352638
htlc: htlc.htlc,
26362639
preimage: htlc.preimage,
26372640
counterparty_sig: htlc.counterparty_sig,

lightning/src/events/bump_transaction.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ pub struct HTLCDescriptor {
9393
pub commitment_txid: Txid,
9494
/// The number of the commitment transaction in which the HTLC output lives.
9595
pub per_commitment_number: u64,
96+
/// The key tweak corresponding to the number of the commitment transaction in which the HTLC
97+
/// output lives. This tweak is applied to all the basepoints for both parties in the channel to
98+
/// arrive at unique keys per commitment.
99+
///
100+
/// See <https://github.com/lightning/bolts/blob/master/03-transactions.md#keys> for more info.
101+
pub per_commitment_point: PublicKey,
96102
/// The details of the HTLC as it appears in the commitment transaction.
97103
pub htlc: HTLCOutputInCommitment,
98104
/// The preimage, if `Some`, to claim the HTLC output with. If `None`, the timeout path must be
@@ -111,17 +117,15 @@ impl HTLCDescriptor {
111117

112118
/// Returns the delayed output created as a result of spending the HTLC output in the commitment
113119
/// transaction.
114-
pub fn tx_output<C: secp256k1::Signing + secp256k1::Verification>(
115-
&self, per_commitment_point: &PublicKey, secp: &Secp256k1<C>
116-
) -> TxOut {
120+
pub fn tx_output<C: secp256k1::Signing + secp256k1::Verification>(&self, secp: &Secp256k1<C>) -> TxOut {
117121
let channel_params = self.channel_parameters.as_holder_broadcastable();
118122
let broadcaster_keys = channel_params.broadcaster_pubkeys();
119123
let counterparty_keys = channel_params.countersignatory_pubkeys();
120124
let broadcaster_delayed_key = chan_utils::derive_public_key(
121-
secp, per_commitment_point, &broadcaster_keys.delayed_payment_basepoint
125+
secp, &self.per_commitment_point, &broadcaster_keys.delayed_payment_basepoint
122126
);
123127
let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
124-
secp, per_commitment_point, &counterparty_keys.revocation_basepoint
128+
secp, &self.per_commitment_point, &counterparty_keys.revocation_basepoint
125129
);
126130
chan_utils::build_htlc_output(
127131
0 /* feerate_per_kw */, channel_params.contest_delay(), &self.htlc,
@@ -130,20 +134,18 @@ impl HTLCDescriptor {
130134
}
131135

132136
/// Returns the witness script of the HTLC output in the commitment transaction.
133-
pub fn witness_script<C: secp256k1::Signing + secp256k1::Verification>(
134-
&self, per_commitment_point: &PublicKey, secp: &Secp256k1<C>
135-
) -> Script {
137+
pub fn witness_script<C: secp256k1::Signing + secp256k1::Verification>(&self, secp: &Secp256k1<C>) -> Script {
136138
let channel_params = self.channel_parameters.as_holder_broadcastable();
137139
let broadcaster_keys = channel_params.broadcaster_pubkeys();
138140
let counterparty_keys = channel_params.countersignatory_pubkeys();
139141
let broadcaster_htlc_key = chan_utils::derive_public_key(
140-
secp, per_commitment_point, &broadcaster_keys.htlc_basepoint
142+
secp, &self.per_commitment_point, &broadcaster_keys.htlc_basepoint
141143
);
142144
let counterparty_htlc_key = chan_utils::derive_public_key(
143-
secp, per_commitment_point, &counterparty_keys.htlc_basepoint
145+
secp, &self.per_commitment_point, &counterparty_keys.htlc_basepoint
144146
);
145147
let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
146-
secp, per_commitment_point, &counterparty_keys.revocation_basepoint
148+
secp, &self.per_commitment_point, &counterparty_keys.revocation_basepoint
147149
);
148150
chan_utils::get_htlc_redeemscript_with_explicit_keys(
149151
&self.htlc, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), &broadcaster_htlc_key, &counterparty_htlc_key,
@@ -696,15 +698,12 @@ where
696698
let mut signers = HashMap::new();
697699
let mut must_spend = Vec::with_capacity(htlc_descriptors.len());
698700
for htlc_descriptor in htlc_descriptors {
699-
let signer = signers.entry(htlc_descriptor.channel_keys_id)
701+
signers.entry(htlc_descriptor.channel_keys_id)
700702
.or_insert_with(||
701703
self.signer_provider.derive_channel_signer(
702704
htlc_descriptor.channel_value_satoshis, htlc_descriptor.channel_keys_id,
703705
)
704706
);
705-
let per_commitment_point = signer.get_per_commitment_point(
706-
htlc_descriptor.per_commitment_number, &self.secp
707-
);
708707

709708
let htlc_input = htlc_descriptor.unsigned_tx_input();
710709
must_spend.push(Input {
@@ -716,7 +715,7 @@ where
716715
},
717716
});
718717
tx.input.push(htlc_input);
719-
let htlc_output = htlc_descriptor.tx_output(&per_commitment_point, &self.secp);
718+
let htlc_output = htlc_descriptor.tx_output(&self.secp);
720719
tx.output.push(htlc_output);
721720
}
722721

@@ -743,10 +742,7 @@ where
743742
let htlc_sig = signer.sign_holder_htlc_transaction(
744743
&htlc_tx, idx, htlc_descriptor, &self.secp
745744
)?;
746-
let per_commitment_point = signer.get_per_commitment_point(
747-
htlc_descriptor.per_commitment_number, &self.secp
748-
);
749-
let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &self.secp);
745+
let witness_script = htlc_descriptor.witness_script(&self.secp);
750746
htlc_tx.input[idx].witness = htlc_descriptor.tx_input_witness(&htlc_sig, &witness_script);
751747
}
752748

lightning/src/ln/monitor_tests.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,12 +1781,9 @@ fn do_test_monitor_rebroadcast_pending_claims(anchors: bool) {
17811781
let signer = nodes[0].keys_manager.derive_channel_keys(
17821782
descriptor.channel_value_satoshis, &descriptor.channel_keys_id,
17831783
);
1784-
let per_commitment_point = signer.get_per_commitment_point(
1785-
descriptor.per_commitment_number, &secp
1786-
);
1787-
tx.output.push(descriptor.tx_output(&per_commitment_point, &secp));
1784+
tx.output.push(descriptor.tx_output(&secp));
17881785
let our_sig = signer.sign_holder_htlc_transaction(&mut tx, 0, &descriptor, &secp).unwrap();
1789-
let witness_script = descriptor.witness_script(&per_commitment_point, &secp);
1786+
let witness_script = descriptor.witness_script(&secp);
17901787
tx.input[0].witness = descriptor.tx_input_witness(&our_sig, &witness_script);
17911788
target_feerate_sat_per_1000_weight as u64
17921789
} else { panic!("unexpected event"); };
@@ -1943,10 +1940,6 @@ fn test_yield_anchors_events() {
19431940
Event::BumpTransaction(BumpTransactionEvent::HTLCResolution { htlc_descriptors, tx_lock_time, .. }) => {
19441941
assert_eq!(htlc_descriptors.len(), 1);
19451942
let htlc_descriptor = &htlc_descriptors[0];
1946-
let signer = nodes[0].keys_manager.derive_channel_keys(
1947-
htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
1948-
);
1949-
let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
19501943
let mut htlc_tx = Transaction {
19511944
version: 2,
19521945
lock_time: tx_lock_time,
@@ -1955,15 +1948,18 @@ fn test_yield_anchors_events() {
19551948
TxIn { ..Default::default() } // Fee input
19561949
],
19571950
output: vec![
1958-
htlc_descriptor.tx_output(&per_commitment_point, &secp), // HTLC output
1951+
htlc_descriptor.tx_output(&secp), // HTLC output
19591952
TxOut { // Fee input change
19601953
value: Amount::ONE_BTC.to_sat(),
19611954
script_pubkey: Script::new_op_return(&[]),
19621955
}
19631956
]
19641957
};
1958+
let signer = nodes[0].keys_manager.derive_channel_keys(
1959+
htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
1960+
);
19651961
let our_sig = signer.sign_holder_htlc_transaction(&mut htlc_tx, 0, htlc_descriptor, &secp).unwrap();
1966-
let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &secp);
1962+
let witness_script = htlc_descriptor.witness_script(&secp);
19671963
htlc_tx.input[0].witness = htlc_descriptor.tx_input_witness(&our_sig, &witness_script);
19681964
htlc_txs.push(htlc_tx);
19691965
},
@@ -2227,12 +2223,8 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
22272223
assert_eq!(htlc_descriptors.len(), 2);
22282224
for htlc_descriptor in &htlc_descriptors {
22292225
assert!(!htlc_descriptor.htlc.offered);
2230-
let signer = nodes[1].keys_manager.derive_channel_keys(
2231-
htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
2232-
);
2233-
let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
22342226
htlc_tx.input.push(htlc_descriptor.unsigned_tx_input());
2235-
htlc_tx.output.push(htlc_descriptor.tx_output(&per_commitment_point, &secp));
2227+
htlc_tx.output.push(htlc_descriptor.tx_output(&secp));
22362228
}
22372229
descriptors.append(&mut htlc_descriptors);
22382230
htlc_tx.lock_time = tx_lock_time;
@@ -2246,8 +2238,7 @@ fn test_anchors_aggregated_revoked_htlc_tx() {
22462238
htlc_descriptor.channel_value_satoshis, &htlc_descriptor.channel_keys_id
22472239
);
22482240
let our_sig = signer.sign_holder_htlc_transaction(&htlc_tx, htlc_input_idx, &htlc_descriptor, &secp).unwrap();
2249-
let per_commitment_point = signer.get_per_commitment_point(htlc_descriptor.per_commitment_number, &secp);
2250-
let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &secp);
2241+
let witness_script = htlc_descriptor.witness_script(&secp);
22512242
htlc_tx.input[htlc_input_idx].witness = htlc_descriptor.tx_input_witness(&our_sig, &witness_script);
22522243
}
22532244
let fee_utxo_sig = {

lightning/src/sign/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,15 +1030,12 @@ impl EcdsaChannelSigner for InMemorySigner {
10301030
&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
10311031
secp_ctx: &Secp256k1<secp256k1::All>
10321032
) -> Result<Signature, ()> {
1033-
let per_commitment_point = self.get_per_commitment_point(
1034-
htlc_descriptor.per_commitment_number, &secp_ctx
1035-
);
1036-
let witness_script = htlc_descriptor.witness_script(&per_commitment_point, secp_ctx);
1033+
let witness_script = htlc_descriptor.witness_script(secp_ctx);
10371034
let sighash = &sighash::SighashCache::new(&*htlc_tx).segwit_signature_hash(
10381035
input, &witness_script, htlc_descriptor.htlc.amount_msat / 1000, EcdsaSighashType::All
10391036
).map_err(|_| ())?;
10401037
let our_htlc_private_key = chan_utils::derive_private_key(
1041-
&secp_ctx, &per_commitment_point, &self.htlc_base_key
1038+
&secp_ctx, &htlc_descriptor.per_commitment_point, &self.htlc_base_key
10421039
);
10431040
Ok(sign_with_aux_rand(&secp_ctx, &hash_to_message!(sighash), &our_htlc_private_key, &self))
10441041
}

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,8 @@ impl EcdsaChannelSigner for EnforcingSigner {
209209
&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
210210
secp_ctx: &Secp256k1<secp256k1::All>
211211
) -> Result<Signature, ()> {
212-
let per_commitment_point = self.get_per_commitment_point(htlc_descriptor.per_commitment_number, secp_ctx);
213212
assert_eq!(htlc_tx.input[input], htlc_descriptor.unsigned_tx_input());
214-
assert_eq!(htlc_tx.output[input], htlc_descriptor.tx_output(&per_commitment_point, secp_ctx));
213+
assert_eq!(htlc_tx.output[input], htlc_descriptor.tx_output(secp_ctx));
215214
Ok(self.inner.sign_holder_htlc_transaction(htlc_tx, input, htlc_descriptor, secp_ctx).unwrap())
216215
}
217216

0 commit comments

Comments
 (0)