1414use bitcoin:: secp256k1:: { self , PublicKey , Secp256k1 , SecretKey } ;
1515
1616use crate :: blinded_path:: { BlindedHop , BlindedPath , IntroductionNode , NodeIdLookUp } ;
17- use crate :: blinded_path:: utils;
17+ use crate :: blinded_path:: utils:: { self , Padding } ;
1818use crate :: crypto:: streams:: ChaChaPolyReadAdapter ;
1919use crate :: io;
2020use crate :: io:: Cursor ;
@@ -86,6 +86,7 @@ pub(crate) enum BlindedPaymentTlvs {
8686}
8787
8888// Used to include forward and receive TLVs in the same iterator for encoding.
89+ #[ derive( Clone ) ]
8990enum BlindedPaymentTlvsRef < ' a > {
9091 Forward ( & ' a ForwardTlvs ) ,
9192 Receive ( & ' a ReceiveTlvs ) ,
@@ -226,7 +227,6 @@ impl Writeable for ReceiveTlvs {
226227
227228impl < ' a > Writeable for BlindedPaymentTlvsRef < ' a > {
228229 fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
229- // TODO: write padding
230230 match self {
231231 Self :: Forward ( tlvs) => tlvs. write ( w) ?,
232232 Self :: Receive ( tlvs) => tlvs. write ( w) ?,
@@ -235,6 +235,19 @@ impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {
235235 }
236236}
237237
238+ impl < ' a > Writeable for ( usize , BlindedPaymentTlvsRef < ' a > ) {
239+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
240+ let length = self . 0 - self . 1 . serialized_length ( ) ;
241+ let padding = Some ( Padding :: new ( length) ) ;
242+
243+ encode_tlv_stream ! ( writer, {
244+ ( 1 , padding, option)
245+ } ) ;
246+
247+ self . 1 . write ( writer)
248+ }
249+ }
250+
238251impl Readable for BlindedPaymentTlvs {
239252 fn read < R : io:: Read > ( r : & mut R ) -> Result < Self , DecodeError > {
240253 _init_and_read_tlv_stream ! ( r, {
@@ -278,7 +291,15 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
278291 . chain ( core:: iter:: once ( & payee_node_id) ) ;
279292 let tlvs = intermediate_nodes. iter ( ) . map ( |node| BlindedPaymentTlvsRef :: Forward ( & node. tlvs ) )
280293 . chain ( core:: iter:: once ( BlindedPaymentTlvsRef :: Receive ( & payee_tlvs) ) ) ;
281- utils:: construct_blinded_hops ( secp_ctx, pks, tlvs, session_priv)
294+
295+ let max_length = tlvs. clone ( )
296+ . max_by_key ( |c| c. serialized_length ( ) )
297+ . map ( |c| c. serialized_length ( ) )
298+ . unwrap_or ( 0 ) ;
299+
300+ let length_tlvs = tlvs. map ( |tlv| ( max_length, tlv) ) ;
301+
302+ utils:: construct_blinded_hops ( secp_ctx, pks, length_tlvs, session_priv)
282303}
283304
284305// Advance the blinded onion payment path by one hop, so make the second hop into the new
0 commit comments