diff --git a/packages/web3-providers/src/resolvers/ProviderResolver.js b/packages/web3-providers/src/resolvers/ProviderResolver.js index 1bc89161304..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; @@ -76,25 +73,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; } } diff --git a/packages/web3-providers/tests/src/resolvers/ProviderResolverTest.js b/packages/web3-providers/tests/src/resolvers/ProviderResolverTest.js index 95ee156afcd..aba811e1b02 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,12 @@ 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', () => { + new MetamaskInpageProvider(); + const metamaskInpageProviderMock = MetamaskInpageProvider.mock.instances[0]; + + providersModuleFactoryMock.createMetamaskInpageProvider.mockReturnValueOnce(metamaskInpageProviderMock); + + expect(providerResolver.resolve(metamaskInpageProviderMock)).toEqual(metamaskInpageProviderMock); }); });