@@ -10,9 +10,12 @@ use crate::io;
10
10
use crate :: ln:: PaymentSecret ;
11
11
use crate :: ln:: features:: BlindedHopFeatures ;
12
12
use crate :: ln:: msgs:: DecodeError ;
13
+ use crate :: offers:: invoice:: BlindedPayInfo ;
13
14
use crate :: prelude:: * ;
14
15
use crate :: util:: ser:: { Readable , Writeable , Writer } ;
15
16
17
+ use core:: convert:: TryFrom ;
18
+
16
19
/// Data to construct a [`BlindedHop`] for forwarding a payment.
17
20
pub struct ForwardTlvs {
18
21
/// The short channel id this payment should be forwarded out over.
@@ -148,6 +151,47 @@ pub(super) fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
148
151
utils:: construct_blinded_hops ( secp_ctx, pks, tlvs, session_priv)
149
152
}
150
153
154
+ pub ( super ) fn compute_payinfo (
155
+ intermediate_nodes : & [ ( PublicKey , ForwardTlvs ) ] , payee_tlvs : & ReceiveTlvs
156
+ ) -> Result < BlindedPayInfo , ( ) > {
157
+ let mut curr_base_fee: u128 = 0 ;
158
+ let mut curr_prop_mil: u128 = 0 ;
159
+ let mut cltv_expiry_delta: u16 = 0 ;
160
+ for ( _, tlvs) in intermediate_nodes. iter ( ) . rev ( ) {
161
+ // In the future, we'll want to take the intersection of all supported features for the
162
+ // `BlindedPayInfo`, but there are no features in that context right now.
163
+ if tlvs. features . requires_unknown_bits ( ) { return Err ( ( ) ) }
164
+
165
+ let next_base_fee = tlvs. payment_relay . fee_base_msat as u128 ;
166
+ let next_prop_mil = tlvs. payment_relay . fee_proportional_millionths as u128 ;
167
+ // Use integer arithmetic to compute `ceil(a/b)` as `(a+b-1)/b`
168
+ // ((next_base_fee * 1_000_000 + (curr_base_fee * (1_000_000 + next_prop_mil))) + 1_000_000 - 1) / 1_000_000
169
+ curr_base_fee = next_prop_mil. checked_add ( 1_000_000 )
170
+ . and_then ( |f| f. checked_mul ( curr_base_fee) )
171
+ . and_then ( |f| next_base_fee. checked_mul ( 1_000_000 ) . and_then ( |base| base. checked_add ( f) ) )
172
+ . and_then ( |f| f. checked_add ( 1_000_000 - 1 ) )
173
+ . map ( |f| f / 1_000_000 )
174
+ . ok_or ( ( ) ) ?;
175
+ // (((curr_prop_mil + next_prop_mil) * 1_000_000 + curr_prop_mil * next_prop_mil) + 1_000_000 - 1) / 1_000_000
176
+ curr_prop_mil = curr_prop_mil. checked_add ( next_prop_mil)
177
+ . and_then ( |f| f. checked_mul ( 1_000_000 ) )
178
+ . and_then ( |f| curr_prop_mil. checked_mul ( next_prop_mil) . and_then ( |prop_mil| prop_mil. checked_add ( f) ) )
179
+ . and_then ( |f| f. checked_add ( 1_000_000 - 1 ) )
180
+ . map ( |f| f / 1_000_000 )
181
+ . ok_or ( ( ) ) ?;
182
+
183
+ cltv_expiry_delta = cltv_expiry_delta. checked_add ( tlvs. payment_relay . cltv_expiry_delta ) . ok_or ( ( ) ) ?;
184
+ }
185
+ Ok ( BlindedPayInfo {
186
+ fee_base_msat : u32:: try_from ( curr_base_fee) . map_err ( |_| ( ) ) ?,
187
+ fee_proportional_millionths : u32:: try_from ( curr_prop_mil) . map_err ( |_| ( ) ) ?,
188
+ cltv_expiry_delta,
189
+ htlc_minimum_msat : 1 , // TODO
190
+ htlc_maximum_msat : 21_000_000 * 100_000_000 * 1_000 , // TODO
191
+ features : BlindedHopFeatures :: empty ( ) ,
192
+ } )
193
+ }
194
+
151
195
impl_writeable_msg ! ( PaymentRelay , {
152
196
cltv_expiry_delta,
153
197
fee_proportional_millionths,
@@ -158,3 +202,69 @@ impl_writeable_msg!(PaymentConstraints, {
158
202
max_cltv_expiry,
159
203
htlc_minimum_msat
160
204
} , { } ) ;
205
+
206
+ #[ cfg( test) ]
207
+ mod tests {
208
+ use bitcoin:: secp256k1:: PublicKey ;
209
+ use crate :: blinded_path:: payment:: { ForwardTlvs , ReceiveTlvs , PaymentConstraints , PaymentRelay } ;
210
+ use crate :: ln:: PaymentSecret ;
211
+ use crate :: ln:: features:: BlindedHopFeatures ;
212
+
213
+ #[ test]
214
+ fn compute_payinfo ( ) {
215
+ // Taken from the spec example for aggregating blinded payment info. See
216
+ // https://github.com/lightning/bolts/blob/master/proposals/route-blinding.md#blinded-payments
217
+ let dummy_pk = PublicKey :: from_slice ( & [ 2 ; 33 ] ) . unwrap ( ) ;
218
+ let intermediate_nodes = vec ! [ ( dummy_pk, ForwardTlvs {
219
+ short_channel_id: 0 ,
220
+ payment_relay: PaymentRelay {
221
+ cltv_expiry_delta: 144 ,
222
+ fee_proportional_millionths: 500 ,
223
+ fee_base_msat: 100 ,
224
+ } ,
225
+ payment_constraints: PaymentConstraints {
226
+ max_cltv_expiry: 0 ,
227
+ htlc_minimum_msat: 100 ,
228
+ } ,
229
+ features: BlindedHopFeatures :: empty( ) ,
230
+ } ) , ( dummy_pk, ForwardTlvs {
231
+ short_channel_id: 0 ,
232
+ payment_relay: PaymentRelay {
233
+ cltv_expiry_delta: 144 ,
234
+ fee_proportional_millionths: 500 ,
235
+ fee_base_msat: 100 ,
236
+ } ,
237
+ payment_constraints: PaymentConstraints {
238
+ max_cltv_expiry: 0 ,
239
+ htlc_minimum_msat: 1_000 ,
240
+ } ,
241
+ features: BlindedHopFeatures :: empty( ) ,
242
+ } ) ] ;
243
+ let recv_tlvs = ReceiveTlvs {
244
+ payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
245
+ payment_constraints : PaymentConstraints {
246
+ max_cltv_expiry : 0 ,
247
+ htlc_minimum_msat : 1 ,
248
+ } ,
249
+ } ;
250
+ let blinded_payinfo = super :: compute_payinfo ( & intermediate_nodes[ ..] , & recv_tlvs) . unwrap ( ) ;
251
+ assert_eq ! ( blinded_payinfo. fee_base_msat, 201 ) ;
252
+ assert_eq ! ( blinded_payinfo. fee_proportional_millionths, 1001 ) ;
253
+ assert_eq ! ( blinded_payinfo. cltv_expiry_delta, 288 ) ;
254
+ }
255
+
256
+ #[ test]
257
+ fn compute_payinfo_1_hop ( ) {
258
+ let recv_tlvs = ReceiveTlvs {
259
+ payment_secret : PaymentSecret ( [ 0 ; 32 ] ) ,
260
+ payment_constraints : PaymentConstraints {
261
+ max_cltv_expiry : 0 ,
262
+ htlc_minimum_msat : 1 ,
263
+ } ,
264
+ } ;
265
+ let blinded_payinfo = super :: compute_payinfo ( & [ ] , & recv_tlvs) . unwrap ( ) ;
266
+ assert_eq ! ( blinded_payinfo. fee_base_msat, 0 ) ;
267
+ assert_eq ! ( blinded_payinfo. fee_proportional_millionths, 0 ) ;
268
+ assert_eq ! ( blinded_payinfo. cltv_expiry_delta, 0 ) ;
269
+ }
270
+ }
0 commit comments