1414use bitcoin:: secp256k1:: { self , PublicKey , Secp256k1 , SecretKey } ;
1515
1616use crate :: blinded_path:: { BlindedHop , BlindedPath , IntroductionNode , NodeIdLookUp } ;
17- use crate :: blinded_path:: utils:: { self , WithPadding } ;
17+ use crate :: blinded_path:: utils;
1818use crate :: crypto:: streams:: ChaChaPolyReadAdapter ;
1919use crate :: io;
2020use crate :: io:: Cursor ;
@@ -35,6 +35,8 @@ use core::ops::Deref;
3535#[ allow( unused_imports) ]
3636use crate :: prelude:: * ;
3737
38+ use super :: utils:: Padding ;
39+
3840/// An intermediate node, its outbound channel, and relay parameters.
3941#[ derive( Clone , Debug ) ]
4042pub struct ForwardNode {
@@ -50,6 +52,8 @@ pub struct ForwardNode {
5052/// Data to construct a [`BlindedHop`] for forwarding a payment.
5153#[ derive( Clone , Debug ) ]
5254pub struct ForwardTlvs {
55+ /// The padding data used to make all packets of a Blinded Path of same size
56+ pub padding : Option < Padding > ,
5357 /// The short channel id this payment should be forwarded out over.
5458 pub short_channel_id : u64 ,
5559 /// Payment parameters for relaying over [`Self::short_channel_id`].
@@ -67,6 +71,8 @@ pub struct ForwardTlvs {
6771/// may not be valid if received by another lightning implementation.
6872#[ derive( Clone , Debug ) ]
6973pub struct ReceiveTlvs {
74+ /// The padding data used to make all packets of a Blinded Path of same size
75+ pub padding : Option < Padding > ,
7076 /// Used to authenticate the sender of a payment to the receiver and tie MPP HTLCs together.
7177 pub payment_secret : PaymentSecret ,
7278 /// Constraints for the receiver of this payment.
@@ -78,6 +84,7 @@ pub struct ReceiveTlvs {
7884/// Data to construct a [`BlindedHop`] for sending a payment over.
7985///
8086/// [`BlindedHop`]: crate::blinded_path::BlindedHop
87+ #[ derive( Clone ) ]
8188pub ( crate ) enum BlindedPaymentTlvs {
8289 /// This blinded payment data is for a forwarding node.
8390 Forward ( ForwardTlvs ) ,
@@ -92,6 +99,27 @@ enum BlindedPaymentTlvsRef<'a> {
9299 Receive ( & ' a ReceiveTlvs ) ,
93100}
94101
102+ impl < ' a > BlindedPaymentTlvsRef < ' a > {
103+ pub ( crate ) fn pad_tlvs ( self , max_length : usize ) -> BlindedPaymentTlvs {
104+ let length = max_length. checked_sub ( self . serialized_length ( ) ) ;
105+ debug_assert ! ( length. is_some( ) , "Size of this packet should not be larger than the size of the largest packet." ) ;
106+ let padding = Some ( Padding :: new ( length. unwrap ( ) ) ) ;
107+
108+ match self {
109+ BlindedPaymentTlvsRef :: Forward ( tlvs) => {
110+ let mut new_tlvs = tlvs. clone ( ) ;
111+ new_tlvs. padding = padding;
112+ BlindedPaymentTlvs :: Forward ( new_tlvs)
113+ }
114+ BlindedPaymentTlvsRef :: Receive ( tlvs) => {
115+ let mut new_tlvs = tlvs. clone ( ) ;
116+ new_tlvs. padding = padding;
117+ BlindedPaymentTlvs :: Receive ( new_tlvs)
118+ }
119+ }
120+ }
121+ }
122+
95123/// Parameters for relaying over a given [`BlindedHop`].
96124///
97125/// [`BlindedHop`]: crate::blinded_path::BlindedHop
@@ -205,6 +233,7 @@ impl Writeable for ForwardTlvs {
205233 if self . features == BlindedHopFeatures :: empty ( ) { None }
206234 else { Some ( & self . features ) } ;
207235 encode_tlv_stream ! ( w, {
236+ ( 1 , self . padding, option) ,
208237 ( 2 , self . short_channel_id, required) ,
209238 ( 10 , self . payment_relay, required) ,
210239 ( 12 , self . payment_constraints, required) ,
@@ -217,6 +246,7 @@ impl Writeable for ForwardTlvs {
217246impl Writeable for ReceiveTlvs {
218247 fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
219248 encode_tlv_stream ! ( w, {
249+ ( 1 , self . padding, option) ,
220250 ( 12 , self . payment_constraints, required) ,
221251 ( 65536 , self . payment_secret, required) ,
222252 ( 65537 , self . payment_context, required)
@@ -225,6 +255,16 @@ impl Writeable for ReceiveTlvs {
225255 }
226256}
227257
258+ impl Writeable for BlindedPaymentTlvs {
259+ fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
260+ match self {
261+ Self :: Forward ( tlvs) => tlvs. write ( w) ?,
262+ Self :: Receive ( tlvs) => tlvs. write ( w) ?,
263+ }
264+ Ok ( ( ) )
265+ }
266+ }
267+
228268impl < ' a > Writeable for BlindedPaymentTlvsRef < ' a > {
229269 fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
230270 match self {
@@ -253,6 +293,7 @@ impl Readable for BlindedPaymentTlvs {
253293 return Err ( DecodeError :: InvalidValue )
254294 }
255295 Ok ( BlindedPaymentTlvs :: Forward ( ForwardTlvs {
296+ padding : None ,
256297 short_channel_id,
257298 payment_relay : payment_relay. ok_or ( DecodeError :: InvalidValue ) ?,
258299 payment_constraints : payment_constraints. 0 . unwrap ( ) ,
@@ -261,6 +302,7 @@ impl Readable for BlindedPaymentTlvs {
261302 } else {
262303 if payment_relay. is_some ( ) || features. is_some ( ) { return Err ( DecodeError :: InvalidValue ) }
263304 Ok ( BlindedPaymentTlvs :: Receive ( ReceiveTlvs {
305+ padding : None ,
264306 payment_secret : payment_secret. ok_or ( DecodeError :: InvalidValue ) ?,
265307 payment_constraints : payment_constraints. 0 . unwrap ( ) ,
266308 payment_context : payment_context. 0 . unwrap ( ) ,
@@ -284,7 +326,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
284326 . max ( )
285327 . unwrap_or ( 0 ) ;
286328
287- let length_tlvs = tlvs. map ( |tlvs| WithPadding { max_length, tlvs } ) ;
329+ let length_tlvs = tlvs. map ( |tlv| tlv . pad_tlvs ( max_length) ) ;
288330
289331 utils:: construct_blinded_hops ( secp_ctx, pks, length_tlvs, session_priv)
290332}
@@ -498,6 +540,7 @@ mod tests {
498540 let intermediate_nodes = vec ! [ ForwardNode {
499541 node_id: dummy_pk,
500542 tlvs: ForwardTlvs {
543+ padding: None ,
501544 short_channel_id: 0 ,
502545 payment_relay: PaymentRelay {
503546 cltv_expiry_delta: 144 ,
@@ -514,6 +557,7 @@ mod tests {
514557 } , ForwardNode {
515558 node_id: dummy_pk,
516559 tlvs: ForwardTlvs {
560+ padding: None ,
517561 short_channel_id: 0 ,
518562 payment_relay: PaymentRelay {
519563 cltv_expiry_delta: 144 ,
@@ -529,6 +573,7 @@ mod tests {
529573 htlc_maximum_msat: u64 :: max_value( ) ,
530574 } ] ;
531575 let recv_tlvs = ReceiveTlvs {
576+ padding : None ,
532577 payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
533578 payment_constraints : PaymentConstraints {
534579 max_cltv_expiry : 0 ,
@@ -548,6 +593,7 @@ mod tests {
548593 #[ test]
549594 fn compute_payinfo_1_hop ( ) {
550595 let recv_tlvs = ReceiveTlvs {
596+ padding : None ,
551597 payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
552598 payment_constraints : PaymentConstraints {
553599 max_cltv_expiry : 0 ,
@@ -571,6 +617,7 @@ mod tests {
571617 let intermediate_nodes = vec ! [ ForwardNode {
572618 node_id: dummy_pk,
573619 tlvs: ForwardTlvs {
620+ padding: None ,
574621 short_channel_id: 0 ,
575622 payment_relay: PaymentRelay {
576623 cltv_expiry_delta: 0 ,
@@ -587,6 +634,7 @@ mod tests {
587634 } , ForwardNode {
588635 node_id: dummy_pk,
589636 tlvs: ForwardTlvs {
637+ padding: None ,
590638 short_channel_id: 0 ,
591639 payment_relay: PaymentRelay {
592640 cltv_expiry_delta: 0 ,
@@ -602,6 +650,7 @@ mod tests {
602650 htlc_maximum_msat: u64 :: max_value( )
603651 } ] ;
604652 let recv_tlvs = ReceiveTlvs {
653+ padding : None ,
605654 payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
606655 payment_constraints : PaymentConstraints {
607656 max_cltv_expiry : 0 ,
@@ -622,6 +671,7 @@ mod tests {
622671 let intermediate_nodes = vec ! [ ForwardNode {
623672 node_id: dummy_pk,
624673 tlvs: ForwardTlvs {
674+ padding: None ,
625675 short_channel_id: 0 ,
626676 payment_relay: PaymentRelay {
627677 cltv_expiry_delta: 0 ,
@@ -638,6 +688,7 @@ mod tests {
638688 } , ForwardNode {
639689 node_id: dummy_pk,
640690 tlvs: ForwardTlvs {
691+ padding: None ,
641692 short_channel_id: 0 ,
642693 payment_relay: PaymentRelay {
643694 cltv_expiry_delta: 0 ,
@@ -653,6 +704,7 @@ mod tests {
653704 htlc_maximum_msat: u64 :: max_value( )
654705 } ] ;
655706 let recv_tlvs = ReceiveTlvs {
707+ padding : None ,
656708 payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
657709 payment_constraints : PaymentConstraints {
658710 max_cltv_expiry : 0 ,
@@ -677,6 +729,7 @@ mod tests {
677729 let intermediate_nodes = vec ! [ ForwardNode {
678730 node_id: dummy_pk,
679731 tlvs: ForwardTlvs {
732+ padding: None ,
680733 short_channel_id: 0 ,
681734 payment_relay: PaymentRelay {
682735 cltv_expiry_delta: 0 ,
@@ -693,6 +746,7 @@ mod tests {
693746 } , ForwardNode {
694747 node_id: dummy_pk,
695748 tlvs: ForwardTlvs {
749+ padding: None ,
696750 short_channel_id: 0 ,
697751 payment_relay: PaymentRelay {
698752 cltv_expiry_delta: 0 ,
@@ -708,6 +762,7 @@ mod tests {
708762 htlc_maximum_msat: 10_000
709763 } ] ;
710764 let recv_tlvs = ReceiveTlvs {
765+ padding : None ,
711766 payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
712767 payment_constraints : PaymentConstraints {
713768 max_cltv_expiry : 0 ,
0 commit comments