From d41499a26084c828de6542380b0b615093bda954 Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Thu, 9 Dec 2021 17:41:33 -0500 Subject: [PATCH] Add new invoice CreationError::InvalidAmount for use in checking `create_inbound_payment` 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 | 4 ++++ lightning-invoice/src/utils.rs | 9 +++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lightning-invoice/src/lib.rs b/lightning-invoice/src/lib.rs index 04ba08bd8..dd86c06f2 100644 --- a/lightning-invoice/src/lib.rs +++ b/lightning-invoice/src/lib.rs @@ -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"), } } } diff --git a/lightning-invoice/src/utils.rs b/lightning-invoice/src/utils.rs index 005be24c3..5918753ae 100644 --- a/lightning-invoice/src/utils.rs +++ b/lightning-invoice/src/utils.rs @@ -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) -- 2.39.5