From 46840d36c978c402c35c563b1a5c144be20929e5 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 7 Dec 2016 16:53:46 +0100 Subject: [PATCH] Add parity_removeAddress RPC --- ethcore/src/account_provider/mod.rs | 5 +++++ ethcore/src/account_provider/stores.rs | 26 ++++++++++++++++++++++++++ js/src/api/rpc/parity/parity.js | 5 +++++ js/src/jsonrpc/interfaces/parity.js | 14 ++++++++++++++ rpc/src/v1/impls/parity_accounts.rs | 10 ++++++++++ rpc/src/v1/traits/parity_accounts.rs | 6 +++++- 6 files changed, 65 insertions(+), 1 deletion(-) diff --git a/ethcore/src/account_provider/mod.rs b/ethcore/src/account_provider/mod.rs index 24548dda3..a2c83f1ce 100644 --- a/ethcore/src/account_provider/mod.rs +++ b/ethcore/src/account_provider/mod.rs @@ -194,6 +194,11 @@ impl AccountProvider { Ok(self.address_book.write().set_meta(account, meta)) } + /// Removes and address from the addressbook + pub fn remove_address(&self, addr: Address) -> Result<(), Error> { + Ok(self.address_book.write().remove(addr)) + } + /// Returns each account along with name and meta. pub fn accounts_info(&self) -> Result, Error> { let r: HashMap = try!(self.sstore.accounts()) diff --git a/ethcore/src/account_provider/stores.rs b/ethcore/src/account_provider/stores.rs index cfc81f495..0d8754442 100644 --- a/ethcore/src/account_provider/stores.rs +++ b/ethcore/src/account_provider/stores.rs @@ -74,6 +74,14 @@ impl AddressBook { } self.save(); } + + /// Removes an entry + pub fn remove(&mut self, a: Address) { + { + self.cache.remove(&a); + } + self.save(); + } } /// Dapps user settings @@ -244,4 +252,22 @@ mod tests { } ]); } + + #[test] + fn should_remove_address() { + let temp = RandomTempPath::create_dir(); + let path = temp.as_str().to_owned(); + let mut b = AddressBook::new(path.clone()); + + b.set_name(1.into(), "One".to_owned()); + b.set_name(2.into(), "Two".to_owned()); + b.set_name(3.into(), "Three".to_owned()); + b.remove(2.into()); + + let b = AddressBook::new(path); + assert_eq!(b.get(), hash_map![ + 1.into() => AccountMeta{name: "One".to_owned(), meta: "{}".to_owned(), uuid: None}, + 3.into() => AccountMeta{name: "Three".to_owned(), meta: "{}".to_owned(), uuid: None} + ]); + } } diff --git a/js/src/api/rpc/parity/parity.js b/js/src/api/rpc/parity/parity.js index ac16eb9b0..7dd5b7eb3 100644 --- a/js/src/api/rpc/parity/parity.js +++ b/js/src/api/rpc/parity/parity.js @@ -128,6 +128,11 @@ export default class Parity { .execute('parity_killAccount', inAddress(account), password); } + removeAddress (address) { + return this._transport + .execute('parity_removeAddress', inAddress(address)); + } + listGethAccounts () { return this._transport .execute('parity_listGethAccounts') diff --git a/js/src/jsonrpc/interfaces/parity.js b/js/src/jsonrpc/interfaces/parity.js index 02155890e..067ced1fc 100644 --- a/js/src/jsonrpc/interfaces/parity.js +++ b/js/src/jsonrpc/interfaces/parity.js @@ -256,6 +256,20 @@ export default { } }, + removeAddress: { + desc: 'Removes an address from the addressbook', + params: [ + { + type: Address, + desc: 'The address to remove' + } + ], + returns: { + type: Boolean, + desc: 'true on success' + } + }, + listGethAccounts: { desc: 'Returns a list of the accounts available from Geth', params: [], diff --git a/rpc/src/v1/impls/parity_accounts.rs b/rpc/src/v1/impls/parity_accounts.rs index ea927de4e..a765c8bb0 100644 --- a/rpc/src/v1/impls/parity_accounts.rs +++ b/rpc/src/v1/impls/parity_accounts.rs @@ -126,6 +126,16 @@ impl ParityAccounts for ParityAccountsClient where C: MiningBlock .map_err(|e| errors::account("Could not delete account.", e)) } + fn remove_address(&self, addr: RpcH160) -> Result { + try!(self.active()); + let store = take_weak!(self.accounts); + let addr: Address = addr.into(); + + store.remove_address(addr, name)) + .expect("remove_address always returns Ok; qed"); + Ok(true) + } + fn set_account_name(&self, addr: RpcH160, name: String) -> Result { try!(self.active()); let store = take_weak!(self.accounts); diff --git a/rpc/src/v1/traits/parity_accounts.rs b/rpc/src/v1/traits/parity_accounts.rs index f72802959..6cfe1bf7b 100644 --- a/rpc/src/v1/traits/parity_accounts.rs +++ b/rpc/src/v1/traits/parity_accounts.rs @@ -58,6 +58,11 @@ build_rpc_trait! { #[rpc(name = "parity_killAccount")] fn kill_account(&self, H160, String) -> Result; + /// Permanently deletes an address from the addressbook + /// Arguments: `address` + #[rpc(name = "parity_removeAddress")] + fn remove_address(&self, H160) -> Result; + /// Set an account's name. #[rpc(name = "parity_setAccountName")] fn set_account_name(&self, H160, String) -> Result; @@ -83,4 +88,3 @@ build_rpc_trait! { fn geth_accounts(&self) -> Result, Error>; } } -