Skip to content

Commit ec93a15

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 b6f577e commit ec93a15

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

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

1144+
/// Retrieve our cached [`Offer`]s for receiving async payments as an often-offline recipient.
1145+
/// Will only be set if [`UserConfig::paths_to_static_invoice_server`] is set and we succeeded in
1146+
/// interactively building a [`StaticInvoice`] with the static invoice server.
1147+
#[cfg(async_payments)]
1148+
pub(crate) fn get_cached_async_receive_offers(&self) -> Vec<Offer> {
1149+
self.async_receive_offer_cache.lock().unwrap().offers(self.duration_since_epoch())
1150+
}
1151+
11441152
/// Sends out [`OfferPathsRequest`] and [`ServeStaticInvoice`] onion messages if we are an
11451153
/// often-offline recipient and are configured to interactively build offers and static invoices
11461154
/// with a static invoice server.

0 commit comments

Comments
 (0)