Skip to content
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

Pay external lightning addresses #329

Draft
wants to merge 1 commit into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ pub struct Account {
/// Pay Invoice request
#[derive(Debug, Serialize, Deserialize)]
pub struct PayInvoiceRequest {
pub payment_request: String,
pub payment_request: Option<String>,
pub recipient: Option<String>,
pub amount: Option<u32>,
}

/// Lightning transaction
Expand Down Expand Up @@ -225,7 +227,28 @@ pub async fn pay_invoice(payment_request: &str, token: &str) -> Result<PayInvoic
let endpoint = LNDHUB_ENDPOINT.read().await;
let url = format!("{endpoint}/payinvoice");
let req = PayInvoiceRequest {
payment_request: payment_request.to_string(),
payment_request: Some(payment_request.to_string()),
recipient: None,
amount: None,
};
let response = post_json_auth(&url, &Some(req), Some(token)).await?;
let response: PayInvoiceResponse = serde_json::from_str(&response)?;

Ok(response)
}

/// Pay a lightning address
pub async fn pay_ln_address(
ln_address: &str,
amount: u32,
token: &str,
) -> Result<PayInvoiceResponse> {
let endpoint = LNDHUB_ENDPOINT.read().await;
let url = format!("{endpoint}/payinvoice");
let req = PayInvoiceRequest {
payment_request: None,
recipient: Some(ln_address.to_string()),
amount: Some(amount),
};
let response = post_json_auth(&url, &Some(req), Some(token)).await?;
let response: PayInvoiceResponse = serde_json::from_str(&response)?;
Expand Down
14 changes: 14 additions & 0 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,20 @@ pub mod lightning {
})
}

#[wasm_bindgen]
pub fn pay_ln_address(ln_address: String, amount: u32, token: String) -> Promise {
set_panic_hook();

future_to_promise(async move {
match crate::lightning::pay_ln_address(&ln_address, amount, &token).await {
Ok(result) => Ok(JsValue::from_string(
serde_json::to_string(&result).unwrap(),
)),
Err(err) => Err(JsValue::from_string(err.to_string())),
}
})
}

#[wasm_bindgen]
pub fn check_payment(payment_hash: String) -> Promise {
set_panic_hook();
Expand Down