Merge pull request #2464 from svyatonik/master

Close after importing keys from geth
This commit is contained in:
Nikolay Volf 2016-10-05 11:54:15 +04:00 committed by GitHub
commit 02c04a3193
4 changed files with 46 additions and 27 deletions

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
use ethcore::ethstore::{EthStore, import_accounts}; use ethcore::ethstore::{EthStore, SecretStore, import_accounts, read_geth_accounts};
use ethcore::ethstore::dir::DiskDirectory; use ethcore::ethstore::dir::DiskDirectory;
use ethcore::account_provider::AccountProvider; use ethcore::account_provider::AccountProvider;
use helpers::{password_prompt, password_from_file}; use helpers::{password_prompt, password_from_file};
@ -24,6 +24,7 @@ pub enum AccountCmd {
New(NewAccount), New(NewAccount),
List(String), List(String),
Import(ImportAccounts), Import(ImportAccounts),
ImportFromGeth(ImportFromGethAccounts)
} }
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -39,11 +40,21 @@ pub struct ImportAccounts {
pub to: String, pub to: String,
} }
/// Parameters for geth accounts' import
#[derive(Debug, PartialEq)]
pub struct ImportFromGethAccounts {
/// import mainnet (false) or testnet (true) accounts
pub testnet: bool,
/// directory to import accounts to
pub to: String,
}
pub fn execute(cmd: AccountCmd) -> Result<String, String> { pub fn execute(cmd: AccountCmd) -> Result<String, String> {
match cmd { match cmd {
AccountCmd::New(new_cmd) => new(new_cmd), AccountCmd::New(new_cmd) => new(new_cmd),
AccountCmd::List(path) => list(path), AccountCmd::List(path) => list(path),
AccountCmd::Import(import_cmd) => import(import_cmd), AccountCmd::Import(import_cmd) => import(import_cmd),
AccountCmd::ImportFromGeth(import_geth_cmd) => import_geth(import_geth_cmd)
} }
} }
@ -51,6 +62,13 @@ fn keys_dir(path: String) -> Result<DiskDirectory, String> {
DiskDirectory::create(path).map_err(|e| format!("Could not open keys directory: {}", e)) DiskDirectory::create(path).map_err(|e| format!("Could not open keys directory: {}", e))
} }
fn secret_store(dir: Box<DiskDirectory>, iterations: Option<u32>) -> Result<EthStore, String> {
match iterations {
Some(i) => EthStore::open_with_iterations(dir, i),
_ => EthStore::open(dir)
}.map_err(|e| format!("Could not open keys store: {}", e))
}
fn new(n: NewAccount) -> Result<String, String> { fn new(n: NewAccount) -> Result<String, String> {
let password: String = match n.password_file { let password: String = match n.password_file {
Some(file) => try!(password_from_file(file)), Some(file) => try!(password_from_file(file)),
@ -58,7 +76,7 @@ fn new(n: NewAccount) -> Result<String, String> {
}; };
let dir = Box::new(try!(keys_dir(n.path))); let dir = Box::new(try!(keys_dir(n.path)));
let secret_store = Box::new(EthStore::open_with_iterations(dir, n.iterations).unwrap()); let secret_store = Box::new(try!(secret_store(dir, Some(n.iterations))));
let acc_provider = AccountProvider::new(secret_store); let acc_provider = AccountProvider::new(secret_store);
let new_account = try!(acc_provider.new_account(&password).map_err(|e| format!("Could not create new account: {}", e))); let new_account = try!(acc_provider.new_account(&password).map_err(|e| format!("Could not create new account: {}", e)));
Ok(format!("{:?}", new_account)) Ok(format!("{:?}", new_account))
@ -66,7 +84,7 @@ fn new(n: NewAccount) -> Result<String, String> {
fn list(path: String) -> Result<String, String> { fn list(path: String) -> Result<String, String> {
let dir = Box::new(try!(keys_dir(path))); let dir = Box::new(try!(keys_dir(path)));
let secret_store = Box::new(EthStore::open(dir).unwrap()); let secret_store = Box::new(try!(secret_store(dir, None)));
let acc_provider = AccountProvider::new(secret_store); let acc_provider = AccountProvider::new(secret_store);
let accounts = acc_provider.accounts(); let accounts = acc_provider.accounts();
let result = accounts.into_iter() let result = accounts.into_iter()
@ -86,3 +104,17 @@ fn import(i: ImportAccounts) -> Result<String, String> {
} }
Ok(format!("{}", imported)) Ok(format!("{}", imported))
} }
fn import_geth(i: ImportFromGethAccounts) -> Result<String, String> {
use std::io::ErrorKind;
use ethcore::ethstore::Error;
let dir = Box::new(try!(keys_dir(i.to)));
let secret_store = Box::new(try!(secret_store(dir, None)));
let geth_accounts = read_geth_accounts(i.testnet);
match secret_store.import_geth_accounts(geth_accounts, i.testnet) {
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))
}
}

View File

@ -39,7 +39,7 @@ use signer::Configuration as SignerConfiguration;
use run::RunCmd; use run::RunCmd;
use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, DataFormat}; use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, DataFormat};
use presale::ImportWallet; use presale::ImportWallet;
use account::{AccountCmd, NewAccount, ImportAccounts}; use account::{AccountCmd, NewAccount, ImportAccounts, ImportFromGethAccounts};
use snapshot::{self, SnapshotCommand}; use snapshot::{self, SnapshotCommand};
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
@ -120,6 +120,14 @@ impl Configuration {
unreachable!(); unreachable!();
}; };
Cmd::Account(account_cmd) Cmd::Account(account_cmd)
} else if self.args.flag_import_geth_keys {
let account_cmd = AccountCmd::ImportFromGeth(
ImportFromGethAccounts {
to: dirs.keys,
testnet: self.args.flag_testnet
}
);
Cmd::Account(account_cmd)
} else if self.args.cmd_wallet { } else if self.args.cmd_wallet {
let presale_cmd = ImportWallet { let presale_cmd = ImportWallet {
iterations: self.args.flag_keys_iterations, iterations: self.args.flag_keys_iterations,
@ -319,7 +327,6 @@ impl Configuration {
fn accounts_config(&self) -> Result<AccountsConfig, String> { fn accounts_config(&self) -> Result<AccountsConfig, String> {
let cfg = AccountsConfig { let cfg = AccountsConfig {
iterations: self.args.flag_keys_iterations, iterations: self.args.flag_keys_iterations,
import_keys: self.args.flag_import_geth_keys,
testnet: self.args.flag_testnet, testnet: self.args.flag_testnet,
password_files: self.args.flag_password.clone(), password_files: self.args.flag_password.clone(),
unlocked_accounts: try!(to_addresses(&self.args.flag_unlock)), unlocked_accounts: try!(to_addresses(&self.args.flag_unlock)),

View File

@ -142,7 +142,6 @@ impl str::FromStr for ResealPolicy {
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub struct AccountsConfig { pub struct AccountsConfig {
pub iterations: u32, pub iterations: u32,
pub import_keys: bool,
pub testnet: bool, pub testnet: bool,
pub password_files: Vec<String>, pub password_files: Vec<String>,
pub unlocked_accounts: Vec<Address>, pub unlocked_accounts: Vec<Address>,
@ -152,7 +151,6 @@ impl Default for AccountsConfig {
fn default() -> Self { fn default() -> Self {
AccountsConfig { AccountsConfig {
iterations: 10240, iterations: 10240,
import_keys: false,
testnet: false, testnet: false,
password_files: Vec::new(), password_files: Vec::new(),
unlocked_accounts: Vec::new(), unlocked_accounts: Vec::new(),

View File

@ -15,7 +15,6 @@
// along with Parity. If not, see <http://www.gnu.org/licenses/>. // along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::sync::{Arc, Mutex, Condvar}; use std::sync::{Arc, Mutex, Condvar};
use std::io::ErrorKind;
use ctrlc::CtrlC; use ctrlc::CtrlC;
use fdlimit::raise_fd_limit; use fdlimit::raise_fd_limit;
use ethcore_logger::{Config as LogConfig, setup_log}; use ethcore_logger::{Config as LogConfig, setup_log};
@ -360,28 +359,11 @@ fn daemonize(_pid_file: String) -> Result<(), String> {
} }
fn prepare_account_provider(dirs: &Directories, cfg: AccountsConfig) -> Result<AccountProvider, String> { fn prepare_account_provider(dirs: &Directories, cfg: AccountsConfig) -> Result<AccountProvider, String> {
use ethcore::ethstore::{import_accounts, EthStore}; use ethcore::ethstore::EthStore;
use ethcore::ethstore::dir::{GethDirectory, DirectoryType, DiskDirectory}; use ethcore::ethstore::dir::DiskDirectory;
use ethcore::ethstore::Error;
let passwords = try!(passwords_from_files(cfg.password_files)); let passwords = try!(passwords_from_files(cfg.password_files));
if cfg.import_keys {
let t = if cfg.testnet {
DirectoryType::Testnet
} else {
DirectoryType::Main
};
let from = GethDirectory::open(t);
let to = try!(DiskDirectory::create(dirs.keys.clone()).map_err(|e| format!("Could not open keys directory: {}", e)));
match import_accounts(&from, &to) {
Ok(_) => {}
Err(Error::Io(ref io_err)) if io_err.kind() == ErrorKind::NotFound => {}
Err(err) => warn!("Import geth accounts failed. {}", err)
}
}
let dir = Box::new(try!(DiskDirectory::create(dirs.keys.clone()).map_err(|e| format!("Could not open keys directory: {}", e)))); let dir = Box::new(try!(DiskDirectory::create(dirs.keys.clone()).map_err(|e| format!("Could not open keys directory: {}", e))));
let account_service = AccountProvider::new(Box::new( let account_service = AccountProvider::new(Box::new(
try!(EthStore::open_with_iterations(dir, cfg.iterations).map_err(|e| format!("Could not open keys directory: {}", e))) try!(EthStore::open_with_iterations(dir, cfg.iterations).map_err(|e| format!("Could not open keys directory: {}", e)))