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

Commit

Permalink
Move all fake-hardware-wallet to its own crate
Browse files Browse the repository at this point in the history
* Removes some conditional compilation flags
* Introduces `fake-hardware-wallet` crate
  • Loading branch information
niklasad1 committed Jun 22, 2018
1 parent 950a54a commit 6548566
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 84 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion ethcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ ethjson = { path = "../json" }
ethkey = { path = "../ethkey" }
ethstore = { path = "../ethstore" }
evm = { path = "evm" }
hardware-wallet = { path = "../hw" }
heapsize = "0.4"
itertools = "0.5"
lazy_static = "1.0"
Expand Down Expand Up @@ -70,6 +69,12 @@ journaldb = { path = "../util/journaldb" }
tempdir = "0.3"
kvdb-rocksdb = { path = "../util/kvdb-rocksdb" }

[target.'cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"))'.dependencies]
hardware-wallet = { path = "../hw" }

[target.'cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android")))'.dependencies]
fake-hardware-wallet = { path = "../util/fake-hardware-wallet" }

[dev-dependencies]
trie-standardmap = { path = "../util/trie-standardmap" }

Expand Down
41 changes: 13 additions & 28 deletions ethcore/src/account_provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
mod stores;

#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android")))]
mod no_hw;

use ethstore::{
SimpleSecretStore, SecretStore, Error as SSError, EthStore, EthMultiStore,
random_string, SecretVaultRef, StoreAccountRef, OpaqueSecret,
Expand All @@ -37,18 +34,9 @@ use std::time::{Instant, Duration};

pub use ethstore::ethkey::Signature;
pub use ethstore::{Derivation, IndexDerivation, KeyFile};
pub use hardware_wallet::{Error as HardwareError, HardwareWalletManager, KeyPath, TransactionInfo};
pub use super::transaction::{Action, Transaction};

#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"))]
mod hw {
pub use hardware_wallet::{Error as HardwareError, HardwareWalletManager, KeyPath, TransactionInfo};
}

#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android")))]
mod hw {
pub use account_provider::no_hw::{HardwareError, HardwareWalletManager};
}

/// Type of unlock.
#[derive(Clone, PartialEq)]
enum Unlock {
Expand Down Expand Up @@ -76,7 +64,7 @@ pub enum SignError {
/// Account does not exist.
NotFound,
/// Low-level hardware device error.
Hardware(hw::HardwareError),
Hardware(HardwareError),
/// Low-level error from store
SStore(SSError),
}
Expand All @@ -92,8 +80,8 @@ impl fmt::Display for SignError {
}
}

impl From<hw::HardwareError> for SignError {
fn from(e: hw::HardwareError) -> Self {
impl From<HardwareError> for SignError {
fn from(e: HardwareError) -> Self {
SignError::Hardware(e)
}
}
Expand Down Expand Up @@ -143,7 +131,7 @@ pub struct AccountProvider {
/// Accounts unlocked with rolling tokens
transient_sstore: EthMultiStore,
/// Accounts in hardware wallets.
hardware_store: Option<hw::HardwareWalletManager>,
hardware_store: Option<HardwareWalletManager>,
/// When unlocking account permanently we additionally keep a raw secret in memory
/// to increase the performance of transaction signing.
unlock_keep_secret: bool,
Expand Down Expand Up @@ -179,16 +167,13 @@ impl AccountProvider {
pub fn new(sstore: Box<SecretStore>, settings: AccountProviderSettings) -> Self {
let mut hardware_store = None;

#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"))]
{
if settings.enable_hardware_wallets {
match hw::HardwareWalletManager::new() {
Ok(manager) => {
manager.set_key_path(if settings.hardware_wallet_classic_key { hw::KeyPath::EthereumClassic } else { hw::KeyPath::Ethereum });
hardware_store = Some(manager)
},
Err(e) => debug!("Error initializing hardware wallets: {}", e),
}
if settings.enable_hardware_wallets {
match HardwareWalletManager::new() {
Ok(manager) => {
manager.set_key_path(if settings.hardware_wallet_classic_key { KeyPath::EthereumClassic } else { KeyPath::Ethereum });
hardware_store = Some(manager)
},
Err(e) => debug!("Error initializing hardware wallets: {}", e),
}
}

Expand Down Expand Up @@ -850,7 +835,7 @@ impl AccountProvider {
chain_id: chain_id,
};
match self.hardware_store.as_ref().map(|s| s.sign_transaction(&address, &t_info, rlp_encoded_transaction)) {
None | Some(Err(hw::HardwareError::KeyNotFound)) => Err(SignError::NotFound),
None | Some(Err(HardwareError::KeyNotFound)) => Err(SignError::NotFound),
Some(Err(e)) => Err(From::from(e)),
Some(Ok(s)) => Ok(s),
}
Expand Down
42 changes: 0 additions & 42 deletions ethcore/src/account_provider/no_hw.rs

This file was deleted.

4 changes: 3 additions & 1 deletion ethcore/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ extern crate journaldb;
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"))]
extern crate hardware_wallet;

#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android")))]
extern crate fake_hardware_wallet as hardware_wallet;

#[cfg(test)]
extern crate tempdir;

Expand Down Expand Up @@ -160,7 +163,6 @@ pub mod state;
pub mod state_db;

// Test helpers made public for usage outside ethcore
pub mod test_helpers;
pub mod trace;
pub mod verification;

Expand Down
6 changes: 6 additions & 0 deletions rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ rlp = { path = "../util/rlp" }
stats = { path = "../util/stats" }
vm = { path = "../ethcore/vm" }

[target.'cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"))'.dependencies]
hardware-wallet = { path = "../hw" }

[target.'cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android")))'.dependencies]
fake-hardware-wallet = { path = "../util/fake-hardware-wallet" }

[dev-dependencies]
ethcore = { path = "../ethcore", features = ["test-helpers"] }
ethcore-network = { path = "../util/network" }
Expand Down
9 changes: 5 additions & 4 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,21 @@ extern crate ethcore_transaction as transaction;
extern crate ethereum_types;
extern crate ethkey;
extern crate ethstore;
extern crate vm;
extern crate fetch;
extern crate keccak_hash as hash;
extern crate node_health;
extern crate parity_reactor;
extern crate parity_updater as updater;
extern crate parity_version as version;
extern crate patricia_trie as trie;
extern crate rlp;
extern crate stats;
extern crate keccak_hash as hash;
extern crate vm;

#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"))]
extern crate hardware_wallet;

extern crate patricia_trie as trie;
#[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android")))]
extern crate fake_hardware_wallet as hardware_wallet;

#[macro_use]
extern crate log;
Expand Down
10 changes: 2 additions & 8 deletions rpc/src/v1/helpers/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ use v1::types::{
SignRequest as RpcSignRequest,
DecryptRequest as RpcDecryptRequest,
};

#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"))]
use rlp;

pub use self::nonce::Reservations;
Expand Down Expand Up @@ -436,11 +434,8 @@ fn sign_transaction(
data: filled.data,
};

#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"))]
{
if accounts.is_hardware_address(&filled.from) {
return hardware_signature(accounts, filled.from, t, chain_id).map(WithToken::No)
}
if accounts.is_hardware_address(&filled.from) {
return hardware_signature(accounts, filled.from, t, chain_id).map(WithToken::No)
}

let hash = t.hash(chain_id);
Expand Down Expand Up @@ -726,7 +721,6 @@ fn signature(accounts: &AccountProvider, address: Address, hash: H256, password:
}

// obtain a hardware signature from the given account.
#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows", target_os = "android"))]
fn hardware_signature(accounts: &AccountProvider, address: Address, t: Transaction, chain_id: Option<u64>)
-> Result<SignedTransaction>
{
Expand Down
10 changes: 10 additions & 0 deletions util/fake-hardware-wallet/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
description = "Fake hardware-wallet, for OS' that don't support libusb"
name = "fake-hardware-wallet"
version = "0.0.1"
license = "GPL-3.0"
authors = ["Parity Technologies <admin@parity.io>"]

[dependencies]
ethereum-types = "0.3"
ethkey = { path = "../../ethkey" }
101 changes: 101 additions & 0 deletions util/fake-hardware-wallet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2015-2018 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/>.

//! Dummy module for platforms that does not provide support for hardware wallets (libusb)
extern crate ethereum_types;
extern crate ethkey;

use std::fmt;
use ethereum_types::U256;
use ethkey::{Address, Signature};

pub struct WalletInfo {
pub address: Address,
pub name: String,
pub manufacturer: String,
}

#[derive(Debug)]
/// `ErrorType` for devices with no `hardware wallet`
pub enum Error {
NoWallet,
KeyNotFound,
}

pub struct TransactionInfo {
/// Nonce
pub nonce: U256,
/// Gas price
pub gas_price: U256,
/// Gas limit
pub gas_limit: U256,
/// Receiver
pub to: Option<Address>,
/// Value
pub value: U256,
/// Data
pub data: Vec<u8>,
/// Chain ID
pub chain_id: Option<u64>,
}

pub enum KeyPath {
/// Ethereum.
Ethereum,
/// Ethereum classic.
EthereumClassic,
}

/// `HardwareWalletManager` for devices with no `hardware wallet`
pub struct HardwareWalletManager;

impl HardwareWalletManager {
pub fn new() -> Result<Self, Error> {
Err(Error::NoWallet)
}

pub fn set_key_path(&self, _key_path: KeyPath) {}

pub fn wallet_info(&self, _: &Address) -> Option<WalletInfo> {
None
}

pub fn list_wallets(&self) -> Vec<WalletInfo> {
Vec::with_capacity(0)
}

pub fn list_locked_wallets(&self) -> Result<Vec<String>, Error> {
Err(Error::NoWallet)
}

pub fn pin_matrix_ack(&self, _: &str, _: &str) -> Result<bool, Error> {
Err(Error::NoWallet)
}

pub fn sign_transaction(&self, _address: &Address, _transaction: &TransactionInfo, _rlp_transaction: &[u8]) -> Result<Signature, Error> {
Err(Error::NoWallet) }

pub fn sign_message(&self, _address: &Address, _msg: &[u8]) -> Result<Signature, Error> {
Err(Error::NoWallet)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "")
}
}

0 comments on commit 6548566

Please sign in to comment.