Skip to content

Commit 940ec03

Browse files
committed
Add helper functions for invoice expiry
1 parent 3d479c9 commit 940ec03

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lightning-invoice/src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,11 @@ impl Invoice {
12131213
self.signed_invoice.recover_payee_pub_key().expect("was checked by constructor").0
12141214
}
12151215

1216+
/// Returns the Duration since the Unix epoch at which the invoice expires.
1217+
pub fn expires_at(&self) -> Option<Duration> {
1218+
self.duration_since_epoch().checked_add(self.expiry_time())
1219+
}
1220+
12161221
/// Returns the invoice's expiry time, if present, otherwise [`DEFAULT_EXPIRY_TIME`].
12171222
pub fn expiry_time(&self) -> Duration {
12181223
self.signed_invoice.expiry_time()
@@ -1235,6 +1240,21 @@ impl Invoice {
12351240
}
12361241
}
12371242

1243+
/// Returns the Duration remaining until the invoice expires.
1244+
#[cfg(feature = "std")]
1245+
pub fn expiration_remaining(&self) -> Option<Duration> {
1246+
match self.timestamp().elapsed() {
1247+
Ok(elapsed) => self.expiry_time().checked_sub(elapsed),
1248+
Err(_) => None,
1249+
}
1250+
}
1251+
1252+
/// Returns the Duration remaining until the invoice expires given the current time.
1253+
/// `time` is the timestamp as a duration since the Unix epoch.
1254+
pub fn expiration_remaining_from_epoch(&self, time: Duration) -> Option<Duration> {
1255+
self.expires_at().map(|x| x.checked_sub(time)).flatten()
1256+
}
1257+
12381258
/// Returns whether the expiry time would pass at the given point in time.
12391259
/// `at_time` is the timestamp as a duration since the Unix epoch.
12401260
pub fn would_expire(&self, at_time: Duration) -> bool {

0 commit comments

Comments
 (0)