diff --git a/jest.config.js b/jest.config.js index 8c1d760f..7c4cdf55 100644 --- a/jest.config.js +++ b/jest.config.js @@ -20,10 +20,10 @@ module.exports = { coveragePathIgnorePatterns: ['/node_modules/', '/mocks/', '/test/'], coverageThreshold: { global: { - branches: 58.12, + branches: 59.24, functions: 54.95, - lines: 60.61, - statements: 60.84, + lines: 60.91, + statements: 61.14, }, }, projects: [ diff --git a/src/extension-provider/createExternalExtensionProvider.test.ts b/src/extension-provider/createExternalExtensionProvider.test.ts index 8156fda2..9693e5f0 100644 --- a/src/extension-provider/createExternalExtensionProvider.test.ts +++ b/src/extension-provider/createExternalExtensionProvider.test.ts @@ -4,6 +4,7 @@ import { StreamProvider } from '../StreamProvider'; import { MockPort } from '../../test/mocks/MockPort'; import messages from '../messages'; import { createExternalExtensionProvider } from './createExternalExtensionProvider'; +import config from './external-extension-config.json'; /** * A fully initialized extension provider, and additional mocks to help @@ -126,6 +127,40 @@ describe('createExternalExtensionProvider', () => { expect(results).toBeInstanceOf(StreamProvider); }); + it('supports Flask', () => { + // `global.chrome.runtime` mock setup by `jest-chrome` in `jest.setup.js` + (global.chrome.runtime.connect as any).mockImplementation(() => { + return new MockPort(); + }); + const results = createExternalExtensionProvider('flask'); + expect(results).toBeInstanceOf(StreamProvider); + expect(global.chrome.runtime.connect).toHaveBeenCalledWith( + config.chromeIds.flask, + ); + }); + + it('supports Beta', () => { + // `global.chrome.runtime` mock setup by `jest-chrome` in `jest.setup.js` + (global.chrome.runtime.connect as any).mockImplementation(() => { + return new MockPort(); + }); + const results = createExternalExtensionProvider('beta'); + expect(results).toBeInstanceOf(StreamProvider); + expect(global.chrome.runtime.connect).toHaveBeenCalledWith( + config.chromeIds.beta, + ); + }); + + it('supports custom extension ID', () => { + // `global.chrome.runtime` mock setup by `jest-chrome` in `jest.setup.js` + (global.chrome.runtime.connect as any).mockImplementation(() => { + return new MockPort(); + }); + const results = createExternalExtensionProvider('foobar'); + expect(results).toBeInstanceOf(StreamProvider); + expect(global.chrome.runtime.connect).toHaveBeenCalledWith('foobar'); + }); + describe('RPC warnings', () => { const warnings = [ { diff --git a/src/extension-provider/createExternalExtensionProvider.ts b/src/extension-provider/createExternalExtensionProvider.ts index dce82ce6..d594fd8c 100644 --- a/src/extension-provider/createExternalExtensionProvider.ts +++ b/src/extension-provider/createExternalExtensionProvider.ts @@ -8,14 +8,16 @@ import config from './external-extension-config.json'; const browser = detect(); -export function createExternalExtensionProvider() { +export type ExtensionType = 'stable' | 'flask' | 'beta' | string; + +export function createExternalExtensionProvider( + typeOrId: ExtensionType = 'stable', +) { let provider; try { - const currentMetaMaskId = getMetaMaskId(); - const metamaskPort = chrome.runtime.connect( - currentMetaMaskId, - ) as Runtime.Port; + const extensionId = getExtensionId(typeOrId); + const metamaskPort = chrome.runtime.connect(extensionId) as Runtime.Port; const pluginStream = new PortStream(metamaskPort); provider = new StreamProvider(pluginStream, { @@ -35,13 +37,8 @@ export function createExternalExtensionProvider() { return provider; } -function getMetaMaskId() { - switch (browser?.name) { - case 'chrome': - return config.CHROME_ID; - case 'firefox': - return config.FIREFOX_ID; - default: - return config.CHROME_ID; - } +function getExtensionId(typeOrId: ExtensionType) { + const ids = + browser?.name === 'firefox' ? config.firefoxIds : config.chromeIds; + return ids[typeOrId as keyof typeof ids] ?? typeOrId; } diff --git a/src/extension-provider/external-extension-config.json b/src/extension-provider/external-extension-config.json index e6c94246..c780400b 100644 --- a/src/extension-provider/external-extension-config.json +++ b/src/extension-provider/external-extension-config.json @@ -1,4 +1,12 @@ { - "CHROME_ID": "nkbihfbeogaeaoehlefnkodbefgpgknn", - "FIREFOX_ID": "webextension@metamask.io" + "chromeIds": { + "stable": "nkbihfbeogaeaoehlefnkodbefgpgknn", + "beta": "pbbkamfgmaedccnfkmjcofcecjhfgldn", + "flask": "ljfoeinjpaedjfecbmggjgodbgkmjkjk" + }, + "firefoxIds": { + "stable": "webextension@metamask.io", + "beta": "webextension-beta@metamask.io", + "flask": "webextension-flask@metamask.io" + } }