Skip to content

Commit 6adac0b

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 e802f9a commit 6adac0b

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
@@ -10356,9 +10356,23 @@ where
1035610356
#[cfg(c_bindings)]
1035710357
create_refund_builder!(self, RefundMaybeWithDerivedMetadataBuilder);
1035810358

10359+
/// Retrieve our cached [`Offer`]s for receiving async payments as an often-offline recipient.
10360+
/// Will only be set if [`UserConfig::paths_to_static_invoice_server`] is set and we succeeded in
10361+
/// interactively building a [`StaticInvoice`] with the static invoice server.
10362+
///
10363+
/// Useful for posting offers to receive payments later, such as posting an offer on a website.
10364+
#[cfg(async_payments)]
10365+
pub fn get_cached_async_receive_offers(&self) -> Vec<Offer> {
10366+
self.flow.get_cached_async_receive_offers()
10367+
}
10368+
1035910369
/// Create an offer for receiving async payments as an often-offline recipient.
1036010370
///
10361-
/// Because we may be offline when the payer attempts to request an invoice, you MUST:
10371+
/// Instead of using this method, it is preferable to set
10372+
/// [`UserConfig::paths_to_static_invoice_server`] and retrieve the automatically built offer via
10373+
/// [`Self::get_cached_async_receive_offers`].
10374+
///
10375+
/// If you want to build the [`StaticInvoice`] manually using this method instead, you MUST:
1036210376
/// 1. Provide at least 1 [`BlindedMessagePath`] terminating at an always-online node that will
1036310377
/// serve the [`StaticInvoice`] created from this offer on our behalf.
1036410378
/// 2. Use [`Self::create_static_invoice_builder`] to create a [`StaticInvoice`] from this
@@ -10374,6 +10388,10 @@ where
1037410388
/// Creates a [`StaticInvoiceBuilder`] from the corresponding [`Offer`] and [`Nonce`] that were
1037510389
/// created via [`Self::create_async_receive_offer_builder`]. If `relative_expiry` is unset, the
1037610390
/// invoice's expiry will default to [`STATIC_INVOICE_DEFAULT_RELATIVE_EXPIRY`].
10391+
///
10392+
/// Instead of using this method to manually build the invoice, it is preferable to set
10393+
/// [`UserConfig::paths_to_static_invoice_server`] and retrieve the automatically built offer via
10394+
/// [`Self::get_cached_async_receive_offers`].
1037710395
#[cfg(async_payments)]
1037810396
pub fn create_static_invoice_builder<'a>(
1037910397
&self, offer: &'a Offer, offer_nonce: Nonce, relative_expiry: Option<Duration>

lightning/src/offers/async_receive_offer_cache.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,26 @@ impl AsyncReceiveOfferCache {
9393
// invoice before giving up.
9494
const MAX_UPDATE_ATTEMPTS: u8 = 3;
9595

96+
/// Retrieve our cached [`Offer`]s for receiving async payments as an often-offline recipient.
97+
pub fn offers(&self, duration_since_epoch: Duration) -> Vec<Offer> {
98+
const NEVER_EXPIRES: Duration = Duration::from_secs(u64::MAX);
99+
100+
self.offers
101+
.iter()
102+
.filter_map(|offer| {
103+
if offer.static_invoice_absolute_expiry < duration_since_epoch {
104+
None
105+
} else if offer.offer.absolute_expiry().unwrap_or(NEVER_EXPIRES)
106+
< duration_since_epoch
107+
{
108+
None
109+
} else {
110+
Some(offer.offer.clone())
111+
}
112+
})
113+
.collect()
114+
}
115+
96116
/// Remove expired offers from the cache.
97117
pub(super) fn prune_expired_offers(&mut self, duration_since_epoch: Duration) {
98118
// 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
@@ -1115,6 +1115,14 @@ where
11151115
core::mem::take(&mut self.pending_dns_onion_messages.lock().unwrap())
11161116
}
11171117

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

0 commit comments

Comments
 (0)