Skip to content

Commit 3c02e50

Browse files
authored
Merge pull request #2125 from benthecarman/invoice-expire-time
Add helper functions for invoice expiry
2 parents 2223e92 + 5687859 commit 3c02e50

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
@@ -1225,6 +1225,12 @@ impl Invoice {
12251225
self.signed_invoice.recover_payee_pub_key().expect("was checked by constructor").0
12261226
}
12271227

1228+
/// Returns the Duration since the Unix epoch at which the invoice expires.
1229+
/// Returning None if overflow occurred.
1230+
pub fn expires_at(&self) -> Option<Duration> {
1231+
self.duration_since_epoch().checked_add(self.expiry_time())
1232+
}
1233+
12281234
/// Returns the invoice's expiry time, if present, otherwise [`DEFAULT_EXPIRY_TIME`].
12291235
pub fn expiry_time(&self) -> Duration {
12301236
self.signed_invoice.expiry_time()
@@ -1247,6 +1253,20 @@ impl Invoice {
12471253
}
12481254
}
12491255

1256+
/// Returns the Duration remaining until the invoice expires.
1257+
#[cfg(feature = "std")]
1258+
pub fn duration_until_expiry(&self) -> Duration {
1259+
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)
1260+
.map(|now| self.expiration_remaining_from_epoch(now))
1261+
.unwrap_or(Duration::from_nanos(0))
1262+
}
1263+
1264+
/// Returns the Duration remaining until the invoice expires given the current time.
1265+
/// `time` is the timestamp as a duration since the Unix epoch.
1266+
pub fn expiration_remaining_from_epoch(&self, time: Duration) -> Duration {
1267+
self.expires_at().map(|x| x.checked_sub(time)).flatten().unwrap_or(Duration::from_nanos(0))
1268+
}
1269+
12501270
/// Returns whether the expiry time would pass at the given point in time.
12511271
/// `at_time` is the timestamp as a duration since the Unix epoch.
12521272
pub fn would_expire(&self, at_time: Duration) -> bool {

0 commit comments

Comments
 (0)