aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders
diff options
context:
space:
mode:
Diffstat (limited to 'packages/subproviders')
-rw-r--r--packages/subproviders/src/globals.d.ts36
-rw-r--r--packages/subproviders/src/index.ts16
-rw-r--r--packages/subproviders/src/subproviders/injected_web3.ts4
-rw-r--r--packages/subproviders/src/subproviders/ledger.ts35
-rw-r--r--packages/subproviders/src/subproviders/redundant_rpc.ts20
-rw-r--r--packages/subproviders/src/subproviders/subprovider.ts4
-rw-r--r--packages/subproviders/src/types.ts7
-rw-r--r--packages/subproviders/test/integration/ledger_subprovider_test.ts18
-rw-r--r--packages/subproviders/test/unit/ledger_subprovider_test.ts46
-rw-r--r--packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts19
-rw-r--r--packages/subproviders/test/utils/report_callback_errors.ts2
11 files changed, 104 insertions, 103 deletions
diff --git a/packages/subproviders/src/globals.d.ts b/packages/subproviders/src/globals.d.ts
index 3800c970f..97fc34734 100644
--- a/packages/subproviders/src/globals.d.ts
+++ b/packages/subproviders/src/globals.d.ts
@@ -47,10 +47,16 @@ declare module 'ledgerco' {
export class eth {
public comm: comm;
constructor(comm: comm);
- public getAddress_async(path: string, display?: boolean, chaincode?: boolean):
- Promise<{publicKey: string; address: string; chainCode: string}>;
+ public getAddress_async(
+ path: string,
+ display?: boolean,
+ chaincode?: boolean,
+ ): Promise<{ publicKey: string; address: string; chainCode: string }>;
public signTransaction_async(path: string, rawTxHex: string): Promise<ECSignatureString>;
- public getAppConfiguration_async(): Promise<{ arbitraryDataEnabled: number; version: string }>;
+ public getAppConfiguration_async(): Promise<{
+ arbitraryDataEnabled: number;
+ version: string;
+ }>;
public signPersonalMessage_async(path: string, messageHex: string): Promise<ECSignature>;
}
}
@@ -73,23 +79,25 @@ declare module 'web3-provider-engine/subproviders/subprovider' {
declare module 'web3-provider-engine/subproviders/rpc' {
import * as Web3 from 'web3';
class RpcSubprovider {
- constructor(options: {rpcUrl: string});
+ constructor(options: { rpcUrl: string });
public handleRequest(
- payload: Web3.JSONRPCRequestPayload, next: () => void, end: (err: Error|null, data?: any) => void,
+ payload: Web3.JSONRPCRequestPayload,
+ next: () => void,
+ end: (err: Error | null, data?: any) => void,
): void;
}
export = RpcSubprovider;
}
declare module 'web3-provider-engine' {
- class Web3ProviderEngine {
- public on(event: string, handler: () => void): void;
- public send(payload: any): void;
- public sendAsync(payload: any, callback: (error: any, response: any) => void): void;
- public addProvider(provider: any): void;
- public start(): void;
- public stop(): void;
- }
- export = Web3ProviderEngine;
+ class Web3ProviderEngine {
+ public on(event: string, handler: () => void): void;
+ public send(payload: any): void;
+ public sendAsync(payload: any, callback: (error: any, response: any) => void): void;
+ public addProvider(provider: any): void;
+ public start(): void;
+ public stop(): void;
+ }
+ export = Web3ProviderEngine;
}
// hdkey declarations
diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts
index 9b7cab3fa..720c4362f 100644
--- a/packages/subproviders/src/index.ts
+++ b/packages/subproviders/src/index.ts
@@ -4,18 +4,12 @@ import {
eth as LedgerEthereumClientFn,
} from 'ledgerco';
-import {LedgerEthereumClient} from './types';
+import { LedgerEthereumClient } from './types';
-export {InjectedWeb3Subprovider} from './subproviders/injected_web3';
-export {RedundantRPCSubprovider} from './subproviders/redundant_rpc';
-export {
- LedgerSubprovider,
-} from './subproviders/ledger';
-export {
- ECSignature,
- LedgerWalletSubprovider,
- LedgerCommunicationClient,
-} from './types';
+export { InjectedWeb3Subprovider } from './subproviders/injected_web3';
+export { RedundantRPCSubprovider } from './subproviders/redundant_rpc';
+export { LedgerSubprovider } from './subproviders/ledger';
+export { ECSignature, LedgerWalletSubprovider, LedgerCommunicationClient } from './types';
/**
* A factory method for creating a LedgerEthereumClient usable in a browser context.
diff --git a/packages/subproviders/src/subproviders/injected_web3.ts b/packages/subproviders/src/subproviders/injected_web3.ts
index b963f0c9b..bd29acb22 100644
--- a/packages/subproviders/src/subproviders/injected_web3.ts
+++ b/packages/subproviders/src/subproviders/injected_web3.ts
@@ -14,7 +14,9 @@ export class InjectedWeb3Subprovider {
this._injectedWeb3 = injectedWeb3;
}
public handleRequest(
- payload: Web3.JSONRPCRequestPayload, next: () => void, end: (err: Error|null, result: any) => void,
+ payload: Web3.JSONRPCRequestPayload,
+ next: () => void,
+ end: (err: Error | null, result: any) => void,
) {
switch (payload.method) {
case 'web3_clientVersion':
diff --git a/packages/subproviders/src/subproviders/ledger.ts b/packages/subproviders/src/subproviders/ledger.ts
index d44d24f82..7267a793e 100644
--- a/packages/subproviders/src/subproviders/ledger.ts
+++ b/packages/subproviders/src/subproviders/ledger.ts
@@ -1,5 +1,5 @@
-import {assert} from '@0xproject/assert';
-import {addressUtils} from '@0xproject/utils';
+import { assert } from '@0xproject/assert';
+import { addressUtils } from '@0xproject/utils';
import EthereumTx = require('ethereumjs-tx');
import ethUtil = require('ethereumjs-util');
import HDNode = require('hdkey');
@@ -16,7 +16,7 @@ import {
ResponseWithTxParams,
} from '../types';
-import {Subprovider} from './subprovider';
+import { Subprovider } from './subprovider';
const DEFAULT_DERIVATION_PATH = `44'/60'/0'`;
const NUM_ADDRESSES_TO_FETCH = 10;
@@ -44,12 +44,11 @@ export class LedgerSubprovider extends Subprovider {
this._networkId = config.networkId;
this._ledgerEthereumClientFactoryAsync = config.ledgerEthereumClientFactoryAsync;
this._derivationPath = config.derivationPath || DEFAULT_DERIVATION_PATH;
- this._shouldAlwaysAskForConfirmation = !_.isUndefined(config.accountFetchingConfigs) &&
- !_.isUndefined(
- config.accountFetchingConfigs.shouldAskForOnDeviceConfirmation,
- ) ?
- config.accountFetchingConfigs.shouldAskForOnDeviceConfirmation :
- ASK_FOR_ON_DEVICE_CONFIRMATION;
+ this._shouldAlwaysAskForConfirmation =
+ !_.isUndefined(config.accountFetchingConfigs) &&
+ !_.isUndefined(config.accountFetchingConfigs.shouldAskForOnDeviceConfirmation)
+ ? config.accountFetchingConfigs.shouldAskForOnDeviceConfirmation
+ : ASK_FOR_ON_DEVICE_CONFIRMATION;
this._derivationPathIndex = 0;
}
public getPath(): string {
@@ -62,7 +61,9 @@ export class LedgerSubprovider extends Subprovider {
this._derivationPathIndex = pathIndex;
}
public async handleRequest(
- payload: Web3.JSONRPCRequestPayload, next: () => void, end: (err: Error|null, result?: any) => void,
+ payload: Web3.JSONRPCRequestPayload,
+ next: () => void,
+ end: (err: Error | null, result?: any) => void,
) {
let accounts;
let txParams;
@@ -132,7 +133,9 @@ export class LedgerSubprovider extends Subprovider {
let ledgerResponse;
try {
ledgerResponse = await this._ledgerClientIfExists.getAddress_async(
- this._derivationPath, this._shouldAlwaysAskForConfirmation, SHOULD_GET_CHAIN_CODE,
+ this._derivationPath,
+ this._shouldAlwaysAskForConfirmation,
+ SHOULD_GET_CHAIN_CODE,
);
} finally {
await this._destroyLedgerClientAsync();
@@ -147,9 +150,9 @@ export class LedgerSubprovider extends Subprovider {
const derivedHDNode = hdKey.derive(`m/${i + this._derivationPathIndex}`);
const derivedPublicKey = derivedHDNode.publicKey;
const shouldSanitizePublicKey = true;
- const ethereumAddressUnprefixed = ethUtil.publicToAddress(
- derivedPublicKey, shouldSanitizePublicKey,
- ).toString('hex');
+ const ethereumAddressUnprefixed = ethUtil
+ .publicToAddress(derivedPublicKey, shouldSanitizePublicKey)
+ .toString('hex');
const ethereumAddressPrefixed = ethUtil.addHexPrefix(ethereumAddressUnprefixed);
accounts.push(ethereumAddressPrefixed.toLowerCase());
}
@@ -195,7 +198,9 @@ export class LedgerSubprovider extends Subprovider {
try {
const derivationPath = this._getDerivationPath();
const result = await this._ledgerClientIfExists.signPersonalMessage_async(
- derivationPath, ethUtil.stripHexPrefix(data));
+ derivationPath,
+ ethUtil.stripHexPrefix(data),
+ );
const v = result.v - 27;
let vHex = v.toString(16);
if (vHex.length < 2) {
diff --git a/packages/subproviders/src/subproviders/redundant_rpc.ts b/packages/subproviders/src/subproviders/redundant_rpc.ts
index 67e2a857b..a3cb463a8 100644
--- a/packages/subproviders/src/subproviders/redundant_rpc.ts
+++ b/packages/subproviders/src/subproviders/redundant_rpc.ts
@@ -1,17 +1,19 @@
-import {promisify} from '@0xproject/utils';
+import { promisify } from '@0xproject/utils';
import * as _ from 'lodash';
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
-import {JSONRPCPayload} from '../types';
+import { JSONRPCPayload } from '../types';
-import {Subprovider} from './subprovider';
+import { Subprovider } from './subprovider';
export class RedundantRPCSubprovider extends Subprovider {
private _rpcs: RpcSubprovider[];
private static async _firstSuccessAsync(
- rpcs: RpcSubprovider[], payload: JSONRPCPayload, next: () => void,
+ rpcs: RpcSubprovider[],
+ payload: JSONRPCPayload,
+ next: () => void,
): Promise<any> {
- let lastErr: Error|undefined;
+ let lastErr: Error | undefined;
for (const rpc of rpcs) {
try {
const data = await promisify(rpc.handleRequest.bind(rpc))(payload, next);
@@ -34,8 +36,11 @@ export class RedundantRPCSubprovider extends Subprovider {
});
}
// tslint:disable-next-line:async-suffix
- public async handleRequest(payload: JSONRPCPayload, next: () => void,
- end: (err: Error|null, data?: any) => void): Promise<void> {
+ public async handleRequest(
+ payload: JSONRPCPayload,
+ next: () => void,
+ end: (err: Error | null, data?: any) => void,
+ ): Promise<void> {
const rpcsCopy = this._rpcs.slice();
try {
const data = await RedundantRPCSubprovider._firstSuccessAsync(rpcsCopy, payload, next);
@@ -43,6 +48,5 @@ export class RedundantRPCSubprovider extends Subprovider {
} catch (err) {
end(err);
}
-
}
}
diff --git a/packages/subproviders/src/subproviders/subprovider.ts b/packages/subproviders/src/subproviders/subprovider.ts
index 56ead214d..6435c9f65 100644
--- a/packages/subproviders/src/subproviders/subprovider.ts
+++ b/packages/subproviders/src/subproviders/subprovider.ts
@@ -1,9 +1,7 @@
import promisify = require('es6-promisify');
import Web3 = require('web3');
-import {
- JSONRPCPayload,
-} from '../types';
+import { JSONRPCPayload } from '../types';
/*
* A version of the base class Subprovider found in providerEngine
* This one has an async/await `emitPayloadAsync` and also defined types.
diff --git a/packages/subproviders/src/types.ts b/packages/subproviders/src/types.ts
index c5ccf1fda..3db8be943 100644
--- a/packages/subproviders/src/types.ts
+++ b/packages/subproviders/src/types.ts
@@ -12,8 +12,11 @@ export interface 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<LedgerGetAddressResult>;
+ getAddress_async: (
+ derivationPath: string,
+ askForDeviceConfirmation: boolean,
+ shouldGetChainCode: true,
+ ) => Promise<LedgerGetAddressResult>;
signPersonalMessage_async: (derivationPath: string, messageHex: string) => Promise<ECSignature>;
signTransaction_async: (derivationPath: string, txHex: string) => Promise<ECSignatureString>;
comm: LedgerCommunicationClient;
diff --git a/packages/subproviders/test/integration/ledger_subprovider_test.ts b/packages/subproviders/test/integration/ledger_subprovider_test.ts
index 7f319ed4e..628b532d7 100644
--- a/packages/subproviders/test/integration/ledger_subprovider_test.ts
+++ b/packages/subproviders/test/integration/ledger_subprovider_test.ts
@@ -6,15 +6,10 @@ import Web3 = require('web3');
import Web3ProviderEngine = require('web3-provider-engine');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
-import {
- ledgerEthereumNodeJsClientFactoryAsync,
- LedgerSubprovider,
-} from '../../src';
-import {
- DoneCallback,
-} from '../../src/types';
-import {chaiSetup} from '../chai_setup';
-import {reportCallbackErrors} from '../utils/report_callback_errors';
+import { ledgerEthereumNodeJsClientFactoryAsync, LedgerSubprovider } from '../../src';
+import { DoneCallback } from '../../src/types';
+import { chaiSetup } from '../chai_setup';
+import { reportCallbackErrors } from '../utils/report_callback_errors';
chaiSetup.configure();
const expect = chai.expect;
@@ -51,8 +46,9 @@ describe('LedgerSubprovider', () => {
chainId: 3,
};
const txHex = await ledgerSubprovider.signTransactionAsync(tx);
- // tslint:disable-next-line:max-line-length
- expect(txHex).to.be.equal('0xf85f8080822710940000000000000000000000000000000000000000808077a088a95ef1378487bc82be558e82c8478baf840c545d5b887536bb1da63673a98ba0019f4a4b9a107d1e6752bf7f701e275f28c13791d6e76af895b07373462cefaa');
+ expect(txHex).to.be.equal(
+ '0xf85f8080822710940000000000000000000000000000000000000000808077a088a95ef1378487bc82be558e82c8478baf840c545d5b887536bb1da63673a98ba0019f4a4b9a107d1e6752bf7f701e275f28c13791d6e76af895b07373462cefaa',
+ );
});
});
describe('calls through a provider', () => {
diff --git a/packages/subproviders/test/unit/ledger_subprovider_test.ts b/packages/subproviders/test/unit/ledger_subprovider_test.ts
index d493adf1e..1c70dd3a6 100644
--- a/packages/subproviders/test/unit/ledger_subprovider_test.ts
+++ b/packages/subproviders/test/unit/ledger_subprovider_test.ts
@@ -5,16 +5,10 @@ import Web3 = require('web3');
import Web3ProviderEngine = require('web3-provider-engine');
import RpcSubprovider = require('web3-provider-engine/subproviders/rpc');
-import {
- LedgerSubprovider,
-} from '../../src';
-import {
- DoneCallback,
- LedgerCommunicationClient,
- LedgerSubproviderErrors,
-} from '../../src/types';
-import {chaiSetup} from '../chai_setup';
-import {reportCallbackErrors} from '../utils/report_callback_errors';
+import { LedgerSubprovider } from '../../src';
+import { DoneCallback, LedgerCommunicationClient, LedgerSubproviderErrors } from '../../src/types';
+import { chaiSetup } from '../chai_setup';
+import { reportCallbackErrors } from '../utils/report_callback_errors';
chaiSetup.configure();
const expect = chai.expect;
@@ -28,8 +22,8 @@ describe('LedgerSubprovider', () => {
// tslint:disable:no-object-literal-type-assertion
const ledgerEthClient = {
getAddress_async: async () => {
- // tslint:disable-next-line:max-line-length
- const publicKey = '04f428290f4c5ed6a198f71b8205f488141dbb3f0840c923bbfa798ecbee6370986c03b5575d94d506772fb48a6a44e345e4ebd4f028a6f609c44b655d6d3e71a1';
+ const publicKey =
+ '04f428290f4c5ed6a198f71b8205f488141dbb3f0840c923bbfa798ecbee6370986c03b5575d94d506772fb48a6a44e345e4ebd4f028a6f609c44b655d6d3e71a1';
const chainCode = 'ac055a5537c0c7e9e02d14a197cad6b857836da2a12043b46912a37d959b5ae8';
const address = '0xBa388BA5e5EEF2c6cE42d831c2B3A28D3c99bdB1';
return {
@@ -76,17 +70,20 @@ describe('LedgerSubprovider', () => {
it('signs a personal message', async () => {
const data = ethUtils.bufferToHex(ethUtils.toBuffer('hello world'));
const ecSignatureHex = await ledgerSubprovider.signPersonalMessageAsync(data);
- // tslint:disable-next-line:max-line-length
- expect(ecSignatureHex).to.be.equal('0xa6cc284bff14b42bdf5e9286730c152be91719d478605ec46b3bebcd0ae491480652a1a7b742ceb0213d1e744316e285f41f878d8af0b8e632cbca4c279132d001');
+ expect(ecSignatureHex).to.be.equal(
+ '0xa6cc284bff14b42bdf5e9286730c152be91719d478605ec46b3bebcd0ae491480652a1a7b742ceb0213d1e744316e285f41f878d8af0b8e632cbca4c279132d001',
+ );
});
});
describe('failure cases', () => {
it('cannot open multiple simultaneous connections to the Ledger device', async () => {
const data = ethUtils.bufferToHex(ethUtils.toBuffer('hello world'));
- return expect(Promise.all([
- ledgerSubprovider.getAccountsAsync(),
- ledgerSubprovider.signPersonalMessageAsync(data),
- ])).to.be.rejectedWith(LedgerSubproviderErrors.MultipleOpenConnectionsDisallowed);
+ return expect(
+ Promise.all([
+ ledgerSubprovider.getAccountsAsync(),
+ ledgerSubprovider.signPersonalMessageAsync(data),
+ ]),
+ ).to.be.rejectedWith(LedgerSubproviderErrors.MultipleOpenConnectionsDisallowed);
});
});
});
@@ -127,8 +124,9 @@ describe('LedgerSubprovider', () => {
};
const callback = reportCallbackErrors(done)((err: Error, response: Web3.JSONRPCResponsePayload) => {
expect(err).to.be.a('null');
- // tslint:disable-next-line:max-line-length
- expect(response.result).to.be.equal('0xa6cc284bff14b42bdf5e9286730c152be91719d478605ec46b3bebcd0ae491480652a1a7b742ceb0213d1e744316e285f41f878d8af0b8e632cbca4c279132d001');
+ expect(response.result).to.be.equal(
+ '0xa6cc284bff14b42bdf5e9286730c152be91719d478605ec46b3bebcd0ae491480652a1a7b742ceb0213d1e744316e285f41f878d8af0b8e632cbca4c279132d001',
+ );
done();
});
provider.sendAsync(payload, callback);
@@ -143,8 +141,9 @@ describe('LedgerSubprovider', () => {
};
const callback = reportCallbackErrors(done)((err: Error, response: Web3.JSONRPCResponsePayload) => {
expect(err).to.be.a('null');
- // tslint:disable-next-line:max-line-length
- expect(response.result).to.be.equal('0xa6cc284bff14b42bdf5e9286730c152be91719d478605ec46b3bebcd0ae491480652a1a7b742ceb0213d1e744316e285f41f878d8af0b8e632cbca4c279132d001');
+ expect(response.result).to.be.equal(
+ '0xa6cc284bff14b42bdf5e9286730c152be91719d478605ec46b3bebcd0ae491480652a1a7b742ceb0213d1e744316e285f41f878d8af0b8e632cbca4c279132d001',
+ );
done();
});
provider.sendAsync(payload, callback);
@@ -221,8 +220,7 @@ describe('LedgerSubprovider', () => {
});
provider.sendAsync(payload, callback);
});
- it('should throw if `from` param invalid address when calling eth_sendTransaction',
- (done: DoneCallback) => {
+ it('should throw if `from` param invalid address when calling eth_sendTransaction', (done: DoneCallback) => {
const tx = {
to: '0xafa3f8684e54059998bc3a7b0d2b0da075154d66',
from: '0xIncorrectEthereumAddress',
diff --git a/packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts b/packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts
index 2f1676915..c3170745c 100644
--- a/packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts
+++ b/packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts
@@ -3,12 +3,10 @@ import * as _ from 'lodash';
import Web3 = require('web3');
import Web3ProviderEngine = require('web3-provider-engine');
-import {RedundantRPCSubprovider} from '../../src';
-import {
- DoneCallback,
-} from '../../src/types';
-import {chaiSetup} from '../chai_setup';
-import {reportCallbackErrors} from '../utils/report_callback_errors';
+import { RedundantRPCSubprovider } from '../../src';
+import { DoneCallback } from '../../src/types';
+import { chaiSetup } from '../chai_setup';
+import { reportCallbackErrors } from '../utils/report_callback_errors';
const expect = chai.expect;
chaiSetup.configure();
@@ -17,9 +15,7 @@ describe('RedundantRpcSubprovider', () => {
let provider: Web3ProviderEngine;
it('succeeds when supplied a healthy endpoint', (done: DoneCallback) => {
provider = new Web3ProviderEngine();
- const endpoints = [
- 'http://localhost:8545',
- ];
+ const endpoints = ['http://localhost:8545'];
const redundantSubprovider = new RedundantRPCSubprovider(endpoints);
provider.addProvider(redundantSubprovider);
provider.start();
@@ -39,10 +35,7 @@ describe('RedundantRpcSubprovider', () => {
});
it('succeeds when supplied at least one healthy endpoint', (done: DoneCallback) => {
provider = new Web3ProviderEngine();
- const endpoints = [
- 'http://does-not-exist:3000',
- 'http://localhost:8545',
- ];
+ const endpoints = ['http://does-not-exist:3000', 'http://localhost:8545'];
const redundantSubprovider = new RedundantRPCSubprovider(endpoints);
provider.addProvider(redundantSubprovider);
provider.start();
diff --git a/packages/subproviders/test/utils/report_callback_errors.ts b/packages/subproviders/test/utils/report_callback_errors.ts
index 0aaef3f05..8a8f4d966 100644
--- a/packages/subproviders/test/utils/report_callback_errors.ts
+++ b/packages/subproviders/test/utils/report_callback_errors.ts
@@ -1,4 +1,4 @@
-import {DoneCallback} from '../../src/types';
+import { DoneCallback } from '../../src/types';
export const reportCallbackErrors = (done: DoneCallback) => {
return (f: (...args: any[]) => void) => {