Skip to content

Commit 15ba6d4

Browse files
Generalize next_hop_packet_pubkey onion util
1 parent ec8edf7 commit 15ba6d4

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

lightning/src/ln/channelmanager.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2777,9 +2777,9 @@ where
27772777
outgoing_cltv_value,
27782778
}, ..
27792779
} => {
2780-
let next_pk = onion_utils::next_hop_packet_pubkey(&self.secp_ctx,
2780+
let next_packet_pk = onion_utils::next_hop_pubkey(&self.secp_ctx,
27812781
msg.onion_routing_packet.public_key.unwrap(), &shared_secret);
2782-
(short_channel_id, amt_to_forward, outgoing_cltv_value, Some(next_pk))
2782+
(short_channel_id, amt_to_forward, outgoing_cltv_value, Some(next_packet_pk))
27832783
},
27842784
// We'll do receive checks in [`Self::construct_pending_htlc_info`] so we have access to the
27852785
// 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)