]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Use SystemTime::now() for Invoice creation time
authorJeffrey Czyz <jkczyz@gmail.com>
Fri, 13 Jan 2023 05:02:39 +0000 (23:02 -0600)
committerJeffrey Czyz <jkczyz@gmail.com>
Fri, 20 Jan 2023 22:04:38 +0000 (16:04 -0600)
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.

lightning/src/offers/invoice_request.rs
lightning/src/offers/refund.rs

index 126d9b552decf689bee53bae7a2c82a9bf681be5..4185c95ded90c278e71f645a27d96fb9402d3fa9 100644 (file)
@@ -57,7 +57,6 @@ use bitcoin::network::constants::Network;
 use bitcoin::secp256k1::{Message, PublicKey};
 use bitcoin::secp256k1::schnorr::Signature;
 use core::convert::TryFrom;
-use core::time::Duration;
 use crate::io;
 use crate::ln::PaymentHash;
 use crate::ln::features::InvoiceRequestFeatures;
@@ -326,23 +325,35 @@ 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 in `std` builds. For `no-std` builds, a final [`Duration`] parameter
+       /// must be given, which is used to set [`Invoice::created_at`] since [`std::time::SystemTime`]
+       /// is not available.
+       ///
+       /// 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.
        ///
        /// Errors if the request contains unknown required features.
        ///
+       /// [`Duration`]: core::time::Duration
        /// [`Invoice`]: crate::offers::invoice::Invoice
+       /// [`Invoice::created_at`]: crate::offers::invoice::Invoice::created_at
        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: core::time::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)
        }
 
index e5d8e78f0794e08c7dee1fe8e994bacb9ab14cd2..48be9774aec2f7bc5e220db29935654119c3e9b0 100644 (file)
@@ -301,7 +301,11 @@ 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 in `std` builds. For `no-std` builds, a final [`Duration`] parameter
+       /// must be given, which is used to set [`Invoice::created_at`] since [`std::time::SystemTime`]
+       /// is not available.
+       ///
+       /// 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
@@ -313,14 +317,22 @@ impl Refund {
        /// Errors if the request contains unknown required features.
        ///
        /// [`Invoice`]: crate::offers::invoice::Invoice
+       /// [`Invoice::created_at`]: crate::offers::invoice::Invoice::created_at
        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)
        }