Skip to content

Commit d613d02

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 623ae04 commit d613d02

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

lightning/src/blinded_path/utils.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,25 @@ impl Writeable for BlindedPathPadding {
228228
Ok(())
229229
}
230230
}
231+
232+
/// A generic struct that applies padding to blinded path TLVs, rounding their size off to `round_off`
233+
pub(crate) struct BlindedPathWithPadding<T: Writeable> {
234+
pub(crate) tlvs: T,
235+
pub(crate) round_off: usize,
236+
}
237+
238+
impl<T: Writeable> Writeable for BlindedPathWithPadding<T> {
239+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
240+
let length = self.tlvs.serialized_length();
241+
let padding_length =
242+
(length + self.round_off - 1) / self.round_off * self.round_off - length;
243+
244+
let padding = Some(BlindedPathPadding::new(padding_length));
245+
246+
encode_tlv_stream!(writer, {
247+
(1, padding, option),
248+
});
249+
250+
self.tlvs.write(writer)
251+
}
252+
}

0 commit comments

Comments
 (0)