2016-06-20 10:06:49 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-06-20 00:10:34 +02:00
|
|
|
use std::collections::BTreeMap;
|
2016-07-31 10:44:17 +02:00
|
|
|
use std::mem;
|
2016-06-20 00:10:34 +02:00
|
|
|
use ethkey::KeyPair;
|
|
|
|
use crypto::KEY_ITERATIONS;
|
|
|
|
use random::Random;
|
2016-10-15 14:44:08 +02:00
|
|
|
use ethkey::{Signature, Address, Message, Secret, Public};
|
2016-06-20 00:10:34 +02:00
|
|
|
use dir::KeyDirectory;
|
|
|
|
use account::SafeAccount;
|
|
|
|
use {Error, SecretStore};
|
2016-08-10 17:57:40 +02:00
|
|
|
use json;
|
2016-07-24 17:38:21 +02:00
|
|
|
use json::UUID;
|
2016-10-25 22:34:52 +02:00
|
|
|
use parking_lot::RwLock;
|
2016-08-10 17:57:40 +02:00
|
|
|
use presale::PresaleWallet;
|
2016-08-11 18:31:28 +02:00
|
|
|
use import;
|
2016-06-20 00:10:34 +02:00
|
|
|
|
|
|
|
pub struct EthStore {
|
|
|
|
dir: Box<KeyDirectory>,
|
|
|
|
iterations: u32,
|
|
|
|
cache: RwLock<BTreeMap<Address, SafeAccount>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EthStore {
|
|
|
|
pub fn open(directory: Box<KeyDirectory>) -> Result<Self, Error> {
|
|
|
|
Self::open_with_iterations(directory, KEY_ITERATIONS as u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn open_with_iterations(directory: Box<KeyDirectory>, iterations: u32) -> Result<Self, Error> {
|
|
|
|
let accounts = try!(directory.load());
|
|
|
|
let cache = accounts.into_iter().map(|account| (account.address.clone(), account)).collect();
|
|
|
|
let store = EthStore {
|
|
|
|
dir: directory,
|
|
|
|
iterations: iterations,
|
|
|
|
cache: RwLock::new(cache),
|
|
|
|
};
|
|
|
|
Ok(store)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn save(&self, account: SafeAccount) -> Result<(), Error> {
|
|
|
|
// save to file
|
2016-07-25 10:45:45 +02:00
|
|
|
let account = try!(self.dir.insert(account.clone()));
|
2016-06-20 00:10:34 +02:00
|
|
|
|
|
|
|
// update cache
|
2016-10-25 22:34:52 +02:00
|
|
|
let mut cache = self.cache.write();
|
2016-06-20 00:10:34 +02:00
|
|
|
cache.insert(account.address.clone(), account);
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-07-31 10:44:17 +02:00
|
|
|
|
|
|
|
fn reload_accounts(&self) -> Result<(), Error> {
|
2016-10-25 22:34:52 +02:00
|
|
|
let mut cache = self.cache.write();
|
2016-07-31 10:44:17 +02:00
|
|
|
let accounts = try!(self.dir.load());
|
|
|
|
let new_accounts: BTreeMap<_, _> = accounts.into_iter().map(|account| (account.address.clone(), account)).collect();
|
|
|
|
mem::replace(&mut *cache, new_accounts);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get(&self, address: &Address) -> Result<SafeAccount, Error> {
|
|
|
|
{
|
2016-10-25 22:34:52 +02:00
|
|
|
let cache = self.cache.read();
|
2016-07-31 10:44:17 +02:00
|
|
|
if let Some(account) = cache.get(address) {
|
|
|
|
return Ok(account.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try!(self.reload_accounts());
|
2016-10-25 22:34:52 +02:00
|
|
|
let cache = self.cache.read();
|
2016-07-31 10:44:17 +02:00
|
|
|
cache.get(address).cloned().ok_or(Error::InvalidAccount)
|
|
|
|
}
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SecretStore for EthStore {
|
|
|
|
fn insert_account(&self, secret: Secret, password: &str) -> Result<Address, Error> {
|
|
|
|
let keypair = try!(KeyPair::from_secret(secret).map_err(|_| Error::CreationFailed));
|
|
|
|
let id: [u8; 16] = Random::random();
|
2016-11-17 01:29:28 +01:00
|
|
|
let account = SafeAccount::create(&keypair, id, password, self.iterations, "".to_owned(), "{}".to_owned());
|
2016-06-20 00:10:34 +02:00
|
|
|
let address = account.address.clone();
|
|
|
|
try!(self.save(account));
|
|
|
|
Ok(address)
|
|
|
|
}
|
|
|
|
|
2016-08-10 17:57:40 +02:00
|
|
|
fn import_presale(&self, json: &[u8], password: &str) -> Result<Address, Error> {
|
|
|
|
let json_wallet = try!(json::PresaleWallet::load(json).map_err(|_| Error::InvalidKeyFile("Invalid JSON format".to_owned())));
|
|
|
|
let wallet = PresaleWallet::from(json_wallet);
|
|
|
|
let keypair = try!(wallet.decrypt(password).map_err(|_| Error::InvalidPassword));
|
|
|
|
self.insert_account(keypair.secret().clone(), password)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn import_wallet(&self, json: &[u8], password: &str) -> Result<Address, Error> {
|
|
|
|
let json_keyfile = try!(json::KeyFile::load(json).map_err(|_| Error::InvalidKeyFile("Invalid JSON format".to_owned())));
|
|
|
|
let mut safe_account = SafeAccount::from_file(json_keyfile, None);
|
|
|
|
let secret = try!(safe_account.crypto.secret(password).map_err(|_| Error::InvalidPassword));
|
|
|
|
safe_account.address = try!(KeyPair::from_secret(secret)).address();
|
|
|
|
let address = safe_account.address.clone();
|
|
|
|
try!(self.save(safe_account));
|
|
|
|
Ok(address)
|
|
|
|
}
|
|
|
|
|
2016-07-31 10:44:17 +02:00
|
|
|
fn accounts(&self) -> Result<Vec<Address>, Error> {
|
|
|
|
try!(self.reload_accounts());
|
2016-10-25 22:34:52 +02:00
|
|
|
Ok(self.cache.read().keys().cloned().collect())
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn change_password(&self, address: &Address, old_password: &str, new_password: &str) -> Result<(), Error> {
|
|
|
|
// change password
|
2016-07-31 10:44:17 +02:00
|
|
|
let account = try!(self.get(address));
|
|
|
|
let account = try!(account.change_password(old_password, new_password, self.iterations));
|
2016-06-20 00:10:34 +02:00
|
|
|
|
|
|
|
// save to file
|
|
|
|
self.save(account)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn remove_account(&self, address: &Address, password: &str) -> Result<(), Error> {
|
|
|
|
let can_remove = {
|
2016-07-31 10:44:17 +02:00
|
|
|
let account = try!(self.get(address));
|
2016-06-20 00:10:34 +02:00
|
|
|
account.check_password(password)
|
|
|
|
};
|
|
|
|
|
|
|
|
if can_remove {
|
|
|
|
try!(self.dir.remove(address));
|
2016-10-25 22:34:52 +02:00
|
|
|
let mut cache = self.cache.write();
|
2016-06-20 00:10:34 +02:00
|
|
|
cache.remove(address);
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(Error::InvalidPassword)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-31 10:44:17 +02:00
|
|
|
fn sign(&self, address: &Address, password: &str, message: &Message) -> Result<Signature, Error> {
|
|
|
|
let account = try!(self.get(address));
|
2016-06-20 00:10:34 +02:00
|
|
|
account.sign(password, message)
|
|
|
|
}
|
2016-07-24 17:38:21 +02:00
|
|
|
|
2016-09-22 14:48:22 +02:00
|
|
|
fn decrypt(&self, account: &Address, password: &str, shared_mac: &[u8], message: &[u8]) -> Result<Vec<u8>, Error> {
|
|
|
|
let account = try!(self.get(account));
|
|
|
|
account.decrypt(password, shared_mac, message)
|
|
|
|
}
|
|
|
|
|
2016-10-15 14:44:08 +02:00
|
|
|
fn public(&self, account: &Address, password: &str) -> Result<Public, Error> {
|
|
|
|
let account = try!(self.get(account));
|
|
|
|
account.public(password)
|
|
|
|
}
|
|
|
|
|
2016-07-31 10:44:17 +02:00
|
|
|
fn uuid(&self, address: &Address) -> Result<UUID, Error> {
|
|
|
|
let account = try!(self.get(address));
|
2016-07-24 17:38:21 +02:00
|
|
|
Ok(account.id.into())
|
|
|
|
}
|
|
|
|
|
2016-07-31 10:44:17 +02:00
|
|
|
fn name(&self, address: &Address) -> Result<String, Error> {
|
|
|
|
let account = try!(self.get(address));
|
2016-07-24 17:38:21 +02:00
|
|
|
Ok(account.name.clone())
|
|
|
|
}
|
|
|
|
|
2016-07-31 10:44:17 +02:00
|
|
|
fn meta(&self, address: &Address) -> Result<String, Error> {
|
|
|
|
let account = try!(self.get(address));
|
2016-07-24 17:38:21 +02:00
|
|
|
Ok(account.meta.clone())
|
|
|
|
}
|
|
|
|
|
2016-07-31 10:44:17 +02:00
|
|
|
fn set_name(&self, address: &Address, name: String) -> Result<(), Error> {
|
|
|
|
let mut account = try!(self.get(address));
|
|
|
|
account.name = name;
|
|
|
|
|
2016-07-24 17:38:21 +02:00
|
|
|
// save to file
|
|
|
|
self.save(account)
|
|
|
|
}
|
|
|
|
|
2016-07-31 10:44:17 +02:00
|
|
|
fn set_meta(&self, address: &Address, meta: String) -> Result<(), Error> {
|
|
|
|
let mut account = try!(self.get(address));
|
|
|
|
account.meta = meta;
|
|
|
|
|
2016-07-24 17:38:21 +02:00
|
|
|
// save to file
|
|
|
|
self.save(account)
|
|
|
|
}
|
2016-08-11 18:31:28 +02:00
|
|
|
|
|
|
|
fn local_path(&self) -> String {
|
|
|
|
self.dir.path().map(|p| p.to_string_lossy().into_owned()).unwrap_or_else(|| String::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn list_geth_accounts(&self, testnet: bool) -> Vec<Address> {
|
|
|
|
import::read_geth_accounts(testnet)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn import_geth_accounts(&self, desired: Vec<Address>, testnet: bool) -> Result<Vec<Address>, Error> {
|
|
|
|
import::import_geth_accounts(&*self.dir, desired.into_iter().collect(), testnet)
|
|
|
|
}
|
2016-06-20 00:10:34 +02:00
|
|
|
}
|