From 22f6f35f6b01f4d49bd026fd58e4e13a459e1beb Mon Sep 17 00:00:00 2001 From: shanejonas Date: Fri, 30 Apr 2021 14:16:50 -0700 Subject: [PATCH 1/3] feat: add createMetaMaskExternalExtensionProvider - remove index.d.ts --- index.d.ts | 150 ------------------ jest.config.js | 1 + jest.setup.js | 1 + package.json | 9 +- src/BaseProvider.ts | 4 +- src/MetaMaskInpageProvider.ts | 2 +- ...createMetaMaskExternalExtensionProvider.ts | 34 ++++ src/external-extension-config.json | 4 + src/index.ts | 2 + src/initializeInpageProvider.ts | 2 +- ...eMetaMaskExternalExtensionProvider.test.js | 34 ++++ tsconfig.json | 1 + yarn.lock | 71 +++++++++ 13 files changed, 159 insertions(+), 156 deletions(-) delete mode 100644 index.d.ts create mode 100644 jest.setup.js create mode 100644 src/createMetaMaskExternalExtensionProvider.ts create mode 100644 src/external-extension-config.json create mode 100644 test/createMetaMaskExternalExtensionProvider.test.js diff --git a/index.d.ts b/index.d.ts deleted file mode 100644 index b86a63f0..00000000 --- a/index.d.ts +++ /dev/null @@ -1,150 +0,0 @@ -// You may have to bring your own Node types (e.g. @types/node) for these imports. -import { EventEmitter } from 'events'; -import { Duplex } from 'stream'; -import { JsonRpcRequest, JsonRpcResponse } from 'json-rpc-engine'; - -export interface MetaMaskInpageProviderOptions { - /** - * The name of the stream used to connect to the wallet. - */ - jsonRpcStreamName?: string; - - /** - * The logging API to use. - */ - logger?: Pick; - - /** - * The maximum number of event listeners. - */ - maxEventListeners?: number; - - /** - * Whether the provider should send page metadata. - */ - shouldSendMetadata?: boolean; -} - -export class MetaMaskInpageProvider extends EventEmitter { - /** - * @param connectionStream - A Node.js duplex stream. - * @param options - An options bag. - */ - constructor( - connectionStream: Duplex, - options?: MetaMaskInpageProviderOptions, - ); - - /** - * Returns whether the provider can process RPC requests. - */ - isConnected(): boolean; - - /** - * Submits an RPC request for the given method, with the given params. - * Resolves with the result of the method call, or rejects on error. - */ - request(args: RequestArguments): Promise; - - /** - * Submits an RPC request per the given JSON-RPC request object. - */ - sendAsync( - payload: JsonRpcRequest, - callback: (error: Error | null, result?: JsonRpcResponse) => void, - ): void; - - /** - * Submits an RPC request for the given method, with the given params. - * @deprecated Use {@link request} instead. - */ - send(method: string, params?: unknown[]): Promise>; - - /** - * Submits an RPC request per the given JSON-RPC request object. - * @deprecated Use {@link request} instead. - */ - send( - payload: JsonRpcRequest, - callback: (error: Error | null, result?: JsonRpcResponse) => void, - ): void; - - /** - * Accepts a JSON-RPC request object, and synchronously returns the cached result - * for the given method. Only supports 4 specific methods. - * @deprecated Use {@link request} instead. - */ - send(payload: SendSyncJsonRpcRequest): JsonRpcResponse; - - readonly isMetaMask: true; - - readonly selectedAddress: string | null; - - readonly networkVersion: string | null; - - readonly chainId: string | undefined; -} - -interface InitializeProviderOptions extends MetaMaskInpageProviderOptions { - /** - * The stream used to connect to the wallet. - */ - connectionStream: Duplex; - - /** - * Whether the provider should be set as window.ethereum. - */ - shouldSetOnWindow?: boolean; - - /** - * Whether the window.web3 shim should be set. - */ - shouldShimWeb3?: boolean; -} - -/** - * Initializes a MetaMaskInpageProvider and (optionally) assigns it as window.ethereum. - * - * @returns The initialized provider (whether set or not). - */ -export function initializeProvider( - options: InitializeProviderOptions, -): MetaMaskInpageProvider; - -/** - * Sets the given provider instance as window.ethereum and dispatches - * the 'ethereum#initialized' event on window. - * - * @param providerInstance - The provider instance. - */ -export function setGlobalProvider( - providerInstance: MetaMaskInpageProvider, -): void; - -/** - * If no existing window.web3 is found, this function injects a web3 "shim" to - * not break dapps that rely on window.web3.currentProvider. - * - * @param provider - The provider to set as window.web3.currentProvider. - * @param log - The logging API to use. - */ -export function shimWeb3( - provider: MetaMaskInpageProvider, - log: typeof console, -): void; - -export interface RequestArguments { - /** The RPC method to request. */ - method: string; - - /** The params of the RPC method, if any. */ - params?: unknown[] | Record; -} - -export interface SendSyncJsonRpcRequest extends JsonRpcRequest { - method: - | 'eth_accounts' - | 'eth_coinbase' - | 'eth_uninstallFilter' - | 'net_version'; -} diff --git a/jest.config.js b/jest.config.js index 8b3653cd..0a989091 100644 --- a/jest.config.js +++ b/jest.config.js @@ -10,6 +10,7 @@ module.exports = { // statements: 100, // }, // }, + setupFilesAfterEnv: ['./jest.setup.js'], moduleFileExtensions: ['js'], silent: true, testEnvironment: 'jsdom', diff --git a/jest.setup.js b/jest.setup.js new file mode 100644 index 00000000..4acf7987 --- /dev/null +++ b/jest.setup.js @@ -0,0 +1 @@ +Object.assign(global, require('jest-chrome')); diff --git a/package.json b/package.json index 58fe2145..7bead720 100644 --- a/package.json +++ b/package.json @@ -39,12 +39,16 @@ "dependencies": { "@metamask/object-multiplex": "^1.1.0", "@metamask/safe-event-emitter": "^2.0.0", + "@types/chrome": "^0.0.136", + "detect-browser": "^5.2.0", "eth-rpc-errors": "^4.0.2", + "extension-port-stream": "^2.0.1", "fast-deep-equal": "^2.0.1", "is-stream": "^2.0.0", "json-rpc-engine": "^6.1.0", "json-rpc-middleware-stream": "^3.0.0", - "pump": "^3.0.0" + "pump": "^3.0.0", + "webextension-polyfill-ts": "^0.25.0" }, "devDependencies": { "@metamask/eslint-config": "^6.0.0", @@ -58,13 +62,14 @@ "@typescript-eslint/eslint-plugin": "^4.10.0", "@typescript-eslint/parser": "^4.10.0", "eslint": "^7.15.0", + "eslint-config-prettier": "^8.1.0", "eslint-plugin-import": "^2.20.2", "eslint-plugin-jest": "^23.18.0", "eslint-plugin-json": "^2.0.1", "eslint-plugin-node": "^11.1.0", "eslint-plugin-prettier": "^3.4.0", - "eslint-config-prettier": "^8.1.0", "jest": "^26.6.3", + "jest-chrome": "^0.7.1", "prettier": "^2.2.1", "typescript": "^4.1.3" } diff --git a/src/BaseProvider.ts b/src/BaseProvider.ts index 6cab00b1..24d3aedd 100644 --- a/src/BaseProvider.ts +++ b/src/BaseProvider.ts @@ -69,7 +69,7 @@ export interface BaseProviderState { export interface JsonRpcConnection { events: SafeEventEmitter; middleware: JsonRpcMiddleware; - stream: _Readable.Duplex; + stream: Duplex; } export default class BaseProvider extends SafeEventEmitter { @@ -112,7 +112,7 @@ export default class BaseProvider extends SafeEventEmitter { * listeners. Default: 100 */ constructor( - connectionStream: typeof Duplex, + connectionStream: Duplex, { jsonRpcStreamName = 'metamask-provider', logger = console, diff --git a/src/MetaMaskInpageProvider.ts b/src/MetaMaskInpageProvider.ts index 6d3a103d..3b3e7319 100644 --- a/src/MetaMaskInpageProvider.ts +++ b/src/MetaMaskInpageProvider.ts @@ -81,7 +81,7 @@ export default class MetaMaskInpageProvider extends BaseProvider { * send page metadata. Default: true */ constructor( - connectionStream: typeof Duplex, + connectionStream: Duplex, { jsonRpcStreamName = 'metamask-provider', logger = console, diff --git a/src/createMetaMaskExternalExtensionProvider.ts b/src/createMetaMaskExternalExtensionProvider.ts new file mode 100644 index 00000000..c164fa82 --- /dev/null +++ b/src/createMetaMaskExternalExtensionProvider.ts @@ -0,0 +1,34 @@ +import PortStream from 'extension-port-stream'; +import { detect } from 'detect-browser'; +import { Runtime } from 'webextension-polyfill-ts'; +import MetaMaskInpageProvider from './MetaMaskInpageProvider'; +import config from './external-extension-config.json'; + +const browser = detect(); + +export default function createMetaMaskExternalExtensionProvider() { + let provider; + try { + const currentMetaMaskId = getMetaMaskId(); + const metamaskPort = chrome.runtime.connect( + currentMetaMaskId, + ) as Runtime.Port; + const pluginStream = new PortStream(metamaskPort); + provider = new MetaMaskInpageProvider(pluginStream); + } catch (e) { + console.dir(`Metamask connect error `, e); + throw e; + } + return provider; +} + +function getMetaMaskId() { + switch (browser?.name) { + case 'chrome': + return config.CHROME_ID; + case 'firefox': + return config.FIREFOX_ID; + default: + return config.CHROME_ID; + } +} diff --git a/src/external-extension-config.json b/src/external-extension-config.json new file mode 100644 index 00000000..e6c94246 --- /dev/null +++ b/src/external-extension-config.json @@ -0,0 +1,4 @@ +{ + "CHROME_ID": "nkbihfbeogaeaoehlefnkodbefgpgknn", + "FIREFOX_ID": "webextension@metamask.io" +} diff --git a/src/index.ts b/src/index.ts index b4937795..a6ad7bf7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ import MetaMaskInpageProvider from './MetaMaskInpageProvider'; +import createMetaMaskExternalExtensionProvider from './createMetaMaskExternalExtensionProvider'; import BaseProvider from './BaseProvider'; import { initializeProvider, @@ -12,4 +13,5 @@ export { BaseProvider, setGlobalProvider, shimWeb3, + createMetaMaskExternalExtensionProvider, }; diff --git a/src/initializeInpageProvider.ts b/src/initializeInpageProvider.ts index df9fc264..b51210a2 100644 --- a/src/initializeInpageProvider.ts +++ b/src/initializeInpageProvider.ts @@ -8,7 +8,7 @@ interface InitializeProviderOptions extends MetaMaskInpageProviderOptions { /** * The stream used to connect to the wallet. */ - connectionStream: typeof Duplex; + connectionStream: Duplex; /** * Whether the provider should be set as window.ethereum. diff --git a/test/createMetaMaskExternalExtensionProvider.test.js b/test/createMetaMaskExternalExtensionProvider.test.js new file mode 100644 index 00000000..129cfae3 --- /dev/null +++ b/test/createMetaMaskExternalExtensionProvider.test.js @@ -0,0 +1,34 @@ +const { + createMetaMaskExternalExtensionProvider, + MetaMaskInpageProvider, +} = require('../dist'); + +describe('createMetaMaskExternalExtensionProvider', () => { + beforeAll(() => { + global.chrome.runtime.connect.mockImplementation(() => { + return { + onMessage: { + addListener: jest.fn(), + }, + onDisconnect: { + addListener: jest.fn(), + }, + postMessage: jest.fn(), + }; + }); + }); + afterAll(() => { + jest.restoreAllMocks(); + }); + it('can be called and not throw', () => { + expect(() => createMetaMaskExternalExtensionProvider()).not.toThrow(); + }); + it('calls connect', () => { + createMetaMaskExternalExtensionProvider(); + expect(global.chrome.runtime.connect).toHaveBeenCalled(); + }); + it('returns a MetaMaskInpageProvider', () => { + const results = createMetaMaskExternalExtensionProvider(); + expect(results).toBeInstanceOf(MetaMaskInpageProvider); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index ab2350e7..92944010 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,7 @@ "esModuleInterop": true, "module": "CommonJS", "moduleResolution": "node", + "resolveJsonModule": true, "outDir": "dist", "sourceMap": true, "strict": true, diff --git a/yarn.lock b/yarn.lock index f5e8f07f..64ab10d2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -621,11 +621,39 @@ dependencies: "@babel/types" "^7.3.0" +"@types/chrome@^0.0.114": + version "0.0.114" + resolved "https://registry.yarnpkg.com/@types/chrome/-/chrome-0.0.114.tgz#8ceb33fa261f4b9e307fa7344ba8182d8d410d4e" + integrity sha512-i7qRr74IrxHtbnrZSKUuP5Uvd5EOKwlwJq/yp7+yTPihOXnPhNQO4Z5bqb1XTnrjdbUKEJicaVVbhcgtRijmLA== + dependencies: + "@types/filesystem" "*" + "@types/har-format" "*" + +"@types/chrome@^0.0.136": + version "0.0.136" + resolved "https://registry.yarnpkg.com/@types/chrome/-/chrome-0.0.136.tgz#7c011b9f997b0156f25a140188a0c5689d3f368f" + integrity sha512-XDEiRhLkMd+SB7Iw3ZUIj/fov3wLd4HyTdLltVszkgl1dBfc3Rb7oPMVZ2Mz2TLqnF7Ow+StbR8E7r9lqpb4DA== + dependencies: + "@types/filesystem" "*" + "@types/har-format" "*" + "@types/color-name@^1.1.1": version "1.1.1" resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0" integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ== +"@types/filesystem@*": + version "0.0.30" + resolved "https://registry.yarnpkg.com/@types/filesystem/-/filesystem-0.0.30.tgz#a7373a2edf34d13e298baf7ee1101f738b2efb7e" + integrity sha512-NCoRgmGmLpTT9VFL6Bb6z0jQuqI3d0E5FGl7M0JOv/J5RQYo9s5aOItPYnpckx9MbYQk1APLXcF8f20Vqnf2yA== + dependencies: + "@types/filewriter" "*" + +"@types/filewriter@*": + version "0.0.29" + resolved "https://registry.yarnpkg.com/@types/filewriter/-/filewriter-0.0.29.tgz#a48795ecadf957f6c0d10e0c34af86c098fa5bee" + integrity sha512-BsPXH/irW0ht0Ji6iw/jJaK8Lj3FJemon2gvEqHKpCdDCeemHa+rI3WBGq5z7cDMZgoLjY40oninGxqk+8NzNQ== + "@types/graceful-fs@^4.1.2": version "4.1.3" resolved "https://registry.yarnpkg.com/@types/graceful-fs/-/graceful-fs-4.1.3.tgz#039af35fe26bec35003e8d86d2ee9c586354348f" @@ -633,6 +661,11 @@ dependencies: "@types/node" "*" +"@types/har-format@*": + version "1.2.5" + resolved "https://registry.yarnpkg.com/@types/har-format/-/har-format-1.2.5.tgz#4f6648814d0fdcb6a510e3364a9db439a753c4b1" + integrity sha512-IG8AE1m2pWtPqQ7wXhFhy6Q59bwwnLwO36v5Rit2FrbXCIp8Sk8E2PfUCreyrdo17STwFSKDAkitVuVYbpEHvQ== + "@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1": version "2.0.3" resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.3.tgz#4ba8ddb720221f432e443bd5f9117fd22cfd4762" @@ -1467,6 +1500,11 @@ delayed-stream@~1.0.0: resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" integrity sha1-3zrhmayt+31ECqrgsp4icrJOxhk= +detect-browser@^5.2.0: + version "5.2.0" + resolved "https://registry.yarnpkg.com/detect-browser/-/detect-browser-5.2.0.tgz#c9cd5afa96a6a19fda0bbe9e9be48a6b6e1e9c97" + integrity sha512-tr7XntDAu50BVENgQfajMLzacmSe34D+qZc4zjnniz0ZVuw/TZcLcyxHQjYpJTM36sGEkZZlYLnIM1hH7alTMA== + detect-newline@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651" @@ -1912,6 +1950,13 @@ extend@~3.0.2: resolved "https://registry.yarnpkg.com/extend/-/extend-3.0.2.tgz#f8b1136b4071fbd8eb140aff858b1019ec2915fa" integrity sha512-fjquC59cD7CyW6urNXK0FBufkZcoiGG80wTuPujX590cB5Ttln20E2UB4S/WARVqhXffZl2LNgS+gQdPIIim/g== +extension-port-stream@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/extension-port-stream/-/extension-port-stream-2.0.1.tgz#d374820c581418c2275d3c4439ade0b82c4cfac6" + integrity sha512-ltrv4Dh/979I04+D4Te6TFygfRSOc5EBzzlHRldWMS8v73V80qWluxH88hqF0qyUsBXTb8NmzlmSipcre6a+rg== + dependencies: + webextension-polyfill-ts "^0.22.0" + extglob@^2.0.4: version "2.0.4" resolved "https://registry.yarnpkg.com/extglob/-/extglob-2.0.4.tgz#ad00fe4dc612a9232e8718711dc5cb5ab0285543" @@ -2621,6 +2666,13 @@ jest-changed-files@^26.6.2: execa "^4.0.0" throat "^5.0.0" +jest-chrome@^0.7.1: + version "0.7.1" + resolved "https://registry.yarnpkg.com/jest-chrome/-/jest-chrome-0.7.1.tgz#72dd7cc0367e6b24f17974fb78f59665dafd0a10" + integrity sha512-rUsDoOIxvZr4JpzyYWepE5/l0xM5VO6010g93jhbaeYwJaJGpazYIClbLtbeWg+zUxMJvLdN3iDMvT6m8wNMwA== + dependencies: + "@types/chrome" "^0.0.114" + jest-cli@^26.6.3: version "26.6.3" resolved "https://registry.yarnpkg.com/jest-cli/-/jest-cli-26.6.3.tgz#43117cfef24bc4cd691a174a8796a532e135e92a" @@ -4684,6 +4736,25 @@ walker@^1.0.7, walker@~1.0.5: dependencies: makeerror "1.0.x" +webextension-polyfill-ts@^0.22.0: + version "0.22.0" + resolved "https://registry.yarnpkg.com/webextension-polyfill-ts/-/webextension-polyfill-ts-0.22.0.tgz#86cfd7bab4d9d779d98c8340983f4b691b2343f3" + integrity sha512-3P33ClMwZ/qiAT7UH1ROrkRC1KM78umlnPpRhdC/292UyoTTW9NcjJEqDsv83HbibcTB6qCtpVeuB2q2/oniHQ== + dependencies: + webextension-polyfill "^0.7.0" + +webextension-polyfill-ts@^0.25.0: + version "0.25.0" + resolved "https://registry.yarnpkg.com/webextension-polyfill-ts/-/webextension-polyfill-ts-0.25.0.tgz#fff041626365dbd0e29c40b197e989a55ec221ca" + integrity sha512-ikQhwwHYkpBu00pFaUzIKY26I6L87DeRI+Q6jBT1daZUNuu8dSrg5U9l/ZbqdaQ1M/TTSPKeAa3kolP5liuedw== + dependencies: + webextension-polyfill "^0.7.0" + +webextension-polyfill@^0.7.0: + version "0.7.0" + resolved "https://registry.yarnpkg.com/webextension-polyfill/-/webextension-polyfill-0.7.0.tgz#0df1120ff0266056319ce1a622b09ad8d4a56505" + integrity sha512-su48BkMLxqzTTvPSE1eWxKToPS2Tv5DLGxKexLEVpwFd6Po6N8hhSLIvG6acPAg7qERoEaDL+Y5HQJeJeml5Aw== + webidl-conversions@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-5.0.0.tgz#ae59c8a00b121543a2acc65c0434f57b0fc11aff" From 82eaee5824671be7e1fe1cd3e355637129ef450b Mon Sep 17 00:00:00 2001 From: shanejonas Date: Mon, 3 May 2021 13:46:37 -0700 Subject: [PATCH 2/3] break out extension code to src/extension-provider --- .../createMetaMaskExternalExtensionProvider.ts | 2 +- src/{ => extension-provider}/external-extension-config.json | 0 src/index.ts | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename src/{ => extension-provider}/createMetaMaskExternalExtensionProvider.ts (93%) rename src/{ => extension-provider}/external-extension-config.json (100%) diff --git a/src/createMetaMaskExternalExtensionProvider.ts b/src/extension-provider/createMetaMaskExternalExtensionProvider.ts similarity index 93% rename from src/createMetaMaskExternalExtensionProvider.ts rename to src/extension-provider/createMetaMaskExternalExtensionProvider.ts index c164fa82..d7f957b6 100644 --- a/src/createMetaMaskExternalExtensionProvider.ts +++ b/src/extension-provider/createMetaMaskExternalExtensionProvider.ts @@ -1,7 +1,7 @@ import PortStream from 'extension-port-stream'; import { detect } from 'detect-browser'; import { Runtime } from 'webextension-polyfill-ts'; -import MetaMaskInpageProvider from './MetaMaskInpageProvider'; +import MetaMaskInpageProvider from '../MetaMaskInpageProvider'; import config from './external-extension-config.json'; const browser = detect(); diff --git a/src/external-extension-config.json b/src/extension-provider/external-extension-config.json similarity index 100% rename from src/external-extension-config.json rename to src/extension-provider/external-extension-config.json diff --git a/src/index.ts b/src/index.ts index a6ad7bf7..2a076485 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import MetaMaskInpageProvider from './MetaMaskInpageProvider'; -import createMetaMaskExternalExtensionProvider from './createMetaMaskExternalExtensionProvider'; +import createMetaMaskExternalExtensionProvider from './extension-provider/createMetaMaskExternalExtensionProvider'; import BaseProvider from './BaseProvider'; import { initializeProvider, From 940d4e1822b6ac20a427551bb7ca251ee3893093 Mon Sep 17 00:00:00 2001 From: shanejonas Date: Mon, 3 May 2021 16:15:54 -0700 Subject: [PATCH 3/3] use BaseProvider for external provider --- ...ider.ts => createExternalExtensionProvider.ts} | 4 ++-- src/index.ts | 4 ++-- ...js => createExternalExtensionProvider.test.js} | 15 ++++++--------- 3 files changed, 10 insertions(+), 13 deletions(-) rename src/extension-provider/{createMetaMaskExternalExtensionProvider.ts => createExternalExtensionProvider.ts} (87%) rename test/{createMetaMaskExternalExtensionProvider.test.js => createExternalExtensionProvider.test.js} (57%) diff --git a/src/extension-provider/createMetaMaskExternalExtensionProvider.ts b/src/extension-provider/createExternalExtensionProvider.ts similarity index 87% rename from src/extension-provider/createMetaMaskExternalExtensionProvider.ts rename to src/extension-provider/createExternalExtensionProvider.ts index d7f957b6..a61ab526 100644 --- a/src/extension-provider/createMetaMaskExternalExtensionProvider.ts +++ b/src/extension-provider/createExternalExtensionProvider.ts @@ -1,7 +1,7 @@ import PortStream from 'extension-port-stream'; import { detect } from 'detect-browser'; import { Runtime } from 'webextension-polyfill-ts'; -import MetaMaskInpageProvider from '../MetaMaskInpageProvider'; +import BaseProvider from '../BaseProvider'; import config from './external-extension-config.json'; const browser = detect(); @@ -14,7 +14,7 @@ export default function createMetaMaskExternalExtensionProvider() { currentMetaMaskId, ) as Runtime.Port; const pluginStream = new PortStream(metamaskPort); - provider = new MetaMaskInpageProvider(pluginStream); + provider = new BaseProvider(pluginStream); } catch (e) { console.dir(`Metamask connect error `, e); throw e; diff --git a/src/index.ts b/src/index.ts index 2a076485..550b0cb2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ import MetaMaskInpageProvider from './MetaMaskInpageProvider'; -import createMetaMaskExternalExtensionProvider from './extension-provider/createMetaMaskExternalExtensionProvider'; +import createExternalExtensionProvider from './extension-provider/createExternalExtensionProvider'; import BaseProvider from './BaseProvider'; import { initializeProvider, @@ -13,5 +13,5 @@ export { BaseProvider, setGlobalProvider, shimWeb3, - createMetaMaskExternalExtensionProvider, + createExternalExtensionProvider, }; diff --git a/test/createMetaMaskExternalExtensionProvider.test.js b/test/createExternalExtensionProvider.test.js similarity index 57% rename from test/createMetaMaskExternalExtensionProvider.test.js rename to test/createExternalExtensionProvider.test.js index 129cfae3..1e685d52 100644 --- a/test/createMetaMaskExternalExtensionProvider.test.js +++ b/test/createExternalExtensionProvider.test.js @@ -1,9 +1,6 @@ -const { - createMetaMaskExternalExtensionProvider, - MetaMaskInpageProvider, -} = require('../dist'); +const { createExternalExtensionProvider, BaseProvider } = require('../dist'); -describe('createMetaMaskExternalExtensionProvider', () => { +describe('createExternalExtensionProvider', () => { beforeAll(() => { global.chrome.runtime.connect.mockImplementation(() => { return { @@ -21,14 +18,14 @@ describe('createMetaMaskExternalExtensionProvider', () => { jest.restoreAllMocks(); }); it('can be called and not throw', () => { - expect(() => createMetaMaskExternalExtensionProvider()).not.toThrow(); + expect(() => createExternalExtensionProvider()).not.toThrow(); }); it('calls connect', () => { - createMetaMaskExternalExtensionProvider(); + createExternalExtensionProvider(); expect(global.chrome.runtime.connect).toHaveBeenCalled(); }); it('returns a MetaMaskInpageProvider', () => { - const results = createMetaMaskExternalExtensionProvider(); - expect(results).toBeInstanceOf(MetaMaskInpageProvider); + const results = createExternalExtensionProvider(); + expect(results).toBeInstanceOf(BaseProvider); }); });