moving unlock logics to secret-store itself

This commit is contained in:
Nikolay Volf
2016-03-04 16:23:00 +03:00
parent 4f62e80de7
commit 0d01099f44
5 changed files with 90 additions and 27 deletions

View File

@@ -36,6 +36,7 @@ libc = "0.2.7"
vergen = "0.1"
target_info = "0.1"
bigint = { path = "bigint" }
chrono = "0.2"
[features]
default = []

View File

@@ -22,6 +22,7 @@ use rcrypto::pbkdf2::*;
use rcrypto::scrypt::*;
use rcrypto::hmac::*;
use crypto;
use chrono::*;
const KEY_LENGTH: u32 = 32;
const KEY_ITERATIONS: u32 = 10240;
@@ -57,7 +58,13 @@ pub enum EncryptedHashMapError {
/// Represent service for storing encrypted arbitrary data
pub struct SecretStore {
directory: KeyDirectory
directory: KeyDirectory,
unlocks: RwLock<HashMap<Address, AccountUnlock>>
}
struct AccountUnlock {
secret: H256,
expires: DateTime<UTC>,
}
impl SecretStore {
@@ -72,7 +79,8 @@ impl SecretStore {
/// new instance of Secret Store in specific directory
pub fn new_in(path: &Path) -> SecretStore {
SecretStore {
directory: KeyDirectory::new(path)
directory: KeyDirectory::new(path),
unlocks: RwLock::new(HashMap::new())
}
}
@@ -120,9 +128,37 @@ impl SecretStore {
#[cfg(test)]
fn new_test(path: &::devtools::RandomTempPath) -> SecretStore {
SecretStore {
directory: KeyDirectory::new(path.as_path())
directory: KeyDirectory::new(path.as_path()),
unlocks: RwLock::new(HashMap::new())
}
}
/// Unlocks account for use
pub fn unlock_account(&self, account: &Address, pass: &str) -> Result<(), EncryptedHashMapError> {
let secret_id = try!(self.account(&account).ok_or(EncryptedHashMapError::UnknownIdentifier));
let secret = try!(self.get(&secret_id, pass));
{
let mut write_lock = self.unlocks.write().unwrap();
let mut unlock = write_lock.entry(*account)
.or_insert_with(|| AccountUnlock { secret: secret, expires: UTC::now() });
unlock.secret = secret;
unlock.expires = UTC::now() + Duration::minutes(20);
}
Ok(())
}
/// Creates new account
pub fn new_account(&mut self, pass: &str) -> Result<Address, ::std::io::Error> {
let secret = H256::random();
let key_id = H128::random();
self.insert(key_id.clone(), secret, pass);
let mut key_file = self.directory.get(&key_id).expect("the key was just inserted");
let address = Address::random();
key_file.account = Some(address);
try!(self.directory.save(key_file));
Ok(address)
}
}
fn derive_key_iterations(password: &str, salt: &H256, c: u32) -> (Bytes, Bytes) {
@@ -369,6 +405,24 @@ mod tests {
assert_eq!(4, sstore.directory.list().unwrap().len())
}
#[test]
fn can_create_account() {
let temp = RandomTempPath::create_dir();
let mut sstore = SecretStore::new_test(&temp);
sstore.new_account("123").unwrap();
assert_eq!(1, sstore.accounts().unwrap().len());
}
#[test]
fn can_unlock_account() {
let temp = RandomTempPath::create_dir();
let mut sstore = SecretStore::new_test(&temp);
let address = sstore.new_account("123").unwrap();
let secret = sstore.unlock_account(&address, "123");
assert!(secret.is_ok());
}
#[test]
fn can_import_account() {
use keys::directory::{KeyFileContent, KeyFileCrypto};

View File

@@ -111,6 +111,7 @@ extern crate rustc_version;
extern crate target_info;
extern crate vergen;
extern crate bigint;
extern crate chrono;
pub mod standard;
#[macro_use]