]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add is_expired_no_std to Offer & Refund
authorbenthecarman <benthecarman@live.com>
Thu, 26 Oct 2023 21:55:03 +0000 (16:55 -0500)
committerbenthecarman <benthecarman@live.com>
Fri, 27 Oct 2023 01:58:04 +0000 (20:58 -0500)
This was available for OfferContents but not an Offer so dependent
projects could not access it.

lightning/src/offers/offer.rs
lightning/src/offers/refund.rs

index 8e0d46261483769864dde69296a6447ff3cfcbad..66dca85cd94c97d6f5b6c594c24d07114d859860 100644 (file)
@@ -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)
@@ -1192,6 +1197,7 @@ 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)
@@ -1199,6 +1205,7 @@ mod tests {
                        .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()));
 
@@ -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()));
        }
index 0a95f72511821f33c7b58214e1dcbe4cf2a5647b..ce39841df8c9750d9f4e7b717d1e3fb50e967d1a 100644 (file)
@@ -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> {
@@ -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)
@@ -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()));
 
@@ -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()));
        }