]> git.bitcoin.ninja Git - ldk-sample/commitdiff
Use `payment_parameters_from_invoice` rather than building by hand
authorMatt Corallo <git@bluematt.me>
Fri, 2 Feb 2024 20:04:18 +0000 (20:04 +0000)
committerMatt Corallo <git@bluematt.me>
Tue, 6 Feb 2024 21:57:07 +0000 (21:57 +0000)
src/cli.rs

index e3487421373c74ad6ae862fefe8f53119551edff..5c544c2bcf06ab52e9beb4455de3e53741e3384f 100644 (file)
@@ -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<FilesystemStore>,
 ) {
        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,