-
-
Notifications
You must be signed in to change notification settings - Fork 128
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
feat: add createExternalExtensionProvider #152
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
22f6f35
feat: add createMetaMaskExternalExtensionProvider
shanejonas 8616333
Merge branch 'main' into add-create-external-extension-provider
rekmarks 82eaee5
break out extension code to src/extension-provider
shanejonas 940d4e1
use BaseProvider for external provider
shanejonas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ | ||
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": "[email protected]" | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to cast this to play nicely with the
webextension-polyfill-ts
type thatextension-port-stream
uses, doesnt seem to match exactly to the typechrome.runtime.connect
returns which is achrome.runtime.Port
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Port- and stream-related types seem to be incompatible 100% of the time.