Skip to content

Commit 638b41b

Browse files
BOLT 12 {Static}Invoices: expose more is_expired methods
In upcoming commits, we need to check whether a static invoice or its underlying offer is expired in no-std builds. Here we expose the methods to do so. The methods could instead be kept private to the crate, but they seem potentially useful.
1 parent 6771d84 commit 638b41b

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

lightning/src/offers/invoice.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,10 @@ impl InvoiceContents {
12231223
is_expired(self.created_at(), self.relative_expiry())
12241224
}
12251225

1226+
fn is_expired_no_std(&self, duration_since_epoch: Duration) -> bool {
1227+
self.created_at().saturating_add(self.relative_expiry()) < duration_since_epoch
1228+
}
1229+
12261230
fn payment_hash(&self) -> PaymentHash {
12271231
self.fields().payment_hash
12281232
}

lightning/src/offers/invoice_macros.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ macro_rules! invoice_accessors_common { ($self: ident, $contents: expr, $invoice
131131
$contents.is_expired()
132132
}
133133

134+
/// Whether the invoice has expired given the current time as duration since the Unix epoch.
135+
pub fn is_expired_no_std(&$self, duration_since_epoch: Duration) -> bool {
136+
$contents.is_expired_no_std(duration_since_epoch)
137+
}
138+
134139
/// Fallback addresses for paying the invoice on-chain, in order of most-preferred to
135140
/// least-preferred.
136141
pub fn fallbacks(&$self) -> Vec<Address> {

lightning/src/offers/static_invoice.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,18 @@ impl StaticInvoice {
395395
self.signature
396396
}
397397

398+
/// Whether the [`Offer`] that this invoice is based on is expired.
399+
#[cfg(feature = "std")]
400+
pub fn is_offer_expired(&self) -> bool {
401+
self.contents.is_expired()
402+
}
403+
404+
/// Whether the [`Offer`] that this invoice is based on is expired, given the current time as
405+
/// duration since the Unix epoch.
406+
pub fn is_offer_expired_no_std(&self, duration_since_epoch: Duration) -> bool {
407+
self.contents.is_offer_expired_no_std(duration_since_epoch)
408+
}
409+
398410
#[allow(unused)] // TODO: remove this once we remove the `async_payments` cfg flag
399411
pub(crate) fn is_from_same_offer(&self, invreq: &InvoiceRequest) -> bool {
400412
let invoice_offer_tlv_stream =
@@ -411,7 +423,6 @@ impl InvoiceContents {
411423
self.offer.is_expired()
412424
}
413425

414-
#[cfg(not(feature = "std"))]
415426
fn is_offer_expired_no_std(&self, duration_since_epoch: Duration) -> bool {
416427
self.offer.is_expired_no_std(duration_since_epoch)
417428
}
@@ -528,6 +539,10 @@ impl InvoiceContents {
528539
is_expired(self.created_at(), self.relative_expiry())
529540
}
530541

542+
fn is_expired_no_std(&self, duration_since_epoch: Duration) -> bool {
543+
self.created_at().saturating_add(self.relative_expiry()) < duration_since_epoch
544+
}
545+
531546
fn fallbacks(&self) -> Vec<Address> {
532547
let chain = self.chain();
533548
self.fallbacks

0 commit comments

Comments
 (0)