-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
Copy pathUtils.js
541 lines (479 loc) · 12.9 KB
/
Utils.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
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
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file Utils.js
* @author Fabian Vogelsteller <[email protected]>
* @author Prince Sinha <[email protected]>
* @date 2017
*/
import isBoolean from 'lodash/isBoolean';
import isString from 'lodash/isString';
import isNumber from 'lodash/isNumber';
import isObject from 'lodash/isObject';
import isNull from 'lodash/isNull';
import numberToBN from 'number-to-bn';
import utf8 from 'utf8';
import Hash from 'eth-lib/lib/hash';
import BN from 'bn.js';
/**
* Returns true if object is BN, otherwise false
*
* @method isBN
*
* @param {Object} object
*
* @returns {Boolean}
*/
export const isBN = (object) => {
return BN.isBN(object);
};
/**
* Returns true if object is BigNumber, otherwise false
*
* @method isBigNumber
*
* @param {Object} object
*
* @returns {Boolean}
*/
export const isBigNumber = (object) => {
return object && object.constructor && object.constructor.name === 'BigNumber';
};
/**
* Takes an input and transforms it into an BN
*
* @method toBN
*
* @param {Number|String|BN} number, string, HEX string or BN
*
* @returns {BN} BN
*/
export const toBN = (number) => {
try {
return numberToBN(number);
} catch (error) {
throw new Error(`${error} Given value: "${number}"`);
}
};
/**
* Takes and input transforms it into BN and if it is negative value, into two's complement
*
* @method toTwosComplement
*
* @param {Number|String|BN} number
*
* @returns {String}
*/
export const toTwosComplement = (number) => {
return `0x${toBN(number)
.toTwos(256)
.toString(16, 64)}`;
};
/**
* Checks if the given string is an address
*
* @method isAddress
*
* @param {String} address the given HEX address
*
* @param {Number} chainId to define checksum behavior
*
* @returns {Boolean}
*/
export const isAddress = (address, chainId = null) => {
// check if it has the basic requirements of an address
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
return false;
// If it's ALL lowercase or ALL upppercase
} else if (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address)) {
return true;
// Otherwise check each case
} else {
return checkAddressChecksum(address, chainId);
}
};
/**
* Removes prefix from address if exists.
*
* @method stripHexPrefix
*
* @param {string} string
*
* @returns {string} address without prefix
*/
export const stripHexPrefix = (string) => {
return string.startsWith('0x') || string.startsWith('0X') ? string.slice(2) : string;
};
/**
* Checks if the given string is a checksummed address
*
* @method checkAddressChecksum
*
* @param {String} address the given HEX address
*
* @param {number} chain where checksummed address should be valid.
*
* @returns {Boolean}
*/
export const checkAddressChecksum = (address, chainId = null) => {
const stripAddress = stripHexPrefix(address).toLowerCase();
const prefix = chainId != null ? chainId.toString() + '0x' : '';
const keccakHash = Hash.keccak256(prefix + stripAddress)
.toString('hex')
.replace(/^0x/i, '');
for (let i = 0; i < stripAddress.length; i++) {
let output = parseInt(keccakHash[i], 16) >= 8 ? stripAddress[i].toUpperCase() : stripAddress[i];
if (stripHexPrefix(address)[i] !== output) {
return false;
}
}
return true;
};
/**
* Should be called to pad string to expected length
*
* @method leftPad
*
* @param {String} string to be padded
* @param {Number} chars that result string should have
* @param {String} sign, by default 0
*
* @returns {String} left aligned string
*/
export const leftPad = (string, chars, sign) => {
const hasPrefix = /^0x/i.test(string) || typeof string === 'number';
string = string.toString(16).replace(/^0x/i, '');
const padding = chars - string.length + 1 >= 0 ? chars - string.length + 1 : 0;
return (hasPrefix ? '0x' : '') + new Array(padding).join(sign || '0') + string;
};
/**
* Should be called to pad string to expected length
*
* @method rightPad
*
* @param {String} string to be padded
* @param {Number} chars that result string should have
* @param {String} sign, by default 0
*
* @returns {String} right aligned string
*/
export const rightPad = (string, chars, sign) => {
const hasPrefix = /^0x/i.test(string) || typeof string === 'number';
string = string.toString(16).replace(/^0x/i, '');
const padding = chars - string.length + 1 >= 0 ? chars - string.length + 1 : 0;
return (hasPrefix ? '0x' : '') + string + new Array(padding).join(sign || '0');
};
/**
* Should be called to get hex representation (prefixed by 0x) of utf8 string
*
* @method utf8ToHex
*
* @param {String} value
*
* @returns {String} hex representation of input string
*/
export const utf8ToHex = (value) => {
value = utf8.encode(value);
let hex = '';
/* eslint-disable no-control-regex */
// remove \u0000 padding from either side
value = value.replace(/^(?:\u0000)*/, '');
value = value
.split('')
.reverse()
.join('');
value = value.replace(/^(?:\u0000)*/, '');
value = value
.split('')
.reverse()
.join('');
/* eslint-enable no-control-regex */
for (let i = 0; i < value.length; i++) {
const code = value.charCodeAt(i);
// if (code !== 0) {
const n = code.toString(16);
hex += n.length < 2 ? `0${n}` : n;
// }
}
return `0x${hex}`;
};
/**
* Should be called to get utf8 from it's hex representation
*
* @method hexToUtf8
*
* @param {String} hex
*
* @returns {String} ascii string representation of hex value
*/
export const hexToUtf8 = (hex) => {
if (!isHexStrict(hex)) throw new Error(`The parameter "${hex}" must be a valid HEX string.`);
let string = '';
let code = 0;
hex = hex.replace(/^0x/i, '');
// remove 00 padding from either side
hex = hex.replace(/^(?:00)*/, '');
hex = hex
.split('')
.reverse()
.join('');
hex = hex.replace(/^(?:00)*/, '');
hex = hex
.split('')
.reverse()
.join('');
const l = hex.length;
for (let i = 0; i < l; i += 2) {
code = parseInt(hex.substr(i, 2), 16);
// if (code !== 0) {
string += String.fromCharCode(code);
// }
}
return utf8.decode(string);
};
/**
* Converts value to it's number representation
*
* @method hexToNumber
*
* @param {String|Number|BN} value
*
* @returns {Number}
*/
export const hexToNumber = (value) => {
if (!value) {
return value;
}
return toBN(value).toNumber();
};
/**
* Converts value to it's decimal representation in string
*
* @method hexToNumberString
*
* @param {String|Number|BN} value
*
* @returns {String}
*/
export const hexToNumberString = (value) => {
if (!value) return value;
if (isString(value)) {
if (!isHexStrict(value)) throw new Error(`Given value "${value}" is not a valid hex string.`);
}
return toBN(value).toString(10);
};
/**
* Converts value to it's hex representation
*
* @method numberToHex
*
* @param {String|Number|BN} value
*
* @returns {String}
*/
export const numberToHex = (value) => {
if (isNull(value) || typeof value === 'undefined') {
return value;
}
if (!isFinite(value) && !isHexStrict(value)) {
throw new Error(`Given input "${value}" is not a number.`);
}
const number = toBN(value);
const result = number.toString(16);
return number.lt(new BN(0)) ? `-0x${result.substr(1)}` : `0x${result}`;
};
/**
* Convert a byte array to a hex string
*
* Note: Implementation from crypto-js
*
* @method bytesToHex
*
* @param {Array} bytes
*
* @returns {String} the hex string
*/
export const bytesToHex = (bytes) => {
let hex = [];
for (let i = 0; i < bytes.length; i++) {
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xf).toString(16));
}
return `0x${hex.join('').replace(/^0+/, '')}`;
};
/**
* Convert a hex string to a byte array
*
* Note: Implementation from crypto-js
*
* @method hexToBytes
*
* @param {String} hex
*
* @returns {Array} the byte array
*/
export const hexToBytes = (hex) => {
hex = hex.toString(16);
if (!isHexStrict(hex)) {
throw new Error(`Given value "${hex}" is not a valid hex string.`);
}
hex = hex.replace(/^0x/i, '');
hex = hex.length % 2 ? '0' + hex : hex;
let bytes = [];
for (let c = 0; c < hex.length; c += 2) {
bytes.push(parseInt(hex.substr(c, 2), 16));
}
return bytes;
};
/**
* Auto converts any given value into it's hex representation.
* And even stringifys objects before.
*
* @method toHex
*
* @param {String|Number|BN|Object} value
* @param {Boolean} returnType
*
* @returns {String}
*/
export const toHex = (value, returnType) => {
if (isAddress(value)) {
return returnType ? 'address' : `0x${value.toLowerCase().replace(/^0x/i, '')}`;
}
if (isBoolean(value)) {
return returnType ? 'bool' : value ? '0x01' : '0x00';
}
if (isObject(value) && !isBigNumber(value) && !isBN(value)) {
return returnType ? 'string' : utf8ToHex(JSON.stringify(value));
}
// if its a negative number, pass it through numberToHex
if (isString(value)) {
if (value.indexOf('-0x') === 0 || value.indexOf('-0X') === 0) {
return returnType ? 'int256' : numberToHex(value);
} else if (value.indexOf('0x') === 0 || value.indexOf('0X') === 0) {
return returnType ? 'bytes' : value;
} else if (!isFinite(value)) {
return returnType ? 'string' : utf8ToHex(value);
}
}
return returnType ? (value < 0 ? 'int256' : 'uint256') : numberToHex(value);
};
/**
* Check if string is HEX, requires a 0x in front
*
* @method isHexStrict
*
* @param {String} hex to be checked
*
* @returns {Boolean}
*/
export const isHexStrict = (hex) => {
return (isString(hex) || isNumber(hex)) && /^(-)?0x[0-9a-f]*$/i.test(hex);
};
/**
* Check if string is HEX
*
* @method isHex
*
* @param {String} hex to be checked
*
* @returns {Boolean}
*/
export const isHex = (hex) => {
return (isString(hex) || isNumber(hex)) && /^(-0x|0x)?[0-9a-f]*$/i.test(hex);
};
/**
* Returns true if given string is a valid Ethereum block header bloom.
*
* TODO UNDOCUMENTED
*
* @method isBloom
*
* @param {String} bloom encoded bloom filter
*
* @returns {Boolean}
*/
export const isBloom = (bloom) => {
if (!/^(0x)?[0-9a-f]{512}$/i.test(bloom)) {
return false;
} else if (/^(0x)?[0-9a-f]{512}$/.test(bloom) || /^(0x)?[0-9A-F]{512}$/.test(bloom)) {
return true;
}
return false;
};
/**
* Returns true if given string is a valid log topic.
*
* TODO UNDOCUMENTED
*
* @method isTopic
*
* @param {String} topic encoded topic
*
* @returns {Boolean}
*/
export const isTopic = (topic) => {
if (!/^(0x)?[0-9a-f]{64}$/i.test(topic)) {
return false;
} else if (/^(0x)?[0-9a-f]{64}$/.test(topic) || /^(0x)?[0-9A-F]{64}$/.test(topic)) {
return true;
}
return false;
};
/**
* Hashes values to a keccak256 hash using keccak 256
*
* To hash a HEX string the hex must have 0x in front.
*
* @method keccak256
* @return {String} the keccak256 string
*/
const KECCAK256_NULL_S = '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470';
export const keccak256 = (value) => {
if (isHexStrict(value) && /^0x/i.test(value.toString())) {
value = hexToBytes(value);
}
const returnValue = Hash.keccak256(value); // jshint ignore:line
if (returnValue === KECCAK256_NULL_S) {
return null;
} else {
return returnValue;
}
};
// expose the under the hood keccak256
keccak256._Hash = Hash;
/**
* Gets the r,s,v values from a signature
*
* @method getSignatureParameters
*
* @param {String} ECDSA signature
*
* @return {Object} with r,s,v values
*/
export const getSignatureParameters = (signature) => {
if (!isHexStrict(signature)) {
throw new Error(`Given value "${signature}" is not a valid hex string.`);
}
const r = signature.slice(0, 66);
const s = `0x${signature.slice(66, 130)}`;
let v = `0x${signature.slice(130, 132)}`;
v = hexToNumber(v);
if (![27, 28].includes(v)) v += 27;
return {
r,
s,
v
};
};