Skip to content

Commit 2f9beb6

Browse files
Cache offer on StaticInvoicePersisted onion message
As an async recipient, we need to interactively build a static invoice that an always-online node will serve on our behalf. Once this invoice is built and persisted by the static invoice server, they will send us a confirmation onion message. At this time, cache the corresponding offer and mark it as ready to receive async payments.
1 parent 63ddee8 commit 2f9beb6

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12759,7 +12759,35 @@ where
1275912759

1276012760
fn handle_static_invoice_persisted(
1276112761
&self, _message: StaticInvoicePersisted, _context: AsyncPaymentsContext,
12762-
) {}
12762+
) {
12763+
#[cfg(async_payments)] {
12764+
let expanded_key = &self.inbound_payment_key;
12765+
let duration_since_epoch = self.duration_since_epoch();
12766+
12767+
let mut new_offer = match _context {
12768+
AsyncPaymentsContext::StaticInvoicePersisted {
12769+
offer, nonce, hmac, path_absolute_expiry
12770+
} => {
12771+
if let Err(()) = signer::verify_static_invoice_persisted_context(nonce, hmac, expanded_key) {
12772+
return
12773+
}
12774+
if duration_since_epoch > path_absolute_expiry { return }
12775+
if offer.is_expired_no_std(duration_since_epoch) { return }
12776+
Some(offer)
12777+
},
12778+
_ => return
12779+
};
12780+
12781+
PersistenceNotifierGuard::optionally_notify(self, || {
12782+
let mut offer_cache = self.async_receive_offer_cache.lock().unwrap();
12783+
if !offer_cache.should_refresh_offer(duration_since_epoch) {
12784+
return NotifyOption::SkipPersistNoEvents
12785+
}
12786+
offer_cache.offer = new_offer.take();
12787+
NotifyOption::DoPersist
12788+
});
12789+
}
12790+
}
1276312791

1276412792
fn handle_held_htlc_available(
1276512793
&self, _message: HeldHtlcAvailable, _context: AsyncPaymentsContext,

lightning/src/offers/signer.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,3 +602,14 @@ pub(crate) fn hmac_for_static_invoice_persisted_context(
602602

603603
Hmac::from_engine(hmac)
604604
}
605+
606+
#[cfg(async_payments)]
607+
pub(crate) fn verify_static_invoice_persisted_context(
608+
nonce: Nonce, hmac: Hmac<Sha256>, expanded_key: &ExpandedKey,
609+
) -> Result<(), ()> {
610+
if hmac_for_static_invoice_persisted_context(nonce, expanded_key) == hmac {
611+
Ok(())
612+
} else {
613+
Err(())
614+
}
615+
}

0 commit comments

Comments
 (0)