Skip to content

Commit 182a1e3

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 20f349f commit 182a1e3

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
@@ -380,6 +380,18 @@ impl StaticInvoice {
380380
self.signature
381381
}
382382

383+
/// Whether the [`Offer`] that this invoice is based on is expired.
384+
#[cfg(feature = "std")]
385+
pub fn is_offer_expired(&self) -> bool {
386+
self.contents.is_expired()
387+
}
388+
389+
/// Whether the [`Offer`] that this invoice is based on is expired, given the current time as
390+
/// duration since the Unix epoch.
391+
pub fn is_offer_expired_no_std(&self, duration_since_epoch: Duration) -> bool {
392+
self.contents.is_offer_expired_no_std(duration_since_epoch)
393+
}
394+
383395
#[cfg(async_payments)]
384396
pub(crate) fn from_same_offer(&self, invreq: &InvoiceRequest) -> bool {
385397
let invoice_offer_tlv_stream =
@@ -396,7 +408,6 @@ impl InvoiceContents {
396408
self.offer.is_expired()
397409
}
398410

399-
#[cfg(not(feature = "std"))]
400411
fn is_offer_expired_no_std(&self, duration_since_epoch: Duration) -> bool {
401412
self.offer.is_expired_no_std(duration_since_epoch)
402413
}
@@ -513,6 +524,10 @@ impl InvoiceContents {
513524
is_expired(self.created_at(), self.relative_expiry())
514525
}
515526

527+
fn is_expired_no_std(&self, duration_since_epoch: Duration) -> bool {
528+
self.created_at().saturating_add(self.relative_expiry()) < duration_since_epoch
529+
}
530+
516531
fn fallbacks(&self) -> Vec<Address> {
517532
let chain = self.chain();
518533
self.fallbacks

0 commit comments

Comments
 (0)