@@ -55,13 +55,15 @@ use crate::ln::onion_utils::HTLCFailReason;
5555use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError};
5656#[cfg(test)]
5757use crate::ln::outbound_payment;
58- use crate::ln::outbound_payment::{OutboundPayments, PaymentAttempts, PendingOutboundPayment, SendAlongPathArgs, StaleExpiration};
58+ use crate::ln::outbound_payment::{Bolt12PaymentError, OutboundPayments, PaymentAttempts, PendingOutboundPayment, SendAlongPathArgs, StaleExpiration};
5959use crate::ln::wire::Encode;
60- use crate::offers::invoice::{BlindedPayInfo, DEFAULT_RELATIVE_EXPIRY};
60+ use crate::offers::invoice::{BlindedPayInfo, Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, InvoiceBuilder};
61+ use crate::offers::invoice_error::InvoiceError;
62+ use crate::offers::merkle::SignError;
6163use crate::offers::offer::{DerivedMetadata, Offer, OfferBuilder};
6264use crate::offers::parse::Bolt12SemanticError;
6365use crate::offers::refund::{Refund, RefundBuilder};
64- use crate::onion_message::{Destination, OffersMessage, PendingOnionMessage};
66+ use crate::onion_message::{Destination, OffersMessage, OffersMessageHandler, PendingOnionMessage};
6567use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider, WriteableEcdsaChannelSigner};
6668use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
6769use crate::util::wakers::{Future, Notifier};
@@ -3554,6 +3556,17 @@ where
35543556 self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
35553557 }
35563558
3559+ pub(super) fn send_payment_for_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
3560+ let best_block_height = self.best_block.read().unwrap().height();
3561+ let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
3562+ self.pending_outbound_payments
3563+ .send_payment_for_bolt12_invoice(
3564+ invoice, payment_id, &self.router, self.list_usable_channels(),
3565+ || self.compute_inflight_htlcs(), &self.entropy_source, &self.node_signer,
3566+ best_block_height, &self.logger, &self.pending_events,
3567+ |args| self.send_payment_along_path(args)
3568+ )
3569+ }
35573570
35583571 /// Signals that no further attempts for the given payment should occur. Useful if you have a
35593572 /// pending outbound payment with retries remaining, but wish to stop retrying the payment before
@@ -8654,6 +8667,127 @@ where
86548667 }
86558668}
86568669
8670+ impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>
8671+ OffersMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, L>
8672+ where
8673+ M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
8674+ T::Target: BroadcasterInterface,
8675+ ES::Target: EntropySource,
8676+ NS::Target: NodeSigner,
8677+ SP::Target: SignerProvider,
8678+ F::Target: FeeEstimator,
8679+ R::Target: Router,
8680+ L::Target: Logger,
8681+ {
8682+ fn handle_message(&self, message: OffersMessage) -> Option<OffersMessage> {
8683+ let secp_ctx = &self.secp_ctx;
8684+ let expanded_key = &self.inbound_payment_key;
8685+
8686+ match message {
8687+ OffersMessage::InvoiceRequest(invoice_request) => {
8688+ let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats(
8689+ &invoice_request
8690+ ) {
8691+ Ok(amount_msats) => Some(amount_msats),
8692+ Err(error) => return Some(OffersMessage::InvoiceError(error.into())),
8693+ };
8694+ let invoice_request = match invoice_request.verify(expanded_key, secp_ctx) {
8695+ Ok(invoice_request) => invoice_request,
8696+ Err(()) => {
8697+ let error = Bolt12SemanticError::InvalidMetadata;
8698+ return Some(OffersMessage::InvoiceError(error.into()));
8699+ },
8700+ };
8701+ let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
8702+
8703+ match self.create_inbound_payment(amount_msats, relative_expiry, None) {
8704+ Ok((payment_hash, payment_secret)) if invoice_request.keys.is_some() => {
8705+ let payment_paths = vec![
8706+ self.create_one_hop_blinded_payment_path(payment_secret),
8707+ ];
8708+ #[cfg(not(feature = "no-std"))]
8709+ let builder = invoice_request.respond_using_derived_keys(
8710+ payment_paths, payment_hash
8711+ );
8712+ #[cfg(feature = "no-std")]
8713+ let created_at = Duration::from_secs(
8714+ self.highest_seen_timestamp.load(Ordering::Acquire) as u64
8715+ );
8716+ #[cfg(feature = "no-std")]
8717+ let builder = invoice_request.respond_using_derived_keys_no_std(
8718+ payment_paths, payment_hash, created_at
8719+ );
8720+ match builder.and_then(|b| b.allow_mpp().build_and_sign(secp_ctx)) {
8721+ Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
8722+ Err(error) => Some(OffersMessage::InvoiceError(error.into())),
8723+ }
8724+ },
8725+ Ok((payment_hash, payment_secret)) => {
8726+ let payment_paths = vec![
8727+ self.create_one_hop_blinded_payment_path(payment_secret),
8728+ ];
8729+ #[cfg(not(feature = "no-std"))]
8730+ let builder = invoice_request.respond_with(payment_paths, payment_hash);
8731+ #[cfg(feature = "no-std")]
8732+ let created_at = Duration::from_secs(
8733+ self.highest_seen_timestamp.load(Ordering::Acquire) as u64
8734+ );
8735+ #[cfg(feature = "no-std")]
8736+ let builder = invoice_request.respond_with_no_std(
8737+ payment_paths, payment_hash, created_at
8738+ );
8739+ let response = builder.and_then(|builder| builder.allow_mpp().build())
8740+ .map_err(|e| OffersMessage::InvoiceError(e.into()))
8741+ .and_then(|invoice|
8742+ match invoice.sign(|invoice| self.node_signer.sign_bolt12_invoice(invoice)) {
8743+ Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
8744+ Err(SignError::Signing(())) => Err(OffersMessage::InvoiceError(
8745+ InvoiceError::from_str("Failed signing invoice")
8746+ )),
8747+ Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
8748+ InvoiceError::from_str("Failed invoice signature verification")
8749+ )),
8750+ });
8751+ match response {
8752+ Ok(invoice) => Some(invoice),
8753+ Err(error) => Some(error),
8754+ }
8755+ },
8756+ Err(()) => {
8757+ Some(OffersMessage::InvoiceError(Bolt12SemanticError::InvalidAmount.into()))
8758+ },
8759+ }
8760+ },
8761+ OffersMessage::Invoice(invoice) => {
8762+ match invoice.verify(expanded_key, secp_ctx) {
8763+ Err(()) => {
8764+ Some(OffersMessage::InvoiceError(InvoiceError::from_str("Unrecognized invoice")))
8765+ },
8766+ Ok(_) if invoice.invoice_features().requires_unknown_bits_from(&self.bolt12_invoice_features()) => {
8767+ Some(OffersMessage::InvoiceError(Bolt12SemanticError::UnknownRequiredFeatures.into()))
8768+ },
8769+ Ok(payment_id) => {
8770+ if let Err(e) = self.send_payment_for_bolt12_invoice(&invoice, payment_id) {
8771+ log_trace!(self.logger, "Failed paying invoice: {:?}", e);
8772+ Some(OffersMessage::InvoiceError(InvoiceError::from_str(&format!("{:?}", e))))
8773+ } else {
8774+ None
8775+ }
8776+ },
8777+ }
8778+ },
8779+ OffersMessage::InvoiceError(invoice_error) => {
8780+ log_trace!(self.logger, "Received invoice_error: {}", invoice_error);
8781+ None
8782+ },
8783+ }
8784+ }
8785+
8786+ fn release_pending_messages(&self) -> Vec<PendingOnionMessage<OffersMessage>> {
8787+ core::mem::take(&mut self.pending_offers_messages.lock().unwrap())
8788+ }
8789+ }
8790+
86578791/// Fetches the set of [`NodeFeatures`] flags that are provided by or required by
86588792/// [`ChannelManager`].
86598793pub(crate) fn provided_node_features(config: &UserConfig) -> NodeFeatures {
0 commit comments