files list separate fn, sha3 of the list
This commit is contained in:
parent
39d4e46073
commit
d4149b965e
@ -90,11 +90,8 @@ impl<T> DiskDirectory<T> where T: KeyFileManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// all accounts found in keys directory
|
fn files(&self) -> Result<Vec<PathBuf>, Error> {
|
||||||
fn files(&self) -> Result<HashMap<PathBuf, SafeAccount>, Error> {
|
Ok(fs::read_dir(&self.path)?
|
||||||
// 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)?
|
|
||||||
.flat_map(Result::ok)
|
.flat_map(Result::ok)
|
||||||
.filter(|entry| {
|
.filter(|entry| {
|
||||||
let metadata = entry.metadata().ok();
|
let metadata = entry.metadata().ok();
|
||||||
@ -102,14 +99,21 @@ impl<T> DiskDirectory<T> where T: KeyFileManager {
|
|||||||
let name = file_name.to_string_lossy();
|
let name = file_name.to_string_lossy();
|
||||||
// filter directories
|
// filter directories
|
||||||
metadata.map_or(false, |m| !m.is_dir()) &&
|
metadata.map_or(false, |m| !m.is_dir()) &&
|
||||||
// hidden files
|
// hidden files
|
||||||
!name.starts_with(".") &&
|
!name.starts_with(".") &&
|
||||||
// other ignored files
|
// other ignored files
|
||||||
!IGNORED_FILES.contains(&&*name)
|
!IGNORED_FILES.contains(&&*name)
|
||||||
})
|
})
|
||||||
.map(|entry| entry.path())
|
.map(|entry| entry.path())
|
||||||
.collect::<Vec<PathBuf>>();
|
.collect::<Vec<PathBuf>>()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// all accounts found in keys directory
|
||||||
|
fn files_content(&self) -> Result<HashMap<PathBuf, SafeAccount>, 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
|
Ok(paths
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|path| {
|
.filter_map(|path| {
|
||||||
@ -166,7 +170,7 @@ impl<T> DiskDirectory<T> where T: KeyFileManager {
|
|||||||
|
|
||||||
impl<T> KeyDirectory for DiskDirectory<T> where T: KeyFileManager {
|
impl<T> KeyDirectory for DiskDirectory<T> where T: KeyFileManager {
|
||||||
fn load(&self) -> Result<Vec<SafeAccount>, Error> {
|
fn load(&self) -> Result<Vec<SafeAccount>, Error> {
|
||||||
let accounts = self.files()?
|
let accounts = self.files_content()?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(_, account)| account)
|
.map(|(_, account)| account)
|
||||||
.collect();
|
.collect();
|
||||||
@ -191,7 +195,7 @@ impl<T> KeyDirectory for DiskDirectory<T> where T: KeyFileManager {
|
|||||||
fn remove(&self, account: &SafeAccount) -> Result<(), Error> {
|
fn remove(&self, account: &SafeAccount) -> Result<(), Error> {
|
||||||
// enumerate all entries in keystore
|
// enumerate all entries in keystore
|
||||||
// and find entry with given address
|
// and find entry with given address
|
||||||
let to_remove = self.files()?
|
let to_remove = self.files_content()?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.find(|&(_, ref acc)| acc.id == account.id && acc.address == account.address);
|
.find(|&(_, ref acc)| acc.id == account.id && acc.address == account.address);
|
||||||
|
|
||||||
|
@ -68,6 +68,24 @@ impl<T> Hashable for T where T: AsRef<[u8]> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> 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.
|
/// Calculate SHA3 of given stream.
|
||||||
pub fn sha3(r: &mut io::BufRead) -> Result<H256, io::Error> {
|
pub fn sha3(r: &mut io::BufRead) -> Result<H256, io::Error> {
|
||||||
let mut output = [0u8; 32];
|
let mut output = [0u8; 32];
|
||||||
@ -120,4 +138,15 @@ mod tests {
|
|||||||
// then
|
// then
|
||||||
assert_eq!(format!("{:?}", hash), "68371d7e884c168ae2022c82bd837d51837718a7f7dfb7aa3f753074a35e1d87");
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user