Skip to content

Commit

Permalink
Add is_expired_no_std to Offer & Refund
Browse files Browse the repository at this point in the history
This was available for OfferContents but not an Offer so dependent
projects could not access it.
  • Loading branch information
benthecarman committed Oct 27, 2023
1 parent d2242f6 commit 5ddd8a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lightning/src/offers/offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,11 @@ impl Offer {
self.contents.is_expired()
}

/// Whether the offer has expired given the duration since the Unix epoch.
pub fn is_expired_no_std(&self, duration_since_epoch: Duration) -> bool {
self.contents.is_expired_no_std(duration_since_epoch)
}

/// Returns whether the given quantity is valid for the offer.
pub fn is_valid_quantity(&self, quantity: u64) -> bool {
self.contents.is_valid_quantity(quantity)
Expand Down Expand Up @@ -1192,13 +1197,15 @@ mod tests {
fn builds_offer_with_absolute_expiry() {
let future_expiry = Duration::from_secs(u64::max_value());
let past_expiry = Duration::from_secs(0);
let now = future_expiry - Duration::from_secs(1_000);

let offer = OfferBuilder::new("foo".into(), pubkey(42))
.absolute_expiry(future_expiry)
.build()
.unwrap();
#[cfg(feature = "std")]
assert!(!offer.is_expired());
assert!(!offer.is_expired_no_std(now));
assert_eq!(offer.absolute_expiry(), Some(future_expiry));
assert_eq!(offer.as_tlv_stream().absolute_expiry, Some(future_expiry.as_secs()));

Expand All @@ -1209,6 +1216,7 @@ mod tests {
.unwrap();
#[cfg(feature = "std")]
assert!(offer.is_expired());
assert!(offer.is_expired_no_std(now));
assert_eq!(offer.absolute_expiry(), Some(past_expiry));
assert_eq!(offer.as_tlv_stream().absolute_expiry, Some(past_expiry.as_secs()));
}
Expand Down
8 changes: 8 additions & 0 deletions lightning/src/offers/refund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ impl Refund {
self.contents.is_expired()
}

/// Whether the refund has expired given the duration since the Unix epoch.
pub fn is_expired_no_std(&self, duration_since_epoch: Duration) -> bool {
self.contents.is_expired_no_std(duration_since_epoch)
}

/// The issuer of the refund, possibly beginning with `user@domain` or `domain`. Intended to be
/// displayed to the user but with the caveat that it has not been verified in any way.
pub fn issuer(&self) -> Option<PrintableString> {
Expand Down Expand Up @@ -993,6 +998,7 @@ mod tests {
fn builds_refund_with_absolute_expiry() {
let future_expiry = Duration::from_secs(u64::max_value());
let past_expiry = Duration::from_secs(0);
let now = future_expiry - Duration::from_secs(1_000);

let refund = RefundBuilder::new("foo".into(), vec![1; 32], payer_pubkey(), 1000).unwrap()
.absolute_expiry(future_expiry)
Expand All @@ -1001,6 +1007,7 @@ mod tests {
let (_, tlv_stream, _) = refund.as_tlv_stream();
#[cfg(feature = "std")]
assert!(!refund.is_expired());
assert!(!refund.is_expired_no_std(now));
assert_eq!(refund.absolute_expiry(), Some(future_expiry));
assert_eq!(tlv_stream.absolute_expiry, Some(future_expiry.as_secs()));

Expand All @@ -1012,6 +1019,7 @@ mod tests {
let (_, tlv_stream, _) = refund.as_tlv_stream();
#[cfg(feature = "std")]
assert!(refund.is_expired());
assert!(refund.is_expired_no_std(now));
assert_eq!(refund.absolute_expiry(), Some(past_expiry));
assert_eq!(tlv_stream.absolute_expiry, Some(past_expiry.as_secs()));
}
Expand Down

0 comments on commit 5ddd8a7

Please sign in to comment.