From 2be7b1eccfac2d35f4603886fad24b09f05a2740 Mon Sep 17 00:00:00 2001 From: Yuki Kishimoto Date: Fri, 19 Jan 2024 20:30:16 +0100 Subject: [PATCH] Fix `api::get_invoice` func --- Cargo.lock | 95 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 7 +++ examples/get-invoice.rs | 11 +++++ src/api.rs | 9 +++- 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 examples/get-invoice.rs diff --git a/Cargo.lock b/Cargo.lock index 5754bde..b2813d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -216,6 +216,12 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +[[package]] +name = "hermit-abi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d3d0e0f38255e7fa3cf31335b3a56f05febd18025f4db5ef7a0cfb4f8da651f" + [[package]] name = "http" version = "0.2.11" @@ -343,6 +349,17 @@ dependencies = [ "reqwest", "serde", "serde_json", + "tokio", +] + +[[package]] +name = "lock_api" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" +dependencies = [ + "autocfg", + "scopeguard", ] [[package]] @@ -383,6 +400,16 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "object" version = "0.32.2" @@ -398,6 +425,29 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + [[package]] name = "percent-encoding" version = "2.3.1" @@ -434,6 +484,15 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa" +dependencies = [ + "bitflags", +] + [[package]] name = "reqwest" version = "0.11.23" @@ -532,6 +591,12 @@ version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c" +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "sct" version = "0.7.1" @@ -585,6 +650,15 @@ dependencies = [ "serde", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "slab" version = "0.4.9" @@ -594,6 +668,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "smallvec" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" + [[package]] name = "socket2" version = "0.5.5" @@ -687,11 +767,26 @@ dependencies = [ "bytes", "libc", "mio", + "num_cpus", + "parking_lot", "pin-project-lite", + "signal-hook-registry", "socket2", + "tokio-macros", "windows-sys", ] +[[package]] +name = "tokio-macros" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tokio-rustls" version = "0.24.1" diff --git a/Cargo.toml b/Cargo.toml index 6c6f409..310a8a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,3 +20,10 @@ bech32 = { version = "0.9", default-features = false } reqwest = { version = "0.11", default-features = false, features = ["json", "rustls-tls", "socks"], optional = true } serde = { version = "1.0", default-features = false, features = ["alloc", "derive"] } serde_json = { version = "1.0", default-features = false, features = ["alloc"] } + +[dev-dependencies] +tokio = { version = "1", features = ["full"] } + +[[example]] +name = "get-invoice" +required-features = ["api"] diff --git a/examples/get-invoice.rs b/examples/get-invoice.rs new file mode 100644 index 0000000..5e21262 --- /dev/null +++ b/examples/get-invoice.rs @@ -0,0 +1,11 @@ +// Copyright (c) 2024 Yuki Kishimoto +// Distributed under the MIT software license + +use lnurl_pay::{api, LightningAddress}; + +#[tokio::main] +async fn main() { + let addr = LightningAddress::parse("yuki@getalby.com").unwrap(); + let invoice = api::get_invoice(addr, 1 * 1000, None, None).await.unwrap(); + println!("{invoice}"); +} diff --git a/src/api.rs b/src/api.rs index a7b02bb..2ea3047 100644 --- a/src/api.rs +++ b/src/api.rs @@ -47,6 +47,12 @@ struct PayResponse { // pub nostr_pubkey: Option, } +#[derive(Serialize, Deserialize)] +struct LnURLPayInvoice { + /// Encoded bolt 11 invoice + pr: String, +} + pub enum Lud06OrLud16 { Lud06(LnUrl), Lud16(LightningAddress), @@ -119,5 +125,6 @@ where None => format!("{}{}amount={}", pay_response.callback, symbol, msats), }; let resp = client.get(&url).send().await?; - Ok(resp.error_for_status()?.json().await?) + let invoice: LnURLPayInvoice = resp.error_for_status()?.json().await?; + Ok(invoice.pr) }