Skip to content

Commit e9e25de

Browse files
Util for blinded paths to configure an async recipient
As part of serving static invoices to payers on behalf of often-offline recipients, these recipients need a way to contact the static invoice server to retrieve blinded paths to include in their offers. Add a utility to create blinded paths for this purpose as a static invoice server. The recipient will be configured with the resulting paths and use them to request offer paths on startup.
1 parent 75eb615 commit e9e25de

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

lightning/src/blinded_path/message.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,34 @@ pub enum OffersContext {
406406
/// [`AsyncPaymentsMessage`]: crate::onion_message::async_payments::AsyncPaymentsMessage
407407
#[derive(Clone, Debug)]
408408
pub enum AsyncPaymentsContext {
409+
/// Context used by a [`BlindedMessagePath`] that an async recipient is configured with in
410+
/// [`UserConfig::paths_to_static_invoice_server`], provided back to us in corresponding
411+
/// [`OfferPathsRequest`]s.
412+
///
413+
/// [`UserConfig::paths_to_static_invoice_server`]: crate::util::config::UserConfig::paths_to_static_invoice_server
414+
/// [`OfferPathsRequest`]: crate::onion_message::async_payments::OfferPathsRequest
415+
OfferPathsRequest {
416+
/// An identifier for the async recipient that is requesting blinded paths to include in their
417+
/// [`Offer::paths`]. This ID is intended to be included in the reply path to our [`OfferPaths`]
418+
/// response, and subsequently rate limit [`ServeStaticInvoice`] messages from recipients.
419+
///
420+
/// [`Offer::paths`]: crate::offers::offer::Offer::paths
421+
/// [`OfferPaths`]: crate::onion_message::async_payments::OfferPaths
422+
/// [`ServeStaticInvoice`]: crate::onion_message::async_payments::ServeStaticInvoice
423+
recipient_id_nonce: Nonce,
424+
/// Authentication code for the [`OfferPathsRequest`].
425+
///
426+
/// Prevents nodes from requesting offer paths from us without having been previously configured
427+
/// with a [`BlindedMessagePath`] that we generated.
428+
///
429+
/// [`OfferPathsRequest`]: crate::onion_message::async_payments::OfferPathsRequest
430+
hmac: Hmac<Sha256>,
431+
/// The time as duration since the Unix epoch at which this path expires and messages sent over
432+
/// it should be ignored.
433+
///
434+
/// Useful to timeout async recipients that are no longer supported as clients.
435+
path_absolute_expiry: core::time::Duration,
436+
},
409437
/// Context used by a reply path to an [`OfferPathsRequest`], provided back to us in corresponding
410438
/// [`OfferPaths`] messages.
411439
///
@@ -581,6 +609,11 @@ impl_writeable_tlv_based_enum!(AsyncPaymentsContext,
581609
(12, hmac, required),
582610
(14, path_absolute_expiry, required),
583611
},
612+
(4, OfferPathsRequest) => {
613+
(0, recipient_id_nonce, required),
614+
(2, hmac, required),
615+
(4, path_absolute_expiry, required),
616+
},
584617
);
585618

586619
/// Contains a simple nonce for use in a blinded path's context.

lightning/src/ln/channelmanager.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10725,6 +10725,29 @@ where
1072510725
}
1072610726

1072710727
#[cfg(any(test, async_payments))]
10728+
/// [`BlindedMessagePath`]s that an async recipient will be configured with via
10729+
/// [`UserConfig::paths_to_static_invoice_server`], enabling the recipient to request blinded
10730+
/// paths from us for inclusion in their [`Offer::paths`].
10731+
///
10732+
/// If `relative_expiry` is unset, the [`BlindedMessagePath`]s expiry will default to
10733+
/// [`DEFAULT_CONFIG_PATH_RELATIVE_EXPIRY`].
10734+
///
10735+
/// Returns the paths to be included in the recipient's
10736+
/// [`UserConfig::paths_to_static_invoice_server`] as well as a nonce that uniquely identifies the
10737+
/// recipient that has been configured with these paths. // TODO link to events that surface this nonce
10738+
///
10739+
/// [`UserConfig::paths_to_static_invoice_server`]: crate::util::config::UserConfig::paths_to_static_invoice_server
10740+
/// [`Offer::paths`]: crate::offers::offer::Offer::paths
10741+
/// [`DEFAULT_CONFIG_PATH_RELATIVE_EXPIRY`]: crate::onion_message::async_payments::DEFAULT_CONFIG_PATH_RELATIVE_EXPIRY
10742+
#[cfg(async_payments)]
10743+
pub fn blinded_paths_for_async_recipient(
10744+
&self, relative_expiry: Option<Duration>
10745+
) -> Result<(Vec<BlindedMessagePath>, Nonce), ()> {
10746+
let peers = self.get_peers_for_blinded_path();
10747+
let entropy = &*self.entropy_source;
10748+
self.flow.blinded_paths_for_async_recipient(peers, relative_expiry, entropy)
10749+
}
10750+
1072810751
pub(super) fn duration_since_epoch(&self) -> Duration {
1072910752
#[cfg(not(feature = "std"))]
1073010753
let now = Duration::from_secs(

lightning/src/offers/flow.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,43 @@ impl<MR: Deref> OffersMessageFlow<MR>
229229
where
230230
MR::Target: MessageRouter,
231231
{
232+
/// [`BlindedMessagePath`]s that an async recipient will be configured with via
233+
/// [`UserConfig::paths_to_static_invoice_server`], enabling the recipient to request blinded
234+
/// paths from us for inclusion in their [`Offer::paths`].
235+
///
236+
/// If `relative_expiry` is unset, the resulting [`BlindedMessagePath`]s will not expire.
237+
///
238+
/// Returns the paths to be included in the recipient's
239+
/// [`UserConfig::paths_to_static_invoice_server`] as well as a nonce that uniquely identifies the
240+
/// recipient that has been configured with these paths.
241+
///
242+
/// [`UserConfig::paths_to_static_invoice_server`]: crate::util::config::UserConfig::paths_to_static_invoice_server
243+
/// [`Offer::paths`]: crate::offers::offer::Offer::paths
244+
/// [`DEFAULT_CONFIG_PATH_RELATIVE_EXPIRY`]: crate::onion_message::async_payments::DEFAULT_CONFIG_PATH_RELATIVE_EXPIRY
245+
#[cfg(async_payments)]
246+
pub fn blinded_paths_for_async_recipient<ES: Deref>(
247+
&self, peers: Vec<MessageForwardNode>, relative_expiry: Option<Duration>, entropy: ES,
248+
) -> Result<(Vec<BlindedMessagePath>, Nonce), ()>
249+
where
250+
ES::Target: EntropySource,
251+
{
252+
let expanded_key = &self.inbound_payment_key;
253+
254+
let path_absolute_expiry = relative_expiry
255+
.unwrap_or(Duration::from_secs(u64::MAX))
256+
.saturating_add(self.duration_since_epoch());
257+
258+
let recipient_id_nonce = Nonce::from_entropy_source(entropy);
259+
let hmac = signer::hmac_for_offer_paths_request_context(recipient_id_nonce, expanded_key);
260+
261+
let context = MessageContext::AsyncPayments(AsyncPaymentsContext::OfferPathsRequest {
262+
recipient_id_nonce,
263+
hmac,
264+
path_absolute_expiry,
265+
});
266+
self.create_blinded_paths(peers, context).map(|paths| (paths, recipient_id_nonce))
267+
}
268+
232269
/// Creates a collection of blinded paths by delegating to [`MessageRouter`] based on
233270
/// the path's intended lifetime.
234271
///

lightning/src/offers/signer.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ const ASYNC_PAYMENTS_OFFER_PATHS_INPUT: &[u8; 16] = &[10; 16];
6565
#[cfg(async_payments)]
6666
const ASYNC_PAYMENTS_STATIC_INV_PERSISTED_INPUT: &[u8; 16] = &[11; 16];
6767

68+
///
69+
#[cfg(async_payments)]
70+
const ASYNC_PAYMENTS_OFFER_PATHS_REQUEST_INPUT: &[u8; 16] = &[12; 16];
71+
6872
/// Message metadata which possibly is derived from [`MetadataMaterial`] such that it can be
6973
/// verified.
7074
#[derive(Clone)]
@@ -581,6 +585,19 @@ pub(crate) fn verify_held_htlc_available_context(
581585
}
582586
}
583587

588+
#[cfg(async_payments)]
589+
pub(crate) fn hmac_for_offer_paths_request_context(
590+
nonce: Nonce, expanded_key: &ExpandedKey,
591+
) -> Hmac<Sha256> {
592+
const IV_BYTES: &[u8; IV_LEN] = b"LDK Paths Please"; // TODO
593+
let mut hmac = expanded_key.hmac_for_offer();
594+
hmac.input(IV_BYTES);
595+
hmac.input(&nonce.0);
596+
hmac.input(ASYNC_PAYMENTS_OFFER_PATHS_REQUEST_INPUT);
597+
598+
Hmac::from_engine(hmac)
599+
}
600+
584601
#[cfg(async_payments)]
585602
pub(crate) fn hmac_for_offer_paths_context(
586603
nonce: Nonce, expanded_key: &ExpandedKey,

0 commit comments

Comments
 (0)