Skip to content

Commit

Permalink
Removed unneeded comments, shifted unused import, and removed unused …
Browse files Browse the repository at this point in the history
…method parameter.
  • Loading branch information
Randy808 committed Aug 10, 2023
1 parent 90797e6 commit 4ce559f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 101 deletions.
82 changes: 1 addition & 81 deletions libs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion libs/gl-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ rustls-pemfile = "*"
sha256 = "1.1.4"
tokio = { version = "1", features = ["full"] }
tonic = { version = "^0.8", features = ["tls", "transport"] }
tor-rtcompat = {version="0.9.2"}
tower = { version = "0.4" }
rcgen = { version = "0.10.0", features = ["pem", "x509-parser"]}
tempfile = "3.3.0"
Expand Down
4 changes: 2 additions & 2 deletions libs/gl-client/src/lnurl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use pay::resolve_to_invoice;
pub struct LNURL;

impl LNURL {
pub async fn resolve_lnurl_to_invoice(&self, lnurl: &str, amount: u64) -> Result<String> {
pub async fn resolve_lnurl_to_invoice(&self, lnurl: &str, amount_msats: u64) -> Result<String> {
let http_client = LnUrlHttpClearnetClient::new();
resolve_to_invoice(http_client, lnurl, amount).await
resolve_to_invoice(http_client, lnurl, amount_msats).await
}
}
2 changes: 0 additions & 2 deletions libs/gl-client/src/lnurl/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub trait LnUrlHttpClient {
async fn get_pay_request_callback_response(
&self,
callback_url: &str,
amount: u64,
) -> Result<PayRequestCallbackResponse>;
}

Expand Down Expand Up @@ -72,7 +71,6 @@ impl LnUrlHttpClient for LnUrlHttpClearnetClient {
async fn get_pay_request_callback_response(
&self,
callback_url: &str,
amount: u64,
) -> Result<PayRequestCallbackResponse> {
self.get::<PayRequestCallbackResponse>(callback_url).await
}
Expand Down
29 changes: 14 additions & 15 deletions libs/gl-client/src/lnurl/pay/mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
use super::models;
use super::utils::parse_lnurl;
use crate::lnurl::models::{
LnUrlHttpClient, MockLnUrlHttpClient, PayRequestCallbackResponse, PayRequestResponse,
};
use crate::lnurl::models::{LnUrlHttpClient, PayRequestCallbackResponse, PayRequestResponse};
use anyhow::{anyhow, ensure, Result};
use futures::future;
use lightning_invoice::{Invoice, InvoiceDescription};
use log::debug;
use reqwest::Url;
use sha256;
use std::str::FromStr; // Import the future module
use std::str::FromStr;

pub async fn resolve_to_invoice<T: LnUrlHttpClient>(
http_client: T,
lnurl: &str,
amount: u64,
amount_msats: u64,
) -> Result<String> {
let url = parse_lnurl(lnurl)?;

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

validate_pay_request_response(&lnurl_pay_request_response, amount)?;
validate_pay_request_response(&lnurl_pay_request_response, amount_msats)?;
let description = extract_description(&lnurl_pay_request_response)?;

debug!("Domain: {}", Url::parse(&url).unwrap().host().unwrap());
Expand All @@ -31,13 +28,13 @@ pub async fn resolve_to_invoice<T: LnUrlHttpClient>(
lnurl_pay_request_response.min_sendable, lnurl_pay_request_response.max_sendable
);

let callback_url = build_callback_url(&lnurl_pay_request_response, amount)?;
let callback_url = build_callback_url(&lnurl_pay_request_response, amount_msats)?;
let callback_response: PayRequestCallbackResponse = http_client
.get_pay_request_callback_response(&callback_url, amount)
.get_pay_request_callback_response(&callback_url)
.await?;

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

Expand Down Expand Up @@ -108,7 +105,7 @@ fn validate_pay_request_response(
fn validate_invoice_from_callback_response(
invoice: &Invoice,
amount: u64,
lnurl_pay_request: PayRequestResponse,
lnurl_pay_request_response: PayRequestResponse,
) -> Result<()> {
ensure!(invoice.amount_milli_satoshis().unwrap_or_default() == amount ,
"Amount found in invoice was not equal to the amount found in the original request\nRequest amount: {}\nInvoice amount:{:?}", amount, invoice.amount_milli_satoshis().unwrap()
Expand All @@ -120,14 +117,16 @@ fn validate_invoice_from_callback_response(
};

ensure!(
description_hash == sha256::digest(lnurl_pay_request.metadata),
description_hash == sha256::digest(lnurl_pay_request_response.metadata),
"description_hash does not match the hash of the metadata"
);
Ok(())
}

#[cfg(test)]
mod tests {
use crate::lnurl::models::MockLnUrlHttpClient;
use futures::future;
use futures::future::Ready;
use std::pin::Pin;

Expand Down Expand Up @@ -160,7 +159,7 @@ mod tests {
convert_to_async_return_value(Ok(x))
});

mock_http_client.expect_get_pay_request_callback_response().returning(|_url, _amount| {
mock_http_client.expect_get_pay_request_callback_response().returning(|_url| {
let invoice = "lnbc1u1pjv9qrvsp5e5wwexctzp9yklcrzx448c68q2a7kma55cm67ruajjwfkrswnqvqpp55x6mmz8ch6nahrcuxjsjvs23xkgt8eu748nukq463zhjcjk4s65shp5dd6hc533r655wtyz63jpf6ja08srn6rz6cjhwsjuyckrqwanhjtsxqzjccqpjrzjqw6lfdpjecp4d5t0gxk5khkrzfejjxyxtxg5exqsd95py6rhwwh72rpgrgqq3hcqqgqqqqlgqqqqqqgq9q9qxpqysgq95njz4sz6h7r2qh7txnevcrvg0jdsfpe72cecmjfka8mw5nvm7tydd0j34ps2u9q9h6v5u8h3vxs8jqq5fwehdda6a8qmpn93fm290cquhuc6r";
let callback_response_json = format!("{{\"pr\":\"{}\",\"routes\":[]}}", invoice).to_string();
let x = serde_json::from_str(&callback_response_json).unwrap();
Expand Down Expand Up @@ -202,7 +201,7 @@ mod tests {
convert_to_async_return_value(Ok(x))
});

mock_http_client.expect_get_pay_request_callback_response().returning(|_url, _amount| {
mock_http_client.expect_get_pay_request_callback_response().returning(|_url| {
let invoice = "lnbc1u1pjv9qrvsp5e5wwexctzp9yklcrzx448c68q2a7kma55cm67ruajjwfkrswnqvqpp55x6mmz8ch6nahrcuxjsjvs23xkgt8eu748nukq463zhjcjk4s65shp5dd6hc533r655wtyz63jpf6ja08srn6rz6cjhwsjuyckrqwanhjtsxqzjccqpjrzjqw6lfdpjecp4d5t0gxk5khkrzfejjxyxtxg5exqsd95py6rhwwh72rpgrgqq3hcqqgqqqqlgqqqqqqgq9q9qxpqysgq95njz4sz6h7r2qh7txnevcrvg0jdsfpe72cecmjfka8mw5nvm7tydd0j34ps2u9q9h6v5u8h3vxs8jqq5fwehdda6a8qmpn93fm290cquhuc6r";
let callback_response_json = format!("{{\"pr\":\"{}\",\"routes\":[]}}", invoice).to_string();
let callback_response = serde_json::from_str(&callback_response_json).unwrap();
Expand Down Expand Up @@ -231,7 +230,7 @@ mod tests {
convert_to_async_return_value(Ok(x))
});

mock_http_client.expect_get_pay_request_callback_response().returning(|_url, _amount| {
mock_http_client.expect_get_pay_request_callback_response().returning(|_url| {
let invoice = "lnbc1u1pjv9qrvsp5e5wwexctzp9yklcrzx448c68q2a7kma55cm67ruajjwfkrswnqvqpp55x6mmz8ch6nahrcuxjsjvs23xkgt8eu748nukq463zhjcjk4s65shp5dd6hc533r655wtyz63jpf6ja08srn6rz6cjhwsjuyckrqwanhjtsxqzjccqpjrzjqw6lfdpjecp4d5t0gxk5khkrzfejjxyxtxg5exqsd95py6rhwwh72rpgrgqq3hcqqgqqqqlgqqqqqqgq9q9qxpqysgq95njz4sz6h7r2qh7txnevcrvg0jdsfpe72cecmjfka8mw5nvm7tydd0j34ps2u9q9h6v5u8h3vxs8jqq5fwehdda6a8qmpn93fm290cquhuc6r";
let callback_response_json = format!("{{\"pr\":\"{}\",\"routes\":[]}}", invoice).to_string();
let value = serde_json::from_str(&callback_response_json).unwrap();
Expand Down

0 comments on commit 4ce559f

Please sign in to comment.