@@ -57,7 +57,7 @@ use crate::ln::onion_utils::{HTLCFailReason, INVALID_ONION_BLINDING};
5757use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError};
5858#[cfg(test)]
5959use crate::ln::outbound_payment;
60- use crate::ln::outbound_payment::{Bolt12PaymentError, OutboundPayments, PaymentAttempts, PendingOutboundPayment, SendAlongPathArgs, StaleExpiration};
60+ use crate::ln::outbound_payment::{OutboundPayments, PaymentAttempts, PendingOutboundPayment, SendAlongPathArgs, StaleExpiration};
6161use crate::ln::wire::Encode;
6262use crate::offers::invoice::{BlindedPayInfo, Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder, UnsignedBolt12Invoice};
6363use crate::offers::invoice_error::InvoiceError;
@@ -104,7 +104,7 @@ use core::time::Duration;
104104use core::ops::Deref;
105105
106106// Re-export this for use in the public API.
107- pub use crate::ln::outbound_payment::{PaymentSendFailure, ProbeSendFailure, Retry, RetryableSendFailure, RecipientOnionFields};
107+ pub use crate::ln::outbound_payment::{Bolt12PaymentError, PaymentSendFailure, ProbeSendFailure, Retry, RetryableSendFailure, RecipientOnionFields};
108108use crate::ln::script::ShutdownScript;
109109
110110// We hold various information about HTLC relay in the HTLC objects in Channel itself:
@@ -4272,7 +4272,28 @@ where
42724272 self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
42734273 }
42744274
4275- pub(super) fn send_payment_for_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
4275+ /// Pays the [`Bolt12Invoice`] associated with the `payment_id` encoded in its `payer_metadata`.
4276+ ///
4277+ /// The invoice's `payer_metadata` is used to authenticate that the invoice was indeed requested
4278+ /// before attempting a payment. [`Bolt12PaymentError::UnexpectedInvoice`] is returned if this
4279+ /// fails or if the encoded `payment_id` is not recognized. The latter may happen once the
4280+ /// invoice is paid and the payment no longer tracked.
4281+ ///
4282+ /// Attempting to pay the same invoice twice while the first payment is still pending will
4283+ /// result in a [`Bolt12PaymentError::DuplicateInvoice`].
4284+ ///
4285+ /// Otherwise, either [`Event::PaymentSent`] or [`Event::PaymentFailed`] are used to indicate
4286+ /// whether or not the payment was successful.
4287+ pub fn send_payment_for_bolt12_invoice(&self, invoice: &Bolt12Invoice) -> Result<(), Bolt12PaymentError> {
4288+ let secp_ctx = &self.secp_ctx;
4289+ let expanded_key = &self.inbound_payment_key;
4290+ match invoice.verify(expanded_key, secp_ctx) {
4291+ Ok(payment_id) => self.send_payment_for_verified_bolt12_invoice(invoice, payment_id),
4292+ Err(()) => Err(Bolt12PaymentError::UnexpectedInvoice),
4293+ }
4294+ }
4295+
4296+ fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
42764297 let best_block_height = self.best_block.read().unwrap().height;
42774298 let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
42784299 self.pending_outbound_payments
@@ -10453,7 +10474,7 @@ where
1045310474 self.pending_events.lock().unwrap().push_back((event, None));
1045410475 return ResponseInstruction::NoResponse;
1045510476 } else {
10456- self.send_payment_for_bolt12_invoice (&invoice, payment_id)
10477+ self.send_payment_for_verified_bolt12_invoice (&invoice, payment_id)
1045710478 .map_err(|e| {
1045810479 log_trace!(self.logger, "Failed paying invoice: {:?}", e);
1045910480 InvoiceError::from_string(format!("{:?}", e))
0 commit comments