Skip to content

Commit

Permalink
wip add bitcoind backend
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguida committed Nov 9, 2023
1 parent 1bab35f commit a37c8c7
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 17 deletions.
109 changes: 100 additions & 9 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ bdk = { git = "https://github.com/bitcoindevkit/bdk", version = "1.0.0-alpha.1",
bdk_esplora = { git = "https://github.com/bitcoindevkit/bdk", rev = "8f38e96e4542db2378e2e64cd9289638ee86ba1a" }
# bdk_file_store = { version = "0.2.0" }
# bdk_file_store = { path = "../bdk/crates/file_store" }
bdk_file_store ={ git = "https://github.com/bitcoindevkit/bdk", rev = "8f38e96e4542db2378e2e64cd9289638ee86ba1a" }
bdk_file_store = { git = "https://github.com/bitcoindevkit/bdk", rev = "8f38e96e4542db2378e2e64cd9289638ee86ba1a" }
bitcoincore-rpc = { path = "../rust-bitcoincore-rpc/client" }
clap = { version = "4.4.0", features = ["derive"] }
cln-plugin = { git = "https://github.com/elementsproject/lightning", version = "0.1.4" }
# cln-plugin = { path = "../../lightning/plugins" }
Expand Down
74 changes: 67 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#[macro_use]
extern crate serde_json;

use bitcoincore_rpc::bitcoincore_rpc_json::ScanBlocksRequest;
use clap::error::ErrorKind;
use clap::{CommandFactory, Parser, Subcommand};
use cln_rpc::model::DatastoreMode;
Expand All @@ -27,18 +28,57 @@ use smaug::wallet::{AddArgs, DescriptorWallet, SMAUG_DATADIR, UTXO_DEPOSIT_TAG,
use cln_plugin::{anyhow, messages, options, Builder, Error, Plugin};
use tokio;

use bdk::TransactionDetails;
use bdk::{TransactionDetails, descriptor};
use smaug::state::{Smaug, State};


fn scanblocks<'a>(
brpc_user: String,
brpc_pass: String,
) -> Result<(), Error>
{
// let external_descriptor = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/0'/0'/0/*)";
// mutinynet_descriptor = "wpkh(tprv8ZgxMBicQKsPdSAgthqLZ5ZWQkm5As4V3qNA5G8KKxGuqdaVVtBhytrUqRGPm4RxTktSdvch8JyUdfWR8g3ddrC49WfZnj4iGZN8y5L8NPZ/*)"
let _mutinynet_descriptor_ext = "wpkh(tprv8ZgxMBicQKsPdSAgthqLZ5ZWQkm5As4V3qNA5G8KKxGuqdaVVtBhytrUqRGPm4RxTktSdvch8JyUdfWR8g3ddrC49WfZnj4iGZN8y5L8NPZ/84'/0'/0'/0/*)";
let _mutinynet_descriptor_int = "wpkh(tprv8ZgxMBicQKsPdSAgthqLZ5ZWQkm5As4V3qNA5G8KKxGuqdaVVtBhytrUqRGPm4RxTktSdvch8JyUdfWR8g3ddrC49WfZnj4iGZN8y5L8NPZ/84'/0'/0'/1/*)";
let _mutinynet_descriptor_ext_2 = "wpkh(tprv8ZgxMBicQKsPeRye8MhHA8hLxMuomycmGYXyRs7zViNck2VJsCJMTPt81Que8qp3PyPgQRnN7Gb1JyBVBKgj8AKEoEmmYxYDwzZJ63q1yjA/84'/0'/0'/0/*)";
let _mutinynet_descriptor_int_2 = "wpkh(tprv8ZgxMBicQKsPeRye8MhHA8hLxMuomycmGYXyRs7zViNck2VJsCJMTPt81Que8qp3PyPgQRnN7Gb1JyBVBKgj8AKEoEmmYxYDwzZJ63q1yjA/84'/0'/0'/1/*)";

extern crate bitcoincore_rpc;

use bitcoincore_rpc::{Auth, Client, RpcApi};

let rpc = Client::new("http://localhost:18443",
Auth::UserPass(brpc_user, brpc_pass)
// Auth::CookieFile(PathBuf::from("/home/cguida/.bitcoin/regtest/.cookie"))
).unwrap();
let descriptor = ScanBlocksRequest::Extended { desc:_mutinynet_descriptor_ext.to_string(), range: None };
let descriptors = &[descriptor];
let res = rpc.scan_blocks_blocking(descriptors);
log::info!("scanblocks result: {:?}", res.unwrap());

return Ok(());
}

#[tokio::main]
// #[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), anyhow::Error> {
let builder = Builder::new(tokio::io::stdin(), tokio::io::stdout())
.option(options::ConfigOption::new(
"wd_network",
"smaug_network",
options::Value::OptString,
"Which network to use: [bitcoin, testnet, signet, regtest, mutinynet]",
))
.option(options::ConfigOption::new(
"smaug_brpc_user",
options::Value::OptString,
"Bitcoind RPC user (Required)",
))
.option(options::ConfigOption::new(
"smaug_brpc_pass",
options::Value::OptString,
"Bitcoind RPC password (Required)",
))
.notification(messages::NotificationTopic::new(UTXO_DEPOSIT_TAG))
.notification(messages::NotificationTopic::new(UTXO_SPENT_TAG))
.rpcmethod(
Expand All @@ -58,17 +98,31 @@ async fn main() -> Result<(), anyhow::Error> {
configured_plugin.configuration()
);
log::debug!(
"wd_network = {:?}, cln_network = {}",
configured_plugin.option("wd_network"),
"smaug_network = {:?}, cln_network = {}",
configured_plugin.option("smaug_network"),
configured_plugin.configuration().network
);
let network = match configured_plugin.option("wd_network") {
Some(wd_network) => match wd_network.as_str() {
let network = match configured_plugin.option("smaug_network") {
Some(smaug_network) => match smaug_network.as_str() {
Some(wdn) => wdn.to_owned(),
None => configured_plugin.configuration().network,
},
None => configured_plugin.configuration().network,
};
let brpc_user = match configured_plugin.option("smaug_brpc_user") {
Some(smaug_brpc_user) => match smaug_brpc_user.as_str() {
Some(wdn) => wdn.to_owned(),
None => {return Err(anyhow!("must specify smaug_brpc_user"))},
},
None => {return Err(anyhow!("must specify smaug_brpc_user (your bitcoind instance rpcuser)"))},
};
let brpc_pass = match configured_plugin.option("smaug_brpc_pass") {
Some(smaug_brpc_pass) => match smaug_brpc_pass.as_str() {
Some(wdn) => wdn.to_owned(),
None => {return Err(anyhow!("must specify smaug_brpc_pass (your bitcoind instance rpcpassword)"))},
},
None => {return Err(anyhow!("must specify smaug_brpc_user"))},
};
let ln_dir: PathBuf = configured_plugin.configuration().lightning_dir.into();
// Create data dir if it does not exist
fs::create_dir_all(ln_dir.join(SMAUG_DATADIR)).unwrap_or_else(|e| {
Expand Down Expand Up @@ -105,6 +159,8 @@ async fn main() -> Result<(), anyhow::Error> {
let watch_descriptor = Smaug {
wallets,
network: network.clone(),
brpc_user: brpc_user.clone(),
brpc_pass: brpc_pass.clone(),
db_dir: ln_dir.join(SMAUG_DATADIR),
};
let plugin_state = Arc::new(Mutex::new(watch_descriptor.clone()));
Expand Down Expand Up @@ -276,7 +332,11 @@ async fn list(
plugin: Plugin<State>,
// _v: serde_json::Value,
) -> Result<serde_json::Value, Error> {
let wallets = &plugin.state().lock().await.wallets;
let state = &plugin.state().lock().await;
let brpc_user = state.brpc_user.clone();
let brpc_pass = state.brpc_pass.clone();
let wallets = state.wallets.clone();
scanblocks(brpc_user, brpc_pass)?;
let mut result = BTreeMap::<String, ListResponseItem>::new();
for (wallet_name, wallet) in wallets {
result.insert(
Expand Down
6 changes: 6 additions & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub struct Smaug {
pub wallets: BTreeMap<String, DescriptorWallet>,
// The network relevant to our wallets
pub network: String,
// Bitcoind RPC user
pub brpc_user: String,
// Bitcoind RPC password
pub brpc_pass: String,
// The db path relevant to our wallets
pub db_dir: PathBuf,
}
Expand All @@ -22,6 +26,8 @@ impl Smaug {
Self {
wallets: BTreeMap::new(),
network: bitcoin::Network::Bitcoin.to_string(),
brpc_user: String::from("bitcoin"),
brpc_pass: String::from("password"),
db_dir: PathBuf::new(),
}
}
Expand Down

0 comments on commit a37c8c7

Please sign in to comment.