Skip to content

Commit 3c58411

Browse files
Swith invoice util's create_invoice to return a SignOrCreationError
1 parent ed19389 commit 3c58411

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lightning-invoice/src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,14 +1292,15 @@ impl<S> Display for SignOrCreationError<S> {
12921292

12931293
/// Convenient utilities to create an invoice.
12941294
pub mod utils {
1295-
use {Currency, Invoice, InvoiceBuilder, SemanticError, SignedRawInvoice};
1295+
use {Currency, Invoice, InvoiceBuilder, SignOrCreationError, SignedRawInvoice};
12961296
use bech32::{FromBase32, ToBase32, u5};
12971297
use bitcoin_hashes::Hash;
12981298
use lightning::chain;
12991299
use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
13001300
use lightning::chain::keysinterface::{Sign, KeysInterface};
13011301
use lightning::ln::channelmanager::{ChannelManager, MIN_FINAL_CLTV_EXPIRY};
13021302
use lightning::ln::features::InvoiceFeatures;
1303+
use lightning::ln::msgs::DecodeError;
13031304
use lightning::routing::network_graph::RoutingFees;
13041305
use lightning::routing::router::RouteHintHop;
13051306
use lightning::util::logger::Logger;
@@ -1312,8 +1313,8 @@ pub mod utils {
13121313
/// that the payment secret is valid when the invoice is paid.
13131314
pub fn create_invoice_from_channelmanager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>(
13141315
channelmanager: &ChannelManager<Signer, M, T, K, F, L>, amt_msat: Option<u64>, description: String,
1315-
network: Currency, keys_manager: &dyn KeysInterface<Signer = Signer>
1316-
) -> Result<Invoice, SemanticError>
1316+
network: Currency, keys_manager: K
1317+
) -> Result<Invoice, SignOrCreationError<DecodeError>>
13171318
where
13181319
M::Target: chain::Watch<Signer>,
13191320
T::Target: BroadcasterInterface,
@@ -1367,7 +1368,10 @@ pub mod utils {
13671368
invoice = invoice.route(hint);
13681369
}
13691370

1370-
let raw_invoice = invoice.build_raw().unwrap();
1371+
let raw_invoice = match invoice.build_raw() {
1372+
Ok(inv) => inv,
1373+
Err(e) => return Err(SignOrCreationError::CreationError(e))
1374+
};
13711375
let hrp_str = raw_invoice.hrp.to_string();
13721376
let hrp_bytes = hrp_str.as_bytes();
13731377
let data_without_signature = raw_invoice.data.to_base32();
@@ -1387,12 +1391,11 @@ pub mod utils {
13871391

13881392
invoice_preimage.extend_from_slice(&Vec::<u8>::from_base32(&data_part)
13891393
.expect("No padding error may occur due to appended zero above."));
1390-
let signature = match keys_manager.sign_invoice(invoice_preimage) {
1391-
Ok(sig) => sig,
1392-
Err(_) => return Err(SemanticError::InvalidSignature)
1394+
let signed_raw_invoice: Result<SignedRawInvoice, DecodeError> = raw_invoice.sign(|_| keys_manager.sign_invoice(invoice_preimage));
1395+
match signed_raw_invoice {
1396+
Ok(inv) => return Ok(Invoice::from_signed(inv).unwrap()),
1397+
Err(e) => return Err(SignOrCreationError::SignError(e))
13931398
};
1394-
let signed_raw_invoice: Result<SignedRawInvoice, SemanticError> = raw_invoice.sign(|_| Ok(signature));
1395-
Invoice::from_signed(signed_raw_invoice.unwrap())
13961399
}
13971400

13981401
}

0 commit comments

Comments
 (0)