@@ -20,7 +20,7 @@ use crate::ln::msgs::DecodeError;
2020use crate :: ln:: onion_utils;
2121use crate :: onion_message:: messenger:: Destination ;
2222use crate :: crypto:: streams:: ChaChaPolyWriteAdapter ;
23- use crate :: util:: ser:: { Readable , Writeable } ;
23+ use crate :: util:: ser:: { Readable , Writeable , Writer } ;
2424
2525use crate :: io;
2626
@@ -135,17 +135,45 @@ fn encrypt_payload<P: Writeable>(payload: P, encrypted_tlvs_rho: [u8; 32]) -> Ve
135135 write_adapter. encode ( )
136136}
137137
138- /// Blinded path encrypted payloads may be padded to ensure they are equal length.
139- ///
140- /// Reads padding to the end, ignoring what's read.
141- pub ( crate ) struct Padding { }
138+ /// Represents optional padding for encrypted payloads.
139+ /// Padding is used to ensure payloads have a consistent length.
140+ pub ( crate ) struct Padding {
141+ length : usize ,
142+ }
143+
144+ impl Padding {
145+ /// Creates a new [`Padding`] instance with a specified size.
146+ /// Use this method when defining the padding size before writing
147+ /// an encrypted payload.
148+ pub fn new ( length : usize ) -> Self {
149+ Self { length }
150+ }
151+ }
152+
142153impl Readable for Padding {
154+ /// Reads and discards the padding data.
143155 #[ inline]
144156 fn read < R : io:: Read > ( reader : & mut R ) -> Result < Self , DecodeError > {
145157 loop {
146158 let mut buf = [ 0 ; 8192 ] ;
147159 if reader. read ( & mut buf[ ..] ) ? == 0 { break ; }
148160 }
149- Ok ( Self { } )
161+ Ok ( Self :: new ( 0 ) )
162+ }
163+ }
164+
165+ impl Writeable for Padding {
166+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
167+ const BUFFER_SIZE : usize = 1024 ;
168+ let buffer = [ 0u8 ; BUFFER_SIZE ] ;
169+
170+ let mut remaining = self . length ;
171+ loop {
172+ let to_write = core:: cmp:: min ( remaining, BUFFER_SIZE ) ;
173+ writer. write_all ( & buffer[ ..to_write] ) ?;
174+ remaining -= to_write;
175+ if remaining == 0 { break ; }
176+ }
177+ Ok ( ( ) )
150178 }
151179}
0 commit comments