Bump rust-bitcoin to v0.30.2
[rust-lightning] / lightning / src / offers / refund.rs
index 4b4572b4df9c85fd8970e9758b727d9b6214a067..ce39841df8c9750d9f4e7b717d1e3fb50e967d1a 100644 (file)
@@ -18,6 +18,8 @@
 //! [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
 //! [`Offer`]: crate::offers::offer::Offer
 //!
+//! # Example
+//!
 //! ```
 //! extern crate bitcoin;
 //! extern crate core;
 //! # Ok(())
 //! # }
 //! ```
+//!
+//! # Note
+//!
+//! If constructing a [`Refund`] for use with a [`ChannelManager`], use
+//! [`ChannelManager::create_refund_builder`] instead of [`RefundBuilder::new`].
+//!
+//! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
+//! [`ChannelManager::create_refund_builder`]: crate::ln::channelmanager::ChannelManager::create_refund_builder
 
 use bitcoin::blockdata::constants::ChainHash;
 use bitcoin::network::constants::Network;
@@ -120,6 +130,14 @@ impl<'a> RefundBuilder<'a, secp256k1::SignOnly> {
        ///
        /// Additionally, sets the required [`Refund::description`], [`Refund::payer_metadata`], and
        /// [`Refund::amount_msats`].
+       ///
+       /// # Note
+       ///
+       /// If constructing a [`Refund`] for use with a [`ChannelManager`], use
+       /// [`ChannelManager::create_refund_builder`] instead of [`RefundBuilder::new`].
+       ///
+       /// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
+       /// [`ChannelManager::create_refund_builder`]: crate::ln::channelmanager::ChannelManager::create_refund_builder
        pub fn new(
                description: String, metadata: Vec<u8>, payer_id: PublicKey, amount_msats: u64
        ) -> Result<Self, Bolt12SemanticError> {
@@ -206,8 +224,16 @@ impl<'a, T: secp256k1::Signing> RefundBuilder<'a, T> {
        /// called, [`Network::Bitcoin`] is assumed.
        ///
        /// Successive calls to this method will override the previous setting.
-       pub fn chain(mut self, network: Network) -> Self {
-               self.refund.chain = Some(ChainHash::using_genesis_block(network));
+       pub fn chain(self, network: Network) -> Self {
+               self.chain_hash(ChainHash::using_genesis_block(network))
+       }
+
+       /// Sets the [`Refund::chain`] of the given [`ChainHash`] for paying an invoice. If not called,
+       /// [`Network::Bitcoin`] is assumed.
+       ///
+       /// Successive calls to this method will override the previous setting.
+       pub(crate) fn chain_hash(mut self, chain: ChainHash) -> Self {
+               self.refund.chain = Some(chain);
                self
        }
 
@@ -333,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> {
@@ -514,13 +545,16 @@ impl RefundContents {
 
        #[cfg(feature = "std")]
        pub(super) fn is_expired(&self) -> bool {
-               match self.absolute_expiry {
-                       Some(seconds_from_epoch) => match SystemTime::UNIX_EPOCH.elapsed() {
-                               Ok(elapsed) => elapsed > seconds_from_epoch,
-                               Err(_) => false,
-                       },
-                       None => false,
-               }
+               SystemTime::UNIX_EPOCH
+                       .elapsed()
+                       .map(|duration_since_epoch| self.is_expired_no_std(duration_since_epoch))
+                       .unwrap_or(false)
+       }
+
+       pub(super) fn is_expired_no_std(&self, duration_since_epoch: Duration) -> bool {
+               self.absolute_expiry
+                       .map(|absolute_expiry| duration_since_epoch > absolute_expiry)
+                       .unwrap_or(false)
        }
 
        pub fn issuer(&self) -> Option<PrintableString> {
@@ -964,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)
@@ -972,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()));
 
@@ -983,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()));
        }