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 ;
@@ -226,7 +226,6 @@ impl Writeable for ReceiveTlvs {
226
226
227
227
impl < ' a > Writeable for BlindedPaymentTlvsRef < ' a > {
228
228
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
229
- // TODO: write padding
230
229
match self {
231
230
Self :: Forward ( tlvs) => tlvs. write ( w) ?,
232
231
Self :: Receive ( tlvs) => tlvs. write ( w) ?,
@@ -235,6 +234,19 @@ impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {
235
234
}
236
235
}
237
236
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
+
238
250
impl Readable for BlindedPaymentTlvs {
239
251
fn read < R : io:: Read > ( r : & mut R ) -> Result < Self , DecodeError > {
240
252
_init_and_read_tlv_stream ! ( r, {
@@ -276,9 +288,18 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
276
288
) -> Result < Vec < BlindedHop > , secp256k1:: Error > {
277
289
let pks = intermediate_nodes. iter ( ) . map ( |node| & node. node_id )
278
290
. 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)
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