Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #5909 from paritytech/mh-always-show-accounts
Browse files Browse the repository at this point in the history
Prioritize accounts over address book
  • Loading branch information
maciejhirsz authored Jun 23, 2017
2 parents e73569c + 2c254e3 commit 02edc95
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 22 deletions.
2 changes: 1 addition & 1 deletion rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extern crate stats;

#[macro_use]
extern crate log;
#[macro_use]
#[cfg_attr(test, macro_use)]
extern crate ethcore_util as util;
#[macro_use]
extern crate jsonrpc_macros;
Expand Down
46 changes: 28 additions & 18 deletions rpc/src/v1/impls/parity_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Account management (personal) rpc implementation
use std::sync::Arc;
use std::collections::BTreeMap;
use std::collections::btree_map::{BTreeMap, Entry};
use util::Address;

use ethkey::{Brain, Generator, Secret};
Expand All @@ -27,7 +27,7 @@ use jsonrpc_core::Error;
use v1::helpers::errors;
use v1::helpers::accounts::unwrap_provider;
use v1::traits::ParityAccounts;
use v1::types::{H160 as RpcH160, H256 as RpcH256, H520 as RpcH520, DappId, Derive, DeriveHierarchical, DeriveHash};
use v1::types::{H160 as RpcH160, H256 as RpcH256, H520 as RpcH520, DappId, Derive, DeriveHierarchical, DeriveHash, ExtAccountInfo};

/// Account management (personal) rpc implementation.
pub struct ParityAccountsClient {
Expand All @@ -50,26 +50,36 @@ impl ParityAccountsClient {
}

impl ParityAccounts for ParityAccountsClient {
fn all_accounts_info(&self) -> Result<BTreeMap<RpcH160, BTreeMap<String, String>>, Error> {
fn all_accounts_info(&self) -> Result<BTreeMap<RpcH160, ExtAccountInfo>, Error> {
let store = self.account_provider()?;
let info = store.accounts_info().map_err(|e| errors::account("Could not fetch account info.", e))?;
let other = store.addresses_info();

Ok(info
.into_iter()
.chain(other.into_iter())
.map(|(address, v)| {
let mut m = map![
"name".to_owned() => v.name,
"meta".to_owned() => v.meta
];
if let &Some(ref uuid) = &v.uuid {
m.insert("uuid".to_owned(), format!("{}", uuid));
}
(address.into(), m)
})
.collect()
)
let account_iter = info
.into_iter()
.chain(other.into_iter())
.map(|(address, v)| (address.into(), ExtAccountInfo {
name: v.name,
meta: v.meta,
uuid: v.uuid.map(|uuid| uuid.to_string())
}));

let mut accounts: BTreeMap<RpcH160, ExtAccountInfo> = BTreeMap::new();

for (address, account) in account_iter {
match accounts.entry(address) {
/// Insert only if occupied entry isn't already an account with UUID
Entry::Occupied(ref mut occupied) if occupied.get().uuid.is_none() => {
occupied.insert(account);
},
Entry::Vacant(vacant) => {
vacant.insert(account);
},
_ => {}
};
}

Ok(accounts)
}

fn new_account_from_phrase(&self, phrase: String, pass: String) -> Result<RpcH160, Error> {
Expand Down
4 changes: 2 additions & 2 deletions rpc/src/v1/traits/parity_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ use std::collections::BTreeMap;

use jsonrpc_core::Error;
use ethstore::KeyFile;
use v1::types::{H160, H256, H520, DappId, DeriveHash, DeriveHierarchical};
use v1::types::{H160, H256, H520, DappId, DeriveHash, DeriveHierarchical, ExtAccountInfo};

build_rpc_trait! {
/// Personal Parity rpc interface.
pub trait ParityAccounts {
/// Returns accounts information.
#[rpc(name = "parity_allAccountsInfo")]
fn all_accounts_info(&self) -> Result<BTreeMap<H160, BTreeMap<String, String>>, Error>;
fn all_accounts_info(&self) -> Result<BTreeMap<H160, ExtAccountInfo>, Error>;

/// Creates new account from the given phrase using standard brainwallet mechanism.
/// Second parameter is password for the new account.
Expand Down
13 changes: 13 additions & 0 deletions rpc/src/v1/types/account_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ pub struct AccountInfo {
pub name: String,
}

/// Extended account information (used by `parity_allAccountInfo`).
#[derive(Debug, Default, Clone, PartialEq, Serialize)]
pub struct ExtAccountInfo {
/// Account name
pub name: String,
/// Account meta JSON
pub meta: String,
/// Account UUID (`None` for address book entries)
#[serde(skip_serializing_if = "Option::is_none")]
pub uuid: Option<String>,
}

/// Hardware wallet information.
#[derive(Debug, Default, Clone, PartialEq, Serialize)]
pub struct HwAccountInfo {
Expand All @@ -29,3 +41,4 @@ pub struct HwAccountInfo {
/// Device manufacturer.
pub manufacturer: String,
}

2 changes: 1 addition & 1 deletion rpc/src/v1/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod work;

pub mod pubsub;

pub use self::account_info::{AccountInfo, HwAccountInfo};
pub use self::account_info::{AccountInfo, ExtAccountInfo, HwAccountInfo};
pub use self::bytes::Bytes;
pub use self::block::{RichBlock, Block, BlockTransactions, Header, RichHeader, Rich};
pub use self::block_number::BlockNumber;
Expand Down

0 comments on commit 02edc95

Please sign in to comment.