@@ -13,31 +13,43 @@ use crate::ln::msgs::DecodeError;
13
13
use crate :: prelude:: * ;
14
14
use crate :: util:: ser:: { Readable , Writeable , Writer } ;
15
15
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
+
16
39
/// Data to construct a [`BlindedHop`] for sending a payment over.
17
40
///
18
41
/// [`BlindedHop`]: crate::blinded_path::BlindedHop
19
- pub enum BlindedPaymentTlvs {
42
+ pub ( crate ) enum BlindedPaymentTlvs {
20
43
/// 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 ) ,
34
45
/// 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 ) ,
41
53
}
42
54
43
55
/// Parameters for relaying over a given [`BlindedHop`].
@@ -72,24 +84,24 @@ pub struct PaymentConstraints {
72
84
pub htlc_minimum_msat : u64 ,
73
85
}
74
86
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 > {
76
100
fn write < W : Writer > ( & self , w : & mut W ) -> Result < ( ) , io:: Error > {
77
101
// TODO: write padding
78
102
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) ?,
93
105
}
94
106
Ok ( ( ) )
95
107
}
@@ -107,28 +119,32 @@ impl Readable for BlindedPaymentTlvs {
107
119
} ) ;
108
120
if let Some ( short_channel_id) = scid {
109
121
if payment_secret. is_some ( ) { return Err ( DecodeError :: InvalidValue ) }
110
- Ok ( BlindedPaymentTlvs :: Forward {
122
+ Ok ( BlindedPaymentTlvs :: Forward ( ForwardTlvs {
111
123
short_channel_id,
112
124
payment_relay : payment_relay. ok_or ( DecodeError :: InvalidValue ) ?,
113
125
payment_constraints : payment_constraints. 0 . unwrap ( ) ,
114
126
features : features. ok_or ( DecodeError :: InvalidValue ) ?,
115
- } )
127
+ } ) )
116
128
} else {
117
129
if payment_relay. is_some ( ) | features. is_some ( ) { return Err ( DecodeError :: InvalidValue ) }
118
- Ok ( BlindedPaymentTlvs :: Receive {
130
+ Ok ( BlindedPaymentTlvs :: Receive ( ReceiveTlvs {
119
131
payment_secret : payment_secret. ok_or ( DecodeError :: InvalidValue ) ?,
120
132
payment_constraints : payment_constraints. 0 . unwrap ( ) ,
121
- } )
133
+ } ) )
122
134
}
123
135
}
124
136
}
125
137
126
- /// Construct blinded payment hops for the given `unblinded_path` .
138
+ /// Construct blinded payment hops for the given `intermediate_nodes` and payee info .
127
139
pub ( 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
129
142
) -> 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)
132
148
}
133
149
134
150
impl_writeable_msg ! ( PaymentRelay , {
0 commit comments