-
Notifications
You must be signed in to change notification settings - Fork 0
feat(api): add /payinvoice endpoint for outbound BOLT11 payments #11
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| use std::str::FromStr; | ||
| use std::sync::Arc; | ||
|
|
||
| use hex::DisplayHex; | ||
| use ldk_node::lightning_invoice::Bolt11Invoice; | ||
| use ldk_node::Node; | ||
|
|
||
| use crate::daemon::api::error::AppError; | ||
| use crate::daemon::types::{PayInvoiceRequest, PayInvoiceResponse}; | ||
|
|
||
| pub async fn handle_pay_invoice( | ||
| node: Arc<Node>, | ||
| req: &PayInvoiceRequest, | ||
| ) -> Result<PayInvoiceResponse, AppError> { | ||
| let invoice = Bolt11Invoice::from_str(req.invoice.trim()) | ||
| .map_err(|e| AppError::BadRequest(format!("invalid bolt11 invoice: {e}")))?; | ||
|
|
||
| let bolt11 = node.bolt11_payment(); | ||
| let payment_id = match (invoice.amount_milli_satoshis(), req.amount_sat) { | ||
| (Some(_), None) => bolt11 | ||
| .send(&invoice, None) | ||
| .map_err(|e| AppError::Internal(format!("pay failed: {e}")))?, | ||
| (None, Some(amount_sat)) => bolt11 | ||
| .send_using_amount(&invoice, amount_sat * 1000, None) | ||
| .map_err(|e| AppError::Internal(format!("pay failed: {e}")))?, | ||
| (Some(invoice_msat), Some(amount_sat)) => { | ||
| if invoice_msat != amount_sat * 1000 { | ||
| return Err(AppError::BadRequest(format!( | ||
| "amountSat ({amount_sat}) does not match invoice amount ({} sat)", | ||
| invoice_msat / 1000 | ||
| ))); | ||
| } | ||
| bolt11 | ||
| .send(&invoice, None) | ||
| .map_err(|e| AppError::Internal(format!("pay failed: {e}")))? | ||
| } | ||
| (None, None) => { | ||
| return Err(AppError::BadRequest( | ||
| "zero-amount invoice requires amountSat".into(), | ||
| )) | ||
| } | ||
| }; | ||
|
|
||
| let payment_hash = invoice.payment_hash().to_string(); | ||
|
|
||
| Ok(PayInvoiceResponse { | ||
| payment_id: payment_id.0.to_lower_hex_string(), | ||
| payment_hash, | ||
| }) | ||
| } | ||
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.
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.
Should throw a bad request error if the request amount does not match the invoice amount
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.
Left this on the wrong line. I think it is fine if both are set as long as they match. Curious what phoenixd does here
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.
1152a95