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

Add parity_removeAddress RPC #3735

Merged
merged 3 commits into from
Dec 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ethcore/src/account_provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashMap<Address, AccountMeta>, Error> {
let r: HashMap<Address, AccountMeta> = try!(self.sstore.accounts())
Expand Down
24 changes: 24 additions & 0 deletions ethcore/src/account_provider/stores.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ impl AddressBook {
}
self.save();
}

/// Removes an entry
pub fn remove(&mut self, a: Address) {
self.cache.remove(&a);
self.save();
}
}

/// Dapps user settings
Expand Down Expand Up @@ -244,4 +250,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}
]);
}
}
5 changes: 5 additions & 0 deletions js/src/api/rpc/parity/parity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
14 changes: 14 additions & 0 deletions js/src/jsonrpc/interfaces/parity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down
10 changes: 10 additions & 0 deletions rpc/src/v1/impls/parity_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ impl<C: 'static> ParityAccounts for ParityAccountsClient<C> where C: MiningBlock
.map_err(|e| errors::account("Could not delete account.", e))
}

fn remove_address(&self, addr: RpcH160) -> Result<bool, Error> {
try!(self.active());
let store = take_weak!(self.accounts);
let addr: Address = addr.into();

store.remove_address(addr)
.expect("remove_address always returns Ok; qed");
Ok(true)
}

fn set_account_name(&self, addr: RpcH160, name: String) -> Result<bool, Error> {
try!(self.active());
let store = take_weak!(self.accounts);
Expand Down
29 changes: 29 additions & 0 deletions rpc/src/v1/tests/mocked/parity_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,32 @@ fn should_be_able_to_kill_account() {
let accounts = tester.accounts.accounts().unwrap();
assert_eq!(accounts.len(), 0);
}

#[test]
fn should_be_able_to_remove_address() {
let tester = setup();

// add an address
let request = r#"{"jsonrpc": "2.0", "method": "parity_setAccountName", "params": ["0x000baba1000baba2000baba3000baba4000baba5", "Test"], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":true,"id":1}"#;
let res = tester.io.handle_request_sync(&request);
assert_eq!(res, Some(response.into()));

// verify it exists
let request = r#"{"jsonrpc": "2.0", "method": "parity_accountsInfo", "params": [], "id": 2}"#;
let res = tester.io.handle_request_sync(request);
let response = r#"{"jsonrpc":"2.0","result":{"0x000baba1000baba2000baba3000baba4000baba5":{"meta":"{}","name":"Test","uuid":null}},"id":2}"#;
assert_eq!(res, Some(response.into()));

// remove the address
let request = r#"{"jsonrpc": "2.0", "method": "parity_removeAddress", "params": ["0x000baba1000baba2000baba3000baba4000baba5"], "id": 3}"#;
let response = r#"{"jsonrpc":"2.0","result":true,"id":3}"#;
let res = tester.io.handle_request_sync(&request);
assert_eq!(res, Some(response.into()));

// verify empty
let request = r#"{"jsonrpc": "2.0", "method": "parity_accountsInfo", "params": [], "id": 4}"#;
let res = tester.io.handle_request_sync(request);
let response = r#"{"jsonrpc":"2.0","result":{},"id":4}"#;
assert_eq!(res, Some(response.into()));
}
6 changes: 5 additions & 1 deletion rpc/src/v1/traits/parity_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ build_rpc_trait! {
#[rpc(name = "parity_killAccount")]
fn kill_account(&self, H160, String) -> Result<bool, Error>;

/// Permanently deletes an address from the addressbook
/// Arguments: `address`
#[rpc(name = "parity_removeAddress")]
fn remove_address(&self, H160) -> Result<bool, Error>;

/// Set an account's name.
#[rpc(name = "parity_setAccountName")]
fn set_account_name(&self, H160, String) -> Result<bool, Error>;
Expand All @@ -83,4 +88,3 @@ build_rpc_trait! {
fn geth_accounts(&self) -> Result<Vec<H160>, Error>;
}
}