Skip to content

Commit 90bcbb2

Browse files
committed
Introduce Padding for blinded message paths.
This commit make the size of each onion packet in a blinded path of the same size, that is equal to the size of that largest onion packet.
1 parent cb18ac3 commit 90bcbb2

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub struct ForwardNode {
4444

4545
/// TLVs to encode in an intermediate onion message packet's hop data. When provided in a blinded
4646
/// route, they are encoded into [`BlindedHop::encrypted_payload`].
47+
#[derive(Clone)]
4748
pub(crate) struct ForwardTlvs {
4849
/// The next hop in the onion message's path.
4950
pub(crate) next_hop: NextMessageHop,
@@ -53,6 +54,7 @@ pub(crate) struct ForwardTlvs {
5354
}
5455

5556
/// Similar to [`ForwardTlvs`], but these TLVs are for the final node.
57+
#[derive(Clone)]
5658
pub(crate) struct ReceiveTlvs {
5759
/// If `context` is `Some`, it is used to identify the blinded path that this onion message is
5860
/// sending to. This is useful for receivers to check that said blinded path is being used in
@@ -66,7 +68,6 @@ impl Writeable for ForwardTlvs {
6668
NextMessageHop::NodeId(pubkey) => (Some(pubkey), None),
6769
NextMessageHop::ShortChannelId(scid) => (None, Some(scid)),
6870
};
69-
// TODO: write padding
7071
encode_tlv_stream!(writer, {
7172
(2, short_channel_id, option),
7273
(4, next_node_id, option),
@@ -78,7 +79,6 @@ impl Writeable for ForwardTlvs {
7879

7980
impl Writeable for ReceiveTlvs {
8081
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
81-
// TODO: write padding
8282
encode_tlv_stream!(writer, {
8383
(65537, self.context, option),
8484
});
@@ -187,7 +187,14 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
187187
.map(|next_hop| ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None }))
188188
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs{ context: Some(context) })));
189189

190-
utils::construct_blinded_hops(secp_ctx, pks, tlvs, session_priv)
190+
let max_length = tlvs.clone()
191+
.max_by_key(|c| c.serialized_length())
192+
.map(|c| c.serialized_length())
193+
.unwrap_or(0);
194+
195+
let length_tlvs = tlvs.map(|tlv| (max_length, tlv));
196+
197+
utils::construct_blinded_hops(secp_ctx, pks, length_tlvs, session_priv)
191198
}
192199

193200
// 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ impl Padding {
151151
}
152152

153153
impl Readable for Padding {
154+
/// Reads and discards the padding data.
154155
#[inline]
155156
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
156157
loop {

lightning/src/onion_message/packet.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ for Payload<ParsedOnionMessageContents<<H as CustomOnionMessageHandler>::CustomM
306306
/// or received. Thus we read a `ControlTlvs` rather than reading a [`ForwardTlvs`] or
307307
/// [`ReceiveTlvs`] directly. Also useful on the encoding side to keep forward and receive TLVs in
308308
/// the same iterator.
309+
#[derive(Clone)]
309310
pub(crate) enum ControlTlvs {
310311
/// This onion message is intended to be forwarded.
311312
Forward(ForwardTlvs),
@@ -359,3 +360,16 @@ impl Writeable for ControlTlvs {
359360
}
360361
}
361362
}
363+
364+
impl Writeable for (usize, ControlTlvs) {
365+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
366+
let length = self.0 - self.1.serialized_length();
367+
let padding = Some(Padding::new(length));
368+
369+
encode_tlv_stream!(writer, {
370+
(1, padding, option)
371+
});
372+
373+
self.1.write(writer)
374+
}
375+
}

0 commit comments

Comments
 (0)