Skip to content

Commit cd62d3a

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 b67220b commit cd62d3a

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

lightning/src/ln/channelmanager.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -12760,7 +12760,35 @@ where
1276012760

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

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

lightning/src/offers/signer.rs

+11
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)