1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var _ = require('underscore');
|
24 | var BN = require('bn.js');
|
25 | var numberToBN = require('number-to-bn');
|
26 | var utf8 = require('utf8');
|
27 | var Hash = require("eth-lib/lib/hash");
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | var isBN = function (object) {
|
38 | return object instanceof BN ||
|
39 | (object && object.constructor && object.constructor.name === 'BN');
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | var isBigNumber = function (object) {
|
50 | return object && object.constructor && object.constructor.name === 'BigNumber';
|
51 | };
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | var toBN = function(number){
|
61 | try {
|
62 | return numberToBN.apply(null, arguments);
|
63 | } catch(e) {
|
64 | throw new Error(e + ' Given value: "'+ number +'"');
|
65 | }
|
66 | };
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | var toTwosComplement = function (number) {
|
77 | return '0x'+ toBN(number).toTwos(256).toString(16, 64);
|
78 | };
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | var isAddress = function (address) {
|
88 |
|
89 | if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
|
90 | return false;
|
91 |
|
92 | } else if (/^(0x|0X)?[0-9a-f]{40}$/.test(address) || /^(0x|0X)?[0-9A-F]{40}$/.test(address)) {
|
93 | return true;
|
94 |
|
95 | } else {
|
96 | return checkAddressChecksum(address);
|
97 | }
|
98 | };
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | var checkAddressChecksum = function (address) {
|
110 |
|
111 | address = address.replace(/^0x/i,'');
|
112 | var addressHash = sha3(address.toLowerCase()).replace(/^0x/i,'');
|
113 |
|
114 | for (var i = 0; i < 40; i++ ) {
|
115 |
|
116 | if ((parseInt(addressHash[i], 16) > 7 && address[i].toUpperCase() !== address[i]) || (parseInt(addressHash[i], 16) <= 7 && address[i].toLowerCase() !== address[i])) {
|
117 | return false;
|
118 | }
|
119 | }
|
120 | return true;
|
121 | };
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | var leftPad = function (string, chars, sign) {
|
133 | var hasPrefix = /^0x/i.test(string) || typeof string === 'number';
|
134 | string = string.toString(16).replace(/^0x/i,'');
|
135 |
|
136 | var padding = (chars - string.length + 1 >= 0) ? chars - string.length + 1 : 0;
|
137 |
|
138 | return (hasPrefix ? '0x' : '') + new Array(padding).join(sign ? sign : "0") + string;
|
139 | };
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | var rightPad = function (string, chars, sign) {
|
151 | var hasPrefix = /^0x/i.test(string) || typeof string === 'number';
|
152 | string = string.toString(16).replace(/^0x/i,'');
|
153 |
|
154 | var padding = (chars - string.length + 1 >= 0) ? chars - string.length + 1 : 0;
|
155 |
|
156 | return (hasPrefix ? '0x' : '') + string + (new Array(padding).join(sign ? sign : "0"));
|
157 | };
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | var utf8ToHex = function(str) {
|
168 | str = utf8.encode(str);
|
169 | var hex = "";
|
170 |
|
171 |
|
172 | str = str.replace(/^(?:\u0000)*/,'');
|
173 | str = str.split("").reverse().join("");
|
174 | str = str.replace(/^(?:\u0000)*/,'');
|
175 | str = str.split("").reverse().join("");
|
176 |
|
177 | for(var i = 0; i < str.length; i++) {
|
178 | var code = str.charCodeAt(i);
|
179 |
|
180 | var n = code.toString(16);
|
181 | hex += n.length < 2 ? '0' + n : n;
|
182 |
|
183 | }
|
184 |
|
185 | return "0x" + hex;
|
186 | };
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | var hexToUtf8 = function(hex) {
|
196 | if (!isHexStrict(hex))
|
197 | throw new Error('The parameter "'+ hex +'" must be a valid HEX string.');
|
198 |
|
199 | var str = "";
|
200 | var code = 0;
|
201 | hex = hex.replace(/^0x/i,'');
|
202 |
|
203 |
|
204 | hex = hex.replace(/^(?:00)*/,'');
|
205 | hex = hex.split("").reverse().join("");
|
206 | hex = hex.replace(/^(?:00)*/,'');
|
207 | hex = hex.split("").reverse().join("");
|
208 |
|
209 | var l = hex.length;
|
210 |
|
211 | for (var i=0; i < l; i+=2) {
|
212 | code = parseInt(hex.substr(i, 2), 16);
|
213 |
|
214 | str += String.fromCharCode(code);
|
215 |
|
216 | }
|
217 |
|
218 | return utf8.decode(str);
|
219 | };
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | var hexToNumber = function (value) {
|
230 | if (!value) {
|
231 | return value;
|
232 | }
|
233 |
|
234 | return toBN(value).toNumber();
|
235 | };
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 | var hexToNumberString = function (value) {
|
245 | if (!value) return value;
|
246 |
|
247 | return toBN(value).toString(10);
|
248 | };
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 | var numberToHex = function (value) {
|
259 | if (_.isNull(value) || _.isUndefined(value)) {
|
260 | return value;
|
261 | }
|
262 |
|
263 | if (!isFinite(value) && !isHexStrict(value)) {
|
264 | throw new Error('Given input "'+value+'" is not a number.');
|
265 | }
|
266 |
|
267 | var number = toBN(value);
|
268 | var result = number.toString(16);
|
269 |
|
270 | return number.lt(new BN(0)) ? '-0x' + result.substr(1) : '0x' + result;
|
271 | };
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | var bytesToHex = function(bytes) {
|
284 | for (var hex = [], i = 0; i < bytes.length; i++) {
|
285 |
|
286 | hex.push((bytes[i] >>> 4).toString(16));
|
287 | hex.push((bytes[i] & 0xF).toString(16));
|
288 |
|
289 | }
|
290 | return '0x'+ hex.join("");
|
291 | };
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 | var hexToBytes = function(hex) {
|
303 | hex = hex.toString(16);
|
304 |
|
305 | if (!isHexStrict(hex)) {
|
306 | throw new Error('Given value "'+ hex +'" is not a valid hex string.');
|
307 | }
|
308 |
|
309 | hex = hex.replace(/^0x/i,'');
|
310 |
|
311 | for (var bytes = [], c = 0; c < hex.length; c += 2)
|
312 | bytes.push(parseInt(hex.substr(c, 2), 16));
|
313 | return bytes;
|
314 | };
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | var toHex = function (value, returnType) {
|
327 |
|
328 |
|
329 | if (isAddress(value)) {
|
330 | return returnType ? 'address' : '0x'+ value.toLowerCase().replace(/^0x/i,'');
|
331 | }
|
332 |
|
333 | if (_.isBoolean(value)) {
|
334 | return returnType ? 'bool' : value ? '0x01' : '0x00';
|
335 | }
|
336 |
|
337 |
|
338 | if (_.isObject(value) && !isBigNumber(value) && !isBN(value)) {
|
339 | return returnType ? 'string' : utf8ToHex(JSON.stringify(value));
|
340 | }
|
341 |
|
342 |
|
343 | if (_.isString(value)) {
|
344 | if (value.indexOf('-0x') === 0 || value.indexOf('-0X') === 0) {
|
345 | return returnType ? 'int256' : numberToHex(value);
|
346 | } else if(value.indexOf('0x') === 0 || value.indexOf('0X') === 0) {
|
347 | return returnType ? 'bytes' : value;
|
348 | } else if (!isFinite(value)) {
|
349 | return returnType ? 'string' : utf8ToHex(value);
|
350 | }
|
351 | }
|
352 |
|
353 | return returnType ? (value < 0 ? 'int256' : 'uint256') : numberToHex(value);
|
354 | };
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 | var isHexStrict = function (hex) {
|
365 | return ((_.isString(hex) || _.isNumber(hex)) && /^(-)?0x[0-9a-f]*$/i.test(hex));
|
366 | };
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | var isHex = function (hex) {
|
376 | return ((_.isString(hex) || _.isNumber(hex)) && /^(-0x|0x)?[0-9a-f]*$/i.test(hex));
|
377 | };
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 | var isBloom = function (bloom) {
|
390 | if (!/^(0x)?[0-9a-f]{512}$/i.test(bloom)) {
|
391 | return false;
|
392 | } else if (/^(0x)?[0-9a-f]{512}$/.test(bloom) || /^(0x)?[0-9A-F]{512}$/.test(bloom)) {
|
393 | return true;
|
394 | }
|
395 | return false;
|
396 | };
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 | var isTopic = function (topic) {
|
408 | if (!/^(0x)?[0-9a-f]{64}$/i.test(topic)) {
|
409 | return false;
|
410 | } else if (/^(0x)?[0-9a-f]{64}$/.test(topic) || /^(0x)?[0-9A-F]{64}$/.test(topic)) {
|
411 | return true;
|
412 | }
|
413 | return false;
|
414 | };
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 | var SHA3_NULL_S = '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470';
|
426 |
|
427 | var sha3 = function (value) {
|
428 | if (isBN(value)) {
|
429 | value = value.toString();
|
430 | }
|
431 |
|
432 | if (isHexStrict(value) && /^0x/i.test((value).toString())) {
|
433 | value = hexToBytes(value);
|
434 | }
|
435 |
|
436 | var returnValue = Hash.keccak256(value);
|
437 |
|
438 | if(returnValue === SHA3_NULL_S) {
|
439 | return null;
|
440 | } else {
|
441 | return returnValue;
|
442 | }
|
443 | };
|
444 |
|
445 | sha3._Hash = Hash;
|
446 |
|
447 |
|
448 | module.exports = {
|
449 | BN: BN,
|
450 | isBN: isBN,
|
451 | isBigNumber: isBigNumber,
|
452 | toBN: toBN,
|
453 | isAddress: isAddress,
|
454 | isBloom: isBloom,
|
455 | isTopic: isTopic,
|
456 | checkAddressChecksum: checkAddressChecksum,
|
457 | utf8ToHex: utf8ToHex,
|
458 | hexToUtf8: hexToUtf8,
|
459 | hexToNumber: hexToNumber,
|
460 | hexToNumberString: hexToNumberString,
|
461 | numberToHex: numberToHex,
|
462 | toHex: toHex,
|
463 | hexToBytes: hexToBytes,
|
464 | bytesToHex: bytesToHex,
|
465 | isHex: isHex,
|
466 | isHexStrict: isHexStrict,
|
467 | leftPad: leftPad,
|
468 | rightPad: rightPad,
|
469 | toTwosComplement: toTwosComplement,
|
470 | sha3: sha3
|
471 | };
|