Skip to content

Commit 5fc6716

Browse files
committed
Introduce async handling determiners for Invoice/InvoiceRequest
1 parent aabc42e commit 5fc6716

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

lightning/src/offers/flow.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,62 @@ impl<MR: Deref> OffersMessageFlow<MR>
570570
where
571571
MR::Target: MessageRouter,
572572
{
573+
/// Determines whether the given [`VerifiedInvoiceRequest`] should be
574+
/// handled synchronously or dispatched as an event, based on [`FlowConfigs`].
575+
///
576+
/// Returns:
577+
/// - `Ok(Some(request))` if the caller should handle it now.
578+
/// - `Ok(None)` if it was dispatched for async processing.
579+
/// - `Err(())` in case of validation or enqueue failure.
580+
pub fn determine_invoice_request_handling(
581+
&self, invoice_request: VerifiedInvoiceRequest,
582+
) -> Result<Option<VerifiedInvoiceRequest>, ()> {
583+
if !self.user_configs.handle_invoice_request_asyncly(&invoice_request.inner)? {
584+
// Synchronous path: return the request for user handling.
585+
return Ok(Some(invoice_request));
586+
}
587+
588+
let amount_source = invoice_request.inner.contents.amount_source().map_err(|_| ())?;
589+
590+
// Dispatch event for async handling.
591+
self.enqueue_offers_event(OfferEvents::InvoiceRequestReceived {
592+
invoice_request,
593+
amount_source,
594+
})?;
595+
596+
Ok(None)
597+
}
598+
599+
/// Determines whether the given [`Bolt12Invoice`] should be handled
600+
/// synchronously or dispatched as an event, based on [`FlowConfigs`].
601+
///
602+
/// Returns:
603+
/// - `Ok(Some(request))` if the caller should handle it now.
604+
/// - `Ok(None)` if it was dispatched for async processing.
605+
/// - `Err(())` in case of validation or enqueue failure.
606+
pub fn determine_invoice_handling(
607+
&self, invoice: Bolt12Invoice, payment_id: PaymentId,
608+
) -> Result<Option<Bolt12Invoice>, ()> {
609+
if !self.user_configs.handle_invoice_asyncly(&invoice)? {
610+
// Synchronous path: return the invoice for user handling.
611+
return Ok(Some(invoice));
612+
}
613+
614+
let invoice_amount = invoice.amount_msats();
615+
let amount_source = invoice.amount_source().map_err(|_| ())?;
616+
617+
let event = OfferEvents::Bolt12InvoiceReceived {
618+
invoice,
619+
payment_id,
620+
invoice_amount,
621+
amount_source,
622+
};
623+
624+
self.enqueue_offers_event(event)?;
625+
626+
Ok(None)
627+
}
628+
573629
/// Verifies an [`InvoiceRequest`] using the provided [`OffersContext`] or the [`InvoiceRequest::metadata`].
574630
///
575631
/// - If an [`OffersContext::InvoiceRequest`] with a `nonce` is provided, verification is performed using recipient context data.

0 commit comments

Comments
 (0)