-
Notifications
You must be signed in to change notification settings - Fork 2
refactor: consolidate business logic in FmcdCore and fix balance calc… #10
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
Merged
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| pub mod resolvers; | ||
| pub mod rest; | ||
| pub mod websockets; | ||
|
|
||
| // Re-export commonly used items | ||
| pub use resolvers::LnurlResolver; | ||
| pub use rest as handlers; // For backward compatibility | ||
| pub use websockets::websocket_handler; |
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| pub mod payment; | ||
|
|
||
| pub use payment::LnurlResolver; |
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 |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| use std::str::FromStr; | ||
|
|
||
| use anyhow::{bail, Context}; | ||
| use async_trait::async_trait; | ||
| use fedimint_core::Amount; | ||
| use fedimint_ln_common::lightning_invoice::Bolt11Invoice; | ||
| use tracing::debug; | ||
|
|
||
| use crate::core::PaymentInfoResolver; | ||
| use crate::error::AppError; | ||
| use crate::observability::sanitize_invoice; | ||
|
|
||
| /// LNURL resolver implementation for the API layer | ||
| /// Handles LNURL and Lightning Address resolution to Bolt11 invoices | ||
| pub struct LnurlResolver { | ||
| http_client: reqwest::Client, | ||
| } | ||
|
|
||
| impl LnurlResolver { | ||
| pub fn new() -> Self { | ||
| Self { | ||
| http_client: reqwest::Client::new(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Default for LnurlResolver { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl PaymentInfoResolver for LnurlResolver { | ||
| async fn resolve_payment_info( | ||
| &self, | ||
| payment_info: &str, | ||
| amount_msat: Option<Amount>, | ||
| lnurl_comment: Option<&str>, | ||
| ) -> Result<Option<String>, AppError> { | ||
| let info = payment_info.trim(); | ||
|
|
||
| // First check if it's already a Bolt11 invoice | ||
| if let Ok(invoice) = Bolt11Invoice::from_str(info) { | ||
| debug!( | ||
| "Payment info is already a bolt11 invoice: {}", | ||
| sanitize_invoice(&invoice) | ||
| ); | ||
|
|
||
| // Validate amount constraints | ||
| match (invoice.amount_milli_satoshis(), amount_msat) { | ||
| (Some(_), Some(_)) => { | ||
| return Err(AppError::validation_error( | ||
| "Amount specified in both invoice and request", | ||
| )); | ||
| } | ||
| (None, _) => { | ||
| return Err(AppError::validation_error( | ||
| "Invoices without amounts are not supported", | ||
| )); | ||
| } | ||
| _ => {} | ||
| } | ||
|
|
||
| // Return None to indicate no resolution needed - use original payment_info | ||
| return Ok(None); | ||
| } | ||
|
|
||
| // Try to parse as LNURL or Lightning Address | ||
| let lnurl = if info.to_lowercase().starts_with("lnurl") { | ||
| lnurl::lnurl::LnUrl::from_str(info) | ||
| .map_err(|e| AppError::validation_error(format!("Invalid LNURL: {}", e)))? | ||
| } else if info.contains('@') { | ||
| lnurl::lightning_address::LightningAddress::from_str(info) | ||
| .map_err(|e| { | ||
| AppError::validation_error(format!("Invalid Lightning Address: {}", e)) | ||
| })? | ||
| .lnurl() | ||
| } else { | ||
| // Not LNURL or Lightning Address, return None to try as Bolt11 | ||
| return Ok(None); | ||
| }; | ||
|
|
||
| debug!("Parsed payment info as LNURL: {:?}", lnurl); | ||
|
|
||
| let amount = amount_msat | ||
| .context("Amount must be specified when using LNURL or Lightning Address") | ||
| .map_err(|e| AppError::validation_error(e.to_string()))?; | ||
|
|
||
| // Create LNURL client | ||
| let async_client = lnurl::AsyncClient::from_client(self.http_client.clone()); | ||
|
|
||
| // Make LNURL request | ||
| let response = async_client | ||
| .make_request(&lnurl.url) | ||
| .await | ||
| .map_err(|e| AppError::gateway_error(format!("LNURL request failed: {}", e)))?; | ||
|
|
||
| match response { | ||
| lnurl::LnUrlResponse::LnUrlPayResponse(pay_response) => { | ||
| // Get the invoice from the LNURL service | ||
| let invoice_response = async_client | ||
| .get_invoice(&pay_response, amount.msats, None, lnurl_comment) | ||
| .await | ||
| .map_err(|e| { | ||
| AppError::gateway_error(format!("Failed to get invoice from LNURL: {}", e)) | ||
| })?; | ||
|
|
||
| // Validate the returned invoice | ||
| let invoice = Bolt11Invoice::from_str(invoice_response.invoice()).map_err(|e| { | ||
| AppError::validation_error(format!("Invalid invoice from LNURL: {}", e)) | ||
| })?; | ||
|
|
||
| // Verify amount matches | ||
| if invoice.amount_milli_satoshis() != Some(amount.msats) { | ||
| return Err(AppError::validation_error(format!( | ||
| "LNURL returned invoice with wrong amount. Expected {} msat, got {:?}", | ||
| amount.msats, | ||
| invoice.amount_milli_satoshis() | ||
| ))); | ||
| } | ||
|
|
||
| Ok(Some(invoice.to_string())) | ||
| } | ||
| other => Err(AppError::validation_error(format!( | ||
| "Unexpected LNURL response type: {:?}", | ||
| other | ||
| ))), | ||
| } | ||
| } | ||
| } | ||
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
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.
Parsing a Bolt11Invoice twice (once here for validation, then again in the core) is inefficient. Consider returning the parsed invoice or restructuring to avoid duplicate parsing.