-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
31 lines (28 loc) · 848 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const { MetaMaskInpageProvider } = require('@metamask/providers')
const PortStream = require('extension-port-stream').default
const { detect } = require('detect-browser')
const browser = detect()
const config = require('./config.json')
module.exports = function createMetaMaskProvider () {
let provider
try {
let currentMetaMaskId = getMetaMaskId()
const metamaskPort = chrome.runtime.connect(currentMetaMaskId)
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 && browser.name) {
case 'chrome':
return config.CHROME_ID
case 'firefox':
return config.FIREFOX_ID
default:
return config.CHROME_ID
}
}