From 2c254e3b323f2f6b00d0811b1c89db6463b8b246 Mon Sep 17 00:00:00 2001 From: maciejhirsz Date: Fri, 23 Jun 2017 16:29:45 +0200 Subject: [PATCH] Prioritize accounts over address book --- rpc/src/lib.rs | 2 +- rpc/src/v1/impls/parity_accounts.rs | 46 +++++++++++++++++----------- rpc/src/v1/traits/parity_accounts.rs | 4 +-- rpc/src/v1/types/account_info.rs | 13 ++++++++ rpc/src/v1/types/mod.rs | 2 +- 5 files changed, 45 insertions(+), 22 deletions(-) diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 22c9b98cd..7b8e207b8 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -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; diff --git a/rpc/src/v1/impls/parity_accounts.rs b/rpc/src/v1/impls/parity_accounts.rs index 6bf8a069e..1a121699a 100644 --- a/rpc/src/v1/impls/parity_accounts.rs +++ b/rpc/src/v1/impls/parity_accounts.rs @@ -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}; @@ -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 { @@ -50,26 +50,36 @@ impl ParityAccountsClient { } impl ParityAccounts for ParityAccountsClient { - fn all_accounts_info(&self) -> Result>, Error> { + fn all_accounts_info(&self) -> Result, 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 = 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 { diff --git a/rpc/src/v1/traits/parity_accounts.rs b/rpc/src/v1/traits/parity_accounts.rs index 7d49148b1..73c40ffd4 100644 --- a/rpc/src/v1/traits/parity_accounts.rs +++ b/rpc/src/v1/traits/parity_accounts.rs @@ -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>, Error>; + fn all_accounts_info(&self) -> Result, Error>; /// Creates new account from the given phrase using standard brainwallet mechanism. /// Second parameter is password for the new account. diff --git a/rpc/src/v1/types/account_info.rs b/rpc/src/v1/types/account_info.rs index 077110df0..f9cabb450 100644 --- a/rpc/src/v1/types/account_info.rs +++ b/rpc/src/v1/types/account_info.rs @@ -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, +} + /// Hardware wallet information. #[derive(Debug, Default, Clone, PartialEq, Serialize)] pub struct HwAccountInfo { @@ -29,3 +41,4 @@ pub struct HwAccountInfo { /// Device manufacturer. pub manufacturer: String, } + diff --git a/rpc/src/v1/types/mod.rs b/rpc/src/v1/types/mod.rs index ef90d844b..3e463f958 100644 --- a/rpc/src/v1/types/mod.rs +++ b/rpc/src/v1/types/mod.rs @@ -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;