From: Riccardo Casatta Date: Thu, 11 Mar 2021 09:57:14 +0000 (+0100) Subject: Handle unwraps, give more details about ChecksumMismatch error X-Git-Tag: v0.3.0~65 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/database/struct.WScriptHash.html?a=commitdiff_plain;h=28735b7228a79ad38d57e1bd4091e32589b6e7fb;p=bdk-cli Handle unwraps, give more details about ChecksumMismatch error --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 48f6d69..b39fba1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 #### Added - `CliSubCommand::Compile` enum variant and `handle_compile_subcommand()` function - Make repl and electrum default features +- Remove unwraps while handling CLI commands ### `bdk-cli` bin diff --git a/src/bdk_cli.rs b/src/bdk_cli.rs index 3d5ce29..0af8a9f 100644 --- a/src/bdk_cli.rs +++ b/src/bdk_cli.rs @@ -27,7 +27,7 @@ use std::path::PathBuf; use bitcoin::Network; use clap::AppSettings; -use log::{debug, info, warn}; +use log::{debug, error, info, warn}; use rustyline::error::ReadlineError; use rustyline::Editor; use structopt::StructOpt; @@ -157,42 +157,54 @@ fn main() { warn!("This is experimental software and not currently recommended for use on Bitcoin mainnet, proceed with caution.") } + match handle_command(cli_opts, network) { + 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')"), + e => error!("{}", e.to_string()), + } + }, + } +} + +fn handle_command(cli_opts: CliOpts, network: Network) -> Result { let result = match cli_opts.subcommand { CliSubCommand::Wallet { wallet_opts, subcommand: WalletSubCommand::OnlineWalletSubCommand(online_subcommand), } => { let database = open_database(&wallet_opts); - let wallet = new_online_wallet(network, &wallet_opts, database).unwrap(); - let result = bdk_cli::handle_online_wallet_subcommand(&wallet, online_subcommand); - serde_json::to_string_pretty(&result.unwrap()).unwrap() + let wallet = new_online_wallet(network, &wallet_opts, database)?; + let result = bdk_cli::handle_online_wallet_subcommand(&wallet, online_subcommand)?; + serde_json::to_string_pretty(&result)? } CliSubCommand::Wallet { wallet_opts, subcommand: WalletSubCommand::OfflineWalletSubCommand(offline_subcommand), } => { let database = open_database(&wallet_opts); - let wallet = new_offline_wallet(network, &wallet_opts, database).unwrap(); - let result = bdk_cli::handle_offline_wallet_subcommand(&wallet, offline_subcommand); - serde_json::to_string_pretty(&result.unwrap()).unwrap() + let wallet = new_offline_wallet(network, &wallet_opts, database)?; + let result = bdk_cli::handle_offline_wallet_subcommand(&wallet, offline_subcommand)?; + serde_json::to_string_pretty(&result)? } CliSubCommand::Key { subcommand: key_subcommand, } => { - let result = bdk_cli::handle_key_subcommand(network, key_subcommand); - serde_json::to_string_pretty(&result.unwrap()).unwrap() + let result = bdk_cli::handle_key_subcommand(network, key_subcommand)?; + serde_json::to_string_pretty(&result)? } #[cfg(feature = "compiler")] CliSubCommand::Compile { policy, script_type, } => { - let result = bdk_cli::handle_compile_subcommand(network, policy, script_type); - serde_json::to_string_pretty(&result.unwrap()).unwrap() + let result = bdk_cli::handle_compile_subcommand(network, policy, script_type)?; + serde_json::to_string_pretty(&result)? } CliSubCommand::Repl { wallet_opts } => { let database = open_database(&wallet_opts); - let online_wallet = new_online_wallet(network, &wallet_opts, database).unwrap(); + let online_wallet = new_online_wallet(network, &wallet_opts, database)?; let mut rl = Editor::<()>::new(); @@ -200,7 +212,8 @@ fn main() { // println!("No previous history."); // } - let split_regex = Regex::new(REPL_LINE_SPLIT_REGEX).unwrap(); + let split_regex = + Regex::new(REPL_LINE_SPLIT_REGEX).map_err(|e| Error::Generic(e.to_string()))?; loop { let readline = rl.readline(">> "); @@ -268,8 +281,7 @@ fn main() { "Exiting REPL".to_string() } }; - - println!("{}", result); + Ok(result) } #[cfg(test)]