diff --git a/ethstore/src/dir/disk.rs b/ethstore/src/dir/disk.rs index 4a4637850..023db1ac2 100755 --- a/ethstore/src/dir/disk.rs +++ b/ethstore/src/dir/disk.rs @@ -90,11 +90,8 @@ impl DiskDirectory where T: KeyFileManager { } } - /// all accounts found in keys directory - fn files(&self) -> Result, Error> { - // it's not done using one iterator cause - // there is an issue with rustc and it takes tooo much time to compile - let paths = fs::read_dir(&self.path)? + fn files(&self) -> Result, Error> { + Ok(fs::read_dir(&self.path)? .flat_map(Result::ok) .filter(|entry| { let metadata = entry.metadata().ok(); @@ -102,14 +99,21 @@ impl DiskDirectory where T: KeyFileManager { let name = file_name.to_string_lossy(); // filter directories metadata.map_or(false, |m| !m.is_dir()) && - // hidden files - !name.starts_with(".") && - // other ignored files - !IGNORED_FILES.contains(&&*name) + // hidden files + !name.starts_with(".") && + // other ignored files + !IGNORED_FILES.contains(&&*name) }) .map(|entry| entry.path()) - .collect::>(); + .collect::>() + ) + } + /// all accounts found in keys directory + fn files_content(&self) -> Result, Error> { + // it's not done using one iterator cause + // there is an issue with rustc and it takes tooo much time to compile + let paths = self.files()?; Ok(paths .into_iter() .filter_map(|path| { @@ -166,7 +170,7 @@ impl DiskDirectory where T: KeyFileManager { impl KeyDirectory for DiskDirectory where T: KeyFileManager { fn load(&self) -> Result, Error> { - let accounts = self.files()? + let accounts = self.files_content()? .into_iter() .map(|(_, account)| account) .collect(); @@ -191,7 +195,7 @@ impl KeyDirectory for DiskDirectory where T: KeyFileManager { fn remove(&self, account: &SafeAccount) -> Result<(), Error> { // enumerate all entries in keystore // and find entry with given address - let to_remove = self.files()? + let to_remove = self.files_content()? .into_iter() .find(|&(_, ref acc)| acc.id == account.id && acc.address == account.address); diff --git a/util/src/sha3.rs b/util/src/sha3.rs index 45d6a34d5..4d3502313 100644 --- a/util/src/sha3.rs +++ b/util/src/sha3.rs @@ -68,6 +68,24 @@ impl Hashable for T where T: AsRef<[u8]> { } } +impl Hashable for [T] where T: Hashable { + fn sha3(&self) -> H256 { + use std::ops::BitXor; + + let mut sha3 = SHA3_EMPTY; + for t in self.iter() { + sha3 = sha3.bitxor(t.sha3()); + }; + + sha3 + } + // todo: optimize? + fn sha3_into(&self, dest: &mut [u8]) { + let sha3 = self.sha3(); + dest.copy_from_slice(&*sha3); + } +} + /// Calculate SHA3 of given stream. pub fn sha3(r: &mut io::BufRead) -> Result { let mut output = [0u8; 32]; @@ -120,4 +138,15 @@ mod tests { // then assert_eq!(format!("{:?}", hash), "68371d7e884c168ae2022c82bd837d51837718a7f7dfb7aa3f753074a35e1d87"); } + + #[test] + fn should_sha3_strs() { + let strs = vec!["abc".to_owned(), "gdc".to_owned()]; + let hash = strs.sha3(); + assert_eq!(hash, "b8f24d705171c55892a34c7b863c258f4d47e6864f7a7da45f84155597a3b338".parse().unwrap()); + + let strs = vec!["abc".to_owned(), "gdc_".to_owned()]; + let hash = strs.sha3(); + assert_eq!(hash, "41bd661b8e02faccad55cdbb28db974dd5c9ae41825b89907fcf25db793b8b09".parse().unwrap()); + } }