@@ -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, WriteableEcdsaChannelSigner};
5964use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
6065use crate::util::wakers::{Future, Notifier};
@@ -3390,6 +3395,17 @@ where
33903395 self.pending_outbound_payments.test_set_payment_metadata(payment_id, new_payment_metadata);
33913396 }
33923397
3398+ pub(super) fn send_payment_for_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), RetryableSendFailure> {
3399+ let best_block_height = self.best_block.read().unwrap().height();
3400+ let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
3401+ self.pending_outbound_payments
3402+ .send_payment_for_bolt12_invoice(
3403+ invoice, payment_id, &self.router, self.list_usable_channels(),
3404+ || self.compute_inflight_htlcs(), &self.entropy_source, &self.node_signer,
3405+ best_block_height, &self.logger, &self.pending_events,
3406+ |args| self.send_payment_along_path(args)
3407+ )
3408+ }
33933409
33943410 /// Signals that no further retries for the given payment should occur. Useful if you have a
33953411 /// pending outbound payment with retries remaining, but wish to stop retrying the payment before
@@ -7720,6 +7736,121 @@ where
77207736 }
77217737}
77227738
7739+ impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>
7740+ OffersMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, L>
7741+ where
7742+ M::Target: chain::Watch<<SP::Target as SignerProvider>::Signer>,
7743+ T::Target: BroadcasterInterface,
7744+ ES::Target: EntropySource,
7745+ NS::Target: NodeSigner,
7746+ SP::Target: SignerProvider,
7747+ F::Target: FeeEstimator,
7748+ R::Target: Router,
7749+ L::Target: Logger,
7750+ {
7751+ fn handle_message(&self, message: OffersMessage) -> Option<OffersMessage> {
7752+ let secp_ctx = &self.secp_ctx;
7753+ let expanded_key = &self.inbound_payment_key;
7754+
7755+ match message {
7756+ OffersMessage::InvoiceRequest(invoice_request) => {
7757+ let amount_msats = match InvoiceBuilder::<DerivedSigningPubkey>::amount_msats(
7758+ &invoice_request
7759+ ) {
7760+ Ok(amount_msats) => Some(amount_msats),
7761+ Err(error) => return Some(OffersMessage::InvoiceError(error.into())),
7762+ };
7763+ let invoice_request = match invoice_request.verify(expanded_key, secp_ctx) {
7764+ Ok(invoice_request) => invoice_request,
7765+ Err(()) => {
7766+ let error = Bolt12SemanticError::InvalidMetadata;
7767+ return Some(OffersMessage::InvoiceError(error.into()));
7768+ },
7769+ };
7770+ let relative_expiry = DEFAULT_RELATIVE_EXPIRY.as_secs() as u32;
7771+
7772+ match self.create_inbound_payment(amount_msats, relative_expiry, None) {
7773+ Ok((payment_hash, _payment_secret)) if invoice_request.keys.is_some() => {
7774+ // TODO: Include payment_secret in payment_paths.
7775+ let payment_paths = vec![];
7776+ #[cfg(not(feature = "no-std"))]
7777+ let builder = invoice_request.respond_using_derived_keys(
7778+ payment_paths, payment_hash
7779+ );
7780+ #[cfg(feature = "no-std")]
7781+ let created_at = Duration::from_secs(
7782+ self.highest_seen_timestamp.load(Ordering::Acquire) as u64
7783+ );
7784+ #[cfg(feature = "no-std")]
7785+ let builder = invoice_request.respond_using_derived_keys_no_std(
7786+ payment_paths, payment_hash, created_at
7787+ );
7788+ match builder.and_then(|b| b.allow_mpp().build_and_sign(secp_ctx)) {
7789+ Ok(invoice) => Some(OffersMessage::Invoice(invoice)),
7790+ Err(error) => Some(OffersMessage::InvoiceError(error.into())),
7791+ }
7792+ },
7793+ Ok((payment_hash, _payment_secret)) => {
7794+ // TODO: Include payment_secret in payment_paths.
7795+ let payment_paths = vec![];
7796+ #[cfg(not(feature = "no-std"))]
7797+ let builder = invoice_request.respond_with(payment_paths, payment_hash);
7798+ #[cfg(feature = "no-std")]
7799+ let created_at = Duration::from_secs(
7800+ self.highest_seen_timestamp.load(Ordering::Acquire) as u64
7801+ );
7802+ #[cfg(feature = "no-std")]
7803+ let builder = invoice_request.respond_with_no_std(
7804+ payment_paths, payment_hash, created_at
7805+ );
7806+ let response = builder.and_then(|builder| builder.allow_mpp().build())
7807+ .map_err(|e| OffersMessage::InvoiceError(e.into()))
7808+ .and_then(|invoice|
7809+ match invoice.sign(|invoice| self.node_signer.sign_bolt12_invoice(invoice)) {
7810+ Ok(invoice) => Ok(OffersMessage::Invoice(invoice)),
7811+ Err(SignError::Signing(())) => Err(OffersMessage::InvoiceError(
7812+ InvoiceError::from_str("Failed signing invoice")
7813+ )),
7814+ Err(SignError::Verification(_)) => Err(OffersMessage::InvoiceError(
7815+ InvoiceError::from_str("Failed invoice signature verification")
7816+ )),
7817+ });
7818+ match response {
7819+ Ok(invoice) => Some(invoice),
7820+ Err(error) => Some(error),
7821+ }
7822+ },
7823+ Err(()) => {
7824+ Some(OffersMessage::InvoiceError(Bolt12SemanticError::InvalidAmount.into()))
7825+ },
7826+ }
7827+ },
7828+ OffersMessage::Invoice(invoice) => {
7829+ match invoice.verify(expanded_key, secp_ctx) {
7830+ Err(()) => {
7831+ Some(OffersMessage::InvoiceError(InvoiceError::from_str("Unrecognized invoice")))
7832+ },
7833+ Ok(_) if invoice.invoice_features().requires_unknown_bits() => {
7834+ Some(OffersMessage::InvoiceError(Bolt12SemanticError::UnknownRequiredFeatures.into()))
7835+ },
7836+ Ok(payment_id) => {
7837+ if let Err(e) = self.send_payment_for_bolt12_invoice(&invoice, payment_id) {
7838+ log_error!(self.logger, "Failed paying invoice: {:?}", e);
7839+ Some(OffersMessage::InvoiceError(InvoiceError::from_str(&format!("{:?}", e))))
7840+ } else {
7841+ None
7842+ }
7843+ },
7844+ }
7845+ },
7846+ OffersMessage::InvoiceError(invoice_error) => {
7847+ log_error!(self.logger, "Received invoice_error: {}", invoice_error);
7848+ None
7849+ },
7850+ }
7851+ }
7852+ }
7853+
77237854/// Fetches the set of [`NodeFeatures`] flags which are provided by or required by
77247855/// [`ChannelManager`].
77257856pub(crate) fn provided_node_features(config: &UserConfig) -> NodeFeatures {
0 commit comments