From cb0e0abc4a89429148f0f58f124ef6690cbb7e81 Mon Sep 17 00:00:00 2001 From: svyatonik Date: Tue, 4 Oct 2016 11:44:47 +0300 Subject: [PATCH 1/3] closes ethcore/parity#1918 --- parity/account.rs | 24 +++++++++++++++++++++++- parity/configuration.rs | 11 +++++++++-- parity/params.rs | 2 -- parity/run.rs | 22 ++-------------------- 4 files changed, 34 insertions(+), 25 deletions(-) diff --git a/parity/account.rs b/parity/account.rs index 26a974090..72a932315 100644 --- a/parity/account.rs +++ b/parity/account.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -use ethcore::ethstore::{EthStore, import_accounts}; +use ethcore::ethstore::{EthStore, SecretStore, import_accounts, read_geth_accounts}; use ethcore::ethstore::dir::DiskDirectory; use ethcore::account_provider::AccountProvider; use helpers::{password_prompt, password_from_file}; @@ -24,6 +24,7 @@ pub enum AccountCmd { New(NewAccount), List(String), Import(ImportAccounts), + ImportFromGeth(ImportFromGethAccounts) } #[derive(Debug, PartialEq)] @@ -39,11 +40,18 @@ pub struct ImportAccounts { pub to: String, } +#[derive(Debug, PartialEq)] +pub struct ImportFromGethAccounts { + pub testnet: bool, + pub to: String, +} + pub fn execute(cmd: AccountCmd) -> Result { match cmd { AccountCmd::New(new_cmd) => new(new_cmd), AccountCmd::List(path) => list(path), AccountCmd::Import(import_cmd) => import(import_cmd), + AccountCmd::ImportFromGeth(import_geth_cmd) => import_geth(import_geth_cmd) } } @@ -86,3 +94,17 @@ fn import(i: ImportAccounts) -> Result { } Ok(format!("{}", imported)) } + +fn import_geth(i: ImportFromGethAccounts) -> Result { + use std::io::ErrorKind; + use ethcore::ethstore::Error; + + let dir = Box::new(try!(keys_dir(i.to))); + let secret_store = Box::new(EthStore::open(dir).unwrap()); + 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)) + } +} diff --git a/parity/configuration.rs b/parity/configuration.rs index 8eb617d2b..811ba6097 100644 --- a/parity/configuration.rs +++ b/parity/configuration.rs @@ -39,7 +39,7 @@ use signer::Configuration as SignerConfiguration; use run::RunCmd; use blockchain::{BlockchainCmd, ImportBlockchain, ExportBlockchain, DataFormat}; use presale::ImportWallet; -use account::{AccountCmd, NewAccount, ImportAccounts}; +use account::{AccountCmd, NewAccount, ImportAccounts, ImportFromGethAccounts}; use snapshot::{self, SnapshotCommand}; #[derive(Debug, PartialEq)] @@ -120,6 +120,14 @@ impl Configuration { unreachable!(); }; 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 { let presale_cmd = ImportWallet { iterations: self.args.flag_keys_iterations, @@ -319,7 +327,6 @@ impl Configuration { fn accounts_config(&self) -> Result { let cfg = AccountsConfig { iterations: self.args.flag_keys_iterations, - import_keys: self.args.flag_import_geth_keys, testnet: self.args.flag_testnet, password_files: self.args.flag_password.clone(), unlocked_accounts: try!(to_addresses(&self.args.flag_unlock)), diff --git a/parity/params.rs b/parity/params.rs index a1b28406a..faba029b2 100644 --- a/parity/params.rs +++ b/parity/params.rs @@ -142,7 +142,6 @@ impl str::FromStr for ResealPolicy { #[derive(Debug, PartialEq)] pub struct AccountsConfig { pub iterations: u32, - pub import_keys: bool, pub testnet: bool, pub password_files: Vec, pub unlocked_accounts: Vec
, @@ -152,7 +151,6 @@ impl Default for AccountsConfig { fn default() -> Self { AccountsConfig { iterations: 10240, - import_keys: false, testnet: false, password_files: Vec::new(), unlocked_accounts: Vec::new(), diff --git a/parity/run.rs b/parity/run.rs index ef84d75a2..528b207c6 100644 --- a/parity/run.rs +++ b/parity/run.rs @@ -15,7 +15,6 @@ // along with Parity. If not, see . use std::sync::{Arc, Mutex, Condvar}; -use std::io::ErrorKind; use ctrlc::CtrlC; use fdlimit::raise_fd_limit; 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 { - use ethcore::ethstore::{import_accounts, EthStore}; - use ethcore::ethstore::dir::{GethDirectory, DirectoryType, DiskDirectory}; - use ethcore::ethstore::Error; + use ethcore::ethstore::EthStore; + use ethcore::ethstore::dir::DiskDirectory; 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 account_service = AccountProvider::new(Box::new( try!(EthStore::open_with_iterations(dir, cfg.iterations).map_err(|e| format!("Could not open keys directory: {}", e))) From abc5db0f80b84e8c20d4649ce475f6b601a00c42 Mon Sep 17 00:00:00 2001 From: svyatonik Date: Tue, 4 Oct 2016 16:01:39 +0300 Subject: [PATCH 2/3] Remove redundant Box-ing. --- parity/account.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parity/account.rs b/parity/account.rs index 72a932315..6a05e945d 100644 --- a/parity/account.rs +++ b/parity/account.rs @@ -100,7 +100,7 @@ fn import_geth(i: ImportFromGethAccounts) -> Result { use ethcore::ethstore::Error; let dir = Box::new(try!(keys_dir(i.to))); - let secret_store = Box::new(EthStore::open(dir).unwrap()); + let secret_store = EthStore::open(dir).unwrap(); 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())), From 0e8dda740fcda5c4bd37a23f32a8386a86e8fd0a Mon Sep 17 00:00:00 2001 From: svyatonik Date: Wed, 5 Oct 2016 00:13:07 +0300 Subject: [PATCH 3/3] * PR 2464: human-readable error message + struct documentation --- parity/account.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/parity/account.rs b/parity/account.rs index 6a05e945d..9d400cab5 100644 --- a/parity/account.rs +++ b/parity/account.rs @@ -40,9 +40,12 @@ pub struct ImportAccounts { 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, } @@ -59,6 +62,13 @@ fn keys_dir(path: String) -> Result { DiskDirectory::create(path).map_err(|e| format!("Could not open keys directory: {}", e)) } +fn secret_store(dir: Box, iterations: Option) -> Result { + 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 { let password: String = match n.password_file { Some(file) => try!(password_from_file(file)), @@ -66,7 +76,7 @@ fn new(n: NewAccount) -> Result { }; 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 new_account = try!(acc_provider.new_account(&password).map_err(|e| format!("Could not create new account: {}", e))); Ok(format!("{:?}", new_account)) @@ -74,7 +84,7 @@ fn new(n: NewAccount) -> Result { fn list(path: String) -> Result { 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 accounts = acc_provider.accounts(); let result = accounts.into_iter() @@ -100,7 +110,7 @@ fn import_geth(i: ImportFromGethAccounts) -> Result { use ethcore::ethstore::Error; let dir = Box::new(try!(keys_dir(i.to))); - let secret_store = EthStore::open(dir).unwrap(); + 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())),