Add new invoice CreationError::InvalidAmount for use in checking `create_inbound_payment`
authorValentine Wallace <vwallace@protonmail.com>
Thu, 9 Dec 2021 22:41:33 +0000 (17:41 -0500)
committerValentine Wallace <vwallace@protonmail.com>
Thu, 16 Dec 2021 23:32:22 +0000 (15:32 -0800)
in an invoice creation utility. Note that if the error type of `create_inbound_payment` ever
changed, we'd be forced to update the invoice utility's callsite to handle the new error

lightning-invoice/src/lib.rs
lightning-invoice/src/utils.rs

index 04ba08bd8552c34379576b59047de719ef200ae3..dd86c06f23f80d524f860aca5bc3c9355962c099 100644 (file)
@@ -1413,6 +1413,9 @@ pub enum CreationError {
 
        /// The supplied expiry time could cause an overflow if added to a `PositiveTimestamp`
        ExpiryTimeOutOfBounds,
+
+       /// The supplied millisatoshi amount was greater than the total bitcoin supply.
+       InvalidAmount,
 }
 
 impl Display for CreationError {
@@ -1422,6 +1425,7 @@ impl Display for CreationError {
                        CreationError::RouteTooLong => f.write_str("The specified route has too many hops and can't be encoded"),
                        CreationError::TimestampOutOfBounds => f.write_str("The unix timestamp of the supplied date is <0 or can't be represented as `SystemTime`"),
                        CreationError::ExpiryTimeOutOfBounds => f.write_str("The supplied expiry time could cause an overflow if added to a `PositiveTimestamp`"),
+                       CreationError::InvalidAmount => f.write_str("The supplied millisatoshi amount was greater than the total bitcoin supply"),
                }
        }
 }
index 005be24c3b75f530e284ed51e937a35ff5e451ff..5918753ae1c1f196ffa03002646a5bd2a97374c3 100644 (file)
@@ -1,6 +1,6 @@
 //! Convenient utilities to create an invoice.
 
-use {Currency, DEFAULT_EXPIRY_TIME, Invoice, InvoiceBuilder, SignOrCreationError, RawInvoice};
+use {CreationError, Currency, DEFAULT_EXPIRY_TIME, Invoice, InvoiceBuilder, SignOrCreationError, RawInvoice};
 use payment::{Payer, Router};
 
 use bech32::ToBase32;
@@ -60,10 +60,11 @@ where
                }]));
        }
 
+       // `create_inbound_payment` only returns an error if the amount is greater than the total bitcoin
+       // supply.
        let (payment_hash, payment_secret) = channelmanager.create_inbound_payment(
-               amt_msat,
-               DEFAULT_EXPIRY_TIME.try_into().unwrap(),
-       ).unwrap();
+               amt_msat, DEFAULT_EXPIRY_TIME.try_into().unwrap())
+               .map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
        let our_node_pubkey = channelmanager.get_our_node_id();
        let mut invoice = InvoiceBuilder::new(network)
                .description(description)