@@ -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, ChannelPhase, ChannelError, ChannelUpdateStatus, ShutdownResult, UpdateFulfillCommitFetch, OutboundV1Channel, InboundV1Channel, WithChannelContext, InboundV2Channel, InteractivelyFunded as _};
@@ -68,14 +67,13 @@ 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
#[cfg(async_payments)]
76
75
use crate::offers::static_invoice::StaticInvoice;
77
76
use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler};
78
- use crate::onion_message::dns_resolution::HumanReadableName;
79
77
use crate::onion_message::messenger::{DefaultMessageRouter, Destination, MessageRouter, MessageSendInstructions, Responder, ResponseInstruction};
80
78
use crate::onion_message::offers::OffersMessage;
81
79
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
@@ -443,11 +441,15 @@ impl Ord for ClaimableHTLC {
443
441
pub trait Verification {
444
442
/// Constructs an HMAC to include in [`OffersContext`] for the data along with the given
445
443
/// [`Nonce`].
444
+ ///
445
+ /// [`OffersContext`]: crate::blinded_path::message::OffersContext
446
446
fn hmac_for_offer_payment(
447
447
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
448
448
) -> Hmac<Sha256>;
449
449
450
450
/// Authenticates the data using an HMAC and a [`Nonce`] taken from an [`OffersContext`].
451
+ ///
452
+ /// [`OffersContext`]: crate::blinded_path::message::OffersContext
451
453
fn verify_for_offer_payment(
452
454
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
453
455
) -> Result<(), ()>;
@@ -456,6 +458,8 @@ pub trait Verification {
456
458
impl Verification for PaymentHash {
457
459
/// Constructs an HMAC to include in [`OffersContext::InboundPayment`] for the payment hash
458
460
/// along with the given [`Nonce`].
461
+ ///
462
+ /// [`OffersContext::InboundPayment`]: crate::blinded_path::message::OffersContext::InboundPayment
459
463
fn hmac_for_offer_payment(
460
464
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
461
465
) -> Hmac<Sha256> {
@@ -464,6 +468,8 @@ impl Verification for PaymentHash {
464
468
465
469
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
466
470
/// [`OffersContext::InboundPayment`].
471
+ ///
472
+ /// [`OffersContext::InboundPayment`]: crate::blinded_path::message::OffersContext::InboundPayment
467
473
fn verify_for_offer_payment(
468
474
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
469
475
) -> Result<(), ()> {
@@ -518,6 +524,8 @@ impl PaymentId {
518
524
impl Verification for PaymentId {
519
525
/// Constructs an HMAC to include in [`OffersContext::OutboundPayment`] for the payment id
520
526
/// along with the given [`Nonce`].
527
+ ///
528
+ /// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
521
529
fn hmac_for_offer_payment(
522
530
&self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
523
531
) -> Hmac<Sha256> {
@@ -526,6 +534,8 @@ impl Verification for PaymentId {
526
534
527
535
/// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
528
536
/// [`OffersContext::OutboundPayment`].
537
+ ///
538
+ /// [`OffersContext::OutboundPayment`]: crate::blinded_path::message::OffersContext::OutboundPayment
529
539
fn verify_for_offer_payment(
530
540
&self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
531
541
) -> Result<(), ()> {
@@ -2612,26 +2622,6 @@ const MAX_UNFUNDED_CHANNEL_PEERS: usize = 50;
2612
2622
/// many peers we reject new (inbound) connections.
2613
2623
const MAX_NO_CHANNEL_PEERS: usize = 250;
2614
2624
2615
- /// The maximum expiration from the current time where an [`Offer`] or [`Refund`] is considered
2616
- /// short-lived, while anything with a greater expiration is considered long-lived.
2617
- ///
2618
- /// Using [`OffersMessageFlow::create_offer_builder`] or [`OffersMessageFlow::create_refund_builder`],
2619
- /// will included a [`BlindedMessagePath`] created using:
2620
- /// - [`MessageRouter::create_compact_blinded_paths`] when short-lived, and
2621
- /// - [`MessageRouter::create_blinded_paths`] when long-lived.
2622
- ///
2623
- /// [`OffersMessageFlow::create_offer_builder`]: crate::offers::flow::OffersMessageFlow::create_offer_builder
2624
- /// [`OffersMessageFlow::create_refund_builder`]: crate::offers::flow::OffersMessageFlow::create_refund_builder
2625
- ///
2626
- ///
2627
- /// Using compact [`BlindedMessagePath`]s may provide better privacy as the [`MessageRouter`] could select
2628
- /// more hops. However, since they use short channel ids instead of pubkeys, they are more likely to
2629
- /// become invalid over time as channels are closed. Thus, they are only suitable for short-term use.
2630
- ///
2631
- /// [`Offer`]: crate::offers::offer
2632
- /// [`Refund`]: crate::offers::refund
2633
- pub const MAX_SHORT_LIVED_RELATIVE_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24);
2634
-
2635
2625
/// Used by [`ChannelManager::list_recent_payments`] to express the status of recent payments.
2636
2626
/// These include payments that have yet to find a successful path, or have unresolved HTLCs.
2637
2627
#[derive(Debug, PartialEq)]
@@ -9640,6 +9630,23 @@ where
9640
9630
self.create_blinded_payment_paths(amount_msats, payment_secret, payment_context)
9641
9631
}
9642
9632
9633
+ fn get_peer_for_blinded_path(&self) -> Vec<MessageForwardNode> {
9634
+ self.per_peer_state.read().unwrap()
9635
+ .iter()
9636
+ .map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
9637
+ .filter(|(_, peer)| peer.is_connected)
9638
+ .filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9639
+ .map(|(node_id, peer)| MessageForwardNode {
9640
+ node_id: *node_id,
9641
+ short_channel_id: peer.channel_by_id
9642
+ .iter()
9643
+ .filter(|(_, channel)| channel.context().is_usable())
9644
+ .min_by_key(|(_, channel)| channel.context().channel_creation_height)
9645
+ .and_then(|(_, channel)| channel.context().get_short_channel_id()),
9646
+ })
9647
+ .collect::<Vec<_>>()
9648
+ }
9649
+
9643
9650
fn send_payment_for_verified_bolt12_invoice(&self, invoice: &Bolt12Invoice, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
9644
9651
self.send_payment_for_verified_bolt12_invoice(invoice, payment_id)
9645
9652
}
@@ -9671,12 +9678,6 @@ where
9671
9678
self.initiate_async_payment(invoice, payment_id)
9672
9679
}
9673
9680
9674
- fn create_blinded_paths_using_absolute_expiry(
9675
- &self, context: OffersContext, absolute_expiry: Option<Duration>,
9676
- ) -> Result<Vec<BlindedMessagePath>, ()> {
9677
- self.create_blinded_paths_using_absolute_expiry(context, absolute_expiry)
9678
- }
9679
-
9680
9681
fn get_chain_hash(&self) -> ChainHash {
9681
9682
self.chain_hash
9682
9683
}
@@ -9695,14 +9696,6 @@ where
9695
9696
self.pending_outbound_payments.add_new_awaiting_offer(payment_id, expiration, retry_strategy, max_total_routing_fee_msat, amount_msats)
9696
9697
}
9697
9698
9698
- fn pay_for_offer_intern<CPP: FnOnce(&InvoiceRequest, Nonce) -> Result<(), Bolt12SemanticError>>(
9699
- &self, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
9700
- payer_note: Option<String>, payment_id: PaymentId,
9701
- human_readable_name: Option<HumanReadableName>, create_pending_payment: CPP,
9702
- ) -> Result<(), Bolt12SemanticError> {
9703
- self.pay_for_offer_intern(offer, quantity, amount_msats, payer_note, payment_id, human_readable_name, create_pending_payment)
9704
- }
9705
-
9706
9699
#[cfg(feature = "dnssec")]
9707
9700
fn amt_msats_for_payment_awaiting_offer(&self, payment_id: PaymentId) -> Result<u64, ()> {
9708
9701
self.pending_outbound_payments.amt_msats_for_payment_awaiting_offer(payment_id)
@@ -9713,6 +9706,11 @@ where
9713
9706
) -> Result<(), ()> {
9714
9707
self.pending_outbound_payments.received_offer(payment_id, retryable_invoice_request)
9715
9708
}
9709
+
9710
+ #[cfg(not(feature = "std"))]
9711
+ fn get_highest_seen_timestamp(&self) -> Duration {
9712
+ Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64)
9713
+ }
9716
9714
}
9717
9715
9718
9716
/// Defines the maximum number of [`OffersMessage`] including different reply paths to be sent
@@ -9734,54 +9732,6 @@ where
9734
9732
MR::Target: MessageRouter,
9735
9733
L::Target: Logger,
9736
9734
{
9737
- /// Internal pay_for_offer
9738
- pub fn pay_for_offer_intern<CPP: FnOnce(&InvoiceRequest, Nonce) -> Result<(), Bolt12SemanticError>>(
9739
- &self, offer: &Offer, quantity: Option<u64>, amount_msats: Option<u64>,
9740
- payer_note: Option<String>, payment_id: PaymentId,
9741
- human_readable_name: Option<HumanReadableName>, create_pending_payment: CPP,
9742
- ) -> Result<(), Bolt12SemanticError> {
9743
- let expanded_key = &self.inbound_payment_key;
9744
- let entropy = &*self.entropy_source;
9745
- let secp_ctx = &self.secp_ctx;
9746
-
9747
- let nonce = Nonce::from_entropy_source(entropy);
9748
- let builder: InvoiceRequestBuilder<secp256k1::All> = offer
9749
- .request_invoice(expanded_key, nonce, secp_ctx, payment_id)?
9750
- .into();
9751
- let builder = builder.chain_hash(self.chain_hash)?;
9752
-
9753
- let builder = match quantity {
9754
- None => builder,
9755
- Some(quantity) => builder.quantity(quantity)?,
9756
- };
9757
- let builder = match amount_msats {
9758
- None => builder,
9759
- Some(amount_msats) => builder.amount_msats(amount_msats)?,
9760
- };
9761
- let builder = match payer_note {
9762
- None => builder,
9763
- Some(payer_note) => builder.payer_note(payer_note),
9764
- };
9765
- let builder = match human_readable_name {
9766
- None => builder,
9767
- Some(hrn) => builder.sourced_from_human_readable_name(hrn),
9768
- };
9769
- let invoice_request = builder.build_and_sign()?;
9770
-
9771
- let hmac = payment_id.hmac_for_offer_payment(nonce, expanded_key);
9772
- let context = MessageContext::Offers(
9773
- OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }
9774
- );
9775
- let reply_paths = self.create_blinded_paths(context)
9776
- .map_err(|_| Bolt12SemanticError::MissingPaths)?;
9777
-
9778
- let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
9779
-
9780
- create_pending_payment(&invoice_request, nonce)?;
9781
-
9782
- self.enqueue_invoice_request(invoice_request, reply_paths)
9783
- }
9784
-
9785
9735
fn enqueue_invoice_request(
9786
9736
&self,
9787
9737
invoice_request: InvoiceRequest,
@@ -9915,86 +9865,25 @@ where
9915
9865
inbound_payment::get_payment_preimage(payment_hash, payment_secret, &self.inbound_payment_key)
9916
9866
}
9917
9867
9918
- /// Creates a collection of blinded paths by delegating to [`MessageRouter`] based on
9919
- /// the path's intended lifetime.
9920
- ///
9921
- /// Whether or not the path is compact depends on whether the path is short-lived or long-lived,
9922
- /// respectively, based on the given `absolute_expiry` as seconds since the Unix epoch. See
9923
- /// [`MAX_SHORT_LIVED_RELATIVE_EXPIRY`].
9924
- pub fn create_blinded_paths_using_absolute_expiry(
9925
- &self, context: OffersContext, absolute_expiry: Option<Duration>,
9926
- ) -> Result<Vec<BlindedMessagePath>, ()> {
9927
- let now = self.duration_since_epoch();
9928
- let max_short_lived_absolute_expiry = now.saturating_add(MAX_SHORT_LIVED_RELATIVE_EXPIRY);
9929
- if absolute_expiry.unwrap_or(Duration::MAX) <= max_short_lived_absolute_expiry {
9930
- self.create_compact_blinded_paths(context)
9931
- } else {
9932
- self.create_blinded_paths(MessageContext::Offers(context))
9933
- }
9934
- }
9935
-
9936
- pub(super) fn duration_since_epoch(&self) -> Duration {
9937
- #[cfg(not(feature = "std"))]
9938
- let now = Duration::from_secs(
9939
- self.highest_seen_timestamp.load(Ordering::Acquire) as u64
9940
- );
9941
- #[cfg(feature = "std")]
9942
- let now = std::time::SystemTime::now()
9943
- .duration_since(std::time::SystemTime::UNIX_EPOCH)
9944
- .expect("SystemTime::now() should come after SystemTime::UNIX_EPOCH");
9945
-
9946
- now
9947
- }
9948
-
9949
9868
/// Creates a collection of blinded paths by delegating to
9950
9869
/// [`MessageRouter::create_blinded_paths`].
9951
9870
///
9952
9871
/// Errors if the `MessageRouter` errors.
9953
9872
fn create_blinded_paths(&self, context: MessageContext) -> Result<Vec<BlindedMessagePath>, ()> {
9954
9873
let recipient = self.get_our_node_id();
9955
9874
let secp_ctx = &self.secp_ctx;
9956
-
9957
9875
let peers = self.per_peer_state.read().unwrap()
9958
9876
.iter()
9959
9877
.map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
9960
9878
.filter(|(_, peer)| peer.is_connected)
9961
9879
.filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9962
9880
.map(|(node_id, _)| *node_id)
9963
9881
.collect::<Vec<_>>();
9964
-
9965
9882
self.message_router
9966
9883
.create_blinded_paths(recipient, context, peers, secp_ctx)
9967
9884
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
9968
9885
}
9969
9886
9970
- /// Creates a collection of blinded paths by delegating to
9971
- /// [`MessageRouter::create_compact_blinded_paths`].
9972
- ///
9973
- /// Errors if the `MessageRouter` errors.
9974
- fn create_compact_blinded_paths(&self, context: OffersContext) -> Result<Vec<BlindedMessagePath>, ()> {
9975
- let recipient = self.get_our_node_id();
9976
- let secp_ctx = &self.secp_ctx;
9977
-
9978
- let peers = self.per_peer_state.read().unwrap()
9979
- .iter()
9980
- .map(|(node_id, peer_state)| (node_id, peer_state.lock().unwrap()))
9981
- .filter(|(_, peer)| peer.is_connected)
9982
- .filter(|(_, peer)| peer.latest_features.supports_onion_messages())
9983
- .map(|(node_id, peer)| MessageForwardNode {
9984
- node_id: *node_id,
9985
- short_channel_id: peer.channel_by_id
9986
- .iter()
9987
- .filter(|(_, channel)| channel.context().is_usable())
9988
- .min_by_key(|(_, channel)| channel.context().channel_creation_height)
9989
- .and_then(|(_, channel)| channel.context().get_short_channel_id()),
9990
- })
9991
- .collect::<Vec<_>>();
9992
-
9993
- self.message_router
9994
- .create_compact_blinded_paths(recipient, MessageContext::Offers(context), peers, secp_ctx)
9995
- .and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
9996
- }
9997
-
9998
9887
/// Creates multi-hop blinded payment paths for the given `amount_msats` by delegating to
9999
9888
/// [`Router::create_blinded_payment_paths`].
10000
9889
fn create_blinded_payment_paths(
@@ -10003,12 +9892,10 @@ where
10003
9892
let expanded_key = &self.inbound_payment_key;
10004
9893
let entropy = &*self.entropy_source;
10005
9894
let secp_ctx = &self.secp_ctx;
10006
-
10007
9895
let first_hops = self.list_usable_channels();
10008
9896
let payee_node_id = self.get_our_node_id();
10009
9897
let max_cltv_expiry = self.best_block.read().unwrap().height + CLTV_FAR_FAR_AWAY
10010
9898
+ LATENCY_GRACE_PERIOD_BLOCKS;
10011
-
10012
9899
let payee_tlvs = UnauthenticatedReceiveTlvs {
10013
9900
payment_secret,
10014
9901
payment_constraints: PaymentConstraints {
@@ -10019,7 +9906,6 @@ where
10019
9906
};
10020
9907
let nonce = Nonce::from_entropy_source(entropy);
10021
9908
let payee_tlvs = payee_tlvs.authenticate(nonce, expanded_key);
10022
-
10023
9909
self.router.create_blinded_payment_paths(
10024
9910
payee_node_id, first_hops, payee_tlvs, amount_msats, secp_ctx
10025
9911
)
@@ -12265,6 +12151,7 @@ where
12265
12151
/// The [`MessageRouter`] used for constructing [`BlindedMessagePath`]s for [`Offer`]s,
12266
12152
/// [`Refund`]s, and any reply paths.
12267
12153
///
12154
+ /// [`BlindedMessagePath`]: crate::blinded_path::message::BlindedMessagePath
12268
12155
/// [`Offer`]: crate::offers::offer
12269
12156
/// [`Refund`]: crate::offers::refund
12270
12157
pub message_router: MR,
0 commit comments