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 ;
@@ -226,7 +226,6 @@ impl Writeable for ReceiveTlvs {
226226
227227impl < ' a > Writeable for BlindedPaymentTlvsRef < ' a > {
228228 fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
229- // TODO: write padding
230229 match self {
231230 Self :: Forward ( tlvs) => tlvs. write ( w) ?,
232231 Self :: Receive ( tlvs) => tlvs. write ( w) ?,
@@ -235,6 +234,19 @@ impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {
235234 }
236235}
237236
237+ impl < ' a > Writeable for ( usize , BlindedPaymentTlvsRef < ' a > ) {
238+ fn write < W : Writer > ( & self , writer : & mut W ) -> Result < ( ) , io:: Error > {
239+ let length = self . 0 - self . 1 . serialized_length ( ) ;
240+ let padding = Some ( Padding :: new ( length) ) ;
241+
242+ encode_tlv_stream ! ( writer, {
243+ ( 1 , padding, option)
244+ } ) ;
245+
246+ self . 1 . write ( writer)
247+ }
248+ }
249+
238250impl Readable for BlindedPaymentTlvs {
239251 fn read < R : io:: Read > ( r : & mut R ) -> Result < Self , DecodeError > {
240252 _init_and_read_tlv_stream ! ( r, {
@@ -276,9 +288,18 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
276288) -> Result < Vec < BlindedHop > , secp256k1:: Error > {
277289 let pks = intermediate_nodes. iter ( ) . map ( |node| & node. node_id )
278290 . chain ( core:: iter:: once ( & payee_node_id) ) ;
279- let tlvs = intermediate_nodes. iter ( ) . map ( |node| BlindedPaymentTlvsRef :: Forward ( & node. tlvs ) )
280- . chain ( core:: iter:: once ( BlindedPaymentTlvsRef :: Receive ( & payee_tlvs) ) ) ;
281- utils:: construct_blinded_hops ( secp_ctx, pks, tlvs, session_priv)
291+ let tlvs: Vec < BlindedPaymentTlvsRef > = intermediate_nodes. iter ( ) . map ( |node| BlindedPaymentTlvsRef :: Forward ( & node. tlvs ) )
292+ . chain ( core:: iter:: once ( BlindedPaymentTlvsRef :: Receive ( & payee_tlvs) ) )
293+ . collect ( ) ;
294+
295+ let max_length = tlvs. iter ( )
296+ . max_by_key ( |c| c. serialized_length ( ) )
297+ . map ( |c| c. serialized_length ( ) )
298+ . unwrap_or ( 0 ) ;
299+
300+ let length_tlvs = tlvs. into_iter ( ) . map ( move |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