From 6ab78a00913642d33c9c6d9582683f763eda87e3 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Wed, 10 Aug 2016 21:23:17 +0200 Subject: [PATCH] RPC for deriving address from phrase. Fixed #1911 --- rpc/src/v1/impls/ethcore.rs | 11 +++++++++-- rpc/src/v1/traits/ethcore.rs | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/rpc/src/v1/impls/ethcore.rs b/rpc/src/v1/impls/ethcore.rs index 117228fcc..93544aaf0 100644 --- a/rpc/src/v1/impls/ethcore.rs +++ b/rpc/src/v1/impls/ethcore.rs @@ -15,7 +15,7 @@ // along with Parity. If not, see . //! Ethcore-specific rpc implementation. -use util::{RotatingLogger}; +use util::{RotatingLogger, KeyPair}; use util::misc::version_data; use std::sync::{Arc, Weak}; use std::collections::{BTreeMap}; @@ -24,7 +24,7 @@ use ethcore::client::{MiningBlockChainClient}; use jsonrpc_core::*; use ethcore::miner::MinerService; use v1::traits::Ethcore; -use v1::types::{Bytes, U256}; +use v1::types::{Bytes, U256, H160}; use v1::helpers::{errors, SigningQueue, ConfirmationsQueue, NetworkSettings}; use v1::helpers::params::expect_no_params; @@ -173,4 +173,11 @@ impl Ethcore for EthcoreClient where M: MinerService + 'static, C: M to_value(&random_phrase(12)) } + + fn phrase_to_address(&self, params: Params) -> Result { + try!(self.active()); + from_params::<(String,)>(params).and_then(|(phrase,)| + to_value(&H160::from(KeyPair::from_phrase(&phrase).address())) + ) + } } diff --git a/rpc/src/v1/traits/ethcore.rs b/rpc/src/v1/traits/ethcore.rs index 73411a584..2419c55dd 100644 --- a/rpc/src/v1/traits/ethcore.rs +++ b/rpc/src/v1/traits/ethcore.rs @@ -70,6 +70,9 @@ pub trait Ethcore: Sized + Send + Sync + 'static { /// Returns a cryptographically random phrase sufficient for securely seeding a secret key. fn generate_secret_phrase(&self, _: Params) -> Result; + /// Returns whatever address would be derived from the given phrase if it were to seed a brainwallet. + fn phrase_to_address(&self, _: Params) -> Result; + /// Should be used to convert object to io delegate. fn to_delegate(self) -> IoDelegate { let mut delegate = IoDelegate::new(Arc::new(self)); @@ -90,6 +93,7 @@ pub trait Ethcore: Sized + Send + Sync + 'static { delegate.add_method("ethcore_gasPriceStatistics", Ethcore::gas_price_statistics); delegate.add_method("ethcore_unsignedTransactionsCount", Ethcore::unsigned_transactions_count); delegate.add_method("ethcore_generateSecretPhrase", Ethcore::generate_secret_phrase); + delegate.add_method("ethcore_phraseToAddress", Ethcore::phrase_to_address); delegate }