Skip to content

Commit f464499

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 f464499

File tree

1 file changed

+44
-89
lines changed

1 file changed

+44
-89
lines changed

lightning/src/ln/invoice_utils.rs

Lines changed: 44 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,16 @@ 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`]
493439
/// This version allows for providing custom [`PaymentHash`] and description hash for the invoice.
494440
///
495441
/// This may be useful if you're building an on-chain swap or involving another protocol where
496442
/// the payment hash is also involved outside the scope of lightning and want to set the
497443
/// 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>(
444+
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>(
499445
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,
446+
network: Currency, amt_msat: Option<u64>, description_hash: Sha256,
501447
invoice_expiry_delta_secs: u32, payment_hash: PaymentHash, min_final_cltv_expiry_delta: Option<u16>,
502448
) -> Result<Bolt11Invoice, SignOrCreationError<()>>
503449
where
@@ -511,6 +457,11 @@ where
511457
MR::Target: MessageRouter,
512458
L::Target: Logger,
513459
{
460+
use std::time::SystemTime;
461+
let duration_since_epoch = SystemTime::now()
462+
.duration_since(SystemTime::UNIX_EPOCH)
463+
.expect("for the foreseeable future this shouldn't happen");
464+
514465
let payment_secret = channelmanager
515466
.create_inbound_payment_for_hash(payment_hash, amt_msat, invoice_expiry_delta_secs,
516467
min_final_cltv_expiry_delta)
@@ -523,14 +474,15 @@ where
523474
)
524475
}
525476

526-
/// See [`create_invoice_from_channelmanager_and_duration_since_epoch`]
477+
#[cfg(feature = "std")]
478+
/// See [`create_invoice_from_channelmanager`]
527479
/// This version allows for providing a custom [`PaymentHash`] for the invoice.
528480
/// This may be useful if you're building an on-chain swap or involving another protocol where
529481
/// 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>(
482+
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>(
531483
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>,
484+
network: Currency, amt_msat: Option<u64>, description: String, invoice_expiry_delta_secs: u32,
485+
payment_hash: PaymentHash, min_final_cltv_expiry_delta: Option<u16>,
534486
) -> Result<Bolt11Invoice, SignOrCreationError<()>>
535487
where
536488
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
@@ -543,6 +495,11 @@ where
543495
MR::Target: MessageRouter,
544496
L::Target: Logger,
545497
{
498+
use std::time::SystemTime;
499+
let duration_since_epoch = SystemTime::now()
500+
.duration_since(SystemTime::UNIX_EPOCH)
501+
.expect("for the foreseeable future this shouldn't happen");
502+
546503
let payment_secret = channelmanager
547504
.create_inbound_payment_for_hash(payment_hash, amt_msat, invoice_expiry_delta_secs,
548505
min_final_cltv_expiry_delta)
@@ -913,10 +870,10 @@ mod test {
913870
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
914871
create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 10001);
915872
let non_default_invoice_expiry_secs = 4200;
916-
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
873+
let invoice = create_invoice_from_channelmanager(
917874
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();
875+
Some(10_000), "test".to_string(), non_default_invoice_expiry_secs, None,
876+
).unwrap();
920877
assert_eq!(invoice.amount_milli_satoshis(), Some(10_000));
921878
// If no `min_final_cltv_expiry_delta` is specified, then it should be `MIN_FINAL_CLTV_EXPIRY_DELTA`.
922879
assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64);
@@ -964,9 +921,9 @@ mod test {
964921
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
965922
let custom_min_final_cltv_expiry_delta = Some(50);
966923

967-
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
924+
let invoice = create_invoice_from_channelmanager(
968925
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
969-
Some(10_000), "".into(), Duration::from_secs(1234567), 3600,
926+
Some(10_000), "".into(), 3600,
970927
if with_custom_delta { custom_min_final_cltv_expiry_delta } else { None },
971928
).unwrap();
972929
assert_eq!(invoice.min_final_cltv_expiry_delta(), if with_custom_delta {
@@ -987,10 +944,9 @@ mod test {
987944
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
988945
let custom_min_final_cltv_expiry_delta = Some(21);
989946

990-
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
947+
let invoice = create_invoice_from_channelmanager(
991948
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,
949+
Some(10_000), "".into(), 3600, custom_min_final_cltv_expiry_delta,
994950
).unwrap();
995951
assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64);
996952
}
@@ -1002,9 +958,9 @@ mod test {
1002958
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1003959
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1004960
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(
961+
let invoice = create_invoice_from_channelmanager_with_description_hash(
1006962
nodes[1].node, nodes[1].keys_manager, nodes[1].logger, Currency::BitcoinTestnet,
1007-
Some(10_000), description_hash, Duration::from_secs(1234567), 3600, None,
963+
Some(10_000), description_hash, 3600, None,
1008964
).unwrap();
1009965
assert_eq!(invoice.amount_milli_satoshis(), Some(10_000));
1010966
assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64);
@@ -1018,10 +974,9 @@ mod test {
1018974
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1019975
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1020976
let payment_hash = PaymentHash([0; 32]);
1021-
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch_with_payment_hash(
977+
let invoice = create_invoice_from_channelmanager_with_payment_hash(
1022978
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,
979+
Some(10_000), "test".to_string(), 3600, payment_hash, None,
1025980
).unwrap();
1026981
assert_eq!(invoice.amount_milli_satoshis(), Some(10_000));
1027982
assert_eq!(invoice.min_final_cltv_expiry_delta(), MIN_FINAL_CLTV_EXPIRY_DELTA as u64);
@@ -1310,10 +1265,10 @@ mod test {
13101265
invoice_node: &Node<'a, 'b, 'c>,
13111266
mut chan_ids_to_match: HashSet<u64>
13121267
) {
1313-
let invoice = create_invoice_from_channelmanager_and_duration_since_epoch(
1268+
let invoice = create_invoice_from_channelmanager(
13141269
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();
1270+
Currency::BitcoinTestnet, invoice_amt, "test".to_string(), 3600, None,
1271+
).unwrap();
13171272
let hints = invoice.private_routes();
13181273

13191274
for hint in hints {
@@ -1948,9 +1903,9 @@ mod test {
19481903
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
19491904
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
19501905
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1951-
let result = create_invoice_from_channelmanager_and_duration_since_epoch(
1906+
let result = create_invoice_from_channelmanager(
19521907
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),
1908+
Some(10_000), "Some description".into(), 3600, Some(MIN_FINAL_CLTV_EXPIRY_DELTA - 4),
19541909
);
19551910
match result {
19561911
Err(SignOrCreationError::CreationError(CreationError::MinFinalCltvExpiryDeltaTooShort)) => {},

0 commit comments

Comments
 (0)