Skip to content

Commit

Permalink
Reduce repeated enclave code by splitting it into its own wallet lib.…
Browse files Browse the repository at this point in the history
… Fix some types and error messages.
  • Loading branch information
wbobeirne committed Jun 14, 2018
1 parent 0c15ddf commit e360b90
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
15 changes: 9 additions & 6 deletions common/components/BalanceSidebar/AccountInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { connect } from 'react-redux';
import { toChecksumAddress } from 'ethereumjs-util';
import { UnitDisplay, NewTabLink } from 'components/ui';
import { IWallet, TrezorWallet, LedgerWallet, Balance } from 'libs/wallet';
import { IWallet, HardwareWallet, Balance } from 'libs/wallet';
import translate, { translateRaw } from 'translations';
import Spinner from 'components/ui/Spinner';
import { getNetworkConfig, getOffline } from 'selectors/config';
Expand Down Expand Up @@ -73,7 +73,7 @@ class AccountInfo extends React.Component<Props, State> {
};

public render() {
const { network, isOffline, balance } = this.props;
const { network, isOffline, balance, wallet } = this.props;
const { address, showLongBalance, confirmAddr } = this.state;

let blockExplorer;
Expand All @@ -84,12 +84,11 @@ class AccountInfo extends React.Component<Props, State> {
tokenExplorer = network.tokenExplorer;
}

const wallet = this.props.wallet as LedgerWallet | TrezorWallet;
return (
<div>
<AccountAddress address={toChecksumAddress(address)} />

{typeof wallet.displayAddress === 'function' && (
{isHardwareWallet(wallet) && (
<div className="AccountInfo-section">
<a
className="AccountInfo-address-hw-addr"
Expand All @@ -98,9 +97,9 @@ class AccountInfo extends React.Component<Props, State> {
wallet
.displayAddress()
.then(() => this.toggleConfirmAddr())
.catch(e => {
.catch((e: Error | string) => {
console.error('Display address failed', e);
this.toggleConfirmAddr();
throw new Error(e);
});
}}
>
Expand Down Expand Up @@ -192,6 +191,10 @@ class AccountInfo extends React.Component<Props, State> {
}
}

function isHardwareWallet(wallet: IWallet): wallet is HardwareWallet {
return typeof (wallet as any).displayAddress === 'function';
}

function mapStateToProps(state: AppState): StateProps {
return {
balance: state.wallet.balance,
Expand Down
12 changes: 8 additions & 4 deletions common/libs/wallet/deterministic/hardware.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import EthTx from 'ethereumjs-tx';
import { DeterministicWallet } from './deterministic';
import { IFullWallet } from '../IWallet';

export interface ChainCodeResponse {
chainCode: string;
publicKey: string;
}

export class HardwareWallet extends DeterministicWallet {
export abstract class HardwareWallet extends DeterministicWallet implements IFullWallet {
// Static functions can't be abstract, so implement an errorous one
// @ts-ignore
public static getChainCode(dpath: string): Promise<ChainCodeResponse> {
throw new Error(`getChainCode is not implemented in ${this.constructor.name}`);
}

public displayAddress(): Promise<boolean> {
throw new Error(`displayAddress is not implemented in ${this.constructor.name}`);
}
public abstract signRawTransaction(t: EthTx): Promise<Buffer>;
public abstract signMessage(msg: string): Promise<string>;
public abstract displayAddress(): Promise<boolean>;
public abstract getWalletType(): string;
}
1 change: 1 addition & 0 deletions common/libs/wallet/deterministic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ function enclaveOrWallet<T>(type: WalletTypes, lib: T) {

export * from './mnemonic';
export * from './trezor';
export * from './hardware';
export const LedgerWallet = enclaveOrWallet(WalletTypes.LEDGER, LedgerWalletWeb);
7 changes: 4 additions & 3 deletions common/libs/wallet/deterministic/trezor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import TC from 'vendor/trezor-connect';
import { HardwareWallet, ChainCodeResponse } from './hardware';
import { getTransactionFields } from 'libs/transaction';
import mapValues from 'lodash/mapValues';
import { IFullWallet } from '../IWallet';
import { translateRaw } from 'translations';
import EnclaveAPI, { WalletTypes } from 'shared/enclave/client';

export const TREZOR_MINIMUM_FIRMWARE = '1.5.2';
const TrezorConnect = TC as any;

export class TrezorWallet extends HardwareWallet implements IFullWallet {
export class TrezorWallet extends HardwareWallet {
public static getChainCode(dpath: string): Promise<ChainCodeResponse> {
if (process.env.BUILD_ELECTRON) {
return EnclaveAPI.getChainCode({
Expand Down Expand Up @@ -78,7 +77,9 @@ export class TrezorWallet extends HardwareWallet implements IFullWallet {
});
}

public signMessage = () => Promise.reject(new Error('Signing via Trezor not yet supported.'));
public signMessage() {
return Promise.reject(new Error('Signing via Trezor not yet supported.'));
}

public displayAddress(): Promise<any> {
return new Promise(resolve => {
Expand Down
1 change: 1 addition & 0 deletions common/translations/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"X_MIST": "Mist",
"X_WEB3": "Web3",
"X_MNEMONIC": "Mnemonic Phrase ",
"X_HARDWARE_WALLET": "hardware wallet ",
"X_PRINT": "Print Paper Wallet ",
"X_PRINTDESC": "ProTip: Click print and save this as a PDF, even if you do not own a printer! ",
"X_PRIVKEY": "Private Key (unencrypted) ",
Expand Down
3 changes: 3 additions & 0 deletions shared/enclave/server/wallets/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ async function getEthApp() {
}
return new LedgerEth(transport);
} catch (err) {
if (err && err.name === 'TransportError') {
throw new Error('ENCLAVE_LEDGER_IN_USE');
}
if (err && err.message && err.message.includes('cannot open device with path')) {
throw new Error('ENCLAVE_LEDGER_IN_USE');
}
Expand Down

0 comments on commit e360b90

Please sign in to comment.