Skip to content

Commit 7351ae7

Browse files
committed
Remove std::SystemTime from create_phantom_invoice, ref #1978
1 parent 828c776 commit 7351ae7

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

lightning-invoice/src/utils.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use secp256k1::PublicKey;
2121
use core::ops::Deref;
2222
use core::time::Duration;
2323

24-
#[cfg(feature = "std")]
2524
/// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
2625
/// See [`PhantomKeysManager`] for more information on phantom node payments.
2726
///
@@ -50,6 +49,9 @@ use core::time::Duration;
5049
/// Note that the provided `keys_manager`'s `NodeSigner` implementation must support phantom
5150
/// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
5251
/// requirement).
52+
///
53+
/// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
54+
/// available and the current time is supplied by the caller.
5355
///
5456
/// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
5557
/// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
@@ -60,7 +62,7 @@ use core::time::Duration;
6062
pub fn create_phantom_invoice<ES: Deref, NS: Deref, L: Deref>(
6163
amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, description: String,
6264
invoice_expiry_delta_secs: u32, phantom_route_hints: Vec<PhantomRouteHints>, entropy_source: ES,
63-
node_signer: NS, logger: L, network: Currency, min_final_cltv_expiry_delta: Option<u16>,
65+
node_signer: NS, logger: L, network: Currency, min_final_cltv_expiry_delta: Option<u16>, duration_since_epoch: Duration,
6466
) -> Result<Invoice, SignOrCreationError<()>>
6567
where
6668
ES::Target: EntropySource,
@@ -71,11 +73,10 @@ where
7173
let description = InvoiceDescription::Direct(&description,);
7274
_create_phantom_invoice::<ES, NS, L>(
7375
amt_msat, payment_hash, description, invoice_expiry_delta_secs, phantom_route_hints,
74-
entropy_source, node_signer, logger, network, min_final_cltv_expiry_delta,
76+
entropy_source, node_signer, logger, network, min_final_cltv_expiry_delta, duration_since_epoch,
7577
)
7678
}
7779

78-
#[cfg(feature = "std")]
7980
/// Utility to create an invoice that can be paid to one of multiple nodes, or a "phantom invoice."
8081
/// See [`PhantomKeysManager`] for more information on phantom node payments.
8182
///
@@ -101,6 +102,9 @@ where
101102
/// Note that the provided `keys_manager`'s `NodeSigner` implementation must support phantom
102103
/// invoices in its `sign_invoice` implementation ([`PhantomKeysManager`] satisfies this
103104
/// requirement).
105+
///
106+
/// This version can be used in a `no_std` environment, where [`std::time::SystemTime`] is not
107+
/// available and the current time is supplied by the caller.
104108
///
105109
/// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
106110
/// [`ChannelManager::get_phantom_route_hints`]: lightning::ln::channelmanager::ChannelManager::get_phantom_route_hints
@@ -110,7 +114,7 @@ where
110114
pub fn create_phantom_invoice_with_description_hash<ES: Deref, NS: Deref, L: Deref>(
111115
amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, invoice_expiry_delta_secs: u32,
112116
description_hash: Sha256, phantom_route_hints: Vec<PhantomRouteHints>, entropy_source: ES,
113-
node_signer: NS, logger: L, network: Currency, min_final_cltv_expiry_delta: Option<u16>,
117+
node_signer: NS, logger: L, network: Currency, min_final_cltv_expiry_delta: Option<u16>, duration_since_epoch: Duration,
114118
) -> Result<Invoice, SignOrCreationError<()>>
115119
where
116120
ES::Target: EntropySource,
@@ -120,22 +124,20 @@ where
120124
_create_phantom_invoice::<ES, NS, L>(
121125
amt_msat, payment_hash, InvoiceDescription::Hash(&description_hash),
122126
invoice_expiry_delta_secs, phantom_route_hints, entropy_source, node_signer, logger, network,
123-
min_final_cltv_expiry_delta,
127+
min_final_cltv_expiry_delta, duration_since_epoch,
124128
)
125129
}
126130

127-
#[cfg(feature = "std")]
128131
fn _create_phantom_invoice<ES: Deref, NS: Deref, L: Deref>(
129132
amt_msat: Option<u64>, payment_hash: Option<PaymentHash>, description: InvoiceDescription,
130133
invoice_expiry_delta_secs: u32, phantom_route_hints: Vec<PhantomRouteHints>, entropy_source: ES,
131-
node_signer: NS, logger: L, network: Currency, min_final_cltv_expiry_delta: Option<u16>,
134+
node_signer: NS, logger: L, network: Currency, min_final_cltv_expiry_delta: Option<u16>, duration_since_epoch: Duration,
132135
) -> Result<Invoice, SignOrCreationError<()>>
133136
where
134137
ES::Target: EntropySource,
135138
NS::Target: NodeSigner,
136139
L::Target: Logger,
137140
{
138-
use std::time::{SystemTime, UNIX_EPOCH};
139141

140142
if phantom_route_hints.len() == 0 {
141143
return Err(SignOrCreationError::CreationError(
@@ -162,9 +164,7 @@ where
162164
amt_msat,
163165
payment_hash,
164166
invoice_expiry_delta_secs,
165-
SystemTime::now()
166-
.duration_since(UNIX_EPOCH)
167-
.expect("Time must be > 1970")
167+
duration_since_epoch
168168
.as_secs(),
169169
min_final_cltv_expiry_delta,
170170
)
@@ -176,9 +176,7 @@ where
176176
amt_msat,
177177
invoice_expiry_delta_secs,
178178
&entropy_source,
179-
SystemTime::now()
180-
.duration_since(UNIX_EPOCH)
181-
.expect("Time must be > 1970")
179+
duration_since_epoch
182180
.as_secs(),
183181
min_final_cltv_expiry_delta,
184182
)
@@ -1072,7 +1070,7 @@ mod test {
10721070
crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestLogger>(
10731071
Some(payment_amt), payment_hash, "test".to_string(), non_default_invoice_expiry_secs,
10741072
route_hints, &nodes[1].keys_manager, &nodes[1].keys_manager, &nodes[1].logger,
1075-
Currency::BitcoinTestnet, None,
1073+
Currency::BitcoinTestnet, None, Duration::from_secs(1234567)
10761074
).unwrap();
10771075
let (payment_hash, payment_secret) = (PaymentHash(invoice.payment_hash().into_inner()), *invoice.payment_secret());
10781076
let payment_preimage = if user_generated_pmt_hash {
@@ -1182,7 +1180,7 @@ mod test {
11821180
let invoice = crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface,
11831181
&test_utils::TestKeysInterface, &test_utils::TestLogger>(Some(payment_amt), Some(payment_hash),
11841182
"test".to_string(), 3600, route_hints, &nodes[1].keys_manager, &nodes[1].keys_manager,
1185-
&nodes[1].logger, Currency::BitcoinTestnet, None).unwrap();
1183+
&nodes[1].logger, Currency::BitcoinTestnet, None, Duration::from_secs(1234567)).unwrap();
11861184

11871185
let chan_0_1 = &nodes[1].node.list_usable_channels()[0];
11881186
assert_eq!(invoice.route_hints()[0].0[0].htlc_minimum_msat, chan_0_1.inbound_htlc_minimum_msat);
@@ -1214,7 +1212,7 @@ mod test {
12141212
>(
12151213
Some(payment_amt), None, non_default_invoice_expiry_secs, description_hash,
12161214
route_hints, &nodes[1].keys_manager, &nodes[1].keys_manager, &nodes[1].logger,
1217-
Currency::BitcoinTestnet, None,
1215+
Currency::BitcoinTestnet, None, Duration::from_secs(1234567),
12181216
)
12191217
.unwrap();
12201218
assert_eq!(invoice.amount_pico_btc(), Some(200_000));
@@ -1240,10 +1238,11 @@ mod test {
12401238
let payment_hash = Some(PaymentHash(Sha256::hash(&user_payment_preimage.0[..]).into_inner()));
12411239
let non_default_invoice_expiry_secs = 4200;
12421240
let min_final_cltv_expiry_delta = Some(100);
1241+
let duration_since_epoch = Duration::from_secs(1234567);
12431242
let invoice = crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface,
12441243
&test_utils::TestKeysInterface, &test_utils::TestLogger>(Some(payment_amt), payment_hash,
12451244
"".to_string(), non_default_invoice_expiry_secs, route_hints, &nodes[1].keys_manager, &nodes[1].keys_manager,
1246-
&nodes[1].logger, Currency::BitcoinTestnet, min_final_cltv_expiry_delta).unwrap();
1245+
&nodes[1].logger, Currency::BitcoinTestnet, min_final_cltv_expiry_delta, duration_since_epoch).unwrap();
12471246
assert_eq!(invoice.amount_pico_btc(), Some(200_000));
12481247
assert_eq!(invoice.min_final_cltv_expiry_delta(), (min_final_cltv_expiry_delta.unwrap() + 3) as u64);
12491248
assert_eq!(invoice.expiry_time(), Duration::from_secs(non_default_invoice_expiry_secs.into()));
@@ -1557,7 +1556,7 @@ mod test {
15571556
let invoice = crate::utils::create_phantom_invoice::<&test_utils::TestKeysInterface,
15581557
&test_utils::TestKeysInterface, &test_utils::TestLogger>(invoice_amt, None, "test".to_string(),
15591558
3600, phantom_route_hints, &invoice_node.keys_manager, &invoice_node.keys_manager,
1560-
&invoice_node.logger, Currency::BitcoinTestnet, None).unwrap();
1559+
&invoice_node.logger, Currency::BitcoinTestnet, None, Duration::from_secs(1234567)).unwrap();
15611560

15621561
let invoice_hints = invoice.private_routes();
15631562

0 commit comments

Comments
 (0)