Skip to content

Commit

Permalink
Add RPC backend
Browse files Browse the repository at this point in the history
Expose the RPC backend feature via cli arg options.
RPC backend can be connected via all default parameters
without specifying any arg options.
  • Loading branch information
rajarshimaitra committed Aug 17, 2021
1 parent a672f34 commit fc9e910
Show file tree
Hide file tree
Showing 2 changed files with 274 additions and 31 deletions.
82 changes: 74 additions & 8 deletions src/bdk_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
use std::fs;
use std::path::PathBuf;

#[cfg(feature = "rpc")]
use bitcoin::secp256k1::Secp256k1;
use bitcoin::Network;
use clap::AppSettings;
use log::{debug, error, info, warn};
Expand All @@ -43,9 +45,17 @@ use bdk::blockchain::electrum::ElectrumBlockchainConfig;
#[cfg(feature = "esplora")]
use bdk::blockchain::esplora::EsploraBlockchainConfig;

#[cfg(any(feature = "electrum", feature = "esplora", feature = "compact_filters"))]
#[cfg(any(
feature = "electrum",
feature = "esplora",
feature = "compact_filters",
feature = "rpc"
))]
use bdk::blockchain::{AnyBlockchain, AnyBlockchainConfig, ConfigurableBlockchain};

#[cfg(feature = "rpc")]
use bdk::blockchain::rpc::{wallet_name_from_descriptor, Auth, RpcConfig};

use bdk::database::BatchDatabase;
use bdk::sled;
use bdk::sled::Tree;
Expand All @@ -54,7 +64,12 @@ use bdk::{bitcoin, Error};
use bdk_cli::WalletSubCommand;
use bdk_cli::{CliOpts, CliSubCommand, KeySubCommand, OfflineWalletSubCommand, WalletOpts};

#[cfg(any(feature = "electrum", feature = "esplora", feature = "compact_filters"))]
#[cfg(any(
feature = "electrum",
feature = "esplora",
feature = "compact_filters",
feature = "rpc"
))]
use bdk_cli::OnlineWalletSubCommand;

#[cfg(feature = "repl")]
Expand All @@ -69,7 +84,12 @@ const REPL_LINE_SPLIT_REGEX: &str = r#""([^"]*)"|'([^']*)'|([\w\-]+)"#;
version = option_env ! ("CARGO_PKG_VERSION").unwrap_or("unknown"),
author = option_env ! ("CARGO_PKG_AUTHORS").unwrap_or(""))]
pub enum ReplSubCommand {
#[cfg(any(feature = "electrum", feature = "esplora", feature = "compact_filters"))]
#[cfg(any(
feature = "electrum",
feature = "esplora",
feature = "compact_filters",
feature = "rpc"
))]
#[structopt(flatten)]
OnlineWalletSubCommand(OnlineWalletSubCommand),
#[structopt(flatten)]
Expand Down Expand Up @@ -107,7 +127,12 @@ fn open_database(wallet_opts: &WalletOpts) -> Tree {
tree
}

#[cfg(any(feature = "electrum", feature = "esplora", feature = "compact_filters"))]
#[cfg(any(
feature = "electrum",
feature = "esplora",
feature = "compact_filters",
feature = "rpc"
))]
fn new_online_wallet<D>(
network: Network,
wallet_opts: &WalletOpts,
Expand All @@ -122,6 +147,7 @@ where
socks5: wallet_opts.proxy_opts.proxy.clone(),
retry: wallet_opts.proxy_opts.retries,
timeout: wallet_opts.electrum_opts.timeout,
stop_gap: 20,
});

#[cfg(feature = "esplora")]
Expand Down Expand Up @@ -151,6 +177,34 @@ where
})
};

#[cfg(feature = "rpc")]
let config: AnyBlockchainConfig = {
let auth = Auth::UserPass {
username: wallet_opts.rpc_opts.auth.0.clone(),
password: wallet_opts.rpc_opts.auth.1.clone(),
};

// Use deterministic wallet name derived from descriptor
let wallet_name = wallet_name_from_descriptor(
&wallet_opts.descriptor[..],
wallet_opts.change_descriptor.as_deref(),
network,
&Secp256k1::new(),
)?;

let mut rpc_url = "http://".to_string();
rpc_url.push_str(&wallet_opts.rpc_opts.address[..]);

let rpc_config = RpcConfig {
url: rpc_url,
auth,
network,
wallet_name,
skip_blocks: wallet_opts.rpc_opts.skip_blocks,
};

AnyBlockchainConfig::Rpc(rpc_config)
};
let descriptor = wallet_opts.descriptor.as_str();
let change_descriptor = wallet_opts.change_descriptor.as_deref();

Expand Down Expand Up @@ -202,7 +256,12 @@ fn main() {

fn handle_command(cli_opts: CliOpts, network: Network) -> Result<String, Error> {
let result = match cli_opts.subcommand {
#[cfg(any(feature = "electrum", feature = "esplora", feature = "compact_filters"))]
#[cfg(any(
feature = "electrum",
feature = "esplora",
feature = "compact_filters",
feature = "rpc"
))]
CliSubCommand::Wallet {
wallet_opts,
subcommand: WalletSubCommand::OnlineWalletSubCommand(online_subcommand),
Expand Down Expand Up @@ -243,13 +302,19 @@ fn handle_command(cli_opts: CliOpts, network: Network) -> Result<String, Error>
CliSubCommand::Repl { wallet_opts } => {
let database = open_database(&wallet_opts);

#[cfg(any(feature = "electrum", feature = "esplora", feature = "compact_filters"))]
#[cfg(any(
feature = "electrum",
feature = "esplora",
feature = "compact_filters",
feature = "rpc"
))]
let wallet = new_online_wallet(network, &wallet_opts, database)?;

#[cfg(not(any(
feature = "electrum",
feature = "esplora",
feature = "compact_filters"
feature = "compact_filters",
feature = "rpc"
)))]
let wallet = new_offline_wallet(network, &wallet_opts, database)?;

Expand Down Expand Up @@ -295,7 +360,8 @@ fn handle_command(cli_opts: CliOpts, network: Network) -> Result<String, Error>
#[cfg(any(
feature = "electrum",
feature = "esplora",
feature = "compact_filters"
feature = "compact_filters",
feature = "rpc"
))]
ReplSubCommand::OnlineWalletSubCommand(online_subcommand) => {
bdk_cli::handle_online_wallet_subcommand(&wallet, online_subcommand)
Expand Down
Loading

0 comments on commit fc9e910

Please sign in to comment.