Skip to content

Commit d30a395

Browse files
committed
Introduce Padding for BlindedMessage Paths.
A note of Compact Blinded Paths: Compact Blinded paths are intended to be as short as possible. So to maintain there compactness, we don't apply padding to them.
1 parent 6f021da commit d30a395

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey};
1414
#[allow(unused_imports)]
1515
use crate::prelude::*;
1616

17-
use crate::blinded_path::utils;
17+
use crate::blinded_path::utils::{self, BlindedPathWithPadding};
1818
use crate::blinded_path::{BlindedHop, BlindedPath, Direction, IntroductionNode, NodeIdLookUp};
1919
use crate::crypto::streams::ChaChaPolyReadAdapter;
2020
use crate::io;
@@ -265,7 +265,6 @@ impl Writeable for ForwardTlvs {
265265
NextMessageHop::NodeId(pubkey) => (Some(pubkey), None),
266266
NextMessageHop::ShortChannelId(scid) => (None, Some(scid)),
267267
};
268-
// TODO: write padding
269268
encode_tlv_stream!(writer, {
270269
(2, short_channel_id, option),
271270
(4, next_node_id, option),
@@ -277,7 +276,6 @@ impl Writeable for ForwardTlvs {
277276

278277
impl Writeable for ReceiveTlvs {
279278
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
280-
// TODO: write padding
281279
encode_tlv_stream!(writer, {
282280
(65537, self.context, option),
283281
});
@@ -490,6 +488,10 @@ impl_writeable_tlv_based!(DNSResolverContext, {
490488
(0, nonce, required),
491489
});
492490

491+
/// Represents the padding round off size (in bytes) that is used
492+
/// to pad message blinded path's [`BlindedHop`]
493+
pub(crate) const MESSAGE_PADDING_ROUND_OFF: usize = 100;
494+
493495
/// Construct blinded onion message hops for the given `intermediate_nodes` and `recipient_node_id`.
494496
pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
495497
secp_ctx: &Secp256k1<T>, intermediate_nodes: &[MessageForwardNode],
@@ -499,6 +501,8 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
499501
.iter()
500502
.map(|node| node.node_id)
501503
.chain(core::iter::once(recipient_node_id));
504+
let is_compact = intermediate_nodes.iter().any(|node| node.short_channel_id.is_some());
505+
502506
let tlvs = pks
503507
.clone()
504508
.skip(1) // The first node's TLVs contains the next node's pubkey
@@ -512,7 +516,15 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
512516
})
513517
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs { context: Some(context) })));
514518

515-
let path = pks.zip(tlvs);
516-
517-
utils::construct_blinded_hops(secp_ctx, path, session_priv)
519+
if is_compact {
520+
let path = pks.zip(tlvs);
521+
utils::construct_blinded_hops(secp_ctx, path, session_priv)
522+
} else {
523+
let path =
524+
pks.zip(tlvs.map(|tlv| BlindedPathWithPadding {
525+
tlvs: tlv,
526+
round_off: MESSAGE_PADDING_ROUND_OFF,
527+
}));
528+
utils::construct_blinded_hops(secp_ctx, path, session_priv)
529+
}
518530
}

lightning/src/blinded_path/payment.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,6 @@ impl Writeable for UnauthenticatedReceiveTlvs {
494494

495495
impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {
496496
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
497-
// TODO: write padding
498497
match self {
499498
Self::Forward(tlvs) => tlvs.write(w)?,
500499
Self::Receive(tlvs) => tlvs.write(w)?,

0 commit comments

Comments
 (0)