Skip to content

Commit

Permalink
Add new invoice CreationError::InvalidAmount for use in checking `cre…
Browse files Browse the repository at this point in the history
…ate_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
  • Loading branch information
valentinewallace committed Dec 15, 2021
1 parent 1f74a24 commit 0fbb69f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lightning-invoice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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"),
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions lightning-invoice/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 0fbb69f

Please sign in to comment.