Skip to content

Commit 80a949d

Browse files
Generalize next_hop_packet_pubkey onion util
Useful for generating a next hop blinding point when forwarding a blinded payment.
1 parent f9bbcf0 commit 80a949d

File tree

4 files changed

+17
-27
lines changed

4 files changed

+17
-27
lines changed

lightning/src/blinded_path/mod.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
1212
pub(crate) mod utils;
1313

14-
use bitcoin::hashes::{Hash, HashEngine};
15-
use bitcoin::hashes::sha256::Hash as Sha256;
16-
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
14+
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
1715

1816
use crate::sign::{EntropySource, NodeSigner, Recipient};
1917
use crate::onion_message::ControlTlvs;
@@ -97,14 +95,8 @@ impl BlindedPath {
9795
let mut new_blinding_point = match next_blinding_override {
9896
Some(blinding_point) => blinding_point,
9997
None => {
100-
let blinding_factor = {
101-
let mut sha = Sha256::engine();
102-
sha.input(&self.blinding_point.serialize()[..]);
103-
sha.input(control_tlvs_ss.as_ref());
104-
Sha256::from_engine(sha).into_inner()
105-
};
106-
self.blinding_point.mul_tweak(secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap())
107-
.map_err(|_| ())?
98+
onion_utils::next_hop_pubkey(secp_ctx, self.blinding_point,
99+
control_tlvs_ss.as_ref()).map_err(|_| ())?
108100
}
109101
};
110102
mem::swap(&mut self.blinding_point, &mut new_blinding_point);

lightning/src/ln/channelmanager.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2868,9 +2868,9 @@ where
28682868
short_channel_id, amt_to_forward, outgoing_cltv_value
28692869
}, ..
28702870
} => {
2871-
let next_pk = onion_utils::next_hop_packet_pubkey(&self.secp_ctx,
2871+
let next_packet_pk = onion_utils::next_hop_pubkey(&self.secp_ctx,
28722872
msg.onion_routing_packet.public_key.unwrap(), &shared_secret);
2873-
(short_channel_id, amt_to_forward, outgoing_cltv_value, Some(next_pk))
2873+
(short_channel_id, amt_to_forward, outgoing_cltv_value, Some(next_packet_pk))
28742874
},
28752875
// We'll do receive checks in [`Self::construct_pending_htlc_info`] so we have access to the
28762876
// inbound channel's state.

lightning/src/ln/onion_utils.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,18 @@ pub(super) fn gen_pad_from_shared_secret(shared_secret: &[u8]) -> [u8; 32] {
9191
Hmac::from_engine(hmac).into_inner()
9292
}
9393

94-
pub(crate) fn next_hop_packet_pubkey<T: secp256k1::Signing + secp256k1::Verification>(secp_ctx: &Secp256k1<T>, packet_pubkey: PublicKey, packet_shared_secret: &[u8; 32]) -> Result<PublicKey, secp256k1::Error> {
94+
/// Calculates a pubkey for the next hop, such as the next hop's packet pubkey or blinding point.
95+
pub(crate) fn next_hop_pubkey<T: secp256k1::Signing + secp256k1::Verification>(
96+
secp_ctx: &Secp256k1<T>, curr_pubkey: PublicKey, shared_secret: &[u8]
97+
) -> Result<PublicKey, secp256k1::Error> {
9598
let blinding_factor = {
9699
let mut sha = Sha256::engine();
97-
sha.input(&packet_pubkey.serialize()[..]);
98-
sha.input(packet_shared_secret);
100+
sha.input(&curr_pubkey.serialize()[..]);
101+
sha.input(shared_secret);
99102
Sha256::from_engine(sha).into_inner()
100103
};
101104

102-
packet_pubkey.mul_tweak(secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap())
105+
curr_pubkey.mul_tweak(secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap())
103106
}
104107

105108
// can only fail if an intermediary hop has an invalid public key or session_priv is invalid

lightning/src/onion_message/messenger.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ where
490490
// unwrapping the onion layers to get to the final payload. Since we don't have the option
491491
// of creating blinded paths with dummy hops currently, we should be ok to not handle this
492492
// for now.
493-
let new_pubkey = match onion_utils::next_hop_packet_pubkey(&self.secp_ctx, msg.onion_routing_packet.public_key, &onion_decode_ss) {
493+
let new_pubkey = match onion_utils::next_hop_pubkey(&self.secp_ctx, msg.onion_routing_packet.public_key, &onion_decode_ss) {
494494
Ok(pk) => pk,
495495
Err(e) => {
496496
log_trace!(self.logger, "Failed to compute next hop packet pubkey: {}", e);
@@ -507,21 +507,16 @@ where
507507
blinding_point: match next_blinding_override {
508508
Some(blinding_point) => blinding_point,
509509
None => {
510-
let blinding_factor = {
511-
let mut sha = Sha256::engine();
512-
sha.input(&msg.blinding_point.serialize()[..]);
513-
sha.input(control_tlvs_ss.as_ref());
514-
Sha256::from_engine(sha).into_inner()
515-
};
516-
let next_blinding_point = msg.blinding_point;
517-
match next_blinding_point.mul_tweak(&self.secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap()) {
510+
match onion_utils::next_hop_pubkey(
511+
&self.secp_ctx, msg.blinding_point, control_tlvs_ss.as_ref()
512+
) {
518513
Ok(bp) => bp,
519514
Err(e) => {
520515
log_trace!(self.logger, "Failed to compute next blinding point: {}", e);
521516
return
522517
}
523518
}
524-
},
519+
}
525520
},
526521
onion_routing_packet: outgoing_packet,
527522
};

0 commit comments

Comments
 (0)