Skip to content

Commit f3da410

Browse files
committed
Use SystemTime::now() for Invoice creation time
For std builds, Invoice::created_at can be automatically set upon construction using SystemTime::now() offset by SystemTime::UNIX_EPOCH. Change InvoiceRequest::respond_with and Refund::respond_with to only take a created_at parameter in no-std builds.
1 parent edc9441 commit f3da410

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lightning/src/offers/invoice_request.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,10 @@ impl InvoiceRequest {
326326
/// Creates an [`Invoice`] for the request with the given required fields.
327327
///
328328
/// Unless [`InvoiceBuilder::relative_expiry`] is set, the invoice will expire two hours after
329-
/// `created_at`. The caller is expected to remember the preimage of `payment_hash` in order to
330-
/// claim a payment for the invoice.
329+
/// calling this method (or after `created_at` in `no-std` builds).
330+
///
331+
/// The caller is expected to remember the preimage of `payment_hash` in order to claim a payment
332+
/// for the invoice.
331333
///
332334
/// The `payment_paths` parameter is useful for maintaining the payment recipient's privacy. It
333335
/// must contain one or more elements.
@@ -336,13 +338,19 @@ impl InvoiceRequest {
336338
///
337339
/// [`Invoice`]: crate::offers::invoice::Invoice
338340
pub fn respond_with(
339-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
340-
payment_hash: PaymentHash
341+
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
342+
#[cfg(not(feature = "std"))]
343+
created_at: Duration
341344
) -> Result<InvoiceBuilder, SemanticError> {
342345
if self.features().requires_unknown_bits() {
343346
return Err(SemanticError::UnknownRequiredFeatures);
344347
}
345348

349+
#[cfg(feature = "std")]
350+
let created_at = std::time::SystemTime::now()
351+
.duration_since(std::time::SystemTime::UNIX_EPOCH)
352+
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
353+
346354
InvoiceBuilder::for_offer(self, payment_paths, created_at, payment_hash)
347355
}
348356

lightning/src/offers/refund.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ impl Refund {
301301
/// Creates an [`Invoice`] for the refund with the given required fields.
302302
///
303303
/// Unless [`InvoiceBuilder::relative_expiry`] is set, the invoice will expire two hours after
304-
/// `created_at`. The caller is expected to remember the preimage of `payment_hash` in order to
304+
/// calling this method (or after `created_at` in `no-std` builds).
305+
///
306+
/// The caller is expected to remember the preimage of `payment_hash` in order to
305307
/// claim a payment for the invoice.
306308
///
307309
/// The `signing_pubkey` is required to sign the invoice since refunds are not in response to an
@@ -314,13 +316,20 @@ impl Refund {
314316
///
315317
/// [`Invoice`]: crate::offers::invoice::Invoice
316318
pub fn respond_with(
317-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
318-
payment_hash: PaymentHash, signing_pubkey: PublicKey
319+
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
320+
signing_pubkey: PublicKey,
321+
#[cfg(not(feature = "std"))]
322+
created_at: Duration
319323
) -> Result<InvoiceBuilder, SemanticError> {
320324
if self.features().requires_unknown_bits() {
321325
return Err(SemanticError::UnknownRequiredFeatures);
322326
}
323327

328+
#[cfg(feature = "std")]
329+
let created_at = std::time::SystemTime::now()
330+
.duration_since(std::time::SystemTime::UNIX_EPOCH)
331+
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
332+
324333
InvoiceBuilder::for_refund(self, payment_paths, created_at, payment_hash, signing_pubkey)
325334
}
326335

0 commit comments

Comments
 (0)