2021-02-04 19:15:23 +01:00
|
|
|
import copyToClipboard from 'copy-to-clipboard';
|
|
|
|
import log from 'loglevel';
|
2022-09-16 21:05:21 +02:00
|
|
|
import { clone } from 'lodash';
|
2021-02-04 19:15:23 +01:00
|
|
|
import React from 'react';
|
|
|
|
import { render } from 'react-dom';
|
2022-03-25 10:54:27 +01:00
|
|
|
import browser from 'webextension-polyfill';
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import { getEnvironmentType } from '../app/scripts/lib/util';
|
2023-01-27 19:28:03 +01:00
|
|
|
import { AlertTypes } from '../shared/constants/alerts';
|
2022-07-22 22:39:48 +02:00
|
|
|
import { maskObject } from '../shared/modules/object.utils';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { SENTRY_STATE } from '../app/scripts/lib/setupSentry';
|
|
|
|
import { ENVIRONMENT_TYPE_POPUP } from '../shared/constants/app';
|
2022-09-16 21:05:21 +02:00
|
|
|
import switchDirection from '../shared/lib/switch-direction';
|
|
|
|
import { setupLocale } from '../shared/lib/error-utils';
|
2021-04-28 21:53:59 +02:00
|
|
|
import * as actions from './store/actions';
|
|
|
|
import configureStore from './store/store';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getPermittedAccountsForCurrentTab,
|
|
|
|
getSelectedAddress,
|
2021-04-28 21:53:59 +02:00
|
|
|
} from './selectors';
|
|
|
|
import { ALERT_STATE } from './ducks/alerts';
|
2020-05-12 15:01:52 +02:00
|
|
|
import {
|
2020-06-15 21:08:53 +02:00
|
|
|
getUnconnectedAccountAlertEnabledness,
|
|
|
|
getUnconnectedAccountAlertShown,
|
2021-04-28 21:53:59 +02:00
|
|
|
} from './ducks/metamask/metamask';
|
|
|
|
import Root from './pages';
|
|
|
|
import txHelper from './helpers/utils/tx-helper';
|
2022-09-05 16:55:34 +02:00
|
|
|
import { _setBackgroundConnection } from './store/action-queue';
|
2017-02-20 21:59:44 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
log.setLevel(global.METAMASK_DEBUG ? 'debug' : 'warn');
|
2017-03-31 22:20:16 +02:00
|
|
|
|
2022-08-01 15:23:21 +02:00
|
|
|
let reduxStore;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to update backgroundConnection object use by UI
|
|
|
|
*
|
|
|
|
* @param backgroundConnection - connection object to background
|
|
|
|
*/
|
|
|
|
export const updateBackgroundConnection = (backgroundConnection) => {
|
2022-09-05 16:55:34 +02:00
|
|
|
_setBackgroundConnection(backgroundConnection);
|
2022-08-01 15:23:21 +02:00
|
|
|
backgroundConnection.onNotification((data) => {
|
|
|
|
if (data.method === 'sendUpdate') {
|
|
|
|
reduxStore.dispatch(actions.updateMetamaskState(data.params[0]));
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
`Internal JSON-RPC Notification Not Handled:\n\n ${JSON.stringify(
|
|
|
|
data,
|
|
|
|
)}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default function launchMetamaskUi(opts, cb) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { backgroundConnection } = opts;
|
2023-04-25 16:32:51 +02:00
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(desktop)
|
2023-02-23 17:39:48 +01:00
|
|
|
let desktopEnabled = false;
|
|
|
|
|
|
|
|
backgroundConnection.getDesktopEnabled(function (err, result) {
|
|
|
|
if (err) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
desktopEnabled = result;
|
|
|
|
});
|
|
|
|
///: END:ONLY_INCLUDE_IN
|
|
|
|
|
2016-04-14 00:28:44 +02:00
|
|
|
// check if we are unlocked first
|
2019-07-10 21:01:48 +02:00
|
|
|
backgroundConnection.getState(function (err, metamaskState) {
|
2019-11-20 01:03:20 +01:00
|
|
|
if (err) {
|
2023-02-23 17:39:48 +01:00
|
|
|
cb(
|
|
|
|
err,
|
|
|
|
{
|
|
|
|
...metamaskState,
|
2023-04-25 16:32:51 +02:00
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(desktop)
|
2023-02-23 17:39:48 +01:00
|
|
|
desktopEnabled,
|
|
|
|
///: END:ONLY_INCLUDE_IN
|
|
|
|
},
|
|
|
|
backgroundConnection,
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
return;
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2020-11-03 00:41:28 +01:00
|
|
|
startApp(metamaskState, backgroundConnection, opts).then((store) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
setupDebuggingHelpers(store);
|
2023-02-23 17:39:48 +01:00
|
|
|
cb(
|
|
|
|
null,
|
|
|
|
store,
|
2023-04-25 16:32:51 +02:00
|
|
|
///: BEGIN:ONLY_INCLUDE_IN(desktop)
|
2023-02-23 17:39:48 +01:00
|
|
|
backgroundConnection,
|
|
|
|
///: END:ONLY_INCLUDE_IN
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2016-04-14 00:28:44 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function startApp(metamaskState, backgroundConnection, opts) {
|
2016-04-14 00:28:44 +02:00
|
|
|
// parse opts
|
2019-11-20 01:03:20 +01:00
|
|
|
if (!metamaskState.featureFlags) {
|
2021-02-04 19:15:23 +01:00
|
|
|
metamaskState.featureFlags = {};
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2018-03-16 01:29:45 +01:00
|
|
|
|
2022-06-07 22:37:15 +02:00
|
|
|
const { currentLocaleMessages, enLocaleMessages } = await setupLocale(
|
|
|
|
metamaskState.currentLocale,
|
|
|
|
);
|
2020-07-03 02:34:48 +02:00
|
|
|
|
2019-09-03 19:47:54 +02:00
|
|
|
if (metamaskState.textDirection === 'rtl') {
|
2021-02-04 19:15:23 +01:00
|
|
|
await switchDirection('rtl');
|
2019-09-03 19:47:54 +02:00
|
|
|
}
|
|
|
|
|
2020-05-12 15:01:52 +02:00
|
|
|
const draftInitialState = {
|
2019-08-01 15:24:33 +02:00
|
|
|
activeTab: opts.activeTab,
|
2016-04-14 00:28:44 +02:00
|
|
|
|
|
|
|
// metamaskState represents the cross-tab state
|
|
|
|
metamask: metamaskState,
|
|
|
|
|
|
|
|
// appState represents the current tab's popup state
|
2016-09-13 06:30:04 +02:00
|
|
|
appState: {},
|
2016-04-20 03:21:28 +02:00
|
|
|
|
2018-03-22 01:41:47 +01:00
|
|
|
localeMessages: {
|
2023-02-02 22:24:48 +01:00
|
|
|
currentLocale: metamaskState.currentLocale,
|
2018-03-22 01:41:47 +01:00
|
|
|
current: currentLocaleMessages,
|
|
|
|
en: enLocaleMessages,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-12 15:01:52 +02:00
|
|
|
|
2022-09-14 17:35:02 +02:00
|
|
|
updateBackgroundConnection(backgroundConnection);
|
|
|
|
|
2020-05-12 15:01:52 +02:00
|
|
|
if (getEnvironmentType() === ENVIRONMENT_TYPE_POPUP) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { origin } = draftInitialState.activeTab;
|
2022-07-31 20:26:40 +02:00
|
|
|
const permittedAccountsForCurrentTab =
|
|
|
|
getPermittedAccountsForCurrentTab(draftInitialState);
|
2021-02-04 19:15:23 +01:00
|
|
|
const selectedAddress = getSelectedAddress(draftInitialState);
|
2022-07-31 20:26:40 +02:00
|
|
|
const unconnectedAccountAlertShownOrigins =
|
|
|
|
getUnconnectedAccountAlertShown(draftInitialState);
|
|
|
|
const unconnectedAccountAlertIsEnabled =
|
|
|
|
getUnconnectedAccountAlertEnabledness(draftInitialState);
|
2020-05-12 15:01:52 +02:00
|
|
|
|
|
|
|
if (
|
|
|
|
origin &&
|
2020-06-15 21:08:53 +02:00
|
|
|
unconnectedAccountAlertIsEnabled &&
|
|
|
|
!unconnectedAccountAlertShownOrigins[origin] &&
|
2020-05-12 15:01:52 +02:00
|
|
|
permittedAccountsForCurrentTab.length > 0 &&
|
|
|
|
!permittedAccountsForCurrentTab.includes(selectedAddress)
|
|
|
|
) {
|
2023-01-27 19:28:03 +01:00
|
|
|
draftInitialState[AlertTypes.unconnectedAccount] = {
|
2020-10-06 19:57:02 +02:00
|
|
|
state: ALERT_STATE.OPEN,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
actions.setUnconnectedAccountAlertShown(origin);
|
2020-05-12 15:01:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const store = configureStore(draftInitialState);
|
2022-08-01 15:23:21 +02:00
|
|
|
reduxStore = store;
|
2016-04-14 00:28:44 +02:00
|
|
|
|
|
|
|
// if unconfirmed txs, start on txConf page
|
2020-03-06 22:34:56 +01:00
|
|
|
const unapprovedTxsAll = txHelper(
|
|
|
|
metamaskState.unapprovedTxs,
|
|
|
|
metamaskState.unapprovedMsgs,
|
|
|
|
metamaskState.unapprovedPersonalMsgs,
|
|
|
|
metamaskState.unapprovedDecryptMsgs,
|
|
|
|
metamaskState.unapprovedEncryptionPublicKeyMsgs,
|
|
|
|
metamaskState.unapprovedTypedMessages,
|
NetworkController: Split `network` into `networkId` and `networkStatus` (#17556)
The `network` store of the network controller crams two types of data
into one place. It roughly tracks whether we have enough information to
make requests to the network and whether the network is capable of
receiving requests, but it also stores the ID of the network (as
obtained via `net_version`).
Generally we shouldn't be using the network ID for anything, as it has
been completely replaced by chain ID, which all custom RPC endpoints
have been required to support for over a year now. However, as the
network ID is used in various places within the extension codebase,
removing it entirely would be a non-trivial effort. So, minimally, this
commit splits `network` into two stores: `networkId` and
`networkStatus`. But it also expands the concept of network status.
Previously, the network was in one of two states: "loading" and
"not-loading". But now it can be in one of four states:
- `available`: The network is able to receive and respond to requests.
- `unavailable`: The network is not able to receive and respond to
requests for unknown reasons.
- `blocked`: The network is actively blocking requests based on the
user's geolocation. (This is specific to Infura.)
- `unknown`: We don't know whether the network can receive and respond
to requests, either because we haven't checked or we tried to check
and were unsuccessful.
This commit also changes how the network status is determined —
specifically, how many requests are used to determine that status, when
they occur, and whether they are awaited. Previously, the network
controller would make 2 to 3 requests during the course of running
`lookupNetwork`.
* First, if it was an Infura network, it would make a request for
`eth_blockNumber` to determine whether Infura was blocking requests or
not, then emit an appropriate event. This operation was not awaited.
* Then, regardless of the network, it would fetch the network ID via
`net_version`. This operation was awaited.
* Finally, regardless of the network, it would fetch the latest block
via `eth_getBlockByNumber`, then use the result to determine whether
the network supported EIP-1559. This operation was awaited.
Now:
* One fewer request is made, specifically `eth_blockNumber`, as we don't
need to make an extra request to determine whether Infura is blocking
requests; we can reuse `eth_getBlockByNumber`;
* All requests are awaited, which makes `lookupNetwork` run fully
in-band instead of partially out-of-band; and
* Both requests for `net_version` and `eth_getBlockByNumber` are
performed in parallel to make `lookupNetwork` run slightly faster.
2023-03-31 00:49:12 +02:00
|
|
|
metamaskState.networkId,
|
2023-05-02 17:53:20 +02:00
|
|
|
metamaskState.providerConfig.chainId,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
const numberOfUnapprovedTx = unapprovedTxsAll.length;
|
2021-02-04 14:11:05 +01:00
|
|
|
if (numberOfUnapprovedTx > 0) {
|
2020-11-03 00:41:28 +01:00
|
|
|
store.dispatch(
|
|
|
|
actions.showConfTxPage({
|
|
|
|
id: unapprovedTxsAll[0].id,
|
|
|
|
}),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2016-04-14 00:28:44 +02:00
|
|
|
}
|
|
|
|
|
2018-03-30 20:16:08 +02:00
|
|
|
// global metamask api - used by tooling
|
|
|
|
global.metamask = {
|
|
|
|
updateCurrentLocale: (code) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
store.dispatch(actions.updateCurrentLocale(code));
|
2018-03-30 20:16:08 +02:00
|
|
|
},
|
|
|
|
setProviderType: (type) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
store.dispatch(actions.setProviderType(type));
|
2018-03-30 20:16:08 +02:00
|
|
|
},
|
2019-09-16 19:11:01 +02:00
|
|
|
setFeatureFlag: (key, value) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
store.dispatch(actions.setFeatureFlag(key, value));
|
2019-09-16 19:11:01 +02:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-03-30 09:48:37 +02:00
|
|
|
|
2016-04-14 00:28:44 +02:00
|
|
|
// start app
|
2021-02-04 19:15:23 +01:00
|
|
|
render(<Root store={store} />, opts.container);
|
2017-03-31 22:20:16 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return store;
|
2016-04-14 00:28:44 +02:00
|
|
|
}
|
2020-02-06 17:38:14 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function setupDebuggingHelpers(store) {
|
2023-01-23 19:58:35 +01:00
|
|
|
/**
|
|
|
|
* The following stateHook is a method intended to throw an error, used in
|
|
|
|
* our E2E test to ensure that errors are attempted to be sent to sentry.
|
|
|
|
*/
|
|
|
|
window.stateHooks.throwTestError = async function () {
|
|
|
|
const error = new Error('Test Error');
|
|
|
|
error.name = 'TestError';
|
|
|
|
throw error;
|
|
|
|
};
|
2022-11-09 20:28:32 +01:00
|
|
|
window.stateHooks.getCleanAppState = async function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const state = clone(store.getState());
|
|
|
|
state.version = global.platform.getVersion();
|
|
|
|
state.browser = window.navigator.userAgent;
|
2022-02-14 19:29:24 +01:00
|
|
|
state.completeTxList = await actions.getTransactions({
|
|
|
|
filterToCurrentNetwork: false,
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
return state;
|
|
|
|
};
|
2022-11-09 20:28:32 +01:00
|
|
|
window.stateHooks.getSentryState = function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const fullState = store.getState();
|
|
|
|
const debugState = maskObject(fullState, SENTRY_STATE);
|
2020-07-17 17:09:38 +02:00
|
|
|
return {
|
|
|
|
browser: window.navigator.userAgent,
|
|
|
|
store: debugState,
|
|
|
|
version: global.platform.getVersion(),
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
|
|
|
};
|
2020-02-06 17:38:14 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 19:29:24 +01:00
|
|
|
window.logStateString = async function (cb) {
|
2022-11-09 20:28:32 +01:00
|
|
|
const state = await window.stateHooks.getCleanAppState();
|
2022-03-25 10:54:27 +01:00
|
|
|
browser.runtime
|
|
|
|
.getPlatformInfo()
|
|
|
|
.then((platform) => {
|
|
|
|
state.platform = platform;
|
|
|
|
const stateString = JSON.stringify(state, null, 2);
|
|
|
|
cb(null, stateString);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
cb(err);
|
2022-03-25 10:54:27 +01:00
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-02-06 17:38:14 +01:00
|
|
|
|
|
|
|
window.logState = function (toClipboard) {
|
|
|
|
return window.logStateString((err, result) => {
|
|
|
|
if (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.error(err.message);
|
2020-02-06 17:38:14 +01:00
|
|
|
} else if (toClipboard) {
|
2021-02-04 19:15:23 +01:00
|
|
|
copyToClipboard(result);
|
|
|
|
console.log('State log copied');
|
2020-02-06 17:38:14 +01:00
|
|
|
} else {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.log(result);
|
2020-02-06 17:38:14 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
};
|