2019-09-12 22:07:27 +02:00
|
|
|
// polyfills
|
2021-02-04 19:15:23 +01:00
|
|
|
import '@formatjs/intl-relativetimeformat/polyfill';
|
|
|
|
|
2021-07-15 19:59:34 +02:00
|
|
|
// dev only, "react-devtools" import is skipped in prod builds
|
|
|
|
import 'react-devtools';
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import PortStream from 'extension-port-stream';
|
2022-03-18 20:07:05 +01:00
|
|
|
import browser from 'webextension-polyfill';
|
2021-02-04 19:15:23 +01:00
|
|
|
|
|
|
|
import Eth from 'ethjs';
|
|
|
|
import EthQuery from 'eth-query';
|
|
|
|
import StreamProvider from 'web3-stream-provider';
|
|
|
|
import log from 'loglevel';
|
|
|
|
import launchMetaMaskUi from '../../ui';
|
2020-08-18 21:18:25 +02:00
|
|
|
import {
|
|
|
|
ENVIRONMENT_TYPE_FULLSCREEN,
|
|
|
|
ENVIRONMENT_TYPE_POPUP,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../shared/constants/app';
|
2022-05-26 06:48:23 +02:00
|
|
|
import { isManifestV3 } from '../../shared/modules/mv3.utils';
|
2022-06-07 22:37:15 +02:00
|
|
|
import { SUPPORT_LINK } from '../../ui/helpers/constants/common';
|
|
|
|
import { getErrorHtml } from '../../ui/helpers/utils/error-utils';
|
2021-02-04 19:15:23 +01:00
|
|
|
import ExtensionPlatform from './platforms/extension';
|
|
|
|
import { setupMultiplex } from './lib/stream-utils';
|
|
|
|
import { getEnvironmentType } from './lib/util';
|
2021-03-18 19:23:46 +01:00
|
|
|
import metaRPCClientFactory from './lib/metaRPCClientFactory';
|
2016-01-15 03:26:54 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
start().catch(log.error);
|
2018-03-15 00:31:45 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function start() {
|
2022-06-07 22:37:15 +02:00
|
|
|
async function displayCriticalError(container, err, metamaskState) {
|
|
|
|
const html = await getErrorHtml(SUPPORT_LINK, metamaskState);
|
|
|
|
|
|
|
|
container.innerHTML = html;
|
|
|
|
|
|
|
|
const button = document.getElementById('critical-error-button');
|
|
|
|
|
|
|
|
button.addEventListener('click', (_) => {
|
|
|
|
browser.runtime.reload();
|
|
|
|
});
|
|
|
|
|
|
|
|
log.error(err.stack);
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2018-03-15 00:31:45 +01:00
|
|
|
// create platform global
|
2021-02-04 19:15:23 +01:00
|
|
|
global.platform = new ExtensionPlatform();
|
2018-03-15 00:31:45 +01:00
|
|
|
|
|
|
|
// identify window type (popup, notification)
|
2021-02-04 19:15:23 +01:00
|
|
|
const windowType = getEnvironmentType();
|
2018-03-15 00:31:45 +01:00
|
|
|
|
|
|
|
// setup stream to background
|
2022-03-18 20:07:05 +01:00
|
|
|
const extensionPort = browser.runtime.connect({ name: windowType });
|
2022-06-07 22:37:15 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const connectionStream = new PortStream(extensionPort);
|
2018-03-15 00:31:45 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const activeTab = await queryCurrentActiveTab(windowType);
|
2022-05-26 06:48:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* In case of MV3 the issue of blank screen was very frequent, it is caused by UI initialising before background is ready to send state.
|
|
|
|
* Code below ensures that UI is rendered only after background is ready.
|
|
|
|
*/
|
|
|
|
if (isManifestV3()) {
|
|
|
|
extensionPort.onMessage.addListener((message) => {
|
|
|
|
if (message?.name === 'CONNECTION_READY') {
|
|
|
|
initializeUiWithTab(activeTab);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
initializeUiWithTab(activeTab);
|
|
|
|
}
|
2016-08-17 01:46:44 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function initializeUiWithTab(tab) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const container = document.getElementById('app-content');
|
2019-08-01 15:24:33 +02:00
|
|
|
initializeUi(tab, container, connectionStream, (err, store) => {
|
|
|
|
if (err) {
|
2022-06-07 22:37:15 +02:00
|
|
|
// if there's an error, store will be = metamaskState
|
|
|
|
displayCriticalError(container, err, store);
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-08-01 15:24:33 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const state = store.getState();
|
|
|
|
const { metamask: { completedOnboarding } = {} } = state;
|
2019-08-01 15:24:33 +02:00
|
|
|
|
|
|
|
if (!completedOnboarding && windowType !== ENVIRONMENT_TYPE_FULLSCREEN) {
|
2021-02-04 19:15:23 +01:00
|
|
|
global.platform.openExtensionInBrowser();
|
2019-08-01 15:24:33 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2019-08-01 15:24:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function queryCurrentActiveTab(windowType) {
|
2019-08-01 15:24:33 +02:00
|
|
|
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) {
|
2021-02-04 19:15:23 +01:00
|
|
|
resolve({});
|
|
|
|
return;
|
2019-08-01 15:24:33 +02:00
|
|
|
}
|
|
|
|
|
2022-03-18 20:07:05 +01:00
|
|
|
browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const [activeTab] = tabs;
|
|
|
|
const { id, title, url } = activeTab;
|
|
|
|
const { origin, protocol } = url ? new URL(url) : {};
|
2020-06-02 01:24:27 +02:00
|
|
|
|
2020-07-02 23:01:04 +02:00
|
|
|
if (!origin || origin === 'null') {
|
2021-02-04 19:15:23 +01:00
|
|
|
resolve({});
|
|
|
|
return;
|
2020-07-02 23:01:04 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
resolve({ id, title, origin, protocol, url });
|
|
|
|
});
|
|
|
|
});
|
2019-08-01 15:24:33 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function initializeUi(activeTab, container, connectionStream, cb) {
|
2019-08-01 15:24:33 +02:00
|
|
|
connectToAccountManager(connectionStream, (err, backgroundConnection) => {
|
|
|
|
if (err) {
|
2022-06-07 22:37:15 +02:00
|
|
|
cb(err, null);
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-08-01 15:24:33 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
launchMetaMaskUi(
|
|
|
|
{
|
|
|
|
activeTab,
|
|
|
|
container,
|
|
|
|
backgroundConnection,
|
|
|
|
},
|
|
|
|
cb,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2019-08-01 15:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
function connectToAccountManager(connectionStream, cb) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const mx = setupMultiplex(connectionStream);
|
|
|
|
setupControllerConnection(mx.createStream('controller'), cb);
|
|
|
|
setupWeb3Connection(mx.createStream('provider'));
|
2019-08-01 15:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
function setupWeb3Connection(connectionStream) {
|
2021-02-04 19:15:23 +01:00
|
|
|
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);
|
2019-08-01 15:24:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
function setupControllerConnection(connectionStream, cb) {
|
2021-03-18 19:23:46 +01:00
|
|
|
const backgroundRPC = metaRPCClientFactory(connectionStream);
|
|
|
|
cb(null, backgroundRPC);
|
2017-03-31 22:20:16 +02:00
|
|
|
}
|