Skip to content

Commit

Permalink
add fn to fetch invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
claddy committed Sep 30, 2024
1 parent f1c232c commit 4ee02fe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ panic = "abort"
[dependencies]
serde = { version = "1.0.0", features = ["derive"] }
serde_json = "1.0.0"
ureq = { version = "2.9.7", features = ["json", "native-tls"] }
ureq = { version = "2.5.0", features = ["json", "native-tls", "socks"] }
bip39 = "2.0.0"
electrum-client = "0.19.0"
bitcoin = {version = "0.31.2", features = ["rand", "base64", "rand-std"]}
Expand All @@ -34,6 +34,8 @@ log = "^0.4"
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
21 changes: 21 additions & 0 deletions src/util/lnurl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use lightning_invoice::Bolt11Invoice;
use lnurl::{
lnurl::LnUrl,
Builder,
LnUrlResponse
};
use std::error::Error;
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());
}
Ok(pay_result.invoice().to_string())
} else { panic!("Wrong response type"); }
}
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{error::Error, network::electrum::ElectrumConfig};

pub mod ec;
pub mod secrets;
mod lnurl;

/// Setup function that will only run once, even if called multiple times.

Expand Down

0 comments on commit 4ee02fe

Please sign in to comment.