Skip to content

Commit 7bd8f8c

Browse files
onion_utils: add next_hop_packet_pubkey method
To get the next hop's packet's pubkey. This will be used to DRY onion message forwarding in the upcoming Onion Messages PR lightningdevkit#1503
1 parent 8e5cf75 commit 7bd8f8c

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use bitcoin::blockdata::transaction::Transaction;
2424
use bitcoin::blockdata::constants::genesis_block;
2525
use bitcoin::network::constants::Network;
2626

27-
use bitcoin::hashes::{Hash, HashEngine};
27+
use bitcoin::hashes::Hash;
2828
use bitcoin::hashes::sha256::Hash as Sha256;
2929
use bitcoin::hashes::sha256d::Hash as Sha256dHash;
3030
use bitcoin::hash_types::{BlockHash, Txid};
@@ -2164,22 +2164,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
21642164
}
21652165
},
21662166
onion_utils::Hop::Forward { next_hop_data, next_hop_hmac, new_packet_bytes } => {
2167-
let mut new_pubkey = msg.onion_routing_packet.public_key.unwrap();
2168-
2169-
let blinding_factor = {
2170-
let mut sha = Sha256::engine();
2171-
sha.input(&new_pubkey.serialize()[..]);
2172-
sha.input(&shared_secret);
2173-
Sha256::from_engine(sha).into_inner()
2174-
};
2175-
2176-
let public_key = if let Err(e) = new_pubkey.mul_assign(&self.secp_ctx, &blinding_factor[..]) {
2177-
Err(e)
2178-
} else { Ok(new_pubkey) };
2179-
2167+
let new_pubkey = msg.onion_routing_packet.public_key.unwrap();
21802168
let outgoing_packet = msgs::OnionPacket {
21812169
version: 0,
2182-
public_key,
2170+
public_key: onion_utils::next_hop_packet_pubkey(&self.secp_ctx, new_pubkey, &shared_secret),
21832171
hop_data: new_packet_bytes,
21842172
hmac: next_hop_hmac.clone(),
21852173
};

lightning/src/ln/onion_utils.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ pub(super) fn gen_ammag_from_shared_secret(shared_secret: &[u8]) -> [u8; 32] {
7474
Hmac::from_engine(hmac).into_inner()
7575
}
7676

77+
pub(super) fn next_hop_packet_pubkey<T: secp256k1::Signing + secp256k1::Verification>(secp_ctx: &Secp256k1<T>, mut packet_pubkey: PublicKey, packet_shared_secret: &[u8; 32]) -> Result<PublicKey, secp256k1::Error> {
78+
let blinding_factor = {
79+
let mut sha = Sha256::engine();
80+
sha.input(&packet_pubkey.serialize()[..]);
81+
sha.input(packet_shared_secret);
82+
Sha256::from_engine(sha).into_inner()
83+
};
84+
85+
packet_pubkey.mul_assign(secp_ctx, &blinding_factor[..]).map(|_| packet_pubkey)
86+
}
87+
7788
// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
7889
#[inline]
7990
pub(super) fn construct_onion_keys_callback<T: secp256k1::Signing, FType: FnMut(SharedSecret, [u8; 32], PublicKey, &RouteHop, usize)> (secp_ctx: &Secp256k1<T>, path: &Vec<RouteHop>, session_priv: &SecretKey, mut callback: FType) -> Result<(), secp256k1::Error> {

0 commit comments

Comments
 (0)