Skip to content

Commit 047a878

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 5d7fac2 commit 047a878

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::prelude::*;
1717
use bitcoin::hashes::hmac::Hmac;
1818
use bitcoin::hashes::sha256::Hash as Sha256;
1919
use crate::blinded_path::{BlindedHop, BlindedPath, Direction, IntroductionNode, NodeIdLookUp};
20-
use crate::blinded_path::utils;
20+
use crate::blinded_path::utils::{self, BlindedPathWithPadding};
2121
use crate::io;
2222
use crate::io::Cursor;
2323
use crate::ln::channelmanager::PaymentId;
@@ -250,7 +250,6 @@ impl Writeable for ForwardTlvs {
250250
NextMessageHop::NodeId(pubkey) => (Some(pubkey), None),
251251
NextMessageHop::ShortChannelId(scid) => (None, Some(scid)),
252252
};
253-
// TODO: write padding
254253
encode_tlv_stream!(writer, {
255254
(2, short_channel_id, option),
256255
(4, next_node_id, option),
@@ -262,7 +261,6 @@ impl Writeable for ForwardTlvs {
262261

263262
impl Writeable for ReceiveTlvs {
264263
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
265-
// TODO: write padding
266264
encode_tlv_stream!(writer, {
267265
(65537, self.context, option),
268266
});
@@ -453,13 +451,19 @@ impl_writeable_tlv_based!(DNSResolverContext, {
453451
(0, nonce, required),
454452
});
455453

454+
/// Represents the padding round off size (in bytes) that is used
455+
/// to pad message blinded path's [`BlindedHop`]
456+
pub(crate) const MESSAGE_PADDING_ROUND_OFF: usize = 100;
457+
456458
/// Construct blinded onion message hops for the given `intermediate_nodes` and `recipient_node_id`.
457459
pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
458460
secp_ctx: &Secp256k1<T>, intermediate_nodes: &[MessageForwardNode],
459461
recipient_node_id: PublicKey, context: MessageContext, session_priv: &SecretKey,
460462
) -> Result<Vec<BlindedHop>, secp256k1::Error> {
461463
let pks = intermediate_nodes.iter().map(|node| node.node_id)
462464
.chain(core::iter::once(recipient_node_id));
465+
let is_compact = intermediate_nodes.iter().any(|node| node.short_channel_id.is_some());
466+
463467
let tlvs = pks.clone()
464468
.skip(1) // The first node's TLVs contains the next node's pubkey
465469
.zip(intermediate_nodes.iter().map(|node| node.short_channel_id))
@@ -470,8 +474,12 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
470474
.map(|next_hop| ControlTlvs::Forward(ForwardTlvs { next_hop, next_blinding_override: None }))
471475
.chain(core::iter::once(ControlTlvs::Receive(ReceiveTlvs{ context: Some(context) })));
472476

473-
let path = pks.zip(tlvs);
474-
475-
utils::construct_blinded_hops(secp_ctx, path, session_priv)
477+
if is_compact {
478+
let path = pks.zip(tlvs);
479+
utils::construct_blinded_hops(secp_ctx, path, session_priv)
480+
} else {
481+
let path = pks.zip(tlvs.map(|tlv| BlindedPathWithPadding { tlvs: tlv, round_off: MESSAGE_PADDING_ROUND_OFF }));
482+
utils::construct_blinded_hops(secp_ctx, path, session_priv)
483+
}
476484
}
477485

lightning/src/blinded_path/payment.rs

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

446446
impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {
447447
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
448-
// TODO: write padding
449448
match self {
450449
Self::Forward(tlvs) => tlvs.write(w)?,
451450
Self::Receive(tlvs) => tlvs.write(w)?,

0 commit comments

Comments
 (0)