Skip to content

Commit 454e998

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 f935996 commit 454e998

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lightning/src/offers/invoice_request.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,10 @@ impl InvoiceRequest {
323323
/// Creates an [`Invoice`] for the request with the given required fields.
324324
///
325325
/// Unless [`InvoiceBuilder::relative_expiry`] is set, the invoice will expire two hours after
326-
/// `created_at`. The caller is expected to remember the preimage of `payment_hash` in order to
327-
/// claim a payment for the invoice.
326+
/// calling this method (or after `created_at` in `no-std` builds).
327+
///
328+
/// The caller is expected to remember the preimage of `payment_hash` in order to claim a payment
329+
/// for the invoice.
328330
///
329331
/// The `payment_paths` parameter is useful for maintaining the payment recipient's privacy. It
330332
/// must contain one or more elements.
@@ -333,13 +335,19 @@ impl InvoiceRequest {
333335
///
334336
/// [`Invoice`]: crate::offers::invoice::Invoice
335337
pub fn respond_with(
336-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
337-
payment_hash: PaymentHash
338+
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
339+
#[cfg(not(feature = "std"))]
340+
created_at: Duration
338341
) -> Result<InvoiceBuilder, SemanticError> {
339342
if self.features().requires_unknown_bits() {
340343
return Err(SemanticError::UnknownRequiredFeatures);
341344
}
342345

346+
#[cfg(feature = "std")]
347+
let created_at = std::time::SystemTime::now()
348+
.duration_since(std::time::SystemTime::UNIX_EPOCH)
349+
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
350+
343351
InvoiceBuilder::for_offer(self, payment_paths, created_at, payment_hash)
344352
}
345353

lightning/src/offers/refund.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ impl Refund {
297297
/// Creates an [`Invoice`] for the refund with the given required fields.
298298
///
299299
/// Unless [`InvoiceBuilder::relative_expiry`] is set, the invoice will expire two hours after
300-
/// `created_at`. The caller is expected to remember the preimage of `payment_hash` in order to
300+
/// calling this method (or after `created_at` in `no-std` builds).
301+
///
302+
/// The caller is expected to remember the preimage of `payment_hash` in order to
301303
/// claim a payment for the invoice.
302304
///
303305
/// The `signing_pubkey` is required to sign the invoice since refunds are not in response to an
@@ -310,13 +312,20 @@ impl Refund {
310312
///
311313
/// [`Invoice`]: crate::offers::invoice::Invoice
312314
pub fn respond_with(
313-
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
314-
payment_hash: PaymentHash, signing_pubkey: PublicKey
315+
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
316+
signing_pubkey: PublicKey,
317+
#[cfg(not(feature = "std"))]
318+
created_at: Duration
315319
) -> Result<InvoiceBuilder, SemanticError> {
316320
if self.features().requires_unknown_bits() {
317321
return Err(SemanticError::UnknownRequiredFeatures);
318322
}
319323

324+
#[cfg(feature = "std")]
325+
let created_at = std::time::SystemTime::now()
326+
.duration_since(std::time::SystemTime::UNIX_EPOCH)
327+
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
328+
320329
InvoiceBuilder::for_refund(self, payment_paths, created_at, payment_hash, signing_pubkey)
321330
}
322331

0 commit comments

Comments
 (0)