Skip to content
Open
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
1,089 changes: 615 additions & 474 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions libs/gl-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ permissive = []
export = ["chacha20poly1305", "secp256k1"]

[dependencies]
aes = "0.8"
anyhow = "1.0.82"
async-stream = "0.3.5"
base64 = "^0.21"
bech32 = "0.9.1"
cbc = { version = "0.1", features = ["alloc"] }
bytes = "1.2.1"
chrono = "0.4.31"
hex = "0.4.3"
Expand Down
112 changes: 51 additions & 61 deletions libs/gl-client/src/lnurl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
mod models;
mod pay;
mod utils;
mod withdraw;

use self::models::{
LnUrlHttpClient, PayRequestCallbackResponse, PayRequestResponse, WithdrawRequestResponse,
};
use self::utils::{parse_invoice, parse_lnurl};
pub mod models;
pub mod pay;
pub mod utils;
pub mod withdraw;

use self::models::{LnUrlHttpClient, PayRequestResponse, WithdrawRequestResponse};
use self::utils::parse_lnurl;
use crate::node::ClnClient;
use crate::pb::cln::{amount_or_any, Amount, AmountOrAny};
use anyhow::{anyhow, Result};
use models::LnUrlHttpClearnetClient;
use pay::{resolve_lnurl_to_invoice, validate_invoice_from_callback_response};
use url::Url;
use withdraw::{build_withdraw_request_callback_url, parse_withdraw_request_response_from_url};

/// Result of resolving an LNURL endpoint via HTTP.
pub enum LnUrlResponse {
Pay(PayRequestResponse),
Withdraw(WithdrawRequestResponse),
}

pub struct LNURL<T: LnUrlHttpClient> {
http_client: T,
Expand All @@ -29,37 +30,34 @@ impl<T: LnUrlHttpClient> LNURL<T> {
LNURL { http_client }
}

pub async fn get_pay_request_response(&self, lnurl: &str) -> Result<PayRequestResponse> {
let url = parse_lnurl(lnurl)?;

let lnurl_pay_request_response: PayRequestResponse =
self.http_client.get_pay_request_response(&url).await?;

if lnurl_pay_request_response.tag != "payRequest" {
return Err(anyhow!("Expected tag to say 'payRequest'"));
/// Resolve an LNURL to its endpoint data with a single HTTP GET.
///
/// Decodes the bech32, fetches the URL, inspects the `tag` field,
/// and returns the appropriate typed response.
pub async fn resolve(&self, url: &str) -> Result<LnUrlResponse> {
let json = self.http_client.get_json(url).await?;

let tag = json
.get("tag")
.and_then(|t| t.as_str())
.unwrap_or("");

match tag {
"payRequest" => {
let response: PayRequestResponse = serde_json::from_value(json)
.map_err(|e| anyhow!("Failed to parse payRequest response: {}", e))?;
Ok(LnUrlResponse::Pay(response))
}
"withdrawRequest" => {
let response: WithdrawRequestResponse = serde_json::from_value(json)
.map_err(|e| anyhow!("Failed to parse withdrawRequest response: {}", e))?;
Ok(LnUrlResponse::Withdraw(response))
}
_ => Err(anyhow!(
"Unknown LNURL tag: '{}'. Expected 'payRequest' or 'withdrawRequest'.",
tag
)),
}

Ok(lnurl_pay_request_response)
}

pub async fn get_pay_request_callback_response(
&self,
base_callback_url: &str,
amount_msats: u64,
metadata: &str,
) -> Result<PayRequestCallbackResponse> {
let mut url = Url::parse(base_callback_url)?;
url.query_pairs_mut()
.append_pair("amount", &amount_msats.to_string());

let callback_response: PayRequestCallbackResponse = self
.http_client
.get_pay_request_callback_response(&url.to_string())
.await?;

let invoice = parse_invoice(&callback_response.pr)?;
validate_invoice_from_callback_response(&invoice, amount_msats, metadata)?;
Ok(callback_response)
}

pub async fn pay(
Expand All @@ -68,24 +66,27 @@ impl<T: LnUrlHttpClient> LNURL<T> {
amount_msats: u64,
node: &mut ClnClient,
) -> Result<tonic::Response<crate::pb::cln::PayResponse>> {
let invoice = resolve_lnurl_to_invoice(&self.http_client, lnurl, amount_msats).await?;
let (invoice, _success_action) =
pay::resolve_lnurl_to_invoice(&self.http_client, lnurl, amount_msats, None).await?;

node.pay(crate::pb::cln::PayRequest {
bolt11: invoice.to_string(),
bolt11: invoice,
..Default::default()
})
.await
.map_err(|e| anyhow!(e))
}

pub async fn get_withdraw_request_response(
pub async fn withdraw(
&self,
lnurl: &str,
) -> Result<WithdrawRequestResponse> {
amount_msats: u64,
node: &mut ClnClient,
) -> Result<()> {
let url = parse_lnurl(lnurl)?;
let withdrawal_request_response = parse_withdraw_request_response_from_url(&url);
let withdrawal_request_response =
withdraw::parse_withdraw_request_response_from_url(&url);

//If it's not a quick withdraw, then get the withdrawal_request_response from the web.
let withdrawal_request_response = match withdrawal_request_response {
Some(w) => w,
None => {
Expand All @@ -95,32 +96,21 @@ impl<T: LnUrlHttpClient> LNURL<T> {
}
};

Ok(withdrawal_request_response)
}

pub async fn withdraw(
&self,
lnurl: &str,
amount_msats: u64,
node: &mut ClnClient,
) -> Result<()> {
let withdraw_request_response = self.get_withdraw_request_response(lnurl).await?;

let amount = AmountOrAny {
value: Some(amount_or_any::Value::Amount(Amount { msat: amount_msats })),
};
let invoice = node
.invoice(crate::pb::cln::InvoiceRequest {
amount_msat: Some(amount),
description: withdraw_request_response.default_description.clone(),
description: withdrawal_request_response.default_description.clone(),
..Default::default()
})
.await
.map_err(|e| anyhow!(e))?
.into_inner();

let callback_url =
build_withdraw_request_callback_url(&withdraw_request_response, invoice.bolt11)?;
withdrawal_request_response.build_callback_url(&invoice.bolt11)?;

let _ = self
.http_client
Expand Down
Loading