Use constant for invoice's default expiry value
[rust-lightning] / lightning-invoice / src / lib.rs
index 3d75d4d65dc057c148d11886ae03a702da9205ea..0e469ff1e8600745cd97648cf7f9739524f18377 100644 (file)
@@ -54,6 +54,11 @@ const SYSTEM_TIME_MAX_UNIX_TIMESTAMP: u64 = std::i32::MAX as u64;
 /// it should be rather low as long as we still have to support 32bit time representations
 const MAX_EXPIRY_TIME: u64 = 60 * 60 * 24 * 356;
 
+/// Default expiry time as defined by [BOLT 11].
+///
+/// [BOLT 11]: https://github.com/lightningnetwork/lightning-rfc/blob/master/11-payment-encoding.md
+const DEFAULT_EXPIRY_TIME: u64 = 3600;
+
 /// This function is used as a static assert for the size of `SystemTime`. If the crate fails to
 /// compile due to it this indicates that your system uses unexpected bounds for `SystemTime`. You
 /// can remove this functions and run the test `test_system_time_bounds_assumptions`. In any case,
@@ -151,6 +156,8 @@ pub fn check_platform() {
 ///  * `D`: exactly one `Description` or `DescriptionHash`
 ///  * `H`: exactly one `PaymentHash`
 ///  * `T`: the timestamp is set
+///
+/// (C-not exported) as we likely need to manually select one set of boolean type parameters.
 #[derive(Eq, PartialEq, Debug, Clone)]
 pub struct InvoiceBuilder<D: tb::Bool, H: tb::Bool, T: tb::Bool> {
        currency: Currency,
@@ -178,6 +185,9 @@ pub struct Invoice {
 
 /// Represents the description of an invoice which has to be either a directly included string or
 /// a hash of a description provided out of band.
+///
+/// (C-not exported) As we don't have a good way to map the reference lifetimes making this
+/// practically impossible to use safely in languages like C.
 #[derive(Eq, PartialEq, Debug, Clone)]
 pub enum InvoiceDescription<'f> {
        /// Reference to the directly supplied description in the invoice
@@ -207,7 +217,7 @@ pub struct SignedRawInvoice {
        hash: [u8; 32],
 
        /// signature of the payment request
-       signature: Signature,
+       signature: InvoiceSignature,
 }
 
 /// Represents an syntactically correct Invoice for a payment on the lightning network,
@@ -225,6 +235,8 @@ pub struct RawInvoice {
 }
 
 /// Data of the `RawInvoice` that is encoded in the human readable part
+///
+/// (C-not exported) As we don't yet support Option<Enum>
 #[derive(Eq, PartialEq, Debug, Clone)]
 pub struct RawHrp {
        /// The currency deferred from the 3rd and 4th character of the bech32 transaction
@@ -283,6 +295,9 @@ impl SiPrefix {
 
        /// Returns all enum variants of `SiPrefix` sorted in descending order of their associated
        /// multiplier.
+       ///
+       /// (C-not exported) As we don't yet support a slice of enums, and also because this function
+       /// isn't the most critical to expose.
        pub fn values_desc() -> &'static [SiPrefix] {
                use SiPrefix::*;
                static VALUES: [SiPrefix; 4] = [Milli, Micro, Nano, Pico];
@@ -381,7 +396,7 @@ pub enum Fallback {
 
 /// Recoverable signature
 #[derive(Eq, PartialEq, Debug, Clone)]
-pub struct Signature(pub RecoverableSignature);
+pub struct InvoiceSignature(pub RecoverableSignature);
 
 /// Private routing information
 ///
@@ -630,7 +645,7 @@ impl SignedRawInvoice {
        ///  1. raw invoice
        ///  2. hash of the raw invoice
        ///  3. signature
-       pub fn into_parts(self) -> (RawInvoice, [u8; 32], Signature) {
+       pub fn into_parts(self) -> (RawInvoice, [u8; 32], InvoiceSignature) {
                (self.raw_invoice, self.hash, self.signature)
        }
 
@@ -644,8 +659,8 @@ impl SignedRawInvoice {
                &self.hash
        }
 
-       /// Signature for the invoice.
-       pub fn signature(&self) -> &Signature {
+       /// InvoiceSignature for the invoice.
+       pub fn signature(&self) -> &InvoiceSignature {
                &self.signature
        }
 
@@ -760,6 +775,9 @@ impl RawInvoice {
        /// Signs the invoice using the supplied `sign_function`. This function MAY fail with an error
        /// of type `E`. Since the signature of a `SignedRawInvoice` is not required to be valid there
        /// are no constraints regarding the validity of the produced signature.
+       ///
+       /// (C-not exported) As we don't currently support passing function pointers into methods
+       /// explicitly.
        pub fn sign<F, E>(self, sign_method: F) -> Result<SignedRawInvoice, E>
                where F: FnOnce(&Message) -> Result<RecoverableSignature, E>
        {
@@ -771,11 +789,13 @@ impl RawInvoice {
                Ok(SignedRawInvoice {
                        raw_invoice: self,
                        hash: raw_hash,
-                       signature: Signature(signature),
+                       signature: InvoiceSignature(signature),
                })
        }
 
        /// Returns an iterator over all tagged fields with known semantics.
+       ///
+       /// (C-not exported) As there is not yet a manual mapping for a FilterMap
        pub fn known_tagged_fields(&self)
                -> FilterMap<Iter<RawTaggedField>, fn(&RawTaggedField) -> Option<&TaggedField>>
        {
@@ -824,6 +844,7 @@ impl RawInvoice {
                find_extract!(self.known_tagged_fields(), TaggedField::Features(ref x), x)
        }
 
+       /// (C-not exported) as we don't support Vec<&NonOpaqueType>
        pub fn fallbacks(&self) -> Vec<&Fallback> {
                self.known_tagged_fields().filter_map(|tf| match tf {
                        &TaggedField::Fallback(ref f) => Some(f),
@@ -981,6 +1002,8 @@ impl Invoice {
        }
 
        /// Returns an iterator over all tagged fields of this Invoice.
+       ///
+       /// (C-not exported) As there is not yet a manual mapping for a FilterMap
        pub fn tagged_fields(&self)
                -> FilterMap<Iter<RawTaggedField>, fn(&RawTaggedField) -> Option<&TaggedField>> {
                self.signed_invoice.raw_invoice().known_tagged_fields()
@@ -992,6 +1015,8 @@ impl Invoice {
        }
 
        /// Return the description or a hash of it for longer ones
+       ///
+       /// (C-not exported) because we don't yet export InvoiceDescription
        pub fn description(&self) -> InvoiceDescription {
                if let Some(ref direct) = self.signed_invoice.description() {
                        return InvoiceDescription::Direct(direct);
@@ -1021,19 +1046,21 @@ impl Invoice {
                self.signed_invoice.recover_payee_pub_key().expect("was checked by constructor").0
        }
 
-       /// Returns the invoice's expiry time if present
+       /// Returns the invoice's expiry time, if present, otherwise [`DEFAULT_EXPIRY_TIME`].
        pub fn expiry_time(&self) -> Duration {
                self.signed_invoice.expiry_time()
                        .map(|x| x.0)
-                       .unwrap_or(Duration::from_secs(3600))
+                       .unwrap_or(Duration::from_secs(DEFAULT_EXPIRY_TIME))
        }
 
        /// Returns the invoice's `min_cltv_expiry` time if present
-       pub fn min_final_cltv_expiry(&self) -> Option<&u64> {
-               self.signed_invoice.min_final_cltv_expiry().map(|x| &x.0)
+       pub fn min_final_cltv_expiry(&self) -> Option<u64> {
+               self.signed_invoice.min_final_cltv_expiry().map(|x| x.0)
        }
 
        /// Returns a list of all fallback addresses
+       ///
+       /// (C-not exported) as we don't support Vec<&NonOpaqueType>
        pub fn fallbacks(&self) -> Vec<&Fallback> {
                self.signed_invoice.fallbacks()
        }
@@ -1192,7 +1219,7 @@ impl Deref for RouteHint {
        }
 }
 
-impl Deref for Signature {
+impl Deref for InvoiceSignature {
        type Target = RecoverableSignature;
 
        fn deref(&self) -> &RecoverableSignature {
@@ -1277,6 +1304,8 @@ impl std::error::Error for SemanticError { }
 
 /// When signing using a fallible method either an user-supplied `SignError` or a `CreationError`
 /// may occur.
+///
+/// (C-not exported) As we don't support unbounded generics
 #[derive(Eq, PartialEq, Debug, Clone)]
 pub enum SignOrCreationError<S> {
        /// An error occurred during signing
@@ -1354,7 +1383,7 @@ mod test {
                use secp256k1::Secp256k1;
                use secp256k1::recovery::{RecoveryId, RecoverableSignature};
                use secp256k1::key::{SecretKey, PublicKey};
-               use {SignedRawInvoice, Signature, RawInvoice, RawHrp, RawDataPart, Currency, Sha256,
+               use {SignedRawInvoice, InvoiceSignature, RawInvoice, RawHrp, RawDataPart, Currency, Sha256,
                         PositiveTimestamp};
 
                let invoice = SignedRawInvoice {
@@ -1383,7 +1412,7 @@ mod test {
                                0x7b, 0x1d, 0x85, 0x8d, 0xb1, 0xd1, 0xf7, 0xab, 0x71, 0x37, 0xdc, 0xb7,
                                0x83, 0x5d, 0xb2, 0xec, 0xd5, 0x18, 0xe1, 0xc9
                        ],
-                       signature: Signature(RecoverableSignature::from_compact(
+                       signature: InvoiceSignature(RecoverableSignature::from_compact(
                                & [
                                        0x38u8, 0xec, 0x68, 0x91, 0x34, 0x5e, 0x20, 0x41, 0x45, 0xbe, 0x8a,
                                        0x3a, 0x99, 0xde, 0x38, 0xe9, 0x8a, 0x39, 0xd6, 0xa5, 0x69, 0x43,
@@ -1591,7 +1620,7 @@ mod test {
                );
                assert_eq!(invoice.payee_pub_key(), Some(&public_key));
                assert_eq!(invoice.expiry_time(), Duration::from_secs(54321));
-               assert_eq!(invoice.min_final_cltv_expiry(), Some(&144));
+               assert_eq!(invoice.min_final_cltv_expiry(), Some(144));
                assert_eq!(invoice.fallbacks(), vec![&Fallback::PubKeyHash([0;20])]);
                assert_eq!(invoice.routes(), vec![&RouteHint(route_1), &RouteHint(route_2)]);
                assert_eq!(