From 8521775389b2e7ddefec20f1d06331e4bc3d0540 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Mon, 5 Mar 2018 21:05:30 -0500 Subject: Update ledgerco packages --- packages/subproviders/package.json | 4 ++- packages/subproviders/src/globals.d.ts | 22 ++++++++++++ packages/subproviders/src/index.ts | 30 ++++++++-------- packages/subproviders/src/subproviders/ledger.ts | 8 ++--- packages/subproviders/src/types.ts | 44 +++++++++++++++++------- 5 files changed, 76 insertions(+), 32 deletions(-) (limited to 'packages') diff --git a/packages/subproviders/package.json b/packages/subproviders/package.json index ec0642bce..7c02b5745 100644 --- a/packages/subproviders/package.json +++ b/packages/subproviders/package.json @@ -21,12 +21,14 @@ "@0xproject/assert": "^0.1.0", "@0xproject/types": "^0.3.0", "@0xproject/utils": "^0.4.0", + "@ledgerhq/hw-app-eth": "^4.3.0", + "@ledgerhq/hw-transport": "^4.3.0", + "@ledgerhq/hw-transport-u2f": "^4.3.0", "bn.js": "^4.11.8", "es6-promisify": "^5.0.0", "ethereumjs-tx": "^1.3.3", "ethereumjs-util": "^5.1.1", "hdkey": "^0.7.1", - "ledgerco": "0xProject/ledger-node-js-api", "lodash": "^4.17.4", "semaphore-async-await": "^1.5.1", "web3": "^0.20.0", diff --git a/packages/subproviders/src/globals.d.ts b/packages/subproviders/src/globals.d.ts index 6f344dcd3..0c8b3fed3 100644 --- a/packages/subproviders/src/globals.d.ts +++ b/packages/subproviders/src/globals.d.ts @@ -32,6 +32,28 @@ interface ECSignature { r: string; s: string; } + +interface LedgerTransport { + close(): Promise +} + +declare module '@ledgerhq/hw-app-eth' { + export class Eth { + constructor(transport: LedgerTransport); + public getAddress(path: string, boolDisplay?: boolean, boolChaincode?: boolean): Promise<{ publicKey: string; address: string; chainCode: string }>; + public signTransaction(path: string, rawTxHex: string): Promise; + public getAppConfiguration(): Promise<{ arbitraryDataEnabled: number; version: string; }>; + public signPersonalMessage(path: string, messageHex: string): Promise; + transport: LedgerTransport; + } +} +declare module '@ledgerhq/hw-transport-u2f' { + export class TransportU2F implements LedgerTransport { + public static create(): Promise; + public close(): Promise; + } +} + declare module 'ledgerco' { interface comm { close_async(): Promise; diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts index 4da405ec0..566743fd0 100644 --- a/packages/subproviders/src/index.ts +++ b/packages/subproviders/src/index.ts @@ -1,8 +1,10 @@ import { - comm_node as LedgerNodeCommunication, - comm_u2f as LedgerBrowserCommunication, - eth as LedgerEthereumClientFn, -} from 'ledgerco'; + Eth as LedgerEthereumClientFn, +} from '@ledgerhq/hw-app-eth'; + +import { + TransportU2F as LedgerBrowserCommunication, +} from '@ledgerhq/hw-transport-u2f'; import { LedgerEthereumClient } from './types'; @@ -19,17 +21,17 @@ export { ECSignature, LedgerWalletSubprovider, LedgerCommunicationClient, NonceS * @return LedgerEthereumClient A browser client */ export async function ledgerEthereumBrowserClientFactoryAsync(): Promise { - const ledgerConnection = await LedgerBrowserCommunication.create_async(); + const ledgerConnection = await LedgerBrowserCommunication.create(); const ledgerEthClient = new LedgerEthereumClientFn(ledgerConnection); return ledgerEthClient; } -/** - * A factory for creating a LedgerEthereumClient usable in a Node.js context. - * @return LedgerEthereumClient A Node.js client - */ -export async function ledgerEthereumNodeJsClientFactoryAsync(): Promise { - const ledgerConnection = await LedgerNodeCommunication.create_async(); - const ledgerEthClient = new LedgerEthereumClientFn(ledgerConnection); - return ledgerEthClient; -} +// /** +// * A factory for creating a LedgerEthereumClient usable in a Node.js context. +// * @return LedgerEthereumClient A Node.js client +// */ +// export async function ledgerEthereumNodeJsClientFactoryAsync(): Promise { +// const ledgerConnection = await LedgerNodeCommunication.create_async(); +// const ledgerEthClient = new LedgerEthereumClientFn(ledgerConnection); +// return ledgerEthClient; +// } diff --git a/packages/subproviders/src/subproviders/ledger.ts b/packages/subproviders/src/subproviders/ledger.ts index 85cdf0efc..0a84caae3 100644 --- a/packages/subproviders/src/subproviders/ledger.ts +++ b/packages/subproviders/src/subproviders/ledger.ts @@ -134,7 +134,7 @@ export class LedgerSubprovider extends Subprovider { let ledgerResponse; try { - ledgerResponse = await this._ledgerClientIfExists.getAddress_async( + ledgerResponse = await this._ledgerClientIfExists.getAddress( this._derivationPath, this._shouldAlwaysAskForConfirmation, SHOULD_GET_CHAIN_CODE, @@ -173,7 +173,7 @@ export class LedgerSubprovider extends Subprovider { const txHex = tx.serialize().toString('hex'); try { const derivationPath = this._getDerivationPath(); - const result = await this._ledgerClientIfExists.signTransaction_async(derivationPath, txHex); + const result = await this._ledgerClientIfExists.signTransaction(derivationPath, txHex); // Store signature in transaction tx.r = Buffer.from(result.r, 'hex'); tx.s = Buffer.from(result.s, 'hex'); @@ -199,7 +199,7 @@ export class LedgerSubprovider extends Subprovider { this._ledgerClientIfExists = await this._createLedgerClientAsync(); try { const derivationPath = this._getDerivationPath(); - const result = await this._ledgerClientIfExists.signPersonalMessage_async( + const result = await this._ledgerClientIfExists.signPersonalMessage( derivationPath, ethUtil.stripHexPrefix(data), ); @@ -236,7 +236,7 @@ export class LedgerSubprovider extends Subprovider { this._connectionLock.signal(); return; } - await this._ledgerClientIfExists.comm.close_async(); + await this._ledgerClientIfExists.transport.close(); this._ledgerClientIfExists = undefined; this._connectionLock.signal(); } diff --git a/packages/subproviders/src/types.ts b/packages/subproviders/src/types.ts index 65b7f6c8f..785547a22 100644 --- a/packages/subproviders/src/types.ts +++ b/packages/subproviders/src/types.ts @@ -1,7 +1,25 @@ import * as _ from 'lodash'; +declare class LedgerTransport { + // tslint:disable-next-line:async-suffix + public static create(): Promise; + // tslint:disable-next-line:async-suffix + public static close(): Promise; +} + export interface LedgerCommunicationClient { - close_async: () => Promise; + close: () => Promise; +} + +export interface LedgerEthereumClient { + getAddress: ( + derivationPath: string, + askForDeviceConfirmation?: boolean, + shouldGetChainCode?: boolean, + ) => Promise<{ publicKey: string; address: string; chainCode: string }>; + signTransaction: (derivationPath: string, rawTxHex: string) => Promise; + signPersonalMessage: (derivationPath: string, messageHex: string) => Promise; + transport: LedgerCommunicationClient; } /* @@ -9,18 +27,18 @@ export interface LedgerCommunicationClient { * It uses an internal LedgerCommunicationClient to relay these requests. Currently * NodeJs and Browser communication are supported. */ -export interface LedgerEthereumClient { - // shouldGetChainCode is defined as `true` instead of `boolean` because other types rely on the assumption - // that we get back the chain code and we don't have dependent types to express it properly - getAddress_async: ( - derivationPath: string, - askForDeviceConfirmation: boolean, - shouldGetChainCode: true, - ) => Promise; - signPersonalMessage_async: (derivationPath: string, messageHex: string) => Promise; - signTransaction_async: (derivationPath: string, txHex: string) => Promise; - comm: LedgerCommunicationClient; -} +// export interface LedgerEthereumClient { +// // shouldGetChainCode is defined as `true` instead of `boolean` because other types rely on the assumption +// // that we get back the chain code and we don't have dependent types to express it properly +// getAddress_async: ( +// derivationPath: string, +// askForDeviceConfirmation: boolean, +// shouldGetChainCode: true, +// ) => Promise; +// signPersonalMessage_async: (derivationPath: string, messageHex: string) => Promise; +// signTransaction_async: (derivationPath: string, txHex: string) => Promise; +// comm: LedgerCommunicationClient; +// } export interface ECSignatureString { v: string; -- cgit