2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-07-25 16:09:47 +02:00
|
|
|
// 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-12-12 16:51:07 +01:00
|
|
|
use std::path::PathBuf;
|
2017-05-19 17:14:47 +02:00
|
|
|
use ethcore::ethstore::{EthStore, SecretStore, import_account, import_accounts, read_geth_accounts};
|
2017-12-24 09:34:43 +01:00
|
|
|
use ethcore::ethstore::accounts_dir::RootDiskDirectory;
|
2017-01-30 11:44:09 +01:00
|
|
|
use ethcore::ethstore::SecretVaultRef;
|
2017-02-10 01:07:06 +01:00
|
|
|
use ethcore::account_provider::{AccountProvider, AccountProviderSettings};
|
2016-07-25 16:09:47 +02:00
|
|
|
use helpers::{password_prompt, password_from_file};
|
2016-12-12 16:51:07 +01:00
|
|
|
use params::SpecType;
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum AccountCmd {
|
|
|
|
New(NewAccount),
|
2016-12-12 16:51:07 +01:00
|
|
|
List(ListAccounts),
|
2016-07-25 16:09:47 +02:00
|
|
|
Import(ImportAccounts),
|
2016-10-04 10:44:47 +02:00
|
|
|
ImportFromGeth(ImportFromGethAccounts)
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
2016-12-12 16:51:07 +01:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct ListAccounts {
|
|
|
|
pub path: String,
|
|
|
|
pub spec: SpecType,
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct NewAccount {
|
|
|
|
pub iterations: u32,
|
|
|
|
pub path: String,
|
2016-12-12 16:51:07 +01:00
|
|
|
pub spec: SpecType,
|
2016-07-25 16:09:47 +02:00
|
|
|
pub password_file: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct ImportAccounts {
|
|
|
|
pub from: Vec<String>,
|
|
|
|
pub to: String,
|
2016-12-12 16:51:07 +01:00
|
|
|
pub spec: SpecType,
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
2017-05-19 17:14:47 +02:00
|
|
|
/// Parameters for geth accounts' import
|
2016-10-04 10:44:47 +02:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct ImportFromGethAccounts {
|
2017-05-19 17:14:47 +02:00
|
|
|
/// import mainnet (false) or testnet (true) accounts
|
2016-10-04 10:44:47 +02:00
|
|
|
pub testnet: bool,
|
2016-10-04 23:13:07 +02:00
|
|
|
/// directory to import accounts to
|
2016-10-04 10:44:47 +02:00
|
|
|
pub to: String,
|
2016-12-12 16:51:07 +01:00
|
|
|
pub spec: SpecType,
|
2016-10-04 10:44:47 +02:00
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
pub fn execute(cmd: AccountCmd) -> Result<String, String> {
|
|
|
|
match cmd {
|
|
|
|
AccountCmd::New(new_cmd) => new(new_cmd),
|
2016-12-12 16:51:07 +01:00
|
|
|
AccountCmd::List(list_cmd) => list(list_cmd),
|
2016-07-25 16:09:47 +02:00
|
|
|
AccountCmd::Import(import_cmd) => import(import_cmd),
|
2016-10-04 10:44:47 +02:00
|
|
|
AccountCmd::ImportFromGeth(import_geth_cmd) => import_geth(import_geth_cmd)
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-30 11:44:09 +01:00
|
|
|
fn keys_dir(path: String, spec: SpecType) -> Result<RootDiskDirectory, String> {
|
2017-07-10 12:57:40 +02:00
|
|
|
let spec = spec.spec(&::std::env::temp_dir())?;
|
2016-12-12 16:51:07 +01:00
|
|
|
let mut path = PathBuf::from(&path);
|
|
|
|
path.push(spec.data_dir);
|
2017-01-30 11:44:09 +01:00
|
|
|
RootDiskDirectory::create(path).map_err(|e| format!("Could not open keys directory: {}", e))
|
2016-08-03 17:58:22 +02:00
|
|
|
}
|
|
|
|
|
2017-01-30 11:44:09 +01:00
|
|
|
fn secret_store(dir: Box<RootDiskDirectory>, iterations: Option<u32>) -> Result<EthStore, String> {
|
2016-10-04 23:13:07 +02:00
|
|
|
match iterations {
|
|
|
|
Some(i) => EthStore::open_with_iterations(dir, i),
|
2017-05-19 17:12:20 +02:00
|
|
|
_ => EthStore::open(dir)
|
2016-10-04 23:13:07 +02:00
|
|
|
}.map_err(|e| format!("Could not open keys store: {}", e))
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
fn new(n: NewAccount) -> Result<String, String> {
|
|
|
|
let password: String = match n.password_file {
|
2016-12-27 12:53:56 +01:00
|
|
|
Some(file) => password_from_file(file)?,
|
|
|
|
None => password_prompt()?,
|
2016-07-25 16:09:47 +02:00
|
|
|
};
|
|
|
|
|
2016-12-27 12:53:56 +01:00
|
|
|
let dir = Box::new(keys_dir(n.path, n.spec)?);
|
|
|
|
let secret_store = Box::new(secret_store(dir, Some(n.iterations))?);
|
2017-02-10 01:07:06 +01:00
|
|
|
let acc_provider = AccountProvider::new(secret_store, AccountProviderSettings::default());
|
2016-12-27 12:53:56 +01:00
|
|
|
let new_account = acc_provider.new_account(&password).map_err(|e| format!("Could not create new account: {}", e))?;
|
2017-05-19 17:14:47 +02:00
|
|
|
Ok(format!("0x{:?}", new_account))
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
2016-12-12 16:51:07 +01:00
|
|
|
fn list(list_cmd: ListAccounts) -> Result<String, String> {
|
2016-12-27 12:53:56 +01:00
|
|
|
let dir = Box::new(keys_dir(list_cmd.path, list_cmd.spec)?);
|
|
|
|
let secret_store = Box::new(secret_store(dir, None)?);
|
2017-02-10 01:07:06 +01:00
|
|
|
let acc_provider = AccountProvider::new(secret_store, AccountProviderSettings::default());
|
2017-05-19 17:14:47 +02:00
|
|
|
let accounts = acc_provider.accounts().map_err(|e| format!("{}", e))?;
|
2016-07-25 16:09:47 +02:00
|
|
|
let result = accounts.into_iter()
|
2017-05-19 17:14:47 +02:00
|
|
|
.map(|a| format!("0x{:?}", a))
|
2016-07-25 16:09:47 +02:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("\n");
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn import(i: ImportAccounts) -> Result<String, String> {
|
2016-12-27 12:53:56 +01:00
|
|
|
let to = keys_dir(i.to, i.spec)?;
|
2016-07-25 16:09:47 +02:00
|
|
|
let mut imported = 0;
|
2017-05-19 17:14:47 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
for path in &i.from {
|
2017-05-19 17:14:47 +02:00
|
|
|
let path = PathBuf::from(path);
|
|
|
|
if path.is_dir() {
|
|
|
|
let from = RootDiskDirectory::at(&path);
|
|
|
|
imported += import_accounts(&from, &to).map_err(|e| format!("Importing accounts from {:?} failed: {}", path, e))?.len();
|
|
|
|
} else if path.is_file() {
|
|
|
|
import_account(&path, &to).map_err(|e| format!("Importing account from {:?} failed: {}", path, e))?;
|
|
|
|
imported += 1;
|
|
|
|
}
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
2017-05-19 17:14:47 +02:00
|
|
|
|
2016-11-21 15:03:20 +01:00
|
|
|
Ok(format!("{} account(s) imported", imported))
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
2016-10-04 10:44:47 +02:00
|
|
|
|
|
|
|
fn import_geth(i: ImportFromGethAccounts) -> Result<String, String> {
|
|
|
|
use std::io::ErrorKind;
|
|
|
|
use ethcore::ethstore::Error;
|
|
|
|
|
2016-12-27 12:53:56 +01:00
|
|
|
let dir = Box::new(keys_dir(i.to, i.spec)?);
|
|
|
|
let secret_store = Box::new(secret_store(dir, None)?);
|
2016-10-04 10:44:47 +02:00
|
|
|
let geth_accounts = read_geth_accounts(i.testnet);
|
2017-01-30 11:44:09 +01:00
|
|
|
match secret_store.import_geth_accounts(SecretVaultRef::Root, geth_accounts, i.testnet) {
|
2016-10-04 10:44:47 +02:00
|
|
|
Ok(v) => Ok(format!("Successfully imported {} account(s) from geth.", v.len())),
|
|
|
|
Err(Error::Io(ref io_err)) if io_err.kind() == ErrorKind::NotFound => Err("Failed to find geth keys folder.".into()),
|
|
|
|
Err(err) => Err(format!("Import geth accounts failed. {}", err))
|
|
|
|
}
|
|
|
|
}
|