@@ -48,6 +48,7 @@ use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, Messa
48
48
// construct one themselves.
49
49
use crate::ln::inbound_payment;
50
50
use crate::ln::types::ChannelId;
51
+ use crate::offers::offer::Offer;
51
52
use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
52
53
use crate::ln::channel::{self, Channel, ChannelPhase, ChannelError, ChannelUpdateStatus, ShutdownResult, UpdateFulfillCommitFetch, OutboundV1Channel, InboundV1Channel, WithChannelContext};
53
54
use crate::ln::channel_state::ChannelDetails;
@@ -67,7 +68,6 @@ use crate::ln::wire::Encode;
67
68
use crate::offers::invoice::{Bolt12Invoice, DEFAULT_RELATIVE_EXPIRY, DerivedSigningPubkey, InvoiceBuilder};
68
69
use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestBuilder};
69
70
use crate::offers::nonce::Nonce;
70
- use crate::offers::offer::Offer;
71
71
use crate::offers::parse::Bolt12SemanticError;
72
72
use crate::offers::refund::Refund;
73
73
use crate::offers::signer;
@@ -1996,57 +1996,6 @@ where
1996
1996
///
1997
1997
/// ## BOLT 12 Offers
1998
1998
///
1999
- /// Use [`pay_for_offer`] to initiated payment, which sends an [`InvoiceRequest`] for an [`Offer`]
2000
- /// and pays the [`Bolt12Invoice`] response.
2001
- ///
2002
- /// ```
2003
- /// # use lightning::events::{Event, EventsProvider};
2004
- /// # use lightning::ln::channelmanager::{AChannelManager, OffersMessageCommons, PaymentId, RecentPaymentDetails, Retry};
2005
- /// # use lightning::offers::offer::Offer;
2006
- /// #
2007
- /// # fn example<T: AChannelManager>(
2008
- /// # channel_manager: T, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
2009
- /// # payer_note: Option<String>, retry: Retry, max_total_routing_fee_msat: Option<u64>
2010
- /// # ) {
2011
- /// # let channel_manager = channel_manager.get_cm();
2012
- /// let payment_id = PaymentId([42; 32]);
2013
- /// match channel_manager.pay_for_offer(
2014
- /// offer, quantity, amount_msats, payer_note, payment_id, retry, max_total_routing_fee_msat
2015
- /// ) {
2016
- /// Ok(()) => println!("Requesting invoice for offer"),
2017
- /// Err(e) => println!("Unable to request invoice for offer: {:?}", e),
2018
- /// }
2019
- ///
2020
- /// // First the payment will be waiting on an invoice
2021
- /// let expected_payment_id = payment_id;
2022
- /// assert!(
2023
- /// channel_manager.list_recent_payments().iter().find(|details| matches!(
2024
- /// details,
2025
- /// RecentPaymentDetails::AwaitingInvoice { payment_id: expected_payment_id }
2026
- /// )).is_some()
2027
- /// );
2028
- ///
2029
- /// // Once the invoice is received, a payment will be sent
2030
- /// assert!(
2031
- /// channel_manager.list_recent_payments().iter().find(|details| matches!(
2032
- /// details,
2033
- /// RecentPaymentDetails::Pending { payment_id: expected_payment_id, .. }
2034
- /// )).is_some()
2035
- /// );
2036
- ///
2037
- /// // On the event processing thread
2038
- /// channel_manager.process_pending_events(&|event| {
2039
- /// match event {
2040
- /// Event::PaymentSent { payment_id: Some(payment_id), .. } => println!("Paid {}", payment_id),
2041
- /// Event::PaymentFailed { payment_id, .. } => println!("Failed paying {}", payment_id),
2042
- /// // ...
2043
- /// # _ => {},
2044
- /// }
2045
- /// Ok(())
2046
- /// });
2047
- /// # }
2048
- /// ```
2049
- ///
2050
1999
/// ## BOLT 12 Refunds
2051
2000
///
2052
2001
/// Use [`request_refund_payment`] to send a [`Bolt12Invoice`] for receiving the refund. Similar to
@@ -2173,7 +2122,7 @@ where
2173
2122
/// [`claim_funds`]: Self::claim_funds
2174
2123
/// [`send_payment`]: Self::send_payment
2175
2124
/// [`offers`]: crate::offers
2176
- /// [`pay_for_offer `]: Self::pay_for_offer
2125
+ /// [`Offer `]: crate::offers::offer
2177
2126
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
2178
2127
/// [`request_refund_payment`]: Self::request_refund_payment
2179
2128
/// [`peer_disconnected`]: msgs::ChannelMessageHandler::peer_disconnected
@@ -2685,6 +2634,8 @@ const MAX_NO_CHANNEL_PEERS: usize = 250;
2685
2634
/// Using compact [`BlindedMessagePath`]s may provide better privacy as the [`MessageRouter`] could select
2686
2635
/// more hops. However, since they use short channel ids instead of pubkeys, they are more likely to
2687
2636
/// become invalid over time as channels are closed. Thus, they are only suitable for short-term use.
2637
+ ///
2638
+ /// [`Offer`]: crate::offers::offer
2688
2639
pub const MAX_SHORT_LIVED_RELATIVE_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24);
2689
2640
2690
2641
/// Used by [`ChannelManager::list_recent_payments`] to express the status of recent payments.
@@ -2693,8 +2644,10 @@ pub const MAX_SHORT_LIVED_RELATIVE_EXPIRY: Duration = Duration::from_secs(60 * 6
2693
2644
pub enum RecentPaymentDetails {
2694
2645
/// When an invoice was requested and thus a payment has not yet been sent.
2695
2646
AwaitingInvoice {
2696
- /// A user-provided identifier in [`ChannelManager ::pay_for_offer`] used to uniquely identify a
2647
+ /// A user-provided identifier in [`OffersMessageFlow ::pay_for_offer`] used to uniquely identify a
2697
2648
/// payment and ensure idempotency in LDK.
2649
+ ///
2650
+ /// [`OffersMessageFlow::pay_for_offer`]: crate::offers::flow::OffersMessageFlow::pay_for_offer
2698
2651
payment_id: PaymentId,
2699
2652
},
2700
2653
/// When a payment is still being sent and awaiting successful delivery.
@@ -2703,7 +2656,7 @@ pub enum RecentPaymentDetails {
2703
2656
/// identify a payment and ensure idempotency in LDK.
2704
2657
///
2705
2658
/// [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
2706
- /// [`pay_for_offer`]: crate::ln::channelmanager::ChannelManager ::pay_for_offer
2659
+ /// [`pay_for_offer`]: crate::offers::flow::OffersMessageFlow ::pay_for_offer
2707
2660
payment_id: PaymentId,
2708
2661
/// Hash of the payment that is currently being sent but has yet to be fulfilled or
2709
2662
/// abandoned.
@@ -2720,7 +2673,7 @@ pub enum RecentPaymentDetails {
2720
2673
/// identify a payment and ensure idempotency in LDK.
2721
2674
///
2722
2675
/// [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
2723
- /// [`pay_for_offer`]: crate::ln::channelmanager::ChannelManager ::pay_for_offer
2676
+ /// [`pay_for_offer`]: crate::offers::flow::OffersMessageFlow ::pay_for_offer
2724
2677
payment_id: PaymentId,
2725
2678
/// Hash of the payment that was claimed. `None` for serializations of [`ChannelManager`]
2726
2679
/// made before LDK version 0.0.104.
@@ -2734,7 +2687,7 @@ pub enum RecentPaymentDetails {
2734
2687
/// identify a payment and ensure idempotency in LDK.
2735
2688
///
2736
2689
/// [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
2737
- /// [`pay_for_offer`]: crate::ln::channelmanager::ChannelManager ::pay_for_offer
2690
+ /// [`pay_for_offer`]: crate::offers::flow::OffersMessageFlow ::pay_for_offer
2738
2691
payment_id: PaymentId,
2739
2692
/// Hash of the payment that we have given up trying to send.
2740
2693
payment_hash: PaymentHash,
@@ -4608,7 +4561,7 @@ where
4608
4561
///
4609
4562
/// # Requested Invoices
4610
4563
///
4611
- /// In the case of paying a [`Bolt12Invoice`] via [`ChannelManager ::pay_for_offer`], abandoning
4564
+ /// In the case of paying a [`Bolt12Invoice`] via [`OffersMessageFlow ::pay_for_offer`], abandoning
4612
4565
/// the payment prior to receiving the invoice will result in an [`Event::PaymentFailed`] and
4613
4566
/// prevent any attempts at paying it once received.
4614
4567
///
@@ -4618,6 +4571,7 @@ where
4618
4571
/// [`ChannelManager`], another [`Event::PaymentFailed`] may be generated.
4619
4572
///
4620
4573
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
4574
+ /// [`OffersMessageFlow::pay_for_offer`]: crate::offers::flow::OffersMessageFlow::pay_for_offer
4621
4575
pub fn abandon_payment(&self, payment_id: PaymentId) {
4622
4576
self.abandon_payment_with_reason(payment_id, PaymentFailureReason::UserAbandoned)
4623
4577
}
@@ -9346,6 +9300,13 @@ pub trait OffersMessageCommons {
9346
9300
///
9347
9301
/// [`Event::PaymentSent`]: events::Event::PaymentSent
9348
9302
fn list_recent_payments(&self) -> Vec<RecentPaymentDetails>;
9303
+
9304
+ /// Internal pay_for_offer
9305
+ fn pay_for_offer_intern<CPP: FnOnce(&InvoiceRequest, Nonce) -> Result<(), Bolt12SemanticError>> (
9306
+ &self, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
9307
+ payer_note: Option<String>, payment_id: PaymentId,
9308
+ human_readable_name: Option<HumanReadableName>, create_pending_payment: CPP,
9309
+ ) -> Result<(), Bolt12SemanticError>;
9349
9310
}
9350
9311
9351
9312
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref> OffersMessageCommons for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
@@ -9519,7 +9480,6 @@ where
9519
9480
}
9520
9481
9521
9482
fn add_new_awaiting_invoice(&self, payment_id: PaymentId, expiration: StaleExpiration, retry_strategy: Retry, max_total_routing_fee_msat: Option<u64>, retryable_invoice_request: Option<RetryableInvoiceRequest>) -> Result<(), ()> {
9522
- let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9523
9483
self.pending_outbound_payments.add_new_awaiting_invoice (
9524
9484
payment_id, expiration, retry_strategy, max_total_routing_fee_msat, retryable_invoice_request,
9525
9485
)
@@ -9555,6 +9515,53 @@ where
9555
9515
})
9556
9516
.collect()
9557
9517
}
9518
+
9519
+ fn pay_for_offer_intern<CPP: FnOnce(&InvoiceRequest, Nonce) -> Result<(), Bolt12SemanticError>>(
9520
+ &self, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
9521
+ payer_note: Option<String>, payment_id: PaymentId,
9522
+ human_readable_name: Option<HumanReadableName>, create_pending_payment: CPP,
9523
+ ) -> Result<(), Bolt12SemanticError> {
9524
+ let expanded_key = &self.inbound_payment_key;
9525
+ let entropy = &*self.entropy_source;
9526
+ let secp_ctx = &self.secp_ctx;
9527
+
9528
+ let nonce = Nonce::from_entropy_source(entropy);
9529
+ let builder: InvoiceRequestBuilder<secp256k1::All> = offer
9530
+ .request_invoice(expanded_key, nonce, secp_ctx, payment_id)?
9531
+ .into();
9532
+ let builder = builder.chain_hash(self.chain_hash)?;
9533
+
9534
+ let builder = match quantity {
9535
+ None => builder,
9536
+ Some(quantity) => builder.quantity(quantity)?,
9537
+ };
9538
+ let builder = match amount_msats {
9539
+ None => builder,
9540
+ Some(amount_msats) => builder.amount_msats(amount_msats)?,
9541
+ };
9542
+ let builder = match payer_note {
9543
+ None => builder,
9544
+ Some(payer_note) => builder.payer_note(payer_note),
9545
+ };
9546
+ let builder = match human_readable_name {
9547
+ None => builder,
9548
+ Some(hrn) => builder.sourced_from_human_readable_name(hrn),
9549
+ };
9550
+ let invoice_request = builder.build_and_sign()?;
9551
+
9552
+ let hmac = payment_id.hmac_for_offer_payment(nonce, expanded_key);
9553
+ let context = MessageContext::Offers(
9554
+ OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }
9555
+ );
9556
+ let reply_paths = self.create_blinded_paths(context)
9557
+ .map_err(|_| Bolt12SemanticError::MissingPaths)?;
9558
+
9559
+ let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9560
+
9561
+ create_pending_payment(&invoice_request, nonce)?;
9562
+
9563
+ self.enqueue_invoice_request(invoice_request, reply_paths)
9564
+ }
9558
9565
}
9559
9566
9560
9567
/// Defines the maximum number of [`OffersMessage`] including different reply paths to be sent
@@ -9649,53 +9656,6 @@ where
9649
9656
})
9650
9657
}
9651
9658
9652
- fn pay_for_offer_intern<CPP: FnOnce(&InvoiceRequest, Nonce) -> Result<(), Bolt12SemanticError>>(
9653
- &self, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
9654
- payer_note: Option<String>, payment_id: PaymentId,
9655
- human_readable_name: Option<HumanReadableName>, create_pending_payment: CPP,
9656
- ) -> Result<(), Bolt12SemanticError> {
9657
- let expanded_key = &self.inbound_payment_key;
9658
- let entropy = &*self.entropy_source;
9659
- let secp_ctx = &self.secp_ctx;
9660
-
9661
- let nonce = Nonce::from_entropy_source(entropy);
9662
- let builder: InvoiceRequestBuilder<secp256k1::All> = offer
9663
- .request_invoice(expanded_key, nonce, secp_ctx, payment_id)?
9664
- .into();
9665
- let builder = builder.chain_hash(self.chain_hash)?;
9666
-
9667
- let builder = match quantity {
9668
- None => builder,
9669
- Some(quantity) => builder.quantity(quantity)?,
9670
- };
9671
- let builder = match amount_msats {
9672
- None => builder,
9673
- Some(amount_msats) => builder.amount_msats(amount_msats)?,
9674
- };
9675
- let builder = match payer_note {
9676
- None => builder,
9677
- Some(payer_note) => builder.payer_note(payer_note),
9678
- };
9679
- let builder = match human_readable_name {
9680
- None => builder,
9681
- Some(hrn) => builder.sourced_from_human_readable_name(hrn),
9682
- };
9683
- let invoice_request = builder.build_and_sign()?;
9684
-
9685
- let hmac = payment_id.hmac_for_offer_payment(nonce, expanded_key);
9686
- let context = MessageContext::Offers(
9687
- OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }
9688
- );
9689
- let reply_paths = self.create_blinded_paths(context)
9690
- .map_err(|_| Bolt12SemanticError::MissingPaths)?;
9691
-
9692
- let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9693
-
9694
- create_pending_payment(&invoice_request, nonce)?;
9695
-
9696
- self.enqueue_invoice_request(invoice_request, reply_paths)
9697
- }
9698
-
9699
9659
/// Creates a [`Bolt12Invoice`] for a [`Refund`] and enqueues it to be sent via an onion
9700
9660
/// message.
9701
9661
///
@@ -12240,6 +12200,8 @@ where
12240
12200
pub router: R,
12241
12201
/// The [`MessageRouter`] used for constructing [`BlindedMessagePath`]s for [`Offer`]s,
12242
12202
/// [`Refund`]s, and any reply paths.
12203
+ ///
12204
+ /// [`Offer`]: crate::offers::offer
12243
12205
pub message_router: MR,
12244
12206
/// The Logger for use in the ChannelManager and which may be used to log information during
12245
12207
/// deserialization.
0 commit comments