1
1
//! Convenient utilities to create an invoice.
2
2
3
- use { CreationError , Currency , DEFAULT_EXPIRY_TIME , Invoice , InvoiceBuilder , SignOrCreationError } ;
3
+ use { CreationError , Currency , Invoice , InvoiceBuilder , SignOrCreationError } ;
4
4
use payment:: { Payer , Router } ;
5
5
6
6
use crate :: { prelude:: * , Description , InvoiceDescription , Sha256 } ;
@@ -20,7 +20,6 @@ use lightning::routing::network_graph::{NetworkGraph, RoutingFees};
20
20
use lightning:: routing:: router:: { Route , RouteHint , RouteHintHop , RouteParameters , find_route} ;
21
21
use lightning:: util:: logger:: Logger ;
22
22
use secp256k1:: PublicKey ;
23
- use core:: convert:: TryInto ;
24
23
use core:: ops:: Deref ;
25
24
use core:: time:: Duration ;
26
25
use sync:: Mutex ;
@@ -213,9 +212,12 @@ fn _create_phantom_invoice<Signer: Sign, K: Deref>(
213
212
/// method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
214
213
/// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
215
214
/// that the payment secret is valid when the invoice is paid.
215
+ ///
216
+ /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
217
+ /// in excess of the current time.
216
218
pub fn create_invoice_from_channelmanager < Signer : Sign , M : Deref , T : Deref , K : Deref , F : Deref , L : Deref > (
217
219
channelmanager : & ChannelManager < Signer , M , T , K , F , L > , keys_manager : K , network : Currency ,
218
- amt_msat : Option < u64 > , description : String
220
+ amt_msat : Option < u64 > , description : String , invoice_expiry_delta_secs : u32
219
221
) -> Result < Invoice , SignOrCreationError < ( ) > >
220
222
where
221
223
M :: Target : chain:: Watch < Signer > ,
@@ -228,7 +230,8 @@ where
228
230
let duration = SystemTime :: now ( ) . duration_since ( SystemTime :: UNIX_EPOCH )
229
231
. expect ( "for the foreseeable future this shouldn't happen" ) ;
230
232
create_invoice_from_channelmanager_and_duration_since_epoch (
231
- channelmanager, keys_manager, network, amt_msat, description, duration
233
+ channelmanager, keys_manager, network, amt_msat,
234
+ description, duration, invoice_expiry_delta_secs
232
235
)
233
236
}
234
237
@@ -239,9 +242,12 @@ where
239
242
/// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
240
243
/// that the payment secret is valid when the invoice is paid.
241
244
/// Use this variant if you want to pass the `description_hash` to the invoice.
245
+ ///
246
+ /// `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
247
+ /// in excess of the current time.
242
248
pub fn create_invoice_from_channelmanager_with_description_hash < Signer : Sign , M : Deref , T : Deref , K : Deref , F : Deref , L : Deref > (
243
249
channelmanager : & ChannelManager < Signer , M , T , K , F , L > , keys_manager : K , network : Currency ,
244
- amt_msat : Option < u64 > , description_hash : Sha256 ,
250
+ amt_msat : Option < u64 > , description_hash : Sha256 , invoice_expiry_delta_secs : u32
245
251
) -> Result < Invoice , SignOrCreationError < ( ) > >
246
252
where
247
253
M :: Target : chain:: Watch < Signer > ,
@@ -257,7 +263,8 @@ where
257
263
. expect ( "for the foreseeable future this shouldn't happen" ) ;
258
264
259
265
create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch (
260
- channelmanager, keys_manager, network, amt_msat, description_hash, duration,
266
+ channelmanager, keys_manager, network, amt_msat,
267
+ description_hash, duration, invoice_expiry_delta_secs
261
268
)
262
269
}
263
270
@@ -267,6 +274,7 @@ where
267
274
pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch < Signer : Sign , M : Deref , T : Deref , K : Deref , F : Deref , L : Deref > (
268
275
channelmanager : & ChannelManager < Signer , M , T , K , F , L > , keys_manager : K , network : Currency ,
269
276
amt_msat : Option < u64 > , description_hash : Sha256 , duration_since_epoch : Duration ,
277
+ invoice_expiry_delta_secs : u32
270
278
) -> Result < Invoice , SignOrCreationError < ( ) > >
271
279
where
272
280
M :: Target : chain:: Watch < Signer > ,
@@ -278,7 +286,7 @@ where
278
286
_create_invoice_from_channelmanager_and_duration_since_epoch (
279
287
channelmanager, keys_manager, network, amt_msat,
280
288
InvoiceDescription :: Hash ( & description_hash) ,
281
- duration_since_epoch,
289
+ duration_since_epoch, invoice_expiry_delta_secs
282
290
)
283
291
}
284
292
@@ -288,6 +296,7 @@ where
288
296
pub fn create_invoice_from_channelmanager_and_duration_since_epoch < Signer : Sign , M : Deref , T : Deref , K : Deref , F : Deref , L : Deref > (
289
297
channelmanager : & ChannelManager < Signer , M , T , K , F , L > , keys_manager : K , network : Currency ,
290
298
amt_msat : Option < u64 > , description : String , duration_since_epoch : Duration ,
299
+ invoice_expiry_delta_secs : u32
291
300
) -> Result < Invoice , SignOrCreationError < ( ) > >
292
301
where
293
302
M :: Target : chain:: Watch < Signer > ,
@@ -301,13 +310,14 @@ where
301
310
InvoiceDescription :: Direct (
302
311
& Description :: new ( description) . map_err ( SignOrCreationError :: CreationError ) ?,
303
312
) ,
304
- duration_since_epoch,
313
+ duration_since_epoch, invoice_expiry_delta_secs
305
314
)
306
315
}
307
316
308
317
fn _create_invoice_from_channelmanager_and_duration_since_epoch < Signer : Sign , M : Deref , T : Deref , K : Deref , F : Deref , L : Deref > (
309
318
channelmanager : & ChannelManager < Signer , M , T , K , F , L > , keys_manager : K , network : Currency ,
310
319
amt_msat : Option < u64 > , description : InvoiceDescription , duration_since_epoch : Duration ,
320
+ invoice_expiry_delta_secs : u32
311
321
) -> Result < Invoice , SignOrCreationError < ( ) > >
312
322
where
313
323
M :: Target : chain:: Watch < Signer > ,
@@ -321,7 +331,7 @@ where
321
331
// `create_inbound_payment` only returns an error if the amount is greater than the total bitcoin
322
332
// supply.
323
333
let ( payment_hash, payment_secret) = channelmanager
324
- . create_inbound_payment ( amt_msat, DEFAULT_EXPIRY_TIME . try_into ( ) . unwrap ( ) )
334
+ . create_inbound_payment ( amt_msat, invoice_expiry_delta_secs )
325
335
. map_err ( |( ) | SignOrCreationError :: CreationError ( CreationError :: InvalidAmount ) ) ?;
326
336
let our_node_pubkey = channelmanager. get_our_node_id ( ) ;
327
337
@@ -338,7 +348,8 @@ where
338
348
. payment_hash ( Hash :: from_slice ( & payment_hash. 0 ) . unwrap ( ) )
339
349
. payment_secret ( payment_secret)
340
350
. basic_mpp ( )
341
- . min_final_cltv_expiry ( MIN_FINAL_CLTV_EXPIRY . into ( ) ) ;
351
+ . min_final_cltv_expiry ( MIN_FINAL_CLTV_EXPIRY . into ( ) )
352
+ . expiry_time ( Duration :: from_secs ( invoice_expiry_delta_secs. into ( ) ) ) ;
342
353
if let Some ( amt) = amt_msat {
343
354
invoice = invoice. amount_milli_satoshis ( amt) ;
344
355
}
@@ -529,10 +540,11 @@ mod test {
529
540
create_unannounced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100000 , 10001 , InitFeatures :: known ( ) , InitFeatures :: known ( ) ) ;
530
541
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch (
531
542
& nodes[ 1 ] . node , nodes[ 1 ] . keys_manager , Currency :: BitcoinTestnet , Some ( 10_000 ) , "test" . to_string ( ) ,
532
- Duration :: from_secs ( 1234567 ) ) . unwrap ( ) ;
543
+ Duration :: from_secs ( 1234567 ) , 3600 ) . unwrap ( ) ;
533
544
assert_eq ! ( invoice. amount_pico_btc( ) , Some ( 100_000 ) ) ;
534
545
assert_eq ! ( invoice. min_final_cltv_expiry( ) , MIN_FINAL_CLTV_EXPIRY as u64 ) ;
535
546
assert_eq ! ( invoice. description( ) , InvoiceDescription :: Direct ( & Description ( "test" . to_string( ) ) ) ) ;
547
+ assert_eq ! ( invoice. expiry_time( ) , Duration :: from_secs( 3600 ) ) ;
536
548
537
549
// Invoice SCIDs should always use inbound SCID aliases over the real channel ID, if one is
538
550
// available.
@@ -593,7 +605,7 @@ mod test {
593
605
let description_hash = crate :: Sha256 ( Hash :: hash ( "Testing description_hash" . as_bytes ( ) ) ) ;
594
606
let invoice = :: utils:: create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch (
595
607
& nodes[ 1 ] . node , nodes[ 1 ] . keys_manager , Currency :: BitcoinTestnet , Some ( 10_000 ) ,
596
- description_hash, Duration :: from_secs ( 1234567 ) ,
608
+ description_hash, Duration :: from_secs ( 1234567 ) , 3600
597
609
) . unwrap ( ) ;
598
610
assert_eq ! ( invoice. amount_pico_btc( ) , Some ( 100_000 ) ) ;
599
611
assert_eq ! ( invoice. min_final_cltv_expiry( ) , MIN_FINAL_CLTV_EXPIRY as u64 ) ;
@@ -753,7 +765,7 @@ mod test {
753
765
) {
754
766
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch (
755
767
& invoice_node. node , invoice_node. keys_manager , Currency :: BitcoinTestnet , invoice_amt, "test" . to_string ( ) ,
756
- Duration :: from_secs ( 1234567 ) ) . unwrap ( ) ;
768
+ Duration :: from_secs ( 1234567 ) , 3600 ) . unwrap ( ) ;
757
769
let hints = invoice. private_routes ( ) ;
758
770
759
771
for hint in hints {
0 commit comments