2021-02-04 19:15:23 +01:00
|
|
|
import copyToClipboard from 'copy-to-clipboard';
|
|
|
|
import log from 'loglevel';
|
2022-06-07 22:37:15 +02:00
|
|
|
import { clone, memoize } 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';
|
|
|
|
import { ALERT_TYPES } 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';
|
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 {
|
|
|
|
fetchLocale,
|
|
|
|
loadRelativeTimeFormatLocaleData,
|
2021-04-28 21:53:59 +02:00
|
|
|
} from './helpers/utils/i18n-helper';
|
|
|
|
import switchDirection from './helpers/utils/switch-direction';
|
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';
|
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) => {
|
|
|
|
actions._setBackgroundConnection(backgroundConnection);
|
|
|
|
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;
|
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) {
|
2022-06-07 22:37:15 +02:00
|
|
|
cb(err, metamaskState);
|
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);
|
|
|
|
cb(null, store);
|
|
|
|
});
|
|
|
|
});
|
2016-04-14 00:28:44 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 22:37:15 +02:00
|
|
|
const _setupLocale = async (currentLocale) => {
|
|
|
|
const currentLocaleMessages = currentLocale
|
|
|
|
? await fetchLocale(currentLocale)
|
|
|
|
: {};
|
|
|
|
const enLocaleMessages = await fetchLocale('en');
|
|
|
|
|
|
|
|
await loadRelativeTimeFormatLocaleData('en');
|
|
|
|
if (currentLocale) {
|
|
|
|
await loadRelativeTimeFormatLocaleData(currentLocale);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { currentLocaleMessages, enLocaleMessages };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setupLocale = memoize(_setupLocale);
|
|
|
|
|
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: {
|
|
|
|
current: currentLocaleMessages,
|
|
|
|
en: enLocaleMessages,
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
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)
|
|
|
|
) {
|
2020-10-06 19:57:02 +02:00
|
|
|
draftInitialState[ALERT_TYPES.unconnectedAccount] = {
|
|
|
|
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,
|
2020-07-14 17:20:41 +02:00
|
|
|
metamaskState.network,
|
2021-03-01 16:15:42 +01:00
|
|
|
metamaskState.provider.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
|
|
|
}
|
|
|
|
|
2022-08-01 15:23:21 +02:00
|
|
|
updateBackgroundConnection(backgroundConnection);
|
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) {
|
2022-02-14 19:29:24 +01:00
|
|
|
window.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;
|
|
|
|
};
|
2020-07-17 17:09:38 +02:00
|
|
|
window.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) {
|
|
|
|
const state = await window.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
|
|
|
});
|
|
|
|
};
|