From 916034c6c97c77eadd1aeb0d69b7f0e8f842e668 Mon Sep 17 00:00:00 2001 From: Samuel Furter Date: Fri, 8 Feb 2019 13:12:25 +0100 Subject: [PATCH 1/3] ProviderResolver updated for la latest EIP1193 spec --- .../src/resolvers/ProviderResolver.js | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/packages/web3-providers/src/resolvers/ProviderResolver.js b/packages/web3-providers/src/resolvers/ProviderResolver.js index 1bc89161304..f80a731baff 100644 --- a/packages/web3-providers/src/resolvers/ProviderResolver.js +++ b/packages/web3-providers/src/resolvers/ProviderResolver.js @@ -76,25 +76,14 @@ export default class ProviderResolver { return this.providersModuleFactory.createMistEthereumProvider(provider); } - switch (provider.constructor.name) { - case 'EthereumProvider': - return this.providersModuleFactory.createEthereumProvider(provider); - case 'MetamaskInpageProvider': - return this.providersModuleFactory.createMetamaskInpageProvider(provider); - case 'HttpProvider': - case 'WebsocketProvider': - case 'IpcProvider': - return provider; + if (provider.constructor.name === 'MetamaskInpageProvider') { + return this.providersModuleFactory.createMetamaskInpageProvider(provider); } - if ( - provider instanceof HttpProvider || - provider instanceof WebsocketProvider || - provider instanceof IpcProvider - ) { - return provider; + if (provider.isEIP1193) { + return this.providersModuleFactory.createEthereumProvider(provider); } - throw new Error('Please provide an valid Web3 provider'); + return provider; } } From d850612a2ce9ef72726a59ddb67e3b6146e123e9 Mon Sep 17 00:00:00 2001 From: Samuel Furter Date: Fri, 8 Feb 2019 13:22:47 +0100 Subject: [PATCH 2/3] ProviderResolverTest updated --- .../tests/src/resolvers/ProviderResolverTest.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/packages/web3-providers/tests/src/resolvers/ProviderResolverTest.js b/packages/web3-providers/tests/src/resolvers/ProviderResolverTest.js index 95ee156afcd..406cf45e890 100644 --- a/packages/web3-providers/tests/src/resolvers/ProviderResolverTest.js +++ b/packages/web3-providers/tests/src/resolvers/ProviderResolverTest.js @@ -4,6 +4,7 @@ import HttpProvider from '../../../src/providers/HttpProvider'; import WebsocketProvider from '../../../src/providers/WebsocketProvider'; import IpcProvider from '../../../src/providers/IpcProvider'; import EthereumProvider from '../../../src/providers/EthereumProvider'; +import MetamaskInpageProvider from '../../../src/providers/MetamaskInpageProvider'; // Mocks jest.mock('../../../src/factories/ProvidersModuleFactory'); @@ -11,6 +12,7 @@ jest.mock('../../../src/providers/HttpProvider'); jest.mock('../../../src/providers/WebsocketProvider'); jest.mock('../../../src/providers/IpcProvider'); jest.mock('../../../src/providers/EthereumProvider'); +jest.mock('../../../src/providers/MetamaskInpageProvider'); /** * ProviderResolver test @@ -70,6 +72,7 @@ describe('ProviderResolverTest', () => { it('calls resolve with the EthereumProvider', () => { new EthereumProvider({}); const ethereumProviderMock = EthereumProvider.mock.instances[0]; + ethereumProviderMock.isEIP1193 = true; providersModuleFactoryMock.createEthereumProvider.mockReturnValueOnce(ethereumProviderMock); @@ -92,9 +95,11 @@ describe('ProviderResolverTest', () => { expect(providerResolver.resolve(ipcProviderMock)).toBeInstanceOf(IpcProvider); }); - it("calls resolve with a provider that isn't supported", () => { - expect(() => { - providerResolver.resolve('LALALA'); - }).toThrow('Please provide an valid Web3 provider'); + it('calls resolve with the MetamaskInpageProvider', () => { + const provider = {constructor: {name: 'MetamaskInpageProvider'}}; + + providersModuleFactoryMock.createMetamaskInpageProvider.mockReturnValueOnce(true) + + expect(providerResolver.resolve(provider)).toEqual(true); }); }); From 4efc7c50172cfd886e3c60a62264fcde77fb369d Mon Sep 17 00:00:00 2001 From: Samuel Furter Date: Fri, 8 Feb 2019 15:24:34 +0100 Subject: [PATCH 3/3] eslint errors fixed and ProviderResolverTest improved --- packages/web3-providers/src/resolvers/ProviderResolver.js | 3 --- .../tests/src/resolvers/ProviderResolverTest.js | 7 ++++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/web3-providers/src/resolvers/ProviderResolver.js b/packages/web3-providers/src/resolvers/ProviderResolver.js index f80a731baff..7a2241bde61 100644 --- a/packages/web3-providers/src/resolvers/ProviderResolver.js +++ b/packages/web3-providers/src/resolvers/ProviderResolver.js @@ -22,9 +22,6 @@ import isFunction from 'lodash/isFunction'; import isObject from 'lodash/isObject'; -import HttpProvider from '../providers/HttpProvider'; -import WebsocketProvider from '../providers/WebsocketProvider'; -import IpcProvider from '../providers/IpcProvider'; /* eslint-disable no-new-func */ let global; diff --git a/packages/web3-providers/tests/src/resolvers/ProviderResolverTest.js b/packages/web3-providers/tests/src/resolvers/ProviderResolverTest.js index 406cf45e890..aba811e1b02 100644 --- a/packages/web3-providers/tests/src/resolvers/ProviderResolverTest.js +++ b/packages/web3-providers/tests/src/resolvers/ProviderResolverTest.js @@ -96,10 +96,11 @@ describe('ProviderResolverTest', () => { }); it('calls resolve with the MetamaskInpageProvider', () => { - const provider = {constructor: {name: 'MetamaskInpageProvider'}}; + new MetamaskInpageProvider(); + const metamaskInpageProviderMock = MetamaskInpageProvider.mock.instances[0]; - providersModuleFactoryMock.createMetamaskInpageProvider.mockReturnValueOnce(true) + providersModuleFactoryMock.createMetamaskInpageProvider.mockReturnValueOnce(metamaskInpageProviderMock); - expect(providerResolver.resolve(provider)).toEqual(true); + expect(providerResolver.resolve(metamaskInpageProviderMock)).toEqual(metamaskInpageProviderMock); }); });