Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ProviderResolver updated for the latest EIP1193 spec #2347

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions packages/web3-providers/src/resolvers/ProviderResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ 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');
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
Expand Down Expand Up @@ -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);

Expand All @@ -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);
});
});