Skip to content

Commit

Permalink
Use payment_parameters_from_invoice rather than building by hand
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueMatt committed Feb 6, 2024
1 parent 3bb8258 commit f0eb724
Showing 1 changed file with 10 additions and 37 deletions.
47 changes: 10 additions & 37 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down

0 comments on commit f0eb724

Please sign in to comment.