openethereum/rpc/src/v1/impls/parity_accounts.rs

370 lines
12 KiB
Rust
Raw Normal View History

// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
//! Account management (personal) rpc implementation
use std::sync::Arc;
2017-06-23 16:29:45 +02:00
use std::collections::btree_map::{BTreeMap, Entry};
use ethereum_types::Address;
use ethkey::{Brain, Generator, Secret};
use ethstore::KeyFile;
use ethcore::account_provider::AccountProvider;
2017-11-14 11:38:17 +01:00
use jsonrpc_core::Result;
use v1::helpers::errors;
2017-03-29 17:07:58 +02:00
use v1::helpers::accounts::unwrap_provider;
use v1::traits::ParityAccounts;
2017-06-23 16:29:45 +02:00
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 {
accounts: Option<Arc<AccountProvider>>,
}
impl ParityAccountsClient {
/// Creates new PersonalClient
2017-03-29 17:07:58 +02:00
pub fn new(store: &Option<Arc<AccountProvider>>) -> Self {
ParityAccountsClient {
accounts: store.clone(),
}
}
2017-03-29 17:07:58 +02:00
/// Attempt to get the `Arc<AccountProvider>`, errors if provider was not
/// set.
2017-11-14 11:38:17 +01:00
fn account_provider(&self) -> Result<Arc<AccountProvider>> {
2017-03-29 17:07:58 +02:00
unwrap_provider(&self.accounts)
}
}
impl ParityAccounts for ParityAccountsClient {
2017-11-14 11:38:17 +01:00
fn all_accounts_info(&self) -> Result<BTreeMap<RpcH160, ExtAccountInfo>> {
2017-03-29 17:07:58 +02:00
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();
2017-06-23 16:29:45 +02:00
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
2017-06-23 16:29:45 +02:00
Entry::Occupied(ref mut occupied) if occupied.get().uuid.is_none() => {
occupied.insert(account);
},
Entry::Vacant(vacant) => {
vacant.insert(account);
},
_ => {}
};
}
Ok(accounts)
}
2017-11-14 11:38:17 +01:00
fn new_account_from_phrase(&self, phrase: String, pass: String) -> Result<RpcH160> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
2016-10-31 17:11:56 +01:00
let brain = Brain::new(phrase).generate().unwrap();
store.insert_account(brain.secret().clone(), &pass)
2016-10-31 17:11:56 +01:00
.map(Into::into)
.map_err(|e| errors::account("Could not create account.", e))
}
2017-11-14 11:38:17 +01:00
fn new_account_from_wallet(&self, json: String, pass: String) -> Result<RpcH160> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
2016-10-31 17:11:56 +01:00
store.import_presale(json.as_bytes(), &pass)
.or_else(|_| store.import_wallet(json.as_bytes(), &pass, true))
2016-10-31 17:11:56 +01:00
.map(Into::into)
.map_err(|e| errors::account("Could not create account.", e))
}
2017-11-14 11:38:17 +01:00
fn new_account_from_secret(&self, secret: RpcH256, pass: String) -> Result<RpcH160> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
2016-10-31 17:11:56 +01:00
let secret = Secret::from_unsafe_slice(&secret.0)
.map_err(|e| errors::account("Could not create account.", e))?;
store.insert_account(secret, &pass)
2016-10-31 17:11:56 +01:00
.map(Into::into)
.map_err(|e| errors::account("Could not create account.", e))
}
2017-11-14 11:38:17 +01:00
fn test_password(&self, account: RpcH160, password: String) -> Result<bool> {
2016-10-31 17:11:56 +01:00
let account: Address = account.into();
2017-03-29 17:07:58 +02:00
self.account_provider()?
2016-11-20 16:17:57 +01:00
.test_password(&account, &password)
2016-10-31 17:11:56 +01:00
.map_err(|e| errors::account("Could not fetch account info.", e))
}
2017-11-14 11:38:17 +01:00
fn change_password(&self, account: RpcH160, password: String, new_password: String) -> Result<bool> {
2016-10-31 17:11:56 +01:00
let account: Address = account.into();
2017-03-29 17:07:58 +02:00
self.account_provider()?
2016-10-31 17:11:56 +01:00
.change_password(&account, password, new_password)
.map(|_| true)
.map_err(|e| errors::account("Could not fetch account info.", e))
}
2017-11-14 11:38:17 +01:00
fn kill_account(&self, account: RpcH160, password: String) -> Result<bool> {
2016-11-20 16:17:57 +01:00
let account: Address = account.into();
2017-03-29 17:07:58 +02:00
self.account_provider()?
2016-11-20 16:17:57 +01:00
.kill_account(&account, &password)
.map(|_| true)
2016-11-23 20:16:33 +01:00
.map_err(|e| errors::account("Could not delete account.", e))
2016-11-20 16:17:57 +01:00
}
2017-11-14 11:38:17 +01:00
fn remove_address(&self, addr: RpcH160) -> Result<bool> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
2016-12-07 16:53:46 +01:00
let addr: Address = addr.into();
store.remove_address(addr);
2016-12-07 16:53:46 +01:00
Ok(true)
}
2017-11-14 11:38:17 +01:00
fn set_account_name(&self, addr: RpcH160, name: String) -> Result<bool> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
2016-10-31 17:11:56 +01:00
let addr: Address = addr.into();
store.set_account_name(addr.clone(), name.clone())
.unwrap_or_else(|_| store.set_address_name(addr, name));
2016-10-31 17:11:56 +01:00
Ok(true)
}
2017-11-14 11:38:17 +01:00
fn set_account_meta(&self, addr: RpcH160, meta: String) -> Result<bool> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
2016-10-31 17:11:56 +01:00
let addr: Address = addr.into();
store.set_account_meta(addr.clone(), meta.clone())
.unwrap_or_else(|_| store.set_address_meta(addr, meta));
2016-10-31 17:11:56 +01:00
Ok(true)
}
2017-11-14 11:38:17 +01:00
fn set_dapp_addresses(&self, dapp: DappId, addresses: Option<Vec<RpcH160>>) -> Result<bool> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
store.set_dapp_addresses(dapp.into(), addresses.map(into_vec))
.map_err(|e| errors::account("Couldn't set dapp addresses.", e))
.map(|_| true)
}
2017-11-14 11:38:17 +01:00
fn dapp_addresses(&self, dapp: DappId) -> Result<Vec<RpcH160>> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
store.dapp_addresses(dapp.into())
.map_err(|e| errors::account("Couldn't get dapp addresses.", e))
.map(into_vec)
}
2017-11-14 11:38:17 +01:00
fn set_dapp_default_address(&self, dapp: DappId, address: RpcH160) -> Result<bool> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
store.set_dapp_default_address(dapp.into(), address.into())
.map_err(|e| errors::account("Couldn't set dapp default address.", e))
.map(|_| true)
}
2017-11-14 11:38:17 +01:00
fn dapp_default_address(&self, dapp: DappId) -> Result<RpcH160> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
store.dapp_default_address(dapp.into())
.map_err(|e| errors::account("Couldn't get dapp default address.", e))
.map(Into::into)
}
2017-11-14 11:38:17 +01:00
fn set_new_dapps_addresses(&self, addresses: Option<Vec<RpcH160>>) -> Result<bool> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
store
.set_new_dapps_addresses(addresses.map(into_vec))
.map_err(|e| errors::account("Couldn't set dapps addresses.", e))
.map(|_| true)
}
2017-11-14 11:38:17 +01:00
fn new_dapps_addresses(&self) -> Result<Option<Vec<RpcH160>>> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
store.new_dapps_addresses()
.map_err(|e| errors::account("Couldn't get dapps addresses.", e))
.map(|accounts| accounts.map(into_vec))
}
2017-11-14 11:38:17 +01:00
fn set_new_dapps_default_address(&self, address: RpcH160) -> Result<bool> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
store.set_new_dapps_default_address(address.into())
.map_err(|e| errors::account("Couldn't set new dapps default address.", e))
.map(|_| true)
}
2017-11-14 11:38:17 +01:00
fn new_dapps_default_address(&self) -> Result<RpcH160> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
store.new_dapps_default_address()
.map_err(|e| errors::account("Couldn't get new dapps default address.", e))
.map(Into::into)
}
2017-11-14 11:38:17 +01:00
fn recent_dapps(&self) -> Result<BTreeMap<DappId, u64>> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
store.recent_dapps()
.map_err(|e| errors::account("Couldn't get recent dapps.", e))
.map(|map| map.into_iter().map(|(k, v)| (k.into(), v)).collect())
}
2017-11-14 11:38:17 +01:00
fn import_geth_accounts(&self, addresses: Vec<RpcH160>) -> Result<Vec<RpcH160>> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
2016-10-31 17:11:56 +01:00
store
.import_geth_accounts(into_vec(addresses), false)
.map(into_vec)
2016-10-31 17:11:56 +01:00
.map_err(|e| errors::account("Couldn't import Geth accounts", e))
}
2017-11-14 11:38:17 +01:00
fn geth_accounts(&self) -> Result<Vec<RpcH160>> {
2017-03-29 17:07:58 +02:00
let store = self.account_provider()?;
2016-10-31 17:11:56 +01:00
Ok(into_vec(store.list_geth_accounts(false)))
}
2017-11-14 11:38:17 +01:00
fn create_vault(&self, name: String, password: String) -> Result<bool> {
2017-03-29 17:07:58 +02:00
self.account_provider()?
.create_vault(&name, &password)
.map_err(|e| errors::account("Could not create vault.", e))
.map(|_| true)
}
2017-11-14 11:38:17 +01:00
fn open_vault(&self, name: String, password: String) -> Result<bool> {
2017-03-29 17:07:58 +02:00
self.account_provider()?
.open_vault(&name, &password)
.map_err(|e| errors::account("Could not open vault.", e))
.map(|_| true)
}
2017-11-14 11:38:17 +01:00
fn close_vault(&self, name: String) -> Result<bool> {
2017-03-29 17:07:58 +02:00
self.account_provider()?
.close_vault(&name)
.map_err(|e| errors::account("Could not close vault.", e))
.map(|_| true)
}
2017-11-14 11:38:17 +01:00
fn list_vaults(&self) -> Result<Vec<String>> {
2017-03-29 17:07:58 +02:00
self.account_provider()?
.list_vaults()
.map_err(|e| errors::account("Could not list vaults.", e))
}
2017-11-14 11:38:17 +01:00
fn list_opened_vaults(&self) -> Result<Vec<String>> {
2017-03-29 17:07:58 +02:00
self.account_provider()?
.list_opened_vaults()
.map_err(|e| errors::account("Could not list vaults.", e))
}
2017-11-14 11:38:17 +01:00
fn change_vault_password(&self, name: String, new_password: String) -> Result<bool> {
2017-03-29 17:07:58 +02:00
self.account_provider()?
.change_vault_password(&name, &new_password)
.map_err(|e| errors::account("Could not change vault password.", e))
.map(|_| true)
}
2017-11-14 11:38:17 +01:00
fn change_vault(&self, address: RpcH160, new_vault: String) -> Result<bool> {
2017-03-29 17:07:58 +02:00
self.account_provider()?
.change_vault(address.into(), &new_vault)
.map_err(|e| errors::account("Could not change vault.", e))
.map(|_| true)
}
2017-11-14 11:38:17 +01:00
fn get_vault_meta(&self, name: String) -> Result<String> {
2017-03-29 17:07:58 +02:00
self.account_provider()?
.get_vault_meta(&name)
.map_err(|e| errors::account("Could not get vault metadata.", e))
}
2017-11-14 11:38:17 +01:00
fn set_vault_meta(&self, name: String, meta: String) -> Result<bool> {
2017-03-29 17:07:58 +02:00
self.account_provider()?
.set_vault_meta(&name, &meta)
.map_err(|e| errors::account("Could not update vault metadata.", e))
.map(|_| true)
}
2017-11-14 11:38:17 +01:00
fn derive_key_index(&self, addr: RpcH160, password: String, derivation: DeriveHierarchical, save_as_account: bool) -> Result<RpcH160> {
let addr: Address = addr.into();
2017-03-29 17:07:58 +02:00
self.account_provider()?
.derive_account(
&addr,
Some(password),
Derive::from(derivation).to_derivation()
.map_err(|c| errors::account("Could not parse derivation request: {:?}", c))?,
save_as_account)
.map(Into::into)
.map_err(|e| errors::account("Could not derive account.", e))
}
2017-11-14 11:38:17 +01:00
fn derive_key_hash(&self, addr: RpcH160, password: String, derivation: DeriveHash, save_as_account: bool) -> Result<RpcH160> {
let addr: Address = addr.into();
2017-03-29 17:07:58 +02:00
self.account_provider()?
.derive_account(
&addr,
Some(password),
Derive::from(derivation).to_derivation()
.map_err(|c| errors::account("Could not parse derivation request: {:?}", c))?,
save_as_account)
.map(Into::into)
.map_err(|e| errors::account("Could not derive account.", e))
}
2017-11-14 11:38:17 +01:00
fn export_account(&self, addr: RpcH160, password: String) -> Result<KeyFile> {
let addr = addr.into();
2017-03-29 17:07:58 +02:00
self.account_provider()?
.export_account(
&addr,
password,
)
.map(Into::into)
.map_err(|e| errors::account("Could not export account.", e))
}
2017-11-14 11:38:17 +01:00
fn sign_message(&self, addr: RpcH160, password: String, message: RpcH256) -> Result<RpcH520> {
self.account_provider()?
.sign(
addr.into(),
Some(password),
message.into()
)
.map(Into::into)
.map_err(|e| errors::account("Could not sign message.", e))
}
Trezor Support (#6403) * Copy modal from keepkey branch and generalize The keepkey PinMatrix modal needs to be the same for Trezor, but we should probably try to keep it general since it can be used for both. * Add trezor communication code This is a result of much trial-and-error and a couple of dead-ends in how to communicate and wire everything up. Code here is still a bit WIP with lots of debug prints and stuff. The test works though, it is possible to sign a transaction. * Extend the basic lib to allow Trezor This is kind of ugly and needs some cleanup and generalization. I’ve just copy-pasted some things to bring in the trezor wallets. I’ve also had to add a lock to the USB API so that only one thing talks to the USB at once. * Add RPC plumbing needed We need to be able to get “locked” devices from the frontend to figure out if we’re going to display the PinMatrix or not. Then we need to be able to send a pin to a device. * Add logic to query backend for Trezor and display PinMatrix There’s a bug somewhere here because signing a transaction fails if you take too long to press the confirm button on the device. * Change back to paritytech branch As my fork has been merged in. * Converting spaces to tabs, as it should be * Incorporate correct handling of EIP-155 Turns out the Trezor was adjusting the v part of the signature, and we’re already doing that so it was done twice. * Some circular logic here that was incorrect BE-encoded U256 is almost the same as RLP encoded without the size-byte, except for <u8 sized values. What’s really done is BE-encoded U256 and then left-trimmed to the smallest size. Kind of obvious in hindsight. * Resolve issue where not clicking fast enough fails The device will not repeat a ButtonRequest when you read from it, so you need to have a blocking `read` for whatever amount of time that you want to give the user to click. You could also have a shorter timeout but keep retrying for some amount of time, but it would amount to the same thing. * Scan after pin entry to make accepting it faster * Remove ability to cancel pin request * Some slight cleanup * Probe for the correct HID Version to determine padding * Move the PinMatrix from Accounts to Application * Removing unused dependencies * Mistake in copying over stuff from keepkey branch * Simplify FormattedMessage * Move generated code to external crate * Remove ethcore-util dependency * Fix broken import in test This test is useless without a connected Trezor, not sure how to make it useful without one. * Merge branch 'master' into fh-4500-trezor-support # Conflicts: # rpc/src/v1/helpers/dispatch.rs * Ignore test that can't be run without trezor device * Fixing grumbles * Avoiding owning data in RPC method * Checking for overflow in v part of signature * s/network_id/chain_id * Propagating an error from the HID Api * Condensing code a little bit * Fixing UI. * Debugging trezor. * Minor styling tweak * Make message type into an actual type This makes the message type that the RPC message accepts into an actual type as opposed to just a string, based on feedback. Although I’m not 100% sure this has actually improved the situation. Overall I think the hardware wallet interface needs some refactoring love. * Split the trezor RPC endpoint It’s split into two more generic endpoints that should be suitable for any hardware wallets with the same behavior to sit behind. * Reflect RPC method split in javascript * Fix bug with pin entry * Fix deadlock for Ledger * Avoid having a USB lock in just listing locked wallets * Fix javascript issue (see #6509) * Replace Mutex with RwLock * Update Ledger test * Fix typo causing faulty signatures (sometimes) * *Actually* fix tests * Update git submodule Needed to make tests pass * Swap line orders to prevent possible deadlock * Make setPinMatrixRequest an @action
2017-09-14 19:28:43 +02:00
2017-11-14 11:38:17 +01:00
fn hardware_pin_matrix_ack(&self, path: String, pin: String) -> Result<bool> {
Trezor Support (#6403) * Copy modal from keepkey branch and generalize The keepkey PinMatrix modal needs to be the same for Trezor, but we should probably try to keep it general since it can be used for both. * Add trezor communication code This is a result of much trial-and-error and a couple of dead-ends in how to communicate and wire everything up. Code here is still a bit WIP with lots of debug prints and stuff. The test works though, it is possible to sign a transaction. * Extend the basic lib to allow Trezor This is kind of ugly and needs some cleanup and generalization. I’ve just copy-pasted some things to bring in the trezor wallets. I’ve also had to add a lock to the USB API so that only one thing talks to the USB at once. * Add RPC plumbing needed We need to be able to get “locked” devices from the frontend to figure out if we’re going to display the PinMatrix or not. Then we need to be able to send a pin to a device. * Add logic to query backend for Trezor and display PinMatrix There’s a bug somewhere here because signing a transaction fails if you take too long to press the confirm button on the device. * Change back to paritytech branch As my fork has been merged in. * Converting spaces to tabs, as it should be * Incorporate correct handling of EIP-155 Turns out the Trezor was adjusting the v part of the signature, and we’re already doing that so it was done twice. * Some circular logic here that was incorrect BE-encoded U256 is almost the same as RLP encoded without the size-byte, except for <u8 sized values. What’s really done is BE-encoded U256 and then left-trimmed to the smallest size. Kind of obvious in hindsight. * Resolve issue where not clicking fast enough fails The device will not repeat a ButtonRequest when you read from it, so you need to have a blocking `read` for whatever amount of time that you want to give the user to click. You could also have a shorter timeout but keep retrying for some amount of time, but it would amount to the same thing. * Scan after pin entry to make accepting it faster * Remove ability to cancel pin request * Some slight cleanup * Probe for the correct HID Version to determine padding * Move the PinMatrix from Accounts to Application * Removing unused dependencies * Mistake in copying over stuff from keepkey branch * Simplify FormattedMessage * Move generated code to external crate * Remove ethcore-util dependency * Fix broken import in test This test is useless without a connected Trezor, not sure how to make it useful without one. * Merge branch 'master' into fh-4500-trezor-support # Conflicts: # rpc/src/v1/helpers/dispatch.rs * Ignore test that can't be run without trezor device * Fixing grumbles * Avoiding owning data in RPC method * Checking for overflow in v part of signature * s/network_id/chain_id * Propagating an error from the HID Api * Condensing code a little bit * Fixing UI. * Debugging trezor. * Minor styling tweak * Make message type into an actual type This makes the message type that the RPC message accepts into an actual type as opposed to just a string, based on feedback. Although I’m not 100% sure this has actually improved the situation. Overall I think the hardware wallet interface needs some refactoring love. * Split the trezor RPC endpoint It’s split into two more generic endpoints that should be suitable for any hardware wallets with the same behavior to sit behind. * Reflect RPC method split in javascript * Fix bug with pin entry * Fix deadlock for Ledger * Avoid having a USB lock in just listing locked wallets * Fix javascript issue (see #6509) * Replace Mutex with RwLock * Update Ledger test * Fix typo causing faulty signatures (sometimes) * *Actually* fix tests * Update git submodule Needed to make tests pass * Swap line orders to prevent possible deadlock * Make setPinMatrixRequest an @action
2017-09-14 19:28:43 +02:00
let store = self.account_provider()?;
Ok(store.hardware_pin_matrix_ack(&path, &pin).map_err(|e| errors::account("Error communicating with hardware wallet.", e))?)
}
}
fn into_vec<A, B>(a: Vec<A>) -> Vec<B> where
A: Into<B>
{
a.into_iter().map(Into::into).collect()
}