Saner error output on multiple attempts to init wallet (#2021)

* less scary error message on duplicate wallet commit

* rustfmt
This commit is contained in:
Yeastplume
2018-11-26 12:02:18 +00:00
committed by GitHub
parent 9db36f7ba3
commit b2c177d16a
3 changed files with 31 additions and 24 deletions
+7
View File
@@ -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,
+3 -3
View File
@@ -25,7 +25,7 @@ use failure::{Backtrace, Context, Fail};
/// Error definition
#[derive(Debug)]
pub struct Error {
inner: Context<ErrorKind>,
pub inner: Context<ErrorKind>,
}
/// 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")]
+21 -21
View File
@@ -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<WalletSeed, Error> {