@@ -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, TaggedHash};
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,123 @@ 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(|message: &TaggedHash, metadata: &[u8]|
7530+ self.node_signer.sign_bolt12_message(message, metadata)
7531+ ) {
7532+ Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
7533+ Err(SignError::Signing(())) => Err(OffersMessage::InvoiceError(
7534+ InvoiceError::from_str("Failed signing invoice")
7535+ )),
7536+ Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
7537+ InvoiceError::from_str("Failed invoice signature verification")
7538+ )),
7539+ });
7540+ match response {
7541+ Ok(invoice) => Some(invoice),
7542+ Err(error) => Some(error),
7543+ }
7544+ },
7545+ Err(()) => {
7546+ Some(OffersMessage::InvoiceError(Bolt12SemanticError::InvalidAmount.into()))
7547+ },
7548+ }
7549+ },
7550+ OffersMessage::Invoice(invoice) => {
7551+ match invoice.verify(expanded_key, secp_ctx) {
7552+ Err(()) => {
7553+ Some(OffersMessage::InvoiceError(InvoiceError::from_str("Unrecognized invoice")))
7554+ },
7555+ Ok(_) if invoice.features().requires_unknown_bits() => {
7556+ Some(OffersMessage::InvoiceError(Bolt12SemanticError::UnknownRequiredFeatures.into()))
7557+ },
7558+ Ok(payment_id) => {
7559+ if let Err(e) = self.send_payment_for_bolt12_invoice(&invoice, payment_id) {
7560+ log_error!(self.logger, "Failed paying invoice: {:?}", e);
7561+ Some(OffersMessage::InvoiceError(InvoiceError::from_str(&format!("{:?}", e))))
7562+ } else {
7563+ None
7564+ }
7565+ },
7566+ }
7567+ },
7568+ OffersMessage::InvoiceError(invoice_error) => {
7569+ log_error!(self.logger, "Received invoice_error: {}", invoice_error);
7570+ None
7571+ },
7572+ }
7573+ }
7574+ }
7575+
74437576/// Fetches the set of [`NodeFeatures`] flags which are provided by or required by
74447577/// [`ChannelManager`].
74457578pub(crate) fn provided_node_features(config: &UserConfig) -> NodeFeatures {
0 commit comments