Add import of raw private key RPCs (#2942)

* Importing an account from raw secret

* Add jsapi & jsonrpc for personal_newAccountFromSecret
This commit is contained in:
Jaco Greeff
2016-10-28 16:46:25 +02:00
committed by Gav Wood
parent 6abd08f5b2
commit 8d66fc50e2
4 changed files with 45 additions and 3 deletions

View File

@@ -20,7 +20,7 @@ use util::{Address};
use jsonrpc_core::*;
use ethkey::{Brain, Generator};
use v1::traits::PersonalAccounts;
use v1::types::{H160 as RpcH160, TransactionRequest};
use v1::types::{H160 as RpcH160, H256 as RpcH256, TransactionRequest};
use v1::helpers::errors;
use v1::helpers::params::expect_no_params;
use v1::helpers::dispatch::sign_and_dispatch;
@@ -95,6 +95,19 @@ impl<C: 'static, M: 'static> PersonalAccounts for PersonalAccountsClient<C, M> w
)
}
fn new_account_from_secret(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
from_params::<(RpcH256, String, )>(params).and_then(
|(secret, pass, )| {
let store = take_weak!(self.accounts);
match store.insert_account(secret.into(), &pass) {
Ok(address) => Ok(to_value(&RpcH160::from(address))),
Err(e) => Err(errors::account("Could not create account.", e)),
}
}
)
}
fn unlock_account(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
from_params::<(RpcH160, String, Option<u64>)>(params).and_then(

View File

@@ -52,6 +52,10 @@ pub trait PersonalAccounts: Sized + Send + Sync + 'static {
/// Second parameter is password for the wallet and the new account.
fn new_account_from_wallet(&self, params: Params) -> Result<Value, Error>;
/// Creates new account from the given raw secret.
/// Second parameter is password for the new account.
fn new_account_from_secret(&self, params: Params) -> Result<Value, Error>;
/// Unlocks specified account for use (can only be one unlocked account at one moment)
fn unlock_account(&self, _: Params) -> Result<Value, Error>;
@@ -84,6 +88,7 @@ pub trait PersonalAccounts: Sized + Send + Sync + 'static {
delegate.add_method("personal_newAccount", PersonalAccounts::new_account);
delegate.add_method("personal_newAccountFromPhrase", PersonalAccounts::new_account_from_phrase);
delegate.add_method("personal_newAccountFromWallet", PersonalAccounts::new_account_from_wallet);
delegate.add_method("personal_newAccountFromSecret", PersonalAccounts::new_account_from_secret);
delegate.add_method("personal_unlockAccount", PersonalAccounts::unlock_account);
delegate.add_method("personal_testPassword", PersonalAccounts::test_password);
delegate.add_method("personal_changePassword", PersonalAccounts::change_password);