2019-10-24 15:54:32 +02:00
|
|
|
|
|
|
|
// this must run before anything else
|
2020-01-09 04:34:58 +01:00
|
|
|
import './lib/freezeGlobals'
|
2019-10-24 15:54:32 +02:00
|
|
|
|
2019-09-12 22:07:27 +02:00
|
|
|
// polyfills
|
|
|
|
import 'abortcontroller-polyfill/dist/polyfill-patch-fetch'
|
2020-06-12 20:46:01 +02:00
|
|
|
import '@formatjs/intl-relativetimeformat/polyfill'
|
2019-09-12 22:07:27 +02:00
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
import PortStream from 'extension-port-stream'
|
|
|
|
import { getEnvironmentType } from './lib/util'
|
|
|
|
|
|
|
|
import {
|
|
|
|
ENVIRONMENT_TYPE_FULLSCREEN,
|
|
|
|
ENVIRONMENT_TYPE_POPUP,
|
|
|
|
} from './lib/enums'
|
|
|
|
|
|
|
|
import extension from 'extensionizer'
|
|
|
|
import ExtensionPlatform from './platforms/extension'
|
|
|
|
|
|
|
|
import setupSentry from './lib/setupSentry'
|
|
|
|
import { EventEmitter } from 'events'
|
|
|
|
import Dnode from 'dnode'
|
|
|
|
import Eth from 'ethjs'
|
|
|
|
import EthQuery from 'eth-query'
|
|
|
|
import launchMetaMaskUi from '../../ui'
|
|
|
|
import StreamProvider from 'web3-stream-provider'
|
|
|
|
import { setupMultiplex } from './lib/stream-utils.js'
|
|
|
|
import log from 'loglevel'
|
2016-01-15 03:26:54 +01:00
|
|
|
|
2018-03-15 00:31:45 +01:00
|
|
|
start().catch(log.error)
|
|
|
|
|
2018-07-03 00:49:33 +02:00
|
|
|
async function start () {
|
2018-03-15 00:31:45 +01:00
|
|
|
|
|
|
|
// create platform global
|
|
|
|
global.platform = new ExtensionPlatform()
|
|
|
|
|
|
|
|
// setup sentry error reporting
|
|
|
|
const release = global.platform.getVersion()
|
2020-07-17 17:09:38 +02:00
|
|
|
setupSentry({
|
|
|
|
release,
|
|
|
|
getState: () => window.getSentryState?.() || {},
|
|
|
|
})
|
2018-03-15 00:31:45 +01:00
|
|
|
|
|
|
|
// identify window type (popup, notification)
|
2020-01-24 20:12:58 +01:00
|
|
|
const windowType = getEnvironmentType()
|
2018-03-15 00:31:45 +01:00
|
|
|
|
|
|
|
// setup stream to background
|
|
|
|
const extensionPort = extension.runtime.connect({ name: windowType })
|
|
|
|
const connectionStream = new PortStream(extensionPort)
|
|
|
|
|
2019-08-01 15:24:33 +02:00
|
|
|
const activeTab = await queryCurrentActiveTab(windowType)
|
|
|
|
initializeUiWithTab(activeTab)
|
2016-08-17 01:46:44 +02:00
|
|
|
|
2019-08-01 15:24:33 +02:00
|
|
|
function displayCriticalError (container, err) {
|
2018-03-15 00:31:45 +01:00
|
|
|
container.innerHTML = '<div class="critical-error">The MetaMask app failed to load: please open and close MetaMask again to restart.</div>'
|
|
|
|
container.style.height = '80px'
|
|
|
|
log.error(err.stack)
|
|
|
|
throw err
|
2016-08-24 00:44:50 +02:00
|
|
|
}
|
2017-03-31 22:20:16 +02:00
|
|
|
|
2019-08-01 15:24:33 +02:00
|
|
|
function initializeUiWithTab (tab) {
|
|
|
|
const container = document.getElementById('app-content')
|
|
|
|
initializeUi(tab, container, connectionStream, (err, store) => {
|
|
|
|
if (err) {
|
|
|
|
return displayCriticalError(container, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = store.getState()
|
|
|
|
const { metamask: { completedOnboarding } = {} } = state
|
|
|
|
|
|
|
|
if (!completedOnboarding && windowType !== ENVIRONMENT_TYPE_FULLSCREEN) {
|
|
|
|
global.platform.openExtensionInBrowser()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function queryCurrentActiveTab (windowType) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
// At the time of writing we only have the `activeTab` permission which means
|
|
|
|
// that this query will only succeed in the popup context (i.e. after a "browserAction")
|
|
|
|
if (windowType !== ENVIRONMENT_TYPE_POPUP) {
|
|
|
|
resolve({})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-03 21:50:55 +01:00
|
|
|
extension.tabs.query({ active: true, currentWindow: true }, (tabs) => {
|
2019-08-01 15:24:33 +02:00
|
|
|
const [activeTab] = tabs
|
2020-07-02 23:26:45 +02:00
|
|
|
const { id, title, url } = activeTab
|
2020-06-02 01:24:27 +02:00
|
|
|
const { origin, protocol } = url ? new URL(url) : {}
|
|
|
|
|
2020-07-02 23:01:04 +02:00
|
|
|
if (!origin || origin === 'null') {
|
|
|
|
resolve({})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-02 23:26:45 +02:00
|
|
|
resolve({ id, title, origin, protocol, url })
|
2019-08-01 15:24:33 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function initializeUi (activeTab, container, connectionStream, cb) {
|
|
|
|
connectToAccountManager(connectionStream, (err, backgroundConnection) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
launchMetaMaskUi({
|
|
|
|
activeTab,
|
|
|
|
container,
|
|
|
|
backgroundConnection,
|
|
|
|
}, cb)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Establishes a connection to the background and a Web3 provider
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {PortDuplexStream} connectionStream - PortStream instance establishing a background connection
|
|
|
|
* @param {Function} cb - Called when controller connection is established
|
2019-08-01 15:24:33 +02:00
|
|
|
*/
|
|
|
|
function connectToAccountManager (connectionStream, cb) {
|
|
|
|
const mx = setupMultiplex(connectionStream)
|
|
|
|
setupControllerConnection(mx.createStream('controller'), cb)
|
|
|
|
setupWeb3Connection(mx.createStream('provider'))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Establishes a streamed connection to a Web3 provider
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {PortDuplexStream} connectionStream - PortStream instance establishing a background connection
|
2019-08-01 15:24:33 +02:00
|
|
|
*/
|
|
|
|
function setupWeb3Connection (connectionStream) {
|
|
|
|
const providerStream = new StreamProvider()
|
|
|
|
providerStream.pipe(connectionStream).pipe(providerStream)
|
|
|
|
connectionStream.on('error', console.error.bind(console))
|
|
|
|
providerStream.on('error', console.error.bind(console))
|
|
|
|
global.ethereumProvider = providerStream
|
|
|
|
global.ethQuery = new EthQuery(providerStream)
|
|
|
|
global.eth = new Eth(providerStream)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Establishes a streamed connection to the background account manager
|
|
|
|
*
|
2020-01-13 19:36:36 +01:00
|
|
|
* @param {PortDuplexStream} connectionStream - PortStream instance establishing a background connection
|
|
|
|
* @param {Function} cb - Called when the remote account manager connection is established
|
2019-08-01 15:24:33 +02:00
|
|
|
*/
|
|
|
|
function setupControllerConnection (connectionStream, cb) {
|
|
|
|
const eventEmitter = new EventEmitter()
|
|
|
|
const backgroundDnode = Dnode({
|
|
|
|
sendUpdate: function (state) {
|
|
|
|
eventEmitter.emit('update', state)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
connectionStream.pipe(backgroundDnode).pipe(connectionStream)
|
|
|
|
backgroundDnode.once('remote', function (backgroundConnection) {
|
|
|
|
backgroundConnection.on = eventEmitter.on.bind(eventEmitter)
|
|
|
|
cb(null, backgroundConnection)
|
|
|
|
})
|
2017-03-31 22:20:16 +02:00
|
|
|
}
|