diff options
author | Jacob Evans <dekz@dekz.net> | 2018-04-12 08:20:52 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-12 08:20:52 +0800 |
commit | be73084e04264d44cfbd7cf6b8ba3a993368133d (patch) | |
tree | 68fc540971871fd99d0977957ac20132d6efed60 /packages/subproviders | |
parent | 63b941fbaf08167234cf7871e874c1a96e4347fa (diff) | |
parent | 5eb90697c824f1c98467cdb6cd71dbb94ff70805 (diff) | |
download | dexon-sol-tools-be73084e04264d44cfbd7cf6b8ba3a993368133d.tar dexon-sol-tools-be73084e04264d44cfbd7cf6b8ba3a993368133d.tar.gz dexon-sol-tools-be73084e04264d44cfbd7cf6b8ba3a993368133d.tar.bz2 dexon-sol-tools-be73084e04264d44cfbd7cf6b8ba3a993368133d.tar.lz dexon-sol-tools-be73084e04264d44cfbd7cf6b8ba3a993368133d.tar.xz dexon-sol-tools-be73084e04264d44cfbd7cf6b8ba3a993368133d.tar.zst dexon-sol-tools-be73084e04264d44cfbd7cf6b8ba3a993368133d.zip |
Merge branch 'development' into feature/subproviders/mnemonic-wallet-subprovider
Diffstat (limited to 'packages/subproviders')
16 files changed, 91 insertions, 52 deletions
diff --git a/packages/subproviders/CHANGELOG.json b/packages/subproviders/CHANGELOG.json index 5287ee987..9166c93d5 100644 --- a/packages/subproviders/CHANGELOG.json +++ b/packages/subproviders/CHANGELOG.json @@ -3,7 +3,12 @@ "version": "0.9.0", "changes": [ { - "note": "Add private key subprovider and refactor shared functionality into a base wallet subprovider.", + "note": + "Refactor RedundantRPCSubprovider into RedundantSubprovider where it now accepts an array of subproviders rather then an array of RPC endpoints", + "pr": 500 + }, + { + "note": "Add private key subprovider and refactor shared functionality into a base wallet subprovider", "pr": 506 }, { @@ -15,7 +20,8 @@ "Support multiple addresses in ledger and mnemonic wallets. Renamed derivationPath to baseDerivationPath.", "pr": 507 } - ] + ], + "timestamp": 1523462196 }, { "timestamp": 1522673609, diff --git a/packages/subproviders/CHANGELOG.md b/packages/subproviders/CHANGELOG.md index 79711e7b7..85cd420dc 100644 --- a/packages/subproviders/CHANGELOG.md +++ b/packages/subproviders/CHANGELOG.md @@ -5,6 +5,10 @@ Edit the package's CHANGELOG.json file only. CHANGELOG +## v0.8.5 - _April 11, 2018_ + + * Add private key subprovider and refactor shared functionality into a base wallet subprovider (#506) + ## v0.8.4 - _April 2, 2018_ * Dependencies updated diff --git a/packages/subproviders/README.md b/packages/subproviders/README.md index 2936be545..8c8807895 100644 --- a/packages/subproviders/README.md +++ b/packages/subproviders/README.md @@ -4,7 +4,7 @@ A few useful web3 subproviders including a LedgerSubprovider useful for adding L We have written up a [Wiki](https://0xproject.com/wiki#Web3-Provider-Examples) article detailing some use cases of this subprovider package. -### Read the [Documentation](0xproject.com/docs/subproviders). +### Read the [Documentation](https://0xproject.com/docs/subproviders). ## Installation diff --git a/packages/subproviders/package.json b/packages/subproviders/package.json index afb6ef5d6..c27b1d2fc 100644 --- a/packages/subproviders/package.json +++ b/packages/subproviders/package.json @@ -1,6 +1,6 @@ { "name": "@0xproject/subproviders", - "version": "0.8.4", + "version": "0.8.5", "main": "lib/src/index.js", "types": "lib/src/index.d.ts", "license": "Apache-2.0", @@ -35,10 +35,10 @@ } }, "dependencies": { - "@0xproject/assert": "^0.2.5", - "@0xproject/types": "^0.5.0", - "@0xproject/typescript-typings": "^0.0.3", - "@0xproject/utils": "^0.5.0", + "@0xproject/assert": "^0.2.6", + "@0xproject/types": "^0.6.0", + "@0xproject/typescript-typings": "^0.1.0", + "@0xproject/utils": "^0.5.1", "@ledgerhq/hw-app-eth": "^4.3.0", "@ledgerhq/hw-transport-u2f": "^4.3.0", "bn.js": "^4.11.8", @@ -53,9 +53,9 @@ "web3-provider-engine": "^13.0.1" }, "devDependencies": { - "@0xproject/monorepo-scripts": "^0.1.16", - "@0xproject/tslint-config": "^0.4.14", - "@0xproject/utils": "^0.5.0", + "@0xproject/monorepo-scripts": "^0.1.17", + "@0xproject/tslint-config": "^0.4.15", + "@0xproject/utils": "^0.5.1", "@types/bip39": "^2.4.0", "@types/lodash": "4.14.104", "@types/mocha": "^2.2.42", diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts index dc45ea9f1..ff28b8a8d 100644 --- a/packages/subproviders/src/index.ts +++ b/packages/subproviders/src/index.ts @@ -7,7 +7,7 @@ import { LedgerEthereumClient } from './types'; export { EmptyWalletSubprovider } from './subproviders/empty_wallet_subprovider'; export { FakeGasEstimateSubprovider } from './subproviders/fake_gas_estimate_subprovider'; export { InjectedWeb3Subprovider } from './subproviders/injected_web3'; -export { RedundantRPCSubprovider } from './subproviders/redundant_rpc'; +export { RedundantSubprovider } from './subproviders/redundant_subprovider'; export { LedgerSubprovider } from './subproviders/ledger'; export { GanacheSubprovider } from './subproviders/ganache'; export { Subprovider } from './subproviders/subprovider'; diff --git a/packages/subproviders/src/subproviders/empty_wallet_subprovider.ts b/packages/subproviders/src/subproviders/empty_wallet_subprovider.ts index 595ec654d..8f152dc0a 100644 --- a/packages/subproviders/src/subproviders/empty_wallet_subprovider.ts +++ b/packages/subproviders/src/subproviders/empty_wallet_subprovider.ts @@ -17,8 +17,8 @@ export class EmptyWalletSubprovider extends Subprovider { * @param next Callback to call if this subprovider decides not to handle the request * @param end Callback to call if subprovider handled the request and wants to pass back the request. */ - // tslint:disable-next-line:prefer-function-over-method - public handleRequest(payload: JSONRPCRequestPayload, next: Callback, end: ErrorCallback) { + // tslint:disable-next-line:prefer-function-over-method async-suffix + public async handleRequest(payload: JSONRPCRequestPayload, next: Callback, end: ErrorCallback) { switch (payload.method) { case 'eth_accounts': end(null, []); diff --git a/packages/subproviders/src/subproviders/fake_gas_estimate_subprovider.ts b/packages/subproviders/src/subproviders/fake_gas_estimate_subprovider.ts index 966c37d8a..54fd7bcb9 100644 --- a/packages/subproviders/src/subproviders/fake_gas_estimate_subprovider.ts +++ b/packages/subproviders/src/subproviders/fake_gas_estimate_subprovider.ts @@ -31,8 +31,8 @@ export class FakeGasEstimateSubprovider extends Subprovider { * @param next Callback to call if this subprovider decides not to handle the request * @param end Callback to call if subprovider handled the request and wants to pass back the request. */ - // tslint:disable-next-line:prefer-function-over-method - public handleRequest(payload: JSONRPCRequestPayload, next: Callback, end: ErrorCallback) { + // tslint:disable-next-line:prefer-function-over-method async-suffix + public async handleRequest(payload: JSONRPCRequestPayload, next: Callback, end: ErrorCallback) { switch (payload.method) { case 'eth_estimateGas': end(null, this._constantGasAmount); diff --git a/packages/subproviders/src/subproviders/ganache.ts b/packages/subproviders/src/subproviders/ganache.ts index 8008d1b4b..debd7ecf0 100644 --- a/packages/subproviders/src/subproviders/ganache.ts +++ b/packages/subproviders/src/subproviders/ganache.ts @@ -27,8 +27,8 @@ export class GanacheSubprovider extends Subprovider { * @param next Callback to call if this subprovider decides not to handle the request * @param end Callback to call if subprovider handled the request and wants to pass back the request. */ - // tslint:disable-next-line:prefer-function-over-method - public handleRequest(payload: JSONRPCRequestPayload, next: Callback, end: ErrorCallback) { + // tslint:disable-next-line:prefer-function-over-method async-suffix + public async handleRequest(payload: JSONRPCRequestPayload, next: Callback, end: ErrorCallback) { this._ganacheProvider.sendAsync(payload, (err: Error | null, result: any) => { end(err, result && result.result); }); diff --git a/packages/subproviders/src/subproviders/injected_web3.ts b/packages/subproviders/src/subproviders/injected_web3.ts index 1d7b2ddfe..3ca0c4bd0 100644 --- a/packages/subproviders/src/subproviders/injected_web3.ts +++ b/packages/subproviders/src/subproviders/injected_web3.ts @@ -30,8 +30,8 @@ export class InjectedWeb3Subprovider extends Subprovider { * @param next Callback to call if this subprovider decides not to handle the request * @param end Callback to call if subprovider handled the request and wants to pass back the request. */ - // tslint:disable-next-line:prefer-function-over-method - public handleRequest(payload: JSONRPCRequestPayload, next: Callback, end: ErrorCallback) { + // tslint:disable-next-line:prefer-function-over-method async-suffix + public async handleRequest(payload: JSONRPCRequestPayload, next: Callback, end: ErrorCallback) { switch (payload.method) { case 'web3_clientVersion': this._injectedWeb3.version.getNode(end); diff --git a/packages/subproviders/src/subproviders/redundant_rpc.ts b/packages/subproviders/src/subproviders/redundant_subprovider.ts index f8ff0915d..37c8bba5a 100644 --- a/packages/subproviders/src/subproviders/redundant_rpc.ts +++ b/packages/subproviders/src/subproviders/redundant_subprovider.ts @@ -1,7 +1,6 @@ import { JSONRPCRequestPayload } from '@0xproject/types'; import { promisify } from '@0xproject/utils'; import * as _ from 'lodash'; -import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); import { Callback } from '../types'; @@ -12,17 +11,17 @@ import { Subprovider } from './subprovider'; * It attempts to handle each JSON RPC request by sequentially attempting to receive a valid response from one of a * set of JSON RPC endpoints. */ -export class RedundantRPCSubprovider extends Subprovider { - private _rpcs: RpcSubprovider[]; +export class RedundantSubprovider extends Subprovider { + private _subproviders: Subprovider[]; private static async _firstSuccessAsync( - rpcs: RpcSubprovider[], + subproviders: Subprovider[], payload: JSONRPCRequestPayload, next: Callback, ): Promise<any> { let lastErr: Error | undefined; - for (const rpc of rpcs) { + for (const subprovider of subproviders) { try { - const data = await promisify(rpc.handleRequest.bind(rpc))(payload, next); + const data = await promisify(subprovider.handleRequest.bind(subprovider))(payload, next); return data; } catch (err) { lastErr = err; @@ -34,16 +33,12 @@ export class RedundantRPCSubprovider extends Subprovider { } } /** - * Instantiates a new RedundantRPCSubprovider + * Instantiates a new RedundantSubprovider * @param endpoints JSON RPC endpoints to attempt. Attempts are made in the order of the endpoints. */ - constructor(endpoints: string[]) { + constructor(subproviders: Subprovider[]) { super(); - this._rpcs = _.map(endpoints, endpoint => { - return new RpcSubprovider({ - rpcUrl: endpoint, - }); - }); + this._subproviders = subproviders; } /** * This method conforms to the web3-provider-engine interface. @@ -59,9 +54,9 @@ export class RedundantRPCSubprovider extends Subprovider { next: Callback, end: (err: Error | null, data?: any) => void, ): Promise<void> { - const rpcsCopy = this._rpcs.slice(); + const subprovidersCopy = this._subproviders.slice(); try { - const data = await RedundantRPCSubprovider._firstSuccessAsync(rpcsCopy, payload, next); + const data = await RedundantSubprovider._firstSuccessAsync(subprovidersCopy, payload, next); end(null, data); } catch (err) { end(err); diff --git a/packages/subproviders/src/subproviders/subprovider.ts b/packages/subproviders/src/subproviders/subprovider.ts index c51462f07..2118f52c8 100644 --- a/packages/subproviders/src/subproviders/subprovider.ts +++ b/packages/subproviders/src/subproviders/subprovider.ts @@ -2,13 +2,14 @@ import { JSONRPCRequestPayload, JSONRPCResponsePayload } from '@0xproject/types' import { promisify } from '@0xproject/utils'; import * as Web3 from 'web3'; -import { JSONRPCRequestPayloadWithMethod } from '../types'; +import { Callback, ErrorCallback, JSONRPCRequestPayloadWithMethod } from '../types'; /** * A altered version of the base class Subprovider found in [web3-provider-engine](https://github.com/MetaMask/provider-engine). * This one has an async/await `emitPayloadAsync` and also defined types. */ -export class Subprovider { - private _engine: any; +export abstract class Subprovider { + // tslint:disable-next-line:underscore-private-and-protected + private engine: any; // Ported from: https://github.com/MetaMask/provider-engine/blob/master/util/random-id.js private static _getRandomId() { const extraDigits = 3; @@ -31,6 +32,13 @@ export class Subprovider { }; return finalPayload; } + // tslint:disable-next-line:async-suffix + public abstract async handleRequest( + payload: JSONRPCRequestPayload, + next: Callback, + end: ErrorCallback, + ): Promise<void>; + /** * Emits a JSON RPC payload that will then be handled by the ProviderEngine instance * this subprovider is a part of. The payload will cascade down the subprovider middleware @@ -40,7 +48,7 @@ export class Subprovider { */ public async emitPayloadAsync(payload: Partial<JSONRPCRequestPayloadWithMethod>): Promise<JSONRPCResponsePayload> { const finalPayload = Subprovider._createFinalPayload(payload); - const response = await promisify<JSONRPCResponsePayload>(this._engine.sendAsync, this._engine)(finalPayload); + const response = await promisify<JSONRPCResponsePayload>(this.engine.sendAsync, this.engine)(finalPayload); return response; } /** @@ -49,6 +57,6 @@ export class Subprovider { * directly. */ public setEngine(engine: any): void { - this._engine = engine; + this.engine = engine; } } diff --git a/packages/subproviders/test/integration/ledger_subprovider_test.ts b/packages/subproviders/test/integration/ledger_subprovider_test.ts index 11b5f6410..0d6e67bd1 100644 --- a/packages/subproviders/test/integration/ledger_subprovider_test.ts +++ b/packages/subproviders/test/integration/ledger_subprovider_test.ts @@ -1,7 +1,7 @@ import { JSONRPCResponsePayload } from '@0xproject/types'; import { promisify } from '@0xproject/utils'; import Eth from '@ledgerhq/hw-app-eth'; -// HACK: This depdency is optional and tslint skips optional depdencies +// HACK: This dependency is optional and tslint skips optional dependencies // tslint:disable-next-line:no-implicit-dependencies import TransportNodeHid from '@ledgerhq/hw-transport-node-hid'; import * as chai from 'chai'; diff --git a/packages/subproviders/test/unit/ledger_subprovider_test.ts b/packages/subproviders/test/unit/ledger_subprovider_test.ts index ad1154831..892c2acd0 100644 --- a/packages/subproviders/test/unit/ledger_subprovider_test.ts +++ b/packages/subproviders/test/unit/ledger_subprovider_test.ts @@ -4,7 +4,6 @@ import * as ethUtils from 'ethereumjs-util'; import * as _ from 'lodash'; import Web3 = require('web3'); import Web3ProviderEngine = require('web3-provider-engine'); -import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); import { LedgerSubprovider } from '../../src'; import { @@ -15,6 +14,7 @@ import { } from '../../src/types'; import { chaiSetup } from '../chai_setup'; import { fixtureData } from '../utils/fixture_data'; +import { ganacheSubprovider } from '../utils/ganache_subprovider'; import { reportCallbackErrors } from '../utils/report_callback_errors'; chaiSetup.configure(); @@ -105,10 +105,7 @@ describe('LedgerSubprovider', () => { before(() => { provider = new Web3ProviderEngine(); provider.addProvider(ledgerSubprovider); - const httpProvider = new RpcSubprovider({ - rpcUrl: 'http://localhost:8545', - }); - provider.addProvider(httpProvider); + provider.addProvider(ganacheSubprovider); provider.start(); }); describe('success cases', () => { diff --git a/packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts b/packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts index a347ab765..e25cb7eb7 100644 --- a/packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts +++ b/packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts @@ -3,21 +3,24 @@ import * as chai from 'chai'; import * as _ from 'lodash'; import Web3 = require('web3'); import Web3ProviderEngine = require('web3-provider-engine'); +import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); -import { RedundantRPCSubprovider } from '../../src'; +import { RedundantSubprovider } from '../../src'; +import { Subprovider } from '../../src/subproviders/subprovider'; import { DoneCallback } from '../../src/types'; import { chaiSetup } from '../chai_setup'; +import { ganacheSubprovider } from '../utils/ganache_subprovider'; import { reportCallbackErrors } from '../utils/report_callback_errors'; const expect = chai.expect; chaiSetup.configure(); -describe('RedundantRpcSubprovider', () => { +describe('RedundantSubprovider', () => { let provider: Web3ProviderEngine; it('succeeds when supplied a healthy endpoint', (done: DoneCallback) => { provider = new Web3ProviderEngine(); - const endpoints = ['http://localhost:8545']; - const redundantSubprovider = new RedundantRPCSubprovider(endpoints); + const subproviders = [ganacheSubprovider]; + const redundantSubprovider = new RedundantSubprovider(subproviders); provider.addProvider(redundantSubprovider); provider.start(); @@ -36,8 +39,11 @@ 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 redundantSubprovider = new RedundantRPCSubprovider(endpoints); + const nonExistentSubprovider = new RpcSubprovider({ + rpcUrl: 'http://does-not-exist:3000', + }); + const subproviders = [nonExistentSubprovider as Subprovider, ganacheSubprovider]; + const redundantSubprovider = new RedundantSubprovider(subproviders); provider.addProvider(redundantSubprovider); provider.start(); diff --git a/packages/subproviders/test/utils/configs.ts b/packages/subproviders/test/utils/configs.ts new file mode 100644 index 000000000..341037e4f --- /dev/null +++ b/packages/subproviders/test/utils/configs.ts @@ -0,0 +1,5 @@ +export const configs = { + port: 8545, + networkId: 50, + mnemonic: 'concert load couple harbor equip island argue ramp clarify fence smart topic', +}; diff --git a/packages/subproviders/test/utils/ganache_subprovider.ts b/packages/subproviders/test/utils/ganache_subprovider.ts new file mode 100644 index 000000000..ac4a9325c --- /dev/null +++ b/packages/subproviders/test/utils/ganache_subprovider.ts @@ -0,0 +1,18 @@ +import * as fs from 'fs'; + +import { GanacheSubprovider } from '../../src/subproviders/ganache'; +import { configs } from '../utils/configs'; + +const logger = { + log: (arg: any) => { + fs.appendFileSync('ganache.log', `${arg}\n`); + }, +}; + +export const ganacheSubprovider = new GanacheSubprovider({ + logger, + verbose: false, + port: configs.port, + networkId: configs.networkId, + mnemonic: configs.mnemonic, +}); |