-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
45 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -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"); } | ||
} | ||
} |