Less cloning and additional pattern simplifications (#326)

* API cleanup

* Config cleanup

* Impl cleanup

* Libwallet cleanup

* Additionnal simplification
This commit is contained in:
Quentin Le Sceller
2020-02-07 09:39:56 +00:00
committed by GitHub
parent 2d264db91a
commit 1116bc5545
35 changed files with 554 additions and 616 deletions
+11 -12
View File
@@ -82,21 +82,21 @@ where
let logging = match logging_config {
Some(l) => Some(l),
None => match default_config.members.as_ref() {
Some(m) => m.clone().logging.clone(),
Some(m) => m.clone().logging,
None => None,
},
};
let wallet = match wallet_config {
Some(w) => w,
None => match default_config.members.as_ref() {
Some(m) => m.clone().wallet.clone(),
Some(m) => m.clone().wallet,
None => WalletConfig::default(),
},
};
let tor = match tor_config {
Some(t) => Some(t),
None => match default_config.members.as_ref() {
Some(m) => m.clone().tor.clone(),
Some(m) => m.clone().tor,
None => Some(TorConfig::default()),
},
};
@@ -138,7 +138,7 @@ where
let mut abs_path = std::env::current_dir()?;
abs_path.push(self.data_dir.clone());
default_config.update_paths(&PathBuf::from(abs_path));
default_config.update_paths(&abs_path);
let res = default_config.write_to_file(config_file_name.to_str().unwrap());
if let Err(e) = res {
let msg = format!(
@@ -180,7 +180,7 @@ where
if !test_mode {
if let Ok(true) = exists {
let msg = format!("Wallet seed already exists at: {}", data_dir_name);
return Err(ErrorKind::WalletSeedExists(msg))?;
return Err(ErrorKind::WalletSeedExists(msg).into());
}
}
WalletSeed::init_file(
@@ -245,10 +245,9 @@ where
}
fn close_wallet(&mut self, _name: Option<&str>) -> Result<(), Error> {
match self.backend.as_mut() {
Some(b) => b.close()?,
None => {}
};
if let Some(b) = self.backend.as_mut() {
b.close()?
}
self.backend = None;
Ok(())
}
@@ -283,7 +282,7 @@ where
fn validate_mnemonic(&self, mnemonic: ZeroingString) -> Result<(), Error> {
match WalletSeed::from_mnemonic(mnemonic) {
Ok(_) => Ok(()),
Err(_) => Err(ErrorKind::GenericError("Validating mnemonic".into()))?,
Err(_) => Err(ErrorKind::GenericError("Validating mnemonic".into()).into()),
}
}
@@ -345,9 +344,9 @@ where
)?;
if orig_wallet_seed != new_wallet_seed {
let msg = format!(
let msg =
"New and Old wallet seeds are not equal on password change, not removing backups."
);
.to_string();
return Err(ErrorKind::Lifecycle(msg).into());
}
// Removin
+11 -12
View File
@@ -29,7 +29,7 @@ use crate::util;
use crate::{Error, ErrorKind};
use failure::ResultExt;
pub const SEED_FILE: &'static str = "wallet.seed";
pub const SEED_FILE: &str = "wallet.seed";
#[derive(Clone, Debug, PartialEq)]
pub struct WalletSeed(Vec<u8>);
@@ -108,10 +108,8 @@ impl WalletSeed {
i += 1;
}
path.push(backup_seed_file_name.clone());
if let Err(_) = fs::rename(seed_file_name, backup_seed_file_name.as_str()) {
return Err(ErrorKind::GenericError(
"Can't rename wallet seed file".to_owned(),
))?;
if fs::rename(seed_file_name, backup_seed_file_name.as_str()).is_err() {
return Err(ErrorKind::GenericError("Can't rename wallet seed file".to_owned()).into());
}
warn!("{} backed up as {}", seed_file_name, backup_seed_file_name);
Ok(backup_seed_file_name)
@@ -133,7 +131,8 @@ impl WalletSeed {
data_file_dir.to_owned(),
"To create a new wallet from a recovery phrase, use 'grin-wallet init -r'"
.to_owned(),
))?;
)
.into());
}
let seed = WalletSeed::from_mnemonic(word_list)?;
let enc_seed = EncryptedWalletSeed::from_seed(&seed, password)?;
@@ -162,7 +161,7 @@ impl WalletSeed {
if exists && !test_mode {
let msg = format!("Wallet seed already exists at: {}", data_file_dir);
error!("{}", msg);
return Err(ErrorKind::WalletSeedExists(msg))?;
return Err(ErrorKind::WalletSeedExists(msg).into());
}
let seed = match recovery_phrase {
@@ -203,7 +202,7 @@ impl WalletSeed {
Run \"grin-wallet init\" to initialize a new wallet.",
seed_file_path
);
Err(ErrorKind::WalletSeedDoesntExist)?
Err(ErrorKind::WalletSeedDoesntExist.into())
}
}
@@ -242,7 +241,7 @@ impl EncryptedWalletSeed {
let mut key = [0; 32];
pbkdf2::derive(&digest::SHA512, 100, &salt, password, &mut key);
let content = seed.0.to_vec();
let mut enc_bytes = content.clone();
let mut enc_bytes = content;
let suffix_len = aead::CHACHA20_POLY1305.tag_len();
for _ in 0..suffix_len {
enc_bytes.push(0);
@@ -262,15 +261,15 @@ impl EncryptedWalletSeed {
pub fn decrypt(&self, password: &str) -> Result<WalletSeed, Error> {
let mut encrypted_seed = match util::from_hex(self.encrypted_seed.clone()) {
Ok(s) => s,
Err(_) => return Err(ErrorKind::Encryption)?,
Err(_) => return Err(ErrorKind::Encryption.into()),
};
let salt = match util::from_hex(self.salt.clone()) {
Ok(s) => s,
Err(_) => return Err(ErrorKind::Encryption)?,
Err(_) => return Err(ErrorKind::Encryption.into()),
};
let nonce = match util::from_hex(self.nonce.clone()) {
Ok(s) => s,
Err(_) => return Err(ErrorKind::Encryption)?,
Err(_) => return Err(ErrorKind::Encryption.into()),
};
let password = password.as_bytes();
let mut key = [0; 32];