@@ -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};
@@ -3579,6 +3581,17 @@ where
35793581 self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
35803582 }
35813583
3584+ pub(super) fn send_payment_for_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
3585+ let best_block_height = self.best_block.read().unwrap().height();
3586+ let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
3587+ self.pending_outbound_payments
3588+ .send_payment_for_bolt12_invoice(
3589+ invoice, payment_id, &self.router, self.list_usable_channels(),
3590+ || self.compute_inflight_htlcs(), &self.entropy_source, &self.node_signer,
3591+ best_block_height, &self.logger, &self.pending_events,
3592+ |args| self.send_payment_along_path(args)
3593+ )
3594+ }
35823595
35833596 /// Signals that no further attempts for the given payment should occur. Useful if you have a
35843597 /// pending outbound payment with retries remaining, but wish to stop retrying the payment before
@@ -8809,6 +8822,127 @@ where
88098822 }
88108823}
88118824
8825+ impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>
8826+ OffersMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, L>
8827+ where
8828+ M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
8829+ T::Target: BroadcasterInterface,
8830+ ES::Target: EntropySource,
8831+ NS::Target: NodeSigner,
8832+ SP::Target: SignerProvider,
8833+ F::Target: FeeEstimator,
8834+ R::Target: Router,
8835+ L::Target: Logger,
8836+ {
8837+ fn handle_message(&self, message: OffersMessage) -> Option<OffersMessage> {
8838+ let secp_ctx = &self.secp_ctx;
8839+ let expanded_key = &self.inbound_payment_key;
8840+
8841+ match message {
8842+ OffersMessage::InvoiceRequest(invoice_request) => {
8843+ let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats(
8844+ &invoice_request
8845+ ) {
8846+ Ok(amount_msats) => Some(amount_msats),
8847+ Err(error) => return Some(OffersMessage::InvoiceError(error.into())),
8848+ };
8849+ let invoice_request = match invoice_request.verify(expanded_key, secp_ctx) {
8850+ Ok(invoice_request) => invoice_request,
8851+ Err(()) => {
8852+ let error = Bolt12SemanticError::InvalidMetadata;
8853+ return Some(OffersMessage::InvoiceError(error.into()));
8854+ },
8855+ };
8856+ let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
8857+
8858+ match self.create_inbound_payment(amount_msats, relative_expiry, None) {
8859+ Ok((payment_hash, payment_secret)) if invoice_request.keys.is_some() => {
8860+ let payment_paths = vec![
8861+ self.create_one_hop_blinded_payment_path(payment_secret),
8862+ ];
8863+ #[cfg(not(feature = "no-std"))]
8864+ let builder = invoice_request.respond_using_derived_keys(
8865+ payment_paths, payment_hash
8866+ );
8867+ #[cfg(feature = "no-std")]
8868+ let created_at = Duration::from_secs(
8869+ self.highest_seen_timestamp.load(Ordering::Acquire) as u64
8870+ );
8871+ #[cfg(feature = "no-std")]
8872+ let builder = invoice_request.respond_using_derived_keys_no_std(
8873+ payment_paths, payment_hash, created_at
8874+ );
8875+ match builder.and_then(|b| b.allow_mpp().build_and_sign(secp_ctx)) {
8876+ Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
8877+ Err(error) => Some(OffersMessage::InvoiceError(error.into())),
8878+ }
8879+ },
8880+ Ok((payment_hash, payment_secret)) => {
8881+ let payment_paths = vec![
8882+ self.create_one_hop_blinded_payment_path(payment_secret),
8883+ ];
8884+ #[cfg(not(feature = "no-std"))]
8885+ let builder = invoice_request.respond_with(payment_paths, payment_hash);
8886+ #[cfg(feature = "no-std")]
8887+ let created_at = Duration::from_secs(
8888+ self.highest_seen_timestamp.load(Ordering::Acquire) as u64
8889+ );
8890+ #[cfg(feature = "no-std")]
8891+ let builder = invoice_request.respond_with_no_std(
8892+ payment_paths, payment_hash, created_at
8893+ );
8894+ let response = builder.and_then(|builder| builder.allow_mpp().build())
8895+ .map_err(|e| OffersMessage::InvoiceError(e.into()))
8896+ .and_then(|invoice|
8897+ match invoice.sign(|invoice| self.node_signer.sign_bolt12_invoice(invoice)) {
8898+ Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
8899+ Err(SignError::Signing(())) => Err(OffersMessage::InvoiceError(
8900+ InvoiceError::from_str("Failed signing invoice")
8901+ )),
8902+ Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
8903+ InvoiceError::from_str("Failed invoice signature verification")
8904+ )),
8905+ });
8906+ match response {
8907+ Ok(invoice) => Some(invoice),
8908+ Err(error) => Some(error),
8909+ }
8910+ },
8911+ Err(()) => {
8912+ Some(OffersMessage::InvoiceError(Bolt12SemanticError::InvalidAmount.into()))
8913+ },
8914+ }
8915+ },
8916+ OffersMessage::Invoice(invoice) => {
8917+ match invoice.verify(expanded_key, secp_ctx) {
8918+ Err(()) => {
8919+ Some(OffersMessage::InvoiceError(InvoiceError::from_str("Unrecognized invoice")))
8920+ },
8921+ Ok(_) if invoice.invoice_features().requires_unknown_bits_from(&self.bolt12_invoice_features()) => {
8922+ Some(OffersMessage::InvoiceError(Bolt12SemanticError::UnknownRequiredFeatures.into()))
8923+ },
8924+ Ok(payment_id) => {
8925+ if let Err(e) = self.send_payment_for_bolt12_invoice(&invoice, payment_id) {
8926+ log_trace!(self.logger, "Failed paying invoice: {:?}", e);
8927+ Some(OffersMessage::InvoiceError(InvoiceError::from_str(&format!("{:?}", e))))
8928+ } else {
8929+ None
8930+ }
8931+ },
8932+ }
8933+ },
8934+ OffersMessage::InvoiceError(invoice_error) => {
8935+ log_trace!(self.logger, "Received invoice_error: {}", invoice_error);
8936+ None
8937+ },
8938+ }
8939+ }
8940+
8941+ fn release_pending_messages(&self) -> Vec<PendingOnionMessage<OffersMessage>> {
8942+ core::mem::take(&mut self.pending_offers_messages.lock().unwrap())
8943+ }
8944+ }
8945+
88128946/// Fetches the set of [`NodeFeatures`] flags that are provided by or required by
88138947/// [`ChannelManager`].
88148948pub(crate) fn provided_node_features(config: &UserConfig) -> NodeFeatures {
0 commit comments