2021-02-04 19:15:23 +01:00
|
|
|
import copyToClipboard from 'copy-to-clipboard';
|
|
|
|
import log from 'loglevel';
|
|
|
|
import { clone } from 'lodash';
|
|
|
|
import React from 'react';
|
|
|
|
import { render } from 'react-dom';
|
|
|
|
import { getEnvironmentType } from '../app/scripts/lib/util';
|
|
|
|
import { ALERT_TYPES } from '../shared/constants/alerts';
|
|
|
|
import { SENTRY_STATE } from '../app/scripts/lib/setupSentry';
|
|
|
|
import { ENVIRONMENT_TYPE_POPUP } from '../shared/constants/app';
|
|
|
|
import Root from './app/pages';
|
|
|
|
import * as actions from './app/store/actions';
|
|
|
|
import configureStore from './app/store/store';
|
|
|
|
import txHelper from './lib/tx-helper';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
fetchLocale,
|
|
|
|
loadRelativeTimeFormatLocaleData,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from './app/helpers/utils/i18n-helper';
|
|
|
|
import switchDirection from './app/helpers/utils/switch-direction';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getPermittedAccountsForCurrentTab,
|
|
|
|
getSelectedAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from './app/selectors';
|
|
|
|
import { ALERT_STATE } from './app/ducks/alerts';
|
2020-05-12 15:01:52 +02:00
|
|
|
import {
|
2020-06-15 21:08:53 +02:00
|
|
|
getUnconnectedAccountAlertEnabledness,
|
|
|
|
getUnconnectedAccountAlertShown,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from './app/ducks/metamask/metamask';
|
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
|
|
|
|
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;
|
|
|
|
actions._setBackgroundConnection(backgroundConnection);
|
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) {
|
2021-02-04 19:15:23 +01:00
|
|
|
cb(err);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-03-28 02:05:12 +02:00
|
|
|
const currentLocaleMessages = metamaskState.currentLocale
|
|
|
|
? await fetchLocale(metamaskState.currentLocale)
|
2021-02-04 19:15:23 +01:00
|
|
|
: {};
|
|
|
|
const enLocaleMessages = await fetchLocale('en');
|
2018-03-19 17:06:16 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
await loadRelativeTimeFormatLocaleData('en');
|
2020-07-03 02:34:48 +02:00
|
|
|
if (metamaskState.currentLocale) {
|
2021-02-04 19:15:23 +01:00
|
|
|
await loadRelativeTimeFormatLocaleData(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;
|
2020-11-03 00:41:28 +01:00
|
|
|
const permittedAccountsForCurrentTab = getPermittedAccountsForCurrentTab(
|
|
|
|
draftInitialState,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
const selectedAddress = getSelectedAddress(draftInitialState);
|
2020-11-03 00:41:28 +01:00
|
|
|
const unconnectedAccountAlertShownOrigins = getUnconnectedAccountAlertShown(
|
|
|
|
draftInitialState,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
const unconnectedAccountAlertIsEnabled = getUnconnectedAccountAlertEnabledness(
|
|
|
|
draftInitialState,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
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);
|
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
|
|
|
}
|
|
|
|
|
2020-08-18 18:36:45 +02:00
|
|
|
backgroundConnection.on('update', function (state) {
|
2021-02-04 19:15:23 +01:00
|
|
|
store.dispatch(actions.updateMetamaskState(state));
|
|
|
|
});
|
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-07-17 17:09:38 +02:00
|
|
|
/**
|
|
|
|
* Return a "masked" copy of the given object.
|
|
|
|
*
|
|
|
|
* The returned object includes only the properties present in the mask. The
|
|
|
|
* mask is an object that mirrors the structure of the given object, except
|
|
|
|
* the only values are `true` or a sub-mask. `true` implies the property
|
|
|
|
* should be included, and a sub-mask implies the property should be further
|
|
|
|
* masked according to that sub-mask.
|
|
|
|
*
|
|
|
|
* @param {Object} object - The object to mask
|
|
|
|
* @param {Object<Object|boolean>} mask - The mask to apply to the object
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
function maskObject(object, mask) {
|
|
|
|
return Object.keys(object).reduce((state, key) => {
|
|
|
|
if (mask[key] === true) {
|
2021-02-04 19:15:23 +01:00
|
|
|
state[key] = object[key];
|
2020-11-03 00:41:28 +01:00
|
|
|
} else if (mask[key]) {
|
2021-02-04 19:15:23 +01:00
|
|
|
state[key] = maskObject(object[key], mask[key]);
|
2020-11-03 00:41:28 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return state;
|
|
|
|
}, {});
|
2020-07-17 17:09:38 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function setupDebuggingHelpers(store) {
|
2020-02-06 17:38:14 +01:00
|
|
|
window.getCleanAppState = function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
const state = clone(store.getState());
|
|
|
|
state.version = global.platform.getVersion();
|
|
|
|
state.browser = window.navigator.userAgent;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
window.logStateString = function (cb) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const state = window.getCleanAppState();
|
2020-02-06 17:38:14 +01:00
|
|
|
global.platform.getPlatformInfo((err, platform) => {
|
|
|
|
if (err) {
|
2021-02-04 19:15:23 +01:00
|
|
|
cb(err);
|
|
|
|
return;
|
2020-02-06 17:38:14 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
state.platform = platform;
|
|
|
|
const stateString = JSON.stringify(state, null, 2);
|
|
|
|
cb(null, stateString);
|
|
|
|
});
|
|
|
|
};
|
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
|
|
|
});
|
|
|
|
};
|