Skip to content

Commit d1cc154

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 62d5a73 commit d1cc154

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10378,9 +10378,21 @@ where
1037810378
#[cfg(c_bindings)]
1037910379
create_refund_builder!(self, RefundMaybeWithDerivedMetadataBuilder);
1038010380

10381+
/// Retrieve our cached [`Offer`]s for receiving async payments as an often-offline recipient.
10382+
/// Will only be set if [`UserConfig::paths_to_static_invoice_server`] is set and we succeeded in
10383+
/// interactively building a [`StaticInvoice`] with the static invoice server.
10384+
#[cfg(async_payments)]
10385+
pub fn get_cached_async_receive_offers(&self) -> Vec<Offer> {
10386+
self.flow.get_cached_async_receive_offers()
10387+
}
10388+
1038110389
/// Create an offer for receiving async payments as an often-offline recipient.
1038210390
///
10383-
/// Because we may be offline when the payer attempts to request an invoice, you MUST:
10391+
/// Instead of using this method, it is preferable to set
10392+
/// [`UserConfig::paths_to_static_invoice_server`] and retrieve the automatically built offer via
10393+
/// [`Self::get_cached_async_receive_offers`].
10394+
///
10395+
/// If you want to build the [`StaticInvoice`] manually using this method instead, you MUST:
1038410396
/// 1. Provide at least 1 [`BlindedMessagePath`] terminating at an always-online node that will
1038510397
/// serve the [`StaticInvoice`] created from this offer on our behalf.
1038610398
/// 2. Use [`Self::create_static_invoice_builder`] to create a [`StaticInvoice`] from this
@@ -10396,6 +10408,10 @@ where
1039610408
/// Creates a [`StaticInvoiceBuilder`] from the corresponding [`Offer`] and [`Nonce`] that were
1039710409
/// created via [`Self::create_async_receive_offer_builder`]. If `relative_expiry` is unset, the
1039810410
/// invoice's expiry will default to [`STATIC_INVOICE_DEFAULT_RELATIVE_EXPIRY`].
10411+
///
10412+
/// Instead of using this method to manually build the invoice, it is preferable to set
10413+
/// [`UserConfig::paths_to_static_invoice_server`] and retrieve the automatically built offer via
10414+
/// [`Self::get_cached_async_receive_offers`].
1039910415
#[cfg(async_payments)]
1040010416
pub fn create_static_invoice_builder<'a>(
1040110417
&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
@@ -1138,6 +1138,14 @@ where
11381138
core::mem::take(&mut self.pending_dns_onion_messages.lock().unwrap())
11391139
}
11401140

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

0 commit comments

Comments
 (0)