Skip to content

Commit

Permalink
if no wallet name was provided, use one derived from the descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrichard committed Jan 11, 2022
1 parent 40a0138 commit e52e819
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 145 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Replace `wallet bump_fee` command `--send_all` with new `--shrink` option
- Add 'reserve' feature to enable proof of reserve
- If no wallet name is provided, derive one from the descriptor instead of using "main"

## [0.3.0]

Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ readme = "README.md"
license = "MIT"

[dependencies]
bdk = { version = "0.14.0", default-features = false, features = ["all-keys"]}
bdk = { version = "0.15", default-features = false, features = ["all-keys"]}
bdk-macros = "0.6"
structopt = "^0.3"
serde_json = { version = "^1.0" }
Expand All @@ -26,7 +26,7 @@ dirs-next = { version = "2.0", optional = true }
env_logger = { version = "0.7", optional = true }
clap = { version = "2.33", optional = true }
regex = { version = "1", optional = true }
bdk-reserves = { version = "0.14.2", optional = true}
bdk-reserves = { version = "0.15", optional = true}

[features]
default = ["cli", "repl"]
Expand Down
52 changes: 35 additions & 17 deletions src/bdk_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use bitcoin::secp256k1::Secp256k1;
use bitcoin::Network;
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 @@ -54,11 +53,12 @@ use bdk::blockchain::esplora::EsploraBlockchainConfig;
use bdk::blockchain::{AnyBlockchain, AnyBlockchainConfig, ConfigurableBlockchain};

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

use bdk::database::BatchDatabase;
use bdk::sled;
use bdk::sled::Tree;
use bdk::wallet::wallet_name_from_descriptor;
use bdk::Wallet;
use bdk::{bitcoin, Error};
use bdk_cli::WalletSubCommand;
Expand Down Expand Up @@ -122,9 +122,13 @@ fn prepare_home_dir() -> Result<PathBuf, Error> {

fn open_database(wallet_opts: &WalletOpts) -> Result<Tree, Error> {
let mut database_path = prepare_home_dir()?;
database_path.push(wallet_opts.wallet.clone());
let wallet_name = wallet_opts
.wallet
.as_deref()
.expect("We should always have a wallet name at this point");
database_path.push(wallet_name);
let database = sled::open(database_path)?;
let tree = database.open_tree(&wallet_opts.wallet)?;
let tree = database.open_tree(&wallet_name)?;
debug!("database opened successfully");
Ok(tree)
}
Expand Down Expand Up @@ -152,18 +156,10 @@ where
stop_gap: wallet_opts.electrum_opts.stop_gap,
});

#[cfg(feature = "esplora-ureq")]
let config = AnyBlockchainConfig::Esplora(EsploraBlockchainConfig {
base_url: wallet_opts.esplora_opts.server.clone(),
timeout_read: wallet_opts.esplora_opts.read_timeout,
timeout_write: wallet_opts.esplora_opts.write_timeout,
stop_gap: wallet_opts.esplora_opts.stop_gap,
proxy: wallet_opts.proxy_opts.proxy.clone(),
});

#[cfg(feature = "esplora-reqwest")]
#[cfg(feature = "esplora")]
let config = AnyBlockchainConfig::Esplora(EsploraBlockchainConfig {
base_url: wallet_opts.esplora_opts.server.clone(),
timeout: Some(wallet_opts.esplora_opts.timeout),
concurrency: Some(wallet_opts.esplora_opts.conc),
stop_gap: wallet_opts.esplora_opts.stop_gap,
proxy: wallet_opts.proxy_opts.proxy.clone(),
Expand Down Expand Up @@ -263,13 +259,33 @@ fn main() {
Ok(result) => println!("{}", result),
Err(e) => {
match e {
Error::ChecksumMismatch => error!("Descriptor checksum mismatch. Are you using a different descriptor for an already defined wallet name? (if you are not specifying the wallet name it defaults to 'main')"),
Error::ChecksumMismatch => error!("Descriptor checksum mismatch. Are you using a different descriptor for an already defined wallet name? (if you are not specifying the wallet name it is automatically named based on the descriptor)"),
e => error!("{}", e.to_string()),
}
},
}
}

fn maybe_descriptor_wallet_name(
wallet_opts: WalletOpts,
network: Network,
) -> Result<WalletOpts, Error> {
if wallet_opts.wallet.is_some() {
return Ok(wallet_opts);
}
// 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 wallet_opts = wallet_opts;
wallet_opts.wallet = Some(wallet_name);

Ok(wallet_opts)
}

fn handle_command(cli_opts: CliOpts, network: Network) -> Result<String, Error> {
let result = match cli_opts.subcommand {
#[cfg(any(
Expand All @@ -282,6 +298,7 @@ fn handle_command(cli_opts: CliOpts, network: Network) -> Result<String, Error>
wallet_opts,
subcommand: WalletSubCommand::OnlineWalletSubCommand(online_subcommand),
} => {
let wallet_opts = maybe_descriptor_wallet_name(wallet_opts, network)?;
let database = open_database(&wallet_opts)?;
let wallet = new_online_wallet(network, &wallet_opts, database)?;
let result = bdk_cli::handle_online_wallet_subcommand(&wallet, online_subcommand)?;
Expand All @@ -291,6 +308,7 @@ fn handle_command(cli_opts: CliOpts, network: Network) -> Result<String, Error>
wallet_opts,
subcommand: WalletSubCommand::OfflineWalletSubCommand(offline_subcommand),
} => {
let wallet_opts = maybe_descriptor_wallet_name(wallet_opts, network)?;
let database = open_database(&wallet_opts)?;
let wallet = new_offline_wallet(network, &wallet_opts, database)?;
let result = bdk_cli::handle_offline_wallet_subcommand(
Expand Down
Loading

0 comments on commit e52e819

Please sign in to comment.