Skip to content

Commit 5ce2b5e

Browse files
committed
Introduce BlindedPathWithPadding struct
Add a generic `BlindedPathWithPadding` struct to handle padding for `ForwardTlvs` and `ReceiveTlvs` used in `BlindedMessagePath` and `BlindedPaymentPath`. This struct applies padding to the contained TLVs, rounding them off to a specified value. This design provides flexibility in padding TLVs of varying sizes. The `PADDING_ROUND_OFF` value is chosen to be sufficiently large to properly mask the data type of the contained TLVs.
1 parent ff7a6b0 commit 5ce2b5e

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

lightning/src/blinded_path/utils.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,33 @@ impl Writeable for BlindedPathPadding {
228228
Ok(())
229229
}
230230
}
231+
232+
/// Padding storage requires two extra bytes:
233+
/// - One byte for the type.
234+
/// - One byte for the padding length.
235+
/// This constant accounts for that overhead.
236+
const TLV_OVERHEAD: usize = 2;
237+
238+
/// A generic struct that applies padding to blinded path TLVs, rounding their size off to `round_off`
239+
pub(crate) struct BlindedPathWithPadding<T: Writeable> {
240+
pub(crate) tlvs: T,
241+
pub(crate) round_off: usize,
242+
}
243+
244+
impl<T: Writeable> Writeable for BlindedPathWithPadding<T> {
245+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
246+
let tlv_length = self.tlvs.serialized_length();
247+
let total_length = tlv_length + TLV_OVERHEAD;
248+
249+
let padding_length =
250+
(total_length + self.round_off - 1) / self.round_off * self.round_off - total_length;
251+
252+
let padding = Some(BlindedPathPadding::new(padding_length));
253+
254+
encode_tlv_stream!(writer, {
255+
(1, padding, option),
256+
});
257+
258+
self.tlvs.write(writer)
259+
}
260+
}

0 commit comments

Comments
 (0)