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:: { self , WithPadding } ;
17
+ use crate :: blinded_path:: utils;
18
18
use crate :: crypto:: streams:: ChaChaPolyReadAdapter ;
19
19
use crate :: io;
20
20
use crate :: io:: Cursor ;
@@ -35,6 +35,8 @@ use core::ops::Deref;
35
35
#[ allow( unused_imports) ]
36
36
use crate :: prelude:: * ;
37
37
38
+ use super :: utils:: Padding ;
39
+
38
40
/// An intermediate node, its outbound channel, and relay parameters.
39
41
#[ derive( Clone , Debug ) ]
40
42
pub struct ForwardNode {
@@ -50,6 +52,8 @@ pub struct ForwardNode {
50
52
/// Data to construct a [`BlindedHop`] for forwarding a payment.
51
53
#[ derive( Clone , Debug ) ]
52
54
pub struct ForwardTlvs {
55
+ /// The padding data used to make all packets of a Blinded Path of same size
56
+ pub padding : Option < Padding > ,
53
57
/// The short channel id this payment should be forwarded out over.
54
58
pub short_channel_id : u64 ,
55
59
/// Payment parameters for relaying over [`Self::short_channel_id`].
@@ -67,6 +71,8 @@ pub struct ForwardTlvs {
67
71
/// may not be valid if received by another lightning implementation.
68
72
#[ derive( Clone , Debug ) ]
69
73
pub struct ReceiveTlvs {
74
+ /// The padding data used to make all packets of a Blinded Path of same size
75
+ pub padding : Option < Padding > ,
70
76
/// Used to authenticate the sender of a payment to the receiver and tie MPP HTLCs together.
71
77
pub payment_secret : PaymentSecret ,
72
78
/// Constraints for the receiver of this payment.
@@ -78,6 +84,7 @@ pub struct ReceiveTlvs {
78
84
/// Data to construct a [`BlindedHop`] for sending a payment over.
79
85
///
80
86
/// [`BlindedHop`]: crate::blinded_path::BlindedHop
87
+ #[ derive( Clone ) ]
81
88
pub ( crate ) enum BlindedPaymentTlvs {
82
89
/// This blinded payment data is for a forwarding node.
83
90
Forward ( ForwardTlvs ) ,
@@ -92,6 +99,27 @@ enum BlindedPaymentTlvsRef<'a> {
92
99
Receive ( & ' a ReceiveTlvs ) ,
93
100
}
94
101
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
+
95
123
/// Parameters for relaying over a given [`BlindedHop`].
96
124
///
97
125
/// [`BlindedHop`]: crate::blinded_path::BlindedHop
@@ -205,6 +233,7 @@ impl Writeable for ForwardTlvs {
205
233
if self . features == BlindedHopFeatures :: empty ( ) { None }
206
234
else { Some ( & self . features ) } ;
207
235
encode_tlv_stream ! ( w, {
236
+ ( 1 , self . padding, option) ,
208
237
( 2 , self . short_channel_id, required) ,
209
238
( 10 , self . payment_relay, required) ,
210
239
( 12 , self . payment_constraints, required) ,
@@ -217,6 +246,7 @@ impl Writeable for ForwardTlvs {
217
246
impl Writeable for ReceiveTlvs {
218
247
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
219
248
encode_tlv_stream ! ( w, {
249
+ ( 1 , self . padding, option) ,
220
250
( 12 , self . payment_constraints, required) ,
221
251
( 65536 , self . payment_secret, required) ,
222
252
( 65537 , self . payment_context, required)
@@ -225,6 +255,16 @@ impl Writeable for ReceiveTlvs {
225
255
}
226
256
}
227
257
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
+
228
268
impl < ' a > Writeable for BlindedPaymentTlvsRef < ' a > {
229
269
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
230
270
match self {
@@ -253,6 +293,7 @@ impl Readable for BlindedPaymentTlvs {
253
293
return Err ( DecodeError :: InvalidValue )
254
294
}
255
295
Ok ( BlindedPaymentTlvs :: Forward ( ForwardTlvs {
296
+ padding : None ,
256
297
short_channel_id,
257
298
payment_relay : payment_relay. ok_or ( DecodeError :: InvalidValue ) ?,
258
299
payment_constraints : payment_constraints. 0 . unwrap ( ) ,
@@ -261,6 +302,7 @@ impl Readable for BlindedPaymentTlvs {
261
302
} else {
262
303
if payment_relay. is_some ( ) || features. is_some ( ) { return Err ( DecodeError :: InvalidValue ) }
263
304
Ok ( BlindedPaymentTlvs :: Receive ( ReceiveTlvs {
305
+ padding : None ,
264
306
payment_secret : payment_secret. ok_or ( DecodeError :: InvalidValue ) ?,
265
307
payment_constraints : payment_constraints. 0 . unwrap ( ) ,
266
308
payment_context : payment_context. 0 . unwrap ( ) ,
@@ -284,7 +326,7 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
284
326
. max ( )
285
327
. unwrap_or ( 0 ) ;
286
328
287
- let length_tlvs = tlvs. map ( |tlvs| WithPadding { max_length, tlvs } ) ;
329
+ let length_tlvs = tlvs. map ( |tlv| tlv . pad_tlvs ( max_length) ) ;
288
330
289
331
utils:: construct_blinded_hops ( secp_ctx, pks, length_tlvs, session_priv)
290
332
}
@@ -498,6 +540,7 @@ mod tests {
498
540
let intermediate_nodes = vec ! [ ForwardNode {
499
541
node_id: dummy_pk,
500
542
tlvs: ForwardTlvs {
543
+ padding: None ,
501
544
short_channel_id: 0 ,
502
545
payment_relay: PaymentRelay {
503
546
cltv_expiry_delta: 144 ,
@@ -514,6 +557,7 @@ mod tests {
514
557
} , ForwardNode {
515
558
node_id: dummy_pk,
516
559
tlvs: ForwardTlvs {
560
+ padding: None ,
517
561
short_channel_id: 0 ,
518
562
payment_relay: PaymentRelay {
519
563
cltv_expiry_delta: 144 ,
@@ -529,6 +573,7 @@ mod tests {
529
573
htlc_maximum_msat: u64 :: max_value( ) ,
530
574
} ] ;
531
575
let recv_tlvs = ReceiveTlvs {
576
+ padding : None ,
532
577
payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
533
578
payment_constraints : PaymentConstraints {
534
579
max_cltv_expiry : 0 ,
@@ -548,6 +593,7 @@ mod tests {
548
593
#[ test]
549
594
fn compute_payinfo_1_hop ( ) {
550
595
let recv_tlvs = ReceiveTlvs {
596
+ padding : None ,
551
597
payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
552
598
payment_constraints : PaymentConstraints {
553
599
max_cltv_expiry : 0 ,
@@ -571,6 +617,7 @@ mod tests {
571
617
let intermediate_nodes = vec ! [ ForwardNode {
572
618
node_id: dummy_pk,
573
619
tlvs: ForwardTlvs {
620
+ padding: None ,
574
621
short_channel_id: 0 ,
575
622
payment_relay: PaymentRelay {
576
623
cltv_expiry_delta: 0 ,
@@ -587,6 +634,7 @@ mod tests {
587
634
} , ForwardNode {
588
635
node_id: dummy_pk,
589
636
tlvs: ForwardTlvs {
637
+ padding: None ,
590
638
short_channel_id: 0 ,
591
639
payment_relay: PaymentRelay {
592
640
cltv_expiry_delta: 0 ,
@@ -602,6 +650,7 @@ mod tests {
602
650
htlc_maximum_msat: u64 :: max_value( )
603
651
} ] ;
604
652
let recv_tlvs = ReceiveTlvs {
653
+ padding : None ,
605
654
payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
606
655
payment_constraints : PaymentConstraints {
607
656
max_cltv_expiry : 0 ,
@@ -622,6 +671,7 @@ mod tests {
622
671
let intermediate_nodes = vec ! [ ForwardNode {
623
672
node_id: dummy_pk,
624
673
tlvs: ForwardTlvs {
674
+ padding: None ,
625
675
short_channel_id: 0 ,
626
676
payment_relay: PaymentRelay {
627
677
cltv_expiry_delta: 0 ,
@@ -638,6 +688,7 @@ mod tests {
638
688
} , ForwardNode {
639
689
node_id: dummy_pk,
640
690
tlvs: ForwardTlvs {
691
+ padding: None ,
641
692
short_channel_id: 0 ,
642
693
payment_relay: PaymentRelay {
643
694
cltv_expiry_delta: 0 ,
@@ -653,6 +704,7 @@ mod tests {
653
704
htlc_maximum_msat: u64 :: max_value( )
654
705
} ] ;
655
706
let recv_tlvs = ReceiveTlvs {
707
+ padding : None ,
656
708
payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
657
709
payment_constraints : PaymentConstraints {
658
710
max_cltv_expiry : 0 ,
@@ -677,6 +729,7 @@ mod tests {
677
729
let intermediate_nodes = vec ! [ ForwardNode {
678
730
node_id: dummy_pk,
679
731
tlvs: ForwardTlvs {
732
+ padding: None ,
680
733
short_channel_id: 0 ,
681
734
payment_relay: PaymentRelay {
682
735
cltv_expiry_delta: 0 ,
@@ -693,6 +746,7 @@ mod tests {
693
746
} , ForwardNode {
694
747
node_id: dummy_pk,
695
748
tlvs: ForwardTlvs {
749
+ padding: None ,
696
750
short_channel_id: 0 ,
697
751
payment_relay: PaymentRelay {
698
752
cltv_expiry_delta: 0 ,
@@ -708,6 +762,7 @@ mod tests {
708
762
htlc_maximum_msat: 10_000
709
763
} ] ;
710
764
let recv_tlvs = ReceiveTlvs {
765
+ padding : None ,
711
766
payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
712
767
payment_constraints : PaymentConstraints {
713
768
max_cltv_expiry : 0 ,
0 commit comments