Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 717047c

Browse files
committedMay 10, 2022
Add expiry to non-phantom invoice utils
1 parent 1eef7c1 commit 717047c

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed
 

‎lightning-invoice/src/payment.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1558,7 +1558,7 @@ mod tests {
15581558

15591559
assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager_and_duration_since_epoch(
15601560
&nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string(),
1561-
duration_since_epoch()).unwrap())
1561+
duration_since_epoch(), 3600).unwrap())
15621562
.is_ok());
15631563
let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
15641564
assert_eq!(htlc_msgs.len(), 2);
@@ -1604,7 +1604,7 @@ mod tests {
16041604

16051605
assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager_and_duration_since_epoch(
16061606
&nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string(),
1607-
duration_since_epoch()).unwrap())
1607+
duration_since_epoch(), 3600).unwrap())
16081608
.is_ok());
16091609
let htlc_msgs = nodes[0].node.get_and_clear_pending_msg_events();
16101610
assert_eq!(htlc_msgs.len(), 2);
@@ -1686,7 +1686,7 @@ mod tests {
16861686

16871687
assert!(invoice_payer.pay_invoice(&create_invoice_from_channelmanager_and_duration_since_epoch(
16881688
&nodes[1].node, nodes[1].keys_manager, Currency::Bitcoin, Some(100_010_000), "Invoice".to_string(),
1689-
duration_since_epoch()).unwrap())
1689+
duration_since_epoch(), 3600).unwrap())
16901690
.is_ok());
16911691
let htlc_updates = SendEvent::from_node(&nodes[0]);
16921692
check_added_monitors!(nodes[0], 1);

‎lightning-invoice/src/utils.rs

+25-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Convenient utilities to create an invoice.
22
3-
use {CreationError, Currency, DEFAULT_EXPIRY_TIME, Invoice, InvoiceBuilder, SignOrCreationError};
3+
use {CreationError, Currency, Invoice, InvoiceBuilder, SignOrCreationError};
44
use payment::{Payer, Router};
55

66
use crate::{prelude::*, Description, InvoiceDescription, Sha256};
@@ -20,7 +20,6 @@ use lightning::routing::network_graph::{NetworkGraph, RoutingFees};
2020
use lightning::routing::router::{Route, RouteHint, RouteHintHop, RouteParameters, find_route};
2121
use lightning::util::logger::Logger;
2222
use secp256k1::PublicKey;
23-
use core::convert::TryInto;
2423
use core::ops::Deref;
2524
use core::time::Duration;
2625
use sync::Mutex;
@@ -213,9 +212,12 @@ fn _create_phantom_invoice<Signer: Sign, K: Deref>(
213212
/// method stores the invoice's payment secret and preimage in `ChannelManager`, so (a) the user
214213
/// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
215214
/// 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.
216218
pub fn create_invoice_from_channelmanager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
217219
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
219221
) -> Result<Invoice, SignOrCreationError<()>>
220222
where
221223
M::Target: chain::Watch<Signer>,
@@ -228,7 +230,8 @@ where
228230
let duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
229231
.expect("for the foreseeable future this shouldn't happen");
230232
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
232235
)
233236
}
234237

@@ -239,9 +242,12 @@ where
239242
/// doesn't have to store preimage/payment secret information and (b) `ChannelManager` can verify
240243
/// that the payment secret is valid when the invoice is paid.
241244
/// 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.
242248
pub fn create_invoice_from_channelmanager_with_description_hash<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
243249
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
245251
) -> Result<Invoice, SignOrCreationError<()>>
246252
where
247253
M::Target: chain::Watch<Signer>,
@@ -257,7 +263,8 @@ where
257263
.expect("for the foreseeable future this shouldn't happen");
258264

259265
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
261268
)
262269
}
263270

@@ -267,6 +274,7 @@ where
267274
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>(
268275
channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, network: Currency,
269276
amt_msat: Option<u64>, description_hash: Sha256, duration_since_epoch: Duration,
277+
invoice_expiry_delta_secs: u32
270278
) -> Result<Invoice, SignOrCreationError<()>>
271279
where
272280
M::Target: chain::Watch<Signer>,
@@ -278,7 +286,7 @@ where
278286
_create_invoice_from_channelmanager_and_duration_since_epoch(
279287
channelmanager, keys_manager, network, amt_msat,
280288
InvoiceDescription::Hash(&description_hash),
281-
duration_since_epoch,
289+
duration_since_epoch, invoice_expiry_delta_secs
282290
)
283291
}
284292

@@ -288,6 +296,7 @@ where
288296
pub fn create_invoice_from_channelmanager_and_duration_since_epoch<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
289297
channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, network: Currency,
290298
amt_msat: Option<u64>, description: String, duration_since_epoch: Duration,
299+
invoice_expiry_delta_secs: u32
291300
) -> Result<Invoice, SignOrCreationError<()>>
292301
where
293302
M::Target: chain::Watch<Signer>,
@@ -301,13 +310,14 @@ where
301310
InvoiceDescription::Direct(
302311
&Description::new(description).map_err(SignOrCreationError::CreationError)?,
303312
),
304-
duration_since_epoch,
313+
duration_since_epoch, invoice_expiry_delta_secs
305314
)
306315
}
307316

308317
fn _create_invoice_from_channelmanager_and_duration_since_epoch<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
309318
channelmanager: &ChannelManager<Signer, M, T, K, F, L>, keys_manager: K, network: Currency,
310319
amt_msat: Option<u64>, description: InvoiceDescription, duration_since_epoch: Duration,
320+
invoice_expiry_delta_secs: u32
311321
) -> Result<Invoice, SignOrCreationError<()>>
312322
where
313323
M::Target: chain::Watch<Signer>,
@@ -321,7 +331,7 @@ where
321331
// `create_inbound_payment` only returns an error if the amount is greater than the total bitcoin
322332
// supply.
323333
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)
325335
.map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
326336
let our_node_pubkey = channelmanager.get_our_node_id();
327337

@@ -338,7 +348,8 @@ where
338348
.payment_hash(Hash::from_slice(&payment_hash.0).unwrap())
339349
.payment_secret(payment_secret)
340350
.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()));
342353
if let Some(amt) = amt_msat {
343354
invoice = invoice.amount_milli_satoshis(amt);
344355
}
@@ -529,10 +540,11 @@ mod test {
529540
create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001, InitFeatures::known(), InitFeatures::known());
530541
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
531542
&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();
533544
assert_eq!(invoice.amount_pico_btc(), Some(100_000));
534545
assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
535546
assert_eq!(invoice.description(), InvoiceDescription::Direct(&Description("test".to_string())));
547+
assert_eq!(invoice.expiry_time(), Duration::from_secs(3600));
536548

537549
// Invoice SCIDs should always use inbound SCID aliases over the real channel ID, if one is
538550
// available.
@@ -593,7 +605,7 @@ mod test {
593605
let description_hash = crate::Sha256(Hash::hash("Testing description_hash".as_bytes()));
594606
let invoice = ::utils::create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(
595607
&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
597609
).unwrap();
598610
assert_eq!(invoice.amount_pico_btc(), Some(100_000));
599611
assert_eq!(invoice.min_final_cltv_expiry(), MIN_FINAL_CLTV_EXPIRY as u64);
@@ -753,7 +765,7 @@ mod test {
753765
) {
754766
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
755767
&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();
757769
let hints = invoice.private_routes();
758770

759771
for hint in hints {

0 commit comments

Comments
 (0)
Please sign in to comment.