@@ -55,6 +55,11 @@ use crate::ln::msgs::{ChannelMessageHandler, DecodeError, LightningError};
5555use crate::ln::outbound_payment;
5656use crate::ln::outbound_payment::{OutboundPayments, PaymentAttempts, PendingOutboundPayment, SendAlongPathArgs};
5757use crate::ln::wire::Encode;
58+ use crate::offers::invoice::{Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, InvoiceBuilder};
59+ use crate::offers::invoice_error::InvoiceError;
60+ use crate::offers::merkle::SignError;
61+ use crate::offers::parse::Bolt12SemanticError;
62+ use crate::onion_message::{OffersMessage, OffersMessageHandler};
5863use crate::sign::{EntropySource, KeysManager, NodeSigner, Recipient, SignerProvider, ChannelSigner, WriteableEcdsaChannelSigner};
5964use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
6065use crate::util::wakers::{Future, Notifier};
@@ -3291,6 +3296,17 @@ where
32913296		self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
32923297	}
32933298
3299+ 	pub(super) fn send_payment_for_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), RetryableSendFailure> {
3300+ 		let best_block_height = self.best_block.read().unwrap().height();
3301+ 		let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
3302+ 		self.pending_outbound_payments
3303+ 			.send_payment_for_bolt12_invoice(
3304+ 				invoice, payment_id, &self.router, self.list_usable_channels(),
3305+ 				|| self.compute_inflight_htlcs(), &self.entropy_source, &self.node_signer,
3306+ 				best_block_height, &self.logger, &self.pending_events,
3307+ 				|args| self.send_payment_along_path(args)
3308+ 			)
3309+ 	}
32943310
32953311	/// Signals that no further retries for the given payment should occur. Useful if you have a
32963312	/// pending outbound payment with retries remaining, but wish to stop retrying the payment before
@@ -7440,6 +7456,121 @@ where
74407456	}
74417457}
74427458
7459+ impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>
7460+ OffersMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, L>
7461+ where
7462+ 	M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
7463+ 	T::Target: BroadcasterInterface,
7464+ 	ES::Target: EntropySource,
7465+ 	NS::Target: NodeSigner,
7466+ 	SP::Target: SignerProvider,
7467+ 	F::Target: FeeEstimator,
7468+ 	R::Target: Router,
7469+ 	L::Target: Logger,
7470+ {
7471+ 	fn handle_message(&self, message: OffersMessage) -> Option<OffersMessage> {
7472+ 		let secp_ctx = &self.secp_ctx;
7473+ 		let expanded_key = &self.inbound_payment_key;
7474+ 
7475+ 		match message {
7476+ 			OffersMessage::InvoiceRequest(invoice_request) => {
7477+ 				let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats(
7478+ 					&invoice_request
7479+ 				) {
7480+ 					Ok(amount_msats) => Some(amount_msats),
7481+ 					Err(error) => return Some(OffersMessage::InvoiceError(error.into())),
7482+ 				};
7483+ 				let invoice_request = match invoice_request.verify(expanded_key, secp_ctx) {
7484+ 					Ok(invoice_request) => invoice_request,
7485+ 					Err(()) => {
7486+ 						let error = Bolt12SemanticError::InvalidMetadata;
7487+ 						return Some(OffersMessage::InvoiceError(error.into()));
7488+ 					},
7489+ 				};
7490+ 				let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
7491+ 
7492+ 				match self.create_inbound_payment(amount_msats, relative_expiry, None) {
7493+ 					Ok((payment_hash, _payment_secret)) if invoice_request.keys.is_some() => {
7494+ 						// TODO: Include payment_secret in payment_paths.
7495+ 						let payment_paths = vec![];
7496+ 						#[cfg(not(feature = "no-std"))]
7497+ 						let builder = invoice_request.respond_using_derived_keys(
7498+ 							payment_paths, payment_hash
7499+ 						);
7500+ 						#[cfg(feature = "no-std")]
7501+ 						let created_at = Duration::from_secs(
7502+ 							self.highest_seen_timestamp.load(Ordering::Acquire) as u64
7503+ 						);
7504+ 						#[cfg(feature = "no-std")]
7505+ 						let builder = invoice_request.respond_using_derived_keys_no_std(
7506+ 							payment_paths, payment_hash, created_at
7507+ 						);
7508+ 						match builder.and_then(|b| b.allow_mpp().build_and_sign(secp_ctx)) {
7509+ 							Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
7510+ 							Err(error) => Some(OffersMessage::InvoiceError(error.into())),
7511+ 						}
7512+ 					},
7513+ 					Ok((payment_hash, _payment_secret)) => {
7514+ 						// TODO: Include payment_secret in payment_paths.
7515+ 						let payment_paths = vec![];
7516+ 						#[cfg(not(feature = "no-std"))]
7517+ 						let builder = invoice_request.respond_with(payment_paths, payment_hash);
7518+ 						#[cfg(feature = "no-std")]
7519+ 						let created_at = Duration::from_secs(
7520+ 							self.highest_seen_timestamp.load(Ordering::Acquire) as u64
7521+ 						);
7522+ 						#[cfg(feature = "no-std")]
7523+ 						let builder = invoice_request.respond_with_no_std(
7524+ 							payment_paths, payment_hash, created_at
7525+ 						);
7526+ 						let response = builder.and_then(|builder| builder.allow_mpp().build())
7527+ 							.map_err(|e| OffersMessage::InvoiceError(e.into()))
7528+ 							.and_then(|invoice|
7529+ 								match invoice.sign(|invoice| self.node_signer.sign_bolt12_invoice(invoice)) {
7530+ 									Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
7531+ 									Err(SignError::Signing(())) => Err(OffersMessage::InvoiceError(
7532+ 											InvoiceError::from_str("Failed signing invoice")
7533+ 									)),
7534+ 									Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
7535+ 											InvoiceError::from_str("Failed invoice signature verification")
7536+ 									)),
7537+ 								});
7538+ 						match response {
7539+ 							Ok(invoice) => Some(invoice),
7540+ 							Err(error) => Some(error),
7541+ 						}
7542+ 					},
7543+ 					Err(()) => {
7544+ 						Some(OffersMessage::InvoiceError(Bolt12SemanticError::InvalidAmount.into()))
7545+ 					},
7546+ 				}
7547+ 			},
7548+ 			OffersMessage::Invoice(invoice) => {
7549+ 				match invoice.verify(expanded_key, secp_ctx) {
7550+ 					Err(()) => {
7551+ 						Some(OffersMessage::InvoiceError(InvoiceError::from_str("Unrecognized invoice")))
7552+ 					},
7553+ 					Ok(_) if invoice.invoice_features().requires_unknown_bits() => {
7554+ 						Some(OffersMessage::InvoiceError(Bolt12SemanticError::UnknownRequiredFeatures.into()))
7555+ 					},
7556+ 					Ok(payment_id) => {
7557+ 						if let Err(e) = self.send_payment_for_bolt12_invoice(&invoice, payment_id) {
7558+ 							log_error!(self.logger, "Failed paying invoice: {:?}", e);
7559+ 							Some(OffersMessage::InvoiceError(InvoiceError::from_str(&format!("{:?}", e))))
7560+ 						} else {
7561+ 							None
7562+ 						}
7563+ 					},
7564+ 				}
7565+ 			},
7566+ 			OffersMessage::InvoiceError(invoice_error) => {
7567+ 				log_error!(self.logger, "Received invoice_error: {}", invoice_error);
7568+ 				None
7569+ 			},
7570+ 		}
7571+ 	}
7572+ }
7573+ 
74437574/// Fetches the set of [`NodeFeatures`] flags which are provided by or required by
74447575/// [`ChannelManager`].
74457576pub(crate) fn provided_node_features(config: &UserConfig) -> NodeFeatures {
0 commit comments