Skip to content

Commit 1da2e46

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 cd2c71b commit 1da2e46

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
1717
use crate::prelude::*;
1818

1919
use crate::blinded_path::{BlindedHop, BlindedPath, IntroductionNode, NextMessageHop, NodeIdLookUp};
20-
use crate::blinded_path::utils;
20+
use crate::blinded_path::utils::{self, WithPadding};
2121
use crate::io;
2222
use crate::io::Cursor;
2323
use crate::ln::channelmanager::PaymentId;
@@ -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+
.map(|tlv| tlv.serialized_length())
192+
.max()
193+
.unwrap_or(0);
194+
195+
let length_tlvs = tlvs.map(|tlvs| WithPadding { max_length, tlvs });
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/onion_message/packet.rs

Lines changed: 1 addition & 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),

0 commit comments

Comments
 (0)