From f0eb724681f30b24cfd437f966f1b6ecc8a1061e Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 2 Feb 2024 20:04:18 +0000 Subject: [PATCH] Use `payment_parameters_from_invoice` rather than building by hand --- src/cli.rs | 47 ++++++++++------------------------------------- 1 file changed, 10 insertions(+), 37 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index e348742..5c544c2 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -19,6 +19,7 @@ use lightning::sign::{EntropySource, KeysManager}; use lightning::util::config::{ChannelHandshakeConfig, ChannelHandshakeLimits, UserConfig}; use lightning::util::persist::KVStore; use lightning::util::ser::{Writeable, Writer}; +use lightning_invoice::payment::payment_parameters_from_invoice; use lightning_invoice::{utils, Bolt11Invoice, Currency}; use lightning_persister::fs_store::FilesystemStore; use std::env; @@ -690,8 +691,16 @@ fn send_payment( outbound_payments: &mut OutboundPaymentInfoStorage, fs_store: Arc, ) { let payment_id = PaymentId((*invoice.payment_hash()).to_byte_array()); - let payment_hash = PaymentHash((*invoice.payment_hash()).to_byte_array()); let payment_secret = Some(*invoice.payment_secret()); + let (payment_hash, recipient_onion, route_params) = + match payment_parameters_from_invoice(invoice) { + Ok(res) => res, + Err(e) => { + println!("Failed to parse invoice"); + print!("> "); + return; + } + }; outbound_payments.payments.insert( payment_id, PaymentInfo { @@ -703,42 +712,6 @@ fn send_payment( ); fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound_payments.encode()).unwrap(); - let mut recipient_onion = RecipientOnionFields::secret_only(*invoice.payment_secret()); - recipient_onion.payment_metadata = invoice.payment_metadata().map(|v| v.clone()); - let mut payment_params = match PaymentParameters::from_node_id( - invoice.recover_payee_pub_key(), - invoice.min_final_cltv_expiry_delta() as u32, - ) - .with_expiry_time( - invoice.duration_since_epoch().as_secs().saturating_add(invoice.expiry_time().as_secs()), - ) - .with_route_hints(invoice.route_hints()) - { - Err(e) => { - println!("ERROR: Could not process invoice to prepare payment parameters, {:?}, invoice: {:?}", e, invoice); - return; - } - Ok(p) => p, - }; - if let Some(features) = invoice.features() { - payment_params = match payment_params.with_bolt11_features(features.clone()) { - Err(e) => { - println!("ERROR: Could not build BOLT11 payment parameters! {:?}", e); - return; - } - Ok(p) => p, - }; - } - let invoice_amount = match invoice.amount_milli_satoshis() { - None => { - println!("ERROR: An invoice with an amount is expected; {:?}", invoice); - return; - } - Some(a) => a, - }; - let route_params = - RouteParameters::from_payment_params_and_value(payment_params, invoice_amount); - match channel_manager.send_payment( payment_hash, recipient_onion, -- 2.39.5