Skip to content

Commit b30660e

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

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lightning-invoice/src/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,12 @@ 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+
/// Returning None if overflow occurred.
1218+
pub fn expires_at(&self) -> Option<Duration> {
1219+
self.duration_since_epoch().checked_add(self.expiry_time())
1220+
}
1221+
12161222
/// Returns the invoice's expiry time, if present, otherwise [`DEFAULT_EXPIRY_TIME`].
12171223
pub fn expiry_time(&self) -> Duration {
12181224
self.signed_invoice.expiry_time()
@@ -1235,6 +1241,20 @@ impl Invoice {
12351241
}
12361242
}
12371243

1244+
/// Returns the Duration remaining until the invoice expires.
1245+
#[cfg(feature = "std")]
1246+
pub fn duration_until_expiry(&self) -> Duration {
1247+
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
1248+
.map(|now| self.expiration_remaining_from_epoch(now))
1249+
.unwrap_or(Duration::ZERO)
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) -> Duration {
1255+
self.expires_at().map(|x| x.checked_sub(time)).flatten().unwrap_or(Duration::ZERO)
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)