UNPKG

27.8 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var _commonjsHelpers = require('./_commonjsHelpers-72d386ba.js');
6require('./miscellaneous.js');
7var environment = require('./environment.js');
8
9var sha3 = _commonjsHelpers.createCommonjsModule(function (module) {
10/**
11 * [js-sha3]{@link https://github.com/emn178/js-sha3}
12 *
13 * @version 0.8.0
14 * @author Chen, Yi-Cyuan [emn178@gmail.com]
15 * @copyright Chen, Yi-Cyuan 2015-2018
16 * @license MIT
17 */
18/*jslint bitwise: true */
19(function () {
20
21 var INPUT_ERROR = 'input is invalid type';
22 var FINALIZE_ERROR = 'finalize already called';
23 var WINDOW = typeof window === 'object';
24 var root = WINDOW ? window : {};
25 if (root.JS_SHA3_NO_WINDOW) {
26 WINDOW = false;
27 }
28 var WEB_WORKER = !WINDOW && typeof self === 'object';
29 var NODE_JS = !root.JS_SHA3_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node;
30 if (NODE_JS) {
31 root = _commonjsHelpers.commonjsGlobal;
32 } else if (WEB_WORKER) {
33 root = self;
34 }
35 var COMMON_JS = !root.JS_SHA3_NO_COMMON_JS && 'object' === 'object' && module.exports;
36 var ARRAY_BUFFER = !root.JS_SHA3_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined';
37 var HEX_CHARS = '0123456789abcdef'.split('');
38 var SHAKE_PADDING = [31, 7936, 2031616, 520093696];
39 var CSHAKE_PADDING = [4, 1024, 262144, 67108864];
40 var KECCAK_PADDING = [1, 256, 65536, 16777216];
41 var PADDING = [6, 1536, 393216, 100663296];
42 var SHIFT = [0, 8, 16, 24];
43 var RC = [1, 0, 32898, 0, 32906, 2147483648, 2147516416, 2147483648, 32907, 0, 2147483649,
44 0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0,
45 2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771,
46 2147483648, 32770, 2147483648, 128, 2147483648, 32778, 0, 2147483658, 2147483648,
47 2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648];
48 var BITS = [224, 256, 384, 512];
49 var SHAKE_BITS = [128, 256];
50 var OUTPUT_TYPES = ['hex', 'buffer', 'arrayBuffer', 'array', 'digest'];
51 var CSHAKE_BYTEPAD = {
52 '128': 168,
53 '256': 136
54 };
55
56 if (root.JS_SHA3_NO_NODE_JS || !Array.isArray) {
57 Array.isArray = function (obj) {
58 return Object.prototype.toString.call(obj) === '[object Array]';
59 };
60 }
61
62 if (ARRAY_BUFFER && (root.JS_SHA3_NO_ARRAY_BUFFER_IS_VIEW || !ArrayBuffer.isView)) {
63 ArrayBuffer.isView = function (obj) {
64 return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer;
65 };
66 }
67
68 var createOutputMethod = function (bits, padding, outputType) {
69 return function (message) {
70 return new Keccak(bits, padding, bits).update(message)[outputType]();
71 };
72 };
73
74 var createShakeOutputMethod = function (bits, padding, outputType) {
75 return function (message, outputBits) {
76 return new Keccak(bits, padding, outputBits).update(message)[outputType]();
77 };
78 };
79
80 var createCshakeOutputMethod = function (bits, padding, outputType) {
81 return function (message, outputBits, n, s) {
82 return methods['cshake' + bits].update(message, outputBits, n, s)[outputType]();
83 };
84 };
85
86 var createKmacOutputMethod = function (bits, padding, outputType) {
87 return function (key, message, outputBits, s) {
88 return methods['kmac' + bits].update(key, message, outputBits, s)[outputType]();
89 };
90 };
91
92 var createOutputMethods = function (method, createMethod, bits, padding) {
93 for (var i = 0; i < OUTPUT_TYPES.length; ++i) {
94 var type = OUTPUT_TYPES[i];
95 method[type] = createMethod(bits, padding, type);
96 }
97 return method;
98 };
99
100 var createMethod = function (bits, padding) {
101 var method = createOutputMethod(bits, padding, 'hex');
102 method.create = function () {
103 return new Keccak(bits, padding, bits);
104 };
105 method.update = function (message) {
106 return method.create().update(message);
107 };
108 return createOutputMethods(method, createOutputMethod, bits, padding);
109 };
110
111 var createShakeMethod = function (bits, padding) {
112 var method = createShakeOutputMethod(bits, padding, 'hex');
113 method.create = function (outputBits) {
114 return new Keccak(bits, padding, outputBits);
115 };
116 method.update = function (message, outputBits) {
117 return method.create(outputBits).update(message);
118 };
119 return createOutputMethods(method, createShakeOutputMethod, bits, padding);
120 };
121
122 var createCshakeMethod = function (bits, padding) {
123 var w = CSHAKE_BYTEPAD[bits];
124 var method = createCshakeOutputMethod(bits, padding, 'hex');
125 method.create = function (outputBits, n, s) {
126 if (!n && !s) {
127 return methods['shake' + bits].create(outputBits);
128 } else {
129 return new Keccak(bits, padding, outputBits).bytepad([n, s], w);
130 }
131 };
132 method.update = function (message, outputBits, n, s) {
133 return method.create(outputBits, n, s).update(message);
134 };
135 return createOutputMethods(method, createCshakeOutputMethod, bits, padding);
136 };
137
138 var createKmacMethod = function (bits, padding) {
139 var w = CSHAKE_BYTEPAD[bits];
140 var method = createKmacOutputMethod(bits, padding, 'hex');
141 method.create = function (key, outputBits, s) {
142 return new Kmac(bits, padding, outputBits).bytepad(['KMAC', s], w).bytepad([key], w);
143 };
144 method.update = function (key, message, outputBits, s) {
145 return method.create(key, outputBits, s).update(message);
146 };
147 return createOutputMethods(method, createKmacOutputMethod, bits, padding);
148 };
149
150 var algorithms = [
151 { name: 'keccak', padding: KECCAK_PADDING, bits: BITS, createMethod: createMethod },
152 { name: 'sha3', padding: PADDING, bits: BITS, createMethod: createMethod },
153 { name: 'shake', padding: SHAKE_PADDING, bits: SHAKE_BITS, createMethod: createShakeMethod },
154 { name: 'cshake', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createCshakeMethod },
155 { name: 'kmac', padding: CSHAKE_PADDING, bits: SHAKE_BITS, createMethod: createKmacMethod }
156 ];
157
158 var methods = {}, methodNames = [];
159
160 for (var i = 0; i < algorithms.length; ++i) {
161 var algorithm = algorithms[i];
162 var bits = algorithm.bits;
163 for (var j = 0; j < bits.length; ++j) {
164 var methodName = algorithm.name + '_' + bits[j];
165 methodNames.push(methodName);
166 methods[methodName] = algorithm.createMethod(bits[j], algorithm.padding);
167 if (algorithm.name !== 'sha3') {
168 var newMethodName = algorithm.name + bits[j];
169 methodNames.push(newMethodName);
170 methods[newMethodName] = methods[methodName];
171 }
172 }
173 }
174
175 function Keccak(bits, padding, outputBits) {
176 this.blocks = [];
177 this.s = [];
178 this.padding = padding;
179 this.outputBits = outputBits;
180 this.reset = true;
181 this.finalized = false;
182 this.block = 0;
183 this.start = 0;
184 this.blockCount = (1600 - (bits << 1)) >> 5;
185 this.byteCount = this.blockCount << 2;
186 this.outputBlocks = outputBits >> 5;
187 this.extraBytes = (outputBits & 31) >> 3;
188
189 for (var i = 0; i < 50; ++i) {
190 this.s[i] = 0;
191 }
192 }
193
194 Keccak.prototype.update = function (message) {
195 if (this.finalized) {
196 throw new Error(FINALIZE_ERROR);
197 }
198 var notString, type = typeof message;
199 if (type !== 'string') {
200 if (type === 'object') {
201 if (message === null) {
202 throw new Error(INPUT_ERROR);
203 } else if (ARRAY_BUFFER && message.constructor === ArrayBuffer) {
204 message = new Uint8Array(message);
205 } else if (!Array.isArray(message)) {
206 if (!ARRAY_BUFFER || !ArrayBuffer.isView(message)) {
207 throw new Error(INPUT_ERROR);
208 }
209 }
210 } else {
211 throw new Error(INPUT_ERROR);
212 }
213 notString = true;
214 }
215 var blocks = this.blocks, byteCount = this.byteCount, length = message.length,
216 blockCount = this.blockCount, index = 0, s = this.s, i, code;
217
218 while (index < length) {
219 if (this.reset) {
220 this.reset = false;
221 blocks[0] = this.block;
222 for (i = 1; i < blockCount + 1; ++i) {
223 blocks[i] = 0;
224 }
225 }
226 if (notString) {
227 for (i = this.start; index < length && i < byteCount; ++index) {
228 blocks[i >> 2] |= message[index] << SHIFT[i++ & 3];
229 }
230 } else {
231 for (i = this.start; index < length && i < byteCount; ++index) {
232 code = message.charCodeAt(index);
233 if (code < 0x80) {
234 blocks[i >> 2] |= code << SHIFT[i++ & 3];
235 } else if (code < 0x800) {
236 blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3];
237 blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
238 } else if (code < 0xd800 || code >= 0xe000) {
239 blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3];
240 blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
241 blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
242 } else {
243 code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
244 blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3];
245 blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3];
246 blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3];
247 blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3];
248 }
249 }
250 }
251 this.lastByteIndex = i;
252 if (i >= byteCount) {
253 this.start = i - byteCount;
254 this.block = blocks[blockCount];
255 for (i = 0; i < blockCount; ++i) {
256 s[i] ^= blocks[i];
257 }
258 f(s);
259 this.reset = true;
260 } else {
261 this.start = i;
262 }
263 }
264 return this;
265 };
266
267 Keccak.prototype.encode = function (x, right) {
268 var o = x & 255, n = 1;
269 var bytes = [o];
270 x = x >> 8;
271 o = x & 255;
272 while (o > 0) {
273 bytes.unshift(o);
274 x = x >> 8;
275 o = x & 255;
276 ++n;
277 }
278 if (right) {
279 bytes.push(n);
280 } else {
281 bytes.unshift(n);
282 }
283 this.update(bytes);
284 return bytes.length;
285 };
286
287 Keccak.prototype.encodeString = function (str) {
288 var notString, type = typeof str;
289 if (type !== 'string') {
290 if (type === 'object') {
291 if (str === null) {
292 throw new Error(INPUT_ERROR);
293 } else if (ARRAY_BUFFER && str.constructor === ArrayBuffer) {
294 str = new Uint8Array(str);
295 } else if (!Array.isArray(str)) {
296 if (!ARRAY_BUFFER || !ArrayBuffer.isView(str)) {
297 throw new Error(INPUT_ERROR);
298 }
299 }
300 } else {
301 throw new Error(INPUT_ERROR);
302 }
303 notString = true;
304 }
305 var bytes = 0, length = str.length;
306 if (notString) {
307 bytes = length;
308 } else {
309 for (var i = 0; i < str.length; ++i) {
310 var code = str.charCodeAt(i);
311 if (code < 0x80) {
312 bytes += 1;
313 } else if (code < 0x800) {
314 bytes += 2;
315 } else if (code < 0xd800 || code >= 0xe000) {
316 bytes += 3;
317 } else {
318 code = 0x10000 + (((code & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff));
319 bytes += 4;
320 }
321 }
322 }
323 bytes += this.encode(bytes * 8);
324 this.update(str);
325 return bytes;
326 };
327
328 Keccak.prototype.bytepad = function (strs, w) {
329 var bytes = this.encode(w);
330 for (var i = 0; i < strs.length; ++i) {
331 bytes += this.encodeString(strs[i]);
332 }
333 var paddingBytes = w - bytes % w;
334 var zeros = [];
335 zeros.length = paddingBytes;
336 this.update(zeros);
337 return this;
338 };
339
340 Keccak.prototype.finalize = function () {
341 if (this.finalized) {
342 return;
343 }
344 this.finalized = true;
345 var blocks = this.blocks, i = this.lastByteIndex, blockCount = this.blockCount, s = this.s;
346 blocks[i >> 2] |= this.padding[i & 3];
347 if (this.lastByteIndex === this.byteCount) {
348 blocks[0] = blocks[blockCount];
349 for (i = 1; i < blockCount + 1; ++i) {
350 blocks[i] = 0;
351 }
352 }
353 blocks[blockCount - 1] |= 0x80000000;
354 for (i = 0; i < blockCount; ++i) {
355 s[i] ^= blocks[i];
356 }
357 f(s);
358 };
359
360 Keccak.prototype.toString = Keccak.prototype.hex = function () {
361 this.finalize();
362
363 var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks,
364 extraBytes = this.extraBytes, i = 0, j = 0;
365 var hex = '', block;
366 while (j < outputBlocks) {
367 for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) {
368 block = s[i];
369 hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F] +
370 HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F] +
371 HEX_CHARS[(block >> 20) & 0x0F] + HEX_CHARS[(block >> 16) & 0x0F] +
372 HEX_CHARS[(block >> 28) & 0x0F] + HEX_CHARS[(block >> 24) & 0x0F];
373 }
374 if (j % blockCount === 0) {
375 f(s);
376 i = 0;
377 }
378 }
379 if (extraBytes) {
380 block = s[i];
381 hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F];
382 if (extraBytes > 1) {
383 hex += HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F];
384 }
385 if (extraBytes > 2) {
386 hex += HEX_CHARS[(block >> 20) & 0x0F] + HEX_CHARS[(block >> 16) & 0x0F];
387 }
388 }
389 return hex;
390 };
391
392 Keccak.prototype.arrayBuffer = function () {
393 this.finalize();
394
395 var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks,
396 extraBytes = this.extraBytes, i = 0, j = 0;
397 var bytes = this.outputBits >> 3;
398 var buffer;
399 if (extraBytes) {
400 buffer = new ArrayBuffer((outputBlocks + 1) << 2);
401 } else {
402 buffer = new ArrayBuffer(bytes);
403 }
404 var array = new Uint32Array(buffer);
405 while (j < outputBlocks) {
406 for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) {
407 array[j] = s[i];
408 }
409 if (j % blockCount === 0) {
410 f(s);
411 }
412 }
413 if (extraBytes) {
414 array[i] = s[i];
415 buffer = buffer.slice(0, bytes);
416 }
417 return buffer;
418 };
419
420 Keccak.prototype.buffer = Keccak.prototype.arrayBuffer;
421
422 Keccak.prototype.digest = Keccak.prototype.array = function () {
423 this.finalize();
424
425 var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks,
426 extraBytes = this.extraBytes, i = 0, j = 0;
427 var array = [], offset, block;
428 while (j < outputBlocks) {
429 for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) {
430 offset = j << 2;
431 block = s[i];
432 array[offset] = block & 0xFF;
433 array[offset + 1] = (block >> 8) & 0xFF;
434 array[offset + 2] = (block >> 16) & 0xFF;
435 array[offset + 3] = (block >> 24) & 0xFF;
436 }
437 if (j % blockCount === 0) {
438 f(s);
439 }
440 }
441 if (extraBytes) {
442 offset = j << 2;
443 block = s[i];
444 array[offset] = block & 0xFF;
445 if (extraBytes > 1) {
446 array[offset + 1] = (block >> 8) & 0xFF;
447 }
448 if (extraBytes > 2) {
449 array[offset + 2] = (block >> 16) & 0xFF;
450 }
451 }
452 return array;
453 };
454
455 function Kmac(bits, padding, outputBits) {
456 Keccak.call(this, bits, padding, outputBits);
457 }
458
459 Kmac.prototype = new Keccak();
460
461 Kmac.prototype.finalize = function () {
462 this.encode(this.outputBits, true);
463 return Keccak.prototype.finalize.call(this);
464 };
465
466 var f = function (s) {
467 var h, l, n, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9,
468 b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17,
469 b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33,
470 b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49;
471 for (n = 0; n < 48; n += 2) {
472 c0 = s[0] ^ s[10] ^ s[20] ^ s[30] ^ s[40];
473 c1 = s[1] ^ s[11] ^ s[21] ^ s[31] ^ s[41];
474 c2 = s[2] ^ s[12] ^ s[22] ^ s[32] ^ s[42];
475 c3 = s[3] ^ s[13] ^ s[23] ^ s[33] ^ s[43];
476 c4 = s[4] ^ s[14] ^ s[24] ^ s[34] ^ s[44];
477 c5 = s[5] ^ s[15] ^ s[25] ^ s[35] ^ s[45];
478 c6 = s[6] ^ s[16] ^ s[26] ^ s[36] ^ s[46];
479 c7 = s[7] ^ s[17] ^ s[27] ^ s[37] ^ s[47];
480 c8 = s[8] ^ s[18] ^ s[28] ^ s[38] ^ s[48];
481 c9 = s[9] ^ s[19] ^ s[29] ^ s[39] ^ s[49];
482
483 h = c8 ^ ((c2 << 1) | (c3 >>> 31));
484 l = c9 ^ ((c3 << 1) | (c2 >>> 31));
485 s[0] ^= h;
486 s[1] ^= l;
487 s[10] ^= h;
488 s[11] ^= l;
489 s[20] ^= h;
490 s[21] ^= l;
491 s[30] ^= h;
492 s[31] ^= l;
493 s[40] ^= h;
494 s[41] ^= l;
495 h = c0 ^ ((c4 << 1) | (c5 >>> 31));
496 l = c1 ^ ((c5 << 1) | (c4 >>> 31));
497 s[2] ^= h;
498 s[3] ^= l;
499 s[12] ^= h;
500 s[13] ^= l;
501 s[22] ^= h;
502 s[23] ^= l;
503 s[32] ^= h;
504 s[33] ^= l;
505 s[42] ^= h;
506 s[43] ^= l;
507 h = c2 ^ ((c6 << 1) | (c7 >>> 31));
508 l = c3 ^ ((c7 << 1) | (c6 >>> 31));
509 s[4] ^= h;
510 s[5] ^= l;
511 s[14] ^= h;
512 s[15] ^= l;
513 s[24] ^= h;
514 s[25] ^= l;
515 s[34] ^= h;
516 s[35] ^= l;
517 s[44] ^= h;
518 s[45] ^= l;
519 h = c4 ^ ((c8 << 1) | (c9 >>> 31));
520 l = c5 ^ ((c9 << 1) | (c8 >>> 31));
521 s[6] ^= h;
522 s[7] ^= l;
523 s[16] ^= h;
524 s[17] ^= l;
525 s[26] ^= h;
526 s[27] ^= l;
527 s[36] ^= h;
528 s[37] ^= l;
529 s[46] ^= h;
530 s[47] ^= l;
531 h = c6 ^ ((c0 << 1) | (c1 >>> 31));
532 l = c7 ^ ((c1 << 1) | (c0 >>> 31));
533 s[8] ^= h;
534 s[9] ^= l;
535 s[18] ^= h;
536 s[19] ^= l;
537 s[28] ^= h;
538 s[29] ^= l;
539 s[38] ^= h;
540 s[39] ^= l;
541 s[48] ^= h;
542 s[49] ^= l;
543
544 b0 = s[0];
545 b1 = s[1];
546 b32 = (s[11] << 4) | (s[10] >>> 28);
547 b33 = (s[10] << 4) | (s[11] >>> 28);
548 b14 = (s[20] << 3) | (s[21] >>> 29);
549 b15 = (s[21] << 3) | (s[20] >>> 29);
550 b46 = (s[31] << 9) | (s[30] >>> 23);
551 b47 = (s[30] << 9) | (s[31] >>> 23);
552 b28 = (s[40] << 18) | (s[41] >>> 14);
553 b29 = (s[41] << 18) | (s[40] >>> 14);
554 b20 = (s[2] << 1) | (s[3] >>> 31);
555 b21 = (s[3] << 1) | (s[2] >>> 31);
556 b2 = (s[13] << 12) | (s[12] >>> 20);
557 b3 = (s[12] << 12) | (s[13] >>> 20);
558 b34 = (s[22] << 10) | (s[23] >>> 22);
559 b35 = (s[23] << 10) | (s[22] >>> 22);
560 b16 = (s[33] << 13) | (s[32] >>> 19);
561 b17 = (s[32] << 13) | (s[33] >>> 19);
562 b48 = (s[42] << 2) | (s[43] >>> 30);
563 b49 = (s[43] << 2) | (s[42] >>> 30);
564 b40 = (s[5] << 30) | (s[4] >>> 2);
565 b41 = (s[4] << 30) | (s[5] >>> 2);
566 b22 = (s[14] << 6) | (s[15] >>> 26);
567 b23 = (s[15] << 6) | (s[14] >>> 26);
568 b4 = (s[25] << 11) | (s[24] >>> 21);
569 b5 = (s[24] << 11) | (s[25] >>> 21);
570 b36 = (s[34] << 15) | (s[35] >>> 17);
571 b37 = (s[35] << 15) | (s[34] >>> 17);
572 b18 = (s[45] << 29) | (s[44] >>> 3);
573 b19 = (s[44] << 29) | (s[45] >>> 3);
574 b10 = (s[6] << 28) | (s[7] >>> 4);
575 b11 = (s[7] << 28) | (s[6] >>> 4);
576 b42 = (s[17] << 23) | (s[16] >>> 9);
577 b43 = (s[16] << 23) | (s[17] >>> 9);
578 b24 = (s[26] << 25) | (s[27] >>> 7);
579 b25 = (s[27] << 25) | (s[26] >>> 7);
580 b6 = (s[36] << 21) | (s[37] >>> 11);
581 b7 = (s[37] << 21) | (s[36] >>> 11);
582 b38 = (s[47] << 24) | (s[46] >>> 8);
583 b39 = (s[46] << 24) | (s[47] >>> 8);
584 b30 = (s[8] << 27) | (s[9] >>> 5);
585 b31 = (s[9] << 27) | (s[8] >>> 5);
586 b12 = (s[18] << 20) | (s[19] >>> 12);
587 b13 = (s[19] << 20) | (s[18] >>> 12);
588 b44 = (s[29] << 7) | (s[28] >>> 25);
589 b45 = (s[28] << 7) | (s[29] >>> 25);
590 b26 = (s[38] << 8) | (s[39] >>> 24);
591 b27 = (s[39] << 8) | (s[38] >>> 24);
592 b8 = (s[48] << 14) | (s[49] >>> 18);
593 b9 = (s[49] << 14) | (s[48] >>> 18);
594
595 s[0] = b0 ^ (~b2 & b4);
596 s[1] = b1 ^ (~b3 & b5);
597 s[10] = b10 ^ (~b12 & b14);
598 s[11] = b11 ^ (~b13 & b15);
599 s[20] = b20 ^ (~b22 & b24);
600 s[21] = b21 ^ (~b23 & b25);
601 s[30] = b30 ^ (~b32 & b34);
602 s[31] = b31 ^ (~b33 & b35);
603 s[40] = b40 ^ (~b42 & b44);
604 s[41] = b41 ^ (~b43 & b45);
605 s[2] = b2 ^ (~b4 & b6);
606 s[3] = b3 ^ (~b5 & b7);
607 s[12] = b12 ^ (~b14 & b16);
608 s[13] = b13 ^ (~b15 & b17);
609 s[22] = b22 ^ (~b24 & b26);
610 s[23] = b23 ^ (~b25 & b27);
611 s[32] = b32 ^ (~b34 & b36);
612 s[33] = b33 ^ (~b35 & b37);
613 s[42] = b42 ^ (~b44 & b46);
614 s[43] = b43 ^ (~b45 & b47);
615 s[4] = b4 ^ (~b6 & b8);
616 s[5] = b5 ^ (~b7 & b9);
617 s[14] = b14 ^ (~b16 & b18);
618 s[15] = b15 ^ (~b17 & b19);
619 s[24] = b24 ^ (~b26 & b28);
620 s[25] = b25 ^ (~b27 & b29);
621 s[34] = b34 ^ (~b36 & b38);
622 s[35] = b35 ^ (~b37 & b39);
623 s[44] = b44 ^ (~b46 & b48);
624 s[45] = b45 ^ (~b47 & b49);
625 s[6] = b6 ^ (~b8 & b0);
626 s[7] = b7 ^ (~b9 & b1);
627 s[16] = b16 ^ (~b18 & b10);
628 s[17] = b17 ^ (~b19 & b11);
629 s[26] = b26 ^ (~b28 & b20);
630 s[27] = b27 ^ (~b29 & b21);
631 s[36] = b36 ^ (~b38 & b30);
632 s[37] = b37 ^ (~b39 & b31);
633 s[46] = b46 ^ (~b48 & b40);
634 s[47] = b47 ^ (~b49 & b41);
635 s[8] = b8 ^ (~b0 & b2);
636 s[9] = b9 ^ (~b1 & b3);
637 s[18] = b18 ^ (~b10 & b12);
638 s[19] = b19 ^ (~b11 & b13);
639 s[28] = b28 ^ (~b20 & b22);
640 s[29] = b29 ^ (~b21 & b23);
641 s[38] = b38 ^ (~b30 & b32);
642 s[39] = b39 ^ (~b31 & b33);
643 s[48] = b48 ^ (~b40 & b42);
644 s[49] = b49 ^ (~b41 & b43);
645
646 s[0] ^= RC[n];
647 s[1] ^= RC[n + 1];
648 }
649 };
650
651 if (COMMON_JS) {
652 module.exports = methods;
653 } else {
654 for (i = 0; i < methodNames.length; ++i) {
655 root[methodNames[i]] = methods[methodNames[i]];
656 }
657 }
658})();
659});
660
661var keccak256 = sha3.keccak_256;
662var EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000';
663var TRANSACTION_REGEX = /^0x[A-Fa-f0-9]{64}$/;
664var ADDRESS_REGEX = /^0x[0-9a-fA-F]{40}$/;
665var TRUST_WALLET_BASE_URL = 'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum';
666var ETHERSCAN_NETWORK_TYPES = new Map([['main', ''], ['kovan', 'kovan.'], ['rinkeby', 'rinkeby.'], ['ropsten', 'ropsten.'], ['goerli', 'goerli.']]);
667var ETHERSCAN_TYPES = new Map([['block', 'block'], ['transaction', 'tx'], ['address', 'address'], ['token', 'token']]);
668var BLOCK_EXPLORERS = {
669 etherscan: function etherscan(_ref) {
670 var type = _ref.type,
671 value = _ref.value,
672 networkType = _ref.networkType;
673
674 if (networkType === 'private') {
675 return '';
676 }
677
678 if (!ETHERSCAN_NETWORK_TYPES.has(networkType)) {
679 throw new Error('provider not supported.');
680 }
681
682 if (!ETHERSCAN_TYPES.has(type)) {
683 throw new Error('type not supported.');
684 }
685
686 var subdomain = ETHERSCAN_NETWORK_TYPES.get(networkType);
687 var typePart = ETHERSCAN_TYPES.get(type);
688 return "https://".concat(subdomain, "etherscan.io/").concat(typePart, "/").concat(value);
689 }
690};
691/**
692 * Converts to a checksum address
693 *
694 * This function is taken from web3-utils:
695 * https://github.com/ethereum/web3.js/blob/22df832303e349f8ae02f0392e56abe10e1dfaac/packages/web3-utils/src/index.js#L287-L315
696 * And was adapted to use js-sha3 rather than soliditySha3.js from web3.js, in
697 * order to avoid adding the BN.js and underscore dependencies.
698 *
699 * @method toChecksumAddress
700 * @param {String} address the given HEX address
701 * @returns {String}
702 */
703
704function toChecksumAddress(address) {
705 if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
706 throw new Error('Given address "' + address + '" is not a valid Ethereum address.');
707 }
708
709 address = address.toLowerCase().replace(/^0x/i, '');
710 var addressHash = keccak256(address).replace(/^0x/i, '');
711 var checksumAddress = '0x';
712
713 for (var i = 0; i < address.length; i++) {
714 // If ith character is 9 to f then make it uppercase
715 if (parseInt(addressHash[i], 16) > 7) {
716 checksumAddress += address[i].toUpperCase();
717 } else {
718 checksumAddress += address[i];
719 }
720 }
721
722 return checksumAddress;
723}
724/**
725 * Check address equality without checksums
726 * @param {string} first First address
727 * @param {string} second Second address
728 * @returns {boolean} Address equality
729 */
730
731
732function addressesEqual(first, second) {
733 first = first && first.toLowerCase();
734 second = second && second.toLowerCase();
735 return first === second;
736}
737/**
738 * Shorten an Ethereum address. `charsLength` allows to change the number of
739 * characters on both sides of the ellipsis.
740 *
741 * Examples:
742 * shortenAddress('0x19731977931271') // 0x1973…1271
743 * shortenAddress('0x19731977931271', 2) // 0x19…71
744 * shortenAddress('0x197319') // 0x197319 (already short enough)
745 *
746 * @param {string} address The address to shorten
747 * @param {number} [charsLength=4] The number of characters to change on both sides of the ellipsis
748 * @returns {string} The shortened address
749 */
750
751function shortenAddress(address) {
752 var charsLength = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 4;
753 var prefixLength = 2; // "0x"
754
755 if (!address) {
756 return '';
757 }
758
759 if (address.length < charsLength * 2 + prefixLength) {
760 return address;
761 }
762
763 return address.slice(0, charsLength + prefixLength) + '…' + address.slice(-charsLength);
764}
765/**
766 * Alias for shortenAddress (to generalize its use)
767 */
768
769var shortenTransaction = shortenAddress;
770/**
771 *
772 * Checks if the given string is an address
773 *
774 * @method isAddress
775 * @param {string} address the given HEX address
776 * @returns {boolean}
777 */
778
779function isAddress(address) {
780 return ADDRESS_REGEX.test(address);
781}
782/**
783 *
784 * Checks if the given string is a transaction
785 *
786 * @method isTransaction
787 * @param {string} address the given HEX address
788 * @returns {boolean}
789 */
790
791function isTransaction(transaction) {
792 return TRANSACTION_REGEX.test(transaction);
793}
794/**
795 * Generates an etherscan URL
796 *
797 * @param {string} type The type of URL (block, transaction, address or token).
798 * @param {string} value Identifier of the object, depending on the type (block number, transaction hash, …).
799 * @param {object} options The optional parameters.
800 * @param {string} options.networkType The Ethereum network type (main, kovan, rinkeby, ropsten, goerli, or private).
801 * @param {string} options.provider The explorer provider (e.g. etherscan).
802 * @returns {string} The generated URL, or an empty string if the parameters are invalid.
803 */
804
805function blockExplorerUrl(type, value) {
806 var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
807 _ref2$networkType = _ref2.networkType,
808 networkType = _ref2$networkType === void 0 ? 'main' : _ref2$networkType,
809 _ref2$provider = _ref2.provider,
810 provider = _ref2$provider === void 0 ? 'etherscan' : _ref2$provider;
811
812 var explorer = BLOCK_EXPLORERS[provider];
813
814 if (!explorer) {
815 environment.warn('blockExplorerUrl(): provider not supported.');
816 return '';
817 }
818
819 try {
820 return explorer({
821 type: type,
822 value: value,
823 networkType: networkType
824 });
825 } catch (err) {
826 environment.warn("blockExplorerUrl(): ".concat(err.message));
827 return '';
828 }
829}
830/**
831 * Get the address of a token icon
832 *
833 * @param {string} address The contract address of the token, or the zero address (0x000…) to get the Ethereum icon.
834 * @returns {string} The generated URL, or an empty string if the parameters are invalid.
835 */
836
837function tokenIconUrl() {
838 var address = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
839
840 try {
841 address = toChecksumAddress(address.trim());
842 } catch (err) {
843 return '';
844 }
845
846 if (address === EMPTY_ADDRESS) {
847 return "".concat(TRUST_WALLET_BASE_URL, "/info/logo.png");
848 }
849
850 return "".concat(TRUST_WALLET_BASE_URL, "/assets/").concat(address, "/logo.png");
851}
852
853exports.addressesEqual = addressesEqual;
854exports.blockExplorerUrl = blockExplorerUrl;
855exports.isAddress = isAddress;
856exports.isTransaction = isTransaction;
857exports.shortenAddress = shortenAddress;
858exports.shortenTransaction = shortenTransaction;
859exports.tokenIconUrl = tokenIconUrl;
860//# sourceMappingURL=web3.js.map