2018-06-04 10:19:50 +02:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2017-02-10 01:07:06 +01:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
//! Hardware wallet management.
|
|
|
|
|
2018-06-18 11:55:23 +02:00
|
|
|
#![warn(missing_docs)]
|
|
|
|
#![warn(warnings)]
|
2018-03-02 18:30:25 +01:00
|
|
|
|
2018-01-10 13:35:18 +01:00
|
|
|
extern crate ethereum_types;
|
2017-09-14 19:28:43 +02:00
|
|
|
extern crate ethkey;
|
2017-02-10 01:07:06 +01:00
|
|
|
extern crate hidapi;
|
|
|
|
extern crate libusb;
|
2017-09-14 19:28:43 +02:00
|
|
|
extern crate parking_lot;
|
|
|
|
extern crate protobuf;
|
2018-06-13 11:01:56 +02:00
|
|
|
extern crate semver;
|
2017-09-14 19:28:43 +02:00
|
|
|
extern crate trezor_sys;
|
2018-06-13 11:01:56 +02:00
|
|
|
|
2017-02-10 01:07:06 +01:00
|
|
|
#[macro_use] extern crate log;
|
2017-07-06 11:26:14 +02:00
|
|
|
#[cfg(test)] extern crate rustc_hex;
|
2017-02-10 01:07:06 +01:00
|
|
|
|
|
|
|
mod ledger;
|
2017-09-14 19:28:43 +02:00
|
|
|
mod trezor;
|
|
|
|
|
2018-06-13 11:01:56 +02:00
|
|
|
use std::sync::{Arc, atomic, atomic::AtomicBool};
|
2018-06-18 11:55:23 +02:00
|
|
|
use std::{fmt, time::Duration};
|
|
|
|
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::U256;
|
2018-06-18 11:55:23 +02:00
|
|
|
use ethkey::{Address, Signature};
|
|
|
|
use parking_lot::Mutex;
|
2017-02-10 01:07:06 +01:00
|
|
|
|
2018-02-27 16:45:16 +01:00
|
|
|
const USB_DEVICE_CLASS_DEVICE: u8 = 0;
|
2018-06-13 11:01:56 +02:00
|
|
|
const POLLING_DURATION: Duration = Duration::from_millis(500);
|
2018-02-27 16:45:16 +01:00
|
|
|
|
2018-06-18 11:55:23 +02:00
|
|
|
/// `HardwareWallet` device
|
2018-05-01 15:01:49 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Device {
|
|
|
|
path: String,
|
|
|
|
info: WalletInfo,
|
|
|
|
}
|
|
|
|
|
2018-06-18 11:55:23 +02:00
|
|
|
/// `Wallet` trait
|
2018-05-01 15:01:49 +02:00
|
|
|
pub trait Wallet<'a> {
|
|
|
|
/// Error
|
|
|
|
type Error;
|
|
|
|
/// Transaction data format
|
|
|
|
type Transaction;
|
|
|
|
|
|
|
|
/// Sign transaction data with wallet managing `address`.
|
|
|
|
fn sign_transaction(&self, address: &Address, transaction: Self::Transaction) -> Result<Signature, Self::Error>;
|
2018-06-13 11:01:56 +02:00
|
|
|
|
2018-05-01 15:01:49 +02:00
|
|
|
/// Set key derivation path for a chain.
|
|
|
|
fn set_key_path(&self, key_path: KeyPath);
|
|
|
|
|
|
|
|
/// Re-populate device list
|
|
|
|
/// Note, this assumes all devices are iterated over and updated
|
2018-06-13 11:01:56 +02:00
|
|
|
fn update_devices(&self, device_direction: DeviceDirection) -> Result<usize, Self::Error>;
|
2018-05-01 15:01:49 +02:00
|
|
|
|
|
|
|
/// Read device info
|
|
|
|
fn read_device(&self, usb: &hidapi::HidApi, dev_info: &hidapi::HidDeviceInfo) -> Result<Device, Self::Error>;
|
|
|
|
|
|
|
|
/// List connected and acknowledged wallets
|
|
|
|
fn list_devices(&self) -> Vec<WalletInfo>;
|
|
|
|
|
|
|
|
/// List locked wallets
|
|
|
|
/// This may be moved if it is the wrong assumption, for example this is not supported by Ledger
|
|
|
|
/// Then this method return a empty vector
|
|
|
|
fn list_locked_devices(&self) -> Vec<String>;
|
|
|
|
|
|
|
|
/// Get wallet info.
|
|
|
|
fn get_wallet(&self, address: &Address) -> Option<WalletInfo>;
|
|
|
|
|
|
|
|
/// Generate ethereum address for a Wallet
|
|
|
|
fn get_address(&self, device: &hidapi::HidDevice) -> Result<Option<Address>, Self::Error>;
|
|
|
|
|
|
|
|
/// Open a device using `device path`
|
|
|
|
/// Note, f - is a closure that borrows HidResult<HidDevice>
|
|
|
|
/// HidDevice is in turn a type alias for a `c_void function pointer`
|
|
|
|
/// For further information see:
|
|
|
|
/// * <https://github.com/paritytech/hidapi-rs>
|
|
|
|
/// * <https://github.com/rust-lang/libc>
|
|
|
|
fn open_path<R, F>(&self, f: F) -> Result<R, Self::Error>
|
|
|
|
where F: Fn() -> Result<R, &'static str>;
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:28:43 +02:00
|
|
|
/// Hardware wallet error.
|
2017-02-10 01:07:06 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
/// Ledger device error.
|
|
|
|
LedgerDevice(ledger::Error),
|
2017-09-14 19:28:43 +02:00
|
|
|
/// Trezor device error
|
|
|
|
TrezorDevice(trezor::Error),
|
2017-02-10 01:07:06 +01:00
|
|
|
/// USB error.
|
|
|
|
Usb(libusb::Error),
|
2017-09-14 19:28:43 +02:00
|
|
|
/// HID error
|
|
|
|
Hid(String),
|
2017-02-10 01:07:06 +01:00
|
|
|
/// Hardware wallet not found for specified key.
|
|
|
|
KeyNotFound,
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:28:43 +02:00
|
|
|
/// This is the transaction info we need to supply to Trezor message. It's more
|
2018-06-18 11:55:23 +02:00
|
|
|
/// or less a duplicate of `ethcore::transaction::Transaction`, but we can't
|
2017-09-14 19:28:43 +02:00
|
|
|
/// import ethcore here as that would be a circular dependency.
|
|
|
|
pub struct TransactionInfo {
|
2018-03-02 18:30:25 +01:00
|
|
|
/// Nonce
|
2017-09-14 19:28:43 +02:00
|
|
|
pub nonce: U256,
|
2018-03-02 18:30:25 +01:00
|
|
|
/// Gas price
|
2017-09-14 19:28:43 +02:00
|
|
|
pub gas_price: U256,
|
2018-03-02 18:30:25 +01:00
|
|
|
/// Gas limit
|
2017-09-14 19:28:43 +02:00
|
|
|
pub gas_limit: U256,
|
2018-03-02 18:30:25 +01:00
|
|
|
/// Receiver
|
2017-09-14 19:28:43 +02:00
|
|
|
pub to: Option<Address>,
|
2018-03-02 18:30:25 +01:00
|
|
|
/// Value
|
2017-09-14 19:28:43 +02:00
|
|
|
pub value: U256,
|
2018-03-02 18:30:25 +01:00
|
|
|
/// Data
|
2017-09-14 19:28:43 +02:00
|
|
|
pub data: Vec<u8>,
|
2018-03-02 18:30:25 +01:00
|
|
|
/// Chain ID
|
2017-09-14 19:28:43 +02:00
|
|
|
pub chain_id: Option<u64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hardware wallet information.
|
2017-02-10 01:07:06 +01:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct WalletInfo {
|
|
|
|
/// Wallet device name.
|
|
|
|
pub name: String,
|
|
|
|
/// Wallet device manufacturer.
|
|
|
|
pub manufacturer: String,
|
|
|
|
/// Wallet device serial number.
|
|
|
|
pub serial: String,
|
|
|
|
/// Ethereum address.
|
|
|
|
pub address: Address,
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:28:43 +02:00
|
|
|
/// Key derivation paths used on hardware wallets.
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub enum KeyPath {
|
|
|
|
/// Ethereum.
|
|
|
|
Ethereum,
|
|
|
|
/// Ethereum classic.
|
|
|
|
EthereumClassic,
|
|
|
|
}
|
|
|
|
|
2017-02-10 01:07:06 +01:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
match *self {
|
|
|
|
Error::KeyNotFound => write!(f, "Key not found for given address."),
|
|
|
|
Error::LedgerDevice(ref e) => write!(f, "{}", e),
|
2017-09-14 19:28:43 +02:00
|
|
|
Error::TrezorDevice(ref e) => write!(f, "{}", e),
|
2017-02-10 01:07:06 +01:00
|
|
|
Error::Usb(ref e) => write!(f, "{}", e),
|
2017-09-14 19:28:43 +02:00
|
|
|
Error::Hid(ref e) => write!(f, "{}", e),
|
2017-02-10 01:07:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ledger::Error> for Error {
|
2018-06-18 11:55:23 +02:00
|
|
|
fn from(err: ledger::Error) -> Self {
|
2017-02-10 01:07:06 +01:00
|
|
|
match err {
|
|
|
|
ledger::Error::KeyNotFound => Error::KeyNotFound,
|
|
|
|
_ => Error::LedgerDevice(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:28:43 +02:00
|
|
|
impl From<trezor::Error> for Error {
|
2018-06-18 11:55:23 +02:00
|
|
|
fn from(err: trezor::Error) -> Self {
|
2017-09-14 19:28:43 +02:00
|
|
|
match err {
|
|
|
|
trezor::Error::KeyNotFound => Error::KeyNotFound,
|
|
|
|
_ => Error::TrezorDevice(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 01:07:06 +01:00
|
|
|
impl From<libusb::Error> for Error {
|
2018-06-18 11:55:23 +02:00
|
|
|
fn from(err: libusb::Error) -> Self {
|
2017-02-10 01:07:06 +01:00
|
|
|
Error::Usb(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-13 11:01:56 +02:00
|
|
|
/// Specifies the direction of the `HardwareWallet` i.e, whether it arrived or left
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum DeviceDirection {
|
2018-06-18 11:55:23 +02:00
|
|
|
/// Device arrived
|
|
|
|
Arrived,
|
|
|
|
/// Device left
|
|
|
|
Left,
|
2018-06-13 11:01:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for DeviceDirection {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
DeviceDirection::Arrived => write!(f, "arrived"),
|
|
|
|
DeviceDirection::Left => write!(f, "left"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 01:07:06 +01:00
|
|
|
/// Hardware wallet management interface.
|
|
|
|
pub struct HardwareWalletManager {
|
|
|
|
exiting: Arc<AtomicBool>,
|
2017-09-14 19:28:43 +02:00
|
|
|
ledger: Arc<ledger::Manager>,
|
|
|
|
trezor: Arc<trezor::Manager>,
|
2017-02-10 01:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HardwareWalletManager {
|
2018-02-27 16:45:16 +01:00
|
|
|
/// Hardware wallet constructor
|
2018-06-18 11:55:23 +02:00
|
|
|
pub fn new() -> Result<Self, Error> {
|
2017-02-10 01:07:06 +01:00
|
|
|
let exiting = Arc::new(AtomicBool::new(false));
|
2018-05-01 15:01:49 +02:00
|
|
|
let hidapi = Arc::new(Mutex::new(hidapi::HidApi::new().map_err(|e| Error::Hid(e.to_string().clone()))?));
|
|
|
|
let ledger = ledger::Manager::new(hidapi.clone(), exiting.clone())?;
|
|
|
|
let trezor = trezor::Manager::new(hidapi.clone(), exiting.clone())?;
|
2018-02-27 16:45:16 +01:00
|
|
|
|
2018-06-18 11:55:23 +02:00
|
|
|
Ok(Self {
|
|
|
|
exiting,
|
|
|
|
ledger,
|
|
|
|
trezor,
|
2017-02-10 01:07:06 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Select key derivation path for a chain.
|
2018-05-01 15:01:49 +02:00
|
|
|
/// Currently, only one hard-coded keypath is supported
|
|
|
|
/// It is managed by `ethcore/account_provider`
|
2017-02-10 01:07:06 +01:00
|
|
|
pub fn set_key_path(&self, key_path: KeyPath) {
|
2017-09-14 19:28:43 +02:00
|
|
|
self.ledger.set_key_path(key_path);
|
|
|
|
self.trezor.set_key_path(key_path);
|
2017-02-10 01:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// List connected wallets. This only returns wallets that are ready to be used.
|
|
|
|
pub fn list_wallets(&self) -> Vec<WalletInfo> {
|
2017-09-14 19:28:43 +02:00
|
|
|
let mut wallets = Vec::new();
|
|
|
|
wallets.extend(self.ledger.list_devices());
|
|
|
|
wallets.extend(self.trezor.list_devices());
|
|
|
|
wallets
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a list of paths to locked hardware wallets
|
2018-05-01 15:01:49 +02:00
|
|
|
/// This is only applicable to Trezor because Ledger only appears as
|
|
|
|
/// a device when it is unlocked
|
2017-09-14 19:28:43 +02:00
|
|
|
pub fn list_locked_wallets(&self) -> Result<Vec<String>, Error> {
|
|
|
|
Ok(self.trezor.list_locked_devices())
|
2017-02-10 01:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get connected wallet info.
|
|
|
|
pub fn wallet_info(&self, address: &Address) -> Option<WalletInfo> {
|
2018-05-01 15:01:49 +02:00
|
|
|
if let Some(info) = self.ledger.get_wallet(address) {
|
2017-09-14 19:28:43 +02:00
|
|
|
Some(info)
|
|
|
|
} else {
|
2018-05-01 15:01:49 +02:00
|
|
|
self.trezor.get_wallet(address)
|
2017-09-14 19:28:43 +02:00
|
|
|
}
|
2017-02-10 01:07:06 +01:00
|
|
|
}
|
|
|
|
|
2018-06-13 11:01:56 +02:00
|
|
|
/// Sign a message with the wallet (only supported by Ledger)
|
|
|
|
pub fn sign_message(&self, address: &Address, msg: &[u8]) -> Result<Signature, Error> {
|
|
|
|
if self.ledger.get_wallet(address).is_some() {
|
|
|
|
Ok(self.ledger.sign_message(address, msg)?)
|
|
|
|
} else if self.trezor.get_wallet(address).is_some() {
|
|
|
|
Err(Error::TrezorDevice(trezor::Error::NoSigningMessage))
|
|
|
|
} else {
|
|
|
|
Err(Error::KeyNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 01:07:06 +01:00
|
|
|
/// Sign transaction data with wallet managing `address`.
|
2017-09-14 19:28:43 +02:00
|
|
|
pub fn sign_transaction(&self, address: &Address, t_info: &TransactionInfo, encoded_transaction: &[u8]) -> Result<Signature, Error> {
|
2018-05-01 15:01:49 +02:00
|
|
|
if self.ledger.get_wallet(address).is_some() {
|
2017-09-14 19:28:43 +02:00
|
|
|
Ok(self.ledger.sign_transaction(address, encoded_transaction)?)
|
2018-05-01 15:01:49 +02:00
|
|
|
} else if self.trezor.get_wallet(address).is_some() {
|
2017-09-14 19:28:43 +02:00
|
|
|
Ok(self.trezor.sign_transaction(address, t_info)?)
|
|
|
|
} else {
|
|
|
|
Err(Error::KeyNotFound)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Send a pin to a device at a certain path to unlock it
|
2018-05-01 15:01:49 +02:00
|
|
|
/// This is only applicable to Trezor because Ledger only appears as
|
|
|
|
/// a device when it is unlocked
|
2017-09-14 19:28:43 +02:00
|
|
|
pub fn pin_matrix_ack(&self, path: &str, pin: &str) -> Result<bool, Error> {
|
|
|
|
self.trezor.pin_matrix_ack(path, pin).map_err(Error::TrezorDevice)
|
2017-02-10 01:07:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for HardwareWalletManager {
|
|
|
|
fn drop(&mut self) {
|
2018-02-27 16:45:16 +01:00
|
|
|
// Indicate to the USB Hotplug handlers that they
|
|
|
|
// shall terminate but don't wait for them to terminate.
|
|
|
|
// If they don't terminate for some reason USB Hotplug events will be handled
|
|
|
|
// even if the HardwareWalletManger has been dropped
|
2017-02-10 01:07:06 +01:00
|
|
|
self.exiting.store(true, atomic::Ordering::Release);
|
|
|
|
}
|
|
|
|
}
|