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/src/index.ts | 2 +- .../subproviders/src/subproviders/injected_web3.ts | 57 ----------------- packages/subproviders/src/subproviders/signer.ts | 74 ++++++++++++++++++++++ 3 files changed, 75 insertions(+), 58 deletions(-) delete mode 100644 packages/subproviders/src/subproviders/injected_web3.ts create mode 100644 packages/subproviders/src/subproviders/signer.ts (limited to 'packages/subproviders/src') 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