@@ -33,9 +33,9 @@ use bitcoin::secp256k1::Secp256k1;
33
33
use bitcoin::{secp256k1, Sequence, Weight};
34
34
35
35
use crate::events::FundingInfo;
36
- use crate::blinded_path::message::{AsyncPaymentsContext, MessageContext, OffersContext };
36
+ use crate::blinded_path::message::{AsyncPaymentsContext, MessageContext, MessageForwardNode };
37
37
use crate::blinded_path::NodeIdLookUp;
38
- use crate::blinded_path::message::{ BlindedMessagePath, MessageForwardNode} ;
38
+ use crate::blinded_path::message::BlindedMessagePath;
39
39
use crate::blinded_path::payment::{BlindedPaymentPath, PaymentConstraints, PaymentContext, UnauthenticatedReceiveTlvs};
40
40
use crate::chain;
41
41
use crate::chain::{Confirm, ChannelMonitorUpdateStatus, Watch, BestBlock};
@@ -47,7 +47,6 @@ use crate::events::{self, Event, EventHandler, EventsProvider, InboundChannelFun
47
47
// construct one themselves.
48
48
use crate::ln::inbound_payment;
49
49
use crate::ln::types::ChannelId;
50
- use crate::offers::offer::Offer;
51
50
use crate::offers::flow::OffersMessageCommons;
52
51
use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};
53
52
use crate::ln::channel::{self, Channel, ChannelError, ChannelUpdateStatus, FundedChannel, ShutdownResult, UpdateFulfillCommitFetch, OutboundV1Channel, ReconnectionMsg, InboundV1Channel, WithChannelContext};
@@ -68,12 +67,11 @@ use crate::ln::outbound_payment;
68
67
use crate::ln::outbound_payment::{OutboundPayments, PendingOutboundPayment, RetryableInvoiceRequest, SendAlongPathArgs, StaleExpiration};
69
68
use crate::offers::invoice::Bolt12Invoice;
70
69
use crate::offers::invoice::UnsignedBolt12Invoice;
71
- use crate::offers::invoice_request::{ InvoiceRequest, InvoiceRequestBuilder} ;
70
+ use crate::offers::invoice_request::InvoiceRequest;
72
71
use crate::offers::nonce::Nonce;
73
72
use crate::offers::parse::Bolt12SemanticError;
74
73
use crate::offers::signer;
75
74
use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler};
76
- use crate::onion_message::dns_resolution::HumanReadableName;
77
75
use crate::onion_message::messenger::{DefaultMessageRouter, Destination, MessageRouter, MessageSendInstructions, Responder, ResponseInstruction};
78
76
use crate::onion_message::offers::OffersMessage;
79
77
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
@@ -461,11 +459,15 @@ impl Ord for ClaimableHTLC {
461
459
pub trait Verification {
462
460
/// Constructs an HMAC to include in [`OffersContext`] for the data along with the given
463
461
/// [`Nonce`].
462
+ ///
463
+ /// [`OffersContext`]: crate::blinded_path::message::OffersContext
464
464
fn hmac_for_offer_payment(
465
465
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
466
466
) -> Hmac<Sha256>;
467
467
468
468
/// Authenticates the data using an HMAC and a [`Nonce`] taken from an [`OffersContext`].
469
+ ///
470
+ /// [`OffersContext`]: crate::blinded_path::message::OffersContext
469
471
fn verify_for_offer_payment(
470
472
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
471
473
) -> Result<(), ()>;
@@ -474,6 +476,8 @@ pub trait Verification {
474
476
impl Verification for PaymentHash {
475
477
/// Constructs an HMAC to include in [`OffersContext::InboundPayment`] for the payment hash
476
478
/// along with the given [`Nonce`].
479
+ ///
480
+ /// [`OffersContext::InboundPayment`]: crate::blinded_path::message::OffersContext::InboundPayment
477
481
fn hmac_for_offer_payment(
478
482
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
479
483
) -> Hmac<Sha256> {
@@ -482,6 +486,8 @@ impl Verification for PaymentHash {
482
486
483
487
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
484
488
/// [`OffersContext::InboundPayment`].
489
+ ///
490
+ /// [`OffersContext::InboundPayment`]: crate::blinded_path::message::OffersContext::InboundPayment
485
491
fn verify_for_offer_payment(
486
492
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
487
493
) -> Result<(), ()> {
@@ -536,6 +542,8 @@ impl PaymentId {
536
542
impl Verification for PaymentId {
537
543
/// Constructs an HMAC to include in [`OffersContext::OutboundPayment`] for the payment id
538
544
/// along with the given [`Nonce`].
545
+ ///
546
+ /// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
539
547
fn hmac_for_offer_payment(
540
548
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
541
549
) -> Hmac<Sha256> {
@@ -544,6 +552,8 @@ impl Verification for PaymentId {
544
552
545
553
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
546
554
/// [`OffersContext::OutboundPayment`].
555
+ ///
556
+ /// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
547
557
fn verify_for_offer_payment(
548
558
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
549
559
) -> Result<(), ()> {
@@ -2633,26 +2643,6 @@ const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
2633
2643
/// many peers we reject new (inbound) connections.
2634
2644
const MAX_NO_CHANNEL_PEERS: usize = 250;
2635
2645
2636
- /// The maximum expiration from the current time where an [`Offer`] or [`Refund`] is considered
2637
- /// short-lived, while anything with a greater expiration is considered long-lived.
2638
- ///
2639
- /// Using [`OffersMessageFlow::create_offer_builder`] or [`OffersMessageFlow::create_refund_builder`],
2640
- /// will included a [`BlindedMessagePath`] created using:
2641
- /// - [`MessageRouter::create_compact_blinded_paths`] when short-lived, and
2642
- /// - [`MessageRouter::create_blinded_paths`] when long-lived.
2643
- ///
2644
- /// [`OffersMessageFlow::create_offer_builder`]: crate::offers::flow::OffersMessageFlow::create_offer_builder
2645
- /// [`OffersMessageFlow::create_refund_builder`]: crate::offers::flow::OffersMessageFlow::create_refund_builder
2646
- ///
2647
- ///
2648
- /// Using compact [`BlindedMessagePath`]s may provide better privacy as the [`MessageRouter`] could select
2649
- /// more hops. However, since they use short channel ids instead of pubkeys, they are more likely to
2650
- /// become invalid over time as channels are closed. Thus, they are only suitable for short-term use.
2651
- ///
2652
- /// [`Offer`]: crate::offers::offer
2653
- /// [`Refund`]: crate::offers::refund
2654
- pub const MAX_SHORT_LIVED_RELATIVE_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24);
2655
-
2656
2646
/// Used by [`ChannelManager::list_recent_payments`] to express the status of recent payments.
2657
2647
/// These include payments that have yet to find a successful path, or have unresolved HTLCs.
2658
2648
#[derive(Debug, PartialEq)]
@@ -9515,6 +9505,23 @@ where
9515
9505
self.create_blinded_payment_paths(amount_msats, payment_secret, payment_context, relative_expiry_time)
9516
9506
}
9517
9507
9508
+ fn get_peer_for_blinded_path(&self) -> Vec<MessageForwardNode> {
9509
+ self.per_peer_state.read().unwrap()
9510
+ .iter()
9511
+ .map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
9512
+ .filter(|(_, peer)| peer.is_connected)
9513
+ .filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9514
+ .map(|(node_id, peer)| MessageForwardNode {
9515
+ node_id: *node_id,
9516
+ short_channel_id: peer.channel_by_id
9517
+ .iter()
9518
+ .filter(|(_, channel)| channel.context().is_usable())
9519
+ .min_by_key(|(_, channel)| channel.context().channel_creation_height)
9520
+ .and_then(|(_, channel)| channel.context().get_short_channel_id()),
9521
+ })
9522
+ .collect::<Vec<_>>()
9523
+ }
9524
+
9518
9525
fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
9519
9526
self.send_payment_for_verified_bolt12_invoice(invoice, payment_id)
9520
9527
}
@@ -9546,12 +9553,6 @@ where
9546
9553
self.initiate_async_payment(invoice, payment_id)
9547
9554
}
9548
9555
9549
- fn create_blinded_paths_using_absolute_expiry(
9550
- &self, context: OffersContext, absolute_expiry: Option<Duration>,
9551
- ) -> Result<Vec<BlindedMessagePath>, ()> {
9552
- self.create_blinded_paths_using_absolute_expiry(context, absolute_expiry)
9553
- }
9554
-
9555
9556
fn get_chain_hash(&self) -> ChainHash {
9556
9557
self.chain_hash
9557
9558
}
@@ -9575,14 +9576,6 @@ where
9575
9576
self.pending_outbound_payments.add_new_awaiting_offer(payment_id, expiration, retry_strategy, max_total_routing_fee_msat, amount_msats)
9576
9577
}
9577
9578
9578
- fn pay_for_offer_intern<CPP: FnOnce(&InvoiceRequest, Nonce) -> Result<(), Bolt12SemanticError>>(
9579
- &self, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
9580
- payer_note: Option<String>, payment_id: PaymentId,
9581
- human_readable_name: Option<HumanReadableName>, create_pending_payment: CPP,
9582
- ) -> Result<(), Bolt12SemanticError> {
9583
- self.pay_for_offer_intern(offer, quantity, amount_msats, payer_note, payment_id, human_readable_name, create_pending_payment)
9584
- }
9585
-
9586
9579
#[cfg(feature = "dnssec")]
9587
9580
fn amt_msats_for_payment_awaiting_offer(&self, payment_id: PaymentId) -> Result<u64, ()> {
9588
9581
self.pending_outbound_payments.amt_msats_for_payment_awaiting_offer(payment_id)
@@ -9614,54 +9607,6 @@ where
9614
9607
MR::Target: MessageRouter,
9615
9608
L::Target: Logger,
9616
9609
{
9617
- /// Internal pay_for_offer
9618
- pub fn pay_for_offer_intern<CPP: FnOnce(&InvoiceRequest, Nonce) -> Result<(), Bolt12SemanticError>>(
9619
- &self, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
9620
- payer_note: Option<String>, payment_id: PaymentId,
9621
- human_readable_name: Option<HumanReadableName>, create_pending_payment: CPP,
9622
- ) -> Result<(), Bolt12SemanticError> {
9623
- let expanded_key = &self.inbound_payment_key;
9624
- let entropy = &*self.entropy_source;
9625
- let secp_ctx = &self.secp_ctx;
9626
-
9627
- let nonce = Nonce::from_entropy_source(entropy);
9628
- let builder: InvoiceRequestBuilder<secp256k1::All> = offer
9629
- .request_invoice(expanded_key, nonce, secp_ctx, payment_id)?
9630
- .into();
9631
- let builder = builder.chain_hash(self.chain_hash)?;
9632
-
9633
- let builder = match quantity {
9634
- None => builder,
9635
- Some(quantity) => builder.quantity(quantity)?,
9636
- };
9637
- let builder = match amount_msats {
9638
- None => builder,
9639
- Some(amount_msats) => builder.amount_msats(amount_msats)?,
9640
- };
9641
- let builder = match payer_note {
9642
- None => builder,
9643
- Some(payer_note) => builder.payer_note(payer_note),
9644
- };
9645
- let builder = match human_readable_name {
9646
- None => builder,
9647
- Some(hrn) => builder.sourced_from_human_readable_name(hrn),
9648
- };
9649
- let invoice_request = builder.build_and_sign()?;
9650
-
9651
- let hmac = payment_id.hmac_for_offer_payment(nonce, expanded_key);
9652
- let context = MessageContext::Offers(
9653
- OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }
9654
- );
9655
- let reply_paths = self.create_blinded_paths(context)
9656
- .map_err(|_| Bolt12SemanticError::MissingPaths)?;
9657
-
9658
- let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9659
-
9660
- create_pending_payment(&invoice_request, nonce)?;
9661
-
9662
- self.enqueue_invoice_request(invoice_request, reply_paths)
9663
- }
9664
-
9665
9610
fn enqueue_invoice_request(
9666
9611
&self,
9667
9612
invoice_request: InvoiceRequest,
@@ -9697,6 +9642,19 @@ where
9697
9642
Ok(())
9698
9643
}
9699
9644
9645
+ #[cfg(async_payments)]
9646
+ pub(super) fn duration_since_epoch(&self) -> Duration {
9647
+ #[cfg(not(feature = "std"))]
9648
+ let now = Duration::from_secs(
9649
+ self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9650
+ );
9651
+ #[cfg(feature = "std")]
9652
+ let now = std::time::SystemTime::now()
9653
+ .duration_since(std::time::SystemTime::UNIX_EPOCH)
9654
+ .expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
9655
+ now
9656
+ }
9657
+
9700
9658
/// Gets a payment secret and payment hash for use in an invoice given to a third party wishing
9701
9659
/// to pay us.
9702
9660
///
@@ -9795,86 +9753,25 @@ where
9795
9753
inbound_payment::get_payment_preimage(payment_hash, payment_secret, &self.inbound_payment_key)
9796
9754
}
9797
9755
9798
- /// Creates a collection of blinded paths by delegating to [`MessageRouter`] based on
9799
- /// the path's intended lifetime.
9800
- ///
9801
- /// Whether or not the path is compact depends on whether the path is short-lived or long-lived,
9802
- /// respectively, based on the given `absolute_expiry` as seconds since the Unix epoch. See
9803
- /// [`MAX_SHORT_LIVED_RELATIVE_EXPIRY`].
9804
- pub fn create_blinded_paths_using_absolute_expiry(
9805
- &self, context: OffersContext, absolute_expiry: Option<Duration>,
9806
- ) -> Result<Vec<BlindedMessagePath>, ()> {
9807
- let now = self.duration_since_epoch();
9808
- let max_short_lived_absolute_expiry = now.saturating_add(MAX_SHORT_LIVED_RELATIVE_EXPIRY);
9809
- if absolute_expiry.unwrap_or(Duration::MAX) <= max_short_lived_absolute_expiry {
9810
- self.create_compact_blinded_paths(context)
9811
- } else {
9812
- self.create_blinded_paths(MessageContext::Offers(context))
9813
- }
9814
- }
9815
-
9816
- pub(super) fn duration_since_epoch(&self) -> Duration {
9817
- #[cfg(not(feature = "std"))]
9818
- let now = Duration::from_secs(
9819
- self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9820
- );
9821
- #[cfg(feature = "std")]
9822
- let now = std::time::SystemTime::now()
9823
- .duration_since(std::time::SystemTime::UNIX_EPOCH)
9824
- .expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
9825
-
9826
- now
9827
- }
9828
-
9829
9756
/// Creates a collection of blinded paths by delegating to
9830
9757
/// [`MessageRouter::create_blinded_paths`].
9831
9758
///
9832
9759
/// Errors if the `MessageRouter` errors.
9833
9760
fn create_blinded_paths(&self, context: MessageContext) -> Result<Vec<BlindedMessagePath>, ()> {
9834
9761
let recipient = self.get_our_node_id();
9835
9762
let secp_ctx = &self.secp_ctx;
9836
-
9837
9763
let peers = self.per_peer_state.read().unwrap()
9838
9764
.iter()
9839
9765
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
9840
9766
.filter(|(_, peer)| peer.is_connected)
9841
9767
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9842
9768
.map(|(node_id, _)| *node_id)
9843
9769
.collect::<Vec<_>>();
9844
-
9845
9770
self.message_router
9846
9771
.create_blinded_paths(recipient, context, peers, secp_ctx)
9847
9772
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
9848
9773
}
9849
9774
9850
- /// Creates a collection of blinded paths by delegating to
9851
- /// [`MessageRouter::create_compact_blinded_paths`].
9852
- ///
9853
- /// Errors if the `MessageRouter` errors.
9854
- fn create_compact_blinded_paths(&self, context: OffersContext) -> Result<Vec<BlindedMessagePath>, ()> {
9855
- let recipient = self.get_our_node_id();
9856
- let secp_ctx = &self.secp_ctx;
9857
-
9858
- let peers = self.per_peer_state.read().unwrap()
9859
- .iter()
9860
- .map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
9861
- .filter(|(_, peer)| peer.is_connected)
9862
- .filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9863
- .map(|(node_id, peer)| MessageForwardNode {
9864
- node_id: *node_id,
9865
- short_channel_id: peer.channel_by_id
9866
- .iter()
9867
- .filter(|(_, channel)| channel.context().is_usable())
9868
- .min_by_key(|(_, channel)| channel.context().channel_creation_height)
9869
- .and_then(|(_, channel)| channel.context().get_short_channel_id()),
9870
- })
9871
- .collect::<Vec<_>>();
9872
-
9873
- self.message_router
9874
- .create_compact_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx)
9875
- .and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
9876
- }
9877
-
9878
9775
/// Creates multi-hop blinded payment paths for the given `amount_msats` by delegating to
9879
9776
/// [`Router::create_blinded_payment_paths`].
9880
9777
fn create_blinded_payment_paths(
@@ -9884,7 +9781,6 @@ where
9884
9781
let expanded_key = &self.inbound_payment_key;
9885
9782
let entropy = &*self.entropy_source;
9886
9783
let secp_ctx = &self.secp_ctx;
9887
-
9888
9784
let first_hops = self.list_usable_channels();
9889
9785
let payee_node_id = self.get_our_node_id();
9890
9786
@@ -9905,7 +9801,6 @@ where
9905
9801
};
9906
9802
let nonce = Nonce::from_entropy_source(entropy);
9907
9803
let payee_tlvs = payee_tlvs.authenticate(nonce, expanded_key);
9908
-
9909
9804
self.router.create_blinded_payment_paths(
9910
9805
payee_node_id, first_hops, payee_tlvs, amount_msats, secp_ctx
9911
9806
)
@@ -12161,6 +12056,7 @@ where
12161
12056
/// The [`MessageRouter`] used for constructing [`BlindedMessagePath`]s for [`Offer`]s,
12162
12057
/// [`Refund`]s, and any reply paths.
12163
12058
///
12059
+ /// [`BlindedMessagePath`]: crate::blinded_path::message::BlindedMessagePath
12164
12060
/// [`Offer`]: crate::offers::offer
12165
12061
/// [`Refund`]: crate::offers::refund
12166
12062
pub message_router: MR,
0 commit comments