From b4aae6bd9c7b4c88de12afabdabb3e38b71af392 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Thu, 16 Jun 2016 16:14:22 +0400 Subject: [PATCH] Key load avoid warning (#1303) * avoid warning with key * fix intendations * more intendation fix * ok() instead of expect() --- util/src/keys/directory.rs | 11 +++++++---- util/src/keys/store.rs | 12 ++++++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/util/src/keys/directory.rs b/util/src/keys/directory.rs index 32ac14b55..385a400c7 100644 --- a/util/src/keys/directory.rs +++ b/util/src/keys/directory.rs @@ -428,9 +428,9 @@ impl KeyFileContent { let crypto = match as_object.get("crypto") { None => { return Err(KeyFileParseError::NoCryptoSection); } Some(crypto_json) => match KeyFileCrypto::from_json(crypto_json) { - Ok(crypto) => crypto, - Err(crypto_error) => { return Err(KeyFileParseError::Crypto(crypto_error)); } - } + Ok(crypto) => crypto, + Err(crypto_error) => { return Err(KeyFileParseError::Crypto(crypto_error)); } + } }; Ok(KeyFileContent { @@ -627,7 +627,10 @@ impl KeyDirectory { } } - + /// Checks if key exists + pub fn exists(&self, id: &Uuid) -> bool { + KeyDirectory::load_key(&self.key_path(id)).is_ok() + } } diff --git a/util/src/keys/store.rs b/util/src/keys/store.rs index f6f41745f..b8a1de272 100644 --- a/util/src/keys/store.rs +++ b/util/src/keys/store.rs @@ -294,9 +294,9 @@ impl SecretStore { if let Some(unlock) = read_lock.get(account) { (unlock.relock_on_use, match crypto::KeyPair::from_secret(unlock.secret) { Ok(pair) => match pair.sign(message) { - Ok(signature) => Ok(signature), - Err(_) => Err(SigningError::InvalidSecret) - }, + Ok(signature) => Ok(signature), + Err(_) => Err(SigningError::InvalidSecret) + }, Err(_) => Err(SigningError::InvalidSecret) }) } else { @@ -348,6 +348,10 @@ impl SecretStore { garbage_lock.shrink_to_fit(); } + + fn exists(&self, key: &H128) -> bool { + self.directory.exists(key) + } } fn derive_key_iterations(password: &str, salt: &H256, c: u32) -> (Bytes, Bytes) { @@ -408,7 +412,7 @@ impl EncryptedHashMap for SecretStore { } fn insert(&mut self, key: H128, value: Value, password: &str) -> Option { - let previous = if let Ok(previous_value) = self.get(&key, password) { Some(previous_value) } else { None }; + let previous = if !self.exists(&key) { None } else { self.get(&key, password).ok() }; // crypto random initiators let salt = H256::random();