-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathutil.ts
More file actions
615 lines (548 loc) · 17 KB
/
util.ts
File metadata and controls
615 lines (548 loc) · 17 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
import urlLib from 'url';
import { AccessList } from '@ethereumjs/tx';
import BN from 'bn.js';
import { memoize } from 'lodash';
import browser from 'webextension-polyfill';
import {
TransactionEnvelopeType,
TransactionMeta,
} from '@metamask/transaction-controller';
import type { Provider } from '@metamask/network-controller';
import { CaipAssetType, parseCaipAssetType } from '@metamask/utils';
import { MultichainAssetsRatesControllerState } from '@metamask/assets-controllers';
import { AssetConversion, FungibleAssetMarketData } from '@metamask/snaps-sdk';
import {
ENVIRONMENT_TYPE_BACKGROUND,
ENVIRONMENT_TYPE_FULLSCREEN,
ENVIRONMENT_TYPE_NOTIFICATION,
ENVIRONMENT_TYPE_SIDEPANEL,
ENVIRONMENT_TYPE_POPUP,
PLATFORM_BRAVE,
PLATFORM_CHROME,
PLATFORM_EDGE,
PLATFORM_FIREFOX,
PLATFORM_OPERA,
INSTALL_TYPE,
type InstallType,
type Platform,
} from '../../../shared/constants/app';
import { CHAIN_IDS, TEST_CHAINS } from '../../../shared/constants/network';
import { stripHexPrefix } from '../../../shared/modules/hexstring-utils';
import { getMethodDataAsync } from '../../../shared/lib/four-byte';
import { getSafeChainsListFromCacheOnly } from '../../../shared/lib/network-utils';
/**
* @see {@link getEnvironmentType}
*/
const getEnvironmentTypeMemo = memoize((url) => {
const parsedUrl = new URL(url);
if (parsedUrl.pathname === '/popup.html') {
return ENVIRONMENT_TYPE_POPUP;
} else if (['/home.html'].includes(parsedUrl.pathname)) {
return ENVIRONMENT_TYPE_FULLSCREEN;
} else if (parsedUrl.pathname === '/notification.html') {
return ENVIRONMENT_TYPE_NOTIFICATION;
} else if (parsedUrl.pathname === '/sidepanel.html') {
return ENVIRONMENT_TYPE_SIDEPANEL;
}
return ENVIRONMENT_TYPE_BACKGROUND;
});
/**
* Returns the window type for the application
*
* - `popup` refers to the extension opened through the browser app icon (in top right corner in chrome and firefox)
* - `fullscreen` refers to the main browser window
* - `notification` refers to the popup that appears in its own window when taking action outside of metamask
* - `background` refers to the background page
*
* NOTE: This should only be called on internal URLs.
*
* @param [url] - the URL of the window
* @returns the environment ENUM
*/
const getEnvironmentType = (url = window.location.href) =>
getEnvironmentTypeMemo(url);
/**
* Returns the platform (browser) where the extension is running.
*
* @returns the platform ENUM
*/
const getPlatform = (): Platform => {
const { navigator } = window;
const { userAgent } = navigator;
if (userAgent.includes('Firefox')) {
return PLATFORM_FIREFOX;
} else if ('brave' in navigator) {
return PLATFORM_BRAVE;
} else if (userAgent.includes('Edg/')) {
return PLATFORM_EDGE;
} else if (userAgent.includes('OPR')) {
return PLATFORM_OPERA;
}
return PLATFORM_CHROME;
};
/**
* Cached install type value.
*
* @see {@link INSTALL_TYPE} for possible values and their meanings.
*/
let cachedInstallType: InstallType = INSTALL_TYPE.UNKNOWN;
/**
* Initializes the install type by fetching it from the browser API.
* This should be called early in the extension lifecycle.
* The result is cached and can be retrieved synchronously via getInstallType().
*
* @returns A promise that resolves to the install type
*/
const initInstallType = async (): Promise<InstallType> => {
try {
const extensionInfo = await browser.management.getSelf();
if (extensionInfo.installType) {
cachedInstallType = extensionInfo.installType as InstallType;
}
} catch (error) {
// Silently fail - install type will remain 'unknown'
console.error('Error getting extension installType', error);
}
return cachedInstallType;
};
/**
* Returns the cached install type.
* Call initInstallType() first to populate the cache.
*
* @returns The install type
*/
const getInstallType = (): InstallType => {
return cachedInstallType;
};
/**
* Converts a hex string to a BN object
*
* @param inputHex - A number represented as a hex string
* @returns A BN object
*/
function hexToBn(inputHex: string) {
return new BN(stripHexPrefix(inputHex), 16);
}
/**
* Used to multiply a BN by a fraction
*
* @param targetBN - The number to multiply by a fraction
* @param numerator - The numerator of the fraction multiplier
* @param denominator - The denominator of the fraction multiplier
* @returns The product of the multiplication
*/
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
// eslint-disable-next-line @typescript-eslint/naming-convention
function BnMultiplyByFraction(
targetBN: BN,
numerator: number,
denominator: number,
) {
const numBN = new BN(numerator);
const denomBN = new BN(denominator);
return targetBN.mul(numBN).div(denomBN);
}
/**
* Prefixes a hex string with '0x' or '-0x' and returns it. Idempotent.
*
* @param str - The string to prefix.
* @returns The prefixed string.
*/
const addHexPrefix = (str: string) => {
if (typeof str !== 'string' || str.match(/^-?0x/u)) {
return str;
}
if (str.match(/^-?0X/u)) {
return str.replace('0X', '0x');
}
if (str.startsWith('-')) {
return str.replace('-', '-0x');
}
return `0x${str}`;
};
function getChainType(chainId: string) {
if (chainId === CHAIN_IDS.MAINNET) {
return 'mainnet';
} else if ((TEST_CHAINS as string[]).includes(chainId)) {
return 'testnet';
}
return 'custom';
}
/**
* Checks if the alarmname exists in the list
*
* @param alarmList
* @param alarmName
* @returns
*/
function checkAlarmExists(alarmList: { name: string }[], alarmName: string) {
return alarmList.some((alarm) => alarm.name === alarmName);
}
export {
BnMultiplyByFraction,
addHexPrefix,
checkAlarmExists,
getChainType,
getEnvironmentType,
getInstallType,
getPlatform,
hexToBn,
initInstallType,
};
// Taken from https://stackoverflow.com/a/1349426/3696652
const characters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
export const generateRandomId = () => {
let result = '';
const charactersLength = characters.length;
for (let i = 0; i < 20; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
};
export const isValidDate = (d: Date | number) => {
return d instanceof Date;
};
/**
* Returns a function with arity 1 that caches the argument that the function
* is called with and invokes the comparator with both the cached, previous,
* value and the current value. If specified, the initialValue will be passed
* in as the previous value on the first invocation of the returned method.
*
* @template A - The type of the compared value.
* @param comparator - A method to compare
* the previous and next values.
* @param [initialValue] - The initial value to supply to prevValue
* on first call of the method.
*/
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31860
// eslint-disable-next-line @typescript-eslint/naming-convention
export function previousValueComparator<A>(
comparator: (previous: A, next: A) => boolean,
initialValue: A,
) {
let first = true;
let cache: A;
return (value: A) => {
try {
if (first) {
first = false;
return comparator(initialValue ?? value, value);
}
return comparator(cache, value);
} finally {
cache = value;
}
};
}
export function addUrlProtocolPrefix(urlString: string) {
let trimmed = urlString.trim();
if (trimmed.length && !urlLib.parse(trimmed).protocol) {
trimmed = `https://${trimmed}`;
}
if (getValidUrl(trimmed) !== null) {
return trimmed;
}
return null;
}
export function getValidUrl(urlString: string): URL | null {
try {
const url = new URL(urlString);
if (url.hostname.length === 0 || url.pathname.length === 0) {
return null;
}
if (url.hostname !== decodeURIComponent(url.hostname)) {
return null; // will happen if there's a %, a space, or other invalid character in the hostname
}
return url;
} catch (error) {
return null;
}
}
export function isValidEmail(email: string): boolean {
return email.match(/^([\w.%+-]+)@([\w-]+\.)+([\w]{2,})$/iu) !== null;
}
export function isWebUrl(urlString: string): boolean {
const url = getValidUrl(urlString);
return (
url !== null && (url.protocol === 'https:' || url.protocol === 'http:')
);
}
/**
* Checks if an origin string is a web origin (http:// or https://).
* This is used to filter out non-web origins like chrome://, about://, moz-extension://, etc.
*
* @param origin - The origin string to check (e.g., "https://example.com", "chrome://newtab")
* @returns true if the origin starts with http:// or https://, false otherwise
*/
export function isWebOrigin(origin: string | undefined | null): boolean {
if (!origin) {
return false;
}
return origin.startsWith('http://') || origin.startsWith('https://');
}
/**
* Determines whether to emit a MetaMetrics event for a given metaMetricsId.
* Relies on the last 4 characters of the metametricsId. Assumes the IDs are evenly distributed.
* If metaMetricsIds are distributed evenly, this should be a 1% sample rate
*
* @param metaMetricsId - The metametricsId to use for the event.
* @returns Whether to emit the event or not.
*/
export function shouldEmitDappViewedEvent(
metaMetricsId: string | null,
): boolean {
const isFireFox = getPlatform() === PLATFORM_FIREFOX;
if (metaMetricsId === null || isFireFox) {
return false;
}
const lastFourCharacters = metaMetricsId.slice(-4);
const lastFourCharactersAsNumber = parseInt(lastFourCharacters, 16);
return lastFourCharactersAsNumber % 100 === 0;
}
type FormattedTransactionMeta = {
blockHash: string | null;
blockNumber: string | null;
from: string;
to?: string;
hash?: string;
nonce: string;
input: string;
v?: string;
r?: string;
s?: string;
value: string;
gas?: string;
gasPrice?: string;
maxFeePerGas?: string;
maxPriorityFeePerGas?: string;
type: TransactionEnvelopeType;
accessList: AccessList | null;
transactionIndex: string | null;
};
export function formatTxMetaForRpcResult(
txMeta: TransactionMeta,
): FormattedTransactionMeta {
const { r, s, v, hash, txReceipt, txParams } = txMeta;
const {
to,
data,
nonce,
gas,
from,
value,
gasPrice,
accessList,
maxFeePerGas,
maxPriorityFeePerGas,
} = txParams;
const formattedTxMeta: FormattedTransactionMeta = {
v,
r,
s,
to,
gas,
from,
hash,
nonce: `${nonce}`,
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31880
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
input: data || '0x',
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31880
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
value: value || '0x0',
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31880
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
accessList: accessList || null,
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31880
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
blockHash: txReceipt?.blockHash || null,
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31880
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
blockNumber: txReceipt?.blockNumber || null,
// TODO: Fix in https://github.com/MetaMask/metamask-extension/issues/31880
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
transactionIndex: txReceipt?.transactionIndex || null,
type:
maxFeePerGas && maxPriorityFeePerGas
? TransactionEnvelopeType.feeMarket
: TransactionEnvelopeType.legacy,
};
if (maxFeePerGas && maxPriorityFeePerGas) {
formattedTxMeta.gasPrice = maxFeePerGas;
formattedTxMeta.maxFeePerGas = maxFeePerGas;
formattedTxMeta.maxPriorityFeePerGas = maxPriorityFeePerGas;
} else {
formattedTxMeta.gasPrice = gasPrice;
}
return formattedTxMeta;
}
export const isValidAmount = (amount: number | null | undefined): boolean =>
amount !== null && amount !== undefined && !Number.isNaN(amount);
export function formatValue(
value: number | null | undefined,
includeParentheses: boolean,
): string {
if (!isValidAmount(value)) {
return '';
}
const numericValue = value as number;
const sign = numericValue >= 0 ? '+' : '';
const formattedNumber = `${sign}${numericValue.toFixed(2)}%`;
return includeParentheses ? `(${formattedNumber})` : formattedNumber;
}
type MethodData = {
name: string;
params: { type: string }[];
};
export const getMethodDataName = async (
knownMethodData: Record<string, MethodData>,
use4ByteResolution: boolean,
prefixedData: string,
addKnownMethodData: (fourBytePrefix: string, methodData: MethodData) => void,
provider: Provider,
) => {
if (!prefixedData || !use4ByteResolution) {
return null;
}
const fourBytePrefix = prefixedData.slice(0, 10);
if (knownMethodData?.[fourBytePrefix]) {
return knownMethodData?.[fourBytePrefix];
}
const methodData = await getMethodDataAsync(
fourBytePrefix,
use4ByteResolution,
provider,
);
if (methodData?.name) {
addKnownMethodData(fourBytePrefix, methodData as MethodData);
}
return methodData;
};
/**
* Get a boolean value for a string or boolean value.
*
* @param value - The value to convert to a boolean.
* @returns `true` if the value is `'true'` or `true`, otherwise `false`.
* @example
* getBooleanFlag('true'); // true
* getBooleanFlag(true); // true
* getBooleanFlag('false'); // false
* getBooleanFlag(false); // false
*/
export function getBooleanFlag(value: string | boolean | undefined): boolean {
return value === true || value === 'true';
}
type AssetsRatesState = {
metamask: MultichainAssetsRatesControllerState;
};
export function getConversionRatesForNativeAsset({
conversionRates,
chainId,
}: {
conversionRates: AssetsRatesState['metamask']['conversionRates'];
chainId: string;
}): (AssetConversion & { marketData?: FungibleAssetMarketData }) | null {
// Return early if conversionRates is falsy
if (!conversionRates) {
return null;
}
let conversionRateResult = null;
Object.entries(conversionRates).forEach(
([caip19Identifier, conversionRate]) => {
const { assetNamespace, chainId: caipChainId } = parseCaipAssetType(
caip19Identifier as CaipAssetType,
);
if (assetNamespace === 'slip44' && caipChainId === chainId) {
conversionRateResult = conversionRate;
}
},
);
return conversionRateResult;
}
// Cache for known domains
let knownDomainsSet: Set<string> | null = null;
let initPromise: Promise<void> | null = null;
/**
* Initialize the set of known domains from the chains list
*/
export async function initializeRpcProviderDomains(): Promise<void> {
if (initPromise) {
return initPromise;
}
initPromise = (async () => {
try {
const chainsList = await getSafeChainsListFromCacheOnly();
knownDomainsSet = new Set<string>();
for (const chain of chainsList) {
if (chain.rpc && Array.isArray(chain.rpc)) {
for (const rpcUrl of chain.rpc) {
try {
const url = new URL(rpcUrl);
knownDomainsSet.add(url.hostname);
} catch (e) {
// Skip invalid URLs
continue;
}
}
}
}
} catch (error) {
console.error('Error initializing known domains:', error);
knownDomainsSet = new Set<string>();
}
})();
return initPromise;
}
/**
* Check if a domain is in the known domains list
*
* @param domain - The domain to check
*/
export function isKnownDomain(domain: string): boolean {
return knownDomainsSet?.has(domain?.toLowerCase()) ?? false;
}
/**
* Extracts the domain from an RPC endpoint URL with privacy considerations
*
* @param rpcUrl - The RPC endpoint URL
* @param knownDomainsForTesting - Optional Set of known domains for testing purposes
* @returns The domain for known providers, 'private' for private/custom networks, or 'invalid' for invalid URLs
*/
export function extractRpcDomain(
rpcUrl: string,
knownDomainsForTesting?: Set<string>,
): string {
if (!rpcUrl) {
return 'invalid';
}
try {
// Try to parse the URL directly
let url;
try {
url = new URL(rpcUrl);
} catch (e) {
// If parsing fails, check if it looks like a domain without protocol
if (rpcUrl.includes('://')) {
return 'invalid';
}
// Try adding https:// prefix for domain-like strings
try {
url = new URL(`https://${rpcUrl}`);
} catch (e2) {
return 'invalid';
}
}
// Use the provided test domains if available, otherwise use isKnownDomain
if (knownDomainsForTesting) {
if (knownDomainsForTesting.has(url.hostname.toLowerCase())) {
return url.hostname.toLowerCase();
}
} else if (isKnownDomain(url.hostname)) {
return url.hostname.toLowerCase();
}
return 'private';
} catch (error) {
return 'invalid';
}
}