Validate dapps accounts with address book (#4407)

* Parametrize address book

* Improving types in account_provider

* Filtering dapps_accounts

* Fixing RPC tests
This commit is contained in:
Tomasz Drwięga
2017-02-03 13:56:48 +01:00
committed by Gav Wood
parent acf41d6f27
commit 853aae2b92
9 changed files with 89 additions and 65 deletions

View File

@@ -130,7 +130,7 @@ impl<C, M, S: ?Sized, U> Parity for ParityClient<C, M, S, U> where
.into_iter().collect::<HashSet<_>>();
let info = store.accounts_info().map_err(|e| errors::account("Could not fetch account info.", e))?;
let other = store.addresses_info().expect("addresses_info always returns Ok; qed");
let other = store.addresses_info();
Ok(info
.into_iter()

View File

@@ -51,22 +51,27 @@ impl<C> ParityAccountsClient<C> where C: MiningBlockChainClient {
}
impl<C: 'static> ParityAccounts for ParityAccountsClient<C> where C: MiningBlockChainClient {
fn all_accounts_info(&self) -> Result<BTreeMap<String, BTreeMap<String, String>>, Error> {
fn all_accounts_info(&self) -> Result<BTreeMap<RpcH160, BTreeMap<String, String>>, Error> {
self.active()?;
let store = take_weak!(self.accounts);
let info = store.accounts_info().map_err(|e| errors::account("Could not fetch account info.", e))?;
let other = store.addresses_info().expect("addresses_info always returns Ok; qed");
let other = store.addresses_info();
Ok(info.into_iter().chain(other.into_iter()).map(|(a, 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));
}
(format!("0x{}", a.hex()), m)
}).collect())
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()
)
}
fn new_account_from_phrase(&self, phrase: String, pass: String) -> Result<RpcH160, Error> {
@@ -132,8 +137,7 @@ impl<C: 'static> ParityAccounts for ParityAccountsClient<C> where C: MiningBlock
let store = take_weak!(self.accounts);
let addr: Address = addr.into();
store.remove_address(addr)
.expect("remove_address always returns Ok; qed");
store.remove_address(addr);
Ok(true)
}
@@ -143,8 +147,7 @@ impl<C: 'static> ParityAccounts for ParityAccountsClient<C> where C: MiningBlock
let addr: Address = addr.into();
store.set_account_name(addr.clone(), name.clone())
.or_else(|_| store.set_address_name(addr, name))
.expect("set_address_name always returns Ok; qed");
.unwrap_or_else(|_| store.set_address_name(addr, name));
Ok(true)
}
@@ -154,8 +157,7 @@ impl<C: 'static> ParityAccounts for ParityAccountsClient<C> where C: MiningBlock
let addr: Address = addr.into();
store.set_account_meta(addr.clone(), meta.clone())
.or_else(|_| store.set_address_meta(addr, meta))
.expect("set_address_meta always returns Ok; qed");
.unwrap_or_else(|_| store.set_address_meta(addr, meta));
Ok(true)
}

View File

@@ -365,6 +365,8 @@ fn rpc_eth_accounts() {
let tester = EthTester::default();
let address = tester.accounts_provider.new_account("").unwrap();
tester.accounts_provider.set_new_dapps_whitelist(None).unwrap();
tester.accounts_provider.set_address_name(1.into(), "1".into());
tester.accounts_provider.set_address_name(10.into(), "10".into());
// with current policy it should return the account
let request = r#"{"jsonrpc": "2.0", "method": "eth_accounts", "params": [], "id": 1}"#;

View File

@@ -120,10 +120,11 @@ fn should_be_able_to_set_meta() {
fn rpc_parity_set_and_get_dapps_accounts() {
// given
let tester = setup();
tester.accounts.set_address_name(10.into(), "10".into());
assert_eq!(tester.accounts.dapps_addresses("app1".into()).unwrap(), vec![]);
// when
let request = r#"{"jsonrpc": "2.0", "method": "parity_setDappsAddresses","params":["app1",["0x000000000000000000000000000000000000000a"]], "id": 1}"#;
let request = r#"{"jsonrpc": "2.0", "method": "parity_setDappsAddresses","params":["app1",["0x000000000000000000000000000000000000000a","0x0000000000000000000000000000000000000001"]], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#;
assert_eq!(tester.io.handle_request_sync(request), Some(response.to_owned()));

View File

@@ -25,7 +25,7 @@ build_rpc_trait! {
pub trait ParityAccounts {
/// Returns accounts information.
#[rpc(name = "parity_allAccountsInfo")]
fn all_accounts_info(&self) -> Result<BTreeMap<String, BTreeMap<String, String>>, Error>;
fn all_accounts_info(&self) -> Result<BTreeMap<H160, BTreeMap<String, String>>, Error>;
/// Creates new account from the given phrase using standard brainwallet mechanism.
/// Second parameter is password for the new account.