From 3b5dd4ec21bbf28911081a7517b8e6b5bc1369d4 Mon Sep 17 00:00:00 2001 From: Will O'Beirne Date: Fri, 4 May 2018 00:40:32 -0400 Subject: [PATCH] Display address --- common/libs/wallet/deterministic/ledger.ts | 13 +++++++++++- shared/enclave/client/index.ts | 8 +++++++- .../enclave/server/handlers/displayAddress.ts | 7 +++++++ shared/enclave/server/handlers/index.ts | 4 +++- shared/enclave/server/wallets/keepkey.ts | 4 ++++ shared/enclave/server/wallets/ledger.ts | 17 ++++++++++++++-- shared/enclave/server/wallets/trezor.ts | 4 ++++ shared/enclave/types.ts | 20 ++++++++++++++++--- 8 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 shared/enclave/server/handlers/displayAddress.ts diff --git a/common/libs/wallet/deterministic/ledger.ts b/common/libs/wallet/deterministic/ledger.ts index f74dafaf..8e4df3a7 100644 --- a/common/libs/wallet/deterministic/ledger.ts +++ b/common/libs/wallet/deterministic/ledger.ts @@ -96,8 +96,19 @@ export class LedgerWallet extends HardwareWallet implements IFullWallet { } public displayAddress() { + const path = this.dPath + '/' + this.index; + + if (process.env.BUILD_ELECTRON) { + return EnclaveAPI.displayAddress({ + walletType: WalletTypes.LEDGER, + path + }) + .then(res => res.success) + .catch(() => false); + } + return this.ethApp - .getAddress_async(this.dPath + '/' + this.index, true, false) + .getAddress_async(path, true, false) .then(() => true) .catch(() => false); } diff --git a/shared/enclave/client/index.ts b/shared/enclave/client/index.ts index 8aaf58ea..6381a8fa 100644 --- a/shared/enclave/client/index.ts +++ b/shared/enclave/client/index.ts @@ -8,7 +8,9 @@ import { SignTransactionParams, SignTransactionResponse, SignMessageParams, - SignMessageResponse + SignMessageResponse, + DisplayAddressParams, + DisplayAddressResponse } from 'shared/enclave/types'; const api = { @@ -26,6 +28,10 @@ const api = { signMessage(params: SignMessageParams) { return makeRequest(EnclaveMethods.SIGN_MESSAGE, params); + }, + + displayAddress(params: DisplayAddressParams) { + return makeRequest(EnclaveMethods.DISPLAY_ADDRESS, params); } }; diff --git a/shared/enclave/server/handlers/displayAddress.ts b/shared/enclave/server/handlers/displayAddress.ts new file mode 100644 index 00000000..87de4174 --- /dev/null +++ b/shared/enclave/server/handlers/displayAddress.ts @@ -0,0 +1,7 @@ +import { getWalletLib } from 'shared/enclave/server/wallets'; +import { DisplayAddressParams, DisplayAddressResponse } from 'shared/enclave/types'; + +export default function(params: DisplayAddressParams): Promise { + const wallet = getWalletLib(params.walletType); + return wallet.displayAddress(params.path); +} diff --git a/shared/enclave/server/handlers/index.ts b/shared/enclave/server/handlers/index.ts index 856591bf..af7068d2 100644 --- a/shared/enclave/server/handlers/index.ts +++ b/shared/enclave/server/handlers/index.ts @@ -2,6 +2,7 @@ import getAddresses from './getAddresses'; import getChainCode from './getChainCode'; import signTransaction from './signTransaction'; import signMessage from './signMessage'; +import displayAddress from './displayAddress'; import { EnclaveMethods, EnclaveMethodParams, EnclaveMethodResponse } from 'shared/enclave/types'; const handlers: { @@ -12,7 +13,8 @@ const handlers: { [EnclaveMethods.GET_ADDRESSES]: getAddresses, [EnclaveMethods.GET_CHAIN_CODE]: getChainCode, [EnclaveMethods.SIGN_TRANSACTION]: signTransaction, - [EnclaveMethods.SIGN_MESSAGE]: signMessage + [EnclaveMethods.SIGN_MESSAGE]: signMessage, + [EnclaveMethods.DISPLAY_ADDRESS]: displayAddress }; export default handlers; diff --git a/shared/enclave/server/wallets/keepkey.ts b/shared/enclave/server/wallets/keepkey.ts index 91ff0d01..82258d78 100644 --- a/shared/enclave/server/wallets/keepkey.ts +++ b/shared/enclave/server/wallets/keepkey.ts @@ -11,6 +11,10 @@ const KeepKey: WalletLib = { async signMessage() { throw new Error('Not yet implemented'); + }, + + async displayAddress() { + throw new Error('Not yet implemented'); } }; diff --git a/shared/enclave/server/wallets/ledger.ts b/shared/enclave/server/wallets/ledger.ts index 52dbcd98..1aeff5fd 100644 --- a/shared/enclave/server/wallets/ledger.ts +++ b/shared/enclave/server/wallets/ledger.ts @@ -9,7 +9,6 @@ async function getEthApp() { const transport = await TransportNodeHid.create(); return new LedgerEth(transport); } catch (err) { - console.log(err.message); if (err && err.message && err.message.includes('cannot open device with path')) { throw new Error( 'Failed to connect with your Ledger. It may be in use with another application. Try plugging the device back in.' @@ -54,7 +53,7 @@ const Ledger: WalletLib = { }; }, - async signMessage(msg: string, path: string) { + async signMessage(msg, path) { const app = await getEthApp(); const msgHex = Buffer.from(msg).toString('hex'); const signed = await app.signPersonalMessage(path, msgHex); @@ -62,6 +61,20 @@ const Ledger: WalletLib = { return { signedMessage: combined }; + }, + + async displayAddress(path) { + try { + const app = await getEthApp(); + await app.getAddress(path, true, false); + return { + success: true + }; + } catch (err) { + return { + success: false + }; + } } }; diff --git a/shared/enclave/server/wallets/trezor.ts b/shared/enclave/server/wallets/trezor.ts index 2752d411..8df6ec61 100644 --- a/shared/enclave/server/wallets/trezor.ts +++ b/shared/enclave/server/wallets/trezor.ts @@ -22,6 +22,10 @@ const Trezor: WalletLib = { async signMessage() { throw new Error('Not yet implemented'); + }, + + async displayAddress() { + throw new Error('Not yet implemented'); } }; diff --git a/shared/enclave/types.ts b/shared/enclave/types.ts index e657be28..11af7b4d 100644 --- a/shared/enclave/types.ts +++ b/shared/enclave/types.ts @@ -3,7 +3,8 @@ export enum EnclaveMethods { GET_ADDRESSES = 'get-addresses', GET_CHAIN_CODE = 'get-chain-code', SIGN_TRANSACTION = 'sign-transaction', - SIGN_MESSAGE = 'sign-message' + SIGN_MESSAGE = 'sign-message', + DISPLAY_ADDRESS = 'display-address' } export enum WalletTypes { @@ -64,17 +65,29 @@ export interface SignMessageResponse { signedMessage: string; } +// Display Address Request +export interface DisplayAddressParams { + walletType: WalletTypes; + path: string; +} + +export interface DisplayAddressResponse { + success: boolean; +} + // All Requests & Responses export type EnclaveMethodParams = | GetAddressesParams | GetChainCodeParams | SignTransactionParams - | SignMessageParams; + | SignMessageParams + | DisplayAddressParams; export type EnclaveMethodResponse = | GetAddressesResponse | GetChainCodeResponse | SignTransactionResponse - | SignMessageResponse; + | SignMessageResponse + | DisplayAddressResponse; // RPC requests, responses & failures export interface EnclaveSuccessResponse { @@ -100,4 +113,5 @@ export interface WalletLib { getChainCode(dpath: string): Promise; signTransaction(transaction: RawTransaction, path: string): Promise; signMessage(msg: string, path: string): Promise; + displayAddress(path: string): Promise; }