@@ -13,31 +13,43 @@ use crate::ln::msgs::DecodeError;
1313use crate :: prelude:: * ;
1414use crate :: util:: ser:: { Readable , Writeable , Writer } ;
1515
16+ /// Data to construct a [`BlindedHop`] for forwarding a payment.
17+ pub struct ForwardTlvs {
18+ /// The short channel id this payment is being forwarded over.
19+ short_channel_id : u64 ,
20+ /// Payment parameters for relaying over this channel.
21+ payment_relay : PaymentRelay ,
22+ /// Payment constraints when relaying over this channel.
23+ payment_constraints : PaymentConstraints ,
24+ /// Supported and required features when relaying a payment onion containing this object's
25+ /// corresponding [`BlindedHop`].
26+ ///
27+ /// [`BlindedHop`]: crate::blinded_path::BlindedHop
28+ features : BlindedHopFeatures ,
29+ }
30+
31+ /// Data to construct a [`BlindedHop`] for receiving a payment.
32+ pub struct ReceiveTlvs {
33+ /// Used to authenticate the sender of a payment to the receiver and tie MPP HTLCs together.
34+ payment_secret : PaymentSecret ,
35+ /// Constraints for the receiver of this payment.
36+ payment_constraints : PaymentConstraints ,
37+ }
38+
1639/// Data to construct a [`BlindedHop`] for sending a payment over.
1740///
1841/// [`BlindedHop`]: crate::blinded_path::BlindedHop
19- pub enum BlindedPaymentTlvs {
42+ pub ( crate ) enum BlindedPaymentTlvs {
2043 /// This blinded payment data is to be forwarded.
21- Forward {
22- /// The short channel id this payment is being forwarded over.
23- short_channel_id : u64 ,
24- /// Payment parameters for relaying over this channel.
25- payment_relay : PaymentRelay ,
26- /// Payment constraints when relaying over this channel.
27- payment_constraints : PaymentConstraints ,
28- /// Supported and required features when relaying a payment onion containing this object's
29- /// corresponding [`BlindedHop`].
30- ///
31- /// [`BlindedHop`]: crate::blinded_path::BlindedHop
32- features : BlindedHopFeatures ,
33- } ,
44+ Forward ( ForwardTlvs ) ,
3445 /// This blinded payment data is to be received.
35- Receive {
36- /// Used to authenticate the sender of a payment to the receiver and tie MPP HTLCs together.
37- payment_secret : PaymentSecret ,
38- /// Constraints for the receiver of this payment.
39- payment_constraints : PaymentConstraints ,
40- } ,
46+ Receive ( ReceiveTlvs ) ,
47+ }
48+
49+ // Used to include forward and receive TLVs in the same iterator for encoding.
50+ enum BlindedPaymentTlvsRef < ' a > {
51+ Forward ( & ' a ForwardTlvs ) ,
52+ Receive ( & ' a ReceiveTlvs ) ,
4153}
4254
4355/// Parameters for relaying over a given [`BlindedHop`].
@@ -72,24 +84,24 @@ pub struct PaymentConstraints {
7284 pub htlc_minimum_msat : u64 ,
7385}
7486
75- impl Writeable for BlindedPaymentTlvs {
87+ impl_writeable_tlv_based ! ( ForwardTlvs , {
88+ ( 2 , short_channel_id, required) ,
89+ ( 10 , payment_relay, required) ,
90+ ( 12 , payment_constraints, required) ,
91+ ( 14 , features, required) ,
92+ } ) ;
93+
94+ impl_writeable_tlv_based ! ( ReceiveTlvs , {
95+ ( 6 , payment_secret, required) ,
96+ ( 12 , payment_constraints, required) ,
97+ } ) ;
98+
99+ impl < ' a > Writeable for BlindedPaymentTlvsRef < ' a > {
76100 fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
77101 // TODO: write padding
78102 match self {
79- Self :: Forward { short_channel_id, payment_relay, payment_constraints, features } => {
80- encode_tlv_stream ! ( w, {
81- ( 2 , short_channel_id, required) ,
82- ( 10 , payment_relay, required) ,
83- ( 12 , payment_constraints, required) ,
84- ( 14 , features, required) ,
85- } ) ;
86- } ,
87- Self :: Receive { payment_secret, payment_constraints } => {
88- encode_tlv_stream ! ( w, {
89- ( 6 , payment_secret, required) ,
90- ( 12 , payment_constraints, required) ,
91- } ) ;
92- }
103+ Self :: Forward ( tlvs) => tlvs. write ( w) ?,
104+ Self :: Receive ( tlvs) => tlvs. write ( w) ?,
93105 }
94106 Ok ( ( ) )
95107 }
@@ -107,28 +119,32 @@ impl Readable for BlindedPaymentTlvs {
107119 } ) ;
108120 if let Some ( short_channel_id) = scid {
109121 if payment_secret. is_some ( ) { return Err ( DecodeError :: InvalidValue ) }
110- Ok ( BlindedPaymentTlvs :: Forward {
122+ Ok ( BlindedPaymentTlvs :: Forward ( ForwardTlvs {
111123 short_channel_id,
112124 payment_relay : payment_relay. ok_or ( DecodeError :: InvalidValue ) ?,
113125 payment_constraints : payment_constraints. 0 . unwrap ( ) ,
114126 features : features. ok_or ( DecodeError :: InvalidValue ) ?,
115- } )
127+ } ) )
116128 } else {
117129 if payment_relay. is_some ( ) | features. is_some ( ) { return Err ( DecodeError :: InvalidValue ) }
118- Ok ( BlindedPaymentTlvs :: Receive {
130+ Ok ( BlindedPaymentTlvs :: Receive ( ReceiveTlvs {
119131 payment_secret : payment_secret. ok_or ( DecodeError :: InvalidValue ) ?,
120132 payment_constraints : payment_constraints. 0 . unwrap ( ) ,
121- } )
133+ } ) )
122134 }
123135 }
124136}
125137
126- /// Construct blinded payment hops for the given `unblinded_path` .
138+ /// Construct blinded payment hops for the given `intermediate_nodes` and payee info .
127139pub ( super ) fn blinded_hops < T : secp256k1:: Signing + secp256k1:: Verification > (
128- secp_ctx : & Secp256k1 < T > , unblinded_path : & [ ( PublicKey , BlindedPaymentTlvs ) ] , session_priv : & SecretKey
140+ secp_ctx : & Secp256k1 < T > , intermediate_nodes : & [ ( PublicKey , ForwardTlvs ) ] ,
141+ payee_node_id : PublicKey , payee_tlvs : ReceiveTlvs , session_priv : & SecretKey
129142) -> Result < Vec < BlindedHop > , secp256k1:: Error > {
130- utils:: construct_blinded_hops ( secp_ctx, unblinded_path. iter ( ) . map ( |( pk, _) | pk) ,
131- unblinded_path. iter ( ) . map ( |( _, tlvs) | tlvs) , session_priv)
143+ let pks = intermediate_nodes. iter ( ) . map ( |( pk, _) | pk)
144+ . chain ( core:: iter:: once ( & payee_node_id) ) ;
145+ let tlvs = intermediate_nodes. iter ( ) . map ( |( _, tlvs) | BlindedPaymentTlvsRef :: Forward ( tlvs) )
146+ . chain ( core:: iter:: once ( BlindedPaymentTlvsRef :: Receive ( & payee_tlvs) ) ) ;
147+ utils:: construct_blinded_hops ( secp_ctx, pks, tlvs, session_priv)
132148}
133149
134150impl_writeable_msg ! ( PaymentRelay , {
0 commit comments