From d528ce757c7d07e7d2e6f2f65ad48efc85ec4e50 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 3 Jul 2018 22:58:17 +0200 Subject: Replace use of web3 w/ web3wrapper in subproviders and rename injectedWeb3 to signer subprovider for clarity --- packages/subproviders/package.json | 2 +- packages/subproviders/src/index.ts | 2 +- .../subproviders/src/subproviders/injected_web3.ts | 57 ----------------- packages/subproviders/src/subproviders/signer.ts | 74 ++++++++++++++++++++++ 4 files changed, 76 insertions(+), 59 deletions(-) delete mode 100644 packages/subproviders/src/subproviders/injected_web3.ts create mode 100644 packages/subproviders/src/subproviders/signer.ts (limited to 'packages/subproviders') diff --git a/packages/subproviders/package.json b/packages/subproviders/package.json index a3ff31f53..2d939f7b4 100644 --- a/packages/subproviders/package.json +++ b/packages/subproviders/package.json @@ -42,6 +42,7 @@ "dependencies": { "@0xproject/assert": "^0.2.12", "@0xproject/types": "^0.8.1", + "@0xproject/web3-wrapper": "^0.7.1", "@0xproject/typescript-typings": "^0.4.2", "@0xproject/utils": "^0.7.1", "@ledgerhq/hw-app-eth": "^4.3.0", @@ -55,7 +56,6 @@ "hdkey": "^0.7.1", "lodash": "^4.17.4", "semaphore-async-await": "^1.5.1", - "web3": "^0.20.0", "web3-provider-engine": "14.0.6" }, "devDependencies": { diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts index 9194c1341..8575cd07d 100644 --- a/packages/subproviders/src/index.ts +++ b/packages/subproviders/src/index.ts @@ -7,7 +7,7 @@ import { LedgerEthereumClient } from './types'; export { prependSubprovider } from './utils/subprovider_utils'; export { EmptyWalletSubprovider } from './subproviders/empty_wallet_subprovider'; export { FakeGasEstimateSubprovider } from './subproviders/fake_gas_estimate_subprovider'; -export { InjectedWeb3Subprovider } from './subproviders/injected_web3'; +export { SignerSubprovider } from './subproviders/signer'; export { RedundantSubprovider } from './subproviders/redundant_subprovider'; export { LedgerSubprovider } from './subproviders/ledger'; export { GanacheSubprovider } from './subproviders/ganache'; diff --git a/packages/subproviders/src/subproviders/injected_web3.ts b/packages/subproviders/src/subproviders/injected_web3.ts deleted file mode 100644 index 2691dec53..000000000 --- a/packages/subproviders/src/subproviders/injected_web3.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { JSONRPCRequestPayload, Provider } from 'ethereum-types'; -import * as Web3 from 'web3'; - -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 JSON RPC requests involving user accounts (getAccounts, - * sendTransaction, etc...) to the provider instance supplied at instantiation. All other requests - * are passed onwards for subsequent subproviders to handle. - */ -export class InjectedWeb3Subprovider extends Subprovider { - private _injectedWeb3: Web3; - /** - * Instantiates a new InjectedWeb3Subprovider - * @param provider Web3 provider that should handle all user account related requests - */ - constructor(provider: Provider) { - super(); - this._injectedWeb3 = new Web3(provider); - } - /** - * 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 { - switch (payload.method) { - case 'web3_clientVersion': - this._injectedWeb3.version.getNode(end); - return; - case 'eth_accounts': - this._injectedWeb3.eth.getAccounts(end); - return; - - case 'eth_sendTransaction': - const [txParams] = payload.params; - this._injectedWeb3.eth.sendTransaction(txParams, end); - return; - - case 'eth_sign': - const [address, message] = payload.params; - this._injectedWeb3.eth.sign(address, message, end); - return; - - default: - next(); - return; - } - } -} diff --git a/packages/subproviders/src/subproviders/signer.ts b/packages/subproviders/src/subproviders/signer.ts new file mode 100644 index 000000000..65b1f2635 --- /dev/null +++ b/packages/subproviders/src/subproviders/signer.ts @@ -0,0 +1,74 @@ +import { Web3Wrapper } from '@0project/web3-wrapper'; +import { JSONRPCRequestPayload, Provider } from 'ethereum-types'; + +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 JSON RPC requests involving the domain of a signer (getAccounts, + * sendTransaction, signMessage etc...) to the provider instance supplied at instantiation. All other requests + * are passed onwards for subsequent subproviders to handle. + */ +export class SignerSubprovider extends Subprovider { + private _SignerWrapper: Web3Wrapper; + /** + * Instantiates a new SignerSubprovider + * @param provider Web3 provider that should handle all user account related requests + */ + constructor(provider: Provider) { + super(); + this._SignerWrapper = new Web3Wrapper(provider); + } + /** + * 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 { + switch (payload.method) { + case 'web3_clientVersion': + try { + const nodeVersion = await this._SignerWrapper.getNodeVersionAsync(); + end(null, nodeVersion); + } catch (err) { + end(err); + } + return; + case 'eth_accounts': + try { + const accounts = await this._SignerWrapper.getAvailableAddressesAsync(); + end(null, accounts); + } catch (err) { + end(err); + } + return; + case 'eth_sendTransaction': + const [txParams] = payload.params; + try { + const txHash = await this._SignerWrapper.sendTransactionAsync(txParams); + end(null, txHash); + } catch (err) { + end(err); + } + return; + case 'eth_sign': + const [address, message] = payload.params; + try { + const signature = await this._SignerWrapper.signMessageAsync(address, message); + end(null, signature); + } catch (err) { + end(err); + } + return; + default: + next(); + return; + } + } +} -- cgit v1.2.3 From cce2fb40a130edfd5116d74b294b5e8062751ffb Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Tue, 3 Jul 2018 23:11:59 +0200 Subject: Fix import --- packages/subproviders/src/subproviders/signer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'packages/subproviders') diff --git a/packages/subproviders/src/subproviders/signer.ts b/packages/subproviders/src/subproviders/signer.ts index 65b1f2635..e436c7855 100644 --- a/packages/subproviders/src/subproviders/signer.ts +++ b/packages/subproviders/src/subproviders/signer.ts @@ -1,4 +1,4 @@ -import { Web3Wrapper } from '@0project/web3-wrapper'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; import { JSONRPCRequestPayload, Provider } from 'ethereum-types'; import { Callback, ErrorCallback } from '../types'; -- cgit v1.2.3 From 6aa4984fed55be7ba4fe0caf0f994a11bccdc155 Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Wed, 4 Jul 2018 00:22:21 +0200 Subject: Fix instance variable name --- packages/subproviders/src/subproviders/signer.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'packages/subproviders') diff --git a/packages/subproviders/src/subproviders/signer.ts b/packages/subproviders/src/subproviders/signer.ts index e436c7855..08a9daceb 100644 --- a/packages/subproviders/src/subproviders/signer.ts +++ b/packages/subproviders/src/subproviders/signer.ts @@ -12,14 +12,14 @@ import { Subprovider } from './subprovider'; * are passed onwards for subsequent subproviders to handle. */ export class SignerSubprovider extends Subprovider { - private _SignerWrapper: Web3Wrapper; + private _web3Wrapper: Web3Wrapper; /** * Instantiates a new SignerSubprovider * @param provider Web3 provider that should handle all user account related requests */ constructor(provider: Provider) { super(); - this._SignerWrapper = new Web3Wrapper(provider); + this._web3Wrapper = new Web3Wrapper(provider); } /** * This method conforms to the web3-provider-engine interface. @@ -34,7 +34,7 @@ export class SignerSubprovider extends Subprovider { switch (payload.method) { case 'web3_clientVersion': try { - const nodeVersion = await this._SignerWrapper.getNodeVersionAsync(); + const nodeVersion = await this._web3Wrapper.getNodeVersionAsync(); end(null, nodeVersion); } catch (err) { end(err); @@ -42,7 +42,7 @@ export class SignerSubprovider extends Subprovider { return; case 'eth_accounts': try { - const accounts = await this._SignerWrapper.getAvailableAddressesAsync(); + const accounts = await this._web3Wrapper.getAvailableAddressesAsync(); end(null, accounts); } catch (err) { end(err); @@ -51,7 +51,7 @@ export class SignerSubprovider extends Subprovider { case 'eth_sendTransaction': const [txParams] = payload.params; try { - const txHash = await this._SignerWrapper.sendTransactionAsync(txParams); + const txHash = await this._web3Wrapper.sendTransactionAsync(txParams); end(null, txHash); } catch (err) { end(err); @@ -60,7 +60,7 @@ export class SignerSubprovider extends Subprovider { case 'eth_sign': const [address, message] = payload.params; try { - const signature = await this._SignerWrapper.signMessageAsync(address, message); + const signature = await this._web3Wrapper.signMessageAsync(address, message); end(null, signature); } catch (err) { end(err); -- cgit v1.2.3