-
Notifications
You must be signed in to change notification settings - Fork 427
Support Currency-Based Offers and Async Invoice Handling via FlowEvents #3833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shaavan
wants to merge
7
commits into
lightningdevkit:main
Choose a base branch
from
shaavan:currency
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3ed36a7
Introduce CurrencyConversion trait
shaavan 8d45f0a
Integrate CurrencyConversion into Bolt12Invoice amount handling
shaavan 3aa3370
Introduce amount check in pay_for_offer
shaavan c0b0817
Split `enqueue_invoice` into destination-specific variants
shaavan 856ee8f
Introduce `enqueue_invoice_error` API
shaavan 5ff7198
Introduce FlowEvents for manual handling of offers messages
shaavan a4742bd
Introduce OfferMessageFlowEvent test with manual offer response
shaavan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,9 +93,11 @@ use crate::offers::async_receive_offer_cache::AsyncReceiveOfferCache; | |
| use crate::offers::flow::{HeldHtlcReplyPath, InvreqResponseInstructions, OffersMessageFlow}; | ||
| use crate::offers::invoice::{Bolt12Invoice, UnsignedBolt12Invoice}; | ||
| use crate::offers::invoice_error::InvoiceError; | ||
| use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestVerifiedFromOffer}; | ||
| use crate::offers::invoice_request::{ | ||
| DefaultCurrencyConversion, InvoiceRequest, InvoiceRequestVerifiedFromOffer, | ||
| }; | ||
| use crate::offers::nonce::Nonce; | ||
| use crate::offers::offer::{Offer, OfferFromHrn}; | ||
| use crate::offers::offer::{Amount, Offer, OfferFromHrn}; | ||
| use crate::offers::parse::Bolt12SemanticError; | ||
| use crate::offers::refund::Refund; | ||
| use crate::offers::static_invoice::StaticInvoice; | ||
|
|
@@ -2664,6 +2666,9 @@ pub struct ChannelManager< | |
| fee_estimator: LowerBoundedFeeEstimator<F>, | ||
| chain_monitor: M, | ||
| tx_broadcaster: T, | ||
| #[cfg(test)] | ||
| pub(super) router: R, | ||
| #[cfg(not(test))] | ||
| router: R, | ||
|
|
||
| #[cfg(test)] | ||
|
|
@@ -2890,6 +2895,9 @@ pub struct ChannelManager< | |
| pub(super) entropy_source: ES, | ||
| #[cfg(not(test))] | ||
| entropy_source: ES, | ||
| #[cfg(test)] | ||
| pub(super) node_signer: NS, | ||
| #[cfg(not(test))] | ||
| node_signer: NS, | ||
| #[cfg(test)] | ||
| pub(super) signer_provider: SP, | ||
|
|
@@ -3945,7 +3953,8 @@ where | |
| let flow = OffersMessageFlow::new( | ||
| ChainHash::using_genesis_block(params.network), params.best_block, | ||
| our_network_pubkey, current_timestamp, expanded_inbound_key, | ||
| node_signer.get_receive_auth_key(), secp_ctx.clone(), message_router, logger.clone(), | ||
| node_signer.get_receive_auth_key(), secp_ctx.clone(), message_router, false, | ||
| logger.clone(), | ||
| ); | ||
|
|
||
| ChannelManager { | ||
|
|
@@ -5666,6 +5675,7 @@ where | |
| let features = self.bolt12_invoice_features(); | ||
| let outbound_pmts_res = self.pending_outbound_payments.static_invoice_received( | ||
| invoice, | ||
| &DefaultCurrencyConversion, | ||
| payment_id, | ||
| features, | ||
| best_block_height, | ||
|
|
@@ -12935,12 +12945,21 @@ where | |
| /// Uses [`InvoiceRequestBuilder`] such that the [`InvoiceRequest`] it builds is recognized by | ||
| /// the [`ChannelManager`] when handling a [`Bolt12Invoice`] message in response to the request. | ||
| /// | ||
| /// `amount_msats` allows you to overpay what is required to satisfy the offer, or may be | ||
| /// required if the offer does not require a specific amount. | ||
| /// | ||
| /// If the [`Offer`] was built from a human readable name resolved using BIP 353, you *must* | ||
| /// instead call [`Self::pay_for_offer_from_hrn`]. | ||
| /// | ||
| /// # Amount | ||
| /// `amount_msats` allows you to overpay what is required to satisfy the offer, or may be | ||
| /// required if the offer does not require a specific amount. | ||
| /// | ||
| /// # Currency | ||
| /// | ||
| /// If the [`Offer`] specifies its amount in a currency (that is, [`Amount::Currency`]), callers | ||
| /// must provide the `amount_msats`. [`ChannelManager`] enforces only that an amount is present | ||
| /// in this case. It does not verify here that the provided `amount_msats` is sufficient once | ||
| /// converted from the currency amount. The recipient may reject the resulting [`InvoiceRequest`] | ||
| /// if the amount is insufficient after conversion. | ||
| /// | ||
| /// # Payment | ||
| /// | ||
| /// The provided `payment_id` is used to ensure that only one invoice is paid for the request | ||
|
|
@@ -13078,6 +13097,13 @@ where | |
| let entropy = &*self.entropy_source; | ||
| let nonce = Nonce::from_entropy_source(entropy); | ||
|
|
||
| // If the offer is for a specific currency, ensure the amount is provided. | ||
| if let Some(Amount::Currency { iso4217_code: _, amount: _ }) = offer.amount() { | ||
| if amount_msats.is_none() { | ||
| return Err(Bolt12SemanticError::MissingAmount); | ||
| } | ||
| } | ||
|
|
||
shaavan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let builder = self.flow.create_invoice_request_builder( | ||
| offer, nonce, payment_id, | ||
| )?; | ||
|
|
@@ -13160,7 +13186,20 @@ where | |
|
|
||
| let invoice = builder.allow_mpp().build_and_sign(secp_ctx)?; | ||
|
|
||
| self.flow.enqueue_invoice(invoice.clone(), refund, self.get_peers_for_blinded_path())?; | ||
| if refund.paths().is_empty() { | ||
| self.flow.enqueue_invoice_using_node_id( | ||
| invoice.clone(), | ||
| refund.payer_signing_pubkey(), | ||
| self.get_peers_for_blinded_path(), | ||
| )?; | ||
| } else { | ||
| self.flow.enqueue_invoice_using_reply_paths( | ||
| invoice.clone(), | ||
| refund.paths(), | ||
| self.get_peers_for_blinded_path(), | ||
| )?; | ||
| } | ||
|
|
||
| Ok(invoice) | ||
| } | ||
|
|
||
|
|
@@ -13383,7 +13422,7 @@ where | |
| now | ||
| } | ||
|
|
||
| fn get_peers_for_blinded_path(&self) -> Vec<MessageForwardNode> { | ||
| pub(crate) fn get_peers_for_blinded_path(&self) -> Vec<MessageForwardNode> { | ||
| let per_peer_state = self.per_peer_state.read().unwrap(); | ||
| per_peer_state | ||
| .iter() | ||
|
|
@@ -15296,7 +15335,7 @@ where | |
| None => return None, | ||
| }; | ||
|
|
||
| let invoice_request = match self.flow.verify_invoice_request(invoice_request, context) { | ||
| let invoice_request = match self.flow.verify_invoice_request(invoice_request, context, responder.clone()) { | ||
| Ok(InvreqResponseInstructions::SendInvoice(invoice_request)) => invoice_request, | ||
| Ok(InvreqResponseInstructions::SendStaticInvoice { recipient_id, invoice_slot, invoice_request }) => { | ||
| self.pending_events.lock().unwrap().push_back((Event::StaticInvoiceRequested { | ||
|
|
@@ -15305,6 +15344,7 @@ where | |
|
|
||
| return None | ||
| }, | ||
| Ok(InvreqResponseInstructions::AsynchronouslyHandleResponse) => return None, | ||
| Err(_) => return None, | ||
| }; | ||
|
|
||
|
|
@@ -15320,6 +15360,7 @@ where | |
| InvoiceRequestVerifiedFromOffer::DerivedKeys(request) => { | ||
| let result = self.flow.create_invoice_builder_from_invoice_request_with_keys( | ||
| &self.router, | ||
| &DefaultCurrencyConversion, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So is the intention that some future PR will enable currency conversion for |
||
| &request, | ||
| self.list_usable_channels(), | ||
| get_payment_info, | ||
|
|
@@ -15344,6 +15385,7 @@ where | |
| InvoiceRequestVerifiedFromOffer::ExplicitKeys(request) => { | ||
| let result = self.flow.create_invoice_builder_from_invoice_request_without_keys( | ||
| &self.router, | ||
| &DefaultCurrencyConversion, | ||
| &request, | ||
| self.list_usable_channels(), | ||
| get_payment_info, | ||
|
|
@@ -18101,6 +18143,7 @@ where | |
| args.node_signer.get_receive_auth_key(), | ||
| secp_ctx.clone(), | ||
| args.message_router, | ||
| false, | ||
| args.logger.clone(), | ||
| ) | ||
| .with_async_payments_offers_cache(async_receive_offer_cache); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logic error: The validation checks if
amount_msats.is_none()for currency offers, but this validation occurs before the invoice request is created. At this point,amount_msatsrepresents the user-provided amount parameter, not the final computed amount from the invoice request. Currency offers may be valid without an explicit amount if the offer itself specifies the amount. This will incorrectly reject valid currency-based offers.Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we ever check if the amount provided by the user meets the converted amount in the offer? Can't recall where we landed on this. At very least we should document
pay_for_offerindicating what is expected inpay_for_offerdocs and update the "Errors" section accordingly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the pointer! I have clarified this in pay_for_offer docs in .10
Since
pay_for_offeris a caller-initiated API, I kept the validation minimal: when the offer specifies a currency amount, the caller must supply anamount_msats. We don’t enforce whether that amount meets the converted minimum at this point. The payee naturally checks for that when the InvoiceRequest is processed. If the amount is insufficient, the payee will reject it.The updated documentation now spells out this expectation and notes where the insufficiency check actually occurs.