Skip to content

Commit

Permalink
Draft RPC backend implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rajarshimaitra committed Aug 3, 2021
1 parent 378b33a commit 41171ae
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 82 deletions.
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ readme = "README.md"
license = "MIT"

[dependencies]
bdk = { version = "^0.7", default-features = false, features = ["all-keys"]}
#bdk = { version = "^0.7", default-features = false, features = ["all-keys"]}
bdk = { git = "https://github.com/bitcoindevkit/bdk", barnch = "master", default-features = false, features = ["all-keys"]}
bdk-macros = "^0.4"
structopt = "^0.3"
serde_json = { version = "^1.0" }
Expand All @@ -31,9 +32,11 @@ default = ["repl", "electrum"]
repl = ["bdk/key-value-db", "clap", "dirs-next", "env_logger", "regex", "rustyline"]
electrum = ["bdk/electrum"]
esplora = ["bdk/esplora"]
rpc = ["bdk/rpc"]
compact_filters = ["bdk/compact_filters"]
compiler = ["bdk/compiler"]
async-interface = ["bdk/async-interface"]
compact_filters = ["bdk/compact_filters"]


[[bin]]
name = "bdk-cli"
Expand Down
52 changes: 47 additions & 5 deletions src/bdk_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
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};
use rustyline::error::ReadlineError;
Expand All @@ -36,9 +39,15 @@ use structopt::StructOpt;
use bdk::blockchain::compact_filters::{BitcoinPeerConfig, CompactFiltersBlockchainConfig};
#[cfg(feature = "esplora")]
use bdk::blockchain::esplora::EsploraBlockchainConfig;
use bdk::blockchain::{
AnyBlockchain, AnyBlockchainConfig, ConfigurableBlockchain, ElectrumBlockchainConfig,
};

#[cfg(feature = "electrum")]
use bdk::blockchain::ElectrumBlockchainConfig;

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 Down Expand Up @@ -118,13 +127,14 @@ where
#[cfg(not(feature = "esplora"))]
let config_esplora = None;

#[cfg(feature = "electrum")]
let config_electrum = AnyBlockchainConfig::Electrum(ElectrumBlockchainConfig {
url: wallet_opts.electrum_opts.electrum.clone(),
socks5: wallet_opts.proxy_opts.proxy.clone(),
retry: wallet_opts.proxy_opts.retries,
timeout: wallet_opts.electrum_opts.timeout,
stop_gap: 20,
});

#[cfg(feature = "compact_filters")]
let config_compact_filters: Option<AnyBlockchainConfig> = {
let mut peers = vec![];
Expand All @@ -151,8 +161,40 @@ where
#[cfg(not(feature = "compact_filters"))]
let config_compact_filters = None;

#[cfg(feature = "rpc")]
let config_rpc: Option<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 rpc_config = RpcConfig {
url: wallet_opts.rpc_opts.address.clone(),
auth,
network,
wallet_name: wallet_name,
skip_blocks: wallet_opts.rpc_opts.skip_blocks,
};

Some(AnyBlockchainConfig::Rpc(rpc_config))
};

#[cfg(not(feature = "rpc"))]
let config_rpc = None;

// Fall back to Electrum config if Esplora or Compact Filter config isn't provided
let config = config_esplora
// Both rpc and electrum can be connected by default without any config options.
// if rpc is enabled, then it trumps over electrum.
let config = config_rpc
.or(config_esplora)
.or(config_compact_filters)
.unwrap_or(config_electrum);

Expand Down
Loading

0 comments on commit 41171ae

Please sign in to comment.