@@ -13,9 +13,10 @@ use crate::ln::features::BlindedHopFeatures;
13
13
use crate :: ln:: msgs:: DecodeError ;
14
14
use crate :: offers:: invoice:: BlindedPayInfo ;
15
15
use crate :: prelude:: * ;
16
- use crate :: util:: ser:: { Readable , Writeable , Writer } ;
16
+ use crate :: util:: ser:: { BigSize , FixedLengthReader , Readable , Writeable , Writer } ;
17
17
18
18
use core:: convert:: TryFrom ;
19
+ use crate :: io:: Read ;
19
20
20
21
/// An intermediate node, its outbound channel, and relay parameters.
21
22
#[ derive( Clone , Debug ) ]
@@ -53,6 +54,8 @@ pub struct ReceiveTlvs {
53
54
pub payment_secret : PaymentSecret ,
54
55
/// Constraints for the receiver of this payment.
55
56
pub payment_constraints : PaymentConstraints ,
57
+ /// Custom Tlvs
58
+ pub custom_tlvs : Vec < ( u64 , Vec < u8 > ) > ,
56
59
}
57
60
58
61
/// Data to construct a [`BlindedHop`] for sending a payment over.
@@ -121,6 +124,7 @@ impl Writeable for ForwardTlvs {
121
124
impl Writeable for ReceiveTlvs {
122
125
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
123
126
encode_tlv_stream ! ( w, {
127
+ ( 1 , self . custom_tlvs, optional_vec) ,
124
128
( 12 , self . payment_constraints, required) ,
125
129
( 65536 , self . payment_secret, required)
126
130
} ) ;
@@ -141,18 +145,29 @@ impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {
141
145
142
146
impl Readable for BlindedPaymentTlvs {
143
147
fn read < R : io:: Read > ( r : & mut R ) -> Result < Self , DecodeError > {
144
- _init_and_read_tlv_stream ! ( r, {
148
+ let mut custom_tlvs = Vec :: new ( ) ;
149
+
150
+ let tlv_len = BigSize :: read ( r) ?;
151
+ let rd = FixedLengthReader :: new ( r, tlv_len. 0 ) ;
152
+ _init_and_read_tlv_stream_with_custom_tlv_decode ! ( rd, {
145
153
( 1 , _padding, option) ,
146
154
( 2 , scid, option) ,
147
155
( 10 , payment_relay, option) ,
148
156
( 12 , payment_constraints, required) ,
149
157
( 14 , features, option) ,
150
158
( 65536 , payment_secret, option) ,
159
+ } , |msg_type: u64 , msg_reader: & mut FixedLengthReader <_>| -> Result <bool , DecodeError > {
160
+ if msg_type < 1 << 16 { return Ok ( false ) }
161
+ let mut value = Vec :: new( ) ;
162
+ msg_reader. read_to_end( & mut value) ?;
163
+ custom_tlvs. push( ( msg_type, value) ) ;
164
+ Ok ( true )
151
165
} ) ;
152
166
let _padding: Option < utils:: Padding > = _padding;
153
167
154
168
if let Some ( short_channel_id) = scid {
155
169
if payment_secret. is_some ( ) { return Err ( DecodeError :: InvalidValue ) }
170
+ if !custom_tlvs. is_empty ( ) { return Err ( DecodeError :: InvalidValue ) }
156
171
Ok ( BlindedPaymentTlvs :: Forward ( ForwardTlvs {
157
172
short_channel_id,
158
173
payment_relay : payment_relay. ok_or ( DecodeError :: InvalidValue ) ?,
@@ -164,6 +179,7 @@ impl Readable for BlindedPaymentTlvs {
164
179
Ok ( BlindedPaymentTlvs :: Receive ( ReceiveTlvs {
165
180
payment_secret : payment_secret. ok_or ( DecodeError :: InvalidValue ) ?,
166
181
payment_constraints : payment_constraints. 0 . unwrap ( ) ,
182
+ custom_tlvs,
167
183
} ) )
168
184
}
169
185
}
@@ -325,6 +341,7 @@ mod tests {
325
341
max_cltv_expiry : 0 ,
326
342
htlc_minimum_msat : 1 ,
327
343
} ,
344
+ custom_tlvs : Vec :: new ( ) ,
328
345
} ;
329
346
let htlc_maximum_msat = 100_000 ;
330
347
let blinded_payinfo = super :: compute_payinfo ( & intermediate_nodes[ ..] , & recv_tlvs, htlc_maximum_msat) . unwrap ( ) ;
@@ -343,6 +360,7 @@ mod tests {
343
360
max_cltv_expiry : 0 ,
344
361
htlc_minimum_msat : 1 ,
345
362
} ,
363
+ custom_tlvs : Vec :: new ( ) ,
346
364
} ;
347
365
let blinded_payinfo = super :: compute_payinfo ( & [ ] , & recv_tlvs, 4242 ) . unwrap ( ) ;
348
366
assert_eq ! ( blinded_payinfo. fee_base_msat, 0 ) ;
@@ -396,6 +414,7 @@ mod tests {
396
414
max_cltv_expiry : 0 ,
397
415
htlc_minimum_msat : 3 ,
398
416
} ,
417
+ custom_tlvs : Vec :: new ( ) ,
399
418
} ;
400
419
let htlc_maximum_msat = 100_000 ;
401
420
let blinded_payinfo = super :: compute_payinfo ( & intermediate_nodes[ ..] , & recv_tlvs, htlc_maximum_msat) . unwrap ( ) ;
@@ -446,6 +465,7 @@ mod tests {
446
465
max_cltv_expiry : 0 ,
447
466
htlc_minimum_msat : 1 ,
448
467
} ,
468
+ custom_tlvs : Vec :: new ( ) ,
449
469
} ;
450
470
let htlc_minimum_msat = 3798 ;
451
471
assert ! ( super :: compute_payinfo( & intermediate_nodes[ ..] , & recv_tlvs, htlc_minimum_msat - 1 ) . is_err( ) ) ;
@@ -500,6 +520,7 @@ mod tests {
500
520
max_cltv_expiry : 0 ,
501
521
htlc_minimum_msat : 1 ,
502
522
} ,
523
+ custom_tlvs : Vec :: new ( ) ,
503
524
} ;
504
525
505
526
let blinded_payinfo = super :: compute_payinfo ( & intermediate_nodes[ ..] , & recv_tlvs, 10_000 ) . unwrap ( ) ;
0 commit comments