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 ;
@@ -50,6 +50,8 @@ pub struct ForwardNode {
5050/// Data to construct a [`BlindedHop`] for forwarding a payment.
5151#[ derive( Clone , Debug ) ]
5252pub struct ForwardTlvs {
53+ /// The padding data used to make all packets of a Blinded Path of same size
54+ pub padding : Option < Padding > ,
5355 /// The short channel id this payment should be forwarded out over.
5456 pub short_channel_id : u64 ,
5557 /// Payment parameters for relaying over [`Self::short_channel_id`].
@@ -67,6 +69,8 @@ pub struct ForwardTlvs {
6769/// may not be valid if received by another lightning implementation.
6870#[ derive( Clone , Debug ) ]
6971pub struct ReceiveTlvs {
72+ /// The padding data used to make all packets of a Blinded Path of same size
73+ pub padding : Option < Padding > ,
7074 /// Used to authenticate the sender of a payment to the receiver and tie MPP HTLCs together.
7175 pub payment_secret : PaymentSecret ,
7276 /// Constraints for the receiver of this payment.
@@ -78,13 +82,29 @@ pub struct ReceiveTlvs {
7882/// Data to construct a [`BlindedHop`] for sending a payment over.
7983///
8084/// [`BlindedHop`]: crate::blinded_path::BlindedHop
85+ #[ derive( Clone ) ]
8186pub ( crate ) enum BlindedPaymentTlvs {
8287 /// This blinded payment data is for a forwarding node.
8388 Forward ( ForwardTlvs ) ,
8489 /// This blinded payment data is for the receiving node.
8590 Receive ( ReceiveTlvs ) ,
8691}
8792
93+ impl BlindedPaymentTlvs {
94+ pub ( crate ) fn pad_tlvs ( mut self , max_length : usize ) -> Self {
95+ let length = max_length. checked_sub ( self . serialized_length ( ) ) ;
96+ debug_assert ! ( length. is_some( ) , "Size of this packet should not be larger than the size of largest packet." ) ;
97+ let padding = Some ( Padding :: new ( length. unwrap ( ) ) ) ;
98+
99+ match & mut self {
100+ BlindedPaymentTlvs :: Forward ( tlvs) => tlvs. padding = padding,
101+ BlindedPaymentTlvs :: Receive ( tlvs) => tlvs. padding = padding,
102+ }
103+
104+ self
105+ }
106+ }
107+
88108/// Parameters for relaying over a given [`BlindedHop`].
89109///
90110/// [`BlindedHop`]: crate::blinded_path::BlindedHop
@@ -198,6 +218,7 @@ impl Writeable for ForwardTlvs {
198218 if self . features == BlindedHopFeatures :: empty ( ) { None }
199219 else { Some ( & self . features ) } ;
200220 encode_tlv_stream ! ( w, {
221+ ( 1 , self . padding, option) ,
201222 ( 2 , self . short_channel_id, required) ,
202223 ( 10 , self . payment_relay, required) ,
203224 ( 12 , self . payment_constraints, required) ,
@@ -210,6 +231,7 @@ impl Writeable for ForwardTlvs {
210231impl Writeable for ReceiveTlvs {
211232 fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
212233 encode_tlv_stream ! ( w, {
234+ ( 1 , self . padding, option) ,
213235 ( 12 , self . payment_constraints, required) ,
214236 ( 65536 , self . payment_secret, required) ,
215237 ( 65537 , self . payment_context, required)
@@ -246,6 +268,7 @@ impl Readable for BlindedPaymentTlvs {
246268 return Err ( DecodeError :: InvalidValue )
247269 }
248270 Ok ( BlindedPaymentTlvs :: Forward ( ForwardTlvs {
271+ padding : None ,
249272 short_channel_id,
250273 payment_relay : payment_relay. ok_or ( DecodeError :: InvalidValue ) ?,
251274 payment_constraints : payment_constraints. 0 . unwrap ( ) ,
@@ -254,6 +277,7 @@ impl Readable for BlindedPaymentTlvs {
254277 } else {
255278 if payment_relay. is_some ( ) || features. is_some ( ) { return Err ( DecodeError :: InvalidValue ) }
256279 Ok ( BlindedPaymentTlvs :: Receive ( ReceiveTlvs {
280+ padding : None ,
257281 payment_secret : payment_secret. ok_or ( DecodeError :: InvalidValue ) ?,
258282 payment_constraints : payment_constraints. 0 . unwrap ( ) ,
259283 payment_context : payment_context. 0 . unwrap ( ) ,
@@ -272,7 +296,14 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
272296 let tlvs = intermediate_nodes. iter ( ) . map ( |node| BlindedPaymentTlvs :: Forward ( node. tlvs . clone ( ) ) )
273297 . chain ( core:: iter:: once ( BlindedPaymentTlvs :: Receive ( payee_tlvs) ) ) ;
274298
275- utils:: construct_blinded_hops ( secp_ctx, pks, tlvs, session_priv)
299+ let max_length = tlvs. clone ( )
300+ . map ( |tlv| tlv. serialized_length ( ) )
301+ . max ( )
302+ . unwrap_or ( 0 ) ;
303+
304+ let length_tlvs = tlvs. map ( |tlv| tlv. pad_tlvs ( max_length) ) ;
305+
306+ utils:: construct_blinded_hops ( secp_ctx, pks, length_tlvs, session_priv)
276307}
277308
278309// Advance the blinded onion payment path by one hop, so make the second hop into the new
@@ -484,6 +515,7 @@ mod tests {
484515 let intermediate_nodes = vec ! [ ForwardNode {
485516 node_id: dummy_pk,
486517 tlvs: ForwardTlvs {
518+ padding: None ,
487519 short_channel_id: 0 ,
488520 payment_relay: PaymentRelay {
489521 cltv_expiry_delta: 144 ,
@@ -500,6 +532,7 @@ mod tests {
500532 } , ForwardNode {
501533 node_id: dummy_pk,
502534 tlvs: ForwardTlvs {
535+ padding: None ,
503536 short_channel_id: 0 ,
504537 payment_relay: PaymentRelay {
505538 cltv_expiry_delta: 144 ,
@@ -515,6 +548,7 @@ mod tests {
515548 htlc_maximum_msat: u64 :: max_value( ) ,
516549 } ] ;
517550 let recv_tlvs = ReceiveTlvs {
551+ padding : None ,
518552 payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
519553 payment_constraints : PaymentConstraints {
520554 max_cltv_expiry : 0 ,
@@ -534,6 +568,7 @@ mod tests {
534568 #[ test]
535569 fn compute_payinfo_1_hop ( ) {
536570 let recv_tlvs = ReceiveTlvs {
571+ padding : None ,
537572 payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
538573 payment_constraints : PaymentConstraints {
539574 max_cltv_expiry : 0 ,
@@ -557,6 +592,7 @@ mod tests {
557592 let intermediate_nodes = vec ! [ ForwardNode {
558593 node_id: dummy_pk,
559594 tlvs: ForwardTlvs {
595+ padding: None ,
560596 short_channel_id: 0 ,
561597 payment_relay: PaymentRelay {
562598 cltv_expiry_delta: 0 ,
@@ -573,6 +609,7 @@ mod tests {
573609 } , ForwardNode {
574610 node_id: dummy_pk,
575611 tlvs: ForwardTlvs {
612+ padding: None ,
576613 short_channel_id: 0 ,
577614 payment_relay: PaymentRelay {
578615 cltv_expiry_delta: 0 ,
@@ -588,6 +625,7 @@ mod tests {
588625 htlc_maximum_msat: u64 :: max_value( )
589626 } ] ;
590627 let recv_tlvs = ReceiveTlvs {
628+ padding : None ,
591629 payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
592630 payment_constraints : PaymentConstraints {
593631 max_cltv_expiry : 0 ,
@@ -608,6 +646,7 @@ mod tests {
608646 let intermediate_nodes = vec ! [ ForwardNode {
609647 node_id: dummy_pk,
610648 tlvs: ForwardTlvs {
649+ padding: None ,
611650 short_channel_id: 0 ,
612651 payment_relay: PaymentRelay {
613652 cltv_expiry_delta: 0 ,
@@ -624,6 +663,7 @@ mod tests {
624663 } , ForwardNode {
625664 node_id: dummy_pk,
626665 tlvs: ForwardTlvs {
666+ padding: None ,
627667 short_channel_id: 0 ,
628668 payment_relay: PaymentRelay {
629669 cltv_expiry_delta: 0 ,
@@ -639,6 +679,7 @@ mod tests {
639679 htlc_maximum_msat: u64 :: max_value( )
640680 } ] ;
641681 let recv_tlvs = ReceiveTlvs {
682+ padding : None ,
642683 payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
643684 payment_constraints : PaymentConstraints {
644685 max_cltv_expiry : 0 ,
@@ -663,6 +704,7 @@ mod tests {
663704 let intermediate_nodes = vec ! [ ForwardNode {
664705 node_id: dummy_pk,
665706 tlvs: ForwardTlvs {
707+ padding: None ,
666708 short_channel_id: 0 ,
667709 payment_relay: PaymentRelay {
668710 cltv_expiry_delta: 0 ,
@@ -679,6 +721,7 @@ mod tests {
679721 } , ForwardNode {
680722 node_id: dummy_pk,
681723 tlvs: ForwardTlvs {
724+ padding: None ,
682725 short_channel_id: 0 ,
683726 payment_relay: PaymentRelay {
684727 cltv_expiry_delta: 0 ,
@@ -694,6 +737,7 @@ mod tests {
694737 htlc_maximum_msat: 10_000
695738 } ] ;
696739 let recv_tlvs = ReceiveTlvs {
740+ padding : None ,
697741 payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
698742 payment_constraints : PaymentConstraints {
699743 max_cltv_expiry : 0 ,
0 commit comments