Skip to content

Commit 4f8d15c

Browse files
Add API to retrieve cached async receive offers
Over the past several commits we've implemented interactively building an async receive offer with a static invoice server that will service invoice requests on our behalf as an async recipient. Here we add an API to retrieve the resulting offers so we can receive payments when we're offline.
1 parent b050d77 commit 4f8d15c

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10608,9 +10608,23 @@ where
1060810608
#[cfg(c_bindings)]
1060910609
create_refund_builder!(self, RefundMaybeWithDerivedMetadataBuilder);
1061010610

10611+
/// Retrieve our cached [`Offer`]s for receiving async payments as an often-offline recipient.
10612+
/// Will only be set if [`UserConfig::paths_to_static_invoice_server`] is set and we succeeded in
10613+
/// interactively building a [`StaticInvoice`] with the static invoice server.
10614+
///
10615+
/// Useful for posting offers to receive payments later, such as posting an offer on a website.
10616+
#[cfg(async_payments)]
10617+
pub fn get_cached_async_receive_offers(&self) -> Vec<Offer> {
10618+
self.flow.get_cached_async_receive_offers()
10619+
}
10620+
1061110621
/// Create an offer for receiving async payments as an often-offline recipient.
1061210622
///
10613-
/// Because we may be offline when the payer attempts to request an invoice, you MUST:
10623+
/// Instead of using this method, it is preferable to set
10624+
/// [`UserConfig::paths_to_static_invoice_server`] and retrieve the automatically built offer via
10625+
/// [`Self::get_cached_async_receive_offers`].
10626+
///
10627+
/// If you want to build the [`StaticInvoice`] manually using this method instead, you MUST:
1061410628
/// 1. Provide at least 1 [`BlindedMessagePath`] terminating at an always-online node that will
1061510629
/// serve the [`StaticInvoice`] created from this offer on our behalf.
1061610630
/// 2. Use [`Self::create_static_invoice_builder`] to create a [`StaticInvoice`] from this
@@ -10627,6 +10641,10 @@ where
1062710641
/// Creates a [`StaticInvoiceBuilder`] from the corresponding [`Offer`] and [`Nonce`] that were
1062810642
/// created via [`Self::create_async_receive_offer_builder`]. If `relative_expiry` is unset, the
1062910643
/// invoice's expiry will default to [`STATIC_INVOICE_DEFAULT_RELATIVE_EXPIRY`].
10644+
///
10645+
/// Instead of using this method to manually build the invoice, it is preferable to set
10646+
/// [`UserConfig::paths_to_static_invoice_server`] and retrieve the automatically built offer via
10647+
/// [`Self::get_cached_async_receive_offers`].
1063010648
#[cfg(async_payments)]
1063110649
#[rustfmt::skip]
1063210650
pub fn create_static_invoice_builder<'a>(

lightning/src/offers/async_receive_offer_cache.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,26 @@ const PATHS_REQUESTS_BUFFER: Duration = Duration::from_secs(3 * 60 * 60);
103103

104104
#[cfg(async_payments)]
105105
impl AsyncReceiveOfferCache {
106+
/// Retrieve our cached [`Offer`]s for receiving async payments as an often-offline recipient.
107+
pub fn offers(&self, duration_since_epoch: Duration) -> Vec<Offer> {
108+
const NEVER_EXPIRES: Duration = Duration::from_secs(u64::MAX);
109+
110+
self.offers
111+
.iter()
112+
.filter_map(|offer| {
113+
if offer.static_invoice_absolute_expiry < duration_since_epoch {
114+
None
115+
} else if offer.offer.absolute_expiry().unwrap_or(NEVER_EXPIRES)
116+
< duration_since_epoch
117+
{
118+
None
119+
} else {
120+
Some(offer.offer.clone())
121+
}
122+
})
123+
.collect()
124+
}
125+
106126
/// Remove expired offers from the cache.
107127
pub(super) fn prune_expired_offers(&mut self, duration_since_epoch: Duration) {
108128
// Remove expired offers from the cache.

lightning/src/offers/flow.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,14 @@ where
11161116
core::mem::take(&mut self.pending_dns_onion_messages.lock().unwrap())
11171117
}
11181118

1119+
/// Retrieve our cached [`Offer`]s for receiving async payments as an often-offline recipient.
1120+
/// Will only be set if [`UserConfig::paths_to_static_invoice_server`] is set and we succeeded in
1121+
/// interactively building a [`StaticInvoice`] with the static invoice server.
1122+
#[cfg(async_payments)]
1123+
pub(crate) fn get_cached_async_receive_offers(&self) -> Vec<Offer> {
1124+
self.async_receive_offer_cache.lock().unwrap().offers(self.duration_since_epoch())
1125+
}
1126+
11191127
/// Sends out [`OfferPathsRequest`] and [`ServeStaticInvoice`] onion messages if we are an
11201128
/// often-offline recipient and are configured to interactively build offers and static invoices
11211129
/// with a static invoice server.

0 commit comments

Comments
 (0)