Skip to content

Commit d41499a

Browse files
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
1 parent 8464875 commit d41499a

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

lightning-invoice/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,9 @@ pub enum CreationError {
14131413

14141414
/// The supplied expiry time could cause an overflow if added to a `PositiveTimestamp`
14151415
ExpiryTimeOutOfBounds,
1416+
1417+
/// The supplied millisatoshi amount was greater than the total bitcoin supply.
1418+
InvalidAmount,
14161419
}
14171420

14181421
impl Display for CreationError {
@@ -1422,6 +1425,7 @@ impl Display for CreationError {
14221425
CreationError::RouteTooLong => f.write_str("The specified route has too many hops and can't be encoded"),
14231426
CreationError::TimestampOutOfBounds => f.write_str("The unix timestamp of the supplied date is <0 or can't be represented as `SystemTime`"),
14241427
CreationError::ExpiryTimeOutOfBounds => f.write_str("The supplied expiry time could cause an overflow if added to a `PositiveTimestamp`"),
1428+
CreationError::InvalidAmount => f.write_str("The supplied millisatoshi amount was greater than the total bitcoin supply"),
14251429
}
14261430
}
14271431
}

lightning-invoice/src/utils.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Convenient utilities to create an invoice.
22
3-
use {Currency, DEFAULT_EXPIRY_TIME, Invoice, InvoiceBuilder, SignOrCreationError, RawInvoice};
3+
use {CreationError, Currency, DEFAULT_EXPIRY_TIME, Invoice, InvoiceBuilder, SignOrCreationError, RawInvoice};
44
use payment::{Payer, Router};
55

66
use bech32::ToBase32;
@@ -60,10 +60,11 @@ where
6060
}]));
6161
}
6262

63+
// `create_inbound_payment` only returns an error if the amount is greater than the total bitcoin
64+
// supply.
6365
let (payment_hash, payment_secret) = channelmanager.create_inbound_payment(
64-
amt_msat,
65-
DEFAULT_EXPIRY_TIME.try_into().unwrap(),
66-
).unwrap();
66+
amt_msat, DEFAULT_EXPIRY_TIME.try_into().unwrap())
67+
.map_err(|()| SignOrCreationError::CreationError(CreationError::InvalidAmount))?;
6768
let our_node_pubkey = channelmanager.get_our_node_id();
6869
let mut invoice = InvoiceBuilder::new(network)
6970
.description(description)

0 commit comments

Comments
 (0)