Skip to content

Commit 0ea225c

Browse files
committed
Remove no-std support from invoice_utils functions
The upcoming ChannelManager::create_bolt11_invoice will not support setting a specific creation time, so remove that functionality from the invoice_utils functions. This will avoid duplicate code when deprecating.
1 parent 2a95402 commit 0ea225c

File tree

1 file changed

+46
-89
lines changed

1 file changed

+46
-89
lines changed

lightning/src/ln/invoice_utils.rs

Lines changed: 46 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -346,11 +346,16 @@ where
346346
L::Target: Logger,
347347
{
348348
use std::time::SystemTime;
349-
let duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
349+
let duration_since_epoch = SystemTime::now()
350+
.duration_since(SystemTime::UNIX_EPOCH)
350351
.expect("for the foreseeable future this shouldn't happen");
351-
create_invoice_from_channelmanager_and_duration_since_epoch(
352+
353+
_create_invoice_from_channelmanager_and_duration_since_epoch(
352354
channelmanager, node_signer, logger, network, amt_msat,
353-
description, duration, invoice_expiry_delta_secs, min_final_cltv_expiry_delta,
355+
Bolt11InvoiceDescription::Direct(
356+
Description::new(description).map_err(SignOrCreationError::CreationError)?,
357+
),
358+
duration_since_epoch, invoice_expiry_delta_secs, min_final_cltv_expiry_delta,
354359
)
355360
}
356361

@@ -388,77 +393,17 @@ where
388393
L::Target: Logger,
389394
{
390395
use std::time::SystemTime;
391-
392-
let duration = SystemTime::now()
396+
let duration_since_epoch = SystemTime::now()
393397
.duration_since(SystemTime::UNIX_EPOCH)
394398
.expect("for the foreseeable future this shouldn't happen");
395399

396-
create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(
397-
channelmanager, node_signer, logger, network, amt_msat,
398-
description_hash, duration, invoice_expiry_delta_secs, min_final_cltv_expiry_delta,
399-
)
400-
}
401-
402-
/// Utility to construct an invoice. Generally, unless you want to do something like a custom
403-
/// `cltv_expiry`, this is what you should be using to create an invoice.
404-
#[cfg_attr(feature = "std", doc = "")]
405-
#[cfg_attr(feature = "std", doc = "See [`create_invoice_from_channelmanager_with_description_hash`] for more information.")]
406-
#[cfg_attr(feature = "std", doc = "")]
407-
#[cfg_attr(feature = "std", doc = "This can be used in a `no_std` environment, where [`std::time::SystemTime`] is not available and the current time is supplied by the caller.")]
408-
pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>(
409-
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, MR, L>, node_signer: NS, logger: L,
410-
network: Currency, amt_msat: Option<u64>, description_hash: Sha256,
411-
duration_since_epoch: Duration, invoice_expiry_delta_secs: u32, min_final_cltv_expiry_delta: Option<u16>,
412-
) -> Result<Bolt11Invoice, SignOrCreationError<()>>
413-
where
414-
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
415-
T::Target: BroadcasterInterface,
416-
ES::Target: EntropySource,
417-
NS::Target: NodeSigner,
418-
SP::Target: SignerProvider,
419-
F::Target: FeeEstimator,
420-
R::Target: Router,
421-
MR::Target: MessageRouter,
422-
L::Target: Logger,
423-
{
424400
_create_invoice_from_channelmanager_and_duration_since_epoch(
425401
channelmanager, node_signer, logger, network, amt_msat,
426402
Bolt11InvoiceDescription::Hash(description_hash),
427403
duration_since_epoch, invoice_expiry_delta_secs, min_final_cltv_expiry_delta,
428404
)
429405
}
430406

431-
/// Utility to construct an invoice. Generally, unless you want to do something like a custom
432-
/// `cltv_expiry`, this is what you should be using to create an invoice.
433-
#[cfg_attr(feature = "std", doc = "")]
434-
#[cfg_attr(feature = "std", doc = "See [`create_invoice_from_channelmanager`] for more information.")]
435-
#[cfg_attr(feature = "std", doc = "")]
436-
#[cfg_attr(feature = "std", doc = "This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not available and the current time is supplied by the caller.")]
437-
pub fn create_invoice_from_channelmanager_and_duration_since_epoch<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>(
438-
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, MR, L>, node_signer: NS, logger: L,
439-
network: Currency, amt_msat: Option<u64>, description: String, duration_since_epoch: Duration,
440-
invoice_expiry_delta_secs: u32, min_final_cltv_expiry_delta: Option<u16>,
441-
) -> Result<Bolt11Invoice, SignOrCreationError<()>>
442-
where
443-
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
444-
T::Target: BroadcasterInterface,
445-
ES::Target: EntropySource,
446-
NS::Target: NodeSigner,
447-
SP::Target: SignerProvider,
448-
F::Target: FeeEstimator,
449-
R::Target: Router,
450-
MR::Target: MessageRouter,
451-
L::Target: Logger,
452-
{
453-
_create_invoice_from_channelmanager_and_duration_since_epoch(
454-
channelmanager, node_signer, logger, network, amt_msat,
455-
Bolt11InvoiceDescription::Direct(
456-
Description::new(description).map_err(SignOrCreationError::CreationError)?,
457-
),
458-
duration_since_epoch, invoice_expiry_delta_secs, min_final_cltv_expiry_delta,
459-
)
460-
}
461-
462407
fn _create_invoice_from_channelmanager_and_duration_since_epoch<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>(
463408
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, MR, L>, node_signer: NS, logger: L,
464409
network: Currency, amt_msat: Option<u64>, description: Bolt11InvoiceDescription,
@@ -489,15 +434,17 @@ where
489434
invoice_expiry_delta_secs, payment_hash, payment_secret, min_final_cltv_expiry_delta)
490435
}
491436

492-
/// See [`create_invoice_from_channelmanager_and_duration_since_epoch`]
437+
#[cfg(feature = "std")]
438+
/// See [`create_invoice_from_channelmanager`].
439+
///
493440
/// This version allows for providing custom [`PaymentHash`] and description hash for the invoice.
494441
///
495442
/// This may be useful if you're building an on-chain swap or involving another protocol where
496443
/// the payment hash is also involved outside the scope of lightning and want to set the
497444
/// description hash.
498-
pub fn create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch_with_payment_hash<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>(
445+
pub fn create_invoice_from_channelmanager_with_description_hash_and_payment_hash<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>(
499446
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, MR, L>, node_signer: NS, logger: L,
500-
network: Currency, amt_msat: Option<u64>, description_hash: Sha256, duration_since_epoch: Duration,
447+
network: Currency, amt_msat: Option<u64>, description_hash: Sha256,
501448
invoice_expiry_delta_secs: u32, payment_hash: PaymentHash, min_final_cltv_expiry_delta: Option<u16>,
502449
) -> Result<Bolt11Invoice, SignOrCreationError<()>>
503450
where
@@ -511,6 +458,11 @@ where
511458
MR::Target: MessageRouter,
512459
L::Target: Logger,
513460
{
461+
use std::time::SystemTime;
462+
let duration_since_epoch = SystemTime::now()
463+
.duration_since(SystemTime::UNIX_EPOCH)
464+
.expect("for the foreseeable future this shouldn't happen");
465+
514466
let payment_secret = channelmanager
515467
.create_inbound_payment_for_hash(payment_hash, amt_msat, invoice_expiry_delta_secs,
516468
min_final_cltv_expiry_delta)
@@ -523,14 +475,16 @@ where
523475
)
524476
}
525477

526-
/// See [`create_invoice_from_channelmanager_and_duration_since_epoch`]
478+
#[cfg(feature = "std")]
479+
/// See [`create_invoice_from_channelmanager`].
480+
///
527481
/// This version allows for providing a custom [`PaymentHash`] for the invoice.
528482
/// This may be useful if you're building an on-chain swap or involving another protocol where
529483
/// the payment hash is also involved outside the scope of lightning.
530-
pub fn create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_hash<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>(
484+
pub fn create_invoice_from_channelmanager_with_payment_hash<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>(
531485
channelmanager: &ChannelManager<M, T, ES, NS, SP, F, R, MR, L>, node_signer: NS, logger: L,
532-
network: Currency, amt_msat: Option<u64>, description: String, duration_since_epoch: Duration,
533-
invoice_expiry_delta_secs: u32, payment_hash: PaymentHash, min_final_cltv_expiry_delta: Option<u16>,
486+
network: Currency, amt_msat: Option<u64>, description: String, invoice_expiry_delta_secs: u32,
487+
payment_hash: PaymentHash, min_final_cltv_expiry_delta: Option<u16>,
534488
) -> Result<Bolt11Invoice, SignOrCreationError<()>>
535489
where
536490
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
@@ -543,6 +497,11 @@ where
543497
MR::Target: MessageRouter,
544498
L::Target: Logger,
545499
{
500+
use std::time::SystemTime;
501+
let duration_since_epoch = SystemTime::now()
502+
.duration_since(SystemTime::UNIX_EPOCH)
503+
.expect("for the foreseeable future this shouldn't happen");
504+
546505
let payment_secret = channelmanager
547506
.create_inbound_payment_for_hash(payment_hash, amt_msat, invoice_expiry_delta_secs,
548507
min_final_cltv_expiry_delta)
@@ -913,10 +872,10 @@ mod test {
913872
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
914873
create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
915874
let non_default_invoice_expiry_secs = 4200;
916-
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
875+
let invoice = create_invoice_from_channelmanager(
917876
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
918-
Some(10_000), "test".to_string(), Duration::from_secs(1234567),
919-
non_default_invoice_expiry_secs, None).unwrap();
877+
Some(10_000), "test".to_string(), non_default_invoice_expiry_secs, None,
878+
).unwrap();
920879
assert_eq!(invoice.amount_milli_satoshis(), Some(10_000));
921880
// If no `min_final_cltv_expiry_delta` is specified, then it should be `MIN_FINAL_CLTV_EXPIRY_DELTA`.
922881
assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64);
@@ -964,9 +923,9 @@ mod test {
964923
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
965924
let custom_min_final_cltv_expiry_delta = Some(50);
966925

967-
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
926+
let invoice = create_invoice_from_channelmanager(
968927
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
969-
Some(10_000), "".into(), Duration::from_secs(1234567), 3600,
928+
Some(10_000), "".into(), 3600,
970929
if with_custom_delta { custom_min_final_cltv_expiry_delta } else { None },
971930
).unwrap();
972931
assert_eq!(invoice.min_final_cltv_expiry_delta(), if with_custom_delta {
@@ -987,10 +946,9 @@ mod test {
987946
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
988947
let custom_min_final_cltv_expiry_delta = Some(21);
989948

990-
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
949+
let invoice = create_invoice_from_channelmanager(
991950
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
992-
Some(10_000), "".into(), Duration::from_secs(1234567), 3600,
993-
custom_min_final_cltv_expiry_delta,
951+
Some(10_000), "".into(), 3600, custom_min_final_cltv_expiry_delta,
994952
).unwrap();
995953
assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64);
996954
}
@@ -1002,9 +960,9 @@ mod test {
1002960
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1003961
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1004962
let description_hash = Sha256(Hash::hash("Testing description_hash".as_bytes()));
1005-
let invoice = create_invoice_from_channelmanager_with_description_hash_and_duration_since_epoch(
963+
let invoice = create_invoice_from_channelmanager_with_description_hash(
1006964
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
1007-
Some(10_000), description_hash, Duration::from_secs(1234567), 3600, None,
965+
Some(10_000), description_hash, 3600, None,
1008966
).unwrap();
1009967
assert_eq!(invoice.amount_milli_satoshis(), Some(10_000));
1010968
assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64);
@@ -1018,10 +976,9 @@ mod test {
1018976
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1019977
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1020978
let payment_hash = PaymentHash([0; 32]);
1021-
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_hash(
979+
let invoice = create_invoice_from_channelmanager_with_payment_hash(
1022980
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
1023-
Some(10_000), "test".to_string(), Duration::from_secs(1234567), 3600,
1024-
payment_hash, None,
981+
Some(10_000), "test".to_string(), 3600, payment_hash, None,
1025982
).unwrap();
1026983
assert_eq!(invoice.amount_milli_satoshis(), Some(10_000));
1027984
assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64);
@@ -1310,10 +1267,10 @@ mod test {
13101267
invoice_node: &Node<'a, 'b, 'c>,
13111268
mut chan_ids_to_match: HashSet<u64>
13121269
) {
1313-
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
1270+
let invoice = create_invoice_from_channelmanager(
13141271
invoice_node.node, invoice_node.keys_manager, invoice_node.logger,
1315-
Currency::BitcoinTestnet, invoice_amt, "test".to_string(), Duration::from_secs(1234567),
1316-
3600, None).unwrap();
1272+
Currency::BitcoinTestnet, invoice_amt, "test".to_string(), 3600, None,
1273+
).unwrap();
13171274
let hints = invoice.private_routes();
13181275

13191276
for hint in hints {
@@ -1948,9 +1905,9 @@ mod test {
19481905
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
19491906
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
19501907
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1951-
let result = create_invoice_from_channelmanager_and_duration_since_epoch(
1908+
let result = create_invoice_from_channelmanager(
19521909
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
1953-
Some(10_000), "Some description".into(), Duration::from_secs(1234567), 3600, Some(MIN_FINAL_CLTV_EXPIRY_DELTA - 4),
1910+
Some(10_000), "Some description".into(), 3600, Some(MIN_FINAL_CLTV_EXPIRY_DELTA - 4),
19541911
);
19551912
match result {
19561913
Err(SignOrCreationError::CreationError(CreationError::MinFinalCltvExpiryDeltaTooShort)) => {},

0 commit comments

Comments
 (0)