From b2c177d16a488502db6cdd949a8a4269d0f010ff Mon Sep 17 00:00:00 2001 From: Yeastplume Date: Mon, 26 Nov 2018 12:02:18 +0000 Subject: [PATCH] Saner error output on multiple attempts to init wallet (#2021) * less scary error message on duplicate wallet commit * rustfmt --- src/bin/cmd/wallet.rs | 7 +++++++ wallet/src/error.rs | 6 +++--- wallet/src/types.rs | 42 +++++++++++++++++++++--------------------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/bin/cmd/wallet.rs b/src/bin/cmd/wallet.rs index b076a600..efef88b3 100644 --- a/src/bin/cmd/wallet.rs +++ b/src/bin/cmd/wallet.rs @@ -106,6 +106,13 @@ pub fn wallet_command(wallet_args: &ArgMatches, config: GlobalWalletConfig) -> i // Decrypt the seed from the seed file and derive the keychain. // Generate the initial wallet seed if we are running "wallet init". if let ("init", Some(r)) = wallet_args.subcommand() { + if let Err(e) = WalletSeed::seed_file_exists(&wallet_config) { + println!( + "Not creating wallet - Wallet seed file already exists at {}", + e.inner + ); + return 0; + } let list_length = match r.is_present("short_wordlist") { false => 32, true => 16, diff --git a/wallet/src/error.rs b/wallet/src/error.rs index 1db13a9c..c5fef591 100644 --- a/wallet/src/error.rs +++ b/wallet/src/error.rs @@ -25,7 +25,7 @@ use failure::{Backtrace, Context, Fail}; /// Error definition #[derive(Debug)] pub struct Error { - inner: Context, + pub inner: Context, } /// Wallet errors, mostly wrappers around underlying crypto or I/O errors. @@ -80,8 +80,8 @@ pub enum ErrorKind { DuplicateTransactionId, /// Wallet seed already exists - #[fail(display = "Wallet seed exists error")] - WalletSeedExists, + #[fail(display = "{}", _0)] + WalletSeedExists(String), /// Wallet seed doesn't exist #[fail(display = "Wallet seed doesn't exist error")] diff --git a/wallet/src/types.rs b/wallet/src/types.rs index 278ad8c4..371620a3 100644 --- a/wallet/src/types.rs +++ b/wallet/src/types.rs @@ -135,6 +135,17 @@ impl WalletSeed { WalletSeed(seed) } + pub fn seed_file_exists(wallet_config: &WalletConfig) -> Result<(), Error> { + let seed_file_path = &format!( + "{}{}{}", + wallet_config.data_file_dir, MAIN_SEPARATOR, SEED_FILE, + ); + if Path::new(seed_file_path).exists() { + return Err(ErrorKind::WalletSeedExists(seed_file_path.to_owned()))?; + } + Ok(()) + } + pub fn recover_from_phrase( wallet_config: &WalletConfig, word_list: &str, @@ -144,14 +155,7 @@ impl WalletSeed { "{}{}{}", wallet_config.data_file_dir, MAIN_SEPARATOR, SEED_FILE, ); - if Path::new(seed_file_path).exists() { - error!( - "wallet seed file {} exists. \ - Please backup and delete this file before attempting recovery.", - seed_file_path - ); - return Err(ErrorKind::WalletSeedExists)?; - } + let _ = WalletSeed::seed_file_exists(wallet_config)?; let seed = WalletSeed::from_mnemonic(word_list)?; let enc_seed = EncryptedWalletSeed::from_seed(&seed, password)?; let enc_seed_json = serde_json::to_string_pretty(&enc_seed).context(ErrorKind::Format)?; @@ -183,20 +187,16 @@ impl WalletSeed { ); warn!("Generating wallet seed file at: {}", seed_file_path); + let _ = WalletSeed::seed_file_exists(wallet_config)?; - if Path::new(seed_file_path).exists() { - Err(ErrorKind::WalletSeedExists)? - } else { - let seed = WalletSeed::init_new(seed_length); - let enc_seed = EncryptedWalletSeed::from_seed(&seed, password)?; - let enc_seed_json = - serde_json::to_string_pretty(&enc_seed).context(ErrorKind::Format)?; - let mut file = File::create(seed_file_path).context(ErrorKind::IO)?; - file.write_all(&enc_seed_json.as_bytes()) - .context(ErrorKind::IO)?; - seed.show_recovery_phrase()?; - Ok(seed) - } + let seed = WalletSeed::init_new(seed_length); + let enc_seed = EncryptedWalletSeed::from_seed(&seed, password)?; + let enc_seed_json = serde_json::to_string_pretty(&enc_seed).context(ErrorKind::Format)?; + let mut file = File::create(seed_file_path).context(ErrorKind::IO)?; + file.write_all(&enc_seed_json.as_bytes()) + .context(ErrorKind::IO)?; + seed.show_recovery_phrase()?; + Ok(seed) } pub fn from_file(wallet_config: &WalletConfig, password: &str) -> Result {