2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2016-07-25 16:09:47 +02:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2016-07-25 16:09:47 +02:00
|
|
|
// 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.
|
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2016-07-25 16:09:47 +02:00
|
|
|
// 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
|
2020-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-07-25 16:09:47 +02:00
|
|
|
|
2021-03-12 14:55:49 +01:00
|
|
|
use crate::params::SpecType;
|
2019-02-06 17:53:34 +01:00
|
|
|
use std::num::NonZeroU32;
|
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-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 {
|
2019-02-06 17:53:34 +01:00
|
|
|
pub iterations: NonZeroU32,
|
2016-07-25 16:09:47 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
#[cfg(not(feature = "accounts"))]
|
2019-02-14 15:54:44 +01:00
|
|
|
pub fn execute(_cmd: AccountCmd) -> Result<String, String> {
|
2020-09-22 14:53:52 +02:00
|
|
|
Err("Account management is deprecated. Please see #9997 for alternatives:\nhttps://github.com/openethereum/openethereum/issues/9997".into())
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
#[cfg(feature = "accounts")]
|
|
|
|
mod command {
|
|
|
|
use super::*;
|
2021-03-12 14:55:49 +01:00
|
|
|
use crate::{
|
|
|
|
accounts::{AccountProvider, AccountProviderSettings},
|
|
|
|
helpers::{password_from_file, password_prompt},
|
|
|
|
};
|
2020-08-07 19:36:32 +02:00
|
|
|
use ethstore::{accounts_dir::RootDiskDirectory, import_account, import_accounts, EthStore};
|
2019-02-07 14:34:24 +01:00
|
|
|
use std::path::PathBuf;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
pub fn execute(cmd: AccountCmd) -> Result<String, String> {
|
|
|
|
match cmd {
|
|
|
|
AccountCmd::New(new_cmd) => new(new_cmd),
|
|
|
|
AccountCmd::List(list_cmd) => list(list_cmd),
|
|
|
|
AccountCmd::Import(import_cmd) => import(import_cmd),
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2019-02-07 14:34:24 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
fn keys_dir(path: String, spec: SpecType) -> Result<RootDiskDirectory, String> {
|
|
|
|
let spec = spec.spec(&::std::env::temp_dir())?;
|
|
|
|
let mut path = PathBuf::from(&path);
|
|
|
|
path.push(spec.data_dir);
|
|
|
|
RootDiskDirectory::create(path).map_err(|e| format!("Could not open keys directory: {}", e))
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
fn secret_store(
|
|
|
|
dir: Box<RootDiskDirectory>,
|
|
|
|
iterations: Option<NonZeroU32>,
|
|
|
|
) -> 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))
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
fn new(n: NewAccount) -> Result<String, String> {
|
|
|
|
let password = match n.password_file {
|
|
|
|
Some(file) => password_from_file(file)?,
|
|
|
|
None => password_prompt()?,
|
|
|
|
};
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
let dir = Box::new(keys_dir(n.path, n.spec)?);
|
|
|
|
let secret_store = Box::new(secret_store(dir, Some(n.iterations))?);
|
|
|
|
let acc_provider = AccountProvider::new(secret_store, AccountProviderSettings::default());
|
|
|
|
let new_account = acc_provider
|
|
|
|
.new_account(&password)
|
|
|
|
.map_err(|e| format!("Could not create new account: {}", e))?;
|
|
|
|
Ok(format!("0x{:x}", new_account))
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
fn list(list_cmd: ListAccounts) -> Result<String, String> {
|
|
|
|
let dir = Box::new(keys_dir(list_cmd.path, list_cmd.spec)?);
|
|
|
|
let secret_store = Box::new(secret_store(dir, None)?);
|
|
|
|
let acc_provider = AccountProvider::new(secret_store, AccountProviderSettings::default());
|
|
|
|
let accounts = acc_provider.accounts().map_err(|e| format!("{}", e))?;
|
|
|
|
let result = accounts
|
|
|
|
.into_iter()
|
|
|
|
.map(|a| format!("0x{:x}", a))
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("\n");
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
Ok(result)
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
fn import(i: ImportAccounts) -> Result<String, String> {
|
|
|
|
let to = keys_dir(i.to, i.spec)?;
|
|
|
|
let mut imported = 0;
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
for path in &i.from {
|
|
|
|
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;
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2019-02-07 14:34:24 +01:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
|
2019-02-07 14:34:24 +01:00
|
|
|
Ok(format!("{} account(s) imported", imported))
|
|
|
|
}
|
2016-10-04 10:44:47 +02:00
|
|
|
}
|
2019-02-07 14:34:24 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "accounts")]
|
|
|
|
pub use self::command::execute;
|