RPC for importing geth keys (#1916)

* Address book for local addresses.

* Simple disk-backed address book.

* Add test and keep file with keys.

* View and import Geth keys.

* Fix test.

* Fix up author info.

[ci:skip]
This commit is contained in:
Gav Wood
2016-08-11 18:31:28 +02:00
committed by GitHub
parent f114a933a3
commit ca54b8e493
16 changed files with 307 additions and 39 deletions

View File

@@ -148,18 +148,20 @@ impl<C: 'static, M: 'static> Personal for PersonalClient<C, M> where C: MiningBl
fn set_account_name(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
let store = take_weak!(self.accounts);
from_params::<(RpcH160, _)>(params).and_then(|(addr, name)| {
from_params::<(RpcH160, String)>(params).and_then(|(addr, name)| {
let addr: Address = addr.into();
store.set_account_name(addr, name).map_err(|e| errors::account("Could not set account name.", e)).map(|_| Value::Null)
store.set_account_name(addr.clone(), name.clone()).or_else(|_| store.set_address_name(addr, name)).expect("set_address_name always returns Ok; qed");
Ok(Value::Null)
})
}
fn set_account_meta(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
let store = take_weak!(self.accounts);
from_params::<(RpcH160, _)>(params).and_then(|(addr, meta)| {
from_params::<(RpcH160, String)>(params).and_then(|(addr, meta)| {
let addr: Address = addr.into();
store.set_account_meta(addr, meta).map_err(|e| errors::account("Could not set account meta.", e)).map(|_| Value::Null)
store.set_account_meta(addr.clone(), meta.clone()).or_else(|_| store.set_address_meta(addr, meta)).expect("set_address_meta always returns Ok; qed");
Ok(Value::Null)
})
}
@@ -168,7 +170,8 @@ impl<C: 'static, M: 'static> Personal for PersonalClient<C, M> where C: MiningBl
try!(expect_no_params(params));
let store = take_weak!(self.accounts);
let info = try!(store.accounts_info().map_err(|e| errors::account("Could not fetch account info.", e)));
Ok(Value::Object(info.into_iter().map(|(a, v)| {
let other = store.addresses_info().expect("addresses_info always returns Ok; qed");
Ok(Value::Object(info.into_iter().chain(other.into_iter()).map(|(a, v)| {
let m = map![
"name".to_owned() => to_value(&v.name).unwrap(),
"meta".to_owned() => to_value(&v.meta).unwrap(),
@@ -181,4 +184,21 @@ impl<C: 'static, M: 'static> Personal for PersonalClient<C, M> where C: MiningBl
(format!("0x{}", a.hex()), Value::Object(m))
}).collect::<BTreeMap<_, _>>()))
}
fn geth_accounts(&self, params: Params) -> Result<Value, Error> {
try!(self.active());
try!(expect_no_params(params));
let store = take_weak!(self.accounts);
to_value(&store.list_geth_accounts(false).into_iter().map(Into::into).collect::<Vec<RpcH160>>())
}
fn import_geth_accounts(&self, params: Params) -> Result<Value, Error> {
from_params::<(Vec<RpcH160>,)>(params).and_then(|(addresses,)| {
let store = take_weak!(self.accounts);
to_value(&try!(store
.import_geth_accounts(addresses.into_iter().map(Into::into).collect(), false)
.map_err(|e| errors::account("Couldn't import Geth accounts", e))
).into_iter().map(Into::into).collect::<Vec<RpcH160>>())
})
}
}

View File

@@ -54,6 +54,12 @@ pub trait Personal: Sized + Send + Sync + 'static {
/// Returns accounts information.
fn accounts_info(&self, _: Params) -> Result<Value, Error>;
/// Returns the accounts available for importing from Geth.
fn geth_accounts(&self, _: Params) -> Result<Value, Error>;
/// Imports a number of Geth accounts, with the list provided as the argument.
fn import_geth_accounts(&self, _: Params) -> Result<Value, Error>;
/// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self));
@@ -67,6 +73,8 @@ pub trait Personal: Sized + Send + Sync + 'static {
delegate.add_method("personal_setAccountName", Personal::set_account_name);
delegate.add_method("personal_setAccountMeta", Personal::set_account_meta);
delegate.add_method("personal_accountsInfo", Personal::accounts_info);
delegate.add_method("personal_listGethAccounts", Personal::geth_accounts);
delegate.add_method("personal_importGethAccounts", Personal::import_geth_accounts);
delegate
}