Skip to content

Commit 6e7fd12

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 dd8a77f commit 6e7fd12

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

lightning/src/blinded_path/utils.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,24 @@ impl Writeable for BlindedPathPadding {
200200
Ok(())
201201
}
202202
}
203+
204+
/// A generic struct that applies padding to blinded path TLVs, rounding their size off to `round_off`
205+
pub(crate) struct BlindedPathWithPadding<T: Writeable> {
206+
pub(crate) tlvs: T,
207+
pub(crate) round_off: usize,
208+
}
209+
210+
impl<T:Writeable> Writeable for BlindedPathWithPadding<T> {
211+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
212+
let length = self.tlvs.serialized_length();
213+
let padding_length = (length + self.round_off - 1) / self.round_off * self.round_off - length;
214+
215+
let padding = Some(BlindedPathPadding::new(padding_length));
216+
217+
encode_tlv_stream!(writer, {
218+
(1, padding, option),
219+
});
220+
221+
self.tlvs.write(writer)
222+
}
223+
}

0 commit comments

Comments
 (0)