From 7a61ce53aa9314564ef7ab8c776ee32b64b025d7 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 12 Dec 2023 11:39:43 +0100 Subject: [PATCH] f Account for `lightning-invoice` payment utils being removed --- src/lib.rs | 95 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 42 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index be78efd8e..361d218d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1117,7 +1117,10 @@ impl Node { return Err(Error::NotRunning); } - let payment_hash = PaymentHash((*invoice.payment_hash()).to_byte_array()); + let (payment_hash, recipient_onion, route_params) = payment::payment_parameters_from_invoice(&invoice).map_err(|_| { + log_error!(self.logger, "Failed to send payment due to the given invoice being \"zero-amount\". Please use send_payment_using_amount instead."); + Error::InvalidInvoice + })?; if let Some(payment) = self.payment_store.get(&payment_hash) { if payment.status == PaymentStatus::Pending @@ -1129,13 +1132,17 @@ impl Node { } let payment_secret = Some(*invoice.payment_secret()); + let payment_id = PaymentId(invoice.payment_hash().to_byte_array()); + let retry_strategy = Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT); - match lightning_invoice::payment::pay_invoice( - &invoice, - Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT), - self.channel_manager.as_ref(), + match self.channel_manager.send_payment( + payment_hash, + recipient_onion, + payment_id, + route_params, + retry_strategy, ) { - Ok(_payment_id) => { + Ok(()) => { let payee_pubkey = invoice.recover_payee_pub_key(); let amt_msat = invoice.amount_milli_satoshis().unwrap(); log_info!(self.logger, "Initiated sending {}msat to {}", amt_msat, payee_pubkey); @@ -1152,11 +1159,7 @@ impl Node { Ok(payment_hash) } - Err(payment::PaymentError::Invoice(e)) => { - log_error!(self.logger, "Failed to send payment due to invalid invoice: {}", e); - Err(Error::InvalidInvoice) - } - Err(payment::PaymentError::Sending(e)) => { + Err(e) => { log_error!(self.logger, "Failed to send payment: {:?}", e); match e { channelmanager::RetryableSendFailure::DuplicatePayment => { @@ -1234,11 +1237,13 @@ impl Node { let retry_strategy = Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT); let recipient_fields = RecipientOnionFields::secret_only(*payment_secret); - match self - .channel_manager - .send_payment(payment_hash, recipient_fields, payment_id, route_params, retry_strategy) - .map_err(payment::PaymentError::Sending) - { + match self.channel_manager.send_payment( + payment_hash, + recipient_fields, + payment_id, + route_params, + retry_strategy, + ) { Ok(_payment_id) => { let payee_pubkey = invoice.recover_payee_pub_key(); log_info!( @@ -1260,11 +1265,7 @@ impl Node { Ok(payment_hash) } - Err(payment::PaymentError::Invoice(e)) => { - log_error!(self.logger, "Failed to send payment due to invalid invoice: {}", e); - Err(Error::InvalidInvoice) - } - Err(payment::PaymentError::Sending(e)) => { + Err(e) => { log_error!(self.logger, "Failed to send payment: {:?}", e); match e { @@ -1382,17 +1383,19 @@ impl Node { return Err(Error::NotRunning); } + let (_payment_hash, _recipient_onion, route_params) = payment::payment_parameters_from_invoice(&invoice).map_err(|_| { + log_error!(self.logger, "Failed to send probes due to the given invoice being \"zero-amount\". Please use send_payment_probes_using_amount instead."); + Error::InvalidInvoice + })?; + let liquidity_limit_multiplier = Some(self.config.probing_liquidity_limit_multiplier); - payment::preflight_probe_invoice( - invoice, - &*self.channel_manager, - liquidity_limit_multiplier, - ) - .map_err(|e| { - log_error!(self.logger, "Failed to send payment probes: {:?}", e); - Error::ProbeSendingFailed - })?; + self.channel_manager + .send_preflight_probes(route_params, liquidity_limit_multiplier) + .map_err(|e| { + log_error!(self.logger, "Failed to send payment probes: {:?}", e); + Error::ProbeSendingFailed + })?; Ok(()) } @@ -1442,27 +1445,35 @@ impl Node { return Err(Error::NotRunning); } - if let Some(invoice_amount_msat) = invoice.amount_milli_satoshis() { + let (_payment_hash, _recipient_onion, route_params) = if let Some(invoice_amount_msat) = + invoice.amount_milli_satoshis() + { if amount_msat < invoice_amount_msat { log_error!( self.logger, "Failed to send probes as the given amount needs to be at least the invoice amount: required {}msat, gave {}msat.", invoice_amount_msat, amount_msat); return Err(Error::InvalidAmount); } - } + + payment::payment_parameters_from_invoice(&invoice).map_err(|_| { + log_error!(self.logger, "Failed to send probes due to the given invoice unexpectedly being \"zero-amount\"."); + Error::InvalidInvoice + })? + } else { + payment::payment_parameters_from_zero_amount_invoice(&invoice, amount_msat).map_err(|_| { + log_error!(self.logger, "Failed to send probes due to the given invoice unexpectedly being not \"zero-amount\"."); + Error::InvalidInvoice + })? + }; let liquidity_limit_multiplier = Some(self.config.probing_liquidity_limit_multiplier); - payment::preflight_probe_zero_value_invoice( - invoice, - amount_msat, - &*self.channel_manager, - liquidity_limit_multiplier, - ) - .map_err(|e| { - log_error!(self.logger, "Failed to send payment probes: {:?}", e); - Error::ProbeSendingFailed - })?; + self.channel_manager + .send_preflight_probes(route_params, liquidity_limit_multiplier) + .map_err(|e| { + log_error!(self.logger, "Failed to send payment probes: {:?}", e); + Error::ProbeSendingFailed + })?; Ok(()) }