@@ -13,9 +13,10 @@ use crate::ln::features::BlindedHopFeatures;
1313use crate :: ln:: msgs:: DecodeError ;
1414use crate :: offers:: invoice:: BlindedPayInfo ;
1515use crate :: prelude:: * ;
16- use crate :: util:: ser:: { Readable , Writeable , Writer } ;
16+ use crate :: util:: ser:: { BigSize , FixedLengthReader , Readable , Writeable , Writer } ;
1717
1818use core:: convert:: TryFrom ;
19+ use crate :: io:: Read ;
1920
2021/// An intermediate node, its outbound channel, and relay parameters.
2122#[ derive( Clone , Debug ) ]
@@ -53,6 +54,8 @@ pub struct ReceiveTlvs {
5354 pub payment_secret : PaymentSecret ,
5455 /// Constraints for the receiver of this payment.
5556 pub payment_constraints : PaymentConstraints ,
57+ /// Custom Tlvs
58+ pub custom_tlvs : Vec < ( u64 , Vec < u8 > ) > ,
5659}
5760
5861/// Data to construct a [`BlindedHop`] for sending a payment over.
@@ -121,6 +124,7 @@ impl Writeable for ForwardTlvs {
121124impl Writeable for ReceiveTlvs {
122125 fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
123126 encode_tlv_stream ! ( w, {
127+ ( 1 , self . custom_tlvs, optional_vec) ,
124128 ( 12 , self . payment_constraints, required) ,
125129 ( 65536 , self . payment_secret, required)
126130 } ) ;
@@ -141,18 +145,29 @@ impl<'a> Writeable for BlindedPaymentTlvsRef<'a> {
141145
142146impl Readable for BlindedPaymentTlvs {
143147 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, {
145153 ( 1 , _padding, option) ,
146154 ( 2 , scid, option) ,
147155 ( 10 , payment_relay, option) ,
148156 ( 12 , payment_constraints, required) ,
149157 ( 14 , features, option) ,
150158 ( 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 )
151165 } ) ;
152166 let _padding: Option < utils:: Padding > = _padding;
153167
154168 if let Some ( short_channel_id) = scid {
155169 if payment_secret. is_some ( ) { return Err ( DecodeError :: InvalidValue ) }
170+ if !custom_tlvs. is_empty ( ) { return Err ( DecodeError :: InvalidValue ) }
156171 Ok ( BlindedPaymentTlvs :: Forward ( ForwardTlvs {
157172 short_channel_id,
158173 payment_relay : payment_relay. ok_or ( DecodeError :: InvalidValue ) ?,
@@ -164,6 +179,7 @@ impl Readable for BlindedPaymentTlvs {
164179 Ok ( BlindedPaymentTlvs :: Receive ( ReceiveTlvs {
165180 payment_secret : payment_secret. ok_or ( DecodeError :: InvalidValue ) ?,
166181 payment_constraints : payment_constraints. 0 . unwrap ( ) ,
182+ custom_tlvs,
167183 } ) )
168184 }
169185 }
@@ -325,6 +341,7 @@ mod tests {
325341 max_cltv_expiry : 0 ,
326342 htlc_minimum_msat : 1 ,
327343 } ,
344+ custom_tlvs : Vec :: new ( ) ,
328345 } ;
329346 let htlc_maximum_msat = 100_000 ;
330347 let blinded_payinfo = super :: compute_payinfo ( & intermediate_nodes[ ..] , & recv_tlvs, htlc_maximum_msat) . unwrap ( ) ;
@@ -343,6 +360,7 @@ mod tests {
343360 max_cltv_expiry : 0 ,
344361 htlc_minimum_msat : 1 ,
345362 } ,
363+ custom_tlvs : Vec :: new ( ) ,
346364 } ;
347365 let blinded_payinfo = super :: compute_payinfo ( & [ ] , & recv_tlvs, 4242 ) . unwrap ( ) ;
348366 assert_eq ! ( blinded_payinfo. fee_base_msat, 0 ) ;
@@ -396,6 +414,7 @@ mod tests {
396414 max_cltv_expiry : 0 ,
397415 htlc_minimum_msat : 3 ,
398416 } ,
417+ custom_tlvs : Vec :: new ( ) ,
399418 } ;
400419 let htlc_maximum_msat = 100_000 ;
401420 let blinded_payinfo = super :: compute_payinfo ( & intermediate_nodes[ ..] , & recv_tlvs, htlc_maximum_msat) . unwrap ( ) ;
@@ -446,6 +465,7 @@ mod tests {
446465 max_cltv_expiry : 0 ,
447466 htlc_minimum_msat : 1 ,
448467 } ,
468+ custom_tlvs : Vec :: new ( ) ,
449469 } ;
450470 let htlc_minimum_msat = 3798 ;
451471 assert ! ( super :: compute_payinfo( & intermediate_nodes[ ..] , & recv_tlvs, htlc_minimum_msat - 1 ) . is_err( ) ) ;
@@ -500,6 +520,7 @@ mod tests {
500520 max_cltv_expiry : 0 ,
501521 htlc_minimum_msat : 1 ,
502522 } ,
523+ custom_tlvs : Vec :: new ( ) ,
503524 } ;
504525
505526 let blinded_payinfo = super :: compute_payinfo ( & intermediate_nodes[ ..] , & recv_tlvs, 10_000 ) . unwrap ( ) ;
0 commit comments