store extra interface

This commit is contained in:
Nikolay Volf 2016-02-22 20:08:16 +03:00
parent 753f52fc22
commit 61a3e687f5
2 changed files with 56 additions and 2 deletions

View File

@ -381,6 +381,16 @@ impl KeyFileContent {
} }
} }
pub fn load(json: &Json) -> Result<KeyFileContent, ()> {
match Self::from_json(json) {
Ok(key_file) => Ok(key_file),
Err(e) => {
warn!(target: "sstore", "Error parsing json for key: {:?}", e);
Err(())
}
}
}
/// Returns key file version if it is known. /// Returns key file version if it is known.
pub fn version(&self) -> Option<u64> { pub fn version(&self) -> Option<u64> {
match self.version { match self.version {
@ -410,7 +420,7 @@ impl KeyFileContent {
Ok(id) => id Ok(id) => id
}; };
let account = as_object.get("account").and_then(|json| json.as_string()).and_then( let account = as_object.get("address").and_then(|json| json.as_string()).and_then(
|account_text| match Address::from_str(account_text) { Ok(account) => Some(account), Err(_) => None }); |account_text| match Address::from_str(account_text) { Ok(account) => Some(account), Err(_) => None });
let crypto = match as_object.get("crypto") { let crypto = match as_object.get("crypto") {

View File

@ -64,8 +64,12 @@ impl SecretStore {
pub fn new() -> SecretStore { pub fn new() -> SecretStore {
let mut path = ::std::env::home_dir().expect("Failed to get home dir"); let mut path = ::std::env::home_dir().expect("Failed to get home dir");
path.push("keystore"); path.push("keystore");
Self::new_in(&path)
}
pub fn new_in(path: &Path) -> SecretStore {
SecretStore { SecretStore {
directory: KeyDirectory::new(&path) directory: KeyDirectory::new(path)
} }
} }
@ -285,6 +289,25 @@ mod tests {
result result
} }
fn pregenerate_accounts(temp: &RandomTempPath, count: usize) -> Vec<H128> {
use keys::directory::{KeyFileContent, KeyFileCrypto};
let mut write_sstore = SecretStore::new_test(&temp);
let mut result = Vec::new();
for i in 0..count {
let mut key_file =
KeyFileContent::new(
KeyFileCrypto::new_pbkdf2(
FromHex::from_hex("5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46").unwrap(),
H128::from_str("6087dab2f9fdbbfaddc31a909735c1e6").unwrap(),
H256::from_str("ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd").unwrap(),
H256::from_str("517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2").unwrap(),
262144,
32));
key_file.account = Some(x!(i as u64));
}
result
}
#[test] #[test]
fn can_get() { fn can_get() {
let temp = RandomTempPath::create_dir(); let temp = RandomTempPath::create_dir();
@ -319,5 +342,26 @@ mod tests {
assert_eq!(4, sstore.directory.list().unwrap().len()) assert_eq!(4, sstore.directory.list().unwrap().len())
} }
#[test]
fn can_import_account() {
let temp = RandomTempPath::create_dir();
use keys::directory::{KeyFileContent, KeyFileCrypto};
let mut key_file =
KeyFileContent::new(
KeyFileCrypto::new_pbkdf2(
FromHex::from_hex("5318b4d5bcd28de64ee5559e671353e16f075ecae9f99c7a79a38af5f869aa46").unwrap(),
H128::from_str("6087dab2f9fdbbfaddc31a909735c1e6").unwrap(),
H256::from_str("ae3cd4e7013836a3df6bd7241b12db061dbe2c6785853cce422d148a624ce0bd").unwrap(),
H256::from_str("517ead924a9d0dc3124507e3393d175ce3ff7c1e96529c6c555ce9e51205e9b2").unwrap(),
262144,
32));
key_file.account = Some(Address::from_str("3f49624084b67849c7b4e805c5988c21a430f9d9").unwrap());
let mut sstore = SecretStore::new_test(&temp);
sstore.import_key(key_file);
assert_eq!(1, sstore.accounts().unwrap().len());
assert!(sstore.account(&Address::from_str("3f49624084b67849c7b4e805c5988c21a430f9d9").unwrap()).is_some());
}
} }