aboutsummaryrefslogtreecommitdiffstats
path: root/packages/subproviders/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/subproviders/src')
-rw-r--r--packages/subproviders/src/index.ts2
-rw-r--r--packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts4
-rw-r--r--packages/subproviders/src/subproviders/fake_gas_estimate_subprovider.ts2
-rw-r--r--packages/subproviders/src/subproviders/ganache.ts2
-rw-r--r--packages/subproviders/src/subproviders/ledger.ts12
-rw-r--r--packages/subproviders/src/subproviders/mnemonic_wallet.ts4
-rw-r--r--packages/subproviders/src/subproviders/nonce_tracker.ts2
-rw-r--r--packages/subproviders/src/subproviders/private_key_wallet.ts4
-rw-r--r--packages/subproviders/src/subproviders/redundant_subprovider.ts2
-rw-r--r--packages/subproviders/src/subproviders/rpc_subprovider.ts90
-rw-r--r--packages/subproviders/src/subproviders/signer.ts2
-rw-r--r--packages/subproviders/src/subproviders/subprovider.ts24
-rw-r--r--packages/subproviders/src/utils/subprovider_utils.ts4
-rw-r--r--packages/subproviders/src/utils/wallet_utils.ts4
14 files changed, 125 insertions, 33 deletions
diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts
index 71d643f14..9d4480e59 100644
--- a/packages/subproviders/src/index.ts
+++ b/packages/subproviders/src/index.ts
@@ -1,5 +1,6 @@
import Eth from '@ledgerhq/hw-app-eth';
import TransportU2F from '@ledgerhq/hw-transport-u2f';
+export import Web3ProviderEngine = require('web3-provider-engine');
export { ECSignature } from '@0xproject/types';
import { LedgerEthereumClient } from './types';
@@ -10,6 +11,7 @@ export { FakeGasEstimateSubprovider } from './subproviders/fake_gas_estimate_sub
export { SignerSubprovider } from './subproviders/signer';
export { RedundantSubprovider } from './subproviders/redundant_subprovider';
export { LedgerSubprovider } from './subproviders/ledger';
+export { RPCSubprovider } from './subproviders/rpc_subprovider';
export { GanacheSubprovider } from './subproviders/ganache';
export { Subprovider } from './subproviders/subprovider';
export { NonceTrackerSubprovider } from './subproviders/nonce_tracker';
diff --git a/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts
index a9ebbb790..454dae58e 100644
--- a/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts
+++ b/packages/subproviders/src/subproviders/eth_lightwallet_subprovider.ts
@@ -12,8 +12,8 @@ import { PrivateKeyWalletSubprovider } from './private_key_wallet';
* Source: https://github.com/MetaMask/provider-engine/blob/master/subproviders/subprovider.js
*/
export class EthLightwalletSubprovider extends BaseWalletSubprovider {
- private _keystore: lightwallet.keystore;
- private _pwDerivedKey: Uint8Array;
+ private readonly _keystore: lightwallet.keystore;
+ private readonly _pwDerivedKey: Uint8Array;
constructor(keystore: lightwallet.keystore, pwDerivedKey: Uint8Array) {
super();
this._keystore = keystore;
diff --git a/packages/subproviders/src/subproviders/fake_gas_estimate_subprovider.ts b/packages/subproviders/src/subproviders/fake_gas_estimate_subprovider.ts
index d750505e7..bc1b34aba 100644
--- a/packages/subproviders/src/subproviders/fake_gas_estimate_subprovider.ts
+++ b/packages/subproviders/src/subproviders/fake_gas_estimate_subprovider.ts
@@ -14,7 +14,7 @@ import { Subprovider } from './subprovider';
* It intercepts the `eth_estimateGas` JSON RPC call and always returns a constant gas amount when queried.
*/
export class FakeGasEstimateSubprovider extends Subprovider {
- private _constantGasAmount: number;
+ private readonly _constantGasAmount: number;
/**
* Instantiates an instance of the FakeGasEstimateSubprovider
* @param constantGasAmount The constant gas amount you want returned
diff --git a/packages/subproviders/src/subproviders/ganache.ts b/packages/subproviders/src/subproviders/ganache.ts
index 79c43bc68..986094dba 100644
--- a/packages/subproviders/src/subproviders/ganache.ts
+++ b/packages/subproviders/src/subproviders/ganache.ts
@@ -10,7 +10,7 @@ import { Subprovider } from './subprovider';
* It intercepts all JSON RPC requests and relays them to an in-process ganache instance.
*/
export class GanacheSubprovider extends Subprovider {
- private _ganacheProvider: Provider;
+ private readonly _ganacheProvider: Provider;
/**
* Instantiates a GanacheSubprovider
* @param opts The desired opts with which to instantiate the Ganache provider
diff --git a/packages/subproviders/src/subproviders/ledger.ts b/packages/subproviders/src/subproviders/ledger.ts
index 8345be33c..6ad5de2e2 100644
--- a/packages/subproviders/src/subproviders/ledger.ts
+++ b/packages/subproviders/src/subproviders/ledger.ts
@@ -32,14 +32,14 @@ const DEFAULT_ADDRESS_SEARCH_LIMIT = 1000;
*/
export class LedgerSubprovider extends BaseWalletSubprovider {
// tslint:disable-next-line:no-unused-variable
- private _nonceLock = new Lock();
- private _connectionLock = new Lock();
- private _networkId: number;
+ private readonly _nonceLock = new Lock();
+ private readonly _connectionLock = new Lock();
+ private readonly _networkId: number;
private _baseDerivationPath: string;
- private _ledgerEthereumClientFactoryAsync: LedgerEthereumClientFactoryAsync;
+ private readonly _ledgerEthereumClientFactoryAsync: LedgerEthereumClientFactoryAsync;
private _ledgerClientIfExists?: LedgerEthereumClient;
- private _shouldAlwaysAskForConfirmation: boolean;
- private _addressSearchLimit: number;
+ private readonly _shouldAlwaysAskForConfirmation: boolean;
+ private readonly _addressSearchLimit: number;
/**
* Instantiates a LedgerSubprovider. Defaults to derivationPath set to `44'/60'/0'`.
* TestRPC/Ganache defaults to `m/44'/60'/0'/0`, so set this in the configs if desired.
diff --git a/packages/subproviders/src/subproviders/mnemonic_wallet.ts b/packages/subproviders/src/subproviders/mnemonic_wallet.ts
index 596300425..1495112b6 100644
--- a/packages/subproviders/src/subproviders/mnemonic_wallet.ts
+++ b/packages/subproviders/src/subproviders/mnemonic_wallet.ts
@@ -20,10 +20,10 @@ const DEFAULT_ADDRESS_SEARCH_LIMIT = 1000;
* all requests with accounts derived from the supplied mnemonic.
*/
export class MnemonicWalletSubprovider extends BaseWalletSubprovider {
- private _addressSearchLimit: number;
+ private readonly _addressSearchLimit: number;
private _baseDerivationPath: string;
private _derivedKeyInfo: DerivedHDKeyInfo;
- private _mnemonic: string;
+ private readonly _mnemonic: string;
/**
* Instantiates a MnemonicWalletSubprovider. Defaults to baseDerivationPath set to `44'/60'/0'/0`.
diff --git a/packages/subproviders/src/subproviders/nonce_tracker.ts b/packages/subproviders/src/subproviders/nonce_tracker.ts
index dcc421574..eea722aee 100644
--- a/packages/subproviders/src/subproviders/nonce_tracker.ts
+++ b/packages/subproviders/src/subproviders/nonce_tracker.ts
@@ -17,7 +17,7 @@ const NONCE_TOO_LOW_ERROR_MESSAGE = 'Transaction nonce is too low';
* We added the additional feature of clearing the cached nonce value when a `nonce value too low` error occurs.
*/
export class NonceTrackerSubprovider extends Subprovider {
- private _nonceCache: { [address: string]: string } = {};
+ private readonly _nonceCache: { [address: string]: string } = {};
private static _reconstructTransaction(payload: JSONRPCRequestPayload): EthereumTx {
const raw = payload.params[0];
if (_.isUndefined(raw)) {
diff --git a/packages/subproviders/src/subproviders/private_key_wallet.ts b/packages/subproviders/src/subproviders/private_key_wallet.ts
index b3f48fd90..9d6fc487e 100644
--- a/packages/subproviders/src/subproviders/private_key_wallet.ts
+++ b/packages/subproviders/src/subproviders/private_key_wallet.ts
@@ -13,8 +13,8 @@ import { BaseWalletSubprovider } from './base_wallet_subprovider';
* all requests with the supplied Ethereum private key.
*/
export class PrivateKeyWalletSubprovider extends BaseWalletSubprovider {
- private _address: string;
- private _privateKeyBuffer: Buffer;
+ private readonly _address: string;
+ private readonly _privateKeyBuffer: Buffer;
/**
* Instantiates a PrivateKeyWalletSubprovider.
* @param privateKey The corresponding private key to an Ethereum address
diff --git a/packages/subproviders/src/subproviders/redundant_subprovider.ts b/packages/subproviders/src/subproviders/redundant_subprovider.ts
index a60d6706e..7aa6226d5 100644
--- a/packages/subproviders/src/subproviders/redundant_subprovider.ts
+++ b/packages/subproviders/src/subproviders/redundant_subprovider.ts
@@ -12,7 +12,7 @@ import { Subprovider } from './subprovider';
* set of JSON RPC endpoints.
*/
export class RedundantSubprovider extends Subprovider {
- private _subproviders: Subprovider[];
+ private readonly _subproviders: Subprovider[];
private static async _firstSuccessAsync(
subproviders: Subprovider[],
payload: JSONRPCRequestPayload,
diff --git a/packages/subproviders/src/subproviders/rpc_subprovider.ts b/packages/subproviders/src/subproviders/rpc_subprovider.ts
new file mode 100644
index 000000000..d874c6f05
--- /dev/null
+++ b/packages/subproviders/src/subproviders/rpc_subprovider.ts
@@ -0,0 +1,90 @@
+import { assert } from '@0xproject/assert';
+import { StatusCodes } from '@0xproject/types';
+import { fetchAsync } from '@0xproject/utils';
+import { JSONRPCRequestPayload } from 'ethereum-types';
+import JsonRpcError = require('json-rpc-error');
+
+import { Callback, ErrorCallback } from '../types';
+
+import { Subprovider } from './subprovider';
+
+/**
+ * This class implements the [web3-provider-engine](https://github.com/MetaMask/provider-engine) subprovider interface.
+ * It forwards on JSON RPC requests to the supplied `rpcUrl` endpoint
+ */
+export class RPCSubprovider extends Subprovider {
+ private readonly _rpcUrl: string;
+ private readonly _requestTimeoutMs: number;
+ constructor(rpcUrl: string, requestTimeoutMs: number = 20000) {
+ super();
+ assert.isString('rpcUrl', rpcUrl);
+ assert.isNumber('requestTimeoutMs', requestTimeoutMs);
+ this._rpcUrl = rpcUrl;
+ this._requestTimeoutMs = requestTimeoutMs;
+ }
+ /**
+ * This method conforms to the web3-provider-engine interface.
+ * It is called internally by the ProviderEngine when it is this subproviders
+ * turn to handle a JSON RPC request.
+ * @param payload JSON RPC payload
+ * @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 async-suffix
+ public async handleRequest(payload: JSONRPCRequestPayload, _next: Callback, end: ErrorCallback): Promise<void> {
+ const finalPayload = Subprovider._createFinalPayload(payload);
+ const headers = new Headers({
+ Accept: 'application/json',
+ 'Content-Type': 'application/json',
+ });
+
+ let response;
+ try {
+ response = await fetchAsync(
+ this._rpcUrl,
+ {
+ method: 'POST',
+ headers,
+ body: JSON.stringify(finalPayload),
+ },
+ this._requestTimeoutMs,
+ );
+ } catch (err) {
+ end(new JsonRpcError.InternalError(err));
+ return;
+ }
+
+ const text = await response.text();
+ if (!response.ok) {
+ const statusCode = response.status;
+ switch (statusCode) {
+ case StatusCodes.MethodNotAllowed:
+ end(new JsonRpcError.MethodNotFound());
+ return;
+ case StatusCodes.GatewayTimeout:
+ const errMsg =
+ 'Gateway timeout. The request took too long to process. This can happen when querying logs over too wide a block range.';
+ const err = new Error(errMsg);
+ end(new JsonRpcError.InternalError(err));
+ return;
+ default:
+ end(new JsonRpcError.InternalError(text));
+ return;
+ }
+ }
+
+ let data;
+ try {
+ data = JSON.parse(text);
+ } catch (err) {
+ end(new JsonRpcError.InternalError(err));
+ return;
+ }
+
+ if (data.error) {
+ end(data.error);
+ return;
+ }
+ end(null, data.result);
+ }
+}
diff --git a/packages/subproviders/src/subproviders/signer.ts b/packages/subproviders/src/subproviders/signer.ts
index f7329e00c..d5fd86897 100644
--- a/packages/subproviders/src/subproviders/signer.ts
+++ b/packages/subproviders/src/subproviders/signer.ts
@@ -12,7 +12,7 @@ import { Subprovider } from './subprovider';
* are passed onwards for subsequent subproviders to handle.
*/
export class SignerSubprovider extends Subprovider {
- private _web3Wrapper: Web3Wrapper;
+ private readonly _web3Wrapper: Web3Wrapper;
/**
* Instantiates a new SignerSubprovider
* @param provider Web3 provider that should handle all user account related requests
diff --git a/packages/subproviders/src/subproviders/subprovider.ts b/packages/subproviders/src/subproviders/subprovider.ts
index ff8378c4e..5dc273569 100644
--- a/packages/subproviders/src/subproviders/subprovider.ts
+++ b/packages/subproviders/src/subproviders/subprovider.ts
@@ -9,18 +9,7 @@ import { Callback, ErrorCallback, JSONRPCRequestPayloadWithMethod } from '../typ
export abstract class Subprovider {
// tslint:disable-next-line:underscore-private-and-protected
private engine!: Provider;
- // Ported from: https://github.com/MetaMask/provider-engine/blob/master/util/random-id.js
- private static _getRandomId(): number {
- const extraDigits = 3;
- const baseTen = 10;
- // 13 time digits
- const datePart = new Date().getTime() * Math.pow(baseTen, extraDigits);
- // 3 random digits
- const extraPart = Math.floor(Math.random() * Math.pow(baseTen, extraDigits));
- // 16 digits
- return datePart + extraPart;
- }
- private static _createFinalPayload(
+ protected static _createFinalPayload(
payload: Partial<JSONRPCRequestPayloadWithMethod>,
): Partial<JSONRPCRequestPayloadWithMethod> {
const finalPayload = {
@@ -32,6 +21,17 @@ export abstract class Subprovider {
};
return finalPayload;
}
+ // Ported from: https://github.com/MetaMask/provider-engine/blob/master/util/random-id.js
+ private static _getRandomId(): number {
+ const extraDigits = 3;
+ const baseTen = 10;
+ // 13 time digits
+ const datePart = new Date().getTime() * Math.pow(baseTen, extraDigits);
+ // 3 random digits
+ const extraPart = Math.floor(Math.random() * Math.pow(baseTen, extraDigits));
+ // 16 digits
+ return datePart + extraPart;
+ }
// tslint:disable-next-line:async-suffix
public abstract async handleRequest(
payload: JSONRPCRequestPayload,
diff --git a/packages/subproviders/src/utils/subprovider_utils.ts b/packages/subproviders/src/utils/subprovider_utils.ts
index 24ebedd06..beda408ab 100644
--- a/packages/subproviders/src/utils/subprovider_utils.ts
+++ b/packages/subproviders/src/utils/subprovider_utils.ts
@@ -1,4 +1,4 @@
-import ProviderEngine = require('web3-provider-engine');
+import Web3ProviderEngine = require('web3-provider-engine');
import { Subprovider } from '../subproviders/subprovider';
@@ -7,7 +7,7 @@ import { Subprovider } from '../subproviders/subprovider';
* @param provider Given provider
* @param subprovider Subprovider to prepend
*/
-export function prependSubprovider(provider: ProviderEngine, subprovider: Subprovider): void {
+export function prependSubprovider(provider: Web3ProviderEngine, subprovider: Subprovider): void {
subprovider.setEngine(provider);
// HACK: We use implementation details of provider engine here
// https://github.com/MetaMask/provider-engine/blob/master/index.js#L68
diff --git a/packages/subproviders/src/utils/wallet_utils.ts b/packages/subproviders/src/utils/wallet_utils.ts
index 3748077ef..4c233645b 100644
--- a/packages/subproviders/src/utils/wallet_utils.ts
+++ b/packages/subproviders/src/utils/wallet_utils.ts
@@ -6,8 +6,8 @@ import { DerivedHDKeyInfo } from '../types';
const DEFAULT_ADDRESS_SEARCH_LIMIT = 1000;
class DerivedHDKeyInfoIterator implements IterableIterator<DerivedHDKeyInfo> {
- private _parentDerivedKeyInfo: DerivedHDKeyInfo;
- private _searchLimit: number;
+ private readonly _parentDerivedKeyInfo: DerivedHDKeyInfo;
+ private readonly _searchLimit: number;
private _index: number;
constructor(initialDerivedKey: DerivedHDKeyInfo, searchLimit: number = DEFAULT_ADDRESS_SEARCH_LIMIT) {