14
14
use bitcoin:: secp256k1:: { self , PublicKey , Secp256k1 , SecretKey } ;
15
15
16
16
use crate :: blinded_path:: { BlindedHop , BlindedPath , IntroductionNode , NodeIdLookUp } ;
17
- use crate :: blinded_path:: utils;
17
+ use crate :: blinded_path:: utils:: { self , Padding } ;
18
18
use crate :: crypto:: streams:: ChaChaPolyReadAdapter ;
19
19
use crate :: io;
20
20
use crate :: io:: Cursor ;
@@ -86,6 +86,7 @@ pub(crate) enum BlindedPaymentTlvs {
86
86
}
87
87
88
88
// Used to include forward and receive TLVs in the same iterator for encoding.
89
+ #[ derive( Clone ) ]
89
90
enum BlindedPaymentTlvsRef < ' a > {
90
91
Forward ( & ' a ForwardTlvs ) ,
91
92
Receive ( & ' a ReceiveTlvs ) ,
@@ -226,7 +227,6 @@ impl Writeable for ReceiveTlvs {
226
227
227
228
impl < ' a > Writeable for BlindedPaymentTlvsRef < ' a > {
228
229
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
229
- // TODO: write padding
230
230
match self {
231
231
Self :: Forward ( tlvs) => tlvs. write ( w) ?,
232
232
Self :: Receive ( tlvs) => tlvs. write ( w) ?,
@@ -235,6 +235,19 @@ impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {
235
235
}
236
236
}
237
237
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
+
238
251
impl Readable for BlindedPaymentTlvs {
239
252
fn read < R : io:: Read > ( r : & mut R ) -> Result < Self , DecodeError > {
240
253
_init_and_read_tlv_stream ! ( r, {
@@ -278,7 +291,15 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
278
291
. chain ( core:: iter:: once ( & payee_node_id) ) ;
279
292
let tlvs = intermediate_nodes. iter ( ) . map ( |node| BlindedPaymentTlvsRef :: Forward ( & node. tlvs ) )
280
293
. 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)
282
303
}
283
304
284
305
// Advance the blinded onion payment path by one hop, so make the second hop into the new
0 commit comments