Skip to content

Commit

Permalink
Pay to lnurls
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Mar 26, 2024
1 parent 308ed86 commit 2d9b844
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
31 changes: 4 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 17 additions & 1 deletion src/lightning.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -28,8 +30,22 @@ pub async fn pay_lightning(state: AppState, bolt11: &str) -> anyhow::Result<Stri
} else {
anyhow::bail!("bolt11 invoice should have an amount");
}
} else if let Some(lnurl) = params.lnurl() {
match state.lnurl.make_request(&lnurl.url).await? {
LnUrlResponse::LnUrlPayResponse(pay) => {
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 = {
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,6 +40,7 @@ pub struct AppState {
network: bitcoin::Network,
lightning_client: LndLightningClient,
bitcoin_client: Arc<Client>,
lnurl: AsyncClient,
}

impl AppState {
Expand All @@ -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,
}
}
}
Expand Down

0 comments on commit 2d9b844

Please sign in to comment.