Skip to content

Commit

Permalink
Use LND's on-chain wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Apr 24, 2024
1 parent 49d3a01 commit 3d1718a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 40 deletions.
6 changes: 1 addition & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ use axum::{
routing::{get, post},
Extension, Json, Router,
};
use bitcoincore_rpc::Client;
use lnurl::withdraw::WithdrawalResponse;
use lnurl::{AsyncClient, Tag};
use log::error;
use nostr::key::Keys;
use serde::Deserialize;
use serde_json::{json, Value};
use std::{net::SocketAddr, sync::Arc};
use std::net::SocketAddr;
use tokio::signal::unix::{signal, SignalKind};
use tokio::sync::oneshot;
use tonic_openssl_lnd::LndLightningClient;
Expand All @@ -39,7 +38,6 @@ pub struct AppState {
keys: Keys,
network: bitcoin::Network,
lightning_client: LndLightningClient,
bitcoin_client: Arc<Client>,
lnurl: AsyncClient,
}

Expand All @@ -48,7 +46,6 @@ impl AppState {
host: String,
keys: Keys,
lightning_client: LndLightningClient,
bitcoin_client: Client,
network: bitcoin::Network,
) -> Self {
let lnurl = lnurl::Builder::default().build_async().unwrap();
Expand All @@ -57,7 +54,6 @@ impl AppState {
keys,
network,
lightning_client,
bitcoin_client: Arc::new(bitcoin_client),
lnurl,
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/nostr_dms.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{AppState, MAX_SEND_AMOUNT};
use bitcoin::Amount;
use bitcoin_waila::PaymentParams;
use bitcoincore_rpc::RpcApi;
use lightning_invoice::Bolt11Invoice;
use lnurl::lightning_address::LightningAddress;
use lnurl::lnurl::LnUrl;
Expand Down Expand Up @@ -182,9 +181,18 @@ async fn handle_event(event: Event, state: AppState) -> anyhow::Result<()> {
return Err(anyhow::anyhow!("Amount exceeds max send amount"));
}

let txid = state
.bitcoin_client
.send_to_address(&address, amount, None, None, None, None, None, None)?;
let resp = {
let mut wallet_client = state.lightning_client.clone();
let req = tonic_openssl_lnd::lnrpc::SendCoinsRequest {
addr: address.to_string(),
amount: amount.to_sat() as i64,
spend_unconfirmed: true,
..Default::default()
};
wallet_client.send_coins(req).await?.into_inner()
};

let txid = resp.txid;

info!("Sent onchain tx: {txid}");
return Ok(());
Expand Down
19 changes: 11 additions & 8 deletions src/onchain.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use bitcoin::{Address, Amount};
use bitcoin_waila::PaymentParams;
use bitcoincore_rpc::RpcApi;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use tokio::task;

use crate::{AppState, MAX_SEND_AMOUNT};

Expand Down Expand Up @@ -43,8 +41,6 @@ pub async fn pay_onchain(
)
};

let bitcoin_client = state.bitcoin_client.clone();

let amount = params
.amount()
.or(payload.sats.map(Amount::from_sat))
Expand All @@ -54,12 +50,19 @@ pub async fn pay_onchain(
anyhow::bail!("max amount is 10,000,000");
}

let txid = task::block_in_place(|| {
bitcoin_client.send_to_address(&address, amount, None, None, None, None, None, None)
})?;
let resp = {
let mut wallet_client = state.lightning_client.clone();
let req = tonic_openssl_lnd::lnrpc::SendCoinsRequest {
addr: address.to_string(),
amount: amount.to_sat() as i64,
spend_unconfirmed: true,
..Default::default()
};
wallet_client.send_coins(req).await?.into_inner()
};

OnchainResponse {
txid: txid.to_string(),
txid: resp.txid,
address: address.to_string(),
}
};
Expand Down
26 changes: 3 additions & 23 deletions src/setup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bitcoincore_rpc::{Auth, Client, RpcApi};
use nostr::key::Keys;
use std::env;

use nostr::key::Keys;
use tonic_openssl_lnd::lnrpc;

use crate::AppState;
Expand Down Expand Up @@ -58,25 +58,5 @@ pub async fn setup() -> anyhow::Result<AppState> {
lightning_client
};

// Setup bitcoin rpc stuff
let bitcoin_client = {
let url = env::var("BITCOIN_RPC_HOST_AND_PORT").expect("missing BITCOIN_RPC_HOST_AND_PORT");
let user = env::var("BITCOIN_RPC_USER").expect("missing BITCOIN_RPC_USER");
let pass = env::var("BITCOIN_RPC_PASSWORD").expect("missing BITCOIN_RPC_PASSWORD");
let rpc =
Client::new(&url, Auth::UserPass(user, pass)).expect("failed to create RPC client");

// Make sure we can get info at startup
let _blockchain_info = rpc.get_blockchain_info();

rpc
};

Ok(AppState::new(
host,
keys,
lightning_client,
bitcoin_client,
network,
))
Ok(AppState::new(host, keys, lightning_client, network))
}

0 comments on commit 3d1718a

Please sign in to comment.