Skip to content

Commit 7f52d26

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 f779bc0 commit 7f52d26

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

lightning/src/offers/invoice_request.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ use bitcoin::network::constants::Network;
5757
use bitcoin::secp256k1::{Message, PublicKey};
5858
use bitcoin::secp256k1::schnorr::Signature;
5959
use core::convert::TryFrom;
60-
use core::time::Duration;
6160
use crate::io;
6261
use crate::ln::PaymentHash;
6362
use crate::ln::features::InvoiceRequestFeatures;
@@ -326,23 +325,35 @@ impl InvoiceRequest {
326325
/// Creates an [`Invoice`] for the request with the given required fields.
327326
///
328327
/// 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.
328+
/// calling this method in `std` builds. For `no-std` builds, a final [`Duration`] parameter
329+
/// must be given, which is used to set [`Invoice::created_at`] since [`std::time::SystemTime`]
330+
/// is not available.
331+
///
332+
/// The caller is expected to remember the preimage of `payment_hash` in order to claim a payment
333+
/// for the invoice.
331334
///
332335
/// The `payment_paths` parameter is useful for maintaining the payment recipient's privacy. It
333336
/// must contain one or more elements.
334337
///
335338
/// Errors if the request contains unknown required features.
336339
///
340+
/// [`Duration`]: core::time::Duration
337341
/// [`Invoice`]: crate::offers::invoice::Invoice
342+
/// [`Invoice::created_at`]: crate::offers::invoice::Invoice::created_at
338343
pub fn respond_with(
339-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
340-
payment_hash: PaymentHash
344+
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
345+
#[cfg(not(feature = "std"))]
346+
created_at: core::time::Duration
341347
) -> Result<InvoiceBuilder, SemanticError> {
342348
if self.features().requires_unknown_bits() {
343349
return Err(SemanticError::UnknownRequiredFeatures);
344350
}
345351

352+
#[cfg(feature = "std")]
353+
let created_at = std::time::SystemTime::now()
354+
.duration_since(std::time::SystemTime::UNIX_EPOCH)
355+
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
356+
346357
InvoiceBuilder::for_offer(self, payment_paths, created_at, payment_hash)
347358
}
348359

lightning/src/offers/refund.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,11 @@ 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 in `std` builds. For `no-std` builds, a final [`Duration`] parameter
305+
/// must be given, which is used to set [`Invoice::created_at`] since [`std::time::SystemTime`]
306+
/// is not available.
307+
///
308+
/// The caller is expected to remember the preimage of `payment_hash` in order to
305309
/// claim a payment for the invoice.
306310
///
307311
/// The `signing_pubkey` is required to sign the invoice since refunds are not in response to an
@@ -313,14 +317,22 @@ impl Refund {
313317
/// Errors if the request contains unknown required features.
314318
///
315319
/// [`Invoice`]: crate::offers::invoice::Invoice
320+
/// [`Invoice::created_at`]: crate::offers::invoice::Invoice::created_at
316321
pub fn respond_with(
317-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
318-
payment_hash: PaymentHash, signing_pubkey: PublicKey
322+
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
323+
signing_pubkey: PublicKey,
324+
#[cfg(not(feature = "std"))]
325+
created_at: Duration
319326
) -> Result<InvoiceBuilder, SemanticError> {
320327
if self.features().requires_unknown_bits() {
321328
return Err(SemanticError::UnknownRequiredFeatures);
322329
}
323330

331+
#[cfg(feature = "std")]
332+
let created_at = std::time::SystemTime::now()
333+
.duration_since(std::time::SystemTime::UNIX_EPOCH)
334+
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
335+
324336
InvoiceBuilder::for_refund(self, payment_paths, created_at, payment_hash, signing_pubkey)
325337
}
326338

0 commit comments

Comments
 (0)