Skip to content

Commit

Permalink
Add test for fetch_invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
claddyy committed Oct 1, 2024
1 parent 4ee02fe commit 17ac916
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ env_logger = "0.7"
native-tls = "0.2.11"
hex = "0.4"
lnurl-rs = "0.8.0"
reqwest = { version = "0.12.7", features = ["json"] }

[patch.crates-io]
secp256k1-zkp = {git = "https://github.com/BlockstreamResearch/rust-secp256k1-zkp.git", rev = "60e631c24588a0c9e271badd61959294848c665d"}

Expand Down
62 changes: 45 additions & 17 deletions src/util/lnurl.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
use crate::error::Error;
use lightning_invoice::Bolt11Invoice;
use lnurl::{
lnurl::LnUrl,
Builder,
LnUrlResponse
};
use std::error::Error;
use lnurl::{lnurl::LnUrl, Builder, LnUrlResponse};
use std::str::FromStr;
async fn fetch_invoice(lnurl_string: &str, amount_msats: u64) -> Result<String, dyn Error> {
let lnurl = LnUrl::from_str(lnurl_string)?;
let async_client = Builder::default().build_async()?;
let res = async_client.make_request(&lnurl_string).await?;
if let LnUrlResponse::LnUrlPayResponse(pay) = res {
let pay_result = async_client.get_invoice(&pay, amount_msats, None, None).await?;
let invoice = Bolt11Invoice::from_str(&pay_result.invoice())?;
if invoice.amount_milli_satoshis() != Some(amount_msats) {
return Err("Invoice amount doesn't match requested amount".into());

pub fn fetch_invoice(lnurl_string: &str, amount_msats: u64) -> Result<String, Error> {
let lnurl = LnUrl::from_str(lnurl_string).map_err(|e| Error::Generic(format!("Invalid LNURL: {}", e)))?;
let client = Builder::default().build_blocking().map_err(|e| Error::Generic(e.to_string()))?;
let res = client.make_request(&lnurl_string).map_err(|e| Error::HTTP(e.to_string()))?;

match res {
LnUrlResponse::LnUrlPayResponse(pay) => {
let pay_result = client
.get_invoice(&pay, amount_msats, None, None)
.map_err(|e| Error::HTTP(e.to_string()))?;
let invoice = Bolt11Invoice::from_str(pay_result.invoice())
.map_err(|e| Error::Bolt11(e))?;

if invoice.amount_milli_satoshis() != Some(amount_msats) {
return Err(Error::Generic("Invoice amount doesn't match requested amount".to_string()));
}

Ok(pay_result.invoice().to_string())
},
_ => Err(Error::Generic("Unexpected response type".to_string()))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_fetch_invoice() {
let lnurl_string = "LNURL1DP68GURN8GHJ7UM9WFMXJCM99E3K7MF0V9CXJ0M385EKVCENXC6R2C35XVUKXEFCV5MKVV34X5EKZD3EV56NYD3HXQURZEPEXEJXXEPNXSCRVWFNV9NXZCN9XQ6XYEFHVGCXXCMYXYMNSERXFQ5FNS";
let amount_msats = 100000;
let result = fetch_invoice(lnurl_string, amount_msats);

match result {
Ok(invoice) => {
assert!(!invoice.is_empty(), "Invoice should not be empty");
assert!(invoice.starts_with("lnbc"), "Invoice should start with 'lnbc'");
},
Err(e) => {
println!("Error occurred: {}. This test may fail if not connected to the internet or if the LNURL is invalid.", e.message());
}
}
Ok(pay_result.invoice().to_string())
} else { panic!("Wrong response type"); }
}
}

0 comments on commit 17ac916

Please sign in to comment.