From 8bc0b7c77c79ee5ba1868e678c502ba0aa026925 Mon Sep 17 00:00:00 2001 From: Nikolay Volf Date: Sun, 21 Feb 2016 23:44:12 +0300 Subject: [PATCH] import pub --- util/src/keys/store.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/util/src/keys/store.rs b/util/src/keys/store.rs index 100f3d30c..284c78b2c 100644 --- a/util/src/keys/store.rs +++ b/util/src/keys/store.rs @@ -69,6 +69,7 @@ impl SecretStore { } } + /// Lists all accounts and corresponding key ids pub fn accounts(&self) -> Result, ::std::io::Error> { let accounts = try!(self.directory.list()).iter().map(|key_id| self.directory.get(key_id)) .filter(|key| key.is_some()) @@ -79,6 +80,21 @@ impl SecretStore { Ok(accounts) } + /// Resolves key_id by account address + pub fn account(&self, account: &Address) -> Option { + let mut accounts = match self.accounts() { + Ok(accounts) => accounts, + Err(e) => { warn!(target: "sstore", "Failed to load accounts: {}", e); return None; } + }; + accounts.retain(|&(ref store_account, _)| account == store_account); + accounts.first().and_then(|&(_, ref key_id)| Some(key_id.clone())) + } + + pub fn import_key(&mut self, key_file: KeyFileContent) -> Result<(), ::std::io::Error> { + try!(self.directory.save(key_file)); + Ok(()) + } + #[cfg(test)] fn new_test(path: &::devtools::RandomTempPath) -> SecretStore { SecretStore {