From 2d9b84477743c7d41ab2cb77d95579bc25b2b54c Mon Sep 17 00:00:00 2001 From: benthecarman Date: Tue, 26 Mar 2024 13:17:08 -0500 Subject: [PATCH] Pay to lnurls --- Cargo.lock | 31 ++++--------------------------- Cargo.toml | 2 +- src/lightning.rs | 18 +++++++++++++++++- src/main.rs | 5 ++++- 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 57e2f52..8ed9373 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -369,8 +369,8 @@ dependencies = [ "bip21", "bitcoin", "itertools 0.12.0", - "lightning 0.0.119", - "lightning-invoice 0.27.0", + "lightning", + "lightning-invoice", "lnurl-rs", "nostr 0.26.2", "url", @@ -1250,16 +1250,6 @@ dependencies = [ "hex-conservative", ] -[[package]] -name = "lightning" -version = "0.0.121" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b0c1f811ae288f86c6767055c55b5f7a721ca1e61bf1897a9ae2ec663e8aba1" -dependencies = [ - "bitcoin", - "hex-conservative", -] - [[package]] name = "lightning-invoice" version = "0.27.0" @@ -1268,20 +1258,7 @@ checksum = "fd3421ccfaeac77763114f68f3fc5308f11a0e8a880618b4f84d1d22493aa465" dependencies = [ "bech32", "bitcoin", - "lightning 0.0.119", - "num-traits", - "secp256k1", -] - -[[package]] -name = "lightning-invoice" -version = "0.29.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b186aca4a605d4db3b85979922be287b9ebd5dedd8132963bb9dbeb8f7d2a04" -dependencies = [ - "bech32", - "bitcoin", - "lightning 0.0.121", + "lightning", "num-traits", "secp256k1", ] @@ -1425,7 +1402,7 @@ dependencies = [ "bitcoincore-rpc", "dotenv", "hex", - "lightning-invoice 0.29.0", + "lightning-invoice", "lnurl-rs", "log", "nostr 0.29.0", diff --git a/Cargo.toml b/Cargo.toml index 3db9bfb..1e0ddf8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ lnurl-rs = "0.4.0" hex = "0.4.3" bitcoincore-rpc = "0.17.0" bitcoin = "0.30.2" -lightning-invoice = "0.29.0" +lightning-invoice = "0.27.0" tower-http = { version = "0.4.0", features = ["cors"] } log = "0.4.20" pretty_env_logger = "0.5.0" diff --git a/src/lightning.rs b/src/lightning.rs index 5a826ea..65cfe24 100644 --- a/src/lightning.rs +++ b/src/lightning.rs @@ -1,6 +1,8 @@ use serde::{Deserialize, Serialize}; use bitcoin_waila::PaymentParams; +use lightning_invoice::Bolt11Invoice; +use lnurl::LnUrlResponse; use std::str::FromStr; use tonic_openssl_lnd::lnrpc; @@ -28,8 +30,22 @@ pub async fn pay_lightning(state: AppState, bolt11: &str) -> anyhow::Result { + if pay.min_sendable > MAX_SEND_AMOUNT { + anyhow::bail!("max amount is 10,000,000"); + } + let inv = state + .lnurl + .get_invoice(&pay, pay.min_sendable, None, None) + .await?; + Bolt11Invoice::from_str(inv.invoice())? + } + _ => anyhow::bail!("invalid lnurl"), + } } else { - anyhow::bail!("invalid bolt11"); + anyhow::bail!("invalid bolt11") }; let payment_preimage = { diff --git a/src/main.rs b/src/main.rs index 62bcea6..8fa6446 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use axum::{ }; use bitcoincore_rpc::Client; use lnurl::withdraw::WithdrawalResponse; -use lnurl::Tag; +use lnurl::{AsyncClient, Tag}; use log::error; use nostr::key::Keys; use serde::Deserialize; @@ -40,6 +40,7 @@ pub struct AppState { network: bitcoin::Network, lightning_client: LndLightningClient, bitcoin_client: Arc, + lnurl: AsyncClient, } impl AppState { @@ -50,12 +51,14 @@ impl AppState { bitcoin_client: Client, network: bitcoin::Network, ) -> Self { + let lnurl = lnurl::Builder::default().build_async().unwrap(); AppState { host, keys, network, lightning_client, bitcoin_client: Arc::new(bitcoin_client), + lnurl, } } }