From c5a6aeb067a49d5d2cc0f3340574c7a0f8ca0ba5 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Thu, 20 Jun 2024 19:46:56 +0100 Subject: [PATCH] Added a working btc-rpc client, works with a running local signet node --- side-node/src/clients/btc.rs | 14 -------------- side-node/src/clients/btc_rpc.rs | 27 +++++++++++++++++++++++++++ side-node/src/clients/mod.rs | 2 +- side-node/src/lib.rs | 2 +- 4 files changed, 29 insertions(+), 16 deletions(-) delete mode 100644 side-node/src/clients/btc.rs create mode 100644 side-node/src/clients/btc_rpc.rs diff --git a/side-node/src/clients/btc.rs b/side-node/src/clients/btc.rs deleted file mode 100644 index dc5359c..0000000 --- a/side-node/src/clients/btc.rs +++ /dev/null @@ -1,14 +0,0 @@ -use bitcoincore_rpc::{Auth, Client, RpcApi}; - -pub fn client() { - let rpc = Client::new( - "http://localhost:8332", - Auth::UserPass( - "".to_string(), - "".to_string(), - ), - ) - .unwrap(); - let best_block_hash = rpc.get_best_block_hash().unwrap(); - println!("best block hash: {}", best_block_hash); -} diff --git a/side-node/src/clients/btc_rpc.rs b/side-node/src/clients/btc_rpc.rs new file mode 100644 index 0000000..b7a9166 --- /dev/null +++ b/side-node/src/clients/btc_rpc.rs @@ -0,0 +1,27 @@ +use std::str::FromStr; + +use bitcoincore_rpc::{Auth, Client, RpcApi}; + +fn parse_and_validate_address( + address: &str, + network: bitcoin::Network, +) -> Result { + let address = address + .parse::>()? + .require_network(network)?; + Ok(address) +} + +pub fn client() { + let rpc = Client::new( + "http://127.0.0.1:38332", + Auth::UserPass("dave".to_string(), "password".to_string()), + ) + .unwrap(); + + let a = "tb1p4dvr6dzszagw34scnr6kde0dr2yrmu6gew9faza788rmqy3d645sdm9e50"; + let address = parse_and_validate_address(a, bitcoin::Network::Signet).unwrap(); + let info = rpc.get_address_info(&address).unwrap(); + + println!("info {info:?} "); +} diff --git a/side-node/src/clients/mod.rs b/side-node/src/clients/mod.rs index e0c6d71..3190f48 100644 --- a/side-node/src/clients/mod.rs +++ b/side-node/src/clients/mod.rs @@ -1,2 +1,2 @@ -pub mod btc; +pub mod btc_rpc; pub mod websocket; diff --git a/side-node/src/lib.rs b/side-node/src/lib.rs index 149b3de..266018f 100644 --- a/side-node/src/lib.rs +++ b/side-node/src/lib.rs @@ -31,7 +31,7 @@ pub async fn run() { node.start().await; } Some(Commands::Btc {}) => { - println!("BTC command not yet implemented."); + clients::btc_rpc::client(); } None => println!("No command provided. Exiting. See --help for more information."), }