-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add createMetaMaskExternalExtensionProvider
- remove index.d.ts
- Loading branch information
1 parent
9c775ba
commit a872031
Showing
13 changed files
with
161 additions
and
157 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
console.log("assigning chrome global"); | ||
Object.assign(global, require('jest-chrome')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"CHROME_ID": "nkbihfbeogaeaoehlefnkodbefgpgknn", | ||
"FIREFOX_ID": "webextension@metamask.io" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.