-
Notifications
You must be signed in to change notification settings - Fork 5k
/
utils.js
439 lines (374 loc) · 11 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
/*
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 <fabian@ethereum.org>
* @date 2017
*/
var _ = require('underscore');
var BN = require('bn.js');
var numberToBN = require('number-to-bn');
var utf8 = require('utf8');
var Hash = require("eth-lib/lib/hash");
/**
* Returns true if object is BN, otherwise false
*
* @method isBN
* @param {Object} object
* @return {Boolean}
*/
var isBN = function (object) {
return object instanceof BN ||
(object && object.constructor && object.constructor.name === 'BN');
};
/**
* Returns true if object is BigNumber, otherwise false
*
* @method isBigNumber
* @param {Object} object
* @return {Boolean}
*/
var isBigNumber = function (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
* @return {BN} BN
*/
var toBN = function(number){
try {
return numberToBN.apply(null, arguments);
} catch(e) {
throw new Error(e + ' Given value: "'+ number +'"');
}
};
/**
* Checks if the given string is an address
*
* @method isAddress
* @param {String} address the given HEX address
* @return {Boolean}
*/
var isAddress = function (address) {
// 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);
}
};
/**
* Checks if the given string is a checksummed address
*
* @method checkAddressChecksum
* @param {String} address the given HEX address
* @return {Boolean}
*/
var checkAddressChecksum = function (address) {
// Check each case
address = address.replace(/^0x/i,'');
var addressHash = sha3(address.toLowerCase()).replace(/^0x/i,'');
for (var i = 0; i < 40; i++ ) {
// the nth letter should be uppercase if the nth digit of casemap is 1
if ((parseInt(addressHash[i], 16) > 7 && address[i].toUpperCase() !== address[i]) || (parseInt(addressHash[i], 16) <= 7 && address[i].toLowerCase() !== address[i])) {
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} right aligned string
*/
var leftPad = function (string, chars, sign) {
var hasPrefix = /^0x/i.test(string) || typeof string === 'number';
string = string.toString(16).replace(/^0x/i,'');
var padding = (chars - string.length + 1 >= 0) ? chars - string.length + 1 : 0;
return (hasPrefix ? '0x' : '') + new Array(padding).join(sign ? 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
*/
var rightPad = function (string, chars, sign) {
var hasPrefix = /^0x/i.test(string) || typeof string === 'number';
string = string.toString(16).replace(/^0x/i,'');
var padding = (chars - string.length + 1 >= 0) ? chars - string.length + 1 : 0;
return (hasPrefix ? '0x' : '') + string + (new Array(padding).join(sign ? sign : "0"));
};
/**
* Should be called to get hex representation (prefixed by 0x) of utf8 string
*
* @method utf8ToHex
* @param {String} str
* @returns {String} hex representation of input string
*/
var utf8ToHex = function(str) {
str = utf8.encode(str);
var hex = "";
// remove \u0000 padding from either side
str = str.replace(/^(?:\u0000)*/,'');
str = str.split("").reverse().join("");
str = str.replace(/^(?:\u0000)*/,'');
str = str.split("").reverse().join("");
for(var i = 0; i < str.length; i++) {
var code = str.charCodeAt(i);
// if (code !== 0) {
var 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
*/
var hexToUtf8 = function(hex) {
if (!isHex(hex))
throw new Error('The parameter "'+ hex +'" must be a valid HEX string.');
var str = "";
var 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("");
var l = hex.length;
for (var i=0; i < l; i+=2) {
code = parseInt(hex.substr(i, 2), 16);
// if (code !== 0) {
str += String.fromCharCode(code);
// }
}
return utf8.decode(str);
};
/**
* Converts value to it's number representation
*
* @method hexToNumber
* @param {String|Number|BN} value
* @return {String}
*/
var hexToNumber = function (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
* @return {String}
*/
var hexToNumberString = function (value) {
if (!value) return value;
return toBN(value).toString(10);
};
/**
* Converts value to it's hex representation
*
* @method numberToHex
* @param {String|Number|BN} value
* @return {String}
*/
var numberToHex = function (value) {
if (!isFinite(value) && !_.isString(value)) {
return value;
}
var number = toBN(value);
var 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
* @return {String} the hex string
*/
var bytesToHex = function(bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) {
/* jshint ignore:start */
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xF).toString(16));
/* jshint ignore:end */
}
return '0x'+ hex.join("");
};
/**
* Convert a hex string to a byte array
*
* Note: Implementation from crypto-js
*
* @method hexToBytes
* @param {string} hex
* @return {Array} the byte array
*/
var hexToBytes = function(hex) {
hex = hex.toString(16);
if (!isHex(hex)) {
throw new Error('Given value "'+ hex +'" is not a valid hex string.');
}
hex = hex.replace(/^0x/i,'');
for (var bytes = [], 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
* @return {String}
*/
var toHex = function (value, returnType) {
/*jshint maxcomplexity: false */
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
*
* @method isHex
* @param {String} hex to be checked
* @returns {Boolean}
*/
var isHex = function (hex) {
return ((_.isString(hex) || _.isNumber(hex)) && /^(-)?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} hex encoded bloom filter
* @return {Boolean}
*/
var isBloom = function (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} hex encoded topic
* @return {Boolean}
*/
var isTopic = function (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 sha3 hash using keccak 256
*
* To hash a HEX string the hex must have 0x in front.
*
* @method sha3
* @return {String} the sha3 string
*/
var SHA3_NULL_S = '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470';
var sha3 = function (value) {
if (isHex(value) && /^0x/i.test((value).toString())) {
value = hexToBytes(value);
}
var returnValue = Hash.keccak256(value); // jshint ignore:line
if(returnValue === SHA3_NULL_S) {
return null;
} else {
return returnValue;
}
};
// expose the under the hood keccak256
sha3._Hash = Hash;
module.exports = {
BN: BN,
isBN: isBN,
isBigNumber: isBigNumber,
toBN: toBN,
isAddress: isAddress,
isBloom: isBloom, // TODO UNDOCUMENTED
isTopic: isTopic, // TODO UNDOCUMENTED
checkAddressChecksum: checkAddressChecksum,
utf8ToHex: utf8ToHex,
hexToUtf8: hexToUtf8,
hexToNumber: hexToNumber,
hexToNumberString: hexToNumberString,
numberToHex: numberToHex,
toHex: toHex,
hexToBytes: hexToBytes,
bytesToHex: bytesToHex,
isHex: isHex,
leftPad: leftPad,
rightPad: rightPad,
sha3: sha3
};