Skip to content

Commit

Permalink
Use SystemTime::now() for Invoice creation time
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jkczyz committed Jan 13, 2023
1 parent cfe0f0d commit df9e279
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
16 changes: 12 additions & 4 deletions lightning/src/offers/invoice_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,10 @@ impl InvoiceRequest {
/// Creates an [`Invoice`] for the request with the given required fields.
///
/// Unless [`InvoiceBuilder::relative_expiry`] is set, the invoice will expire two hours after
/// `created_at`. The caller is expected to remember the preimage of `payment_hash` in order to
/// claim a payment for the invoice.
/// calling this method (or after `created_at` in `no-std` builds).
///
/// The caller is expected to remember the preimage of `payment_hash` in order to claim a payment
/// for the invoice.
///
/// The `payment_paths` parameter is useful for maintaining the payment recipient's privacy. It
/// must contain one or more elements.
Expand All @@ -333,13 +335,19 @@ impl InvoiceRequest {
///
/// [`Invoice`]: crate::offers::invoice::Invoice
pub fn respond_with(
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
payment_hash: PaymentHash
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
#[cfg(not(feature = "std"))]
created_at: Duration
) -> Result<InvoiceBuilder, SemanticError> {
if self.features().requires_unknown_bits() {
return Err(SemanticError::UnknownRequiredFeatures);
}

#[cfg(feature = "std")]
let created_at = std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");

InvoiceBuilder::for_offer(self, payment_paths, created_at, payment_hash)
}

Expand Down
15 changes: 12 additions & 3 deletions lightning/src/offers/refund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ impl Refund {
/// Creates an [`Invoice`] for the refund with the given required fields.
///
/// Unless [`InvoiceBuilder::relative_expiry`] is set, the invoice will expire two hours after
/// `created_at`. The caller is expected to remember the preimage of `payment_hash` in order to
/// calling this method (or after `created_at` in `no-std` builds).
///
/// The caller is expected to remember the preimage of `payment_hash` in order to
/// claim a payment for the invoice.
///
/// The `signing_pubkey` is required to sign the invoice since refunds are not in response to an
Expand All @@ -310,13 +312,20 @@ impl Refund {
///
/// [`Invoice`]: crate::offers::invoice::Invoice
pub fn respond_with(
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, created_at: Duration,
payment_hash: PaymentHash, signing_pubkey: PublicKey
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
signing_pubkey: PublicKey,
#[cfg(not(feature = "std"))]
created_at: Duration
) -> Result<InvoiceBuilder, SemanticError> {
if self.features().requires_unknown_bits() {
return Err(SemanticError::UnknownRequiredFeatures);
}

#[cfg(feature = "std")]
let created_at = std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");

InvoiceBuilder::for_refund(self, payment_paths, created_at, payment_hash, signing_pubkey)
}

Expand Down

0 comments on commit df9e279

Please sign in to comment.