Skip to content

Commit d224f98

Browse files
Simplify onion message blinded hop construction
Also adds a util for general blinded hop creation to be reused for blinded payment paths.
1 parent cf64e3f commit d224f98

File tree

3 files changed

+42
-31
lines changed

3 files changed

+42
-31
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,14 @@ impl Writeable for ReceiveTlvs {
5757
pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
5858
secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], session_priv: &SecretKey
5959
) -> Result<Vec<BlindedHop>, secp256k1::Error> {
60-
let mut blinded_hops = Vec::with_capacity(unblinded_path.len());
60+
let blinded_tlvs = unblinded_path.iter()
61+
.skip(1) // The first node's TLVs contains the next node's pubkey
62+
.map(|pk| {
63+
ControlTlvs::Forward(ForwardTlvs { next_node_id: *pk, next_blinding_override: None })
64+
})
65+
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { path_id: None })));
6166

62-
let mut prev_ss_and_blinded_node_id = None;
63-
utils::construct_keys_callback(secp_ctx, unblinded_path.iter(), None, session_priv,
64-
|blinded_node_id, _, _, encrypted_payload_ss, unblinded_pk, _| {
65-
if let Some((prev_ss, prev_blinded_node_id)) = prev_ss_and_blinded_node_id {
66-
if let Some(pk) = unblinded_pk {
67-
let payload = ForwardTlvs {
68-
next_node_id: pk,
69-
next_blinding_override: None,
70-
};
71-
blinded_hops.push(BlindedHop {
72-
blinded_node_id: prev_blinded_node_id,
73-
encrypted_payload: utils::encrypt_payload(payload, prev_ss),
74-
});
75-
} else { debug_assert!(false); }
76-
}
77-
prev_ss_and_blinded_node_id = Some((encrypted_payload_ss, blinded_node_id));
78-
})?;
79-
80-
if let Some((final_ss, final_blinded_node_id)) = prev_ss_and_blinded_node_id {
81-
let final_payload = ReceiveTlvs { path_id: None };
82-
blinded_hops.push(BlindedHop {
83-
blinded_node_id: final_blinded_node_id,
84-
encrypted_payload: utils::encrypt_payload(final_payload, final_ss),
85-
});
86-
} else { debug_assert!(false) }
87-
88-
Ok(blinded_hops)
67+
utils::construct_blinded_hops(secp_ctx, unblinded_path.iter(), blinded_tlvs, session_priv)
8968
}
9069

9170
// Advance the blinded onion message path by one hop, so make the second hop into the new

lightning/src/blinded_path/utils.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use bitcoin::hashes::sha256::Hash as Sha256;
1515
use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey, Scalar};
1616
use bitcoin::secp256k1::ecdh::SharedSecret;
1717

18-
use super::BlindedPath;
18+
use super::{BlindedHop, BlindedPath};
1919
use crate::ln::msgs::DecodeError;
2020
use crate::ln::onion_utils;
2121
use crate::onion_message::Destination;
@@ -105,8 +105,30 @@ where
105105
Ok(())
106106
}
107107

108+
// Panics if `unblinded_tlvs` length is less than `unblinded_pks` length
109+
pub(super) fn construct_blinded_hops<'a, T, I1, I2>(
110+
secp_ctx: &Secp256k1<T>, unblinded_pks: I1, mut unblinded_tlvs: I2, session_priv: &SecretKey
111+
) -> Result<Vec<BlindedHop>, secp256k1::Error>
112+
where
113+
T: secp256k1::Signing + secp256k1::Verification,
114+
I1: ExactSizeIterator<Item=&'a PublicKey>,
115+
I2: Iterator,
116+
I2::Item: Writeable
117+
{
118+
let mut blinded_hops = Vec::with_capacity(unblinded_pks.len());
119+
construct_keys_callback(
120+
secp_ctx, unblinded_pks, None, session_priv,
121+
|blinded_node_id, _, _, encrypted_payload_rho, _, _| {
122+
blinded_hops.push(BlindedHop {
123+
blinded_node_id,
124+
encrypted_payload: encrypt_payload(unblinded_tlvs.next().unwrap(), encrypted_payload_rho),
125+
});
126+
})?;
127+
Ok(blinded_hops)
128+
}
129+
108130
/// Encrypt TLV payload to be used as a [`crate::blinded_path::BlindedHop::encrypted_payload`].
109-
pub(super) fn encrypt_payload<P: Writeable>(payload: P, encrypted_tlvs_ss: [u8; 32]) -> Vec<u8> {
131+
fn encrypt_payload<P: Writeable>(payload: P, encrypted_tlvs_ss: [u8; 32]) -> Vec<u8> {
110132
let mut writer = VecWriter(Vec::new());
111133
let write_adapter = ChaChaPolyWriteAdapter::new(encrypted_tlvs_ss, &payload);
112134
write_adapter.write(&mut writer).expect("In-memory writes cannot fail");

lightning/src/onion_message/packet.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ ReadableArgs<(SharedSecret, &H, &L)> for Payload<<H as CustomOnionMessageHandler
265265

266266
/// When reading a packet off the wire, we don't know a priori whether the packet is to be forwarded
267267
/// or received. Thus we read a ControlTlvs rather than reading a ForwardControlTlvs or
268-
/// ReceiveControlTlvs directly.
268+
/// ReceiveControlTlvs directly. Also useful on the encoding side to keep forward and receive TLVs
269+
/// in the same iterator.
269270
pub(crate) enum ControlTlvs {
270271
/// This onion message is intended to be forwarded.
271272
Forward(ForwardTlvs),
@@ -304,3 +305,12 @@ impl Readable for ControlTlvs {
304305
Ok(payload_fmt)
305306
}
306307
}
308+
309+
impl Writeable for ControlTlvs {
310+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
311+
match self {
312+
Self::Forward(tlvs) => tlvs.write(w),
313+
Self::Receive(tlvs) => tlvs.write(w),
314+
}
315+
}
316+
}

0 commit comments

Comments
 (0)