Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC backend implementation #36

Merged
merged 5 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Add RPC backend support, after bdk v0.12.0 release
- Update default feature to not include electrum
- Upgrade to `bdk` v0.12.x
- Add top level command "Compile" which compiles a miniscript policy to an output descriptor
Expand Down
45 changes: 41 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ esplora-ureq = ["esplora", "bdk/use-esplora-ureq"]
esplora-reqwest = ["esplora", "bdk/use-esplora-reqwest"]
compiler = ["bdk/compiler"]
compact_filters = ["bdk/compact_filters"]
rpc = ["bdk/rpc"]

[[bin]]
name = "bdk-cli"
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ RUST_LOG=debug cargo run --features esplora-ureq -- wallet --descriptor "wpkh(tp
```

At most one blockchain feature can be enabled, available blockchain client features are:
`electrum`, `esplora-ureq` (blocking), `esplora-reqwest` (async), and `compact_filters`.
`electrum`, `esplora-ureq` (blocking), `esplora-reqwest` (async), `compact_filters` and `rpc`.

### From crates.io
You can the install the binaries for the latest tag of `bdk-cli` with online wallet features
Expand All @@ -52,6 +52,12 @@ To sync a wallet to the default electrum server:
cargo run --features electrum -- wallet --descriptor "wpkh(tpubEBr4i6yk5nf5DAaJpsi9N2pPYBeJ7fZ5Z9rmN4977iYLCGco1VyjB9tvvuvYtfZzjD5A8igzgw3HeWeeKFmanHYqksqZXYXGsw5zjnj7KM9/*)" sync
```

To sync a wallet to Bitcoin Core node (assuming a regtest node at 127.0.0.1:18443) using the core rpc:

```shell
cargo run --features rpc -- --network regtest wallet --node 127.0.0.1:18443 --descriptor "wpkh(tpubEBr4i6yk5nf5DAaJpsi9N2pPYBeJ7fZ5Z9rmN4977iYLCGco1VyjB9tvvuvYtfZzjD5A8igzgw3HeWeeKFmanHYqksqZXYXGsw5zjnj7KM9/*)" sync
```

To sync a wallet to Bitcoin Core node (assuming a regtest node at 127.0.0.1:18444) serving compact filters:
Note:
- This will increase build time by few minutes for the binaries because of `librocksdb`.
Expand Down
3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ fn main() {
let esplora = env::var_os("CARGO_FEATURE_ESPLORA").map(|_| "esplora".to_string());
let compact_filters =
env::var_os("CARGO_FEATURE_COMPACT_FILTERS").map(|_| "compact_filters".to_string());
let rpc = env::var_os("CARGO_FEATURE_RPC").map(|_| "rpc".to_string());

let blockchain_features: Vec<String> = vec![electrum, esplora, compact_filters]
let blockchain_features: Vec<String> = vec![electrum, esplora, compact_filters, rpc]
.iter()
.map(|f| f.to_owned())
.flatten()
Expand Down
81 changes: 73 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 Down Expand Up @@ -163,6 +188,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 @@ -214,7 +267,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 @@ -255,13 +313,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 @@ -307,7 +371,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