-
-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathutil.ts
747 lines (699 loc) · 21.6 KB
/
util.ts
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
import {
addHexPrefix,
isValidAddress,
isHexString,
bufferToHex,
BN,
toChecksumAddress,
} from 'ethereumjs-util';
import { stripHexPrefix } from 'ethjs-util';
import { fromWei, toWei } from 'ethjs-unit';
import { ethErrors } from 'eth-rpc-errors';
import ensNamehash from 'eth-ens-namehash';
import { TYPED_MESSAGE_SCHEMA, typedSignatureHash } from 'eth-sig-util';
import { validate } from 'jsonschema';
import {
Transaction,
FetchAllOptions,
GasPriceValue,
FeeMarketEIP1559Values,
} from './transaction/TransactionController';
import { MessageParams } from './message-manager/MessageManager';
import { PersonalMessageParams } from './message-manager/PersonalMessageManager';
import { TypedMessageParams } from './message-manager/TypedMessageManager';
import { Token } from './assets/TokenRatesController';
import { MAINNET } from './constants';
const hexRe = /^[0-9A-Fa-f]+$/gu;
const NORMALIZERS: { [param in keyof Transaction]: any } = {
data: (data: string) => addHexPrefix(data),
from: (from: string) => addHexPrefix(from).toLowerCase(),
gas: (gas: string) => addHexPrefix(gas),
gasPrice: (gasPrice: string) => addHexPrefix(gasPrice),
nonce: (nonce: string) => addHexPrefix(nonce),
to: (to: string) => addHexPrefix(to).toLowerCase(),
value: (value: string) => addHexPrefix(value),
maxFeePerGas: (maxFeePerGas: string) => addHexPrefix(maxFeePerGas),
maxPriorityFeePerGas: (maxPriorityFeePerGas: string) =>
addHexPrefix(maxPriorityFeePerGas),
estimatedBaseFee: (maxPriorityFeePerGas: string) =>
addHexPrefix(maxPriorityFeePerGas),
};
/**
* Converts a BN object to a hex string with a '0x' prefix
*
* @param inputBn - BN instance to convert to a hex string
* @returns - '0x'-prefixed hex string
*
*/
export function BNToHex(inputBn: any) {
return addHexPrefix(inputBn.toString(16));
}
/**
* Used to multiply a BN by a fraction
*
* @param targetBN - Number to multiply by a fraction
* @param numerator - Numerator of the fraction multiplier
* @param denominator - Denominator of the fraction multiplier
* @returns - Product of the multiplication
*/
export function fractionBN(
targetBN: any,
numerator: number | string,
denominator: number | string,
) {
const numBN = new BN(numerator);
const denomBN = new BN(denominator);
return targetBN.mul(numBN).div(denomBN);
}
/**
* Used to convert a base-10 number from GWEI to WEI. Can handle numbers with decimal parts
*
* @param n - The base 10 number to convert to WEI
* @returns - The number in WEI, as a BN
*/
export function gweiDecToWEIBN(n: number | string) {
if (Number.isNaN(n)) {
return new BN(0);
}
const parts = n.toString().split('.');
const wholePart = parts[0] || '0';
let decimalPart = parts[1] || '';
if (!decimalPart) {
return toWei(wholePart, 'gwei');
}
if (decimalPart.length <= 9) {
return toWei(`${wholePart}.${decimalPart}`, 'gwei');
}
const decimalPartToRemove = decimalPart.slice(9);
const decimalRoundingDigit = decimalPartToRemove[0];
decimalPart = decimalPart.slice(0, 9);
let wei = toWei(`${wholePart}.${decimalPart}`, 'gwei');
if (Number(decimalRoundingDigit) >= 5) {
wei = wei.add(new BN(1));
}
return wei;
}
/**
* Used to convert values from wei hex format to dec gwei format
* @param hex - value in hex wei
* @returns - value in dec gwei as string
*/
export function weiHexToGweiDec(hex: string) {
const hexWei = new BN(stripHexPrefix(hex), 16);
return fromWei(hexWei, 'gwei').toString(10);
}
/**
* Return a URL that can be used to obtain ETH for a given network
*
* @param networkCode - Network code of desired network
* @param address - Address to deposit obtained ETH
* @param amount - How much ETH is desired
* @returns - URL to buy ETH based on network
*/
export function getBuyURL(
networkCode = '1',
address?: string,
amount = 5,
): string | undefined {
switch (networkCode) {
case '1':
return `https://buy.coinbase.com/?code=9ec56d01-7e81-5017-930c-513daa27bb6a&amount=${amount}&address=${address}&crypto_currency=ETH`;
case '3':
return 'https://faucet.metamask.io/';
case '4':
return 'https://www.rinkeby.io/';
case '5':
return 'https://goerli-faucet.slock.it/';
case '42':
return 'https://github.com/kovan-testnet/faucet';
default:
return undefined;
}
}
/**
* Return a URL that can be used to fetch ETH transactions
*
* @param networkType - Network type of desired network
* @param address - Address to get the transactions from
* @param fromBlock? - Block from which transactions are needed
* @returns - URL to fetch the transactions from
*/
export function getEtherscanApiUrl(
networkType: string,
address: string,
action: string,
fromBlock?: string,
etherscanApiKey?: string,
): string {
let etherscanSubdomain = 'api';
if (networkType !== MAINNET) {
etherscanSubdomain = `api-${networkType}`;
}
const apiUrl = `https://${etherscanSubdomain}.etherscan.io`;
let url = `${apiUrl}/api?module=account&action=${action}&address=${address}&tag=latest&page=1`;
if (fromBlock) {
url += `&startBlock=${fromBlock}`;
}
if (etherscanApiKey) {
url += `&apikey=${etherscanApiKey}`;
}
return url;
}
/**
* Handles the fetch of incoming transactions
*
* @param networkType - Network type of desired network
* @param address - Address to get the transactions from
* @param opt? - Object that can contain fromBlock and Etherscan service API key
* @returns - Responses for both ETH and ERC20 token transactions
*/
export async function handleTransactionFetch(
networkType: string,
address: string,
opt?: FetchAllOptions,
): Promise<[{ [result: string]: [] }, { [result: string]: [] }]> {
// transactions
const etherscanTxUrl = getEtherscanApiUrl(
networkType,
address,
'txlist',
opt?.fromBlock,
opt?.etherscanApiKey,
);
const etherscanTxResponsePromise = handleFetch(etherscanTxUrl);
// tokens
const etherscanTokenUrl = getEtherscanApiUrl(
networkType,
address,
'tokentx',
opt?.fromBlock,
opt?.etherscanApiKey,
);
const etherscanTokenResponsePromise = handleFetch(etherscanTokenUrl);
let [etherscanTxResponse, etherscanTokenResponse] = await Promise.all([
etherscanTxResponsePromise,
etherscanTokenResponsePromise,
]);
if (
etherscanTxResponse.status === '0' ||
etherscanTxResponse.result.length <= 0
) {
etherscanTxResponse = { result: [] };
}
if (
etherscanTokenResponse.status === '0' ||
etherscanTokenResponse.result.length <= 0
) {
etherscanTokenResponse = { result: [] };
}
return [etherscanTxResponse, etherscanTokenResponse];
}
/**
* Converts a hex string to a BN object
*
* @param inputHex - Number represented as a hex string
* @returns - A BN instance
*
*/
export function hexToBN(inputHex: string) {
return new BN(stripHexPrefix(inputHex), 16);
}
/**
* A helper function that converts hex data to human readable string
*
* @param hex - The hex string to convert to string
* @returns - A human readable string conversion
*
*/
export function hexToText(hex: string) {
try {
const stripped = stripHexPrefix(hex);
const buff = Buffer.from(stripped, 'hex');
return buff.toString('utf8');
} catch (e) {
/* istanbul ignore next */
return hex;
}
}
/**
* Normalizes properties on a Transaction object
*
* @param transaction - Transaction object to normalize
* @returns - Normalized Transaction object
*/
export function normalizeTransaction(transaction: Transaction) {
const normalizedTransaction: Transaction = { from: '' };
let key: keyof Transaction;
for (key in NORMALIZERS) {
if (transaction[key as keyof Transaction]) {
normalizedTransaction[key] = NORMALIZERS[key](transaction[key]) as never;
}
}
return normalizedTransaction;
}
/**
* Execute and return an asynchronous operation without throwing errors
*
* @param operation - Function returning a Promise
* @param logError - Determines if the error should be logged
* @param retry - Function called if an error is caught
* @returns - Promise resolving to the result of the async operation
*/
export async function safelyExecute(
operation: () => Promise<any>,
logError = false,
retry?: (error: Error) => void,
) {
try {
return await operation();
} catch (error) {
/* istanbul ignore next */
if (logError) {
console.error(error);
}
retry?.(error);
return undefined;
}
}
/**
* Execute and return an asynchronous operation with a timeout
*
* @param operation - Function returning a Promise
* @param logError - Determines if the error should be logged
* @param retry - Function called if an error is caught
* @param timeout - Timeout to fail the operation
* @returns - Promise resolving to the result of the async operation
*/
export async function safelyExecuteWithTimeout(
operation: () => Promise<any>,
logError = false,
timeout = 500,
) {
try {
return await Promise.race([
operation(),
new Promise<void>((_, reject) =>
setTimeout(() => {
reject(new Error('timeout'));
}, timeout),
),
]);
} catch (error) {
/* istanbul ignore next */
if (logError) {
console.error(error);
}
return undefined;
}
}
export function toChecksumHexAddress(address: string) {
const hexPrefixed = addHexPrefix(address);
if (!isHexString(hexPrefixed)) {
// Version 5.1 of ethereumjs-utils would have returned '0xY' for input 'y'
// but we shouldn't waste effort trying to change case on a clearly invalid
// string. Instead just return the hex prefixed original string which most
// closely mimics the original behavior.
return hexPrefixed;
}
return toChecksumAddress(hexPrefixed);
}
/**
* Validates that the input is a hex address. This utility method is a thin
* wrapper around ethereumjs-util.isValidAddress, with the exception that it
* does not throw an error when provided values that are not hex strings. In
* addition, and by default, this method will return true for hex strings that
* meet the length requirement of a hex address, but are not prefixed with `0x`
* Finally, if the mixedCaseUseChecksum flag is true and a mixed case string is
* provided this method will validate it has the proper checksum formatting.
* @param {string} possibleAddress - Input parameter to check against
* @param {Object} [options] - options bag
* @param {boolean} [options.allowNonPrefixed] - If true will first ensure '0x'
* is prepended to the string
* @returns {boolean} whether or not the input is a valid hex address
*/
export function isValidHexAddress(
possibleAddress: string,
{ allowNonPrefixed = true } = {},
) {
const addressToCheck = allowNonPrefixed
? addHexPrefix(possibleAddress)
: possibleAddress;
if (!isHexString(addressToCheck)) {
return false;
}
return isValidAddress(addressToCheck);
}
/**
* Validates a Transaction object for required properties and throws in
* the event of any validation error.
*
* @param transaction - Transaction object to validate
*/
export function validateTransaction(transaction: Transaction) {
if (
!transaction.from ||
typeof transaction.from !== 'string' ||
!isValidHexAddress(transaction.from)
) {
throw new Error(
`Invalid "from" address: ${transaction.from} must be a valid string.`,
);
}
if (transaction.to === '0x' || transaction.to === undefined) {
if (transaction.data) {
delete transaction.to;
} else {
throw new Error(
`Invalid "to" address: ${transaction.to} must be a valid string.`,
);
}
} else if (
transaction.to !== undefined &&
!isValidHexAddress(transaction.to)
) {
throw new Error(
`Invalid "to" address: ${transaction.to} must be a valid string.`,
);
}
if (transaction.value !== undefined) {
const value = transaction.value.toString();
if (value.includes('-')) {
throw new Error(`Invalid "value": ${value} is not a positive number.`);
}
if (value.includes('.')) {
throw new Error(
`Invalid "value": ${value} number must be denominated in wei.`,
);
}
const intValue = parseInt(transaction.value, 10);
const isValid =
Number.isFinite(intValue) &&
!Number.isNaN(intValue) &&
!isNaN(Number(value)) &&
Number.isSafeInteger(intValue);
if (!isValid) {
throw new Error(
`Invalid "value": ${value} number must be a valid number.`,
);
}
}
}
/**
* A helper function that converts rawmessageData buffer data to a hex, or just returns the data if
* it is already formatted as a hex.
*
* @param data - The buffer data to convert to a hex
* @returns - A hex string conversion of the buffer data
*
*/
export function normalizeMessageData(data: string) {
try {
const stripped = stripHexPrefix(data);
if (stripped.match(hexRe)) {
return addHexPrefix(stripped);
}
} catch (e) {
/* istanbul ignore next */
}
return bufferToHex(Buffer.from(data, 'utf8'));
}
/**
* Validates a PersonalMessageParams and MessageParams objects for required properties and throws in
* the event of any validation error.
*
* @param messageData - PersonalMessageParams object to validate
*/
export function validateSignMessageData(
messageData: PersonalMessageParams | MessageParams,
) {
const { from, data } = messageData;
if (!from || typeof from !== 'string' || !isValidHexAddress(from)) {
throw new Error(`Invalid "from" address: ${from} must be a valid string.`);
}
if (!data || typeof data !== 'string') {
throw new Error(`Invalid message "data": ${data} must be a valid string.`);
}
}
/**
* Validates a TypedMessageParams object for required properties and throws in
* the event of any validation error for eth_signTypedMessage_V1.
*
* @param messageData - TypedMessageParams object to validate
* @param activeChainId - Active chain id
*/
export function validateTypedSignMessageDataV1(
messageData: TypedMessageParams,
) {
if (
!messageData.from ||
typeof messageData.from !== 'string' ||
!isValidHexAddress(messageData.from)
) {
throw new Error(
`Invalid "from" address: ${messageData.from} must be a valid string.`,
);
}
if (!messageData.data || !Array.isArray(messageData.data)) {
throw new Error(
`Invalid message "data": ${messageData.data} must be a valid array.`,
);
}
try {
// typedSignatureHash will throw if the data is invalid.
typedSignatureHash(messageData.data as any);
} catch (e) {
throw new Error(`Expected EIP712 typed data.`);
}
}
/**
* Validates a TypedMessageParams object for required properties and throws in
* the event of any validation error for eth_signTypedMessage_V3.
*
* @param messageData - TypedMessageParams object to validate
*/
export function validateTypedSignMessageDataV3(
messageData: TypedMessageParams,
) {
if (
!messageData.from ||
typeof messageData.from !== 'string' ||
!isValidHexAddress(messageData.from)
) {
throw new Error(
`Invalid "from" address: ${messageData.from} must be a valid string.`,
);
}
if (!messageData.data || typeof messageData.data !== 'string') {
throw new Error(
`Invalid message "data": ${messageData.data} must be a valid array.`,
);
}
let data;
try {
data = JSON.parse(messageData.data);
} catch (e) {
throw new Error('Data must be passed as a valid JSON string.');
}
const validation = validate(data, TYPED_MESSAGE_SCHEMA);
if (validation.errors.length > 0) {
throw new Error(
'Data must conform to EIP-712 schema. See https://git.io/fNtcx.',
);
}
}
/**
* Validates a ERC20 token to be added with EIP747.
*
* @param token - Token object to validate
*/
export function validateTokenToWatch(token: Token) {
const { address, symbol, decimals } = token;
if (!address || !symbol || typeof decimals === 'undefined') {
throw ethErrors.rpc.invalidParams(
`Must specify address, symbol, and decimals.`,
);
}
if (typeof symbol !== 'string') {
throw ethErrors.rpc.invalidParams(`Invalid symbol: not a string.`);
}
if (symbol.length > 11) {
throw ethErrors.rpc.invalidParams(
`Invalid symbol "${symbol}": longer than 11 characters.`,
);
}
const numDecimals = parseInt((decimals as unknown) as string, 10);
if (isNaN(numDecimals) || numDecimals > 36 || numDecimals < 0) {
throw ethErrors.rpc.invalidParams(
`Invalid decimals "${decimals}": must be 0 <= 36.`,
);
}
if (!isValidHexAddress(address)) {
throw ethErrors.rpc.invalidParams(`Invalid address "${address}".`);
}
}
/**
* Returns wether the given code corresponds to a smart contract
*
* @returns {string} - Corresponding code to review
*/
export function isSmartContractCode(code: string) {
/* istanbul ignore if */
if (!code) {
return false;
}
// Geth will return '0x', and ganache-core v2.2.1 will return '0x0'
const smartContractCode = code !== '0x' && code !== '0x0';
return smartContractCode;
}
/**
* Execute fetch and verify that the response was successful
*
* @param request - Request information
* @param options - Options
* @returns - Promise resolving to the fetch response
*/
export async function successfulFetch(request: string, options?: RequestInit) {
const response = await fetch(request, options);
if (!response.ok) {
throw new Error(
`Fetch failed with status '${response.status}' for request '${request}'`,
);
}
return response;
}
/**
* Execute fetch and return object response
*
* @param request - Request information
* @param options - Options
* @returns - Promise resolving to the result object of fetch
*/
export async function handleFetch(request: string, options?: RequestInit) {
const response = await successfulFetch(request, options);
const object = await response.json();
return object;
}
/**
* Fetch that fails after timeout
*
* @param url - Url to fetch
* @param options - Options to send with the request
* @param timeout - Timeout to fail request
*
* @returns - Promise resolving the request
*/
export async function timeoutFetch(
url: string,
options?: RequestInit,
timeout = 500,
): Promise<Response> {
return Promise.race([
successfulFetch(url, options),
new Promise<Response>((_, reject) =>
setTimeout(() => {
reject(new Error('timeout'));
}, timeout),
),
]);
}
/**
* Normalizes the given ENS name.
*
* @param {string} ensName - The ENS name
*
* @returns - the normalized ENS name string
*/
export function normalizeEnsName(ensName: string): string | null {
if (ensName && typeof ensName === 'string') {
try {
const normalized = ensNamehash.normalize(ensName.trim());
// this regex is only sufficient with the above call to ensNamehash.normalize
// TODO: change 7 in regex to 3 when shorter ENS domains are live
if (normalized.match(/^(([\w\d-]+)\.)*[\w\d-]{7,}\.(eth|test)$/u)) {
return normalized;
}
} catch (_) {
// do nothing
}
}
return null;
}
/**
* Wrapper method to handle EthQuery requests
*
* @param ethQuery - EthQuery object initialized with a provider
* @param method - Method to request
* @param args - Arguments to send
*
* @returns - Promise resolving the request
*/
export function query(
ethQuery: any,
method: string,
args: any[] = [],
): Promise<any> {
return new Promise((resolve, reject) => {
ethQuery[method](...args, (error: Error, result: any) => {
if (error) {
reject(error);
return;
}
resolve(result);
});
});
}
/**
* Checks if a transaction is EIP-1559 by checking for the existence of
* maxFeePerGas and maxPriorityFeePerGas within its parameters
*
* @param transaction - Transaction object to add
* @returns - Boolean that is true if the transaction is EIP-1559 (has maxFeePerGas and maxPriorityFeePerGas), otherwise returns false
*/
export const isEIP1559Transaction = (transaction: Transaction): boolean => {
const hasOwnProp = (obj: Transaction, key: string) =>
Object.prototype.hasOwnProperty.call(obj, key);
return (
hasOwnProp(transaction, 'maxFeePerGas') &&
hasOwnProp(transaction, 'maxPriorityFeePerGas')
);
};
export const convertPriceToDecimal = (value: string | undefined): number =>
parseInt(value === undefined ? '0x0' : value, 16);
export const getIncreasedPriceHex = (value: number, rate: number): string =>
addHexPrefix(`${parseInt(`${value * rate}`, 10).toString(16)}`);
export const getIncreasedPriceFromExisting = (
value: string | undefined,
rate: number,
): string => {
return getIncreasedPriceHex(convertPriceToDecimal(value), rate);
};
export const validateGasValues = (
gasValues: GasPriceValue | FeeMarketEIP1559Values,
) => {
Object.keys(gasValues).forEach((key) => {
const value = (gasValues as any)[key];
if (typeof value !== 'string' || !isHexString(value)) {
throw new TypeError(
`expected hex string for ${key} but received: ${value}`,
);
}
});
};
export const isFeeMarketEIP1559Values = (
gasValues?: GasPriceValue | FeeMarketEIP1559Values,
): gasValues is FeeMarketEIP1559Values =>
(gasValues as FeeMarketEIP1559Values)?.maxFeePerGas !== undefined ||
(gasValues as FeeMarketEIP1559Values)?.maxPriorityFeePerGas !== undefined;
export const isGasPriceValue = (
gasValues?: GasPriceValue | FeeMarketEIP1559Values,
): gasValues is GasPriceValue =>
(gasValues as GasPriceValue)?.gasPrice !== undefined;
export function validateMinimumIncrease(proposed: string, min: string) {
const proposedDecimal = convertPriceToDecimal(proposed);
const minDecimal = convertPriceToDecimal(min);
if (proposedDecimal >= minDecimal) {
return proposed;
}
const errorMsg = `The proposed value: ${proposedDecimal} should meet or exceed the minimum value: ${minDecimal}`;
throw new Error(errorMsg);
}