-
Notifications
You must be signed in to change notification settings - Fork 5k
/
index.js
346 lines (305 loc) · 10.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import { promisify } from 'util';
import copyToClipboard from 'copy-to-clipboard';
import log from 'loglevel';
import { clone } from 'lodash';
import React from 'react';
import { render } from 'react-dom';
import browser from 'webextension-polyfill';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { getEnvironmentType } from '../app/scripts/lib/util';
import { AlertTypes } from '../shared/constants/alerts';
import { maskObject } from '../shared/modules/object.utils';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { SENTRY_UI_STATE } from '../app/scripts/constants/sentry-state';
import { ENVIRONMENT_TYPE_POPUP } from '../shared/constants/app';
import { COPY_OPTIONS } from '../shared/constants/copy';
import switchDirection from '../shared/lib/switch-direction';
import { setupLocale } from '../shared/lib/error-utils';
import { trace, TraceName } from '../shared/lib/trace';
import { getCurrentChainId } from '../shared/modules/selectors/networks';
import * as actions from './store/actions';
import configureStore from './store/store';
import {
getOriginOfCurrentTab,
getPermittedAccountsForCurrentTab,
getSelectedInternalAccount,
getUnapprovedTransactions,
getNetworkToAutomaticallySwitchTo,
getSwitchedNetworkDetails,
getUseRequestQueue,
} from './selectors';
import { ALERT_STATE } from './ducks/alerts';
import {
getUnconnectedAccountAlertEnabledness,
getUnconnectedAccountAlertShown,
} from './ducks/metamask/metamask';
import Root from './pages';
import txHelper from './helpers/utils/tx-helper';
import { setBackgroundConnection } from './store/background-connection';
import { getStartupTraceTags } from './helpers/utils/tags';
log.setLevel(global.METAMASK_DEBUG ? 'debug' : 'warn', false);
let reduxStore;
/**
* Method to update backgroundConnection object use by UI
*
* @param backgroundConnection - connection object to background
*/
export const updateBackgroundConnection = (backgroundConnection) => {
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,
)}`,
);
}
});
};
export default async function launchMetamaskUi(opts) {
const { backgroundConnection, traceContext } = opts;
const metamaskState = await trace(
{ name: TraceName.GetState, parentContext: traceContext },
() => promisify(backgroundConnection.getState.bind(backgroundConnection))(),
);
const store = await startApp(metamaskState, backgroundConnection, opts);
await promisify(
backgroundConnection.startPatches.bind(backgroundConnection),
)();
setupStateHooks(store);
return store;
}
/**
* Method to setup initial redux store for the ui application
*
* @param {*} metamaskState - flatten background state
* @param {*} backgroundConnection - rpc client connecting to the background process
* @param {*} activeTab - active browser tab
* @returns redux store
*/
export async function setupInitialStore(
metamaskState,
backgroundConnection,
activeTab,
) {
// parse opts
if (!metamaskState.featureFlags) {
metamaskState.featureFlags = {};
}
const { currentLocaleMessages, enLocaleMessages } = await setupLocale(
metamaskState.currentLocale,
);
if (metamaskState.textDirection === 'rtl') {
switchDirection('rtl');
}
const draftInitialState = {
activeTab,
// metamaskState represents the cross-tab state
metamask: metamaskState,
// appState represents the current tab's popup state
appState: {},
localeMessages: {
currentLocale: metamaskState.currentLocale,
current: currentLocaleMessages,
en: enLocaleMessages,
},
};
updateBackgroundConnection(backgroundConnection);
if (getEnvironmentType() === ENVIRONMENT_TYPE_POPUP) {
const { origin } = draftInitialState.activeTab;
const permittedAccountsForCurrentTab =
getPermittedAccountsForCurrentTab(draftInitialState);
const selectedAddress =
getSelectedInternalAccount(draftInitialState)?.address ?? '';
const unconnectedAccountAlertShownOrigins =
getUnconnectedAccountAlertShown(draftInitialState);
const unconnectedAccountAlertIsEnabled =
getUnconnectedAccountAlertEnabledness(draftInitialState);
if (
origin &&
unconnectedAccountAlertIsEnabled &&
!unconnectedAccountAlertShownOrigins[origin] &&
permittedAccountsForCurrentTab.length > 0 &&
!permittedAccountsForCurrentTab.includes(selectedAddress)
) {
draftInitialState[AlertTypes.unconnectedAccount] = {
state: ALERT_STATE.OPEN,
};
actions.setUnconnectedAccountAlertShown(origin);
}
}
const store = configureStore(draftInitialState);
reduxStore = store;
const unapprovedTxs = getUnapprovedTransactions(metamaskState);
// if unconfirmed txs, start on txConf page
const unapprovedTxsAll = txHelper(
unapprovedTxs,
metamaskState.unapprovedPersonalMsgs,
metamaskState.unapprovedDecryptMsgs,
metamaskState.unapprovedEncryptionPublicKeyMsgs,
metamaskState.unapprovedTypedMessages,
metamaskState.networkId,
getCurrentChainId({ metamask: metamaskState }),
);
const numberOfUnapprovedTx = unapprovedTxsAll.length;
if (numberOfUnapprovedTx > 0) {
store.dispatch(
actions.showConfTxPage({
id: unapprovedTxsAll[0].id,
}),
);
}
return store;
}
async function startApp(metamaskState, backgroundConnection, opts) {
const { traceContext } = opts;
const tags = getStartupTraceTags({ metamask: metamaskState });
const store = await trace(
{
name: TraceName.SetupStore,
parentContext: traceContext,
tags,
},
() =>
setupInitialStore(metamaskState, backgroundConnection, opts.activeTab),
);
// global metamask api - used by tooling
global.metamask = {
updateCurrentLocale: (code) => {
store.dispatch(actions.updateCurrentLocale(code));
},
setFeatureFlag: (key, value) => {
store.dispatch(actions.setFeatureFlag(key, value));
},
};
await trace(
{ name: TraceName.InitialActions, parentContext: traceContext },
() => runInitialActions(store),
);
trace({ name: TraceName.FirstRender, parentContext: traceContext }, () =>
render(<Root store={store} />, opts.container),
);
return store;
}
async function runInitialActions(store) {
const state = store.getState();
// This block autoswitches chains based on the last chain used
// for a given dapp, when there are no pending confimrations
// This allows the user to be connected on one chain
// for one dapp, and automatically change for another
const networkIdToSwitchTo = getNetworkToAutomaticallySwitchTo(state);
if (networkIdToSwitchTo) {
await store.dispatch(
actions.automaticallySwitchNetwork(
networkIdToSwitchTo,
getOriginOfCurrentTab(state),
),
);
} else if (getSwitchedNetworkDetails(state)) {
// It's possible that old details could exist if the user
// opened the toast but then didn't close it
// Clear out any existing switchedNetworkDetails
// if the user didn't just change the dapp network
await store.dispatch(actions.clearSwitchedNetworkDetails());
}
// Register this window as the current popup
// and set in background state
if (
getUseRequestQueue(state) &&
getEnvironmentType() === ENVIRONMENT_TYPE_POPUP
) {
const thisPopupId = Date.now();
global.metamask.id = thisPopupId;
await store.dispatch(actions.setCurrentExtensionPopupId(thisPopupId));
}
}
/**
* Setup functions on `window.stateHooks`. Some of these support
* application features, and some are just for debugging or testing.
*
* @param {object} store - The Redux store.
*/
function setupStateHooks(store) {
if (
process.env.METAMASK_DEBUG ||
process.env.IN_TEST ||
process.env.ENABLE_SETTINGS_PAGE_DEV_OPTIONS
) {
/**
* 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.
*
* @param {string} [msg] - The error message to throw, defaults to 'Test Error'
*/
window.stateHooks.throwTestError = async function (msg = 'Test Error') {
const error = new Error(msg);
error.name = 'TestError';
throw error;
};
/**
* The following stateHook is a method intended to throw an error in the
* background, used in our E2E test to ensure that errors are attempted to be
* sent to sentry.
*
* @param {string} [msg] - The error message to throw, defaults to 'Test Error'
*/
window.stateHooks.throwTestBackgroundError = async function (
msg = 'Test Error',
) {
await actions.throwTestBackgroundError(msg);
};
}
window.stateHooks.getCleanAppState = async function () {
const state = clone(store.getState());
// we use the manifest.json version from getVersion and not
// `process.env.METAMASK_VERSION` as they can be different (see `getVersion`
// for more info)
state.version = global.platform.getVersion();
state.browser = window.navigator.userAgent;
return state;
};
window.stateHooks.getSentryAppState = function () {
const reduxState = store.getState();
return maskObject(reduxState, SENTRY_UI_STATE);
};
window.stateHooks.getLogs = function () {
// These logs are logged by LoggingController
const reduxState = store.getState();
const { logs } = reduxState.metamask;
const logsArray = Object.values(logs).sort((a, b) => {
return a.timestamp - b.timestamp;
});
return logsArray;
};
}
window.logStateString = async function (cb) {
const state = await window.stateHooks.getCleanAppState();
const logs = window.stateHooks.getLogs();
browser.runtime
.getPlatformInfo()
.then((platform) => {
state.platform = platform;
state.logs = logs;
const stateString = JSON.stringify(state, null, 2);
cb(null, stateString);
})
.catch((err) => {
cb(err);
});
};
window.logState = function (toClipboard) {
return window.logStateString((err, result) => {
if (err) {
console.error(err.message);
} else if (toClipboard) {
copyToClipboard(result, COPY_OPTIONS);
console.log('State log copied');
} else {
console.log(result);
}
});
};