github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/internal/jsre/deps/vnt.js (about) 1 require=(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ 2 module.exports=[ 3 { 4 "constant": true, 5 "inputs": [ 6 { 7 "name": "_owner", 8 "type": "address" 9 } 10 ], 11 "name": "name", 12 "outputs": [ 13 { 14 "name": "o_name", 15 "type": "bytes32" 16 } 17 ], 18 "type": "function" 19 }, 20 { 21 "constant": true, 22 "inputs": [ 23 { 24 "name": "_name", 25 "type": "bytes32" 26 } 27 ], 28 "name": "owner", 29 "outputs": [ 30 { 31 "name": "", 32 "type": "address" 33 } 34 ], 35 "type": "function" 36 }, 37 { 38 "constant": true, 39 "inputs": [ 40 { 41 "name": "_name", 42 "type": "bytes32" 43 } 44 ], 45 "name": "content", 46 "outputs": [ 47 { 48 "name": "", 49 "type": "bytes32" 50 } 51 ], 52 "type": "function" 53 }, 54 { 55 "constant": true, 56 "inputs": [ 57 { 58 "name": "_name", 59 "type": "bytes32" 60 } 61 ], 62 "name": "addr", 63 "outputs": [ 64 { 65 "name": "", 66 "type": "address" 67 } 68 ], 69 "type": "function" 70 }, 71 { 72 "constant": false, 73 "inputs": [ 74 { 75 "name": "_name", 76 "type": "bytes32" 77 } 78 ], 79 "name": "reserve", 80 "outputs": [], 81 "type": "function" 82 }, 83 { 84 "constant": true, 85 "inputs": [ 86 { 87 "name": "_name", 88 "type": "bytes32" 89 } 90 ], 91 "name": "subRegistrar", 92 "outputs": [ 93 { 94 "name": "", 95 "type": "address" 96 } 97 ], 98 "type": "function" 99 }, 100 { 101 "constant": false, 102 "inputs": [ 103 { 104 "name": "_name", 105 "type": "bytes32" 106 }, 107 { 108 "name": "_newOwner", 109 "type": "address" 110 } 111 ], 112 "name": "transfer", 113 "outputs": [], 114 "type": "function" 115 }, 116 { 117 "constant": false, 118 "inputs": [ 119 { 120 "name": "_name", 121 "type": "bytes32" 122 }, 123 { 124 "name": "_registrar", 125 "type": "address" 126 } 127 ], 128 "name": "setSubRegistrar", 129 "outputs": [], 130 "type": "function" 131 }, 132 { 133 "constant": false, 134 "inputs": [], 135 "name": "Registrar", 136 "outputs": [], 137 "type": "function" 138 }, 139 { 140 "constant": false, 141 "inputs": [ 142 { 143 "name": "_name", 144 "type": "bytes32" 145 }, 146 { 147 "name": "_a", 148 "type": "address" 149 }, 150 { 151 "name": "_primary", 152 "type": "bool" 153 } 154 ], 155 "name": "setAddress", 156 "outputs": [], 157 "type": "function" 158 }, 159 { 160 "constant": false, 161 "inputs": [ 162 { 163 "name": "_name", 164 "type": "bytes32" 165 }, 166 { 167 "name": "_content", 168 "type": "bytes32" 169 } 170 ], 171 "name": "setContent", 172 "outputs": [], 173 "type": "function" 174 }, 175 { 176 "constant": false, 177 "inputs": [ 178 { 179 "name": "_name", 180 "type": "bytes32" 181 } 182 ], 183 "name": "disown", 184 "outputs": [], 185 "type": "function" 186 }, 187 { 188 "anonymous": false, 189 "inputs": [ 190 { 191 "indexed": true, 192 "name": "_name", 193 "type": "bytes32" 194 }, 195 { 196 "indexed": false, 197 "name": "_winner", 198 "type": "address" 199 } 200 ], 201 "name": "AuctionEnded", 202 "type": "event" 203 }, 204 { 205 "anonymous": false, 206 "inputs": [ 207 { 208 "indexed": true, 209 "name": "_name", 210 "type": "bytes32" 211 }, 212 { 213 "indexed": false, 214 "name": "_bidder", 215 "type": "address" 216 }, 217 { 218 "indexed": false, 219 "name": "_value", 220 "type": "uint256" 221 } 222 ], 223 "name": "NewBid", 224 "type": "event" 225 }, 226 { 227 "anonymous": false, 228 "inputs": [ 229 { 230 "indexed": true, 231 "name": "name", 232 "type": "bytes32" 233 } 234 ], 235 "name": "Changed", 236 "type": "event" 237 }, 238 { 239 "anonymous": false, 240 "inputs": [ 241 { 242 "indexed": true, 243 "name": "name", 244 "type": "bytes32" 245 }, 246 { 247 "indexed": true, 248 "name": "addr", 249 "type": "address" 250 } 251 ], 252 "name": "PrimaryChanged", 253 "type": "event" 254 } 255 ] 256 257 },{}],2:[function(require,module,exports){ 258 module.exports=[ 259 { 260 "constant": true, 261 "inputs": [ 262 { 263 "name": "_name", 264 "type": "bytes32" 265 } 266 ], 267 "name": "owner", 268 "outputs": [ 269 { 270 "name": "", 271 "type": "address" 272 } 273 ], 274 "type": "function" 275 }, 276 { 277 "constant": false, 278 "inputs": [ 279 { 280 "name": "_name", 281 "type": "bytes32" 282 }, 283 { 284 "name": "_refund", 285 "type": "address" 286 } 287 ], 288 "name": "disown", 289 "outputs": [], 290 "type": "function" 291 }, 292 { 293 "constant": true, 294 "inputs": [ 295 { 296 "name": "_name", 297 "type": "bytes32" 298 } 299 ], 300 "name": "addr", 301 "outputs": [ 302 { 303 "name": "", 304 "type": "address" 305 } 306 ], 307 "type": "function" 308 }, 309 { 310 "constant": false, 311 "inputs": [ 312 { 313 "name": "_name", 314 "type": "bytes32" 315 } 316 ], 317 "name": "reserve", 318 "outputs": [], 319 "type": "function" 320 }, 321 { 322 "constant": false, 323 "inputs": [ 324 { 325 "name": "_name", 326 "type": "bytes32" 327 }, 328 { 329 "name": "_newOwner", 330 "type": "address" 331 } 332 ], 333 "name": "transfer", 334 "outputs": [], 335 "type": "function" 336 }, 337 { 338 "constant": false, 339 "inputs": [ 340 { 341 "name": "_name", 342 "type": "bytes32" 343 }, 344 { 345 "name": "_a", 346 "type": "address" 347 } 348 ], 349 "name": "setAddr", 350 "outputs": [], 351 "type": "function" 352 }, 353 { 354 "anonymous": false, 355 "inputs": [ 356 { 357 "indexed": true, 358 "name": "name", 359 "type": "bytes32" 360 } 361 ], 362 "name": "Changed", 363 "type": "event" 364 } 365 ] 366 367 },{}],3:[function(require,module,exports){ 368 module.exports=[ 369 { 370 "constant": false, 371 "inputs": [ 372 { 373 "name": "from", 374 "type": "bytes32" 375 }, 376 { 377 "name": "to", 378 "type": "address" 379 }, 380 { 381 "name": "value", 382 "type": "uint256" 383 } 384 ], 385 "name": "transfer", 386 "outputs": [], 387 "type": "function" 388 }, 389 { 390 "constant": false, 391 "inputs": [ 392 { 393 "name": "from", 394 "type": "bytes32" 395 }, 396 { 397 "name": "to", 398 "type": "address" 399 }, 400 { 401 "name": "indirectId", 402 "type": "bytes32" 403 }, 404 { 405 "name": "value", 406 "type": "uint256" 407 } 408 ], 409 "name": "icapTransfer", 410 "outputs": [], 411 "type": "function" 412 }, 413 { 414 "constant": false, 415 "inputs": [ 416 { 417 "name": "to", 418 "type": "bytes32" 419 } 420 ], 421 "name": "deposit", 422 "outputs": [], 423 "payable": true, 424 "type": "function" 425 }, 426 { 427 "anonymous": false, 428 "inputs": [ 429 { 430 "indexed": true, 431 "name": "from", 432 "type": "address" 433 }, 434 { 435 "indexed": false, 436 "name": "value", 437 "type": "uint256" 438 } 439 ], 440 "name": "AnonymousDeposit", 441 "type": "event" 442 }, 443 { 444 "anonymous": false, 445 "inputs": [ 446 { 447 "indexed": true, 448 "name": "from", 449 "type": "address" 450 }, 451 { 452 "indexed": true, 453 "name": "to", 454 "type": "bytes32" 455 }, 456 { 457 "indexed": false, 458 "name": "value", 459 "type": "uint256" 460 } 461 ], 462 "name": "Deposit", 463 "type": "event" 464 }, 465 { 466 "anonymous": false, 467 "inputs": [ 468 { 469 "indexed": true, 470 "name": "from", 471 "type": "bytes32" 472 }, 473 { 474 "indexed": true, 475 "name": "to", 476 "type": "address" 477 }, 478 { 479 "indexed": false, 480 "name": "value", 481 "type": "uint256" 482 } 483 ], 484 "name": "Transfer", 485 "type": "event" 486 }, 487 { 488 "anonymous": false, 489 "inputs": [ 490 { 491 "indexed": true, 492 "name": "from", 493 "type": "bytes32" 494 }, 495 { 496 "indexed": true, 497 "name": "to", 498 "type": "address" 499 }, 500 { 501 "indexed": false, 502 "name": "indirectId", 503 "type": "bytes32" 504 }, 505 { 506 "indexed": false, 507 "name": "value", 508 "type": "uint256" 509 } 510 ], 511 "name": "IcapTransfer", 512 "type": "event" 513 } 514 ] 515 516 },{}],4:[function(require,module,exports){ 517 var f = require('./formatters'); 518 var Type = require('./type'); 519 520 /** 521 * TypeAddress is a prootype that represents address type 522 * It matches: 523 * address 524 * address[] 525 * address[4] 526 * address[][] 527 * address[3][] 528 * address[][6][], ... 529 */ 530 var TypeAddress = function () { 531 this._inputFormatter = f.formatInputInt; 532 this._outputFormatter = f.formatOutputAddress; 533 }; 534 535 TypeAddress.prototype = new Type({}); 536 TypeAddress.prototype.constructor = TypeAddress; 537 538 TypeAddress.prototype.isType = function (name) { 539 return !!name.match(/address(\[([0-9]*)\])?/); 540 }; 541 542 module.exports = TypeAddress; 543 544 },{"./formatters":9,"./type":14}],5:[function(require,module,exports){ 545 var f = require('./formatters'); 546 var Type = require('./type'); 547 548 /** 549 * TypeBool is a prootype that represents bool type 550 * It matches: 551 * bool 552 * bool[] 553 * bool[4] 554 * bool[][] 555 * bool[3][] 556 * bool[][6][], ... 557 */ 558 var TypeBool = function () { 559 this._inputFormatter = f.formatInputBool; 560 this._outputFormatter = f.formatOutputBool; 561 }; 562 563 TypeBool.prototype = new Type({}); 564 TypeBool.prototype.constructor = TypeBool; 565 566 TypeBool.prototype.isType = function (name) { 567 return !!name.match(/^bool(\[([0-9]*)\])*$/); 568 }; 569 570 module.exports = TypeBool; 571 572 },{"./formatters":9,"./type":14}],6:[function(require,module,exports){ 573 var f = require('./formatters'); 574 var Type = require('./type'); 575 576 /** 577 * TypeBytes is a prototype that represents the bytes type. 578 * It matches: 579 * bytes 580 * bytes[] 581 * bytes[4] 582 * bytes[][] 583 * bytes[3][] 584 * bytes[][6][], ... 585 * bytes32 586 * bytes8[4] 587 * bytes[3][] 588 */ 589 var TypeBytes = function () { 590 this._inputFormatter = f.formatInputBytes; 591 this._outputFormatter = f.formatOutputBytes; 592 }; 593 594 TypeBytes.prototype = new Type({}); 595 TypeBytes.prototype.constructor = TypeBytes; 596 597 TypeBytes.prototype.isType = function (name) { 598 return !!name.match(/^bytes([0-9]{1,})(\[([0-9]*)\])*$/); 599 }; 600 601 module.exports = TypeBytes; 602 603 },{"./formatters":9,"./type":14}],7:[function(require,module,exports){ 604 /* 605 This file is part of vnt.js. 606 607 vnt.js is free software: you can redistribute it and/or modify 608 it under the terms of the GNU Lesser General Public License as published by 609 the Free Software Foundation, either version 3 of the License, or 610 (at your option) any later version. 611 612 vnt.js is distributed in the hope that it will be useful, 613 but WITHOUT ANY WARRANTY; without even the implied warranty of 614 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 615 GNU Lesser General Public License for more details. 616 617 You should have received a copy of the GNU Lesser General Public License 618 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 619 */ 620 /** 621 * @file coder.js 622 * @author Marek Kotewicz <marek@ethdev.com> 623 * @date 2015 624 */ 625 626 var f = require('./formatters'); 627 628 var TypeAddress = require('./address'); 629 var TypeBool = require('./bool'); 630 var TypeInt = require('./int'); 631 var TypeUInt = require('./uint'); 632 var TypeDynamicBytes = require('./dynamicbytes'); 633 var TypeString = require('./string'); 634 var TypeReal = require('./real'); 635 var TypeUReal = require('./ureal'); 636 var TypeBytes = require('./bytes'); 637 var sha3 = require('../utils/sha3') 638 639 var isDynamic = function (Type, type) { 640 return Type.isDynamicType(type) || 641 Type.isDynamicArray(type); 642 }; 643 644 /** 645 * Coder prototype should be used to encode/decode params of any type 646 */ 647 var Coder = function (types) { 648 this._types = types; 649 }; 650 651 /** 652 * This method should be used to transform type to Type 653 * 654 * @method _requireType 655 * @param {String} type 656 * @returns {Type} 657 * @throws {Error} throws if no matching type is found 658 */ 659 Coder.prototype._requireType = function (type) { 660 var _type = this._types.filter(function (t) { 661 return t.isType(type); 662 })[0]; 663 664 if (!_type) { 665 throw Error('invalid type!: ' + type); 666 } 667 668 return _type; 669 }; 670 671 /** 672 * Should be used to encode plain param 673 * 674 * @method encodeParam 675 * @param {String} type 676 * @param {Object} plain param 677 * @return {String} encoded plain param 678 */ 679 Coder.prototype.encodeParam = function (type, param, indexed) { 680 indexed = indexed || false 681 if(type === "string" && indexed) { 682 return sha3(param) 683 } 684 return this.encodeParams([type], [param], indexed); 685 }; 686 687 /** 688 * Should be used to encode list of params 689 * 690 * @method encodeParams 691 * @param {Array} types 692 * @param {Array} params 693 * @return {String} encoded list of params 694 */ 695 Coder.prototype.encodeParams = function (types, params, indexed) { 696 indexed = indexed || false 697 var Types = this.getTypes(types); 698 699 var encodeds = Types.map(function (Type, index) { 700 return Type.encode(params[index], types[index]); 701 }); 702 703 var dynamicOffset = Types.reduce(function (acc, Type, index) { 704 var staticPartLength = Type.staticPartLength(types[index]); 705 var roundedStaticPartLength = Math.floor((staticPartLength + 31) / 32) * 32; 706 707 return acc + (isDynamic(Types[index], types[index]) ? 708 32 : 709 roundedStaticPartLength); 710 }, 0); 711 712 var result = this.encodeMultiWithOffset(types, Types, encodeds, dynamicOffset); 713 714 return result; 715 }; 716 717 Coder.prototype.encodeMultiWithOffset = function (types, Types, encodeds, dynamicOffset) { 718 var result = ""; 719 var self = this; 720 721 types.forEach(function (type, i) { 722 if (isDynamic(Types[i], types[i])) { 723 result += f.formatInputInt(dynamicOffset).encode(); 724 var e = self.encodeWithOffset(types[i], Types[i], encodeds[i], dynamicOffset); 725 dynamicOffset += e.length / 2; 726 } else { 727 // don't add length to dynamicOffset. it's already counted 728 result += self.encodeWithOffset(types[i], Types[i], encodeds[i], dynamicOffset); 729 } 730 731 // TODO: figure out nested arrays 732 }); 733 734 types.forEach(function (type, i) { 735 if (isDynamic(Types[i], types[i])) { 736 var e = self.encodeWithOffset(types[i], Types[i], encodeds[i], dynamicOffset); 737 dynamicOffset += e.length / 2; 738 result += e; 739 } 740 }); 741 return result; 742 }; 743 744 Coder.prototype.encodeWithOffset = function (type, Type, encoded, offset) { 745 /* jshint maxcomplexity: 17 */ 746 /* jshint maxdepth: 5 */ 747 748 var self = this; 749 var encodingMode={dynamic:1,static:2,other:3}; 750 751 var mode=(Type.isDynamicArray(type)?encodingMode.dynamic:(Type.isStaticArray(type)?encodingMode.static:encodingMode.other)); 752 753 if(mode !== encodingMode.other){ 754 var nestedName = Type.nestedName(type); 755 var nestedStaticPartLength = Type.staticPartLength(nestedName); 756 var result = (mode === encodingMode.dynamic ? encoded[0] : ''); 757 758 if (Type.isDynamicArray(nestedName)) { 759 var previousLength = (mode === encodingMode.dynamic ? 2 : 0); 760 761 for (var i = 0; i < encoded.length; i++) { 762 // calculate length of previous item 763 if(mode === encodingMode.dynamic){ 764 previousLength += +(encoded[i - 1])[0] || 0; 765 } 766 else if(mode === encodingMode.static){ 767 previousLength += +(encoded[i - 1] || [])[0] || 0; 768 } 769 result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode(); 770 } 771 } 772 773 var len= (mode === encodingMode.dynamic ? encoded.length-1 : encoded.length); 774 for (var c = 0; c < len; c++) { 775 var additionalOffset = result / 2; 776 if(mode === encodingMode.dynamic){ 777 result += self.encodeWithOffset(nestedName, Type, encoded[c + 1], offset + additionalOffset); 778 } 779 else if(mode === encodingMode.static){ 780 result += self.encodeWithOffset(nestedName, Type, encoded[c], offset + additionalOffset); 781 } 782 } 783 784 return result; 785 } 786 787 return encoded; 788 }; 789 790 791 /** 792 * Should be used to decode bytes to plain param 793 * 794 * @method decodeParam 795 * @param {String} type 796 * @param {String} bytes 797 * @return {Object} plain param 798 */ 799 Coder.prototype.decodeParam = function (type, bytes, indexed) { 800 indexed = indexed || false 801 return this.decodeParams([type], bytes, indexed)[0]; 802 }; 803 804 /** 805 * Should be used to decode list of params 806 * 807 * @method decodeParam 808 * @param {Array} types 809 * @param {String} bytes 810 * @return {Array} array of plain params 811 */ 812 Coder.prototype.decodeParams = function (types, bytes, indexed) { 813 indexed = indexed || false 814 var Types = this.getTypes(types); 815 var offsets = this.getOffsets(types, Types); 816 817 return Types.map(function (Type, index) { 818 return Type.decode(bytes, offsets[index], types[index], indexed); 819 }); 820 }; 821 822 Coder.prototype.getOffsets = function (types, Types) { 823 var lengths = Types.map(function (Type, index) { 824 return Type.staticPartLength(types[index]); 825 }); 826 827 for (var i = 1; i < lengths.length; i++) { 828 // sum with length of previous element 829 lengths[i] += lengths[i - 1]; 830 } 831 832 return lengths.map(function (length, index) { 833 // remove the current length, so the length is sum of previous elements 834 var staticPartLength = Types[index].staticPartLength(types[index]); 835 return length - staticPartLength; 836 }); 837 }; 838 839 Coder.prototype.getTypes = function (types) { 840 var self = this; 841 return types.map(function (type) { 842 return self._requireType(type); 843 }); 844 }; 845 846 var coder = new Coder([ 847 new TypeAddress(), 848 new TypeBool(), 849 new TypeInt(), 850 new TypeUInt(), 851 new TypeDynamicBytes(), 852 new TypeBytes(), 853 new TypeString(), 854 new TypeReal(), 855 new TypeUReal() 856 ]); 857 858 module.exports = coder; 859 860 },{"../utils/sha3":20,"./address":4,"./bool":5,"./bytes":6,"./dynamicbytes":8,"./formatters":9,"./int":10,"./real":12,"./string":13,"./uint":15,"./ureal":16}],8:[function(require,module,exports){ 861 var f = require('./formatters'); 862 var Type = require('./type'); 863 864 var TypeDynamicBytes = function () { 865 this._inputFormatter = f.formatInputDynamicBytes; 866 this._outputFormatter = f.formatOutputDynamicBytes; 867 }; 868 869 TypeDynamicBytes.prototype = new Type({}); 870 TypeDynamicBytes.prototype.constructor = TypeDynamicBytes; 871 872 TypeDynamicBytes.prototype.isType = function (name) { 873 return !!name.match(/^bytes(\[([0-9]*)\])*$/); 874 }; 875 876 TypeDynamicBytes.prototype.isDynamicType = function () { 877 return true; 878 }; 879 880 module.exports = TypeDynamicBytes; 881 882 },{"./formatters":9,"./type":14}],9:[function(require,module,exports){ 883 /* 884 This file is part of vnt.js. 885 886 vnt.js is free software: you can redistribute it and/or modify 887 it under the terms of the GNU Lesser General Public License as published by 888 the Free Software Foundation, either version 3 of the License, or 889 (at your option) any later version. 890 891 vnt.js is distributed in the hope that it will be useful, 892 but WITHOUT ANY WARRANTY; without even the implied warranty of 893 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 894 GNU Lesser General Public License for more details. 895 896 You should have received a copy of the GNU Lesser General Public License 897 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 898 */ 899 /** 900 * @file formatters.js 901 * @author Marek Kotewicz <marek@ethdev.com> 902 * @date 2015 903 */ 904 905 var BigNumber = require('bignumber.js'); 906 var utils = require('../utils/utils'); 907 var c = require('../utils/config'); 908 var Param = require('./param'); 909 910 911 /** 912 * Formats input value to byte representation of int 913 * If value is negative, return it's two's complement 914 * If the value is floating point, round it down 915 * 916 * @method formatInputInt 917 * @param {String|Number|BigNumber} value that needs to be formatted 918 * @returns {Param} 919 */ 920 var formatInputInt = function (value) { 921 BigNumber.config(c.VNT_BIGNUMBER_ROUNDING_MODE); 922 var result = utils.padLeft(utils.toTwosComplement(value).toString(16), 64); 923 return new Param(result); 924 }; 925 926 /** 927 * Formats input bytes 928 * 929 * @method formatInputBytes 930 * @param {String} 931 * @returns {Param} 932 */ 933 var formatInputBytes = function (value) { 934 var result = utils.toHex(value).substr(2); 935 var l = Math.floor((result.length + 63) / 64); 936 result = utils.padRight(result, l * 64); 937 return new Param(result); 938 }; 939 940 /** 941 * Formats input bytes 942 * 943 * @method formatDynamicInputBytes 944 * @param {String} 945 * @returns {Param} 946 */ 947 var formatInputDynamicBytes = function (value) { 948 var result = utils.toHex(value).substr(2); 949 var length = result.length / 2; 950 var l = Math.floor((result.length + 63) / 64); 951 result = utils.padRight(result, l * 64); 952 return new Param(formatInputInt(length).value + result); 953 }; 954 955 /** 956 * Formats input value to byte representation of string 957 * 958 * @method formatInputString 959 * @param {String} 960 * @returns {Param} 961 */ 962 var formatInputString = function (value) { 963 var result = utils.fromUtf8(value).substr(2); 964 var length = result.length / 2; 965 var l = Math.floor((result.length + 63) / 64); 966 result = utils.padRight(result, l * 64); 967 return new Param(formatInputInt(length).value + result); 968 }; 969 970 /** 971 * Formats input value to byte representation of bool 972 * 973 * @method formatInputBool 974 * @param {Boolean} 975 * @returns {Param} 976 */ 977 var formatInputBool = function (value) { 978 var result = '000000000000000000000000000000000000000000000000000000000000000' + (value ? '1' : '0'); 979 return new Param(result); 980 }; 981 982 /** 983 * Formats input value to byte representation of real 984 * Values are multiplied by 2^m and encoded as integers 985 * 986 * @method formatInputReal 987 * @param {String|Number|BigNumber} 988 * @returns {Param} 989 */ 990 var formatInputReal = function (value) { 991 return formatInputInt(new BigNumber(value).times(new BigNumber(2).pow(128))); 992 }; 993 994 /** 995 * Check if input value is negative 996 * 997 * @method signedIsNegative 998 * @param {String} value is hex format 999 * @returns {Boolean} true if it is negative, otherwise false 1000 */ 1001 var signedIsNegative = function (value) { 1002 return (new BigNumber(value.substr(0, 1), 16).toString(2).substr(0, 1)) === '1'; 1003 }; 1004 1005 /** 1006 * Formats right-aligned output bytes to int 1007 * 1008 * @method formatOutputInt 1009 * @param {Param} param 1010 * @returns {BigNumber} right-aligned output bytes formatted to big number 1011 */ 1012 var formatOutputInt = function (param) { 1013 var value = param.staticPart() || "0"; 1014 1015 // check if it's negative number 1016 // it it is, return two's complement 1017 if (signedIsNegative(value)) { 1018 return new BigNumber(value, 16).minus(new BigNumber('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16)).minus(1); 1019 } 1020 return new BigNumber(value, 16); 1021 }; 1022 1023 /** 1024 * Formats right-aligned output bytes to uint 1025 * 1026 * @method formatOutputUInt 1027 * @param {Param} 1028 * @returns {BigNumeber} right-aligned output bytes formatted to uint 1029 */ 1030 var formatOutputUInt = function (param) { 1031 var value = param.staticPart() || "0"; 1032 return new BigNumber(value, 16); 1033 }; 1034 1035 /** 1036 * Formats right-aligned output bytes to real 1037 * 1038 * @method formatOutputReal 1039 * @param {Param} 1040 * @returns {BigNumber} input bytes formatted to real 1041 */ 1042 var formatOutputReal = function (param) { 1043 return formatOutputInt(param).dividedBy(new BigNumber(2).pow(128)); 1044 }; 1045 1046 /** 1047 * Formats right-aligned output bytes to ureal 1048 * 1049 * @method formatOutputUReal 1050 * @param {Param} 1051 * @returns {BigNumber} input bytes formatted to ureal 1052 */ 1053 var formatOutputUReal = function (param) { 1054 return formatOutputUInt(param).dividedBy(new BigNumber(2).pow(128)); 1055 }; 1056 1057 /** 1058 * Should be used to format output bool 1059 * 1060 * @method formatOutputBool 1061 * @param {Param} 1062 * @returns {Boolean} right-aligned input bytes formatted to bool 1063 */ 1064 var formatOutputBool = function (param) { 1065 return param.staticPart() === '0000000000000000000000000000000000000000000000000000000000000001' ? true : false; 1066 }; 1067 1068 /** 1069 * Should be used to format output bytes 1070 * 1071 * @method formatOutputBytes 1072 * @param {Param} left-aligned hex representation of string 1073 * @param {String} name type name 1074 * @returns {String} hex string 1075 */ 1076 var formatOutputBytes = function (param, name) { 1077 var matches = name.match(/^bytes([0-9]*)/); 1078 var size = parseInt(matches[1]); 1079 return '0x' + param.staticPart().slice(0, 2 * size); 1080 }; 1081 1082 /** 1083 * Should be used to format output bytes 1084 * 1085 * @method formatOutputDynamicBytes 1086 * @param {Param} left-aligned hex representation of string 1087 * @returns {String} hex string 1088 */ 1089 var formatOutputDynamicBytes = function (param) { 1090 var length = (new BigNumber(param.dynamicPart().slice(0, 64), 16)).toNumber() * 2; 1091 return '0x' + param.dynamicPart().substr(64, length); 1092 }; 1093 1094 /** 1095 * Should be used to format output string 1096 * 1097 * @method formatOutputString 1098 * @param {Param} left-aligned hex representation of string 1099 * @returns {String} ascii string 1100 */ 1101 var formatOutputString = function (param, name, indexed) { 1102 indexed = indexed || false 1103 if(indexed) { 1104 // return utils.toUtf8(param.value) 1105 return "" 1106 } 1107 var length = (new BigNumber(param.dynamicPart().slice(0, 64), 16)).toNumber() * 2; 1108 return utils.toUtf8(param.dynamicPart().substr(64, length)); 1109 }; 1110 1111 /** 1112 * Should be used to format output address 1113 * 1114 * @method formatOutputAddress 1115 * @param {Param} right-aligned input bytes 1116 * @returns {String} address 1117 */ 1118 var formatOutputAddress = function (param) { 1119 var value = param.staticPart(); 1120 return "0x" + value.slice(value.length - 40, value.length); 1121 }; 1122 1123 module.exports = { 1124 formatInputInt: formatInputInt, 1125 formatInputBytes: formatInputBytes, 1126 formatInputDynamicBytes: formatInputDynamicBytes, 1127 formatInputString: formatInputString, 1128 formatInputBool: formatInputBool, 1129 formatInputReal: formatInputReal, 1130 formatOutputInt: formatOutputInt, 1131 formatOutputUInt: formatOutputUInt, 1132 formatOutputReal: formatOutputReal, 1133 formatOutputUReal: formatOutputUReal, 1134 formatOutputBool: formatOutputBool, 1135 formatOutputBytes: formatOutputBytes, 1136 formatOutputDynamicBytes: formatOutputDynamicBytes, 1137 formatOutputString: formatOutputString, 1138 formatOutputAddress: formatOutputAddress 1139 }; 1140 1141 },{"../utils/config":18,"../utils/utils":21,"./param":11,"bignumber.js":"bignumber.js"}],10:[function(require,module,exports){ 1142 var f = require('./formatters'); 1143 var Type = require('./type'); 1144 1145 /** 1146 * TypeInt is a prootype that represents int type 1147 * It matches: 1148 * int 1149 * int[] 1150 * int[4] 1151 * int[][] 1152 * int[3][] 1153 * int[][6][], ... 1154 * int32 1155 * int64[] 1156 * int8[4] 1157 * int256[][] 1158 * int[3][] 1159 * int64[][6][], ... 1160 */ 1161 var TypeInt = function () { 1162 this._inputFormatter = f.formatInputInt; 1163 this._outputFormatter = f.formatOutputInt; 1164 }; 1165 1166 TypeInt.prototype = new Type({}); 1167 TypeInt.prototype.constructor = TypeInt; 1168 1169 TypeInt.prototype.isType = function (name) { 1170 return !!name.match(/^int([0-9]*)?(\[([0-9]*)\])*$/); 1171 }; 1172 1173 module.exports = TypeInt; 1174 1175 },{"./formatters":9,"./type":14}],11:[function(require,module,exports){ 1176 /* 1177 This file is part of vnt.js. 1178 1179 vnt.js is free software: you can redistribute it and/or modify 1180 it under the terms of the GNU Lesser General Public License as published by 1181 the Free Software Foundation, either version 3 of the License, or 1182 (at your option) any later version. 1183 1184 vnt.js is distributed in the hope that it will be useful, 1185 but WITHOUT ANY WARRANTY; without even the implied warranty of 1186 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1187 GNU Lesser General Public License for more details. 1188 1189 You should have received a copy of the GNU Lesser General Public License 1190 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 1191 */ 1192 /** 1193 * @file param.js 1194 * @author Marek Kotewicz <marek@ethdev.com> 1195 * @date 2015 1196 */ 1197 1198 var utils = require('../utils/utils'); 1199 1200 /** 1201 * Param object prototype. 1202 * Should be used when encoding, decoding bytes 1203 */ 1204 var Param = function (value, offset) { 1205 this.value = value || ''; 1206 this.offset = offset; // offset in bytes 1207 }; 1208 1209 /** 1210 * This method should be used to get length of params's dynamic part 1211 * 1212 * @method dynamicPartLength 1213 * @returns {Number} length of dynamic part (in bytes) 1214 */ 1215 Param.prototype.dynamicPartLength = function () { 1216 return this.dynamicPart().length / 2; 1217 }; 1218 1219 /** 1220 * This method should be used to create copy of param with different offset 1221 * 1222 * @method withOffset 1223 * @param {Number} offset length in bytes 1224 * @returns {Param} new param with applied offset 1225 */ 1226 Param.prototype.withOffset = function (offset) { 1227 return new Param(this.value, offset); 1228 }; 1229 1230 /** 1231 * This method should be used to combine params together 1232 * eg. when appending an array 1233 * 1234 * @method combine 1235 * @param {Param} param with which we should combine 1236 * @param {Param} result of combination 1237 */ 1238 Param.prototype.combine = function (param) { 1239 return new Param(this.value + param.value); 1240 }; 1241 1242 /** 1243 * This method should be called to check if param has dynamic size. 1244 * If it has, it returns true, otherwise false 1245 * 1246 * @method isDynamic 1247 * @returns {Boolean} 1248 */ 1249 Param.prototype.isDynamic = function () { 1250 return this.offset !== undefined; 1251 }; 1252 1253 /** 1254 * This method should be called to transform offset to bytes 1255 * 1256 * @method offsetAsBytes 1257 * @returns {String} bytes representation of offset 1258 */ 1259 Param.prototype.offsetAsBytes = function () { 1260 return !this.isDynamic() ? '' : utils.padLeft(utils.toTwosComplement(this.offset).toString(16), 64); 1261 }; 1262 1263 /** 1264 * This method should be called to get static part of param 1265 * 1266 * @method staticPart 1267 * @returns {String} offset if it is a dynamic param, otherwise value 1268 */ 1269 Param.prototype.staticPart = function () { 1270 if (!this.isDynamic()) { 1271 return this.value; 1272 } 1273 return this.offsetAsBytes(); 1274 }; 1275 1276 /** 1277 * This method should be called to get dynamic part of param 1278 * 1279 * @method dynamicPart 1280 * @returns {String} returns a value if it is a dynamic param, otherwise empty string 1281 */ 1282 Param.prototype.dynamicPart = function () { 1283 return this.isDynamic() ? this.value : ''; 1284 }; 1285 1286 /** 1287 * This method should be called to encode param 1288 * 1289 * @method encode 1290 * @returns {String} 1291 */ 1292 Param.prototype.encode = function () { 1293 return this.staticPart() + this.dynamicPart(); 1294 }; 1295 1296 /** 1297 * This method should be called to encode array of params 1298 * 1299 * @method encodeList 1300 * @param {Array[Param]} params 1301 * @returns {String} 1302 */ 1303 Param.encodeList = function (params) { 1304 1305 // updating offsets 1306 var totalOffset = params.length * 32; 1307 var offsetParams = params.map(function (param) { 1308 if (!param.isDynamic()) { 1309 return param; 1310 } 1311 var offset = totalOffset; 1312 totalOffset += param.dynamicPartLength(); 1313 return param.withOffset(offset); 1314 }); 1315 1316 // encode everything! 1317 return offsetParams.reduce(function (result, param) { 1318 return result + param.dynamicPart(); 1319 }, offsetParams.reduce(function (result, param) { 1320 return result + param.staticPart(); 1321 }, '')); 1322 }; 1323 1324 1325 1326 module.exports = Param; 1327 1328 },{"../utils/utils":21}],12:[function(require,module,exports){ 1329 var f = require('./formatters'); 1330 var Type = require('./type'); 1331 1332 /** 1333 * TypeReal is a prootype that represents real type 1334 * It matches: 1335 * real 1336 * real[] 1337 * real[4] 1338 * real[][] 1339 * real[3][] 1340 * real[][6][], ... 1341 * real32 1342 * real64[] 1343 * real8[4] 1344 * real256[][] 1345 * real[3][] 1346 * real64[][6][], ... 1347 */ 1348 var TypeReal = function () { 1349 this._inputFormatter = f.formatInputReal; 1350 this._outputFormatter = f.formatOutputReal; 1351 }; 1352 1353 TypeReal.prototype = new Type({}); 1354 TypeReal.prototype.constructor = TypeReal; 1355 1356 TypeReal.prototype.isType = function (name) { 1357 return !!name.match(/real([0-9]*)?(\[([0-9]*)\])?/); 1358 }; 1359 1360 module.exports = TypeReal; 1361 1362 },{"./formatters":9,"./type":14}],13:[function(require,module,exports){ 1363 var f = require('./formatters'); 1364 var Type = require('./type'); 1365 1366 var TypeString = function () { 1367 this._inputFormatter = f.formatInputString; 1368 this._outputFormatter = f.formatOutputString; 1369 }; 1370 1371 TypeString.prototype = new Type({}); 1372 TypeString.prototype.constructor = TypeString; 1373 1374 TypeString.prototype.isType = function (name) { 1375 return !!name.match(/^string(\[([0-9]*)\])*$/); 1376 }; 1377 1378 TypeString.prototype.isDynamicType = function () { 1379 return true; 1380 }; 1381 1382 module.exports = TypeString; 1383 1384 },{"./formatters":9,"./type":14}],14:[function(require,module,exports){ 1385 var f = require('./formatters'); 1386 var Param = require('./param'); 1387 1388 /** 1389 * Type prototype is used to encode/decode params of certain type 1390 */ 1391 var Type = function (config) { 1392 this._inputFormatter = config.inputFormatter; 1393 this._outputFormatter = config.outputFormatter; 1394 }; 1395 1396 /** 1397 * Should be used to determine if this Type do match given name 1398 * 1399 * @method isType 1400 * @param {String} name 1401 * @return {Bool} true if type match this Type, otherwise false 1402 */ 1403 Type.prototype.isType = function (name) { 1404 throw "this method should be overrwritten for type " + name; 1405 }; 1406 1407 /** 1408 * Should be used to determine what is the length of static part in given type 1409 * 1410 * @method staticPartLength 1411 * @param {String} name 1412 * @return {Number} length of static part in bytes 1413 */ 1414 Type.prototype.staticPartLength = function (name) { 1415 // If name isn't an array then treat it like a single element array. 1416 return (this.nestedTypes(name) || ['[1]']) 1417 .map(function (type) { 1418 // the length of the nested array 1419 return parseInt(type.slice(1, -1), 10) || 1; 1420 }) 1421 .reduce(function (previous, current) { 1422 return previous * current; 1423 // all basic types are 32 bytes long 1424 }, 32); 1425 }; 1426 1427 /** 1428 * Should be used to determine if type is dynamic array 1429 * eg: 1430 * "type[]" => true 1431 * "type[4]" => false 1432 * 1433 * @method isDynamicArray 1434 * @param {String} name 1435 * @return {Bool} true if the type is dynamic array 1436 */ 1437 Type.prototype.isDynamicArray = function (name) { 1438 var nestedTypes = this.nestedTypes(name); 1439 return !!nestedTypes && !nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g); 1440 }; 1441 1442 /** 1443 * Should be used to determine if type is static array 1444 * eg: 1445 * "type[]" => false 1446 * "type[4]" => true 1447 * 1448 * @method isStaticArray 1449 * @param {String} name 1450 * @return {Bool} true if the type is static array 1451 */ 1452 Type.prototype.isStaticArray = function (name) { 1453 var nestedTypes = this.nestedTypes(name); 1454 return !!nestedTypes && !!nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g); 1455 }; 1456 1457 /** 1458 * Should return length of static array 1459 * eg. 1460 * "int[32]" => 32 1461 * "int256[14]" => 14 1462 * "int[2][3]" => 3 1463 * "int" => 1 1464 * "int[1]" => 1 1465 * "int[]" => 1 1466 * 1467 * @method staticArrayLength 1468 * @param {String} name 1469 * @return {Number} static array length 1470 */ 1471 Type.prototype.staticArrayLength = function (name) { 1472 var nestedTypes = this.nestedTypes(name); 1473 if (nestedTypes) { 1474 return parseInt(nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g) || 1); 1475 } 1476 return 1; 1477 }; 1478 1479 /** 1480 * Should return nested type 1481 * eg. 1482 * "int[32]" => "int" 1483 * "int256[14]" => "int256" 1484 * "int[2][3]" => "int[2]" 1485 * "int" => "int" 1486 * "int[]" => "int" 1487 * 1488 * @method nestedName 1489 * @param {String} name 1490 * @return {String} nested name 1491 */ 1492 Type.prototype.nestedName = function (name) { 1493 // remove last [] in name 1494 var nestedTypes = this.nestedTypes(name); 1495 if (!nestedTypes) { 1496 return name; 1497 } 1498 1499 return name.substr(0, name.length - nestedTypes[nestedTypes.length - 1].length); 1500 }; 1501 1502 /** 1503 * Should return true if type has dynamic size by default 1504 * such types are "string", "bytes" 1505 * 1506 * @method isDynamicType 1507 * @param {String} name 1508 * @return {Bool} true if is dynamic, otherwise false 1509 */ 1510 Type.prototype.isDynamicType = function () { 1511 return false; 1512 }; 1513 1514 /** 1515 * Should return array of nested types 1516 * eg. 1517 * "int[2][3][]" => ["[2]", "[3]", "[]"] 1518 * "int[] => ["[]"] 1519 * "int" => null 1520 * 1521 * @method nestedTypes 1522 * @param {String} name 1523 * @return {Array} array of nested types 1524 */ 1525 Type.prototype.nestedTypes = function (name) { 1526 // return list of strings eg. "[]", "[3]", "[]", "[2]" 1527 return name.match(/(\[[0-9]*\])/g); 1528 }; 1529 1530 /** 1531 * Should be used to encode the value 1532 * 1533 * @method encode 1534 * @param {Object} value 1535 * @param {String} name 1536 * @return {String} encoded value 1537 */ 1538 Type.prototype.encode = function (value, name) { 1539 var self = this; 1540 if (this.isDynamicArray(name)) { 1541 1542 return (function () { 1543 var length = value.length; // in int 1544 var nestedName = self.nestedName(name); 1545 1546 var result = []; 1547 result.push(f.formatInputInt(length).encode()); 1548 1549 value.forEach(function (v) { 1550 result.push(self.encode(v, nestedName)); 1551 }); 1552 1553 return result; 1554 })(); 1555 1556 } else if (this.isStaticArray(name)) { 1557 1558 return (function () { 1559 var length = self.staticArrayLength(name); // in int 1560 var nestedName = self.nestedName(name); 1561 1562 var result = []; 1563 for (var i = 0; i < length; i++) { 1564 result.push(self.encode(value[i], nestedName)); 1565 } 1566 1567 return result; 1568 })(); 1569 1570 } 1571 1572 return this._inputFormatter(value, name).encode(); 1573 }; 1574 1575 /** 1576 * Should be used to decode value from bytes 1577 * 1578 * @method decode 1579 * @param {String} bytes 1580 * @param {Number} offset in bytes 1581 * @param {String} name type name 1582 * @returns {Object} decoded value 1583 */ 1584 Type.prototype.decode = function (bytes, offset, name, indexed) { 1585 indexed = indexed || false 1586 var self = this; 1587 1588 if (indexed) { 1589 var length = this.staticPartLength(name); 1590 var param = new Param(bytes.substr(offset * 2, length * 2)); 1591 return this._outputFormatter(param, name, indexed); 1592 } 1593 1594 if (this.isDynamicArray(name)) { 1595 1596 return (function () { 1597 var arrayOffset = parseInt('0x' + bytes.substr(offset * 2, 64)); // in bytes 1598 var length = parseInt('0x' + bytes.substr(arrayOffset * 2, 64)); // in int 1599 var arrayStart = arrayOffset + 32; // array starts after length; // in bytes 1600 1601 var nestedName = self.nestedName(name); 1602 var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes 1603 var roundedNestedStaticPartLength = Math.floor((nestedStaticPartLength + 31) / 32) * 32; 1604 var result = []; 1605 1606 for (var i = 0; i < length * roundedNestedStaticPartLength; i += roundedNestedStaticPartLength) { 1607 result.push(self.decode(bytes, arrayStart + i, nestedName)); 1608 } 1609 1610 return result; 1611 })(); 1612 1613 } else if (this.isStaticArray(name)) { 1614 1615 return (function () { 1616 var length = self.staticArrayLength(name); // in int 1617 var arrayStart = offset; // in bytes 1618 1619 var nestedName = self.nestedName(name); 1620 var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes 1621 var roundedNestedStaticPartLength = Math.floor((nestedStaticPartLength + 31) / 32) * 32; 1622 var result = []; 1623 1624 for (var i = 0; i < length * roundedNestedStaticPartLength; i += roundedNestedStaticPartLength) { 1625 result.push(self.decode(bytes, arrayStart + i, nestedName)); 1626 } 1627 1628 return result; 1629 })(); 1630 } else if (this.isDynamicType(name)) { 1631 1632 return (function () { 1633 var dynamicOffset = parseInt('0x' + bytes.substr(offset * 2, 64)); // in bytes 1634 var length = parseInt('0x' + bytes.substr(dynamicOffset * 2, 64)); // in bytes 1635 var roundedLength = Math.floor((length + 31) / 32); // in int 1636 var param = new Param(bytes.substr(dynamicOffset * 2, ( 1 + roundedLength) * 64), 0); 1637 return self._outputFormatter(param, name); 1638 })(); 1639 } 1640 1641 var length = this.staticPartLength(name); 1642 var param = new Param(bytes.substr(offset * 2, length * 2)); 1643 return this._outputFormatter(param, name); 1644 }; 1645 1646 module.exports = Type; 1647 1648 },{"./formatters":9,"./param":11}],15:[function(require,module,exports){ 1649 var f = require('./formatters'); 1650 var Type = require('./type'); 1651 1652 /** 1653 * TypeUInt is a prootype that represents uint type 1654 * It matches: 1655 * uint 1656 * uint[] 1657 * uint[4] 1658 * uint[][] 1659 * uint[3][] 1660 * uint[][6][], ... 1661 * uint32 1662 * uint64[] 1663 * uint8[4] 1664 * uint256[][] 1665 * uint[3][] 1666 * uint64[][6][], ... 1667 */ 1668 var TypeUInt = function () { 1669 this._inputFormatter = f.formatInputInt; 1670 this._outputFormatter = f.formatOutputUInt; 1671 }; 1672 1673 TypeUInt.prototype = new Type({}); 1674 TypeUInt.prototype.constructor = TypeUInt; 1675 1676 TypeUInt.prototype.isType = function (name) { 1677 return !!name.match(/^uint([0-9]*)?(\[([0-9]*)\])*$/); 1678 }; 1679 1680 module.exports = TypeUInt; 1681 1682 },{"./formatters":9,"./type":14}],16:[function(require,module,exports){ 1683 var f = require('./formatters'); 1684 var Type = require('./type'); 1685 1686 /** 1687 * TypeUReal is a prootype that represents ureal type 1688 * It matches: 1689 * ureal 1690 * ureal[] 1691 * ureal[4] 1692 * ureal[][] 1693 * ureal[3][] 1694 * ureal[][6][], ... 1695 * ureal32 1696 * ureal64[] 1697 * ureal8[4] 1698 * ureal256[][] 1699 * ureal[3][] 1700 * ureal64[][6][], ... 1701 */ 1702 var TypeUReal = function () { 1703 this._inputFormatter = f.formatInputReal; 1704 this._outputFormatter = f.formatOutputUReal; 1705 }; 1706 1707 TypeUReal.prototype = new Type({}); 1708 TypeUReal.prototype.constructor = TypeUReal; 1709 1710 TypeUReal.prototype.isType = function (name) { 1711 return !!name.match(/^ureal([0-9]*)?(\[([0-9]*)\])*$/); 1712 }; 1713 1714 module.exports = TypeUReal; 1715 1716 },{"./formatters":9,"./type":14}],17:[function(require,module,exports){ 1717 'use strict'; 1718 1719 // go env doesn't have and need XMLHttpRequest 1720 if (typeof XMLHttpRequest === 'undefined') { 1721 exports.XMLHttpRequest = {}; 1722 } else { 1723 exports.XMLHttpRequest = XMLHttpRequest; // jshint ignore:line 1724 } 1725 1726 1727 },{}],18:[function(require,module,exports){ 1728 /* 1729 This file is part of vnt.js. 1730 1731 vnt.js is free software: you can redistribute it and/or modify 1732 it under the terms of the GNU Lesser General Public License as published by 1733 the Free Software Foundation, either version 3 of the License, or 1734 (at your option) any later version. 1735 1736 vnt.js is distributed in the hope that it will be useful, 1737 but WITHOUT ANY WARRANTY; without even the implied warranty of 1738 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1739 GNU Lesser General Public License for more details. 1740 1741 You should have received a copy of the GNU Lesser General Public License 1742 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 1743 */ 1744 /** @file config.js 1745 * @authors: 1746 * Marek Kotewicz <marek@ethdev.com> 1747 * @date 2015 1748 */ 1749 1750 /** 1751 * Utils 1752 * 1753 * @module utils 1754 */ 1755 1756 /** 1757 * Utility functions 1758 * 1759 * @class [utils] config 1760 * @constructor 1761 */ 1762 1763 1764 /// required to define VNT_BIGNUMBER_ROUNDING_MODE 1765 var BigNumber = require('bignumber.js'); 1766 1767 var VNT_UNITS = [ 1768 'wei', 1769 'Kwei', 1770 'Mwei', 1771 'Gwei', 1772 'microvnt', 1773 'micro', 1774 'millivnt', 1775 'milli', 1776 'vnt' 1777 ]; 1778 1779 module.exports = { 1780 VNT_PADDING: 32, 1781 VNT_SIGNATURE_LENGTH: 4, 1782 VNT_UNITS: VNT_UNITS, 1783 VNT_BIGNUMBER_ROUNDING_MODE: { ROUNDING_MODE: BigNumber.ROUND_DOWN }, 1784 VNT_POLLING_TIMEOUT: 1000/2, 1785 defaultBlock: 'latest', 1786 defaultAccount: undefined 1787 }; 1788 1789 },{"bignumber.js":"bignumber.js"}],19:[function(require,module,exports){ 1790 // This was ported from https://github.com/emn178/js-sha3, with some minor 1791 // modifications and pruning. It is licensed under MIT: 1792 // 1793 // Copyright 2015-2016 Chen, Yi-Cyuan 1794 // 1795 // Permission is hereby granted, free of charge, to any person obtaining 1796 // a copy of this software and associated documentation files (the 1797 // "Software"), to deal in the Software without restriction, including 1798 // without limitation the rights to use, copy, modify, merge, publish, 1799 // distribute, sublicense, and/or sell copies of the Software, and to 1800 // permit persons to whom the Software is furnished to do so, subject to 1801 // the following conditions: 1802 // 1803 // The above copyright notice and this permission notice shall be 1804 // included in all copies or substantial portions of the Software. 1805 // 1806 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 1807 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 1808 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 1809 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 1810 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 1811 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 1812 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 1813 1814 var HEX_CHARS = '0123456789abcdef'.split(''); 1815 var KECCAK_PADDING = [1, 256, 65536, 16777216]; 1816 var SHIFT = [0, 8, 16, 24]; 1817 var RC = [1, 0, 32898, 0, 32906, 2147483648, 2147516416, 2147483648, 32907, 0, 2147483649, 0, 2147516545, 2147483648, 32777, 2147483648, 138, 0, 136, 0, 2147516425, 0, 2147483658, 0, 2147516555, 0, 139, 2147483648, 32905, 2147483648, 32771, 2147483648, 32770, 2147483648, 128, 2147483648, 32778, 0, 2147483658, 2147483648, 2147516545, 2147483648, 32896, 2147483648, 2147483649, 0, 2147516424, 2147483648]; 1818 1819 var Keccak = function Keccak(bits) { 1820 return { 1821 blocks: [], 1822 reset: true, 1823 block: 0, 1824 start: 0, 1825 blockCount: 1600 - (bits << 1) >> 5, 1826 outputBlocks: bits >> 5, 1827 s: function (s) { 1828 return [].concat(s, s, s, s, s); 1829 }([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) 1830 }; 1831 }; 1832 1833 var update = function update(state, message) { 1834 var length = message.length, 1835 blocks = state.blocks, 1836 byteCount = state.blockCount << 2, 1837 blockCount = state.blockCount, 1838 outputBlocks = state.outputBlocks, 1839 s = state.s, 1840 index = 0, 1841 i, 1842 code; 1843 1844 // update 1845 while (index < length) { 1846 if (state.reset) { 1847 state.reset = false; 1848 blocks[0] = state.block; 1849 for (i = 1; i < blockCount + 1; ++i) { 1850 blocks[i] = 0; 1851 } 1852 } 1853 if (typeof message !== "string") { 1854 for (i = state.start; index < length && i < byteCount; ++index) { 1855 blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; 1856 } 1857 } else { 1858 for (i = state.start; index < length && i < byteCount; ++index) { 1859 code = message.charCodeAt(index); 1860 if (code < 0x80) { 1861 blocks[i >> 2] |= code << SHIFT[i++ & 3]; 1862 } else if (code < 0x800) { 1863 blocks[i >> 2] |= (0xc0 | code >> 6) << SHIFT[i++ & 3]; 1864 blocks[i >> 2] |= (0x80 | code & 0x3f) << SHIFT[i++ & 3]; 1865 } else if (code < 0xd800 || code >= 0xe000) { 1866 blocks[i >> 2] |= (0xe0 | code >> 12) << SHIFT[i++ & 3]; 1867 blocks[i >> 2] |= (0x80 | code >> 6 & 0x3f) << SHIFT[i++ & 3]; 1868 blocks[i >> 2] |= (0x80 | code & 0x3f) << SHIFT[i++ & 3]; 1869 } else { 1870 code = 0x10000 + ((code & 0x3ff) << 10 | message.charCodeAt(++index) & 0x3ff); 1871 blocks[i >> 2] |= (0xf0 | code >> 18) << SHIFT[i++ & 3]; 1872 blocks[i >> 2] |= (0x80 | code >> 12 & 0x3f) << SHIFT[i++ & 3]; 1873 blocks[i >> 2] |= (0x80 | code >> 6 & 0x3f) << SHIFT[i++ & 3]; 1874 blocks[i >> 2] |= (0x80 | code & 0x3f) << SHIFT[i++ & 3]; 1875 } 1876 } 1877 } 1878 state.lastByteIndex = i; 1879 if (i >= byteCount) { 1880 state.start = i - byteCount; 1881 state.block = blocks[blockCount]; 1882 for (i = 0; i < blockCount; ++i) { 1883 s[i] ^= blocks[i]; 1884 } 1885 f(s); 1886 state.reset = true; 1887 } else { 1888 state.start = i; 1889 } 1890 } 1891 1892 // finalize 1893 i = state.lastByteIndex; 1894 blocks[i >> 2] |= KECCAK_PADDING[i & 3]; 1895 if (state.lastByteIndex === byteCount) { 1896 blocks[0] = blocks[blockCount]; 1897 for (i = 1; i < blockCount + 1; ++i) { 1898 blocks[i] = 0; 1899 } 1900 } 1901 blocks[blockCount - 1] |= 0x80000000; 1902 for (i = 0; i < blockCount; ++i) { 1903 s[i] ^= blocks[i]; 1904 } 1905 f(s); 1906 1907 // toString 1908 var hex = '', 1909 i = 0, 1910 j = 0, 1911 block; 1912 while (j < outputBlocks) { 1913 for (i = 0; i < blockCount && j < outputBlocks; ++i, ++j) { 1914 block = s[i]; 1915 hex += HEX_CHARS[block >> 4 & 0x0F] + HEX_CHARS[block & 0x0F] + HEX_CHARS[block >> 12 & 0x0F] + HEX_CHARS[block >> 8 & 0x0F] + HEX_CHARS[block >> 20 & 0x0F] + HEX_CHARS[block >> 16 & 0x0F] + HEX_CHARS[block >> 28 & 0x0F] + HEX_CHARS[block >> 24 & 0x0F]; 1916 } 1917 if (j % blockCount === 0) { 1918 f(s); 1919 i = 0; 1920 } 1921 } 1922 return "0x" + hex; 1923 }; 1924 1925 var f = function f(s) { 1926 var h, l, n, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49; 1927 1928 for (n = 0; n < 48; n += 2) { 1929 c0 = s[0] ^ s[10] ^ s[20] ^ s[30] ^ s[40]; 1930 c1 = s[1] ^ s[11] ^ s[21] ^ s[31] ^ s[41]; 1931 c2 = s[2] ^ s[12] ^ s[22] ^ s[32] ^ s[42]; 1932 c3 = s[3] ^ s[13] ^ s[23] ^ s[33] ^ s[43]; 1933 c4 = s[4] ^ s[14] ^ s[24] ^ s[34] ^ s[44]; 1934 c5 = s[5] ^ s[15] ^ s[25] ^ s[35] ^ s[45]; 1935 c6 = s[6] ^ s[16] ^ s[26] ^ s[36] ^ s[46]; 1936 c7 = s[7] ^ s[17] ^ s[27] ^ s[37] ^ s[47]; 1937 c8 = s[8] ^ s[18] ^ s[28] ^ s[38] ^ s[48]; 1938 c9 = s[9] ^ s[19] ^ s[29] ^ s[39] ^ s[49]; 1939 1940 h = c8 ^ (c2 << 1 | c3 >>> 31); 1941 l = c9 ^ (c3 << 1 | c2 >>> 31); 1942 s[0] ^= h; 1943 s[1] ^= l; 1944 s[10] ^= h; 1945 s[11] ^= l; 1946 s[20] ^= h; 1947 s[21] ^= l; 1948 s[30] ^= h; 1949 s[31] ^= l; 1950 s[40] ^= h; 1951 s[41] ^= l; 1952 h = c0 ^ (c4 << 1 | c5 >>> 31); 1953 l = c1 ^ (c5 << 1 | c4 >>> 31); 1954 s[2] ^= h; 1955 s[3] ^= l; 1956 s[12] ^= h; 1957 s[13] ^= l; 1958 s[22] ^= h; 1959 s[23] ^= l; 1960 s[32] ^= h; 1961 s[33] ^= l; 1962 s[42] ^= h; 1963 s[43] ^= l; 1964 h = c2 ^ (c6 << 1 | c7 >>> 31); 1965 l = c3 ^ (c7 << 1 | c6 >>> 31); 1966 s[4] ^= h; 1967 s[5] ^= l; 1968 s[14] ^= h; 1969 s[15] ^= l; 1970 s[24] ^= h; 1971 s[25] ^= l; 1972 s[34] ^= h; 1973 s[35] ^= l; 1974 s[44] ^= h; 1975 s[45] ^= l; 1976 h = c4 ^ (c8 << 1 | c9 >>> 31); 1977 l = c5 ^ (c9 << 1 | c8 >>> 31); 1978 s[6] ^= h; 1979 s[7] ^= l; 1980 s[16] ^= h; 1981 s[17] ^= l; 1982 s[26] ^= h; 1983 s[27] ^= l; 1984 s[36] ^= h; 1985 s[37] ^= l; 1986 s[46] ^= h; 1987 s[47] ^= l; 1988 h = c6 ^ (c0 << 1 | c1 >>> 31); 1989 l = c7 ^ (c1 << 1 | c0 >>> 31); 1990 s[8] ^= h; 1991 s[9] ^= l; 1992 s[18] ^= h; 1993 s[19] ^= l; 1994 s[28] ^= h; 1995 s[29] ^= l; 1996 s[38] ^= h; 1997 s[39] ^= l; 1998 s[48] ^= h; 1999 s[49] ^= l; 2000 2001 b0 = s[0]; 2002 b1 = s[1]; 2003 b32 = s[11] << 4 | s[10] >>> 28; 2004 b33 = s[10] << 4 | s[11] >>> 28; 2005 b14 = s[20] << 3 | s[21] >>> 29; 2006 b15 = s[21] << 3 | s[20] >>> 29; 2007 b46 = s[31] << 9 | s[30] >>> 23; 2008 b47 = s[30] << 9 | s[31] >>> 23; 2009 b28 = s[40] << 18 | s[41] >>> 14; 2010 b29 = s[41] << 18 | s[40] >>> 14; 2011 b20 = s[2] << 1 | s[3] >>> 31; 2012 b21 = s[3] << 1 | s[2] >>> 31; 2013 b2 = s[13] << 12 | s[12] >>> 20; 2014 b3 = s[12] << 12 | s[13] >>> 20; 2015 b34 = s[22] << 10 | s[23] >>> 22; 2016 b35 = s[23] << 10 | s[22] >>> 22; 2017 b16 = s[33] << 13 | s[32] >>> 19; 2018 b17 = s[32] << 13 | s[33] >>> 19; 2019 b48 = s[42] << 2 | s[43] >>> 30; 2020 b49 = s[43] << 2 | s[42] >>> 30; 2021 b40 = s[5] << 30 | s[4] >>> 2; 2022 b41 = s[4] << 30 | s[5] >>> 2; 2023 b22 = s[14] << 6 | s[15] >>> 26; 2024 b23 = s[15] << 6 | s[14] >>> 26; 2025 b4 = s[25] << 11 | s[24] >>> 21; 2026 b5 = s[24] << 11 | s[25] >>> 21; 2027 b36 = s[34] << 15 | s[35] >>> 17; 2028 b37 = s[35] << 15 | s[34] >>> 17; 2029 b18 = s[45] << 29 | s[44] >>> 3; 2030 b19 = s[44] << 29 | s[45] >>> 3; 2031 b10 = s[6] << 28 | s[7] >>> 4; 2032 b11 = s[7] << 28 | s[6] >>> 4; 2033 b42 = s[17] << 23 | s[16] >>> 9; 2034 b43 = s[16] << 23 | s[17] >>> 9; 2035 b24 = s[26] << 25 | s[27] >>> 7; 2036 b25 = s[27] << 25 | s[26] >>> 7; 2037 b6 = s[36] << 21 | s[37] >>> 11; 2038 b7 = s[37] << 21 | s[36] >>> 11; 2039 b38 = s[47] << 24 | s[46] >>> 8; 2040 b39 = s[46] << 24 | s[47] >>> 8; 2041 b30 = s[8] << 27 | s[9] >>> 5; 2042 b31 = s[9] << 27 | s[8] >>> 5; 2043 b12 = s[18] << 20 | s[19] >>> 12; 2044 b13 = s[19] << 20 | s[18] >>> 12; 2045 b44 = s[29] << 7 | s[28] >>> 25; 2046 b45 = s[28] << 7 | s[29] >>> 25; 2047 b26 = s[38] << 8 | s[39] >>> 24; 2048 b27 = s[39] << 8 | s[38] >>> 24; 2049 b8 = s[48] << 14 | s[49] >>> 18; 2050 b9 = s[49] << 14 | s[48] >>> 18; 2051 2052 s[0] = b0 ^ ~b2 & b4; 2053 s[1] = b1 ^ ~b3 & b5; 2054 s[10] = b10 ^ ~b12 & b14; 2055 s[11] = b11 ^ ~b13 & b15; 2056 s[20] = b20 ^ ~b22 & b24; 2057 s[21] = b21 ^ ~b23 & b25; 2058 s[30] = b30 ^ ~b32 & b34; 2059 s[31] = b31 ^ ~b33 & b35; 2060 s[40] = b40 ^ ~b42 & b44; 2061 s[41] = b41 ^ ~b43 & b45; 2062 s[2] = b2 ^ ~b4 & b6; 2063 s[3] = b3 ^ ~b5 & b7; 2064 s[12] = b12 ^ ~b14 & b16; 2065 s[13] = b13 ^ ~b15 & b17; 2066 s[22] = b22 ^ ~b24 & b26; 2067 s[23] = b23 ^ ~b25 & b27; 2068 s[32] = b32 ^ ~b34 & b36; 2069 s[33] = b33 ^ ~b35 & b37; 2070 s[42] = b42 ^ ~b44 & b46; 2071 s[43] = b43 ^ ~b45 & b47; 2072 s[4] = b4 ^ ~b6 & b8; 2073 s[5] = b5 ^ ~b7 & b9; 2074 s[14] = b14 ^ ~b16 & b18; 2075 s[15] = b15 ^ ~b17 & b19; 2076 s[24] = b24 ^ ~b26 & b28; 2077 s[25] = b25 ^ ~b27 & b29; 2078 s[34] = b34 ^ ~b36 & b38; 2079 s[35] = b35 ^ ~b37 & b39; 2080 s[44] = b44 ^ ~b46 & b48; 2081 s[45] = b45 ^ ~b47 & b49; 2082 s[6] = b6 ^ ~b8 & b0; 2083 s[7] = b7 ^ ~b9 & b1; 2084 s[16] = b16 ^ ~b18 & b10; 2085 s[17] = b17 ^ ~b19 & b11; 2086 s[26] = b26 ^ ~b28 & b20; 2087 s[27] = b27 ^ ~b29 & b21; 2088 s[36] = b36 ^ ~b38 & b30; 2089 s[37] = b37 ^ ~b39 & b31; 2090 s[46] = b46 ^ ~b48 & b40; 2091 s[47] = b47 ^ ~b49 & b41; 2092 s[8] = b8 ^ ~b0 & b2; 2093 s[9] = b9 ^ ~b1 & b3; 2094 s[18] = b18 ^ ~b10 & b12; 2095 s[19] = b19 ^ ~b11 & b13; 2096 s[28] = b28 ^ ~b20 & b22; 2097 s[29] = b29 ^ ~b21 & b23; 2098 s[38] = b38 ^ ~b30 & b32; 2099 s[39] = b39 ^ ~b31 & b33; 2100 s[48] = b48 ^ ~b40 & b42; 2101 s[49] = b49 ^ ~b41 & b43; 2102 2103 s[0] ^= RC[n]; 2104 s[1] ^= RC[n + 1]; 2105 } 2106 }; 2107 2108 var keccak = function keccak(bits) { 2109 return function (str) { 2110 var msg; 2111 if (str.slice(0, 2) === "0x") { 2112 msg = []; 2113 for (var i = 2, l = str.length; i < l; i += 2) { 2114 msg.push(parseInt(str.slice(i, i + 2), 16)); 2115 } 2116 } else { 2117 msg = str; 2118 } 2119 return update(Keccak(bits, bits), msg); 2120 }; 2121 }; 2122 2123 module.exports = { 2124 keccak256: keccak(256), 2125 keccak512: keccak(512), 2126 keccak256s: keccak(256), 2127 keccak512s: keccak(512) 2128 }; 2129 },{}],20:[function(require,module,exports){ 2130 /* 2131 This file is part of vnt.js. 2132 2133 vnt.js is free software: you can redistribute it and/or modify 2134 it under the terms of the GNU Lesser General Public License as published by 2135 the Free Software Foundation, either version 3 of the License, or 2136 (at your option) any later version. 2137 2138 vnt.js is distributed in the hope that it will be useful, 2139 but WITHOUT ANY WARRANTY; without even the implied warranty of 2140 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2141 GNU Lesser General Public License for more details. 2142 2143 You should have received a copy of the GNU Lesser General Public License 2144 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 2145 */ 2146 /** 2147 * @file sha3.js 2148 * @author Marek Kotewicz <marek@ethdev.com> 2149 * @date 2015 2150 */ 2151 2152 var CryptoJS = require('crypto-js'); 2153 var sha3 = require('crypto-js/sha3'); 2154 2155 module.exports = function (value, options) { 2156 if (options && options.encoding === 'hex') { 2157 if (value.length > 2 && value.substr(0, 2) === '0x') { 2158 value = value.substr(2); 2159 } 2160 value = CryptoJS.enc.Hex.parse(value); 2161 } 2162 2163 return sha3(value, { 2164 outputLength: 256 2165 }).toString(); 2166 }; 2167 2168 2169 },{"crypto-js":59,"crypto-js/sha3":80}],21:[function(require,module,exports){ 2170 /* 2171 This file is part of vnt.js. 2172 2173 vnt.js is free software: you can redistribute it and/or modify 2174 it under the terms of the GNU Lesser General Public License as published by 2175 the Free Software Foundation, either version 3 of the License, or 2176 (at your option) any later version. 2177 2178 vnt.js is distributed in the hope that it will be useful, 2179 but WITHOUT ANY WARRANTY; without even the implied warranty of 2180 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2181 GNU Lesser General Public License for more details. 2182 2183 You should have received a copy of the GNU Lesser General Public License 2184 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 2185 */ 2186 /** 2187 * @file utils.js 2188 * @author Marek Kotewicz <marek@ethdev.com> 2189 * @date 2015 2190 */ 2191 2192 /** 2193 * Utils 2194 * 2195 * @module utils 2196 */ 2197 2198 /** 2199 * Utility functions 2200 * 2201 * @class [utils] utils 2202 * @constructor 2203 */ 2204 2205 2206 var BigNumber = require('bignumber.js'); 2207 var sha3 = require('./sha3.js'); 2208 var utf8 = require('utf8'); 2209 2210 var unitMap = { 2211 'novnt': '0', 2212 'wei': '1', 2213 'kwei': '1000', 2214 'Kwei': '1000', 2215 'mwei': '1000000', 2216 'Mwei': '1000000', 2217 'gwei': '1000000000', 2218 'Gwei': '1000000000', 2219 'microvnt': '1000000000000', 2220 'micro': '1000000000000', 2221 'millivnt': '1000000000000000', 2222 'milli': '1000000000000000', 2223 'vnt': '1000000000000000000' 2224 }; 2225 2226 /** 2227 * Should be called to pad string to expected length 2228 * 2229 * @method padLeft 2230 * @param {String} string to be padded 2231 * @param {Number} characters that result string should have 2232 * @param {String} sign, by default 0 2233 * @returns {String} right aligned string 2234 */ 2235 var padLeft = function (string, chars, sign) { 2236 return new Array(chars - string.length + 1).join(sign ? sign : "0") + string; 2237 }; 2238 2239 /** 2240 * Should be called to pad string to expected length 2241 * 2242 * @method padRight 2243 * @param {String} string to be padded 2244 * @param {Number} characters that result string should have 2245 * @param {String} sign, by default 0 2246 * @returns {String} right aligned string 2247 */ 2248 var padRight = function (string, chars, sign) { 2249 return string + (new Array(chars - string.length + 1).join(sign ? sign : "0")); 2250 }; 2251 2252 /** 2253 * Should be called to get utf8 from it's hex representation 2254 * 2255 * @method toUtf8 2256 * @param {String} string in hex 2257 * @returns {String} ascii string representation of hex value 2258 */ 2259 var toUtf8 = function(hex) { 2260 // Find termination 2261 var str = ""; 2262 var i = 0, l = hex.length; 2263 if (hex.substring(0, 2) === '0x') { 2264 i = 2; 2265 } 2266 for (; i < l; i+=2) { 2267 var code = parseInt(hex.substr(i, 2), 16); 2268 if (code === 0) 2269 continue; 2270 str += String.fromCharCode(code); 2271 } 2272 2273 return utf8.decode(str); 2274 }; 2275 2276 /** 2277 * Should be called to get ascii from it's hex representation 2278 * 2279 * @method toAscii 2280 * @param {String} string in hex 2281 * @returns {String} ascii string representation of hex value 2282 */ 2283 var toAscii = function(hex) { 2284 // Find termination 2285 var str = ""; 2286 var i = 0, l = hex.length; 2287 if (hex.substring(0, 2) === '0x') { 2288 i = 2; 2289 } 2290 for (; i < l; i+=2) { 2291 var code = parseInt(hex.substr(i, 2), 16); 2292 str += String.fromCharCode(code); 2293 } 2294 2295 return str; 2296 }; 2297 2298 /** 2299 * Should be called to get hex representation (prefixed by 0x) of utf8 string 2300 * 2301 * @method fromUtf8 2302 * @param {String} string 2303 * @param {Boolean} allowZero to convert code point zero to 00 instead of end of string 2304 * @returns {String} hex representation of input string 2305 */ 2306 var fromUtf8 = function(str, allowZero) { 2307 str = utf8.encode(str); 2308 var hex = ""; 2309 for(var i = 0; i < str.length; i++) { 2310 var code = str.charCodeAt(i); 2311 if (code === 0) { 2312 if (allowZero) { 2313 hex += '00'; 2314 } else { 2315 break; 2316 } 2317 } else { 2318 var n = code.toString(16); 2319 hex += n.length < 2 ? '0' + n : n; 2320 } 2321 } 2322 2323 return "0x" + hex; 2324 }; 2325 2326 /** 2327 * Should be called to get hex representation (prefixed by 0x) of ascii string 2328 * 2329 * @method fromAscii 2330 * @param {String} string 2331 * @param {Number} optional padding 2332 * @returns {String} hex representation of input string 2333 */ 2334 var fromAscii = function(str) { 2335 var hex = ""; 2336 for(var i = 0; i < str.length; i++) { 2337 var code = str.charCodeAt(i); 2338 var n = code.toString(16); 2339 hex += n.length < 2 ? '0' + n : n; 2340 } 2341 2342 return "0x" + hex; 2343 }; 2344 2345 /** 2346 * Should be used to create full function/event name from json abi 2347 * 2348 * @method transformToFullName 2349 * @param {Object} json-abi 2350 * @return {String} full fnction/event name 2351 */ 2352 var transformToFullName = function (json) { 2353 if (json.name.indexOf('(') !== -1) { 2354 return json.name; 2355 } 2356 2357 var typeName = json.inputs.map(function(i){return i.type; }).join(); 2358 return json.name + '(' + typeName + ')'; 2359 }; 2360 2361 /** 2362 * Should be called to get display name of contract function 2363 * 2364 * @method extractDisplayName 2365 * @param {String} name of function/event 2366 * @returns {String} display name for function/event eg. multiply(uint256) -> multiply 2367 */ 2368 var extractDisplayName = function (name) { 2369 var stBracket = name.indexOf('('); 2370 var endBracket = name.indexOf(')'); 2371 return (stBracket !== -1 && endBracket !== -1) ? name.substr(0, stBracket) : name; 2372 }; 2373 2374 /** 2375 * Should be called to get type name of contract function 2376 * 2377 * @method extractTypeName 2378 * @param {String} name of function/event 2379 * @returns {String} type name for function/event eg. multiply(uint256) -> uint256 2380 */ 2381 var extractTypeName = function (name) { 2382 var stBracket = name.indexOf('('); 2383 var endBracket = name.indexOf(')'); 2384 return (stBracket !== -1 && endBracket !== -1) ? name.substr(stBracket + 1, endBracket - stBracket - 1).replace(' ', '') : ""; 2385 }; 2386 2387 /** 2388 * Converts value to it's decimal representation in string 2389 * 2390 * @method toDecimal 2391 * @param {String|Number|BigNumber} 2392 * @return {String} 2393 */ 2394 var toDecimal = function (value) { 2395 return toBigNumber(value).toNumber(); 2396 }; 2397 2398 /** 2399 * Converts value to it's hex representation 2400 * 2401 * @method fromDecimal 2402 * @param {String|Number|BigNumber} 2403 * @return {String} 2404 */ 2405 var fromDecimal = function (value) { 2406 var number = toBigNumber(value); 2407 var result = number.toString(16); 2408 2409 return number.lessThan(0) ? '-0x' + result.substr(1) : '0x' + result; 2410 }; 2411 2412 /** 2413 * Auto converts any given value into it's hex representation. 2414 * 2415 * And even stringifys objects before. 2416 * 2417 * @method toHex 2418 * @param {String|Number|BigNumber|Object} 2419 * @return {String} 2420 */ 2421 var toHex = function (val) { 2422 /*jshint maxcomplexity: 8 */ 2423 2424 if (isBoolean(val)) 2425 return fromDecimal(+val); 2426 2427 if (isBigNumber(val)) 2428 return fromDecimal(val); 2429 2430 if (typeof val === 'object') 2431 return fromUtf8(JSON.stringify(val)); 2432 2433 // if its a negative number, pass it through fromDecimal 2434 if (isString(val)) { 2435 if (val.indexOf('-0x') === 0) 2436 return fromDecimal(val); 2437 else if(val.indexOf('0x') === 0) 2438 return val; 2439 else if (!isFinite(val)) 2440 return fromUtf8(val,1); 2441 } 2442 2443 return fromDecimal(val); 2444 }; 2445 2446 /** 2447 * Returns value of unit in Wei 2448 * 2449 * @method getValueOfUnit 2450 * @param {String} unit the unit to convert to, default vnt 2451 * @returns {BigNumber} value of the unit (in Wei) 2452 * @throws error if the unit is not correct:w 2453 */ 2454 var getValueOfUnit = function (unit) { 2455 unit = unit ? unit.toLowerCase() : 'vnt'; 2456 var unitValue = unitMap[unit]; 2457 if (unitValue === undefined) { 2458 throw new Error('This unit doesn\'t exists, please use the one of the following units' + JSON.stringify(unitMap, null, 2)); 2459 } 2460 return new BigNumber(unitValue, 10); 2461 }; 2462 2463 /** 2464 * Takes a number of wei and converts it to any other vnt unit. 2465 * 2466 * Possible units are: 2467 * SI Short Other 2468 * - kwei -- 2469 * - mwei -- 2470 * - gwei -- 2471 * - microvnt micro 2472 * - millivnt milli 2473 * - vnt -- 2474 * 2475 * @method fromWei 2476 * @param {Number|String} number can be a number, number string or a HEX of a decimal 2477 * @param {String} unit the unit to convert to, default vnt 2478 * @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number 2479 */ 2480 var fromWei = function(number, unit) { 2481 var returnValue = toBigNumber(number).dividedBy(getValueOfUnit(unit)); 2482 2483 return isBigNumber(number) ? returnValue : returnValue.toString(10); 2484 }; 2485 2486 /** 2487 * Takes a number of a unit and converts it to wei. 2488 * 2489 * Possible units are: 2490 * SI Short Other 2491 * - kwei -- 2492 * - mwei -- 2493 * - gwei -- 2494 * - microvnt micro 2495 * - millivnt milli 2496 * - vnt -- 2497 * 2498 * @method toWei 2499 * @param {Number|String|BigNumber} number can be a number, number string or a HEX of a decimal 2500 * @param {String} unit the unit to convert from, default vnt 2501 * @return {String|Object} When given a BigNumber object it returns one as well, otherwise a number 2502 */ 2503 var toWei = function(number, unit) { 2504 var returnValue = toBigNumber(number).times(getValueOfUnit(unit)); 2505 2506 return isBigNumber(number) ? returnValue : returnValue.toString(10); 2507 }; 2508 2509 /** 2510 * Takes an input and transforms it into an bignumber 2511 * 2512 * @method toBigNumber 2513 * @param {Number|String|BigNumber} a number, string, HEX string or BigNumber 2514 * @return {BigNumber} BigNumber 2515 */ 2516 var toBigNumber = function(number) { 2517 /*jshint maxcomplexity:5 */ 2518 number = number || 0; 2519 if (isBigNumber(number)) 2520 return number; 2521 2522 if (isString(number) && (number.indexOf('0x') === 0 || number.indexOf('-0x') === 0)) { 2523 return new BigNumber(number.replace('0x',''), 16); 2524 } 2525 2526 return new BigNumber(number.toString(10), 10); 2527 }; 2528 2529 /** 2530 * Takes and input transforms it into bignumber and if it is negative value, into two's complement 2531 * 2532 * @method toTwosComplement 2533 * @param {Number|String|BigNumber} 2534 * @return {BigNumber} 2535 */ 2536 var toTwosComplement = function (number) { 2537 var bigNumber = toBigNumber(number).round(); 2538 if (bigNumber.lessThan(0)) { 2539 return new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(bigNumber).plus(1); 2540 } 2541 return bigNumber; 2542 }; 2543 2544 /** 2545 * Checks if the given string is strictly an address 2546 * 2547 * @method isStrictAddress 2548 * @param {String} address the given HEX adress 2549 * @return {Boolean} 2550 */ 2551 var isStrictAddress = function (address) { 2552 return /^0x[0-9a-f]{40}$/i.test(address); 2553 }; 2554 2555 /** 2556 * Checks if the given string is an address 2557 * 2558 * @method isAddress 2559 * @param {String} address the given HEX adress 2560 * @return {Boolean} 2561 */ 2562 var isAddress = function (address) { 2563 if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) { 2564 // check if it has the basic requirements of an address 2565 return false; 2566 } else if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) { 2567 // If it's all small caps or all all caps, return true 2568 return true; 2569 } else { 2570 // Otherwise check each case 2571 return isChecksumAddress(address); 2572 } 2573 }; 2574 2575 /** 2576 * Checks if the given string is a checksummed address 2577 * 2578 * @method isChecksumAddress 2579 * @param {String} address the given HEX adress 2580 * @return {Boolean} 2581 */ 2582 var isChecksumAddress = function (address) { 2583 // Check each case 2584 address = address.replace('0x',''); 2585 var addressHash = sha3(address.toLowerCase()); 2586 2587 for (var i = 0; i < 40; i++ ) { 2588 // the nth letter should be uppercase if the nth digit of casemap is 1 2589 if ((parseInt(addressHash[i], 16) > 7 && address[i].toUpperCase() !== address[i]) || (parseInt(addressHash[i], 16) <= 7 && address[i].toLowerCase() !== address[i])) { 2590 return false; 2591 } 2592 } 2593 return true; 2594 }; 2595 2596 2597 2598 /** 2599 * Makes a checksum address 2600 * 2601 * @method toChecksumAddress 2602 * @param {String} address the given HEX adress 2603 * @return {String} 2604 */ 2605 var toChecksumAddress = function (address) { 2606 if (typeof address === 'undefined') return ''; 2607 2608 address = address.toLowerCase().replace('0x',''); 2609 var addressHash = sha3(address); 2610 var checksumAddress = '0x'; 2611 2612 for (var i = 0; i < address.length; i++ ) { 2613 // If ith character is 9 to f then make it uppercase 2614 if (parseInt(addressHash[i], 16) > 7) { 2615 checksumAddress += address[i].toUpperCase(); 2616 } else { 2617 checksumAddress += address[i]; 2618 } 2619 } 2620 return checksumAddress; 2621 }; 2622 2623 /** 2624 * Transforms given string to valid 20 bytes-length addres with 0x prefix 2625 * 2626 * @method toAddress 2627 * @param {String} address 2628 * @return {String} formatted address 2629 */ 2630 var toAddress = function (address) { 2631 if (isStrictAddress(address)) { 2632 return address; 2633 } 2634 2635 if (/^[0-9a-f]{40}$/.test(address)) { 2636 return '0x' + address; 2637 } 2638 2639 return '0x' + padLeft(toHex(address).substr(2), 40); 2640 }; 2641 2642 /** 2643 * Returns true if object is BigNumber, otherwise false 2644 * 2645 * @method isBigNumber 2646 * @param {Object} 2647 * @return {Boolean} 2648 */ 2649 var isBigNumber = function (object) { 2650 return object instanceof BigNumber || 2651 (object && object.constructor && object.constructor.name === 'BigNumber'); 2652 }; 2653 2654 /** 2655 * Returns true if object is string, otherwise false 2656 * 2657 * @method isString 2658 * @param {Object} 2659 * @return {Boolean} 2660 */ 2661 var isString = function (object) { 2662 return typeof object === 'string' || 2663 (object && object.constructor && object.constructor.name === 'String'); 2664 }; 2665 2666 /** 2667 * Returns true if object is function, otherwise false 2668 * 2669 * @method isFunction 2670 * @param {Object} 2671 * @return {Boolean} 2672 */ 2673 var isFunction = function (object) { 2674 return typeof object === 'function'; 2675 }; 2676 2677 /** 2678 * Returns true if object is Objet, otherwise false 2679 * 2680 * @method isObject 2681 * @param {Object} 2682 * @return {Boolean} 2683 */ 2684 var isObject = function (object) { 2685 return object !== null && !(Array.isArray(object)) && typeof object === 'object'; 2686 }; 2687 2688 /** 2689 * Returns true if object is boolean, otherwise false 2690 * 2691 * @method isBoolean 2692 * @param {Object} 2693 * @return {Boolean} 2694 */ 2695 var isBoolean = function (object) { 2696 return typeof object === 'boolean'; 2697 }; 2698 2699 /** 2700 * Returns true if object is array, otherwise false 2701 * 2702 * @method isArray 2703 * @param {Object} 2704 * @return {Boolean} 2705 */ 2706 var isArray = function (object) { 2707 return Array.isArray(object); 2708 }; 2709 2710 /** 2711 * Returns true if given string is valid json object 2712 * 2713 * @method isJson 2714 * @param {String} 2715 * @return {Boolean} 2716 */ 2717 var isJson = function (str) { 2718 try { 2719 return !!JSON.parse(str); 2720 } catch (e) { 2721 return false; 2722 } 2723 }; 2724 2725 /** 2726 * Returns true if given string is a valid VNTChain block header bloom. 2727 * 2728 * @method isBloom 2729 * @param {String} hex encoded bloom filter 2730 * @return {Boolean} 2731 */ 2732 var isBloom = function (bloom) { 2733 if (!/^(0x)?[0-9a-f]{512}$/i.test(bloom)) { 2734 return false; 2735 } else if (/^(0x)?[0-9a-f]{512}$/.test(bloom) || /^(0x)?[0-9A-F]{512}$/.test(bloom)) { 2736 return true; 2737 } 2738 return false; 2739 }; 2740 2741 /** 2742 * Returns true if given string is a valid log topic. 2743 * 2744 * @method isTopic 2745 * @param {String} hex encoded topic 2746 * @return {Boolean} 2747 */ 2748 var isTopic = function (topic) { 2749 if (!/^(0x)?[0-9a-f]{64}$/i.test(topic)) { 2750 return false; 2751 } else if (/^(0x)?[0-9a-f]{64}$/.test(topic) || /^(0x)?[0-9A-F]{64}$/.test(topic)) { 2752 return true; 2753 } 2754 return false; 2755 }; 2756 2757 module.exports = { 2758 padLeft: padLeft, 2759 padRight: padRight, 2760 toHex: toHex, 2761 toDecimal: toDecimal, 2762 fromDecimal: fromDecimal, 2763 toUtf8: toUtf8, 2764 toAscii: toAscii, 2765 fromUtf8: fromUtf8, 2766 fromAscii: fromAscii, 2767 transformToFullName: transformToFullName, 2768 extractDisplayName: extractDisplayName, 2769 extractTypeName: extractTypeName, 2770 toWei: toWei, 2771 fromWei: fromWei, 2772 toBigNumber: toBigNumber, 2773 toTwosComplement: toTwosComplement, 2774 toAddress: toAddress, 2775 isBigNumber: isBigNumber, 2776 isStrictAddress: isStrictAddress, 2777 isAddress: isAddress, 2778 isChecksumAddress: isChecksumAddress, 2779 toChecksumAddress: toChecksumAddress, 2780 isFunction: isFunction, 2781 isString: isString, 2782 isObject: isObject, 2783 isBoolean: isBoolean, 2784 isArray: isArray, 2785 isJson: isJson, 2786 isBloom: isBloom, 2787 isTopic: isTopic, 2788 }; 2789 2790 },{"./sha3.js":20,"bignumber.js":"bignumber.js","utf8":85}],22:[function(require,module,exports){ 2791 module.exports={ 2792 "version": "0.20.7" 2793 } 2794 2795 },{}],23:[function(require,module,exports){ 2796 /* 2797 This file is part of vnt.js. 2798 2799 vnt.js is free software: you can redistribute it and/or modify 2800 it under the terms of the GNU Lesser General Public License as published by 2801 the Free Software Foundation, either version 3 of the License, or 2802 (at your option) any later version. 2803 2804 vnt.js is distributed in the hope that it will be useful, 2805 but WITHOUT ANY WARRANTY; without even the implied warranty of 2806 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2807 GNU Lesser General Public License for more details. 2808 2809 You should have received a copy of the GNU Lesser General Public License 2810 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 2811 */ 2812 /** 2813 * @file vnt.js 2814 * @authors: 2815 * Jeffrey Wilcke <jeff@ethdev.com> 2816 * Marek Kotewicz <marek@ethdev.com> 2817 * Marian Oancea <marian@ethdev.com> 2818 * Fabian Vogelsteller <fabian@ethdev.com> 2819 * Gav Wood <g@ethdev.com> 2820 * @date 2014 2821 */ 2822 2823 var RequestManager = require('./vnt/requestmanager'); 2824 var Iban = require('./vnt/iban'); 2825 var Core = require('./vnt/methods/core'); 2826 var Shh = require('./vnt/methods/shh'); 2827 var Net = require('./vnt/methods/net'); 2828 var Personal = require('./vnt/methods/personal'); 2829 var Swarm = require('./vnt/methods/swarm'); 2830 var Settings = require('./vnt/settings'); 2831 var version = require('./version.json'); 2832 var utils = require('./utils/utils'); 2833 var sha3 = require('./utils/sha3'); 2834 var Hash = require('./utils/hash'); 2835 var extend = require('./vnt/extend'); 2836 var Batch = require('./vnt/batch'); 2837 var Property = require('./vnt/property'); 2838 var HttpProvider = require('./vnt/httpprovider'); 2839 var IpcProvider = require('./vnt/ipcprovider'); 2840 var BigNumber = require('bignumber.js'); 2841 2842 2843 2844 function Vnt (provider) { 2845 this._requestManager = new RequestManager(provider); 2846 this.currentProvider = provider; 2847 this.core = new Core(this); 2848 this.shh = new Shh(this); 2849 this.net = new Net(this); 2850 this.hash = Hash; 2851 this.utils = utils; 2852 this.personal = new Personal(this); 2853 this.bzz = new Swarm(this); 2854 this.settings = new Settings(); 2855 this.version = { 2856 api: version.version 2857 }; 2858 this.providers = { 2859 HttpProvider: HttpProvider, 2860 IpcProvider: IpcProvider 2861 }; 2862 this._extend = extend(this); 2863 this._extend({ 2864 properties: properties() 2865 }); 2866 } 2867 2868 // expose providers on the class 2869 Vnt.providers = { 2870 HttpProvider: HttpProvider, 2871 IpcProvider: IpcProvider 2872 }; 2873 2874 Vnt.prototype.setProvider = function (provider) { 2875 this._requestManager.setProvider(provider); 2876 this.currentProvider = provider; 2877 }; 2878 2879 Vnt.prototype.reset = function (keepIsSyncing) { 2880 this._requestManager.reset(keepIsSyncing); 2881 this.settings = new Settings(); 2882 }; 2883 2884 Vnt.prototype.BigNumber = BigNumber; 2885 Vnt.prototype.toHex = utils.toHex; 2886 Vnt.prototype.toAscii = utils.toAscii; 2887 Vnt.prototype.toUtf8 = utils.toUtf8; 2888 Vnt.prototype.fromAscii = utils.fromAscii; 2889 Vnt.prototype.fromUtf8 = utils.fromUtf8; 2890 Vnt.prototype.toDecimal = utils.toDecimal; 2891 Vnt.prototype.fromDecimal = utils.fromDecimal; 2892 Vnt.prototype.toBigNumber = utils.toBigNumber; 2893 Vnt.prototype.toWei = utils.toWei; 2894 Vnt.prototype.fromWei = utils.fromWei; 2895 Vnt.prototype.isAddress = utils.isAddress; 2896 Vnt.prototype.isChecksumAddress = utils.isChecksumAddress; 2897 Vnt.prototype.toChecksumAddress = utils.toChecksumAddress; 2898 Vnt.prototype.isIBAN = utils.isIBAN; 2899 Vnt.prototype.padLeft = utils.padLeft; 2900 Vnt.prototype.padRight = utils.padRight; 2901 2902 2903 Vnt.prototype.sha3 = function(string, options) { 2904 return '0x' + sha3(string, options); 2905 }; 2906 2907 /** 2908 * Transforms direct icap to address 2909 */ 2910 Vnt.prototype.fromICAP = function (icap) { 2911 var iban = new Iban(icap); 2912 return iban.address(); 2913 }; 2914 2915 var properties = function () { 2916 return [ 2917 new Property({ 2918 name: 'version.node', 2919 getter: 'vnt_clientVersion' 2920 }), 2921 new Property({ 2922 name: 'version.network', 2923 getter: 'net_version', 2924 inputFormatter: utils.toDecimal 2925 }), 2926 new Property({ 2927 name: 'version.vntchain', 2928 getter: 'core_protocolVersion', 2929 inputFormatter: utils.toDecimal 2930 }), 2931 new Property({ 2932 name: 'version.whisper', 2933 getter: 'shh_version', 2934 inputFormatter: utils.toDecimal 2935 }) 2936 ]; 2937 }; 2938 2939 Vnt.prototype.isConnected = function(){ 2940 return (this.currentProvider && this.currentProvider.isConnected()); 2941 }; 2942 2943 Vnt.prototype.createBatch = function () { 2944 return new Batch(this); 2945 }; 2946 2947 module.exports = Vnt; 2948 2949 },{"./utils/hash":19,"./utils/sha3":20,"./utils/utils":21,"./version.json":22,"./vnt/batch":25,"./vnt/extend":29,"./vnt/httpprovider":33,"./vnt/iban":34,"./vnt/ipcprovider":35,"./vnt/methods/core":38,"./vnt/methods/net":39,"./vnt/methods/personal":40,"./vnt/methods/shh":41,"./vnt/methods/swarm":42,"./vnt/property":45,"./vnt/requestmanager":46,"./vnt/settings":47,"bignumber.js":"bignumber.js"}],24:[function(require,module,exports){ 2950 /* 2951 This file is part of vnt.js. 2952 2953 vnt.js is free software: you can redistribute it and/or modify 2954 it under the terms of the GNU Lesser General Public License as published by 2955 the Free Software Foundation, either version 3 of the License, or 2956 (at your option) any later version. 2957 2958 vnt.js is distributed in the hope that it will be useful, 2959 but WITHOUT ANY WARRANTY; without even the implied warranty of 2960 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 2961 GNU Lesser General Public License for more details. 2962 2963 You should have received a copy of the GNU Lesser General Public License 2964 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 2965 */ 2966 /** 2967 * @file allevents.js 2968 * @author Marek Kotewicz <marek@ethdev.com> 2969 * @date 2014 2970 */ 2971 2972 var sha3 = require('../utils/sha3'); 2973 var Event = require('./event'); 2974 var formatters = require('./formatters'); 2975 var utils = require('../utils/utils'); 2976 var Filter = require('./filter'); 2977 var watches = require('./methods/watches'); 2978 2979 var AllEvents = function (requestManager, json, address) { 2980 this._requestManager = requestManager; 2981 this._json = json; 2982 this._address = address; 2983 }; 2984 2985 AllEvents.prototype.encode = function (options) { 2986 options = options || {}; 2987 var result = {}; 2988 2989 ['fromBlock', 'toBlock'].filter(function (f) { 2990 return options[f] !== undefined; 2991 }).forEach(function (f) { 2992 result[f] = formatters.inputBlockNumberFormatter(options[f]); 2993 }); 2994 2995 result.address = this._address; 2996 2997 return result; 2998 }; 2999 3000 AllEvents.prototype.decode = function (data) { 3001 data.data = data.data || ''; 3002 3003 3004 var eventTopic = (utils.isArray(data.topics) && utils.isString(data.topics[0])) ? data.topics[0].slice(2) : ''; 3005 var match = this._json.filter(function (j) { 3006 return eventTopic === sha3(utils.transformToFullName(j)); 3007 })[0]; 3008 3009 if (!match) { // cannot find matching event? 3010 return formatters.outputLogFormatter(data); 3011 } 3012 3013 var event = new Event(this._requestManager, match, this._address); 3014 return event.decode(data); 3015 }; 3016 3017 AllEvents.prototype.execute = function (options, callback) { 3018 3019 if (utils.isFunction(arguments[arguments.length - 1])) { 3020 callback = arguments[arguments.length - 1]; 3021 if(arguments.length === 1) 3022 options = null; 3023 } 3024 3025 var o = this.encode(options); 3026 var formatter = this.decode.bind(this); 3027 return new Filter(o, 'vnt', this._requestManager, watches.vnt(), formatter, callback); 3028 }; 3029 3030 AllEvents.prototype.attachToContract = function (contract) { 3031 var execute = this.execute.bind(this); 3032 contract.allEvents = execute; 3033 }; 3034 3035 module.exports = AllEvents; 3036 3037 },{"../utils/sha3":20,"../utils/utils":21,"./event":28,"./filter":30,"./formatters":31,"./methods/watches":43}],25:[function(require,module,exports){ 3038 /* 3039 This file is part of vnt.js. 3040 3041 vnt.js is free software: you can redistribute it and/or modify 3042 it under the terms of the GNU Lesser General Public License as published by 3043 the Free Software Foundation, either version 3 of the License, or 3044 (at your option) any later version. 3045 3046 vnt.js is distributed in the hope that it will be useful, 3047 but WITHOUT ANY WARRANTY; without even the implied warranty of 3048 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 3049 GNU Lesser General Public License for more details. 3050 3051 You should have received a copy of the GNU Lesser General Public License 3052 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 3053 */ 3054 /** 3055 * @file batch.js 3056 * @author Marek Kotewicz <marek@ethdev.com> 3057 * @date 2015 3058 */ 3059 3060 var Jsonrpc = require('./jsonrpc'); 3061 var errors = require('./errors'); 3062 3063 var Batch = function (vnt) { 3064 this.requestManager = vnt._requestManager; 3065 this.requests = []; 3066 }; 3067 3068 /** 3069 * Should be called to add create new request to batch request 3070 * 3071 * @method add 3072 * @param {Object} jsonrpc requet object 3073 */ 3074 Batch.prototype.add = function (request) { 3075 this.requests.push(request); 3076 }; 3077 3078 /** 3079 * Should be called to execute batch request 3080 * 3081 * @method execute 3082 */ 3083 Batch.prototype.execute = function () { 3084 var requests = this.requests; 3085 this.requestManager.sendBatch(requests, function (err, results) { 3086 results = results || []; 3087 requests.map(function (request, index) { 3088 return results[index] || {}; 3089 }).forEach(function (result, index) { 3090 if (requests[index].callback) { 3091 3092 if (!Jsonrpc.isValidResponse(result)) { 3093 return requests[index].callback(errors.InvalidResponse(result)); 3094 } 3095 3096 requests[index].callback(null, (requests[index].format ? requests[index].format(result.result) : result.result)); 3097 } 3098 }); 3099 }); 3100 }; 3101 3102 module.exports = Batch; 3103 3104 3105 },{"./errors":27,"./jsonrpc":36}],26:[function(require,module,exports){ 3106 /* 3107 This file is part of vnt.js. 3108 3109 vnt.js is free software: you can redistribute it and/or modify 3110 it under the terms of the GNU Lesser General Public License as published by 3111 the Free Software Foundation, either version 3 of the License, or 3112 (at your option) any later version. 3113 3114 vnt.js is distributed in the hope that it will be useful, 3115 but WITHOUT ANY WARRANTY; without even the implied warranty of 3116 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 3117 GNU Lesser General Public License for more details. 3118 3119 You should have received a copy of the GNU Lesser General Public License 3120 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 3121 */ 3122 /** 3123 * @file contract.js 3124 * @author Marek Kotewicz <marek@ethdev.com> 3125 * @date 2014 3126 */ 3127 3128 var fs = require('fs') 3129 var utils = require('../utils/utils'); 3130 var coder = require('../types/coder'); 3131 var Event = require('./event'); 3132 var Func = require('./function'); 3133 var AllEvents = require('./allevents'); 3134 var sha3 = require('../utils/sha3'); 3135 3136 /** 3137 * Should be called to encode constructor params 3138 * 3139 * @method encodeConstructorParams 3140 * @param {Array} abi 3141 * @param {Array} constructor params 3142 */ 3143 var encodeConstructorParams = function (abi, params) { 3144 return abi.filter(function (json) { 3145 return json.type === 'constructor' && json.inputs.length === params.length; 3146 }).map(function (json) { 3147 return json.inputs.map(function (input) { 3148 return input.type; 3149 }); 3150 }).map(function (types) { 3151 return coder.encodeParams(types, params); 3152 })[0] || ''; 3153 }; 3154 3155 /** 3156 * Should be called to add functions to contract object 3157 * 3158 * @method addFunctionsToContract 3159 * @param {Contract} contract 3160 * @param {Array} abi 3161 */ 3162 var addFunctionsToContract = function (contract) { 3163 contract.abi.filter(function (json) { 3164 return json.type === 'function'; 3165 }).map(function (json) { 3166 return new Func(contract._core, json, contract.address); 3167 }).forEach(function (f) { 3168 f.attachToContract(contract); 3169 }); 3170 }; 3171 3172 /** 3173 * Should be called to add events to contract object 3174 * 3175 * @method addEventsToContract 3176 * @param {Contract} contract 3177 * @param {Array} abi 3178 */ 3179 var addEventsToContract = function (contract) { 3180 var events = contract.abi.filter(function (json) { 3181 return json.type === 'event'; 3182 }); 3183 3184 var All = new AllEvents(contract._core._requestManager, events, contract.address); 3185 All.attachToContract(contract); 3186 3187 events.map(function (json) { 3188 return new Event(contract._core._requestManager, json, contract.address); 3189 }).forEach(function (e) { 3190 e.attachToContract(contract); 3191 }); 3192 }; 3193 3194 3195 /** 3196 * Should be called to check if the contract gets properly deployed on the blockchain. 3197 * 3198 * @method checkForContractAddress 3199 * @param {Object} contract 3200 * @param {Func} callback 3201 * @returns {Undefined} 3202 */ 3203 var checkForContractAddress = function(contract, callback){ 3204 var count = 0, 3205 callbackFired = false; 3206 3207 // wait for receipt 3208 var filter = contract._core.filter('latest', function(e){ 3209 if (!e && !callbackFired) { 3210 count++; 3211 3212 // stop watching after 50 blocks (timeout) 3213 if (count > 50) { 3214 3215 filter.stopWatching(function() {}); 3216 callbackFired = true; 3217 3218 if (callback) 3219 callback(new Error('Contract transaction couldn\'t be found after 50 blocks')); 3220 else 3221 throw new Error('Contract transaction couldn\'t be found after 50 blocks'); 3222 3223 3224 } else { 3225 3226 contract._core.getTransactionReceipt(contract.transactionHash, function(e, receipt){ 3227 if(receipt && receipt.blockHash && !callbackFired) { 3228 3229 contract._core.getCode(receipt.contractAddress, function(e, code){ 3230 /*jshint maxcomplexity: 6 */ 3231 3232 if(callbackFired || !code) 3233 return; 3234 3235 filter.stopWatching(function() {}); 3236 callbackFired = true; 3237 3238 if(code.length > 3) { 3239 3240 // console.log('Contract code deployed!'); 3241 3242 contract.address = receipt.contractAddress; 3243 3244 // attach events and methods again after we have 3245 addFunctionsToContract(contract); 3246 addEventsToContract(contract); 3247 3248 // call callback for the second time 3249 if(callback) 3250 callback(null, contract); 3251 3252 } else { 3253 if(callback) 3254 callback(new Error('The contract code couldn\'t be stored, please check your gas amount.')); 3255 else 3256 throw new Error('The contract code couldn\'t be stored, please check your gas amount.'); 3257 } 3258 }); 3259 } 3260 }); 3261 } 3262 } 3263 }); 3264 }; 3265 3266 /** 3267 * Should be called to create new ContractFactory instance 3268 * 3269 * @method ContractFactory 3270 * @param {Array} abi 3271 */ 3272 var ContractFactory = function (core, abi) { 3273 this.core = core; 3274 this.abi = abi; 3275 this.code = null; 3276 3277 /** 3278 * Should be called to create new contract on a blockchain 3279 * 3280 * @method new 3281 * @param {Any} contract constructor param1 (optional) 3282 * @param {Any} contract constructor param2 (optional) 3283 * @param {Object} contract transaction object (required) 3284 * @param {Func} callback 3285 * @returns {Contract} returns contract instance 3286 */ 3287 this.new = function () { 3288 /*jshint maxcomplexity: 7 */ 3289 3290 var contract = new Contract(this.core, this.abi); 3291 3292 // parse arguments 3293 var options = {}; // required! 3294 var callback; 3295 3296 var args = Array.prototype.slice.call(arguments); 3297 if (utils.isFunction(args[args.length - 1])) { 3298 callback = args.pop(); 3299 } 3300 3301 var last = args[args.length - 1]; 3302 if (utils.isObject(last) && !utils.isArray(last)) { 3303 options = args.pop(); 3304 } 3305 3306 if (options.value > 0) { 3307 var constructorAbi = abi.filter(function (json) { 3308 return json.type === 'constructor' && json.inputs.length === args.length; 3309 })[0] || {}; 3310 if (constructorAbi.name.substring(0, 1) != '$') { 3311 throw new Error('Cannot send value to non-payable constructor'); 3312 } 3313 } 3314 3315 var bytes = encodeConstructorParams(this.abi, args); 3316 options.data += bytes; 3317 3318 if (callback) { 3319 3320 // wait for the contract address and check if the code was deployed 3321 this.core.sendTransaction(options, function (err, hash) { 3322 if (err) { 3323 callback(err); 3324 } else { 3325 // add the transaction hash 3326 contract.transactionHash = hash; 3327 3328 // call callback for the first time 3329 callback(null, contract); 3330 3331 checkForContractAddress(contract, callback); 3332 } 3333 }); 3334 } else { 3335 var hash = this.core.sendTransaction(options); 3336 // add the transaction hash 3337 contract.transactionHash = hash; 3338 checkForContractAddress(contract); 3339 } 3340 3341 return contract; 3342 }; 3343 3344 this.new.getData = this.getData.bind(this); 3345 }; 3346 3347 /** 3348 * Should be called to create new ContractFactory 3349 * 3350 * @method contract 3351 * @param {Array} abi 3352 * @returns {ContractFactory} new contract factory 3353 */ 3354 //var contract = function (abi) { 3355 //return new ContractFactory(abi); 3356 //}; 3357 3358 3359 3360 /** 3361 * Should be called to get access to existing contract on a blockchain 3362 * 3363 * @method at 3364 * @param {Address} contract address (required) 3365 * @param {Func} callback {optional) 3366 * @returns {Contract} returns contract if no callback was passed, 3367 * otherwise calls callback function (err, contract) 3368 */ 3369 ContractFactory.prototype.at = function (address, callback) { 3370 var contract = new Contract(this.core, this.abi, address); 3371 3372 // this functions are not part of prototype, 3373 // because we dont want to spoil the interface 3374 addFunctionsToContract(contract); 3375 addEventsToContract(contract); 3376 3377 if (callback) { 3378 callback(null, contract); 3379 } 3380 return contract; 3381 }; 3382 3383 /** 3384 * Use this method to specify the code file path 3385 * Should be called when trying to deploy a new contract 3386 * 3387 * @method at 3388 * @param {String} codeFile (required) 3389 * @returns {ContractFactory} returns ContractFactory itself 3390 */ 3391 ContractFactory.prototype.codeFile = function(codeFile) { 3392 var code = "0x" + fs.readFileSync(codeFile).toString('hex'); 3393 this.code = code; 3394 return this; 3395 } 3396 3397 /** 3398 * Use this method to pack the code with constructor params 3399 * Should be called when trying to deploy a new contract 3400 * 3401 * @method at 3402 * @param {Array} the param list (required) 3403 * @returns {HexString} returns the packed data 3404 */ 3405 ContractFactory.prototype.packContructorData = function() { 3406 if(!this.code) { 3407 throw new Error('There is no code file specified. Please specify it with codeFile method.'); 3408 } 3409 3410 var args = Array.prototype.slice.call(arguments); 3411 var bytes = encodeConstructorParams(this.abi, args); 3412 return this.code + bytes 3413 } 3414 3415 /** 3416 * Should be called to encode function params 3417 * 3418 * @method packFunctionData 3419 * @param {Array} function name 3420 * @param {Array} function params list 3421 * @return {HexString} returns the packed data 3422 */ 3423 ContractFactory.prototype.packFunctionData = function () { 3424 var args = Array.prototype.slice.call(arguments); 3425 if(args.length < 1) { 3426 throw new Error('Please specify the function name and parameters.'); 3427 } 3428 var funcName = args[0]; 3429 var params = [] 3430 if(args.length > 1) { 3431 params = args[1] 3432 } 3433 3434 if(!utils.isString(funcName)) { 3435 throw new Error('Invalide function name.'); 3436 } 3437 3438 if(!utils.isArray(params)) { 3439 throw new Error('Invalide arguments array.'); 3440 } 3441 3442 var abiJson = this.abi.filter(function (json) { 3443 return json.type === 'function' && json.name==funcName && json.inputs.length === params.length; 3444 })[0] 3445 3446 if(!abiJson) { 3447 throw new Error('Invalide function name or arguments.'); 3448 } 3449 3450 var types = abiJson.inputs.map(function (input) { 3451 return input.type; 3452 }) 3453 3454 var sigName = funcName + "(" + types.toString() + ")" 3455 3456 var sig = sha3(sigName).slice(0, 8); 3457 return '0x' + sig + coder.encodeParams(types, params); 3458 }; 3459 3460 ContractFactory.prototype.unPackOutput = function() { 3461 var args = Array.prototype.slice.call(arguments); 3462 if(args.length < 2) { 3463 throw new Error('Please specify the function name and parameters.'); 3464 } 3465 var funcName = args[0]; 3466 var output = args[1] 3467 3468 if (!output) { 3469 return; 3470 } 3471 3472 if(!utils.isString(funcName)) { 3473 throw new Error('Invalide function name.'); 3474 } 3475 3476 if(!utils.isString(output)) { 3477 throw new Error('Invalide output hex string.'); 3478 } 3479 3480 var abiJson = this.abi.filter(function (json) { 3481 return json.type === 'function' && json.name==funcName; 3482 })[0] 3483 3484 if(!abiJson) { 3485 throw new Error('Invalide function name.'); 3486 } 3487 3488 var types = abiJson.outputs.map(function (output) { 3489 return output.type; 3490 }) 3491 3492 output = output.length >= 2 ? output.slice(2) : output; 3493 var result = coder.decodeParams(types, output); 3494 return result.length === 1 ? result[0] : result; 3495 } 3496 3497 /** 3498 * Gets the data, which is data to deploy plus constructor params 3499 * 3500 * @method getData 3501 */ 3502 ContractFactory.prototype.getData = function () { 3503 var options = {}; // required! 3504 var args = Array.prototype.slice.call(arguments); 3505 3506 var last = args[args.length - 1]; 3507 if (utils.isObject(last) && !utils.isArray(last)) { 3508 options = args.pop(); 3509 } 3510 3511 var bytes = encodeConstructorParams(this.abi, args); 3512 options.data += bytes; 3513 3514 return options.data; 3515 }; 3516 3517 /** 3518 * Should be called to create new contract instance 3519 * 3520 * @method Contract 3521 * @param {Array} abi 3522 * @param {Address} contract address 3523 */ 3524 var Contract = function (core, abi, address) { 3525 this._core = core; 3526 this.transactionHash = null; 3527 this.address = address; 3528 this.abi = abi; 3529 }; 3530 3531 module.exports = ContractFactory; 3532 3533 },{"../types/coder":7,"../utils/sha3":20,"../utils/utils":21,"./allevents":24,"./event":28,"./function":32,"fs":50}],27:[function(require,module,exports){ 3534 /* 3535 This file is part of vnt.js. 3536 3537 vnt.js is free software: you can redistribute it and/or modify 3538 it under the terms of the GNU Lesser General Public License as published by 3539 the Free Software Foundation, either version 3 of the License, or 3540 (at your option) any later version. 3541 3542 vnt.js is distributed in the hope that it will be useful, 3543 but WITHOUT ANY WARRANTY; without even the implied warranty of 3544 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 3545 GNU Lesser General Public License for more details. 3546 3547 You should have received a copy of the GNU Lesser General Public License 3548 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 3549 */ 3550 /** 3551 * @file errors.js 3552 * @author Marek Kotewicz <marek@ethdev.com> 3553 * @date 2015 3554 */ 3555 3556 module.exports = { 3557 InvalidNumberOfArgs: function () { 3558 return new Error('Invalid number of arguments to function'); 3559 }, 3560 InvalidNumberOfRPCParams: function () { 3561 return new Error('Invalid number of input parameters to RPC method'); 3562 }, 3563 InvalidConnection: function (host){ 3564 return new Error('CONNECTION ERROR: Couldn\'t connect to node '+ host +'.'); 3565 }, 3566 InvalidProvider: function () { 3567 return new Error('Provider not set or invalid'); 3568 }, 3569 InvalidResponse: function (result){ 3570 var message = !!result && !!result.error && !!result.error.message ? result.error.message : 'Invalid JSON RPC response: ' + JSON.stringify(result); 3571 return new Error(message); 3572 }, 3573 ConnectionTimeout: function (ms){ 3574 return new Error('CONNECTION TIMEOUT: timeout of ' + ms + ' ms achived'); 3575 } 3576 }; 3577 3578 },{}],28:[function(require,module,exports){ 3579 /* 3580 This file is part of vnt.js. 3581 3582 vnt.js is free software: you can redistribute it and/or modify 3583 it under the terms of the GNU Lesser General Public License as published by 3584 the Free Software Foundation, either version 3 of the License, or 3585 (at your option) any later version. 3586 3587 vnt.js is distributed in the hope that it will be useful, 3588 but WITHOUT ANY WARRANTY; without even the implied warranty of 3589 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 3590 GNU Lesser General Public License for more details. 3591 3592 You should have received a copy of the GNU Lesser General Public License 3593 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 3594 */ 3595 /** 3596 * @file event.js 3597 * @author Marek Kotewicz <marek@ethdev.com> 3598 * @date 2014 3599 */ 3600 3601 var utils = require('../utils/utils'); 3602 var coder = require('../types/coder'); 3603 var formatters = require('./formatters'); 3604 var sha3 = require('../utils/sha3'); 3605 var Filter = require('./filter'); 3606 var watches = require('./methods/watches'); 3607 3608 /** 3609 * This prototype should be used to create event filters 3610 */ 3611 var Event = function (requestManager, json, address) { 3612 this._requestManager = requestManager; 3613 this._params = json.inputs; 3614 this._name = utils.transformToFullName(json); 3615 this._address = address; 3616 this._anonymous = json.anonymous; 3617 }; 3618 3619 /** 3620 * Should be used to get filtered param types 3621 * 3622 * @method types 3623 * @param {Bool} decide if returned typed should be indexed 3624 * @return {Array} array of types 3625 */ 3626 Event.prototype.types = function (indexed) { 3627 return this._params.filter(function (i) { 3628 return i.indexed === indexed; 3629 }).map(function (i) { 3630 return i.type; 3631 }); 3632 }; 3633 3634 /** 3635 * Should be used to get event display name 3636 * 3637 * @method displayName 3638 * @return {String} event display name 3639 */ 3640 Event.prototype.displayName = function () { 3641 return utils.extractDisplayName(this._name); 3642 }; 3643 3644 /** 3645 * Should be used to get event type name 3646 * 3647 * @method typeName 3648 * @return {String} event type name 3649 */ 3650 Event.prototype.typeName = function () { 3651 return utils.extractTypeName(this._name); 3652 }; 3653 3654 /** 3655 * Should be used to get event signature 3656 * 3657 * @method signature 3658 * @return {String} event signature 3659 */ 3660 Event.prototype.signature = function () { 3661 return sha3(this._name); 3662 }; 3663 3664 /** 3665 * Should be used to encode indexed params and options to one final object 3666 * 3667 * @method encode 3668 * @param {Object} indexed 3669 * @param {Object} options 3670 * @return {Object} everything combined together and encoded 3671 */ 3672 Event.prototype.encode = function (indexed, options) { 3673 indexed = indexed || {}; 3674 options = options || {}; 3675 var result = {}; 3676 3677 ['fromBlock', 'toBlock'].filter(function (f) { 3678 return options[f] !== undefined; 3679 }).forEach(function (f) { 3680 result[f] = formatters.inputBlockNumberFormatter(options[f]); 3681 }); 3682 3683 result.topics = []; 3684 3685 result.address = this._address; 3686 if (!this._anonymous) { 3687 result.topics.push('0x' + this.signature()); 3688 } 3689 3690 var indexedTopics = this._params.filter(function (i) { 3691 return i.indexed === true; 3692 }).map(function (i) { 3693 var value = indexed[i.name]; 3694 if (value === undefined || value === null) { 3695 return null; 3696 } 3697 3698 if (utils.isArray(value)) { 3699 return value.map(function (v) { 3700 return '0x' + coder.encodeParam(i.type, v, true); 3701 }); 3702 } 3703 return '0x' + coder.encodeParam(i.type, value, true); 3704 }); 3705 3706 result.topics = result.topics.concat(indexedTopics); 3707 3708 return result; 3709 }; 3710 3711 /** 3712 * Should be used to decode indexed params and options 3713 * 3714 * @method decode 3715 * @param {Object} data 3716 * @return {Object} result object with decoded indexed && not indexed params 3717 */ 3718 Event.prototype.decode = function (data) { 3719 3720 data.data = data.data || ''; 3721 data.topics = data.topics || []; 3722 3723 3724 var argTopics = this._anonymous ? data.topics : data.topics.slice(1); 3725 var indexedData = argTopics.map(function (topics) { return topics.slice(2); }).join(""); 3726 var indexedParams = coder.decodeParams(this.types(true), indexedData, true); 3727 3728 var notIndexedData = data.data.slice(2); 3729 var notIndexedParams = coder.decodeParams(this.types(false), notIndexedData); 3730 3731 var result = formatters.outputLogFormatter(data); 3732 result.event = this.displayName(); 3733 result.address = data.address; 3734 3735 result.args = this._params.reduce(function (acc, current) { 3736 acc[current.name] = current.indexed ? indexedParams.shift() : notIndexedParams.shift(); 3737 return acc; 3738 }, {}); 3739 3740 delete result.data; 3741 delete result.topics; 3742 3743 return result; 3744 }; 3745 3746 /** 3747 * Should be used to create new filter object from event 3748 * 3749 * @method execute 3750 * @param {Object} indexed 3751 * @param {Object} options 3752 * @return {Object} filter object 3753 */ 3754 Event.prototype.execute = function (indexed, options, callback) { 3755 3756 if (utils.isFunction(arguments[arguments.length - 1])) { 3757 callback = arguments[arguments.length - 1]; 3758 if(arguments.length === 2){ 3759 options = arguments[0]; 3760 indxed = {} 3761 } else if(arguments.length === 1) { 3762 options = null; 3763 indexed = {}; 3764 } 3765 } else { 3766 if(arguments.length === 2){ 3767 options = arguments[1]; 3768 indxed = arguments[0]; 3769 } else if(arguments.length === 1) { 3770 options = arguments[0]; 3771 indexed = {}; 3772 } else { 3773 options = null; 3774 indexed = {}; 3775 } 3776 } 3777 3778 var o = this.encode(indexed, options); 3779 var formatter = this.decode.bind(this); 3780 return new Filter(o, 'vnt', this._requestManager, watches.vnt(), formatter, callback); 3781 }; 3782 3783 /** 3784 * Should be used to attach event to contract object 3785 * 3786 * @method attachToContract 3787 * @param {Contract} 3788 */ 3789 Event.prototype.attachToContract = function (contract) { 3790 var execute = this.execute.bind(this); 3791 var displayName = this.displayName(); 3792 if (!contract[displayName]) { 3793 contract[displayName] = execute; 3794 } 3795 contract[displayName][this.typeName()] = this.execute.bind(this, contract); 3796 }; 3797 3798 module.exports = Event; 3799 3800 },{"../types/coder":7,"../utils/sha3":20,"../utils/utils":21,"./filter":30,"./formatters":31,"./methods/watches":43}],29:[function(require,module,exports){ 3801 var formatters = require('./formatters'); 3802 var utils = require('./../utils/utils'); 3803 var Method = require('./method'); 3804 var Property = require('./property'); 3805 3806 // TODO: refactor, so the input params are not altered. 3807 // it's necessary to make same 'extension' work with multiple providers 3808 var extend = function (vnt) { 3809 /* jshint maxcomplexity:5 */ 3810 var ex = function (extension) { 3811 3812 var extendedObject; 3813 if (extension.property) { 3814 if (!vnt[extension.property]) { 3815 vnt[extension.property] = {}; 3816 } 3817 extendedObject = vnt[extension.property]; 3818 } else { 3819 extendedObject = vnt; 3820 } 3821 3822 if (extension.methods) { 3823 extension.methods.forEach(function (method) { 3824 method.attachToObject(extendedObject); 3825 method.setRequestManager(vnt._requestManager); 3826 }); 3827 } 3828 3829 if (extension.properties) { 3830 extension.properties.forEach(function (property) { 3831 property.attachToObject(extendedObject); 3832 property.setRequestManager(vnt._requestManager); 3833 }); 3834 } 3835 }; 3836 3837 ex.formatters = formatters; 3838 ex.utils = utils; 3839 ex.Method = Method; 3840 ex.Property = Property; 3841 3842 return ex; 3843 }; 3844 3845 3846 3847 module.exports = extend; 3848 3849 3850 },{"./../utils/utils":21,"./formatters":31,"./method":37,"./property":45}],30:[function(require,module,exports){ 3851 /* 3852 This file is part of vnt.js. 3853 3854 vnt.js is free software: you can redistribute it and/or modify 3855 it under the terms of the GNU Lesser General Public License as published by 3856 the Free Software Foundation, either version 3 of the License, or 3857 (at your option) any later version. 3858 3859 vnt.js is distributed in the hope that it will be useful, 3860 but WITHOUT ANY WARRANTY; without even the implied warranty of 3861 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 3862 GNU Lesser General Public License for more details. 3863 3864 You should have received a copy of the GNU Lesser General Public License 3865 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 3866 */ 3867 /** @file filter.js 3868 * @authors: 3869 * Jeffrey Wilcke <jeff@ethdev.com> 3870 * Marek Kotewicz <marek@ethdev.com> 3871 * Marian Oancea <marian@ethdev.com> 3872 * Fabian Vogelsteller <fabian@ethdev.com> 3873 * Gav Wood <g@ethdev.com> 3874 * @date 2014 3875 */ 3876 3877 var formatters = require('./formatters'); 3878 var utils = require('../utils/utils'); 3879 3880 /** 3881 * Converts a given topic to a hex string, but also allows null values. 3882 * 3883 * @param {Mixed} value 3884 * @return {String} 3885 */ 3886 var toTopic = function(value){ 3887 3888 if(value === null || typeof value === 'undefined') 3889 return null; 3890 3891 value = String(value); 3892 3893 if(value.indexOf('0x') === 0) 3894 return value; 3895 else 3896 return utils.fromUtf8(value); 3897 }; 3898 3899 /// This method should be called on options object, to verify deprecated properties && lazy load dynamic ones 3900 /// @param should be string or object 3901 /// @returns options string or object 3902 var getOptions = function (options, type) { 3903 /*jshint maxcomplexity: 6 */ 3904 3905 if (utils.isString(options)) { 3906 return options; 3907 } 3908 3909 options = options || {}; 3910 3911 3912 switch(type) { 3913 case 'vnt': 3914 3915 // make sure topics, get converted to hex 3916 options.topics = options.topics || []; 3917 options.topics = options.topics.map(function(topic){ 3918 return (utils.isArray(topic)) ? topic.map(toTopic) : toTopic(topic); 3919 }); 3920 3921 return { 3922 topics: options.topics, 3923 from: options.from, 3924 to: options.to, 3925 address: options.address, 3926 fromBlock: formatters.inputBlockNumberFormatter(options.fromBlock), 3927 toBlock: formatters.inputBlockNumberFormatter(options.toBlock) 3928 }; 3929 case 'shh': 3930 return options; 3931 } 3932 }; 3933 3934 /** 3935 Adds the callback and sets up the methods, to iterate over the results. 3936 3937 @method getLogsAtStart 3938 @param {Object} self 3939 @param {function} callback 3940 */ 3941 var getLogsAtStart = function(self, callback){ 3942 // call getFilterLogs for the first watch callback start 3943 if (!utils.isString(self.options)) { 3944 self.get(function (err, messages) { 3945 // don't send all the responses to all the watches again... just to self one 3946 if (err) { 3947 callback(err); 3948 } 3949 3950 if(utils.isArray(messages)) { 3951 messages.forEach(function (message) { 3952 callback(null, message); 3953 }); 3954 } 3955 }); 3956 } 3957 }; 3958 3959 /** 3960 Adds the callback and sets up the methods, to iterate over the results. 3961 3962 @method pollFilter 3963 @param {Object} self 3964 */ 3965 var pollFilter = function(self) { 3966 3967 var onMessage = function (error, messages) { 3968 if (error) { 3969 return self.callbacks.forEach(function (callback) { 3970 callback(error); 3971 }); 3972 } 3973 3974 if(utils.isArray(messages)) { 3975 messages.forEach(function (message) { 3976 message = self.formatter ? self.formatter(message) : message; 3977 self.callbacks.forEach(function (callback) { 3978 callback(null, message); 3979 }); 3980 }); 3981 } 3982 }; 3983 3984 self.requestManager.startPolling({ 3985 method: self.implementation.poll.call, 3986 params: [self.filterId], 3987 }, self.filterId, onMessage, self.stopWatching.bind(self)); 3988 3989 }; 3990 3991 var Filter = function (options, type, requestManager, methods, formatter, callback, filterCreationErrorCallback) { 3992 var self = this; 3993 var implementation = {}; 3994 methods.forEach(function (method) { 3995 method.setRequestManager(requestManager); 3996 method.attachToObject(implementation); 3997 }); 3998 this.requestManager = requestManager; 3999 this.options = getOptions(options, type); 4000 this.implementation = implementation; 4001 this.filterId = null; 4002 this.callbacks = []; 4003 this.getLogsCallbacks = []; 4004 this.pollFilters = []; 4005 this.formatter = formatter; 4006 this.implementation.newFilter(this.options, function(error, id){ 4007 if(error) { 4008 self.callbacks.forEach(function(cb){ 4009 cb(error); 4010 }); 4011 if (typeof filterCreationErrorCallback === 'function') { 4012 filterCreationErrorCallback(error); 4013 } 4014 } else { 4015 self.filterId = id; 4016 4017 // check if there are get pending callbacks as a consequence 4018 // of calling get() with filterId unassigned. 4019 self.getLogsCallbacks.forEach(function (cb){ 4020 self.get(cb); 4021 }); 4022 self.getLogsCallbacks = []; 4023 4024 // get filter logs for the already existing watch calls 4025 self.callbacks.forEach(function(cb){ 4026 getLogsAtStart(self, cb); 4027 }); 4028 if(self.callbacks.length > 0) 4029 pollFilter(self); 4030 4031 // start to watch immediately 4032 if(typeof callback === 'function') { 4033 return self.watch(callback); 4034 } 4035 } 4036 }); 4037 4038 return this; 4039 }; 4040 4041 Filter.prototype.watch = function (callback) { 4042 this.callbacks.push(callback); 4043 4044 if(this.filterId) { 4045 getLogsAtStart(this, callback); 4046 pollFilter(this); 4047 } 4048 4049 return this; 4050 }; 4051 4052 Filter.prototype.stopWatching = function (callback) { 4053 this.requestManager.stopPolling(this.filterId); 4054 this.callbacks = []; 4055 // remove filter async 4056 if (callback) { 4057 this.implementation.uninstallFilter(this.filterId, callback); 4058 } else { 4059 return this.implementation.uninstallFilter(this.filterId); 4060 } 4061 }; 4062 4063 Filter.prototype.get = function (callback) { 4064 var self = this; 4065 if (utils.isFunction(callback)) { 4066 if (this.filterId === null) { 4067 // If filterId is not set yet, call it back 4068 // when newFilter() assigns it. 4069 this.getLogsCallbacks.push(callback); 4070 } else { 4071 this.implementation.getLogs(this.filterId, function(err, res){ 4072 if (err) { 4073 callback(err); 4074 } else { 4075 callback(null, res.map(function (log) { 4076 return self.formatter ? self.formatter(log) : log; 4077 })); 4078 } 4079 }); 4080 } 4081 } else { 4082 if (this.filterId === null) { 4083 throw new Error('Filter ID Error: filter().get() can\'t be chained synchronous, please provide a callback for the get() method.'); 4084 } 4085 var logs = this.implementation.getLogs(this.filterId); 4086 return logs.map(function (log) { 4087 return self.formatter ? self.formatter(log) : log; 4088 }); 4089 } 4090 4091 return this; 4092 }; 4093 4094 module.exports = Filter; 4095 4096 },{"../utils/utils":21,"./formatters":31}],31:[function(require,module,exports){ 4097 /* 4098 This file is part of vnt.js. 4099 4100 vnt.js is free software: you can redistribute it and/or modify 4101 it under the terms of the GNU Lesser General Public License as published by 4102 the Free Software Foundation, either version 3 of the License, or 4103 (at your option) any later version. 4104 4105 vnt.js is distributed in the hope that it will be useful, 4106 but WITHOUT ANY WARRANTY; without even the implied warranty of 4107 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 4108 GNU Lesser General Public License for more details. 4109 4110 You should have received a copy of the GNU Lesser General Public License 4111 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 4112 */ 4113 /** 4114 * @file formatters.js 4115 * @author Marek Kotewicz <marek@ethdev.com> 4116 * @author Fabian Vogelsteller <fabian@ethdev.com> 4117 * @date 2015 4118 */ 4119 4120 'use strict'; 4121 4122 4123 var utils = require('../utils/utils'); 4124 var config = require('../utils/config'); 4125 var Iban = require('./iban'); 4126 4127 /** 4128 * Should the format output to a big number 4129 * 4130 * @method outputBigNumberFormatter 4131 * @param {String|Number|BigNumber} 4132 * @returns {BigNumber} object 4133 */ 4134 var outputBigNumberFormatter = function (number) { 4135 return utils.toBigNumber(number); 4136 }; 4137 4138 var isPredefinedBlockNumber = function (blockNumber) { 4139 return blockNumber === 'latest' || blockNumber === 'pending' || blockNumber === 'earliest'; 4140 }; 4141 4142 var inputDefaultBlockNumberFormatter = function (blockNumber) { 4143 if (blockNumber === undefined) { 4144 return config.defaultBlock; 4145 } 4146 return inputBlockNumberFormatter(blockNumber); 4147 }; 4148 4149 var inputBlockNumberFormatter = function (blockNumber) { 4150 if (blockNumber === undefined) { 4151 return undefined; 4152 } else if (isPredefinedBlockNumber(blockNumber)) { 4153 return blockNumber; 4154 } 4155 return utils.toHex(blockNumber); 4156 }; 4157 4158 /** 4159 * Formats the input of a transaction and converts all values to HEX 4160 * 4161 * @method inputCallFormatter 4162 * @param {Object} transaction options 4163 * @returns object 4164 */ 4165 var inputCallFormatter = function (options){ 4166 4167 options.from = options.from || config.defaultAccount; 4168 4169 if (options.from) { 4170 options.from = inputAddressFormatter(options.from); 4171 } 4172 4173 if (options.to) { // it might be contract creation 4174 options.to = inputAddressFormatter(options.to); 4175 } 4176 4177 ['gasPrice', 'gas', 'value', 'nonce'].filter(function (key) { 4178 return options[key] !== undefined; 4179 }).forEach(function(key){ 4180 options[key] = utils.fromDecimal(options[key]); 4181 }); 4182 4183 return options; 4184 }; 4185 4186 /** 4187 * Formats the input of a transaction and converts all values to HEX 4188 * 4189 * @method inputTransactionFormatter 4190 * @param {Object} transaction options 4191 * @returns object 4192 */ 4193 var inputTransactionFormatter = function (options){ 4194 4195 options.from = options.from || config.defaultAccount; 4196 options.from = inputAddressFormatter(options.from); 4197 4198 if (options.to) { // it might be contract creation 4199 options.to = inputAddressFormatter(options.to); 4200 } 4201 4202 ['gasPrice', 'gas', 'value', 'nonce'].filter(function (key) { 4203 return options[key] !== undefined; 4204 }).forEach(function(key){ 4205 options[key] = utils.fromDecimal(options[key]); 4206 }); 4207 4208 return options; 4209 }; 4210 4211 /** 4212 * Formats the output of a transaction to its proper values 4213 * 4214 * @method outputTransactionFormatter 4215 * @param {Object} tx 4216 * @returns {Object} 4217 */ 4218 var outputTransactionFormatter = function (tx){ 4219 if(tx.blockNumber !== null) 4220 tx.blockNumber = utils.toDecimal(tx.blockNumber); 4221 if(tx.transactionIndex !== null) 4222 tx.transactionIndex = utils.toDecimal(tx.transactionIndex); 4223 tx.nonce = utils.toDecimal(tx.nonce); 4224 tx.gas = utils.toDecimal(tx.gas); 4225 tx.gasPrice = utils.toBigNumber(tx.gasPrice); 4226 tx.value = utils.toBigNumber(tx.value); 4227 return tx; 4228 }; 4229 4230 /** 4231 * Formats the output of a transaction receipt to its proper values 4232 * 4233 * @method outputTransactionReceiptFormatter 4234 * @param {Object} receipt 4235 * @returns {Object} 4236 */ 4237 var outputTransactionReceiptFormatter = function (receipt){ 4238 if(receipt.blockNumber !== null) 4239 receipt.blockNumber = utils.toDecimal(receipt.blockNumber); 4240 if(receipt.transactionIndex !== null) 4241 receipt.transactionIndex = utils.toDecimal(receipt.transactionIndex); 4242 receipt.cumulativeGasUsed = utils.toDecimal(receipt.cumulativeGasUsed); 4243 receipt.gasUsed = utils.toDecimal(receipt.gasUsed); 4244 4245 if(utils.isArray(receipt.logs)) { 4246 receipt.logs = receipt.logs.map(function(log){ 4247 return outputLogFormatter(log); 4248 }); 4249 } 4250 4251 return receipt; 4252 }; 4253 4254 /** 4255 * Formats the output of a block to its proper values 4256 * 4257 * @method outputBlockFormatter 4258 * @param {Object} block 4259 * @returns {Object} 4260 */ 4261 var outputBlockFormatter = function(block) { 4262 4263 // transform to number 4264 block.gasLimit = utils.toDecimal(block.gasLimit); 4265 block.gasUsed = utils.toDecimal(block.gasUsed); 4266 block.size = utils.toDecimal(block.size); 4267 block.timestamp = utils.toDecimal(block.timestamp); 4268 if(block.number !== null) 4269 block.number = utils.toDecimal(block.number); 4270 4271 block.difficulty = utils.toBigNumber(block.difficulty); 4272 block.totalDifficulty = utils.toBigNumber(block.totalDifficulty); 4273 4274 if (utils.isArray(block.transactions)) { 4275 block.transactions.forEach(function(item){ 4276 if(!utils.isString(item)) 4277 return outputTransactionFormatter(item); 4278 }); 4279 } 4280 4281 return block; 4282 }; 4283 4284 /** 4285 * Formats the output of a log 4286 * 4287 * @method outputLogFormatter 4288 * @param {Object} log object 4289 * @returns {Object} log 4290 */ 4291 var outputLogFormatter = function(log) { 4292 if(log.blockNumber) 4293 log.blockNumber = utils.toDecimal(log.blockNumber); 4294 if(log.transactionIndex) 4295 log.transactionIndex = utils.toDecimal(log.transactionIndex); 4296 if(log.logIndex) 4297 log.logIndex = utils.toDecimal(log.logIndex); 4298 4299 return log; 4300 }; 4301 4302 /** 4303 * Formats the input of a whisper post and converts all values to HEX 4304 * 4305 * @method inputPostFormatter 4306 * @param {Object} transaction object 4307 * @returns {Object} 4308 */ 4309 var inputPostFormatter = function(post) { 4310 4311 // post.payload = utils.toHex(post.payload); 4312 post.ttl = utils.fromDecimal(post.ttl); 4313 post.workToProve = utils.fromDecimal(post.workToProve); 4314 post.priority = utils.fromDecimal(post.priority); 4315 4316 // fallback 4317 if (!utils.isArray(post.topics)) { 4318 post.topics = post.topics ? [post.topics] : []; 4319 } 4320 4321 // format the following options 4322 post.topics = post.topics.map(function(topic){ 4323 // convert only if not hex 4324 return (topic.indexOf('0x') === 0) ? topic : utils.fromUtf8(topic); 4325 }); 4326 4327 return post; 4328 }; 4329 4330 /** 4331 * Formats the output of a received post message 4332 * 4333 * @method outputPostFormatter 4334 * @param {Object} 4335 * @returns {Object} 4336 */ 4337 var outputPostFormatter = function(post){ 4338 4339 post.expiry = utils.toDecimal(post.expiry); 4340 post.sent = utils.toDecimal(post.sent); 4341 post.ttl = utils.toDecimal(post.ttl); 4342 post.workProved = utils.toDecimal(post.workProved); 4343 // post.payloadRaw = post.payload; 4344 // post.payload = utils.toAscii(post.payload); 4345 4346 // if (utils.isJson(post.payload)) { 4347 // post.payload = JSON.parse(post.payload); 4348 // } 4349 4350 // format the following options 4351 if (!post.topics) { 4352 post.topics = []; 4353 } 4354 post.topics = post.topics.map(function(topic){ 4355 return utils.toAscii(topic); 4356 }); 4357 4358 return post; 4359 }; 4360 4361 var inputAddressFormatter = function (address) { 4362 var iban = new Iban(address); 4363 if (iban.isValid() && iban.isDirect()) { 4364 return '0x' + iban.address(); 4365 } else if (utils.isStrictAddress(address)) { 4366 return address; 4367 } else if (utils.isAddress(address)) { 4368 return '0x' + address; 4369 } 4370 throw new Error('invalid address'); 4371 }; 4372 4373 4374 var outputSyncingFormatter = function(result) { 4375 if (!result) { 4376 return result; 4377 } 4378 4379 result.startingBlock = utils.toDecimal(result.startingBlock); 4380 result.currentBlock = utils.toDecimal(result.currentBlock); 4381 result.highestBlock = utils.toDecimal(result.highestBlock); 4382 if (result.knownStates) { 4383 result.knownStates = utils.toDecimal(result.knownStates); 4384 result.pulledStates = utils.toDecimal(result.pulledStates); 4385 } 4386 4387 return result; 4388 }; 4389 4390 module.exports = { 4391 inputDefaultBlockNumberFormatter: inputDefaultBlockNumberFormatter, 4392 inputBlockNumberFormatter: inputBlockNumberFormatter, 4393 inputCallFormatter: inputCallFormatter, 4394 inputTransactionFormatter: inputTransactionFormatter, 4395 inputAddressFormatter: inputAddressFormatter, 4396 inputPostFormatter: inputPostFormatter, 4397 outputBigNumberFormatter: outputBigNumberFormatter, 4398 outputTransactionFormatter: outputTransactionFormatter, 4399 outputTransactionReceiptFormatter: outputTransactionReceiptFormatter, 4400 outputBlockFormatter: outputBlockFormatter, 4401 outputLogFormatter: outputLogFormatter, 4402 outputPostFormatter: outputPostFormatter, 4403 outputSyncingFormatter: outputSyncingFormatter 4404 }; 4405 4406 4407 },{"../utils/config":18,"../utils/utils":21,"./iban":34}],32:[function(require,module,exports){ 4408 /* 4409 This file is part of vnt.js. 4410 4411 vnt.js is free software: you can redistribute it and/or modify 4412 it under the terms of the GNU Lesser General Public License as published by 4413 the Free Software Foundation, either version 3 of the License, or 4414 (at your option) any later version. 4415 4416 vnt.js is distributed in the hope that it will be useful, 4417 but WITHOUT ANY WARRANTY; without even the implied warranty of 4418 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 4419 GNU Lesser General Public License for more details. 4420 4421 You should have received a copy of the GNU Lesser General Public License 4422 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 4423 */ 4424 /** 4425 * @file function.js 4426 * @author Marek Kotewicz <marek@ethdev.com> 4427 * @date 2015 4428 */ 4429 4430 var coder = require('../types/coder'); 4431 var utils = require('../utils/utils'); 4432 var errors = require('./errors'); 4433 var formatters = require('./formatters'); 4434 var sha3 = require('../utils/sha3'); 4435 4436 /** 4437 * This prototype should be used to call/sendTransaction to functions 4438 */ 4439 var Func = function (vnt, json, address) { 4440 this._vnt = vnt; 4441 this._inputTypes = json.inputs.map(function (i) { 4442 return i.type; 4443 }); 4444 this._outputTypes = json.outputs.map(function (i) { 4445 return i.type; 4446 }); 4447 this._constant = json.constant; 4448 if (json.name.substring(0, 1) == '$') { 4449 this._payable = true; 4450 } 4451 this._name = utils.transformToFullName(json); 4452 this._address = address; 4453 }; 4454 4455 Func.prototype.extractCallback = function (args) { 4456 if (utils.isFunction(args[args.length - 1])) { 4457 return args.pop(); // modify the args array! 4458 } 4459 }; 4460 4461 Func.prototype.extractDefaultBlock = function (args) { 4462 if (args.length > this._inputTypes.length && !utils.isObject(args[args.length -1])) { 4463 return formatters.inputDefaultBlockNumberFormatter(args.pop()); // modify the args array! 4464 } 4465 }; 4466 4467 /** 4468 * Should be called to check if the number of arguments is correct 4469 * 4470 * @method validateArgs 4471 * @param {Array} arguments 4472 * @throws {Error} if it is not 4473 */ 4474 Func.prototype.validateArgs = function (args) { 4475 var inputArgs = args.filter(function (a) { 4476 // filter the options object but not arguments that are arrays 4477 return !( (utils.isObject(a) === true) && 4478 (utils.isArray(a) === false) && 4479 (utils.isBigNumber(a) === false) 4480 ); 4481 }); 4482 if (inputArgs.length !== this._inputTypes.length) { 4483 throw errors.InvalidNumberOfArgs(); 4484 } 4485 }; 4486 4487 /** 4488 * Should be used to create payload from arguments 4489 * 4490 * @method toPayload 4491 * @param {Array} function params 4492 * @param {Object} optional payload options 4493 */ 4494 Func.prototype.toPayload = function (args) { 4495 var options = {}; 4496 if (args.length > this._inputTypes.length && utils.isObject(args[args.length -1])) { 4497 options = args[args.length - 1]; 4498 } 4499 this.validateArgs(args); 4500 options.to = this._address; 4501 options.data = '0x' + this.signature() + coder.encodeParams(this._inputTypes, args); 4502 return options; 4503 }; 4504 4505 /** 4506 * Should be used to get function signature 4507 * 4508 * @method signature 4509 * @return {String} function signature 4510 */ 4511 Func.prototype.signature = function () { 4512 return sha3(this._name).slice(0, 8); 4513 }; 4514 4515 4516 Func.prototype.unpackOutput = function (output) { 4517 if (!output) { 4518 return; 4519 } 4520 4521 output = output.length >= 2 ? output.slice(2) : output; 4522 var result = coder.decodeParams(this._outputTypes, output); 4523 return result.length === 1 ? result[0] : result; 4524 }; 4525 4526 /** 4527 * Calls a contract function. 4528 * 4529 * @method call 4530 * @param {...Object} Contract function arguments 4531 * @param {function} If the last argument is a function, the contract function 4532 * call will be asynchronous, and the callback will be passed the 4533 * error and result. 4534 * @return {String} output bytes 4535 */ 4536 Func.prototype.call = function () { 4537 var args = Array.prototype.slice.call(arguments).filter(function (a) {return a !== undefined; }); 4538 var callback = this.extractCallback(args); 4539 var defaultBlock = this.extractDefaultBlock(args); 4540 var payload = this.toPayload(args); 4541 4542 4543 if (!callback) { 4544 var output = this._vnt.call(payload, defaultBlock); 4545 return this.unpackOutput(output); 4546 } 4547 4548 var self = this; 4549 this._vnt.call(payload, defaultBlock, function (error, output) { 4550 if (error) return callback(error, null); 4551 4552 var unpacked = null; 4553 try { 4554 unpacked = self.unpackOutput(output); 4555 } 4556 catch (e) { 4557 error = e; 4558 } 4559 4560 callback(error, unpacked); 4561 }); 4562 }; 4563 4564 /** 4565 * Should be used to sendTransaction to function 4566 * 4567 * @method sendTransaction 4568 */ 4569 Func.prototype.sendTransaction = function () { 4570 var args = Array.prototype.slice.call(arguments).filter(function (a) {return a !== undefined; }); 4571 var callback = this.extractCallback(args); 4572 var payload = this.toPayload(args); 4573 4574 if (payload.value > 0 && !this._payable) { 4575 throw new Error('Cannot send value to non-payable function'); 4576 } 4577 4578 if (!callback) { 4579 return this._vnt.sendTransaction(payload); 4580 } 4581 4582 this._vnt.sendTransaction(payload, callback); 4583 }; 4584 4585 /** 4586 * Should be used to estimateGas of function 4587 * 4588 * @method estimateGas 4589 */ 4590 Func.prototype.estimateGas = function () { 4591 var args = Array.prototype.slice.call(arguments); 4592 var callback = this.extractCallback(args); 4593 var payload = this.toPayload(args); 4594 4595 if (!callback) { 4596 return this._vnt.estimateGas(payload); 4597 } 4598 4599 this._vnt.estimateGas(payload, callback); 4600 }; 4601 4602 /** 4603 * Return the encoded data of the call 4604 * 4605 * @method getData 4606 * @return {String} the encoded data 4607 */ 4608 Func.prototype.getData = function () { 4609 var args = Array.prototype.slice.call(arguments); 4610 var payload = this.toPayload(args); 4611 4612 return payload.data; 4613 }; 4614 4615 /** 4616 * Should be used to get function display name 4617 * 4618 * @method displayName 4619 * @return {String} display name of the function 4620 */ 4621 Func.prototype.displayName = function () { 4622 return utils.extractDisplayName(this._name); 4623 }; 4624 4625 /** 4626 * Should be used to get function type name 4627 * 4628 * @method typeName 4629 * @return {String} type name of the function 4630 */ 4631 Func.prototype.typeName = function () { 4632 return utils.extractTypeName(this._name); 4633 }; 4634 4635 /** 4636 * Should be called to get rpc requests from function 4637 * 4638 * @method request 4639 * @returns {Object} 4640 */ 4641 Func.prototype.request = function () { 4642 var args = Array.prototype.slice.call(arguments); 4643 var callback = this.extractCallback(args); 4644 var payload = this.toPayload(args); 4645 var format = this.unpackOutput.bind(this); 4646 4647 return { 4648 method: this._constant ? 'core_call' : 'core_sendTransaction', 4649 callback: callback, 4650 params: [payload], 4651 format: format 4652 }; 4653 }; 4654 4655 /** 4656 * Should be called to execute function 4657 * 4658 * @method execute 4659 */ 4660 Func.prototype.execute = function () { 4661 var transaction = !this._constant; 4662 4663 // send transaction 4664 if (transaction) { 4665 return this.sendTransaction.apply(this, Array.prototype.slice.call(arguments)); 4666 } 4667 4668 // call 4669 return this.call.apply(this, Array.prototype.slice.call(arguments)); 4670 }; 4671 4672 /** 4673 * Should be called to attach function to contract 4674 * 4675 * @method attachToContract 4676 * @param {Contract} 4677 */ 4678 Func.prototype.attachToContract = function (contract) { 4679 var execute = this.execute.bind(this); 4680 execute.request = this.request.bind(this); 4681 execute.call = this.call.bind(this); 4682 execute.sendTransaction = this.sendTransaction.bind(this); 4683 execute.estimateGas = this.estimateGas.bind(this); 4684 execute.getData = this.getData.bind(this); 4685 var displayName = this.displayName(); 4686 if (!contract[displayName]) { 4687 contract[displayName] = execute; 4688 } 4689 contract[displayName][this.typeName()] = execute; // circular!!!! 4690 }; 4691 4692 module.exports = Func; 4693 4694 },{"../types/coder":7,"../utils/sha3":20,"../utils/utils":21,"./errors":27,"./formatters":31}],33:[function(require,module,exports){ 4695 /* 4696 This file is part of vnt.js. 4697 4698 vnt.js is free software: you can redistribute it and/or modify 4699 it under the terms of the GNU Lesser General Public License as published by 4700 the Free Software Foundation, either version 3 of the License, or 4701 (at your option) any later version. 4702 4703 vnt.js is distributed in the hope that it will be useful, 4704 but WITHOUT ANY WARRANTY; without even the implied warranty of 4705 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 4706 GNU Lesser General Public License for more details. 4707 4708 You should have received a copy of the GNU Lesser General Public License 4709 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 4710 */ 4711 /** @file httpprovider.js 4712 * @authors: 4713 * Marek Kotewicz <marek@ethdev.com> 4714 * Marian Oancea <marian@ethdev.com> 4715 * Fabian Vogelsteller <fabian@ethdev.com> 4716 * @date 2015 4717 */ 4718 4719 var errors = require('./errors'); 4720 4721 // workaround to use httpprovider in different envs 4722 4723 // browser 4724 if (typeof window !== 'undefined' && window.XMLHttpRequest) { 4725 XMLHttpRequest = window.XMLHttpRequest; // jshint ignore: line 4726 // node 4727 } else { 4728 XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore: line 4729 } 4730 4731 var XHR2 = require('xhr2'); // jshint ignore: line 4732 4733 /** 4734 * HttpProvider should be used to send rpc calls over http 4735 */ 4736 var HttpProvider = function (host, timeout, user, password, headers) { 4737 this.host = host || 'http://localhost:8545'; 4738 this.timeout = timeout || 0; 4739 this.user = user; 4740 this.password = password; 4741 this.headers = headers; 4742 }; 4743 4744 /** 4745 * Should be called to prepare new XMLHttpRequest 4746 * 4747 * @method prepareRequest 4748 * @param {Boolean} true if request should be async 4749 * @return {XMLHttpRequest} object 4750 */ 4751 HttpProvider.prototype.prepareRequest = function (async) { 4752 var request; 4753 4754 if (async) { 4755 request = new XHR2(); 4756 request.timeout = this.timeout; 4757 } else { 4758 request = new XMLHttpRequest(); 4759 } 4760 4761 request.open('POST', this.host, async); 4762 if (this.user && this.password) { 4763 var auth = 'Basic ' + new Buffer(this.user + ':' + this.password).toString('base64'); 4764 request.setRequestHeader('Authorization', auth); 4765 } request.setRequestHeader('Content-Type', 'application/json'); 4766 if(this.headers) { 4767 this.headers.forEach(function(header) { 4768 request.setRequestHeader(header.name, header.value); 4769 }); 4770 } 4771 return request; 4772 }; 4773 4774 /** 4775 * Should be called to make sync request 4776 * 4777 * @method send 4778 * @param {Object} payload 4779 * @return {Object} result 4780 */ 4781 HttpProvider.prototype.send = function (payload) { 4782 var request = this.prepareRequest(false); 4783 4784 try { 4785 request.send(JSON.stringify(payload)); 4786 } catch (error) { 4787 throw errors.InvalidConnection(this.host); 4788 } 4789 4790 var result = request.responseText; 4791 4792 try { 4793 result = JSON.parse(result); 4794 } catch (e) { 4795 throw errors.InvalidResponse(request.responseText); 4796 } 4797 4798 return result; 4799 }; 4800 4801 /** 4802 * Should be used to make async request 4803 * 4804 * @method sendAsync 4805 * @param {Object} payload 4806 * @param {Function} callback triggered on end with (err, result) 4807 */ 4808 HttpProvider.prototype.sendAsync = function (payload, callback) { 4809 var request = this.prepareRequest(true); 4810 4811 request.onreadystatechange = function () { 4812 if (request.readyState === 4 && request.timeout !== 1) { 4813 var result = request.responseText; 4814 var error = null; 4815 try { 4816 result = JSON.parse(result); 4817 } catch (e) { 4818 error = errors.InvalidResponse(request.responseText); 4819 } 4820 4821 callback(error, result); 4822 } 4823 }; 4824 4825 request.ontimeout = function () { 4826 callback(errors.ConnectionTimeout(this.timeout)); 4827 }; 4828 4829 try { 4830 request.send(JSON.stringify(payload)); 4831 } catch (error) { 4832 callback(errors.InvalidConnection(this.host)); 4833 } 4834 }; 4835 4836 /** 4837 * Synchronously tries to make Http request 4838 * 4839 * @method isConnected 4840 * @return {Boolean} returns true if request haven't failed. Otherwise false 4841 */ 4842 HttpProvider.prototype.isConnected = function () { 4843 try { 4844 this.send({ 4845 id: 9999999999, 4846 jsonrpc: '2.0', 4847 method: 'net_listening', 4848 params: [] 4849 }); 4850 return true; 4851 } catch (e) { 4852 return false; 4853 } 4854 }; 4855 4856 module.exports = HttpProvider; 4857 4858 },{"./errors":27,"xhr2":86,"xmlhttprequest":17}],34:[function(require,module,exports){ 4859 /* 4860 This file is part of vnt.js. 4861 4862 vnt.js is free software: you can redistribute it and/or modify 4863 it under the terms of the GNU Lesser General Public License as published by 4864 the Free Software Foundation, either version 3 of the License, or 4865 (at your option) any later version. 4866 4867 vnt.js is distributed in the hope that it will be useful, 4868 but WITHOUT ANY WARRANTY; without even the implied warranty of 4869 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 4870 GNU Lesser General Public License for more details. 4871 4872 You should have received a copy of the GNU Lesser General Public License 4873 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 4874 */ 4875 /** 4876 * @file iban.js 4877 * @author Marek Kotewicz <marek@ethdev.com> 4878 * @date 2015 4879 */ 4880 4881 var BigNumber = require('bignumber.js'); 4882 4883 var padLeft = function (string, bytes) { 4884 var result = string; 4885 while (result.length < bytes * 2) { 4886 result = '0' + result; 4887 } 4888 return result; 4889 }; 4890 4891 /** 4892 * Prepare an IBAN for mod 97 computation by moving the first 4 chars to the end and transforming the letters to 4893 * numbers (A = 10, B = 11, ..., Z = 35), as specified in ISO13616. 4894 * 4895 * @method iso13616Prepare 4896 * @param {String} iban the IBAN 4897 * @returns {String} the prepared IBAN 4898 */ 4899 var iso13616Prepare = function (iban) { 4900 var A = 'A'.charCodeAt(0); 4901 var Z = 'Z'.charCodeAt(0); 4902 4903 iban = iban.toUpperCase(); 4904 iban = iban.substr(4) + iban.substr(0,4); 4905 4906 return iban.split('').map(function(n){ 4907 var code = n.charCodeAt(0); 4908 if (code >= A && code <= Z){ 4909 // A = 10, B = 11, ... Z = 35 4910 return code - A + 10; 4911 } else { 4912 return n; 4913 } 4914 }).join(''); 4915 }; 4916 4917 /** 4918 * Calculates the MOD 97 10 of the passed IBAN as specified in ISO7064. 4919 * 4920 * @method mod9710 4921 * @param {String} iban 4922 * @returns {Number} 4923 */ 4924 var mod9710 = function (iban) { 4925 var remainder = iban, 4926 block; 4927 4928 while (remainder.length > 2){ 4929 block = remainder.slice(0, 9); 4930 remainder = parseInt(block, 10) % 97 + remainder.slice(block.length); 4931 } 4932 4933 return parseInt(remainder, 10) % 97; 4934 }; 4935 4936 /** 4937 * This prototype should be used to create iban object from iban correct string 4938 * 4939 * @param {String} iban 4940 */ 4941 var Iban = function (iban) { 4942 this._iban = iban; 4943 }; 4944 4945 /** 4946 * This method should be used to create iban object from vntchain address 4947 * 4948 * @method fromAddress 4949 * @param {String} address 4950 * @return {Iban} the IBAN object 4951 */ 4952 Iban.fromAddress = function (address) { 4953 var asBn = new BigNumber(address, 16); 4954 var base36 = asBn.toString(36); 4955 var padded = padLeft(base36, 15); 4956 return Iban.fromBban(padded.toUpperCase()); 4957 }; 4958 4959 /** 4960 * Convert the passed BBAN to an IBAN for this country specification. 4961 * Please note that <i>"generation of the IBAN shall be the exclusive responsibility of the bank/branch servicing the account"</i>. 4962 * This method implements the preferred algorithm described in http://en.wikipedia.org/wiki/International_Bank_Account_Number#Generating_IBAN_check_digits 4963 * 4964 * @method fromBban 4965 * @param {String} bban the BBAN to convert to IBAN 4966 * @returns {Iban} the IBAN object 4967 */ 4968 Iban.fromBban = function (bban) { 4969 var countryCode = 'XE'; 4970 4971 var remainder = mod9710(iso13616Prepare(countryCode + '00' + bban)); 4972 var checkDigit = ('0' + (98 - remainder)).slice(-2); 4973 4974 return new Iban(countryCode + checkDigit + bban); 4975 }; 4976 4977 /** 4978 * Should be used to create IBAN object for given institution and identifier 4979 * 4980 * @method createIndirect 4981 * @param {Object} options, required options are "institution" and "identifier" 4982 * @return {Iban} the IBAN object 4983 */ 4984 Iban.createIndirect = function (options) { 4985 return Iban.fromBban('VNT' + options.institution + options.identifier); 4986 }; 4987 4988 /** 4989 * Thos method should be used to check if given string is valid iban object 4990 * 4991 * @method isValid 4992 * @param {String} iban string 4993 * @return {Boolean} true if it is valid IBAN 4994 */ 4995 Iban.isValid = function (iban) { 4996 var i = new Iban(iban); 4997 return i.isValid(); 4998 }; 4999 5000 /** 5001 * Should be called to check if iban is correct 5002 * 5003 * @method isValid 5004 * @returns {Boolean} true if it is, otherwise false 5005 */ 5006 Iban.prototype.isValid = function () { 5007 return /^XE[0-9]{2}(VNT[0-9A-Z]{13}|[0-9A-Z]{30,31})$/.test(this._iban) && 5008 mod9710(iso13616Prepare(this._iban)) === 1; 5009 }; 5010 5011 /** 5012 * Should be called to check if iban number is direct 5013 * 5014 * @method isDirect 5015 * @returns {Boolean} true if it is, otherwise false 5016 */ 5017 Iban.prototype.isDirect = function () { 5018 return this._iban.length === 34 || this._iban.length === 35; 5019 }; 5020 5021 /** 5022 * Should be called to check if iban number if indirect 5023 * 5024 * @method isIndirect 5025 * @returns {Boolean} true if it is, otherwise false 5026 */ 5027 Iban.prototype.isIndirect = function () { 5028 return this._iban.length === 20; 5029 }; 5030 5031 /** 5032 * Should be called to get iban checksum 5033 * Uses the mod-97-10 checksumming protocol (ISO/IEC 7064:2003) 5034 * 5035 * @method checksum 5036 * @returns {String} checksum 5037 */ 5038 Iban.prototype.checksum = function () { 5039 return this._iban.substr(2, 2); 5040 }; 5041 5042 /** 5043 * Should be called to get institution identifier 5044 * eg. XREG 5045 * 5046 * @method institution 5047 * @returns {String} institution identifier 5048 */ 5049 Iban.prototype.institution = function () { 5050 return this.isIndirect() ? this._iban.substr(7, 4) : ''; 5051 }; 5052 5053 /** 5054 * Should be called to get client identifier within institution 5055 * eg. GAVOFYORK 5056 * 5057 * @method client 5058 * @returns {String} client identifier 5059 */ 5060 Iban.prototype.client = function () { 5061 return this.isIndirect() ? this._iban.substr(11) : ''; 5062 }; 5063 5064 /** 5065 * Should be called to get client direct address 5066 * 5067 * @method address 5068 * @returns {String} client direct address 5069 */ 5070 Iban.prototype.address = function () { 5071 if (this.isDirect()) { 5072 var base36 = this._iban.substr(4); 5073 var asBn = new BigNumber(base36, 36); 5074 return padLeft(asBn.toString(16), 20); 5075 } 5076 5077 return ''; 5078 }; 5079 5080 Iban.prototype.toString = function () { 5081 return this._iban; 5082 }; 5083 5084 module.exports = Iban; 5085 5086 },{"bignumber.js":"bignumber.js"}],35:[function(require,module,exports){ 5087 /* 5088 This file is part of vnt.js. 5089 5090 vnt.js is free software: you can redistribute it and/or modify 5091 it under the terms of the GNU Lesser General Public License as published by 5092 the Free Software Foundation, either version 3 of the License, or 5093 (at your option) any later version. 5094 5095 vnt.js is distributed in the hope that it will be useful, 5096 but WITHOUT ANY WARRANTY; without even the implied warranty of 5097 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 5098 GNU Lesser General Public License for more details. 5099 5100 You should have received a copy of the GNU Lesser General Public License 5101 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 5102 */ 5103 /** @file ipcprovider.js 5104 * @authors: 5105 * Fabian Vogelsteller <fabian@ethdev.com> 5106 * @date 2015 5107 */ 5108 5109 "use strict"; 5110 5111 var utils = require('../utils/utils'); 5112 var errors = require('./errors'); 5113 5114 5115 var IpcProvider = function (path, net) { 5116 var _this = this; 5117 this.responseCallbacks = {}; 5118 this.path = path; 5119 5120 this.connection = net.connect({path: this.path}); 5121 5122 this.connection.on('error', function(e){ 5123 console.error('IPC Connection Error', e); 5124 _this._timeout(); 5125 }); 5126 5127 this.connection.on('end', function(){ 5128 _this._timeout(); 5129 }); 5130 5131 5132 // LISTEN FOR CONNECTION RESPONSES 5133 this.connection.on('data', function(data) { 5134 /*jshint maxcomplexity: 6 */ 5135 5136 _this._parseResponse(data.toString()).forEach(function(result){ 5137 5138 var id = null; 5139 5140 // get the id which matches the returned id 5141 if(utils.isArray(result)) { 5142 result.forEach(function(load){ 5143 if(_this.responseCallbacks[load.id]) 5144 id = load.id; 5145 }); 5146 } else { 5147 id = result.id; 5148 } 5149 5150 // fire the callback 5151 if(_this.responseCallbacks[id]) { 5152 _this.responseCallbacks[id](null, result); 5153 delete _this.responseCallbacks[id]; 5154 } 5155 }); 5156 }); 5157 }; 5158 5159 /** 5160 Will parse the response and make an array out of it. 5161 5162 @method _parseResponse 5163 @param {String} data 5164 */ 5165 IpcProvider.prototype._parseResponse = function(data) { 5166 var _this = this, 5167 returnValues = []; 5168 5169 // DE-CHUNKER 5170 var dechunkedData = data 5171 .replace(/\}[\n\r]?\{/g,'}|--|{') // }{ 5172 .replace(/\}\][\n\r]?\[\{/g,'}]|--|[{') // }][{ 5173 .replace(/\}[\n\r]?\[\{/g,'}|--|[{') // }[{ 5174 .replace(/\}\][\n\r]?\{/g,'}]|--|{') // }]{ 5175 .split('|--|'); 5176 5177 dechunkedData.forEach(function(data){ 5178 5179 // prepend the last chunk 5180 if(_this.lastChunk) 5181 data = _this.lastChunk + data; 5182 5183 var result = null; 5184 5185 try { 5186 result = JSON.parse(data); 5187 5188 } catch(e) { 5189 5190 _this.lastChunk = data; 5191 5192 // start timeout to cancel all requests 5193 clearTimeout(_this.lastChunkTimeout); 5194 _this.lastChunkTimeout = setTimeout(function(){ 5195 _this._timeout(); 5196 throw errors.InvalidResponse(data); 5197 }, 1000 * 15); 5198 5199 return; 5200 } 5201 5202 // cancel timeout and set chunk to null 5203 clearTimeout(_this.lastChunkTimeout); 5204 _this.lastChunk = null; 5205 5206 if(result) 5207 returnValues.push(result); 5208 }); 5209 5210 return returnValues; 5211 }; 5212 5213 5214 /** 5215 Get the adds a callback to the responseCallbacks object, 5216 which will be called if a response matching the response Id will arrive. 5217 5218 @method _addResponseCallback 5219 */ 5220 IpcProvider.prototype._addResponseCallback = function(payload, callback) { 5221 var id = payload.id || payload[0].id; 5222 var method = payload.method || payload[0].method; 5223 5224 this.responseCallbacks[id] = callback; 5225 this.responseCallbacks[id].method = method; 5226 }; 5227 5228 /** 5229 Timeout all requests when the end/error event is fired 5230 5231 @method _timeout 5232 */ 5233 IpcProvider.prototype._timeout = function() { 5234 for(var key in this.responseCallbacks) { 5235 if(this.responseCallbacks.hasOwnProperty(key)){ 5236 this.responseCallbacks[key](errors.InvalidConnection('on IPC')); 5237 delete this.responseCallbacks[key]; 5238 } 5239 } 5240 }; 5241 5242 5243 /** 5244 Check if the current connection is still valid. 5245 5246 @method isConnected 5247 */ 5248 IpcProvider.prototype.isConnected = function() { 5249 var _this = this; 5250 5251 // try reconnect, when connection is gone 5252 if(!_this.connection.writable) 5253 _this.connection.connect({path: _this.path}); 5254 5255 return !!this.connection.writable; 5256 }; 5257 5258 IpcProvider.prototype.send = function (payload) { 5259 5260 if(this.connection.writeSync) { 5261 var result; 5262 5263 // try reconnect, when connection is gone 5264 if(!this.connection.writable) 5265 this.connection.connect({path: this.path}); 5266 5267 var data = this.connection.writeSync(JSON.stringify(payload)); 5268 5269 try { 5270 result = JSON.parse(data); 5271 } catch(e) { 5272 throw errors.InvalidResponse(data); 5273 } 5274 5275 return result; 5276 5277 } else { 5278 throw new Error('You tried to send "'+ payload.method +'" synchronously. Synchronous requests are not supported by the IPC provider.'); 5279 } 5280 }; 5281 5282 IpcProvider.prototype.sendAsync = function (payload, callback) { 5283 // try reconnect, when connection is gone 5284 if(!this.connection.writable) 5285 this.connection.connect({path: this.path}); 5286 5287 5288 this.connection.write(JSON.stringify(payload)); 5289 this._addResponseCallback(payload, callback); 5290 }; 5291 5292 module.exports = IpcProvider; 5293 5294 },{"../utils/utils":21,"./errors":27}],36:[function(require,module,exports){ 5295 /* 5296 This file is part of vnt.js. 5297 5298 vnt.js is free software: you can redistribute it and/or modify 5299 it under the terms of the GNU Lesser General Public License as published by 5300 the Free Software Foundation, either version 3 of the License, or 5301 (at your option) any later version. 5302 5303 vnt.js is distributed in the hope that it will be useful, 5304 but WITHOUT ANY WARRANTY; without even the implied warranty of 5305 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 5306 GNU Lesser General Public License for more details. 5307 5308 You should have received a copy of the GNU Lesser General Public License 5309 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 5310 */ 5311 /** @file jsonrpc.js 5312 * @authors: 5313 * Marek Kotewicz <marek@ethdev.com> 5314 * Aaron Kumavis <aaron@kumavis.me> 5315 * @date 2015 5316 */ 5317 5318 // Initialize Jsonrpc as a simple object with utility functions. 5319 var Jsonrpc = { 5320 messageId: 0 5321 }; 5322 5323 /** 5324 * Should be called to valid json create payload object 5325 * 5326 * @method toPayload 5327 * @param {Function} method of jsonrpc call, required 5328 * @param {Array} params, an array of method params, optional 5329 * @returns {Object} valid jsonrpc payload object 5330 */ 5331 Jsonrpc.toPayload = function (method, params) { 5332 if (!method) 5333 console.error('jsonrpc method should be specified!'); 5334 5335 // advance message ID 5336 Jsonrpc.messageId++; 5337 5338 return { 5339 jsonrpc: '2.0', 5340 id: Jsonrpc.messageId, 5341 method: method, 5342 params: params || [] 5343 }; 5344 }; 5345 5346 /** 5347 * Should be called to check if jsonrpc response is valid 5348 * 5349 * @method isValidResponse 5350 * @param {Object} 5351 * @returns {Boolean} true if response is valid, otherwise false 5352 */ 5353 Jsonrpc.isValidResponse = function (response) { 5354 return Array.isArray(response) ? response.every(validateSingleMessage) : validateSingleMessage(response); 5355 5356 function validateSingleMessage(message){ 5357 return !!message && 5358 !message.error && 5359 message.jsonrpc === '2.0' && 5360 typeof message.id === 'number' && 5361 message.result !== undefined; // only undefined is not valid json object 5362 } 5363 }; 5364 5365 /** 5366 * Should be called to create batch payload object 5367 * 5368 * @method toBatchPayload 5369 * @param {Array} messages, an array of objects with method (required) and params (optional) fields 5370 * @returns {Array} batch payload 5371 */ 5372 Jsonrpc.toBatchPayload = function (messages) { 5373 return messages.map(function (message) { 5374 return Jsonrpc.toPayload(message.method, message.params); 5375 }); 5376 }; 5377 5378 module.exports = Jsonrpc; 5379 5380 5381 },{}],37:[function(require,module,exports){ 5382 /* 5383 This file is part of vnt.js. 5384 5385 vnt.js is free software: you can redistribute it and/or modify 5386 it under the terms of the GNU Lesser General Public License as published by 5387 the Free Software Foundation, either version 3 of the License, or 5388 (at your option) any later version. 5389 5390 vnt.js is distributed in the hope that it will be useful, 5391 but WITHOUT ANY WARRANTY; without even the implied warranty of 5392 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 5393 GNU Lesser General Public License for more details. 5394 5395 You should have received a copy of the GNU Lesser General Public License 5396 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 5397 */ 5398 /** 5399 * @file method.js 5400 * @author Marek Kotewicz <marek@ethdev.com> 5401 * @date 2015 5402 */ 5403 5404 var utils = require('../utils/utils'); 5405 var errors = require('./errors'); 5406 5407 var Method = function (options) { 5408 this.name = options.name; 5409 this.call = options.call; 5410 this.params = options.params || 0; 5411 this.inputFormatter = options.inputFormatter; 5412 this.outputFormatter = options.outputFormatter; 5413 this.requestManager = null; 5414 }; 5415 5416 Method.prototype.setRequestManager = function (rm) { 5417 this.requestManager = rm; 5418 }; 5419 5420 /** 5421 * Should be used to determine name of the jsonrpc method based on arguments 5422 * 5423 * @method getCall 5424 * @param {Array} arguments 5425 * @return {String} name of jsonrpc method 5426 */ 5427 Method.prototype.getCall = function (args) { 5428 return utils.isFunction(this.call) ? this.call(args) : this.call; 5429 }; 5430 5431 /** 5432 * Should be used to extract callback from array of arguments. Modifies input param 5433 * 5434 * @method extractCallback 5435 * @param {Array} arguments 5436 * @return {Function|Null} callback, if exists 5437 */ 5438 Method.prototype.extractCallback = function (args) { 5439 if (utils.isFunction(args[args.length - 1])) { 5440 return args.pop(); // modify the args array! 5441 } 5442 }; 5443 5444 /** 5445 * Should be called to check if the number of arguments is correct 5446 * 5447 * @method validateArgs 5448 * @param {Array} arguments 5449 * @throws {Error} if it is not 5450 */ 5451 Method.prototype.validateArgs = function (args) { 5452 if (args.length !== this.params) { 5453 throw errors.InvalidNumberOfRPCParams(); 5454 } 5455 }; 5456 5457 /** 5458 * Should be called to format input args of method 5459 * 5460 * @method formatInput 5461 * @param {Array} 5462 * @return {Array} 5463 */ 5464 Method.prototype.formatInput = function (args) { 5465 if (!this.inputFormatter) { 5466 return args; 5467 } 5468 5469 return this.inputFormatter.map(function (formatter, index) { 5470 return formatter ? formatter(args[index]) : args[index]; 5471 }); 5472 }; 5473 5474 /** 5475 * Should be called to format output(result) of method 5476 * 5477 * @method formatOutput 5478 * @param {Object} 5479 * @return {Object} 5480 */ 5481 Method.prototype.formatOutput = function (result) { 5482 return this.outputFormatter && result ? this.outputFormatter(result) : result; 5483 }; 5484 5485 /** 5486 * Should create payload from given input args 5487 * 5488 * @method toPayload 5489 * @param {Array} args 5490 * @return {Object} 5491 */ 5492 Method.prototype.toPayload = function (args) { 5493 var call = this.getCall(args); 5494 var callback = this.extractCallback(args); 5495 var params = this.formatInput(args); 5496 this.validateArgs(params); 5497 5498 return { 5499 method: call, 5500 params: params, 5501 callback: callback 5502 }; 5503 }; 5504 5505 Method.prototype.attachToObject = function (obj) { 5506 var func = this.buildCall(); 5507 func.call = this.call; // TODO!!! that's ugly. filter.js uses it 5508 var name = this.name.split('.'); 5509 if (name.length > 1) { 5510 obj[name[0]] = obj[name[0]] || {}; 5511 obj[name[0]][name[1]] = func; 5512 } else { 5513 obj[name[0]] = func; 5514 } 5515 }; 5516 5517 Method.prototype.buildCall = function() { 5518 var method = this; 5519 var send = function () { 5520 var payload = method.toPayload(Array.prototype.slice.call(arguments)); 5521 if (payload.callback) { 5522 return method.requestManager.sendAsync(payload, function (err, result) { 5523 payload.callback(err, method.formatOutput(result)); 5524 }); 5525 } 5526 return method.formatOutput(method.requestManager.send(payload)); 5527 }; 5528 send.request = this.request.bind(this); 5529 return send; 5530 }; 5531 5532 /** 5533 * Should be called to create pure JSONRPC request which can be used in batch request 5534 * 5535 * @method request 5536 * @param {...} params 5537 * @return {Object} jsonrpc request 5538 */ 5539 Method.prototype.request = function () { 5540 var payload = this.toPayload(Array.prototype.slice.call(arguments)); 5541 payload.format = this.formatOutput.bind(this); 5542 return payload; 5543 }; 5544 5545 module.exports = Method; 5546 5547 },{"../utils/utils":21,"./errors":27}],38:[function(require,module,exports){ 5548 /* 5549 This file is part of vnt.js. 5550 5551 vnt.js is free software: you can redistribute it and/or modify 5552 it under the terms of the GNU Lesser General Public License as published by 5553 the Free Software Foundation, either version 3 of the License, or 5554 (at your option) any later version. 5555 5556 vnt.js is distributed in the hope that it will be useful, 5557 but WITHOUT ANY WARRANTY; without even the implied warranty of 5558 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 5559 GNU Lesser General Public License for more details. 5560 5561 You should have received a copy of the GNU Lesser General Public License 5562 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 5563 */ 5564 /** 5565 * @file core.js 5566 * @author Marek Kotewicz <marek@ethdev.com> 5567 * @author Fabian Vogelsteller <fabian@ethdev.com> 5568 * @date 2015 5569 */ 5570 5571 "use strict"; 5572 5573 var formatters = require('../formatters'); 5574 var utils = require('../../utils/utils'); 5575 var Method = require('../method'); 5576 var Property = require('../property'); 5577 var c = require('../../utils/config'); 5578 var Contract = require('../contract'); 5579 var watches = require('./watches'); 5580 var Filter = require('../filter'); 5581 var IsSyncing = require('../syncing'); 5582 var namereg = require('../namereg'); 5583 var Iban = require('../iban'); 5584 var transfer = require('../transfer'); 5585 5586 var blockCall = function (args) { 5587 return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? "core_getBlockByHash" : "core_getBlockByNumber"; 5588 }; 5589 5590 var transactionFromBlockCall = function (args) { 5591 return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'core_getTransactionByBlockHashAndIndex' : 'core_getTransactionByBlockNumberAndIndex'; 5592 }; 5593 5594 var getBlockTransactionCountCall = function (args) { 5595 return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'core_getBlockTransactionCountByHash' : 'core_getBlockTransactionCountByNumber'; 5596 }; 5597 5598 function Core(vnt) { 5599 this._requestManager = vnt._requestManager; 5600 5601 var self = this; 5602 5603 methods().forEach(function(method) { 5604 method.attachToObject(self); 5605 method.setRequestManager(self._requestManager); 5606 }); 5607 5608 properties().forEach(function(p) { 5609 p.attachToObject(self); 5610 p.setRequestManager(self._requestManager); 5611 }); 5612 5613 5614 this.iban = Iban; 5615 this.sendIBANTransaction = transfer.bind(null, this); 5616 } 5617 5618 Object.defineProperty(Core.prototype, 'defaultBlock', { 5619 get: function () { 5620 return c.defaultBlock; 5621 }, 5622 set: function (val) { 5623 c.defaultBlock = val; 5624 return val; 5625 } 5626 }); 5627 5628 Object.defineProperty(Core.prototype, 'defaultAccount', { 5629 get: function () { 5630 return c.defaultAccount; 5631 }, 5632 set: function (val) { 5633 c.defaultAccount = val; 5634 return val; 5635 } 5636 }); 5637 5638 var methods = function () { 5639 var getBalance = new Method({ 5640 name: 'getBalance', 5641 call: 'core_getBalance', 5642 params: 2, 5643 inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter], 5644 outputFormatter: formatters.outputBigNumberFormatter 5645 }); 5646 5647 var getStorageAt = new Method({ 5648 name: 'getStorageAt', 5649 call: 'core_getStorageAt', 5650 params: 3, 5651 inputFormatter: [null, utils.toHex, formatters.inputDefaultBlockNumberFormatter] 5652 }); 5653 5654 var getCode = new Method({ 5655 name: 'getCode', 5656 call: 'core_getCode', 5657 params: 2, 5658 inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter] 5659 }); 5660 5661 var getBlock = new Method({ 5662 name: 'getBlock', 5663 call: blockCall, 5664 params: 2, 5665 inputFormatter: [formatters.inputBlockNumberFormatter, function (val) { return !!val; }], 5666 outputFormatter: formatters.outputBlockFormatter 5667 }); 5668 5669 var getBlockTransactionCount = new Method({ 5670 name: 'getBlockTransactionCount', 5671 call: getBlockTransactionCountCall, 5672 params: 1, 5673 inputFormatter: [formatters.inputBlockNumberFormatter], 5674 outputFormatter: utils.toDecimal 5675 }); 5676 5677 var getTransaction = new Method({ 5678 name: 'getTransaction', 5679 call: 'core_getTransactionByHash', 5680 params: 1, 5681 outputFormatter: formatters.outputTransactionFormatter 5682 }); 5683 5684 var getTransactionFromBlock = new Method({ 5685 name: 'getTransactionFromBlock', 5686 call: transactionFromBlockCall, 5687 params: 2, 5688 inputFormatter: [formatters.inputBlockNumberFormatter, utils.toHex], 5689 outputFormatter: formatters.outputTransactionFormatter 5690 }); 5691 5692 var getTransactionReceipt = new Method({ 5693 name: 'getTransactionReceipt', 5694 call: 'core_getTransactionReceipt', 5695 params: 1, 5696 outputFormatter: formatters.outputTransactionReceiptFormatter 5697 }); 5698 5699 var getTransactionCount = new Method({ 5700 name: 'getTransactionCount', 5701 call: 'core_getTransactionCount', 5702 params: 2, 5703 inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter], 5704 outputFormatter: utils.toDecimal 5705 }); 5706 5707 var sendRawTransaction = new Method({ 5708 name: 'sendRawTransaction', 5709 call: 'core_sendRawTransaction', 5710 params: 1, 5711 inputFormatter: [null] 5712 }); 5713 5714 var sendTransaction = new Method({ 5715 name: 'sendTransaction', 5716 call: 'core_sendTransaction', 5717 params: 1, 5718 inputFormatter: [formatters.inputTransactionFormatter] 5719 }); 5720 5721 var signTransaction = new Method({ 5722 name: 'signTransaction', 5723 call: 'core_signTransaction', 5724 params: 1, 5725 inputFormatter: [formatters.inputTransactionFormatter] 5726 }); 5727 5728 var sign = new Method({ 5729 name: 'sign', 5730 call: 'core_sign', 5731 params: 2, 5732 inputFormatter: [formatters.inputAddressFormatter, null] 5733 }); 5734 5735 var call = new Method({ 5736 name: 'call', 5737 call: 'core_call', 5738 params: 2, 5739 inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter] 5740 }); 5741 5742 var estimateGas = new Method({ 5743 name: 'estimateGas', 5744 call: 'core_estimateGas', 5745 params: 1, 5746 inputFormatter: [formatters.inputCallFormatter], 5747 outputFormatter: utils.toDecimal 5748 }); 5749 5750 return [ 5751 getBalance, 5752 getStorageAt, 5753 getCode, 5754 getBlock, 5755 getBlockTransactionCount, 5756 getTransaction, 5757 getTransactionFromBlock, 5758 getTransactionReceipt, 5759 getTransactionCount, 5760 call, 5761 estimateGas, 5762 sendRawTransaction, 5763 signTransaction, 5764 sendTransaction, 5765 sign 5766 ]; 5767 }; 5768 5769 5770 var properties = function () { 5771 return [ 5772 new Property({ 5773 name: 'coinbase', 5774 getter: 'core_coinbase' 5775 }), 5776 new Property({ 5777 name: 'producing', 5778 getter: 'core_producing' 5779 }), 5780 new Property({ 5781 name: 'syncing', 5782 getter: 'core_syncing', 5783 outputFormatter: formatters.outputSyncingFormatter 5784 }), 5785 new Property({ 5786 name: 'gasPrice', 5787 getter: 'core_gasPrice', 5788 outputFormatter: formatters.outputBigNumberFormatter 5789 }), 5790 new Property({ 5791 name: 'accounts', 5792 getter: 'core_accounts' 5793 }), 5794 new Property({ 5795 name: 'blockNumber', 5796 getter: 'core_blockNumber', 5797 outputFormatter: utils.toDecimal 5798 }), 5799 new Property({ 5800 name: 'protocolVersion', 5801 getter: 'core_protocolVersion' 5802 }) 5803 ]; 5804 }; 5805 5806 Core.prototype.contract = function (abi) { 5807 var factory = new Contract(this, abi); 5808 return factory; 5809 }; 5810 5811 Core.prototype.filter = function (options, callback, filterCreationErrorCallback) { 5812 return new Filter(options, 'vnt', this._requestManager, watches.vnt(), formatters.outputLogFormatter, callback, filterCreationErrorCallback); 5813 }; 5814 5815 Core.prototype.namereg = function () { 5816 return this.contract(namereg.global.abi).at(namereg.global.address); 5817 }; 5818 5819 Core.prototype.icapNamereg = function () { 5820 return this.contract(namereg.icap.abi).at(namereg.icap.address); 5821 }; 5822 5823 Core.prototype.isSyncing = function (callback) { 5824 return new IsSyncing(this._requestManager, callback); 5825 }; 5826 5827 module.exports = Core; 5828 5829 },{"../../utils/config":18,"../../utils/utils":21,"../contract":26,"../filter":30,"../formatters":31,"../iban":34,"../method":37,"../namereg":44,"../property":45,"../syncing":48,"../transfer":49,"./watches":43}],39:[function(require,module,exports){ 5830 /* 5831 This file is part of vnt.js. 5832 5833 vnt.js is free software: you can redistribute it and/or modify 5834 it under the terms of the GNU Lesser General Public License as published by 5835 the Free Software Foundation, either version 3 of the License, or 5836 (at your option) any later version. 5837 5838 vnt.js is distributed in the hope that it will be useful, 5839 but WITHOUT ANY WARRANTY; without even the implied warranty of 5840 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 5841 GNU Lesser General Public License for more details. 5842 5843 You should have received a copy of the GNU Lesser General Public License 5844 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 5845 */ 5846 /** @file net.js 5847 * @authors: 5848 * Marek Kotewicz <marek@ethdev.com> 5849 * @date 2015 5850 */ 5851 5852 var utils = require('../../utils/utils'); 5853 var Property = require('../property'); 5854 5855 var Net = function (vnt) { 5856 this._requestManager = vnt._requestManager; 5857 5858 var self = this; 5859 5860 properties().forEach(function(p) { 5861 p.attachToObject(self); 5862 p.setRequestManager(vnt._requestManager); 5863 }); 5864 }; 5865 5866 /// @returns an array of objects describing vnt.core api properties 5867 var properties = function () { 5868 return [ 5869 new Property({ 5870 name: 'listening', 5871 getter: 'net_listening' 5872 }), 5873 new Property({ 5874 name: 'peerCount', 5875 getter: 'net_peerCount', 5876 outputFormatter: utils.toDecimal 5877 }) 5878 ]; 5879 }; 5880 5881 module.exports = Net; 5882 5883 },{"../../utils/utils":21,"../property":45}],40:[function(require,module,exports){ 5884 /* 5885 This file is part of vnt.js. 5886 5887 vnt.js is free software: you can redistribute it and/or modify 5888 it under the terms of the GNU Lesser General Public License as published by 5889 the Free Software Foundation, either version 3 of the License, or 5890 (at your option) any later version. 5891 5892 vnt.js is distributed in the hope that it will be useful, 5893 but WITHOUT ANY WARRANTY; without even the implied warranty of 5894 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 5895 GNU Lesser General Public License for more details. 5896 5897 You should have received a copy of the GNU Lesser General Public License 5898 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 5899 */ 5900 /** 5901 * @file personal.js 5902 * @author Marek Kotewicz <marek@ethdev.com> 5903 * @author Fabian Vogelsteller <fabian@ethdev.com> 5904 * @date 2015 5905 */ 5906 5907 "use strict"; 5908 5909 var Method = require('../method'); 5910 var Property = require('../property'); 5911 var formatters = require('../formatters'); 5912 5913 function Personal(vnt) { 5914 this._requestManager = vnt._requestManager; 5915 5916 var self = this; 5917 5918 methods().forEach(function(method) { 5919 method.attachToObject(self); 5920 method.setRequestManager(self._requestManager); 5921 }); 5922 5923 properties().forEach(function(p) { 5924 p.attachToObject(self); 5925 p.setRequestManager(self._requestManager); 5926 }); 5927 } 5928 5929 var methods = function () { 5930 var newAccount = new Method({ 5931 name: 'newAccount', 5932 call: 'personal_newAccount', 5933 params: 1, 5934 inputFormatter: [null] 5935 }); 5936 5937 var importRawKey = new Method({ 5938 name: 'importRawKey', 5939 call: 'personal_importRawKey', 5940 params: 2 5941 }); 5942 5943 var sign = new Method({ 5944 name: 'sign', 5945 call: 'personal_sign', 5946 params: 3, 5947 inputFormatter: [null, formatters.inputAddressFormatter, null] 5948 }); 5949 5950 var ecRecover = new Method({ 5951 name: 'ecRecover', 5952 call: 'personal_ecRecover', 5953 params: 2 5954 }); 5955 5956 var unlockAccount = new Method({ 5957 name: 'unlockAccount', 5958 call: 'personal_unlockAccount', 5959 params: 3, 5960 inputFormatter: [formatters.inputAddressFormatter, null, null] 5961 }); 5962 5963 var sendTransaction = new Method({ 5964 name: 'sendTransaction', 5965 call: 'personal_sendTransaction', 5966 params: 2, 5967 inputFormatter: [formatters.inputTransactionFormatter, null] 5968 }); 5969 5970 var lockAccount = new Method({ 5971 name: 'lockAccount', 5972 call: 'personal_lockAccount', 5973 params: 1, 5974 inputFormatter: [formatters.inputAddressFormatter] 5975 }); 5976 5977 return [ 5978 newAccount, 5979 importRawKey, 5980 unlockAccount, 5981 ecRecover, 5982 sign, 5983 sendTransaction, 5984 lockAccount 5985 ]; 5986 }; 5987 5988 var properties = function () { 5989 return [ 5990 new Property({ 5991 name: 'listAccounts', 5992 getter: 'personal_listAccounts' 5993 }) 5994 ]; 5995 }; 5996 5997 5998 module.exports = Personal; 5999 6000 },{"../formatters":31,"../method":37,"../property":45}],41:[function(require,module,exports){ 6001 /* 6002 This file is part of vnt.js. 6003 6004 vnt.js is free software: you can redistribute it and/or modify 6005 it under the terms of the GNU Lesser General Public License as published by 6006 the Free Software Foundation, either version 3 of the License, or 6007 (at your option) any later version. 6008 6009 vnt.js is distributed in the hope that it will be useful, 6010 but WITHOUT ANY WARRANTY; without even the implied warranty of 6011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 6012 GNU Lesser General Public License for more details. 6013 6014 You should have received a copy of the GNU Lesser General Public License 6015 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 6016 */ 6017 /** @file shh.js 6018 * @authors: 6019 * Fabian Vogelsteller <fabian@ethereum.org> 6020 * Marek Kotewicz <marek@ethcore.io> 6021 * @date 2017 6022 */ 6023 6024 var Method = require('../method'); 6025 var Filter = require('../filter'); 6026 var watches = require('./watches'); 6027 6028 var Shh = function (vnt) { 6029 this._requestManager = vnt._requestManager; 6030 6031 var self = this; 6032 6033 methods().forEach(function(method) { 6034 method.attachToObject(self); 6035 method.setRequestManager(self._requestManager); 6036 }); 6037 }; 6038 6039 Shh.prototype.newMessageFilter = function (options, callback, filterCreationErrorCallback) { 6040 return new Filter(options, 'shh', this._requestManager, watches.shh(), null, callback, filterCreationErrorCallback); 6041 }; 6042 6043 var methods = function () { 6044 6045 return [ 6046 new Method({ 6047 name: 'version', 6048 call: 'shh_version', 6049 params: 0 6050 }), 6051 new Method({ 6052 name: 'info', 6053 call: 'shh_info', 6054 params: 0 6055 }), 6056 new Method({ 6057 name: 'setMaxMessageSize', 6058 call: 'shh_setMaxMessageSize', 6059 params: 1 6060 }), 6061 new Method({ 6062 name: 'setMinPoW', 6063 call: 'shh_setMinPoW', 6064 params: 1 6065 }), 6066 new Method({ 6067 name: 'markTrustedPeer', 6068 call: 'shh_markTrustedPeer', 6069 params: 1 6070 }), 6071 new Method({ 6072 name: 'newKeyPair', 6073 call: 'shh_newKeyPair', 6074 params: 0 6075 }), 6076 new Method({ 6077 name: 'addPrivateKey', 6078 call: 'shh_addPrivateKey', 6079 params: 1 6080 }), 6081 new Method({ 6082 name: 'deleteKeyPair', 6083 call: 'shh_deleteKeyPair', 6084 params: 1 6085 }), 6086 new Method({ 6087 name: 'hasKeyPair', 6088 call: 'shh_hasKeyPair', 6089 params: 1 6090 }), 6091 new Method({ 6092 name: 'getPublicKey', 6093 call: 'shh_getPublicKey', 6094 params: 1 6095 }), 6096 new Method({ 6097 name: 'getPrivateKey', 6098 call: 'shh_getPrivateKey', 6099 params: 1 6100 }), 6101 new Method({ 6102 name: 'newSymKey', 6103 call: 'shh_newSymKey', 6104 params: 0 6105 }), 6106 new Method({ 6107 name: 'addSymKey', 6108 call: 'shh_addSymKey', 6109 params: 1 6110 }), 6111 new Method({ 6112 name: 'generateSymKeyFromPassword', 6113 call: 'shh_generateSymKeyFromPassword', 6114 params: 1 6115 }), 6116 new Method({ 6117 name: 'hasSymKey', 6118 call: 'shh_hasSymKey', 6119 params: 1 6120 }), 6121 new Method({ 6122 name: 'getSymKey', 6123 call: 'shh_getSymKey', 6124 params: 1 6125 }), 6126 new Method({ 6127 name: 'deleteSymKey', 6128 call: 'shh_deleteSymKey', 6129 params: 1 6130 }), 6131 6132 // subscribe and unsubscribe missing 6133 6134 new Method({ 6135 name: 'post', 6136 call: 'shh_post', 6137 params: 1, 6138 inputFormatter: [null] 6139 }) 6140 ]; 6141 }; 6142 6143 module.exports = Shh; 6144 6145 6146 },{"../filter":30,"../method":37,"./watches":43}],42:[function(require,module,exports){ 6147 /* 6148 This file is part of vnt.js. 6149 6150 vnt.js is free software: you can redistribute it and/or modify 6151 it under the terms of the GNU Lesser General Public License as published by 6152 the Free Software Foundation, either version 3 of the License, or 6153 (at your option) any later version. 6154 6155 vnt.js is distributed in the hope that it will be useful, 6156 but WITHOUT ANY WARRANTY; without even the implied warranty of 6157 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 6158 GNU Lesser General Public License for more details. 6159 6160 You should have received a copy of the GNU Lesser General Public License 6161 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 6162 */ 6163 /** 6164 * @file bzz.js 6165 * @author Alex Beregszaszi <alex@rtfs.hu> 6166 * @date 2016 6167 * 6168 */ 6169 6170 "use strict"; 6171 6172 var Method = require('../method'); 6173 var Property = require('../property'); 6174 6175 function Swarm(vnt) { 6176 this._requestManager = vnt._requestManager; 6177 6178 var self = this; 6179 6180 methods().forEach(function(method) { 6181 method.attachToObject(self); 6182 method.setRequestManager(self._requestManager); 6183 }); 6184 6185 properties().forEach(function(p) { 6186 p.attachToObject(self); 6187 p.setRequestManager(self._requestManager); 6188 }); 6189 } 6190 6191 var methods = function () { 6192 var blockNetworkRead = new Method({ 6193 name: 'blockNetworkRead', 6194 call: 'bzz_blockNetworkRead', 6195 params: 1, 6196 inputFormatter: [null] 6197 }); 6198 6199 var syncEnabled = new Method({ 6200 name: 'syncEnabled', 6201 call: 'bzz_syncEnabled', 6202 params: 1, 6203 inputFormatter: [null] 6204 }); 6205 6206 var swapEnabled = new Method({ 6207 name: 'swapEnabled', 6208 call: 'bzz_swapEnabled', 6209 params: 1, 6210 inputFormatter: [null] 6211 }); 6212 6213 var download = new Method({ 6214 name: 'download', 6215 call: 'bzz_download', 6216 params: 2, 6217 inputFormatter: [null, null] 6218 }); 6219 6220 var upload = new Method({ 6221 name: 'upload', 6222 call: 'bzz_upload', 6223 params: 2, 6224 inputFormatter: [null, null] 6225 }); 6226 6227 var retrieve = new Method({ 6228 name: 'retrieve', 6229 call: 'bzz_retrieve', 6230 params: 1, 6231 inputFormatter: [null] 6232 }); 6233 6234 var store = new Method({ 6235 name: 'store', 6236 call: 'bzz_store', 6237 params: 2, 6238 inputFormatter: [null, null] 6239 }); 6240 6241 var get = new Method({ 6242 name: 'get', 6243 call: 'bzz_get', 6244 params: 1, 6245 inputFormatter: [null] 6246 }); 6247 6248 var put = new Method({ 6249 name: 'put', 6250 call: 'bzz_put', 6251 params: 2, 6252 inputFormatter: [null, null] 6253 }); 6254 6255 var modify = new Method({ 6256 name: 'modify', 6257 call: 'bzz_modify', 6258 params: 4, 6259 inputFormatter: [null, null, null, null] 6260 }); 6261 6262 return [ 6263 blockNetworkRead, 6264 syncEnabled, 6265 swapEnabled, 6266 download, 6267 upload, 6268 retrieve, 6269 store, 6270 get, 6271 put, 6272 modify 6273 ]; 6274 }; 6275 6276 var properties = function () { 6277 return [ 6278 new Property({ 6279 name: 'hive', 6280 getter: 'bzz_hive' 6281 }), 6282 new Property({ 6283 name: 'info', 6284 getter: 'bzz_info' 6285 }) 6286 ]; 6287 }; 6288 6289 6290 module.exports = Swarm; 6291 6292 },{"../method":37,"../property":45}],43:[function(require,module,exports){ 6293 /* 6294 This file is part of vnt.js. 6295 6296 vnt.js is free software: you can redistribute it and/or modify 6297 it under the terms of the GNU Lesser General Public License as published by 6298 the Free Software Foundation, either version 3 of the License, or 6299 (at your option) any later version. 6300 6301 vnt.js is distributed in the hope that it will be useful, 6302 but WITHOUT ANY WARRANTY; without even the implied warranty of 6303 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 6304 GNU Lesser General Public License for more details. 6305 6306 You should have received a copy of the GNU Lesser General Public License 6307 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 6308 */ 6309 /** @file watches.js 6310 * @authors: 6311 * Marek Kotewicz <marek@ethdev.com> 6312 * @date 2015 6313 */ 6314 6315 var Method = require('../method'); 6316 6317 /// @returns an array of objects describing vnt.core.filter api methods 6318 var vnt = function () { 6319 var newFilterCall = function (args) { 6320 var type = args[0]; 6321 6322 switch(type) { 6323 case 'latest': 6324 args.shift(); 6325 this.params = 0; 6326 return 'core_newBlockFilter'; 6327 case 'pending': 6328 args.shift(); 6329 this.params = 0; 6330 return 'core_newPendingTransactionFilter'; 6331 default: 6332 return 'core_newFilter'; 6333 } 6334 }; 6335 6336 var newFilter = new Method({ 6337 name: 'newFilter', 6338 call: newFilterCall, 6339 params: 1 6340 }); 6341 6342 var uninstallFilter = new Method({ 6343 name: 'uninstallFilter', 6344 call: 'core_uninstallFilter', 6345 params: 1 6346 }); 6347 6348 var getLogs = new Method({ 6349 name: 'getLogs', 6350 call: 'core_getFilterLogs', 6351 params: 1 6352 }); 6353 6354 var poll = new Method({ 6355 name: 'poll', 6356 call: 'core_getFilterChanges', 6357 params: 1 6358 }); 6359 6360 return [ 6361 newFilter, 6362 uninstallFilter, 6363 getLogs, 6364 poll 6365 ]; 6366 }; 6367 6368 /// @returns an array of objects describing vnt.shh.watch api methods 6369 var shh = function () { 6370 6371 return [ 6372 new Method({ 6373 name: 'newFilter', 6374 call: 'shh_newMessageFilter', 6375 params: 1 6376 }), 6377 new Method({ 6378 name: 'uninstallFilter', 6379 call: 'shh_deleteMessageFilter', 6380 params: 1 6381 }), 6382 new Method({ 6383 name: 'getLogs', 6384 call: 'shh_getFilterMessages', 6385 params: 1 6386 }), 6387 new Method({ 6388 name: 'poll', 6389 call: 'shh_getFilterMessages', 6390 params: 1 6391 }) 6392 ]; 6393 }; 6394 6395 module.exports = { 6396 vnt: vnt, 6397 shh: shh 6398 }; 6399 6400 },{"../method":37}],44:[function(require,module,exports){ 6401 /* 6402 This file is part of vnt.js. 6403 6404 vnt.js is free software: you can redistribute it and/or modify 6405 it under the terms of the GNU Lesser General Public License as published by 6406 the Free Software Foundation, either version 3 of the License, or 6407 (at your option) any later version. 6408 6409 vnt.js is distributed in the hope that it will be useful, 6410 but WITHOUT ANY WARRANTY; without even the implied warranty of 6411 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 6412 GNU Lesser General Public License for more details. 6413 6414 You should have received a copy of the GNU Lesser General Public License 6415 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 6416 */ 6417 /** 6418 * @file namereg.js 6419 * @author Marek Kotewicz <marek@ethdev.com> 6420 * @date 2015 6421 */ 6422 6423 var globalRegistrarAbi = require('../contracts/GlobalRegistrar.json'); 6424 var icapRegistrarAbi= require('../contracts/ICAPRegistrar.json'); 6425 6426 var globalNameregAddress = '0xc6d9d2cd449a754c494264e1809c50e34d64562b'; 6427 var icapNameregAddress = '0xa1a111bc074c9cfa781f0c38e63bd51c91b8af00'; 6428 6429 module.exports = { 6430 global: { 6431 abi: globalRegistrarAbi, 6432 address: globalNameregAddress 6433 }, 6434 icap: { 6435 abi: icapRegistrarAbi, 6436 address: icapNameregAddress 6437 } 6438 }; 6439 6440 6441 },{"../contracts/GlobalRegistrar.json":1,"../contracts/ICAPRegistrar.json":2}],45:[function(require,module,exports){ 6442 /* 6443 This file is part of vnt.js. 6444 6445 vnt.js is free software: you can redistribute it and/or modify 6446 it under the terms of the GNU Lesser General Public License as published by 6447 the Free Software Foundation, either version 3 of the License, or 6448 (at your option) any later version. 6449 6450 vnt.js is distributed in the hope that it will be useful, 6451 but WITHOUT ANY WARRANTY; without even the implied warranty of 6452 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 6453 GNU Lesser General Public License for more details. 6454 6455 You should have received a copy of the GNU Lesser General Public License 6456 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 6457 */ 6458 /** 6459 * @file property.js 6460 * @author Fabian Vogelsteller <fabian@frozeman.de> 6461 * @author Marek Kotewicz <marek@ethdev.com> 6462 * @date 2015 6463 */ 6464 6465 var utils = require('../utils/utils'); 6466 6467 var Property = function (options) { 6468 this.name = options.name; 6469 this.getter = options.getter; 6470 this.setter = options.setter; 6471 this.outputFormatter = options.outputFormatter; 6472 this.inputFormatter = options.inputFormatter; 6473 this.requestManager = null; 6474 }; 6475 6476 Property.prototype.setRequestManager = function (rm) { 6477 this.requestManager = rm; 6478 }; 6479 6480 /** 6481 * Should be called to format input args of method 6482 * 6483 * @method formatInput 6484 * @param {Array} 6485 * @return {Array} 6486 */ 6487 Property.prototype.formatInput = function (arg) { 6488 return this.inputFormatter ? this.inputFormatter(arg) : arg; 6489 }; 6490 6491 /** 6492 * Should be called to format output(result) of method 6493 * 6494 * @method formatOutput 6495 * @param {Object} 6496 * @return {Object} 6497 */ 6498 Property.prototype.formatOutput = function (result) { 6499 return this.outputFormatter && result !== null && result !== undefined ? this.outputFormatter(result) : result; 6500 }; 6501 6502 /** 6503 * Should be used to extract callback from array of arguments. Modifies input param 6504 * 6505 * @method extractCallback 6506 * @param {Array} arguments 6507 * @return {Function|Null} callback, if exists 6508 */ 6509 Property.prototype.extractCallback = function (args) { 6510 if (utils.isFunction(args[args.length - 1])) { 6511 return args.pop(); // modify the args array! 6512 } 6513 }; 6514 6515 6516 /** 6517 * Should attach function to method 6518 * 6519 * @method attachToObject 6520 * @param {Object} 6521 * @param {Function} 6522 */ 6523 Property.prototype.attachToObject = function (obj) { 6524 var proto = { 6525 get: this.buildGet(), 6526 enumerable: true 6527 }; 6528 6529 var names = this.name.split('.'); 6530 var name = names[0]; 6531 if (names.length > 1) { 6532 obj[names[0]] = obj[names[0]] || {}; 6533 obj = obj[names[0]]; 6534 name = names[1]; 6535 } 6536 6537 Object.defineProperty(obj, name, proto); 6538 obj[asyncGetterName(name)] = this.buildAsyncGet(); 6539 }; 6540 6541 var asyncGetterName = function (name) { 6542 return 'get' + name.charAt(0).toUpperCase() + name.slice(1); 6543 }; 6544 6545 Property.prototype.buildGet = function () { 6546 var property = this; 6547 return function get() { 6548 return property.formatOutput(property.requestManager.send({ 6549 method: property.getter 6550 })); 6551 }; 6552 }; 6553 6554 Property.prototype.buildAsyncGet = function () { 6555 var property = this; 6556 var get = function (callback) { 6557 property.requestManager.sendAsync({ 6558 method: property.getter 6559 }, function (err, result) { 6560 callback(err, property.formatOutput(result)); 6561 }); 6562 }; 6563 get.request = this.request.bind(this); 6564 return get; 6565 }; 6566 6567 /** 6568 * Should be called to create pure JSONRPC request which can be used in batch request 6569 * 6570 * @method request 6571 * @param {...} params 6572 * @return {Object} jsonrpc request 6573 */ 6574 Property.prototype.request = function () { 6575 var payload = { 6576 method: this.getter, 6577 params: [], 6578 callback: this.extractCallback(Array.prototype.slice.call(arguments)) 6579 }; 6580 payload.format = this.formatOutput.bind(this); 6581 return payload; 6582 }; 6583 6584 module.exports = Property; 6585 6586 6587 },{"../utils/utils":21}],46:[function(require,module,exports){ 6588 /* 6589 This file is part of vnt.js. 6590 6591 vnt.js is free software: you can redistribute it and/or modify 6592 it under the terms of the GNU Lesser General Public License as published by 6593 the Free Software Foundation, either version 3 of the License, or 6594 (at your option) any later version. 6595 6596 vnt.js is distributed in the hope that it will be useful, 6597 but WITHOUT ANY WARRANTY; without even the implied warranty of 6598 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 6599 GNU Lesser General Public License for more details. 6600 6601 You should have received a copy of the GNU Lesser General Public License 6602 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 6603 */ 6604 /** 6605 * @file requestmanager.js 6606 * @author Jeffrey Wilcke <jeff@ethdev.com> 6607 * @author Marek Kotewicz <marek@ethdev.com> 6608 * @author Marian Oancea <marian@ethdev.com> 6609 * @author Fabian Vogelsteller <fabian@ethdev.com> 6610 * @author Gav Wood <g@ethdev.com> 6611 * @date 2014 6612 */ 6613 6614 var Jsonrpc = require('./jsonrpc'); 6615 var utils = require('../utils/utils'); 6616 var c = require('../utils/config'); 6617 var errors = require('./errors'); 6618 6619 /** 6620 * It's responsible for passing messages to providers 6621 * It's also responsible for polling the vntchain node for incoming messages 6622 * Default poll timeout is 1 second 6623 * Singleton 6624 */ 6625 var RequestManager = function (provider) { 6626 this.provider = provider; 6627 this.polls = {}; 6628 this.timeout = null; 6629 }; 6630 6631 /** 6632 * Should be used to synchronously send request 6633 * 6634 * @method send 6635 * @param {Object} data 6636 * @return {Object} 6637 */ 6638 RequestManager.prototype.send = function (data) { 6639 if (!this.provider) { 6640 console.error(errors.InvalidProvider()); 6641 return null; 6642 } 6643 6644 var payload = Jsonrpc.toPayload(data.method, data.params); 6645 var result = this.provider.send(payload); 6646 6647 if (!Jsonrpc.isValidResponse(result)) { 6648 throw errors.InvalidResponse(result); 6649 } 6650 6651 return result.result; 6652 }; 6653 6654 /** 6655 * Should be used to asynchronously send request 6656 * 6657 * @method sendAsync 6658 * @param {Object} data 6659 * @param {Function} callback 6660 */ 6661 RequestManager.prototype.sendAsync = function (data, callback) { 6662 if (!this.provider) { 6663 return callback(errors.InvalidProvider()); 6664 } 6665 6666 var payload = Jsonrpc.toPayload(data.method, data.params); 6667 this.provider.sendAsync(payload, function (err, result) { 6668 if (err) { 6669 return callback(err); 6670 } 6671 6672 if (!Jsonrpc.isValidResponse(result)) { 6673 return callback(errors.InvalidResponse(result)); 6674 } 6675 6676 callback(null, result.result); 6677 }); 6678 }; 6679 6680 /** 6681 * Should be called to asynchronously send batch request 6682 * 6683 * @method sendBatch 6684 * @param {Array} batch data 6685 * @param {Function} callback 6686 */ 6687 RequestManager.prototype.sendBatch = function (data, callback) { 6688 if (!this.provider) { 6689 return callback(errors.InvalidProvider()); 6690 } 6691 6692 var payload = Jsonrpc.toBatchPayload(data); 6693 6694 this.provider.sendAsync(payload, function (err, results) { 6695 if (err) { 6696 return callback(err); 6697 } 6698 6699 if (!utils.isArray(results)) { 6700 return callback(errors.InvalidResponse(results)); 6701 } 6702 6703 callback(err, results); 6704 }); 6705 }; 6706 6707 /** 6708 * Should be used to set provider of request manager 6709 * 6710 * @method setProvider 6711 * @param {Object} 6712 */ 6713 RequestManager.prototype.setProvider = function (p) { 6714 this.provider = p; 6715 }; 6716 6717 /** 6718 * Should be used to start polling 6719 * 6720 * @method startPolling 6721 * @param {Object} data 6722 * @param {Number} pollId 6723 * @param {Function} callback 6724 * @param {Function} uninstall 6725 * 6726 * @todo cleanup number of params 6727 */ 6728 RequestManager.prototype.startPolling = function (data, pollId, callback, uninstall) { 6729 this.polls[pollId] = {data: data, id: pollId, callback: callback, uninstall: uninstall}; 6730 6731 6732 // start polling 6733 if (!this.timeout) { 6734 this.poll(); 6735 } 6736 }; 6737 6738 /** 6739 * Should be used to stop polling for filter with given id 6740 * 6741 * @method stopPolling 6742 * @param {Number} pollId 6743 */ 6744 RequestManager.prototype.stopPolling = function (pollId) { 6745 delete this.polls[pollId]; 6746 6747 // stop polling 6748 if(Object.keys(this.polls).length === 0 && this.timeout) { 6749 clearTimeout(this.timeout); 6750 this.timeout = null; 6751 } 6752 }; 6753 6754 /** 6755 * Should be called to reset the polling mechanism of the request manager 6756 * 6757 * @method reset 6758 */ 6759 RequestManager.prototype.reset = function (keepIsSyncing) { 6760 /*jshint maxcomplexity:5 */ 6761 6762 for (var key in this.polls) { 6763 // remove all polls, except sync polls, 6764 // they need to be removed manually by calling syncing.stopWatching() 6765 if(!keepIsSyncing || key.indexOf('syncPoll_') === -1) { 6766 this.polls[key].uninstall(); 6767 delete this.polls[key]; 6768 } 6769 } 6770 6771 // stop polling 6772 if(Object.keys(this.polls).length === 0 && this.timeout) { 6773 clearTimeout(this.timeout); 6774 this.timeout = null; 6775 } 6776 }; 6777 6778 /** 6779 * Should be called to poll for changes on filter with given id 6780 * 6781 * @method poll 6782 */ 6783 RequestManager.prototype.poll = function () { 6784 /*jshint maxcomplexity: 6 */ 6785 this.timeout = setTimeout(this.poll.bind(this), c.VNT_POLLING_TIMEOUT); 6786 6787 if (Object.keys(this.polls).length === 0) { 6788 return; 6789 } 6790 6791 if (!this.provider) { 6792 console.error(errors.InvalidProvider()); 6793 return; 6794 } 6795 6796 var pollsData = []; 6797 var pollsIds = []; 6798 for (var key in this.polls) { 6799 pollsData.push(this.polls[key].data); 6800 pollsIds.push(key); 6801 } 6802 6803 if (pollsData.length === 0) { 6804 return; 6805 } 6806 6807 var payload = Jsonrpc.toBatchPayload(pollsData); 6808 6809 // map the request id to they poll id 6810 var pollsIdMap = {}; 6811 payload.forEach(function(load, index){ 6812 pollsIdMap[load.id] = pollsIds[index]; 6813 }); 6814 6815 6816 var self = this; 6817 this.provider.sendAsync(payload, function (error, results) { 6818 6819 6820 // TODO: console log? 6821 if (error) { 6822 return; 6823 } 6824 6825 if (!utils.isArray(results)) { 6826 throw errors.InvalidResponse(results); 6827 } 6828 results.map(function (result) { 6829 var id = pollsIdMap[result.id]; 6830 6831 // make sure the filter is still installed after arrival of the request 6832 if (self.polls[id]) { 6833 result.callback = self.polls[id].callback; 6834 return result; 6835 } else 6836 return false; 6837 }).filter(function (result) { 6838 return !!result; 6839 }).filter(function (result) { 6840 var valid = Jsonrpc.isValidResponse(result); 6841 if (!valid) { 6842 result.callback(errors.InvalidResponse(result)); 6843 } 6844 return valid; 6845 }).forEach(function (result) { 6846 result.callback(null, result.result); 6847 }); 6848 }); 6849 }; 6850 6851 module.exports = RequestManager; 6852 6853 },{"../utils/config":18,"../utils/utils":21,"./errors":27,"./jsonrpc":36}],47:[function(require,module,exports){ 6854 6855 6856 var Settings = function () { 6857 this.defaultBlock = 'latest'; 6858 this.defaultAccount = undefined; 6859 }; 6860 6861 module.exports = Settings; 6862 6863 6864 },{}],48:[function(require,module,exports){ 6865 /* 6866 This file is part of vnt.js. 6867 6868 vnt.js is free software: you can redistribute it and/or modify 6869 it under the terms of the GNU Lesser General Public License as published by 6870 the Free Software Foundation, either version 3 of the License, or 6871 (at your option) any later version. 6872 6873 vnt.js is distributed in the hope that it will be useful, 6874 but WITHOUT ANY WARRANTY; without even the implied warranty of 6875 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 6876 GNU Lesser General Public License for more details. 6877 6878 You should have received a copy of the GNU Lesser General Public License 6879 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 6880 */ 6881 /** @file syncing.js 6882 * @authors: 6883 * Fabian Vogelsteller <fabian@ethdev.com> 6884 * @date 2015 6885 */ 6886 6887 var formatters = require('./formatters'); 6888 var utils = require('../utils/utils'); 6889 6890 var count = 1; 6891 6892 /** 6893 Adds the callback and sets up the methods, to iterate over the results. 6894 6895 @method pollSyncing 6896 @param {Object} self 6897 */ 6898 var pollSyncing = function(self) { 6899 6900 var onMessage = function (error, sync) { 6901 if (error) { 6902 return self.callbacks.forEach(function (callback) { 6903 callback(error); 6904 }); 6905 } 6906 6907 if(utils.isObject(sync) && sync.startingBlock) 6908 sync = formatters.outputSyncingFormatter(sync); 6909 6910 self.callbacks.forEach(function (callback) { 6911 if (self.lastSyncState !== sync) { 6912 6913 // call the callback with true first so the app can stop anything, before receiving the sync data 6914 if(!self.lastSyncState && utils.isObject(sync)) 6915 callback(null, true); 6916 6917 // call on the next CPU cycle, so the actions of the sync stop can be processes first 6918 setTimeout(function() { 6919 callback(null, sync); 6920 }, 0); 6921 6922 self.lastSyncState = sync; 6923 } 6924 }); 6925 }; 6926 6927 self.requestManager.startPolling({ 6928 method: 'core_syncing', 6929 params: [], 6930 }, self.pollId, onMessage, self.stopWatching.bind(self)); 6931 6932 }; 6933 6934 var IsSyncing = function (requestManager, callback) { 6935 this.requestManager = requestManager; 6936 this.pollId = 'syncPoll_'+ count++; 6937 this.callbacks = []; 6938 this.addCallback(callback); 6939 this.lastSyncState = false; 6940 pollSyncing(this); 6941 6942 return this; 6943 }; 6944 6945 IsSyncing.prototype.addCallback = function (callback) { 6946 if(callback) 6947 this.callbacks.push(callback); 6948 return this; 6949 }; 6950 6951 IsSyncing.prototype.stopWatching = function () { 6952 this.requestManager.stopPolling(this.pollId); 6953 this.callbacks = []; 6954 }; 6955 6956 module.exports = IsSyncing; 6957 6958 },{"../utils/utils":21,"./formatters":31}],49:[function(require,module,exports){ 6959 /* 6960 This file is part of vnt.js. 6961 6962 vnt.js is free software: you can redistribute it and/or modify 6963 it under the terms of the GNU Lesser General Public License as published by 6964 the Free Software Foundation, either version 3 of the License, or 6965 (at your option) any later version. 6966 6967 vnt.js is distributed in the hope that it will be useful, 6968 but WITHOUT ANY WARRANTY; without even the implied warranty of 6969 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 6970 GNU Lesser General Public License for more details. 6971 6972 You should have received a copy of the GNU Lesser General Public License 6973 along with vnt.js. If not, see <http://www.gnu.org/licenses/>. 6974 */ 6975 /** 6976 * @file transfer.js 6977 * @author Marek Kotewicz <marek@ethdev.com> 6978 * @date 2015 6979 */ 6980 6981 var Iban = require('./iban'); 6982 var exchangeAbi = require('../contracts/SmartExchange.json'); 6983 6984 /** 6985 * Should be used to make Iban transfer 6986 * 6987 * @method transfer 6988 * @param {String} from 6989 * @param {String} to iban 6990 * @param {Value} value to be tranfered 6991 * @param {Function} callback, callback 6992 */ 6993 var transfer = function (vnt, from, to, value, callback) { 6994 var iban = new Iban(to); 6995 if (!iban.isValid()) { 6996 throw new Error('invalid iban address'); 6997 } 6998 6999 if (iban.isDirect()) { 7000 return transferToAddress(vnt, from, iban.address(), value, callback); 7001 } 7002 7003 if (!callback) { 7004 var address = vnt.icapNamereg().addr(iban.institution()); 7005 return deposit(vnt, from, address, value, iban.client()); 7006 } 7007 7008 vnt.icapNamereg().addr(iban.institution(), function (err, address) { 7009 return deposit(vnt, from, address, value, iban.client(), callback); 7010 }); 7011 7012 }; 7013 7014 /** 7015 * Should be used to transfer funds to certain address 7016 * 7017 * @method transferToAddress 7018 * @param {String} from 7019 * @param {String} to 7020 * @param {Value} value to be tranfered 7021 * @param {Function} callback, callback 7022 */ 7023 var transferToAddress = function (vnt, from, to, value, callback) { 7024 return vnt.sendTransaction({ 7025 to: to, 7026 from: from, 7027 value: value 7028 }, callback); 7029 }; 7030 7031 /** 7032 * Should be used to deposit funds to generic Exchange contract (must implement deposit(bytes32) method!) 7033 * 7034 * @method deposit 7035 * @param {String} from 7036 * @param {String} to 7037 * @param {Value} value to be transfered 7038 * @param {String} client unique identifier 7039 * @param {Function} callback, callback 7040 */ 7041 var deposit = function (vnt, from, to, value, client, callback) { 7042 var abi = exchangeAbi; 7043 return vnt.contract(abi).at(to).deposit(client, { 7044 from: from, 7045 value: value 7046 }, callback); 7047 }; 7048 7049 module.exports = transfer; 7050 7051 },{"../contracts/SmartExchange.json":3,"./iban":34}],50:[function(require,module,exports){ 7052 7053 },{}],51:[function(require,module,exports){ 7054 ;(function (root, factory, undef) { 7055 if (typeof exports === "object") { 7056 // CommonJS 7057 module.exports = exports = factory(require("./core"), require("./enc-base64"), require("./md5"), require("./evpkdf"), require("./cipher-core")); 7058 } 7059 else if (typeof define === "function" && define.amd) { 7060 // AMD 7061 define(["./core", "./enc-base64", "./md5", "./evpkdf", "./cipher-core"], factory); 7062 } 7063 else { 7064 // Global (browser) 7065 factory(root.CryptoJS); 7066 } 7067 }(this, function (CryptoJS) { 7068 7069 (function () { 7070 // Shortcuts 7071 var C = CryptoJS; 7072 var C_lib = C.lib; 7073 var BlockCipher = C_lib.BlockCipher; 7074 var C_algo = C.algo; 7075 7076 // Lookup tables 7077 var SBOX = []; 7078 var INV_SBOX = []; 7079 var SUB_MIX_0 = []; 7080 var SUB_MIX_1 = []; 7081 var SUB_MIX_2 = []; 7082 var SUB_MIX_3 = []; 7083 var INV_SUB_MIX_0 = []; 7084 var INV_SUB_MIX_1 = []; 7085 var INV_SUB_MIX_2 = []; 7086 var INV_SUB_MIX_3 = []; 7087 7088 // Compute lookup tables 7089 (function () { 7090 // Compute double table 7091 var d = []; 7092 for (var i = 0; i < 256; i++) { 7093 if (i < 128) { 7094 d[i] = i << 1; 7095 } else { 7096 d[i] = (i << 1) ^ 0x11b; 7097 } 7098 } 7099 7100 // Walk GF(2^8) 7101 var x = 0; 7102 var xi = 0; 7103 for (var i = 0; i < 256; i++) { 7104 // Compute sbox 7105 var sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4); 7106 sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63; 7107 SBOX[x] = sx; 7108 INV_SBOX[sx] = x; 7109 7110 // Compute multiplication 7111 var x2 = d[x]; 7112 var x4 = d[x2]; 7113 var x8 = d[x4]; 7114 7115 // Compute sub bytes, mix columns tables 7116 var t = (d[sx] * 0x101) ^ (sx * 0x1010100); 7117 SUB_MIX_0[x] = (t << 24) | (t >>> 8); 7118 SUB_MIX_1[x] = (t << 16) | (t >>> 16); 7119 SUB_MIX_2[x] = (t << 8) | (t >>> 24); 7120 SUB_MIX_3[x] = t; 7121 7122 // Compute inv sub bytes, inv mix columns tables 7123 var t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100); 7124 INV_SUB_MIX_0[sx] = (t << 24) | (t >>> 8); 7125 INV_SUB_MIX_1[sx] = (t << 16) | (t >>> 16); 7126 INV_SUB_MIX_2[sx] = (t << 8) | (t >>> 24); 7127 INV_SUB_MIX_3[sx] = t; 7128 7129 // Compute next counter 7130 if (!x) { 7131 x = xi = 1; 7132 } else { 7133 x = x2 ^ d[d[d[x8 ^ x2]]]; 7134 xi ^= d[d[xi]]; 7135 } 7136 } 7137 }()); 7138 7139 // Precomputed Rcon lookup 7140 var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36]; 7141 7142 /** 7143 * AES block cipher algorithm. 7144 */ 7145 var AES = C_algo.AES = BlockCipher.extend({ 7146 _doReset: function () { 7147 // Shortcuts 7148 var key = this._key; 7149 var keyWords = key.words; 7150 var keySize = key.sigBytes / 4; 7151 7152 // Compute number of rounds 7153 var nRounds = this._nRounds = keySize + 6 7154 7155 // Compute number of key schedule rows 7156 var ksRows = (nRounds + 1) * 4; 7157 7158 // Compute key schedule 7159 var keySchedule = this._keySchedule = []; 7160 for (var ksRow = 0; ksRow < ksRows; ksRow++) { 7161 if (ksRow < keySize) { 7162 keySchedule[ksRow] = keyWords[ksRow]; 7163 } else { 7164 var t = keySchedule[ksRow - 1]; 7165 7166 if (!(ksRow % keySize)) { 7167 // Rot word 7168 t = (t << 8) | (t >>> 24); 7169 7170 // Sub word 7171 t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff]; 7172 7173 // Mix Rcon 7174 t ^= RCON[(ksRow / keySize) | 0] << 24; 7175 } else if (keySize > 6 && ksRow % keySize == 4) { 7176 // Sub word 7177 t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff]; 7178 } 7179 7180 keySchedule[ksRow] = keySchedule[ksRow - keySize] ^ t; 7181 } 7182 } 7183 7184 // Compute inv key schedule 7185 var invKeySchedule = this._invKeySchedule = []; 7186 for (var invKsRow = 0; invKsRow < ksRows; invKsRow++) { 7187 var ksRow = ksRows - invKsRow; 7188 7189 if (invKsRow % 4) { 7190 var t = keySchedule[ksRow]; 7191 } else { 7192 var t = keySchedule[ksRow - 4]; 7193 } 7194 7195 if (invKsRow < 4 || ksRow <= 4) { 7196 invKeySchedule[invKsRow] = t; 7197 } else { 7198 invKeySchedule[invKsRow] = INV_SUB_MIX_0[SBOX[t >>> 24]] ^ INV_SUB_MIX_1[SBOX[(t >>> 16) & 0xff]] ^ 7199 INV_SUB_MIX_2[SBOX[(t >>> 8) & 0xff]] ^ INV_SUB_MIX_3[SBOX[t & 0xff]]; 7200 } 7201 } 7202 }, 7203 7204 encryptBlock: function (M, offset) { 7205 this._doCryptBlock(M, offset, this._keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX); 7206 }, 7207 7208 decryptBlock: function (M, offset) { 7209 // Swap 2nd and 4th rows 7210 var t = M[offset + 1]; 7211 M[offset + 1] = M[offset + 3]; 7212 M[offset + 3] = t; 7213 7214 this._doCryptBlock(M, offset, this._invKeySchedule, INV_SUB_MIX_0, INV_SUB_MIX_1, INV_SUB_MIX_2, INV_SUB_MIX_3, INV_SBOX); 7215 7216 // Inv swap 2nd and 4th rows 7217 var t = M[offset + 1]; 7218 M[offset + 1] = M[offset + 3]; 7219 M[offset + 3] = t; 7220 }, 7221 7222 _doCryptBlock: function (M, offset, keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX) { 7223 // Shortcut 7224 var nRounds = this._nRounds; 7225 7226 // Get input, add round key 7227 var s0 = M[offset] ^ keySchedule[0]; 7228 var s1 = M[offset + 1] ^ keySchedule[1]; 7229 var s2 = M[offset + 2] ^ keySchedule[2]; 7230 var s3 = M[offset + 3] ^ keySchedule[3]; 7231 7232 // Key schedule row counter 7233 var ksRow = 4; 7234 7235 // Rounds 7236 for (var round = 1; round < nRounds; round++) { 7237 // Shift rows, sub bytes, mix columns, add round key 7238 var t0 = SUB_MIX_0[s0 >>> 24] ^ SUB_MIX_1[(s1 >>> 16) & 0xff] ^ SUB_MIX_2[(s2 >>> 8) & 0xff] ^ SUB_MIX_3[s3 & 0xff] ^ keySchedule[ksRow++]; 7239 var t1 = SUB_MIX_0[s1 >>> 24] ^ SUB_MIX_1[(s2 >>> 16) & 0xff] ^ SUB_MIX_2[(s3 >>> 8) & 0xff] ^ SUB_MIX_3[s0 & 0xff] ^ keySchedule[ksRow++]; 7240 var t2 = SUB_MIX_0[s2 >>> 24] ^ SUB_MIX_1[(s3 >>> 16) & 0xff] ^ SUB_MIX_2[(s0 >>> 8) & 0xff] ^ SUB_MIX_3[s1 & 0xff] ^ keySchedule[ksRow++]; 7241 var t3 = SUB_MIX_0[s3 >>> 24] ^ SUB_MIX_1[(s0 >>> 16) & 0xff] ^ SUB_MIX_2[(s1 >>> 8) & 0xff] ^ SUB_MIX_3[s2 & 0xff] ^ keySchedule[ksRow++]; 7242 7243 // Update state 7244 s0 = t0; 7245 s1 = t1; 7246 s2 = t2; 7247 s3 = t3; 7248 } 7249 7250 // Shift rows, sub bytes, add round key 7251 var t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++]; 7252 var t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++]; 7253 var t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++]; 7254 var t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++]; 7255 7256 // Set output 7257 M[offset] = t0; 7258 M[offset + 1] = t1; 7259 M[offset + 2] = t2; 7260 M[offset + 3] = t3; 7261 }, 7262 7263 keySize: 256/32 7264 }); 7265 7266 /** 7267 * Shortcut functions to the cipher's object interface. 7268 * 7269 * @example 7270 * 7271 * var ciphertext = CryptoJS.AES.encrypt(message, key, cfg); 7272 * var plaintext = CryptoJS.AES.decrypt(ciphertext, key, cfg); 7273 */ 7274 C.AES = BlockCipher._createHelper(AES); 7275 }()); 7276 7277 7278 return CryptoJS.AES; 7279 7280 })); 7281 },{"./cipher-core":52,"./core":53,"./enc-base64":54,"./evpkdf":56,"./md5":61}],52:[function(require,module,exports){ 7282 ;(function (root, factory) { 7283 if (typeof exports === "object") { 7284 // CommonJS 7285 module.exports = exports = factory(require("./core")); 7286 } 7287 else if (typeof define === "function" && define.amd) { 7288 // AMD 7289 define(["./core"], factory); 7290 } 7291 else { 7292 // Global (browser) 7293 factory(root.CryptoJS); 7294 } 7295 }(this, function (CryptoJS) { 7296 7297 /** 7298 * Cipher core components. 7299 */ 7300 CryptoJS.lib.Cipher || (function (undefined) { 7301 // Shortcuts 7302 var C = CryptoJS; 7303 var C_lib = C.lib; 7304 var Base = C_lib.Base; 7305 var WordArray = C_lib.WordArray; 7306 var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm; 7307 var C_enc = C.enc; 7308 var Utf8 = C_enc.Utf8; 7309 var Base64 = C_enc.Base64; 7310 var C_algo = C.algo; 7311 var EvpKDF = C_algo.EvpKDF; 7312 7313 /** 7314 * Abstract base cipher template. 7315 * 7316 * @property {number} keySize This cipher's key size. Default: 4 (128 bits) 7317 * @property {number} ivSize This cipher's IV size. Default: 4 (128 bits) 7318 * @property {number} _ENC_XFORM_MODE A constant representing encryption mode. 7319 * @property {number} _DEC_XFORM_MODE A constant representing decryption mode. 7320 */ 7321 var Cipher = C_lib.Cipher = BufferedBlockAlgorithm.extend({ 7322 /** 7323 * Configuration options. 7324 * 7325 * @property {WordArray} iv The IV to use for this operation. 7326 */ 7327 cfg: Base.extend(), 7328 7329 /** 7330 * Creates this cipher in encryption mode. 7331 * 7332 * @param {WordArray} key The key. 7333 * @param {Object} cfg (Optional) The configuration options to use for this operation. 7334 * 7335 * @return {Cipher} A cipher instance. 7336 * 7337 * @static 7338 * 7339 * @example 7340 * 7341 * var cipher = CryptoJS.algo.AES.createEncryptor(keyWordArray, { iv: ivWordArray }); 7342 */ 7343 createEncryptor: function (key, cfg) { 7344 return this.create(this._ENC_XFORM_MODE, key, cfg); 7345 }, 7346 7347 /** 7348 * Creates this cipher in decryption mode. 7349 * 7350 * @param {WordArray} key The key. 7351 * @param {Object} cfg (Optional) The configuration options to use for this operation. 7352 * 7353 * @return {Cipher} A cipher instance. 7354 * 7355 * @static 7356 * 7357 * @example 7358 * 7359 * var cipher = CryptoJS.algo.AES.createDecryptor(keyWordArray, { iv: ivWordArray }); 7360 */ 7361 createDecryptor: function (key, cfg) { 7362 return this.create(this._DEC_XFORM_MODE, key, cfg); 7363 }, 7364 7365 /** 7366 * Initializes a newly created cipher. 7367 * 7368 * @param {number} xformMode Either the encryption or decryption transormation mode constant. 7369 * @param {WordArray} key The key. 7370 * @param {Object} cfg (Optional) The configuration options to use for this operation. 7371 * 7372 * @example 7373 * 7374 * var cipher = CryptoJS.algo.AES.create(CryptoJS.algo.AES._ENC_XFORM_MODE, keyWordArray, { iv: ivWordArray }); 7375 */ 7376 init: function (xformMode, key, cfg) { 7377 // Apply config defaults 7378 this.cfg = this.cfg.extend(cfg); 7379 7380 // Store transform mode and key 7381 this._xformMode = xformMode; 7382 this._key = key; 7383 7384 // Set initial values 7385 this.reset(); 7386 }, 7387 7388 /** 7389 * Resets this cipher to its initial state. 7390 * 7391 * @example 7392 * 7393 * cipher.reset(); 7394 */ 7395 reset: function () { 7396 // Reset data buffer 7397 BufferedBlockAlgorithm.reset.call(this); 7398 7399 // Perform concrete-cipher logic 7400 this._doReset(); 7401 }, 7402 7403 /** 7404 * Adds data to be encrypted or decrypted. 7405 * 7406 * @param {WordArray|string} dataUpdate The data to encrypt or decrypt. 7407 * 7408 * @return {WordArray} The data after processing. 7409 * 7410 * @example 7411 * 7412 * var encrypted = cipher.process('data'); 7413 * var encrypted = cipher.process(wordArray); 7414 */ 7415 process: function (dataUpdate) { 7416 // Append 7417 this._append(dataUpdate); 7418 7419 // Process available blocks 7420 return this._process(); 7421 }, 7422 7423 /** 7424 * Finalizes the encryption or decryption process. 7425 * Note that the finalize operation is effectively a destructive, read-once operation. 7426 * 7427 * @param {WordArray|string} dataUpdate The final data to encrypt or decrypt. 7428 * 7429 * @return {WordArray} The data after final processing. 7430 * 7431 * @example 7432 * 7433 * var encrypted = cipher.finalize(); 7434 * var encrypted = cipher.finalize('data'); 7435 * var encrypted = cipher.finalize(wordArray); 7436 */ 7437 finalize: function (dataUpdate) { 7438 // Final data update 7439 if (dataUpdate) { 7440 this._append(dataUpdate); 7441 } 7442 7443 // Perform concrete-cipher logic 7444 var finalProcessedData = this._doFinalize(); 7445 7446 return finalProcessedData; 7447 }, 7448 7449 keySize: 128/32, 7450 7451 ivSize: 128/32, 7452 7453 _ENC_XFORM_MODE: 1, 7454 7455 _DEC_XFORM_MODE: 2, 7456 7457 /** 7458 * Creates shortcut functions to a cipher's object interface. 7459 * 7460 * @param {Cipher} cipher The cipher to create a helper for. 7461 * 7462 * @return {Object} An object with encrypt and decrypt shortcut functions. 7463 * 7464 * @static 7465 * 7466 * @example 7467 * 7468 * var AES = CryptoJS.lib.Cipher._createHelper(CryptoJS.algo.AES); 7469 */ 7470 _createHelper: (function () { 7471 function selectCipherStrategy(key) { 7472 if (typeof key == 'string') { 7473 return PasswordBasedCipher; 7474 } else { 7475 return SerializableCipher; 7476 } 7477 } 7478 7479 return function (cipher) { 7480 return { 7481 encrypt: function (message, key, cfg) { 7482 return selectCipherStrategy(key).encrypt(cipher, message, key, cfg); 7483 }, 7484 7485 decrypt: function (ciphertext, key, cfg) { 7486 return selectCipherStrategy(key).decrypt(cipher, ciphertext, key, cfg); 7487 } 7488 }; 7489 }; 7490 }()) 7491 }); 7492 7493 /** 7494 * Abstract base stream cipher template. 7495 * 7496 * @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 1 (32 bits) 7497 */ 7498 var StreamCipher = C_lib.StreamCipher = Cipher.extend({ 7499 _doFinalize: function () { 7500 // Process partial blocks 7501 var finalProcessedBlocks = this._process(!!'flush'); 7502 7503 return finalProcessedBlocks; 7504 }, 7505 7506 blockSize: 1 7507 }); 7508 7509 /** 7510 * Mode namespace. 7511 */ 7512 var C_mode = C.mode = {}; 7513 7514 /** 7515 * Abstract base block cipher mode template. 7516 */ 7517 var BlockCipherMode = C_lib.BlockCipherMode = Base.extend({ 7518 /** 7519 * Creates this mode for encryption. 7520 * 7521 * @param {Cipher} cipher A block cipher instance. 7522 * @param {Array} iv The IV words. 7523 * 7524 * @static 7525 * 7526 * @example 7527 * 7528 * var mode = CryptoJS.mode.CBC.createEncryptor(cipher, iv.words); 7529 */ 7530 createEncryptor: function (cipher, iv) { 7531 return this.Encryptor.create(cipher, iv); 7532 }, 7533 7534 /** 7535 * Creates this mode for decryption. 7536 * 7537 * @param {Cipher} cipher A block cipher instance. 7538 * @param {Array} iv The IV words. 7539 * 7540 * @static 7541 * 7542 * @example 7543 * 7544 * var mode = CryptoJS.mode.CBC.createDecryptor(cipher, iv.words); 7545 */ 7546 createDecryptor: function (cipher, iv) { 7547 return this.Decryptor.create(cipher, iv); 7548 }, 7549 7550 /** 7551 * Initializes a newly created mode. 7552 * 7553 * @param {Cipher} cipher A block cipher instance. 7554 * @param {Array} iv The IV words. 7555 * 7556 * @example 7557 * 7558 * var mode = CryptoJS.mode.CBC.Encryptor.create(cipher, iv.words); 7559 */ 7560 init: function (cipher, iv) { 7561 this._cipher = cipher; 7562 this._iv = iv; 7563 } 7564 }); 7565 7566 /** 7567 * Cipher Block Chaining mode. 7568 */ 7569 var CBC = C_mode.CBC = (function () { 7570 /** 7571 * Abstract base CBC mode. 7572 */ 7573 var CBC = BlockCipherMode.extend(); 7574 7575 /** 7576 * CBC encryptor. 7577 */ 7578 CBC.Encryptor = CBC.extend({ 7579 /** 7580 * Processes the data block at offset. 7581 * 7582 * @param {Array} words The data words to operate on. 7583 * @param {number} offset The offset where the block starts. 7584 * 7585 * @example 7586 * 7587 * mode.processBlock(data.words, offset); 7588 */ 7589 processBlock: function (words, offset) { 7590 // Shortcuts 7591 var cipher = this._cipher; 7592 var blockSize = cipher.blockSize; 7593 7594 // XOR and encrypt 7595 xorBlock.call(this, words, offset, blockSize); 7596 cipher.encryptBlock(words, offset); 7597 7598 // Remember this block to use with next block 7599 this._prevBlock = words.slice(offset, offset + blockSize); 7600 } 7601 }); 7602 7603 /** 7604 * CBC decryptor. 7605 */ 7606 CBC.Decryptor = CBC.extend({ 7607 /** 7608 * Processes the data block at offset. 7609 * 7610 * @param {Array} words The data words to operate on. 7611 * @param {number} offset The offset where the block starts. 7612 * 7613 * @example 7614 * 7615 * mode.processBlock(data.words, offset); 7616 */ 7617 processBlock: function (words, offset) { 7618 // Shortcuts 7619 var cipher = this._cipher; 7620 var blockSize = cipher.blockSize; 7621 7622 // Remember this block to use with next block 7623 var thisBlock = words.slice(offset, offset + blockSize); 7624 7625 // Decrypt and XOR 7626 cipher.decryptBlock(words, offset); 7627 xorBlock.call(this, words, offset, blockSize); 7628 7629 // This block becomes the previous block 7630 this._prevBlock = thisBlock; 7631 } 7632 }); 7633 7634 function xorBlock(words, offset, blockSize) { 7635 // Shortcut 7636 var iv = this._iv; 7637 7638 // Choose mixing block 7639 if (iv) { 7640 var block = iv; 7641 7642 // Remove IV for subsequent blocks 7643 this._iv = undefined; 7644 } else { 7645 var block = this._prevBlock; 7646 } 7647 7648 // XOR blocks 7649 for (var i = 0; i < blockSize; i++) { 7650 words[offset + i] ^= block[i]; 7651 } 7652 } 7653 7654 return CBC; 7655 }()); 7656 7657 /** 7658 * Padding namespace. 7659 */ 7660 var C_pad = C.pad = {}; 7661 7662 /** 7663 * PKCS #5/7 padding strategy. 7664 */ 7665 var Pkcs7 = C_pad.Pkcs7 = { 7666 /** 7667 * Pads data using the algorithm defined in PKCS #5/7. 7668 * 7669 * @param {WordArray} data The data to pad. 7670 * @param {number} blockSize The multiple that the data should be padded to. 7671 * 7672 * @static 7673 * 7674 * @example 7675 * 7676 * CryptoJS.pad.Pkcs7.pad(wordArray, 4); 7677 */ 7678 pad: function (data, blockSize) { 7679 // Shortcut 7680 var blockSizeBytes = blockSize * 4; 7681 7682 // Count padding bytes 7683 var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes; 7684 7685 // Create padding word 7686 var paddingWord = (nPaddingBytes << 24) | (nPaddingBytes << 16) | (nPaddingBytes << 8) | nPaddingBytes; 7687 7688 // Create padding 7689 var paddingWords = []; 7690 for (var i = 0; i < nPaddingBytes; i += 4) { 7691 paddingWords.push(paddingWord); 7692 } 7693 var padding = WordArray.create(paddingWords, nPaddingBytes); 7694 7695 // Add padding 7696 data.concat(padding); 7697 }, 7698 7699 /** 7700 * Unpads data that had been padded using the algorithm defined in PKCS #5/7. 7701 * 7702 * @param {WordArray} data The data to unpad. 7703 * 7704 * @static 7705 * 7706 * @example 7707 * 7708 * CryptoJS.pad.Pkcs7.unpad(wordArray); 7709 */ 7710 unpad: function (data) { 7711 // Get number of padding bytes from last byte 7712 var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff; 7713 7714 // Remove padding 7715 data.sigBytes -= nPaddingBytes; 7716 } 7717 }; 7718 7719 /** 7720 * Abstract base block cipher template. 7721 * 7722 * @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 4 (128 bits) 7723 */ 7724 var BlockCipher = C_lib.BlockCipher = Cipher.extend({ 7725 /** 7726 * Configuration options. 7727 * 7728 * @property {Mode} mode The block mode to use. Default: CBC 7729 * @property {Padding} padding The padding strategy to use. Default: Pkcs7 7730 */ 7731 cfg: Cipher.cfg.extend({ 7732 mode: CBC, 7733 padding: Pkcs7 7734 }), 7735 7736 reset: function () { 7737 // Reset cipher 7738 Cipher.reset.call(this); 7739 7740 // Shortcuts 7741 var cfg = this.cfg; 7742 var iv = cfg.iv; 7743 var mode = cfg.mode; 7744 7745 // Reset block mode 7746 if (this._xformMode == this._ENC_XFORM_MODE) { 7747 var modeCreator = mode.createEncryptor; 7748 } else /* if (this._xformMode == this._DEC_XFORM_MODE) */ { 7749 var modeCreator = mode.createDecryptor; 7750 7751 // Keep at least one block in the buffer for unpadding 7752 this._minBufferSize = 1; 7753 } 7754 this._mode = modeCreator.call(mode, this, iv && iv.words); 7755 }, 7756 7757 _doProcessBlock: function (words, offset) { 7758 this._mode.processBlock(words, offset); 7759 }, 7760 7761 _doFinalize: function () { 7762 // Shortcut 7763 var padding = this.cfg.padding; 7764 7765 // Finalize 7766 if (this._xformMode == this._ENC_XFORM_MODE) { 7767 // Pad data 7768 padding.pad(this._data, this.blockSize); 7769 7770 // Process final blocks 7771 var finalProcessedBlocks = this._process(!!'flush'); 7772 } else /* if (this._xformMode == this._DEC_XFORM_MODE) */ { 7773 // Process final blocks 7774 var finalProcessedBlocks = this._process(!!'flush'); 7775 7776 // Unpad data 7777 padding.unpad(finalProcessedBlocks); 7778 } 7779 7780 return finalProcessedBlocks; 7781 }, 7782 7783 blockSize: 128/32 7784 }); 7785 7786 /** 7787 * A collection of cipher parameters. 7788 * 7789 * @property {WordArray} ciphertext The raw ciphertext. 7790 * @property {WordArray} key The key to this ciphertext. 7791 * @property {WordArray} iv The IV used in the ciphering operation. 7792 * @property {WordArray} salt The salt used with a key derivation function. 7793 * @property {Cipher} algorithm The cipher algorithm. 7794 * @property {Mode} mode The block mode used in the ciphering operation. 7795 * @property {Padding} padding The padding scheme used in the ciphering operation. 7796 * @property {number} blockSize The block size of the cipher. 7797 * @property {Format} formatter The default formatting strategy to convert this cipher params object to a string. 7798 */ 7799 var CipherParams = C_lib.CipherParams = Base.extend({ 7800 /** 7801 * Initializes a newly created cipher params object. 7802 * 7803 * @param {Object} cipherParams An object with any of the possible cipher parameters. 7804 * 7805 * @example 7806 * 7807 * var cipherParams = CryptoJS.lib.CipherParams.create({ 7808 * ciphertext: ciphertextWordArray, 7809 * key: keyWordArray, 7810 * iv: ivWordArray, 7811 * salt: saltWordArray, 7812 * algorithm: CryptoJS.algo.AES, 7813 * mode: CryptoJS.mode.CBC, 7814 * padding: CryptoJS.pad.PKCS7, 7815 * blockSize: 4, 7816 * formatter: CryptoJS.format.OpenSSL 7817 * }); 7818 */ 7819 init: function (cipherParams) { 7820 this.mixIn(cipherParams); 7821 }, 7822 7823 /** 7824 * Converts this cipher params object to a string. 7825 * 7826 * @param {Format} formatter (Optional) The formatting strategy to use. 7827 * 7828 * @return {string} The stringified cipher params. 7829 * 7830 * @throws Error If neither the formatter nor the default formatter is set. 7831 * 7832 * @example 7833 * 7834 * var string = cipherParams + ''; 7835 * var string = cipherParams.toString(); 7836 * var string = cipherParams.toString(CryptoJS.format.OpenSSL); 7837 */ 7838 toString: function (formatter) { 7839 return (formatter || this.formatter).stringify(this); 7840 } 7841 }); 7842 7843 /** 7844 * Format namespace. 7845 */ 7846 var C_format = C.format = {}; 7847 7848 /** 7849 * OpenSSL formatting strategy. 7850 */ 7851 var OpenSSLFormatter = C_format.OpenSSL = { 7852 /** 7853 * Converts a cipher params object to an OpenSSL-compatible string. 7854 * 7855 * @param {CipherParams} cipherParams The cipher params object. 7856 * 7857 * @return {string} The OpenSSL-compatible string. 7858 * 7859 * @static 7860 * 7861 * @example 7862 * 7863 * var openSSLString = CryptoJS.format.OpenSSL.stringify(cipherParams); 7864 */ 7865 stringify: function (cipherParams) { 7866 // Shortcuts 7867 var ciphertext = cipherParams.ciphertext; 7868 var salt = cipherParams.salt; 7869 7870 // Format 7871 if (salt) { 7872 var wordArray = WordArray.create([0x53616c74, 0x65645f5f]).concat(salt).concat(ciphertext); 7873 } else { 7874 var wordArray = ciphertext; 7875 } 7876 7877 return wordArray.toString(Base64); 7878 }, 7879 7880 /** 7881 * Converts an OpenSSL-compatible string to a cipher params object. 7882 * 7883 * @param {string} openSSLStr The OpenSSL-compatible string. 7884 * 7885 * @return {CipherParams} The cipher params object. 7886 * 7887 * @static 7888 * 7889 * @example 7890 * 7891 * var cipherParams = CryptoJS.format.OpenSSL.parse(openSSLString); 7892 */ 7893 parse: function (openSSLStr) { 7894 // Parse base64 7895 var ciphertext = Base64.parse(openSSLStr); 7896 7897 // Shortcut 7898 var ciphertextWords = ciphertext.words; 7899 7900 // Test for salt 7901 if (ciphertextWords[0] == 0x53616c74 && ciphertextWords[1] == 0x65645f5f) { 7902 // Extract salt 7903 var salt = WordArray.create(ciphertextWords.slice(2, 4)); 7904 7905 // Remove salt from ciphertext 7906 ciphertextWords.splice(0, 4); 7907 ciphertext.sigBytes -= 16; 7908 } 7909 7910 return CipherParams.create({ ciphertext: ciphertext, salt: salt }); 7911 } 7912 }; 7913 7914 /** 7915 * A cipher wrapper that returns ciphertext as a serializable cipher params object. 7916 */ 7917 var SerializableCipher = C_lib.SerializableCipher = Base.extend({ 7918 /** 7919 * Configuration options. 7920 * 7921 * @property {Formatter} format The formatting strategy to convert cipher param objects to and from a string. Default: OpenSSL 7922 */ 7923 cfg: Base.extend({ 7924 format: OpenSSLFormatter 7925 }), 7926 7927 /** 7928 * Encrypts a message. 7929 * 7930 * @param {Cipher} cipher The cipher algorithm to use. 7931 * @param {WordArray|string} message The message to encrypt. 7932 * @param {WordArray} key The key. 7933 * @param {Object} cfg (Optional) The configuration options to use for this operation. 7934 * 7935 * @return {CipherParams} A cipher params object. 7936 * 7937 * @static 7938 * 7939 * @example 7940 * 7941 * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key); 7942 * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv }); 7943 * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv, format: CryptoJS.format.OpenSSL }); 7944 */ 7945 encrypt: function (cipher, message, key, cfg) { 7946 // Apply config defaults 7947 cfg = this.cfg.extend(cfg); 7948 7949 // Encrypt 7950 var encryptor = cipher.createEncryptor(key, cfg); 7951 var ciphertext = encryptor.finalize(message); 7952 7953 // Shortcut 7954 var cipherCfg = encryptor.cfg; 7955 7956 // Create and return serializable cipher params 7957 return CipherParams.create({ 7958 ciphertext: ciphertext, 7959 key: key, 7960 iv: cipherCfg.iv, 7961 algorithm: cipher, 7962 mode: cipherCfg.mode, 7963 padding: cipherCfg.padding, 7964 blockSize: cipher.blockSize, 7965 formatter: cfg.format 7966 }); 7967 }, 7968 7969 /** 7970 * Decrypts serialized ciphertext. 7971 * 7972 * @param {Cipher} cipher The cipher algorithm to use. 7973 * @param {CipherParams|string} ciphertext The ciphertext to decrypt. 7974 * @param {WordArray} key The key. 7975 * @param {Object} cfg (Optional) The configuration options to use for this operation. 7976 * 7977 * @return {WordArray} The plaintext. 7978 * 7979 * @static 7980 * 7981 * @example 7982 * 7983 * var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, key, { iv: iv, format: CryptoJS.format.OpenSSL }); 7984 * var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, key, { iv: iv, format: CryptoJS.format.OpenSSL }); 7985 */ 7986 decrypt: function (cipher, ciphertext, key, cfg) { 7987 // Apply config defaults 7988 cfg = this.cfg.extend(cfg); 7989 7990 // Convert string to CipherParams 7991 ciphertext = this._parse(ciphertext, cfg.format); 7992 7993 // Decrypt 7994 var plaintext = cipher.createDecryptor(key, cfg).finalize(ciphertext.ciphertext); 7995 7996 return plaintext; 7997 }, 7998 7999 /** 8000 * Converts serialized ciphertext to CipherParams, 8001 * else assumed CipherParams already and returns ciphertext unchanged. 8002 * 8003 * @param {CipherParams|string} ciphertext The ciphertext. 8004 * @param {Formatter} format The formatting strategy to use to parse serialized ciphertext. 8005 * 8006 * @return {CipherParams} The unserialized ciphertext. 8007 * 8008 * @static 8009 * 8010 * @example 8011 * 8012 * var ciphertextParams = CryptoJS.lib.SerializableCipher._parse(ciphertextStringOrParams, format); 8013 */ 8014 _parse: function (ciphertext, format) { 8015 if (typeof ciphertext == 'string') { 8016 return format.parse(ciphertext, this); 8017 } else { 8018 return ciphertext; 8019 } 8020 } 8021 }); 8022 8023 /** 8024 * Key derivation function namespace. 8025 */ 8026 var C_kdf = C.kdf = {}; 8027 8028 /** 8029 * OpenSSL key derivation function. 8030 */ 8031 var OpenSSLKdf = C_kdf.OpenSSL = { 8032 /** 8033 * Derives a key and IV from a password. 8034 * 8035 * @param {string} password The password to derive from. 8036 * @param {number} keySize The size in words of the key to generate. 8037 * @param {number} ivSize The size in words of the IV to generate. 8038 * @param {WordArray|string} salt (Optional) A 64-bit salt to use. If omitted, a salt will be generated randomly. 8039 * 8040 * @return {CipherParams} A cipher params object with the key, IV, and salt. 8041 * 8042 * @static 8043 * 8044 * @example 8045 * 8046 * var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32); 8047 * var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32, 'saltsalt'); 8048 */ 8049 execute: function (password, keySize, ivSize, salt) { 8050 // Generate random salt 8051 if (!salt) { 8052 salt = WordArray.random(64/8); 8053 } 8054 8055 // Derive key and IV 8056 var key = EvpKDF.create({ keySize: keySize + ivSize }).compute(password, salt); 8057 8058 // Separate key and IV 8059 var iv = WordArray.create(key.words.slice(keySize), ivSize * 4); 8060 key.sigBytes = keySize * 4; 8061 8062 // Return params 8063 return CipherParams.create({ key: key, iv: iv, salt: salt }); 8064 } 8065 }; 8066 8067 /** 8068 * A serializable cipher wrapper that derives the key from a password, 8069 * and returns ciphertext as a serializable cipher params object. 8070 */ 8071 var PasswordBasedCipher = C_lib.PasswordBasedCipher = SerializableCipher.extend({ 8072 /** 8073 * Configuration options. 8074 * 8075 * @property {KDF} kdf The key derivation function to use to generate a key and IV from a password. Default: OpenSSL 8076 */ 8077 cfg: SerializableCipher.cfg.extend({ 8078 kdf: OpenSSLKdf 8079 }), 8080 8081 /** 8082 * Encrypts a message using a password. 8083 * 8084 * @param {Cipher} cipher The cipher algorithm to use. 8085 * @param {WordArray|string} message The message to encrypt. 8086 * @param {string} password The password. 8087 * @param {Object} cfg (Optional) The configuration options to use for this operation. 8088 * 8089 * @return {CipherParams} A cipher params object. 8090 * 8091 * @static 8092 * 8093 * @example 8094 * 8095 * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password'); 8096 * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password', { format: CryptoJS.format.OpenSSL }); 8097 */ 8098 encrypt: function (cipher, message, password, cfg) { 8099 // Apply config defaults 8100 cfg = this.cfg.extend(cfg); 8101 8102 // Derive key and other params 8103 var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize); 8104 8105 // Add IV to config 8106 cfg.iv = derivedParams.iv; 8107 8108 // Encrypt 8109 var ciphertext = SerializableCipher.encrypt.call(this, cipher, message, derivedParams.key, cfg); 8110 8111 // Mix in derived params 8112 ciphertext.mixIn(derivedParams); 8113 8114 return ciphertext; 8115 }, 8116 8117 /** 8118 * Decrypts serialized ciphertext using a password. 8119 * 8120 * @param {Cipher} cipher The cipher algorithm to use. 8121 * @param {CipherParams|string} ciphertext The ciphertext to decrypt. 8122 * @param {string} password The password. 8123 * @param {Object} cfg (Optional) The configuration options to use for this operation. 8124 * 8125 * @return {WordArray} The plaintext. 8126 * 8127 * @static 8128 * 8129 * @example 8130 * 8131 * var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, 'password', { format: CryptoJS.format.OpenSSL }); 8132 * var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, 'password', { format: CryptoJS.format.OpenSSL }); 8133 */ 8134 decrypt: function (cipher, ciphertext, password, cfg) { 8135 // Apply config defaults 8136 cfg = this.cfg.extend(cfg); 8137 8138 // Convert string to CipherParams 8139 ciphertext = this._parse(ciphertext, cfg.format); 8140 8141 // Derive key and other params 8142 var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, ciphertext.salt); 8143 8144 // Add IV to config 8145 cfg.iv = derivedParams.iv; 8146 8147 // Decrypt 8148 var plaintext = SerializableCipher.decrypt.call(this, cipher, ciphertext, derivedParams.key, cfg); 8149 8150 return plaintext; 8151 } 8152 }); 8153 }()); 8154 8155 8156 })); 8157 },{"./core":53}],53:[function(require,module,exports){ 8158 ;(function (root, factory) { 8159 if (typeof exports === "object") { 8160 // CommonJS 8161 module.exports = exports = factory(); 8162 } 8163 else if (typeof define === "function" && define.amd) { 8164 // AMD 8165 define([], factory); 8166 } 8167 else { 8168 // Global (browser) 8169 root.CryptoJS = factory(); 8170 } 8171 }(this, function () { 8172 8173 /** 8174 * CryptoJS core components. 8175 */ 8176 var CryptoJS = CryptoJS || (function (Math, undefined) { 8177 /** 8178 * CryptoJS namespace. 8179 */ 8180 var C = {}; 8181 8182 /** 8183 * Library namespace. 8184 */ 8185 var C_lib = C.lib = {}; 8186 8187 /** 8188 * Base object for prototypal inheritance. 8189 */ 8190 var Base = C_lib.Base = (function () { 8191 function F() {} 8192 8193 return { 8194 /** 8195 * Creates a new object that inherits from this object. 8196 * 8197 * @param {Object} overrides Properties to copy into the new object. 8198 * 8199 * @return {Object} The new object. 8200 * 8201 * @static 8202 * 8203 * @example 8204 * 8205 * var MyType = CryptoJS.lib.Base.extend({ 8206 * field: 'value', 8207 * 8208 * method: function () { 8209 * } 8210 * }); 8211 */ 8212 extend: function (overrides) { 8213 // Spawn 8214 F.prototype = this; 8215 var subtype = new F(); 8216 8217 // Augment 8218 if (overrides) { 8219 subtype.mixIn(overrides); 8220 } 8221 8222 // Create default initializer 8223 if (!subtype.hasOwnProperty('init')) { 8224 subtype.init = function () { 8225 subtype.$super.init.apply(this, arguments); 8226 }; 8227 } 8228 8229 // Initializer's prototype is the subtype object 8230 subtype.init.prototype = subtype; 8231 8232 // Reference supertype 8233 subtype.$super = this; 8234 8235 return subtype; 8236 }, 8237 8238 /** 8239 * Extends this object and runs the init method. 8240 * Arguments to create() will be passed to init(). 8241 * 8242 * @return {Object} The new object. 8243 * 8244 * @static 8245 * 8246 * @example 8247 * 8248 * var instance = MyType.create(); 8249 */ 8250 create: function () { 8251 var instance = this.extend(); 8252 instance.init.apply(instance, arguments); 8253 8254 return instance; 8255 }, 8256 8257 /** 8258 * Initializes a newly created object. 8259 * Override this method to add some logic when your objects are created. 8260 * 8261 * @example 8262 * 8263 * var MyType = CryptoJS.lib.Base.extend({ 8264 * init: function () { 8265 * // ... 8266 * } 8267 * }); 8268 */ 8269 init: function () { 8270 }, 8271 8272 /** 8273 * Copies properties into this object. 8274 * 8275 * @param {Object} properties The properties to mix in. 8276 * 8277 * @example 8278 * 8279 * MyType.mixIn({ 8280 * field: 'value' 8281 * }); 8282 */ 8283 mixIn: function (properties) { 8284 for (var propertyName in properties) { 8285 if (properties.hasOwnProperty(propertyName)) { 8286 this[propertyName] = properties[propertyName]; 8287 } 8288 } 8289 8290 // IE won't copy toString using the loop above 8291 if (properties.hasOwnProperty('toString')) { 8292 this.toString = properties.toString; 8293 } 8294 }, 8295 8296 /** 8297 * Creates a copy of this object. 8298 * 8299 * @return {Object} The clone. 8300 * 8301 * @example 8302 * 8303 * var clone = instance.clone(); 8304 */ 8305 clone: function () { 8306 return this.init.prototype.extend(this); 8307 } 8308 }; 8309 }()); 8310 8311 /** 8312 * An array of 32-bit words. 8313 * 8314 * @property {Array} words The array of 32-bit words. 8315 * @property {number} sigBytes The number of significant bytes in this word array. 8316 */ 8317 var WordArray = C_lib.WordArray = Base.extend({ 8318 /** 8319 * Initializes a newly created word array. 8320 * 8321 * @param {Array} words (Optional) An array of 32-bit words. 8322 * @param {number} sigBytes (Optional) The number of significant bytes in the words. 8323 * 8324 * @example 8325 * 8326 * var wordArray = CryptoJS.lib.WordArray.create(); 8327 * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607]); 8328 * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607], 6); 8329 */ 8330 init: function (words, sigBytes) { 8331 words = this.words = words || []; 8332 8333 if (sigBytes != undefined) { 8334 this.sigBytes = sigBytes; 8335 } else { 8336 this.sigBytes = words.length * 4; 8337 } 8338 }, 8339 8340 /** 8341 * Converts this word array to a string. 8342 * 8343 * @param {Encoder} encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex 8344 * 8345 * @return {string} The stringified word array. 8346 * 8347 * @example 8348 * 8349 * var string = wordArray + ''; 8350 * var string = wordArray.toString(); 8351 * var string = wordArray.toString(CryptoJS.enc.Utf8); 8352 */ 8353 toString: function (encoder) { 8354 return (encoder || Hex).stringify(this); 8355 }, 8356 8357 /** 8358 * Concatenates a word array to this word array. 8359 * 8360 * @param {WordArray} wordArray The word array to append. 8361 * 8362 * @return {WordArray} This word array. 8363 * 8364 * @example 8365 * 8366 * wordArray1.concat(wordArray2); 8367 */ 8368 concat: function (wordArray) { 8369 // Shortcuts 8370 var thisWords = this.words; 8371 var thatWords = wordArray.words; 8372 var thisSigBytes = this.sigBytes; 8373 var thatSigBytes = wordArray.sigBytes; 8374 8375 // Clamp excess bits 8376 this.clamp(); 8377 8378 // Concat 8379 if (thisSigBytes % 4) { 8380 // Copy one byte at a time 8381 for (var i = 0; i < thatSigBytes; i++) { 8382 var thatByte = (thatWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff; 8383 thisWords[(thisSigBytes + i) >>> 2] |= thatByte << (24 - ((thisSigBytes + i) % 4) * 8); 8384 } 8385 } else if (thatWords.length > 0xffff) { 8386 // Copy one word at a time 8387 for (var i = 0; i < thatSigBytes; i += 4) { 8388 thisWords[(thisSigBytes + i) >>> 2] = thatWords[i >>> 2]; 8389 } 8390 } else { 8391 // Copy all words at once 8392 thisWords.push.apply(thisWords, thatWords); 8393 } 8394 this.sigBytes += thatSigBytes; 8395 8396 // Chainable 8397 return this; 8398 }, 8399 8400 /** 8401 * Removes insignificant bits. 8402 * 8403 * @example 8404 * 8405 * wordArray.clamp(); 8406 */ 8407 clamp: function () { 8408 // Shortcuts 8409 var words = this.words; 8410 var sigBytes = this.sigBytes; 8411 8412 // Clamp 8413 words[sigBytes >>> 2] &= 0xffffffff << (32 - (sigBytes % 4) * 8); 8414 words.length = Math.ceil(sigBytes / 4); 8415 }, 8416 8417 /** 8418 * Creates a copy of this word array. 8419 * 8420 * @return {WordArray} The clone. 8421 * 8422 * @example 8423 * 8424 * var clone = wordArray.clone(); 8425 */ 8426 clone: function () { 8427 var clone = Base.clone.call(this); 8428 clone.words = this.words.slice(0); 8429 8430 return clone; 8431 }, 8432 8433 /** 8434 * Creates a word array filled with random bytes. 8435 * 8436 * @param {number} nBytes The number of random bytes to generate. 8437 * 8438 * @return {WordArray} The random word array. 8439 * 8440 * @static 8441 * 8442 * @example 8443 * 8444 * var wordArray = CryptoJS.lib.WordArray.random(16); 8445 */ 8446 random: function (nBytes) { 8447 var words = []; 8448 8449 var r = (function (m_w) { 8450 var m_w = m_w; 8451 var m_z = 0x3ade68b1; 8452 var mask = 0xffffffff; 8453 8454 return function () { 8455 m_z = (0x9069 * (m_z & 0xFFFF) + (m_z >> 0x10)) & mask; 8456 m_w = (0x4650 * (m_w & 0xFFFF) + (m_w >> 0x10)) & mask; 8457 var result = ((m_z << 0x10) + m_w) & mask; 8458 result /= 0x100000000; 8459 result += 0.5; 8460 return result * (Math.random() > .5 ? 1 : -1); 8461 } 8462 }); 8463 8464 for (var i = 0, rcache; i < nBytes; i += 4) { 8465 var _r = r((rcache || Math.random()) * 0x100000000); 8466 8467 rcache = _r() * 0x3ade67b7; 8468 words.push((_r() * 0x100000000) | 0); 8469 } 8470 8471 return new WordArray.init(words, nBytes); 8472 } 8473 }); 8474 8475 /** 8476 * Encoder namespace. 8477 */ 8478 var C_enc = C.enc = {}; 8479 8480 /** 8481 * Hex encoding strategy. 8482 */ 8483 var Hex = C_enc.Hex = { 8484 /** 8485 * Converts a word array to a hex string. 8486 * 8487 * @param {WordArray} wordArray The word array. 8488 * 8489 * @return {string} The hex string. 8490 * 8491 * @static 8492 * 8493 * @example 8494 * 8495 * var hexString = CryptoJS.enc.Hex.stringify(wordArray); 8496 */ 8497 stringify: function (wordArray) { 8498 // Shortcuts 8499 var words = wordArray.words; 8500 var sigBytes = wordArray.sigBytes; 8501 8502 // Convert 8503 var hexChars = []; 8504 for (var i = 0; i < sigBytes; i++) { 8505 var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff; 8506 hexChars.push((bite >>> 4).toString(16)); 8507 hexChars.push((bite & 0x0f).toString(16)); 8508 } 8509 8510 return hexChars.join(''); 8511 }, 8512 8513 /** 8514 * Converts a hex string to a word array. 8515 * 8516 * @param {string} hexStr The hex string. 8517 * 8518 * @return {WordArray} The word array. 8519 * 8520 * @static 8521 * 8522 * @example 8523 * 8524 * var wordArray = CryptoJS.enc.Hex.parse(hexString); 8525 */ 8526 parse: function (hexStr) { 8527 // Shortcut 8528 var hexStrLength = hexStr.length; 8529 8530 // Convert 8531 var words = []; 8532 for (var i = 0; i < hexStrLength; i += 2) { 8533 words[i >>> 3] |= parseInt(hexStr.substr(i, 2), 16) << (24 - (i % 8) * 4); 8534 } 8535 8536 return new WordArray.init(words, hexStrLength / 2); 8537 } 8538 }; 8539 8540 /** 8541 * Latin1 encoding strategy. 8542 */ 8543 var Latin1 = C_enc.Latin1 = { 8544 /** 8545 * Converts a word array to a Latin1 string. 8546 * 8547 * @param {WordArray} wordArray The word array. 8548 * 8549 * @return {string} The Latin1 string. 8550 * 8551 * @static 8552 * 8553 * @example 8554 * 8555 * var latin1String = CryptoJS.enc.Latin1.stringify(wordArray); 8556 */ 8557 stringify: function (wordArray) { 8558 // Shortcuts 8559 var words = wordArray.words; 8560 var sigBytes = wordArray.sigBytes; 8561 8562 // Convert 8563 var latin1Chars = []; 8564 for (var i = 0; i < sigBytes; i++) { 8565 var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff; 8566 latin1Chars.push(String.fromCharCode(bite)); 8567 } 8568 8569 return latin1Chars.join(''); 8570 }, 8571 8572 /** 8573 * Converts a Latin1 string to a word array. 8574 * 8575 * @param {string} latin1Str The Latin1 string. 8576 * 8577 * @return {WordArray} The word array. 8578 * 8579 * @static 8580 * 8581 * @example 8582 * 8583 * var wordArray = CryptoJS.enc.Latin1.parse(latin1String); 8584 */ 8585 parse: function (latin1Str) { 8586 // Shortcut 8587 var latin1StrLength = latin1Str.length; 8588 8589 // Convert 8590 var words = []; 8591 for (var i = 0; i < latin1StrLength; i++) { 8592 words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8); 8593 } 8594 8595 return new WordArray.init(words, latin1StrLength); 8596 } 8597 }; 8598 8599 /** 8600 * UTF-8 encoding strategy. 8601 */ 8602 var Utf8 = C_enc.Utf8 = { 8603 /** 8604 * Converts a word array to a UTF-8 string. 8605 * 8606 * @param {WordArray} wordArray The word array. 8607 * 8608 * @return {string} The UTF-8 string. 8609 * 8610 * @static 8611 * 8612 * @example 8613 * 8614 * var utf8String = CryptoJS.enc.Utf8.stringify(wordArray); 8615 */ 8616 stringify: function (wordArray) { 8617 try { 8618 return decodeURIComponent(escape(Latin1.stringify(wordArray))); 8619 } catch (e) { 8620 throw new Error('Malformed UTF-8 data'); 8621 } 8622 }, 8623 8624 /** 8625 * Converts a UTF-8 string to a word array. 8626 * 8627 * @param {string} utf8Str The UTF-8 string. 8628 * 8629 * @return {WordArray} The word array. 8630 * 8631 * @static 8632 * 8633 * @example 8634 * 8635 * var wordArray = CryptoJS.enc.Utf8.parse(utf8String); 8636 */ 8637 parse: function (utf8Str) { 8638 return Latin1.parse(unescape(encodeURIComponent(utf8Str))); 8639 } 8640 }; 8641 8642 /** 8643 * Abstract buffered block algorithm template. 8644 * 8645 * The property blockSize must be implemented in a concrete subtype. 8646 * 8647 * @property {number} _minBufferSize The number of blocks that should be kept unprocessed in the buffer. Default: 0 8648 */ 8649 var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm = Base.extend({ 8650 /** 8651 * Resets this block algorithm's data buffer to its initial state. 8652 * 8653 * @example 8654 * 8655 * bufferedBlockAlgorithm.reset(); 8656 */ 8657 reset: function () { 8658 // Initial values 8659 this._data = new WordArray.init(); 8660 this._nDataBytes = 0; 8661 }, 8662 8663 /** 8664 * Adds new data to this block algorithm's buffer. 8665 * 8666 * @param {WordArray|string} data The data to append. Strings are converted to a WordArray using UTF-8. 8667 * 8668 * @example 8669 * 8670 * bufferedBlockAlgorithm._append('data'); 8671 * bufferedBlockAlgorithm._append(wordArray); 8672 */ 8673 _append: function (data) { 8674 // Convert string to WordArray, else assume WordArray already 8675 if (typeof data == 'string') { 8676 data = Utf8.parse(data); 8677 } 8678 8679 // Append 8680 this._data.concat(data); 8681 this._nDataBytes += data.sigBytes; 8682 }, 8683 8684 /** 8685 * Processes available data blocks. 8686 * 8687 * This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype. 8688 * 8689 * @param {boolean} doFlush Whether all blocks and partial blocks should be processed. 8690 * 8691 * @return {WordArray} The processed data. 8692 * 8693 * @example 8694 * 8695 * var processedData = bufferedBlockAlgorithm._process(); 8696 * var processedData = bufferedBlockAlgorithm._process(!!'flush'); 8697 */ 8698 _process: function (doFlush) { 8699 // Shortcuts 8700 var data = this._data; 8701 var dataWords = data.words; 8702 var dataSigBytes = data.sigBytes; 8703 var blockSize = this.blockSize; 8704 var blockSizeBytes = blockSize * 4; 8705 8706 // Count blocks ready 8707 var nBlocksReady = dataSigBytes / blockSizeBytes; 8708 if (doFlush) { 8709 // Round up to include partial blocks 8710 nBlocksReady = Math.ceil(nBlocksReady); 8711 } else { 8712 // Round down to include only full blocks, 8713 // less the number of blocks that must remain in the buffer 8714 nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0); 8715 } 8716 8717 // Count words ready 8718 var nWordsReady = nBlocksReady * blockSize; 8719 8720 // Count bytes ready 8721 var nBytesReady = Math.min(nWordsReady * 4, dataSigBytes); 8722 8723 // Process blocks 8724 if (nWordsReady) { 8725 for (var offset = 0; offset < nWordsReady; offset += blockSize) { 8726 // Perform concrete-algorithm logic 8727 this._doProcessBlock(dataWords, offset); 8728 } 8729 8730 // Remove processed words 8731 var processedWords = dataWords.splice(0, nWordsReady); 8732 data.sigBytes -= nBytesReady; 8733 } 8734 8735 // Return processed words 8736 return new WordArray.init(processedWords, nBytesReady); 8737 }, 8738 8739 /** 8740 * Creates a copy of this object. 8741 * 8742 * @return {Object} The clone. 8743 * 8744 * @example 8745 * 8746 * var clone = bufferedBlockAlgorithm.clone(); 8747 */ 8748 clone: function () { 8749 var clone = Base.clone.call(this); 8750 clone._data = this._data.clone(); 8751 8752 return clone; 8753 }, 8754 8755 _minBufferSize: 0 8756 }); 8757 8758 /** 8759 * Abstract hasher template. 8760 * 8761 * @property {number} blockSize The number of 32-bit words this hasher operates on. Default: 16 (512 bits) 8762 */ 8763 var Hasher = C_lib.Hasher = BufferedBlockAlgorithm.extend({ 8764 /** 8765 * Configuration options. 8766 */ 8767 cfg: Base.extend(), 8768 8769 /** 8770 * Initializes a newly created hasher. 8771 * 8772 * @param {Object} cfg (Optional) The configuration options to use for this hash computation. 8773 * 8774 * @example 8775 * 8776 * var hasher = CryptoJS.algo.SHA256.create(); 8777 */ 8778 init: function (cfg) { 8779 // Apply config defaults 8780 this.cfg = this.cfg.extend(cfg); 8781 8782 // Set initial values 8783 this.reset(); 8784 }, 8785 8786 /** 8787 * Resets this hasher to its initial state. 8788 * 8789 * @example 8790 * 8791 * hasher.reset(); 8792 */ 8793 reset: function () { 8794 // Reset data buffer 8795 BufferedBlockAlgorithm.reset.call(this); 8796 8797 // Perform concrete-hasher logic 8798 this._doReset(); 8799 }, 8800 8801 /** 8802 * Updates this hasher with a message. 8803 * 8804 * @param {WordArray|string} messageUpdate The message to append. 8805 * 8806 * @return {Hasher} This hasher. 8807 * 8808 * @example 8809 * 8810 * hasher.update('message'); 8811 * hasher.update(wordArray); 8812 */ 8813 update: function (messageUpdate) { 8814 // Append 8815 this._append(messageUpdate); 8816 8817 // Update the hash 8818 this._process(); 8819 8820 // Chainable 8821 return this; 8822 }, 8823 8824 /** 8825 * Finalizes the hash computation. 8826 * Note that the finalize operation is effectively a destructive, read-once operation. 8827 * 8828 * @param {WordArray|string} messageUpdate (Optional) A final message update. 8829 * 8830 * @return {WordArray} The hash. 8831 * 8832 * @example 8833 * 8834 * var hash = hasher.finalize(); 8835 * var hash = hasher.finalize('message'); 8836 * var hash = hasher.finalize(wordArray); 8837 */ 8838 finalize: function (messageUpdate) { 8839 // Final message update 8840 if (messageUpdate) { 8841 this._append(messageUpdate); 8842 } 8843 8844 // Perform concrete-hasher logic 8845 var hash = this._doFinalize(); 8846 8847 return hash; 8848 }, 8849 8850 blockSize: 512/32, 8851 8852 /** 8853 * Creates a shortcut function to a hasher's object interface. 8854 * 8855 * @param {Hasher} hasher The hasher to create a helper for. 8856 * 8857 * @return {Function} The shortcut function. 8858 * 8859 * @static 8860 * 8861 * @example 8862 * 8863 * var SHA256 = CryptoJS.lib.Hasher._createHelper(CryptoJS.algo.SHA256); 8864 */ 8865 _createHelper: function (hasher) { 8866 return function (message, cfg) { 8867 return new hasher.init(cfg).finalize(message); 8868 }; 8869 }, 8870 8871 /** 8872 * Creates a shortcut function to the HMAC's object interface. 8873 * 8874 * @param {Hasher} hasher The hasher to use in this HMAC helper. 8875 * 8876 * @return {Function} The shortcut function. 8877 * 8878 * @static 8879 * 8880 * @example 8881 * 8882 * var HmacSHA256 = CryptoJS.lib.Hasher._createHmacHelper(CryptoJS.algo.SHA256); 8883 */ 8884 _createHmacHelper: function (hasher) { 8885 return function (message, key) { 8886 return new C_algo.HMAC.init(hasher, key).finalize(message); 8887 }; 8888 } 8889 }); 8890 8891 /** 8892 * Algorithm namespace. 8893 */ 8894 var C_algo = C.algo = {}; 8895 8896 return C; 8897 }(Math)); 8898 8899 8900 return CryptoJS; 8901 8902 })); 8903 },{}],54:[function(require,module,exports){ 8904 ;(function (root, factory) { 8905 if (typeof exports === "object") { 8906 // CommonJS 8907 module.exports = exports = factory(require("./core")); 8908 } 8909 else if (typeof define === "function" && define.amd) { 8910 // AMD 8911 define(["./core"], factory); 8912 } 8913 else { 8914 // Global (browser) 8915 factory(root.CryptoJS); 8916 } 8917 }(this, function (CryptoJS) { 8918 8919 (function () { 8920 // Shortcuts 8921 var C = CryptoJS; 8922 var C_lib = C.lib; 8923 var WordArray = C_lib.WordArray; 8924 var C_enc = C.enc; 8925 8926 /** 8927 * Base64 encoding strategy. 8928 */ 8929 var Base64 = C_enc.Base64 = { 8930 /** 8931 * Converts a word array to a Base64 string. 8932 * 8933 * @param {WordArray} wordArray The word array. 8934 * 8935 * @return {string} The Base64 string. 8936 * 8937 * @static 8938 * 8939 * @example 8940 * 8941 * var base64String = CryptoJS.enc.Base64.stringify(wordArray); 8942 */ 8943 stringify: function (wordArray) { 8944 // Shortcuts 8945 var words = wordArray.words; 8946 var sigBytes = wordArray.sigBytes; 8947 var map = this._map; 8948 8949 // Clamp excess bits 8950 wordArray.clamp(); 8951 8952 // Convert 8953 var base64Chars = []; 8954 for (var i = 0; i < sigBytes; i += 3) { 8955 var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff; 8956 var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff; 8957 var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff; 8958 8959 var triplet = (byte1 << 16) | (byte2 << 8) | byte3; 8960 8961 for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) { 8962 base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f)); 8963 } 8964 } 8965 8966 // Add padding 8967 var paddingChar = map.charAt(64); 8968 if (paddingChar) { 8969 while (base64Chars.length % 4) { 8970 base64Chars.push(paddingChar); 8971 } 8972 } 8973 8974 return base64Chars.join(''); 8975 }, 8976 8977 /** 8978 * Converts a Base64 string to a word array. 8979 * 8980 * @param {string} base64Str The Base64 string. 8981 * 8982 * @return {WordArray} The word array. 8983 * 8984 * @static 8985 * 8986 * @example 8987 * 8988 * var wordArray = CryptoJS.enc.Base64.parse(base64String); 8989 */ 8990 parse: function (base64Str) { 8991 // Shortcuts 8992 var base64StrLength = base64Str.length; 8993 var map = this._map; 8994 8995 // Ignore padding 8996 var paddingChar = map.charAt(64); 8997 if (paddingChar) { 8998 var paddingIndex = base64Str.indexOf(paddingChar); 8999 if (paddingIndex != -1) { 9000 base64StrLength = paddingIndex; 9001 } 9002 } 9003 9004 // Convert 9005 var words = []; 9006 var nBytes = 0; 9007 for (var i = 0; i < base64StrLength; i++) { 9008 if (i % 4) { 9009 var bits1 = map.indexOf(base64Str.charAt(i - 1)) << ((i % 4) * 2); 9010 var bits2 = map.indexOf(base64Str.charAt(i)) >>> (6 - (i % 4) * 2); 9011 words[nBytes >>> 2] |= (bits1 | bits2) << (24 - (nBytes % 4) * 8); 9012 nBytes++; 9013 } 9014 } 9015 9016 return WordArray.create(words, nBytes); 9017 }, 9018 9019 _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' 9020 }; 9021 }()); 9022 9023 9024 return CryptoJS.enc.Base64; 9025 9026 })); 9027 },{"./core":53}],55:[function(require,module,exports){ 9028 ;(function (root, factory) { 9029 if (typeof exports === "object") { 9030 // CommonJS 9031 module.exports = exports = factory(require("./core")); 9032 } 9033 else if (typeof define === "function" && define.amd) { 9034 // AMD 9035 define(["./core"], factory); 9036 } 9037 else { 9038 // Global (browser) 9039 factory(root.CryptoJS); 9040 } 9041 }(this, function (CryptoJS) { 9042 9043 (function () { 9044 // Shortcuts 9045 var C = CryptoJS; 9046 var C_lib = C.lib; 9047 var WordArray = C_lib.WordArray; 9048 var C_enc = C.enc; 9049 9050 /** 9051 * UTF-16 BE encoding strategy. 9052 */ 9053 var Utf16BE = C_enc.Utf16 = C_enc.Utf16BE = { 9054 /** 9055 * Converts a word array to a UTF-16 BE string. 9056 * 9057 * @param {WordArray} wordArray The word array. 9058 * 9059 * @return {string} The UTF-16 BE string. 9060 * 9061 * @static 9062 * 9063 * @example 9064 * 9065 * var utf16String = CryptoJS.enc.Utf16.stringify(wordArray); 9066 */ 9067 stringify: function (wordArray) { 9068 // Shortcuts 9069 var words = wordArray.words; 9070 var sigBytes = wordArray.sigBytes; 9071 9072 // Convert 9073 var utf16Chars = []; 9074 for (var i = 0; i < sigBytes; i += 2) { 9075 var codePoint = (words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff; 9076 utf16Chars.push(String.fromCharCode(codePoint)); 9077 } 9078 9079 return utf16Chars.join(''); 9080 }, 9081 9082 /** 9083 * Converts a UTF-16 BE string to a word array. 9084 * 9085 * @param {string} utf16Str The UTF-16 BE string. 9086 * 9087 * @return {WordArray} The word array. 9088 * 9089 * @static 9090 * 9091 * @example 9092 * 9093 * var wordArray = CryptoJS.enc.Utf16.parse(utf16String); 9094 */ 9095 parse: function (utf16Str) { 9096 // Shortcut 9097 var utf16StrLength = utf16Str.length; 9098 9099 // Convert 9100 var words = []; 9101 for (var i = 0; i < utf16StrLength; i++) { 9102 words[i >>> 1] |= utf16Str.charCodeAt(i) << (16 - (i % 2) * 16); 9103 } 9104 9105 return WordArray.create(words, utf16StrLength * 2); 9106 } 9107 }; 9108 9109 /** 9110 * UTF-16 LE encoding strategy. 9111 */ 9112 C_enc.Utf16LE = { 9113 /** 9114 * Converts a word array to a UTF-16 LE string. 9115 * 9116 * @param {WordArray} wordArray The word array. 9117 * 9118 * @return {string} The UTF-16 LE string. 9119 * 9120 * @static 9121 * 9122 * @example 9123 * 9124 * var utf16Str = CryptoJS.enc.Utf16LE.stringify(wordArray); 9125 */ 9126 stringify: function (wordArray) { 9127 // Shortcuts 9128 var words = wordArray.words; 9129 var sigBytes = wordArray.sigBytes; 9130 9131 // Convert 9132 var utf16Chars = []; 9133 for (var i = 0; i < sigBytes; i += 2) { 9134 var codePoint = swapEndian((words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff); 9135 utf16Chars.push(String.fromCharCode(codePoint)); 9136 } 9137 9138 return utf16Chars.join(''); 9139 }, 9140 9141 /** 9142 * Converts a UTF-16 LE string to a word array. 9143 * 9144 * @param {string} utf16Str The UTF-16 LE string. 9145 * 9146 * @return {WordArray} The word array. 9147 * 9148 * @static 9149 * 9150 * @example 9151 * 9152 * var wordArray = CryptoJS.enc.Utf16LE.parse(utf16Str); 9153 */ 9154 parse: function (utf16Str) { 9155 // Shortcut 9156 var utf16StrLength = utf16Str.length; 9157 9158 // Convert 9159 var words = []; 9160 for (var i = 0; i < utf16StrLength; i++) { 9161 words[i >>> 1] |= swapEndian(utf16Str.charCodeAt(i) << (16 - (i % 2) * 16)); 9162 } 9163 9164 return WordArray.create(words, utf16StrLength * 2); 9165 } 9166 }; 9167 9168 function swapEndian(word) { 9169 return ((word << 8) & 0xff00ff00) | ((word >>> 8) & 0x00ff00ff); 9170 } 9171 }()); 9172 9173 9174 return CryptoJS.enc.Utf16; 9175 9176 })); 9177 },{"./core":53}],56:[function(require,module,exports){ 9178 ;(function (root, factory, undef) { 9179 if (typeof exports === "object") { 9180 // CommonJS 9181 module.exports = exports = factory(require("./core"), require("./sha1"), require("./hmac")); 9182 } 9183 else if (typeof define === "function" && define.amd) { 9184 // AMD 9185 define(["./core", "./sha1", "./hmac"], factory); 9186 } 9187 else { 9188 // Global (browser) 9189 factory(root.CryptoJS); 9190 } 9191 }(this, function (CryptoJS) { 9192 9193 (function () { 9194 // Shortcuts 9195 var C = CryptoJS; 9196 var C_lib = C.lib; 9197 var Base = C_lib.Base; 9198 var WordArray = C_lib.WordArray; 9199 var C_algo = C.algo; 9200 var MD5 = C_algo.MD5; 9201 9202 /** 9203 * This key derivation function is meant to conform with EVP_BytesToKey. 9204 * www.openssl.org/docs/crypto/EVP_BytesToKey.html 9205 */ 9206 var EvpKDF = C_algo.EvpKDF = Base.extend({ 9207 /** 9208 * Configuration options. 9209 * 9210 * @property {number} keySize The key size in words to generate. Default: 4 (128 bits) 9211 * @property {Hasher} hasher The hash algorithm to use. Default: MD5 9212 * @property {number} iterations The number of iterations to perform. Default: 1 9213 */ 9214 cfg: Base.extend({ 9215 keySize: 128/32, 9216 hasher: MD5, 9217 iterations: 1 9218 }), 9219 9220 /** 9221 * Initializes a newly created key derivation function. 9222 * 9223 * @param {Object} cfg (Optional) The configuration options to use for the derivation. 9224 * 9225 * @example 9226 * 9227 * var kdf = CryptoJS.algo.EvpKDF.create(); 9228 * var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8 }); 9229 * var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8, iterations: 1000 }); 9230 */ 9231 init: function (cfg) { 9232 this.cfg = this.cfg.extend(cfg); 9233 }, 9234 9235 /** 9236 * Derives a key from a password. 9237 * 9238 * @param {WordArray|string} password The password. 9239 * @param {WordArray|string} salt A salt. 9240 * 9241 * @return {WordArray} The derived key. 9242 * 9243 * @example 9244 * 9245 * var key = kdf.compute(password, salt); 9246 */ 9247 compute: function (password, salt) { 9248 // Shortcut 9249 var cfg = this.cfg; 9250 9251 // Init hasher 9252 var hasher = cfg.hasher.create(); 9253 9254 // Initial values 9255 var derivedKey = WordArray.create(); 9256 9257 // Shortcuts 9258 var derivedKeyWords = derivedKey.words; 9259 var keySize = cfg.keySize; 9260 var iterations = cfg.iterations; 9261 9262 // Generate key 9263 while (derivedKeyWords.length < keySize) { 9264 if (block) { 9265 hasher.update(block); 9266 } 9267 var block = hasher.update(password).finalize(salt); 9268 hasher.reset(); 9269 9270 // Iterations 9271 for (var i = 1; i < iterations; i++) { 9272 block = hasher.finalize(block); 9273 hasher.reset(); 9274 } 9275 9276 derivedKey.concat(block); 9277 } 9278 derivedKey.sigBytes = keySize * 4; 9279 9280 return derivedKey; 9281 } 9282 }); 9283 9284 /** 9285 * Derives a key from a password. 9286 * 9287 * @param {WordArray|string} password The password. 9288 * @param {WordArray|string} salt A salt. 9289 * @param {Object} cfg (Optional) The configuration options to use for this computation. 9290 * 9291 * @return {WordArray} The derived key. 9292 * 9293 * @static 9294 * 9295 * @example 9296 * 9297 * var key = CryptoJS.EvpKDF(password, salt); 9298 * var key = CryptoJS.EvpKDF(password, salt, { keySize: 8 }); 9299 * var key = CryptoJS.EvpKDF(password, salt, { keySize: 8, iterations: 1000 }); 9300 */ 9301 C.EvpKDF = function (password, salt, cfg) { 9302 return EvpKDF.create(cfg).compute(password, salt); 9303 }; 9304 }()); 9305 9306 9307 return CryptoJS.EvpKDF; 9308 9309 })); 9310 },{"./core":53,"./hmac":58,"./sha1":77}],57:[function(require,module,exports){ 9311 ;(function (root, factory, undef) { 9312 if (typeof exports === "object") { 9313 // CommonJS 9314 module.exports = exports = factory(require("./core"), require("./cipher-core")); 9315 } 9316 else if (typeof define === "function" && define.amd) { 9317 // AMD 9318 define(["./core", "./cipher-core"], factory); 9319 } 9320 else { 9321 // Global (browser) 9322 factory(root.CryptoJS); 9323 } 9324 }(this, function (CryptoJS) { 9325 9326 (function (undefined) { 9327 // Shortcuts 9328 var C = CryptoJS; 9329 var C_lib = C.lib; 9330 var CipherParams = C_lib.CipherParams; 9331 var C_enc = C.enc; 9332 var Hex = C_enc.Hex; 9333 var C_format = C.format; 9334 9335 var HexFormatter = C_format.Hex = { 9336 /** 9337 * Converts the ciphertext of a cipher params object to a hexadecimally encoded string. 9338 * 9339 * @param {CipherParams} cipherParams The cipher params object. 9340 * 9341 * @return {string} The hexadecimally encoded string. 9342 * 9343 * @static 9344 * 9345 * @example 9346 * 9347 * var hexString = CryptoJS.format.Hex.stringify(cipherParams); 9348 */ 9349 stringify: function (cipherParams) { 9350 return cipherParams.ciphertext.toString(Hex); 9351 }, 9352 9353 /** 9354 * Converts a hexadecimally encoded ciphertext string to a cipher params object. 9355 * 9356 * @param {string} input The hexadecimally encoded string. 9357 * 9358 * @return {CipherParams} The cipher params object. 9359 * 9360 * @static 9361 * 9362 * @example 9363 * 9364 * var cipherParams = CryptoJS.format.Hex.parse(hexString); 9365 */ 9366 parse: function (input) { 9367 var ciphertext = Hex.parse(input); 9368 return CipherParams.create({ ciphertext: ciphertext }); 9369 } 9370 }; 9371 }()); 9372 9373 9374 return CryptoJS.format.Hex; 9375 9376 })); 9377 },{"./cipher-core":52,"./core":53}],58:[function(require,module,exports){ 9378 ;(function (root, factory) { 9379 if (typeof exports === "object") { 9380 // CommonJS 9381 module.exports = exports = factory(require("./core")); 9382 } 9383 else if (typeof define === "function" && define.amd) { 9384 // AMD 9385 define(["./core"], factory); 9386 } 9387 else { 9388 // Global (browser) 9389 factory(root.CryptoJS); 9390 } 9391 }(this, function (CryptoJS) { 9392 9393 (function () { 9394 // Shortcuts 9395 var C = CryptoJS; 9396 var C_lib = C.lib; 9397 var Base = C_lib.Base; 9398 var C_enc = C.enc; 9399 var Utf8 = C_enc.Utf8; 9400 var C_algo = C.algo; 9401 9402 /** 9403 * HMAC algorithm. 9404 */ 9405 var HMAC = C_algo.HMAC = Base.extend({ 9406 /** 9407 * Initializes a newly created HMAC. 9408 * 9409 * @param {Hasher} hasher The hash algorithm to use. 9410 * @param {WordArray|string} key The secret key. 9411 * 9412 * @example 9413 * 9414 * var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key); 9415 */ 9416 init: function (hasher, key) { 9417 // Init hasher 9418 hasher = this._hasher = new hasher.init(); 9419 9420 // Convert string to WordArray, else assume WordArray already 9421 if (typeof key == 'string') { 9422 key = Utf8.parse(key); 9423 } 9424 9425 // Shortcuts 9426 var hasherBlockSize = hasher.blockSize; 9427 var hasherBlockSizeBytes = hasherBlockSize * 4; 9428 9429 // Allow arbitrary length keys 9430 if (key.sigBytes > hasherBlockSizeBytes) { 9431 key = hasher.finalize(key); 9432 } 9433 9434 // Clamp excess bits 9435 key.clamp(); 9436 9437 // Clone key for inner and outer pads 9438 var oKey = this._oKey = key.clone(); 9439 var iKey = this._iKey = key.clone(); 9440 9441 // Shortcuts 9442 var oKeyWords = oKey.words; 9443 var iKeyWords = iKey.words; 9444 9445 // XOR keys with pad constants 9446 for (var i = 0; i < hasherBlockSize; i++) { 9447 oKeyWords[i] ^= 0x5c5c5c5c; 9448 iKeyWords[i] ^= 0x36363636; 9449 } 9450 oKey.sigBytes = iKey.sigBytes = hasherBlockSizeBytes; 9451 9452 // Set initial values 9453 this.reset(); 9454 }, 9455 9456 /** 9457 * Resets this HMAC to its initial state. 9458 * 9459 * @example 9460 * 9461 * hmacHasher.reset(); 9462 */ 9463 reset: function () { 9464 // Shortcut 9465 var hasher = this._hasher; 9466 9467 // Reset 9468 hasher.reset(); 9469 hasher.update(this._iKey); 9470 }, 9471 9472 /** 9473 * Updates this HMAC with a message. 9474 * 9475 * @param {WordArray|string} messageUpdate The message to append. 9476 * 9477 * @return {HMAC} This HMAC instance. 9478 * 9479 * @example 9480 * 9481 * hmacHasher.update('message'); 9482 * hmacHasher.update(wordArray); 9483 */ 9484 update: function (messageUpdate) { 9485 this._hasher.update(messageUpdate); 9486 9487 // Chainable 9488 return this; 9489 }, 9490 9491 /** 9492 * Finalizes the HMAC computation. 9493 * Note that the finalize operation is effectively a destructive, read-once operation. 9494 * 9495 * @param {WordArray|string} messageUpdate (Optional) A final message update. 9496 * 9497 * @return {WordArray} The HMAC. 9498 * 9499 * @example 9500 * 9501 * var hmac = hmacHasher.finalize(); 9502 * var hmac = hmacHasher.finalize('message'); 9503 * var hmac = hmacHasher.finalize(wordArray); 9504 */ 9505 finalize: function (messageUpdate) { 9506 // Shortcut 9507 var hasher = this._hasher; 9508 9509 // Compute HMAC 9510 var innerHash = hasher.finalize(messageUpdate); 9511 hasher.reset(); 9512 var hmac = hasher.finalize(this._oKey.clone().concat(innerHash)); 9513 9514 return hmac; 9515 } 9516 }); 9517 }()); 9518 9519 9520 })); 9521 },{"./core":53}],59:[function(require,module,exports){ 9522 ;(function (root, factory, undef) { 9523 if (typeof exports === "object") { 9524 // CommonJS 9525 module.exports = exports = factory(require("./core"), require("./x64-core"), require("./lib-typedarrays"), require("./enc-utf16"), require("./enc-base64"), require("./md5"), require("./sha1"), require("./sha256"), require("./sha224"), require("./sha512"), require("./sha384"), require("./sha3"), require("./ripemd160"), require("./hmac"), require("./pbkdf2"), require("./evpkdf"), require("./cipher-core"), require("./mode-cfb"), require("./mode-ctr"), require("./mode-ctr-gladman"), require("./mode-ofb"), require("./mode-ecb"), require("./pad-ansix923"), require("./pad-iso10126"), require("./pad-iso97971"), require("./pad-zeropadding"), require("./pad-nopadding"), require("./format-hex"), require("./aes"), require("./tripledes"), require("./rc4"), require("./rabbit"), require("./rabbit-legacy")); 9526 } 9527 else if (typeof define === "function" && define.amd) { 9528 // AMD 9529 define(["./core", "./x64-core", "./lib-typedarrays", "./enc-utf16", "./enc-base64", "./md5", "./sha1", "./sha256", "./sha224", "./sha512", "./sha384", "./sha3", "./ripemd160", "./hmac", "./pbkdf2", "./evpkdf", "./cipher-core", "./mode-cfb", "./mode-ctr", "./mode-ctr-gladman", "./mode-ofb", "./mode-ecb", "./pad-ansix923", "./pad-iso10126", "./pad-iso97971", "./pad-zeropadding", "./pad-nopadding", "./format-hex", "./aes", "./tripledes", "./rc4", "./rabbit", "./rabbit-legacy"], factory); 9530 } 9531 else { 9532 // Global (browser) 9533 root.CryptoJS = factory(root.CryptoJS); 9534 } 9535 }(this, function (CryptoJS) { 9536 9537 return CryptoJS; 9538 9539 })); 9540 },{"./aes":51,"./cipher-core":52,"./core":53,"./enc-base64":54,"./enc-utf16":55,"./evpkdf":56,"./format-hex":57,"./hmac":58,"./lib-typedarrays":60,"./md5":61,"./mode-cfb":62,"./mode-ctr":64,"./mode-ctr-gladman":63,"./mode-ecb":65,"./mode-ofb":66,"./pad-ansix923":67,"./pad-iso10126":68,"./pad-iso97971":69,"./pad-nopadding":70,"./pad-zeropadding":71,"./pbkdf2":72,"./rabbit":74,"./rabbit-legacy":73,"./rc4":75,"./ripemd160":76,"./sha1":77,"./sha224":78,"./sha256":79,"./sha3":80,"./sha384":81,"./sha512":82,"./tripledes":83,"./x64-core":84}],60:[function(require,module,exports){ 9541 ;(function (root, factory) { 9542 if (typeof exports === "object") { 9543 // CommonJS 9544 module.exports = exports = factory(require("./core")); 9545 } 9546 else if (typeof define === "function" && define.amd) { 9547 // AMD 9548 define(["./core"], factory); 9549 } 9550 else { 9551 // Global (browser) 9552 factory(root.CryptoJS); 9553 } 9554 }(this, function (CryptoJS) { 9555 9556 (function () { 9557 // Check if typed arrays are supported 9558 if (typeof ArrayBuffer != 'function') { 9559 return; 9560 } 9561 9562 // Shortcuts 9563 var C = CryptoJS; 9564 var C_lib = C.lib; 9565 var WordArray = C_lib.WordArray; 9566 9567 // Reference original init 9568 var superInit = WordArray.init; 9569 9570 // Augment WordArray.init to handle typed arrays 9571 var subInit = WordArray.init = function (typedArray) { 9572 // Convert buffers to uint8 9573 if (typedArray instanceof ArrayBuffer) { 9574 typedArray = new Uint8Array(typedArray); 9575 } 9576 9577 // Convert other array views to uint8 9578 if ( 9579 typedArray instanceof Int8Array || 9580 (typeof Uint8ClampedArray !== "undefined" && typedArray instanceof Uint8ClampedArray) || 9581 typedArray instanceof Int16Array || 9582 typedArray instanceof Uint16Array || 9583 typedArray instanceof Int32Array || 9584 typedArray instanceof Uint32Array || 9585 typedArray instanceof Float32Array || 9586 typedArray instanceof Float64Array 9587 ) { 9588 typedArray = new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength); 9589 } 9590 9591 // Handle Uint8Array 9592 if (typedArray instanceof Uint8Array) { 9593 // Shortcut 9594 var typedArrayByteLength = typedArray.byteLength; 9595 9596 // Extract bytes 9597 var words = []; 9598 for (var i = 0; i < typedArrayByteLength; i++) { 9599 words[i >>> 2] |= typedArray[i] << (24 - (i % 4) * 8); 9600 } 9601 9602 // Initialize this word array 9603 superInit.call(this, words, typedArrayByteLength); 9604 } else { 9605 // Else call normal init 9606 superInit.apply(this, arguments); 9607 } 9608 }; 9609 9610 subInit.prototype = WordArray; 9611 }()); 9612 9613 9614 return CryptoJS.lib.WordArray; 9615 9616 })); 9617 },{"./core":53}],61:[function(require,module,exports){ 9618 ;(function (root, factory) { 9619 if (typeof exports === "object") { 9620 // CommonJS 9621 module.exports = exports = factory(require("./core")); 9622 } 9623 else if (typeof define === "function" && define.amd) { 9624 // AMD 9625 define(["./core"], factory); 9626 } 9627 else { 9628 // Global (browser) 9629 factory(root.CryptoJS); 9630 } 9631 }(this, function (CryptoJS) { 9632 9633 (function (Math) { 9634 // Shortcuts 9635 var C = CryptoJS; 9636 var C_lib = C.lib; 9637 var WordArray = C_lib.WordArray; 9638 var Hasher = C_lib.Hasher; 9639 var C_algo = C.algo; 9640 9641 // Constants table 9642 var T = []; 9643 9644 // Compute constants 9645 (function () { 9646 for (var i = 0; i < 64; i++) { 9647 T[i] = (Math.abs(Math.sin(i + 1)) * 0x100000000) | 0; 9648 } 9649 }()); 9650 9651 /** 9652 * MD5 hash algorithm. 9653 */ 9654 var MD5 = C_algo.MD5 = Hasher.extend({ 9655 _doReset: function () { 9656 this._hash = new WordArray.init([ 9657 0x67452301, 0xefcdab89, 9658 0x98badcfe, 0x10325476 9659 ]); 9660 }, 9661 9662 _doProcessBlock: function (M, offset) { 9663 // Swap endian 9664 for (var i = 0; i < 16; i++) { 9665 // Shortcuts 9666 var offset_i = offset + i; 9667 var M_offset_i = M[offset_i]; 9668 9669 M[offset_i] = ( 9670 (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) | 9671 (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00) 9672 ); 9673 } 9674 9675 // Shortcuts 9676 var H = this._hash.words; 9677 9678 var M_offset_0 = M[offset + 0]; 9679 var M_offset_1 = M[offset + 1]; 9680 var M_offset_2 = M[offset + 2]; 9681 var M_offset_3 = M[offset + 3]; 9682 var M_offset_4 = M[offset + 4]; 9683 var M_offset_5 = M[offset + 5]; 9684 var M_offset_6 = M[offset + 6]; 9685 var M_offset_7 = M[offset + 7]; 9686 var M_offset_8 = M[offset + 8]; 9687 var M_offset_9 = M[offset + 9]; 9688 var M_offset_10 = M[offset + 10]; 9689 var M_offset_11 = M[offset + 11]; 9690 var M_offset_12 = M[offset + 12]; 9691 var M_offset_13 = M[offset + 13]; 9692 var M_offset_14 = M[offset + 14]; 9693 var M_offset_15 = M[offset + 15]; 9694 9695 // Working varialbes 9696 var a = H[0]; 9697 var b = H[1]; 9698 var c = H[2]; 9699 var d = H[3]; 9700 9701 // Computation 9702 a = FF(a, b, c, d, M_offset_0, 7, T[0]); 9703 d = FF(d, a, b, c, M_offset_1, 12, T[1]); 9704 c = FF(c, d, a, b, M_offset_2, 17, T[2]); 9705 b = FF(b, c, d, a, M_offset_3, 22, T[3]); 9706 a = FF(a, b, c, d, M_offset_4, 7, T[4]); 9707 d = FF(d, a, b, c, M_offset_5, 12, T[5]); 9708 c = FF(c, d, a, b, M_offset_6, 17, T[6]); 9709 b = FF(b, c, d, a, M_offset_7, 22, T[7]); 9710 a = FF(a, b, c, d, M_offset_8, 7, T[8]); 9711 d = FF(d, a, b, c, M_offset_9, 12, T[9]); 9712 c = FF(c, d, a, b, M_offset_10, 17, T[10]); 9713 b = FF(b, c, d, a, M_offset_11, 22, T[11]); 9714 a = FF(a, b, c, d, M_offset_12, 7, T[12]); 9715 d = FF(d, a, b, c, M_offset_13, 12, T[13]); 9716 c = FF(c, d, a, b, M_offset_14, 17, T[14]); 9717 b = FF(b, c, d, a, M_offset_15, 22, T[15]); 9718 9719 a = GG(a, b, c, d, M_offset_1, 5, T[16]); 9720 d = GG(d, a, b, c, M_offset_6, 9, T[17]); 9721 c = GG(c, d, a, b, M_offset_11, 14, T[18]); 9722 b = GG(b, c, d, a, M_offset_0, 20, T[19]); 9723 a = GG(a, b, c, d, M_offset_5, 5, T[20]); 9724 d = GG(d, a, b, c, M_offset_10, 9, T[21]); 9725 c = GG(c, d, a, b, M_offset_15, 14, T[22]); 9726 b = GG(b, c, d, a, M_offset_4, 20, T[23]); 9727 a = GG(a, b, c, d, M_offset_9, 5, T[24]); 9728 d = GG(d, a, b, c, M_offset_14, 9, T[25]); 9729 c = GG(c, d, a, b, M_offset_3, 14, T[26]); 9730 b = GG(b, c, d, a, M_offset_8, 20, T[27]); 9731 a = GG(a, b, c, d, M_offset_13, 5, T[28]); 9732 d = GG(d, a, b, c, M_offset_2, 9, T[29]); 9733 c = GG(c, d, a, b, M_offset_7, 14, T[30]); 9734 b = GG(b, c, d, a, M_offset_12, 20, T[31]); 9735 9736 a = HH(a, b, c, d, M_offset_5, 4, T[32]); 9737 d = HH(d, a, b, c, M_offset_8, 11, T[33]); 9738 c = HH(c, d, a, b, M_offset_11, 16, T[34]); 9739 b = HH(b, c, d, a, M_offset_14, 23, T[35]); 9740 a = HH(a, b, c, d, M_offset_1, 4, T[36]); 9741 d = HH(d, a, b, c, M_offset_4, 11, T[37]); 9742 c = HH(c, d, a, b, M_offset_7, 16, T[38]); 9743 b = HH(b, c, d, a, M_offset_10, 23, T[39]); 9744 a = HH(a, b, c, d, M_offset_13, 4, T[40]); 9745 d = HH(d, a, b, c, M_offset_0, 11, T[41]); 9746 c = HH(c, d, a, b, M_offset_3, 16, T[42]); 9747 b = HH(b, c, d, a, M_offset_6, 23, T[43]); 9748 a = HH(a, b, c, d, M_offset_9, 4, T[44]); 9749 d = HH(d, a, b, c, M_offset_12, 11, T[45]); 9750 c = HH(c, d, a, b, M_offset_15, 16, T[46]); 9751 b = HH(b, c, d, a, M_offset_2, 23, T[47]); 9752 9753 a = II(a, b, c, d, M_offset_0, 6, T[48]); 9754 d = II(d, a, b, c, M_offset_7, 10, T[49]); 9755 c = II(c, d, a, b, M_offset_14, 15, T[50]); 9756 b = II(b, c, d, a, M_offset_5, 21, T[51]); 9757 a = II(a, b, c, d, M_offset_12, 6, T[52]); 9758 d = II(d, a, b, c, M_offset_3, 10, T[53]); 9759 c = II(c, d, a, b, M_offset_10, 15, T[54]); 9760 b = II(b, c, d, a, M_offset_1, 21, T[55]); 9761 a = II(a, b, c, d, M_offset_8, 6, T[56]); 9762 d = II(d, a, b, c, M_offset_15, 10, T[57]); 9763 c = II(c, d, a, b, M_offset_6, 15, T[58]); 9764 b = II(b, c, d, a, M_offset_13, 21, T[59]); 9765 a = II(a, b, c, d, M_offset_4, 6, T[60]); 9766 d = II(d, a, b, c, M_offset_11, 10, T[61]); 9767 c = II(c, d, a, b, M_offset_2, 15, T[62]); 9768 b = II(b, c, d, a, M_offset_9, 21, T[63]); 9769 9770 // Intermediate hash value 9771 H[0] = (H[0] + a) | 0; 9772 H[1] = (H[1] + b) | 0; 9773 H[2] = (H[2] + c) | 0; 9774 H[3] = (H[3] + d) | 0; 9775 }, 9776 9777 _doFinalize: function () { 9778 // Shortcuts 9779 var data = this._data; 9780 var dataWords = data.words; 9781 9782 var nBitsTotal = this._nDataBytes * 8; 9783 var nBitsLeft = data.sigBytes * 8; 9784 9785 // Add padding 9786 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32); 9787 9788 var nBitsTotalH = Math.floor(nBitsTotal / 0x100000000); 9789 var nBitsTotalL = nBitsTotal; 9790 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = ( 9791 (((nBitsTotalH << 8) | (nBitsTotalH >>> 24)) & 0x00ff00ff) | 9792 (((nBitsTotalH << 24) | (nBitsTotalH >>> 8)) & 0xff00ff00) 9793 ); 9794 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = ( 9795 (((nBitsTotalL << 8) | (nBitsTotalL >>> 24)) & 0x00ff00ff) | 9796 (((nBitsTotalL << 24) | (nBitsTotalL >>> 8)) & 0xff00ff00) 9797 ); 9798 9799 data.sigBytes = (dataWords.length + 1) * 4; 9800 9801 // Hash final blocks 9802 this._process(); 9803 9804 // Shortcuts 9805 var hash = this._hash; 9806 var H = hash.words; 9807 9808 // Swap endian 9809 for (var i = 0; i < 4; i++) { 9810 // Shortcut 9811 var H_i = H[i]; 9812 9813 H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) | 9814 (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00); 9815 } 9816 9817 // Return final computed hash 9818 return hash; 9819 }, 9820 9821 clone: function () { 9822 var clone = Hasher.clone.call(this); 9823 clone._hash = this._hash.clone(); 9824 9825 return clone; 9826 } 9827 }); 9828 9829 function FF(a, b, c, d, x, s, t) { 9830 var n = a + ((b & c) | (~b & d)) + x + t; 9831 return ((n << s) | (n >>> (32 - s))) + b; 9832 } 9833 9834 function GG(a, b, c, d, x, s, t) { 9835 var n = a + ((b & d) | (c & ~d)) + x + t; 9836 return ((n << s) | (n >>> (32 - s))) + b; 9837 } 9838 9839 function HH(a, b, c, d, x, s, t) { 9840 var n = a + (b ^ c ^ d) + x + t; 9841 return ((n << s) | (n >>> (32 - s))) + b; 9842 } 9843 9844 function II(a, b, c, d, x, s, t) { 9845 var n = a + (c ^ (b | ~d)) + x + t; 9846 return ((n << s) | (n >>> (32 - s))) + b; 9847 } 9848 9849 /** 9850 * Shortcut function to the hasher's object interface. 9851 * 9852 * @param {WordArray|string} message The message to hash. 9853 * 9854 * @return {WordArray} The hash. 9855 * 9856 * @static 9857 * 9858 * @example 9859 * 9860 * var hash = CryptoJS.MD5('message'); 9861 * var hash = CryptoJS.MD5(wordArray); 9862 */ 9863 C.MD5 = Hasher._createHelper(MD5); 9864 9865 /** 9866 * Shortcut function to the HMAC's object interface. 9867 * 9868 * @param {WordArray|string} message The message to hash. 9869 * @param {WordArray|string} key The secret key. 9870 * 9871 * @return {WordArray} The HMAC. 9872 * 9873 * @static 9874 * 9875 * @example 9876 * 9877 * var hmac = CryptoJS.HmacMD5(message, key); 9878 */ 9879 C.HmacMD5 = Hasher._createHmacHelper(MD5); 9880 }(Math)); 9881 9882 9883 return CryptoJS.MD5; 9884 9885 })); 9886 },{"./core":53}],62:[function(require,module,exports){ 9887 ;(function (root, factory, undef) { 9888 if (typeof exports === "object") { 9889 // CommonJS 9890 module.exports = exports = factory(require("./core"), require("./cipher-core")); 9891 } 9892 else if (typeof define === "function" && define.amd) { 9893 // AMD 9894 define(["./core", "./cipher-core"], factory); 9895 } 9896 else { 9897 // Global (browser) 9898 factory(root.CryptoJS); 9899 } 9900 }(this, function (CryptoJS) { 9901 9902 /** 9903 * Cipher Feedback block mode. 9904 */ 9905 CryptoJS.mode.CFB = (function () { 9906 var CFB = CryptoJS.lib.BlockCipherMode.extend(); 9907 9908 CFB.Encryptor = CFB.extend({ 9909 processBlock: function (words, offset) { 9910 // Shortcuts 9911 var cipher = this._cipher; 9912 var blockSize = cipher.blockSize; 9913 9914 generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher); 9915 9916 // Remember this block to use with next block 9917 this._prevBlock = words.slice(offset, offset + blockSize); 9918 } 9919 }); 9920 9921 CFB.Decryptor = CFB.extend({ 9922 processBlock: function (words, offset) { 9923 // Shortcuts 9924 var cipher = this._cipher; 9925 var blockSize = cipher.blockSize; 9926 9927 // Remember this block to use with next block 9928 var thisBlock = words.slice(offset, offset + blockSize); 9929 9930 generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher); 9931 9932 // This block becomes the previous block 9933 this._prevBlock = thisBlock; 9934 } 9935 }); 9936 9937 function generateKeystreamAndEncrypt(words, offset, blockSize, cipher) { 9938 // Shortcut 9939 var iv = this._iv; 9940 9941 // Generate keystream 9942 if (iv) { 9943 var keystream = iv.slice(0); 9944 9945 // Remove IV for subsequent blocks 9946 this._iv = undefined; 9947 } else { 9948 var keystream = this._prevBlock; 9949 } 9950 cipher.encryptBlock(keystream, 0); 9951 9952 // Encrypt 9953 for (var i = 0; i < blockSize; i++) { 9954 words[offset + i] ^= keystream[i]; 9955 } 9956 } 9957 9958 return CFB; 9959 }()); 9960 9961 9962 return CryptoJS.mode.CFB; 9963 9964 })); 9965 },{"./cipher-core":52,"./core":53}],63:[function(require,module,exports){ 9966 ;(function (root, factory, undef) { 9967 if (typeof exports === "object") { 9968 // CommonJS 9969 module.exports = exports = factory(require("./core"), require("./cipher-core")); 9970 } 9971 else if (typeof define === "function" && define.amd) { 9972 // AMD 9973 define(["./core", "./cipher-core"], factory); 9974 } 9975 else { 9976 // Global (browser) 9977 factory(root.CryptoJS); 9978 } 9979 }(this, function (CryptoJS) { 9980 9981 /** @preserve 9982 * Counter block mode compatible with Dr Brian Gladman fileenc.c 9983 * derived from CryptoJS.mode.CTR 9984 * Jan Hruby jhruby.web@gmail.com 9985 */ 9986 CryptoJS.mode.CTRGladman = (function () { 9987 var CTRGladman = CryptoJS.lib.BlockCipherMode.extend(); 9988 9989 function incWord(word) 9990 { 9991 if (((word >> 24) & 0xff) === 0xff) { //overflow 9992 var b1 = (word >> 16)&0xff; 9993 var b2 = (word >> 8)&0xff; 9994 var b3 = word & 0xff; 9995 9996 if (b1 === 0xff) // overflow b1 9997 { 9998 b1 = 0; 9999 if (b2 === 0xff) 10000 { 10001 b2 = 0; 10002 if (b3 === 0xff) 10003 { 10004 b3 = 0; 10005 } 10006 else 10007 { 10008 ++b3; 10009 } 10010 } 10011 else 10012 { 10013 ++b2; 10014 } 10015 } 10016 else 10017 { 10018 ++b1; 10019 } 10020 10021 word = 0; 10022 word += (b1 << 16); 10023 word += (b2 << 8); 10024 word += b3; 10025 } 10026 else 10027 { 10028 word += (0x01 << 24); 10029 } 10030 return word; 10031 } 10032 10033 function incCounter(counter) 10034 { 10035 if ((counter[0] = incWord(counter[0])) === 0) 10036 { 10037 // encr_data in fileenc.c from Dr Brian Gladman's counts only with DWORD j < 8 10038 counter[1] = incWord(counter[1]); 10039 } 10040 return counter; 10041 } 10042 10043 var Encryptor = CTRGladman.Encryptor = CTRGladman.extend({ 10044 processBlock: function (words, offset) { 10045 // Shortcuts 10046 var cipher = this._cipher 10047 var blockSize = cipher.blockSize; 10048 var iv = this._iv; 10049 var counter = this._counter; 10050 10051 // Generate keystream 10052 if (iv) { 10053 counter = this._counter = iv.slice(0); 10054 10055 // Remove IV for subsequent blocks 10056 this._iv = undefined; 10057 } 10058 10059 incCounter(counter); 10060 10061 var keystream = counter.slice(0); 10062 cipher.encryptBlock(keystream, 0); 10063 10064 // Encrypt 10065 for (var i = 0; i < blockSize; i++) { 10066 words[offset + i] ^= keystream[i]; 10067 } 10068 } 10069 }); 10070 10071 CTRGladman.Decryptor = Encryptor; 10072 10073 return CTRGladman; 10074 }()); 10075 10076 10077 10078 10079 return CryptoJS.mode.CTRGladman; 10080 10081 })); 10082 },{"./cipher-core":52,"./core":53}],64:[function(require,module,exports){ 10083 ;(function (root, factory, undef) { 10084 if (typeof exports === "object") { 10085 // CommonJS 10086 module.exports = exports = factory(require("./core"), require("./cipher-core")); 10087 } 10088 else if (typeof define === "function" && define.amd) { 10089 // AMD 10090 define(["./core", "./cipher-core"], factory); 10091 } 10092 else { 10093 // Global (browser) 10094 factory(root.CryptoJS); 10095 } 10096 }(this, function (CryptoJS) { 10097 10098 /** 10099 * Counter block mode. 10100 */ 10101 CryptoJS.mode.CTR = (function () { 10102 var CTR = CryptoJS.lib.BlockCipherMode.extend(); 10103 10104 var Encryptor = CTR.Encryptor = CTR.extend({ 10105 processBlock: function (words, offset) { 10106 // Shortcuts 10107 var cipher = this._cipher 10108 var blockSize = cipher.blockSize; 10109 var iv = this._iv; 10110 var counter = this._counter; 10111 10112 // Generate keystream 10113 if (iv) { 10114 counter = this._counter = iv.slice(0); 10115 10116 // Remove IV for subsequent blocks 10117 this._iv = undefined; 10118 } 10119 var keystream = counter.slice(0); 10120 cipher.encryptBlock(keystream, 0); 10121 10122 // Increment counter 10123 counter[blockSize - 1] = (counter[blockSize - 1] + 1) | 0 10124 10125 // Encrypt 10126 for (var i = 0; i < blockSize; i++) { 10127 words[offset + i] ^= keystream[i]; 10128 } 10129 } 10130 }); 10131 10132 CTR.Decryptor = Encryptor; 10133 10134 return CTR; 10135 }()); 10136 10137 10138 return CryptoJS.mode.CTR; 10139 10140 })); 10141 },{"./cipher-core":52,"./core":53}],65:[function(require,module,exports){ 10142 ;(function (root, factory, undef) { 10143 if (typeof exports === "object") { 10144 // CommonJS 10145 module.exports = exports = factory(require("./core"), require("./cipher-core")); 10146 } 10147 else if (typeof define === "function" && define.amd) { 10148 // AMD 10149 define(["./core", "./cipher-core"], factory); 10150 } 10151 else { 10152 // Global (browser) 10153 factory(root.CryptoJS); 10154 } 10155 }(this, function (CryptoJS) { 10156 10157 /** 10158 * Electronic Codebook block mode. 10159 */ 10160 CryptoJS.mode.ECB = (function () { 10161 var ECB = CryptoJS.lib.BlockCipherMode.extend(); 10162 10163 ECB.Encryptor = ECB.extend({ 10164 processBlock: function (words, offset) { 10165 this._cipher.encryptBlock(words, offset); 10166 } 10167 }); 10168 10169 ECB.Decryptor = ECB.extend({ 10170 processBlock: function (words, offset) { 10171 this._cipher.decryptBlock(words, offset); 10172 } 10173 }); 10174 10175 return ECB; 10176 }()); 10177 10178 10179 return CryptoJS.mode.ECB; 10180 10181 })); 10182 },{"./cipher-core":52,"./core":53}],66:[function(require,module,exports){ 10183 ;(function (root, factory, undef) { 10184 if (typeof exports === "object") { 10185 // CommonJS 10186 module.exports = exports = factory(require("./core"), require("./cipher-core")); 10187 } 10188 else if (typeof define === "function" && define.amd) { 10189 // AMD 10190 define(["./core", "./cipher-core"], factory); 10191 } 10192 else { 10193 // Global (browser) 10194 factory(root.CryptoJS); 10195 } 10196 }(this, function (CryptoJS) { 10197 10198 /** 10199 * Output Feedback block mode. 10200 */ 10201 CryptoJS.mode.OFB = (function () { 10202 var OFB = CryptoJS.lib.BlockCipherMode.extend(); 10203 10204 var Encryptor = OFB.Encryptor = OFB.extend({ 10205 processBlock: function (words, offset) { 10206 // Shortcuts 10207 var cipher = this._cipher 10208 var blockSize = cipher.blockSize; 10209 var iv = this._iv; 10210 var keystream = this._keystream; 10211 10212 // Generate keystream 10213 if (iv) { 10214 keystream = this._keystream = iv.slice(0); 10215 10216 // Remove IV for subsequent blocks 10217 this._iv = undefined; 10218 } 10219 cipher.encryptBlock(keystream, 0); 10220 10221 // Encrypt 10222 for (var i = 0; i < blockSize; i++) { 10223 words[offset + i] ^= keystream[i]; 10224 } 10225 } 10226 }); 10227 10228 OFB.Decryptor = Encryptor; 10229 10230 return OFB; 10231 }()); 10232 10233 10234 return CryptoJS.mode.OFB; 10235 10236 })); 10237 },{"./cipher-core":52,"./core":53}],67:[function(require,module,exports){ 10238 ;(function (root, factory, undef) { 10239 if (typeof exports === "object") { 10240 // CommonJS 10241 module.exports = exports = factory(require("./core"), require("./cipher-core")); 10242 } 10243 else if (typeof define === "function" && define.amd) { 10244 // AMD 10245 define(["./core", "./cipher-core"], factory); 10246 } 10247 else { 10248 // Global (browser) 10249 factory(root.CryptoJS); 10250 } 10251 }(this, function (CryptoJS) { 10252 10253 /** 10254 * ANSI X.923 padding strategy. 10255 */ 10256 CryptoJS.pad.AnsiX923 = { 10257 pad: function (data, blockSize) { 10258 // Shortcuts 10259 var dataSigBytes = data.sigBytes; 10260 var blockSizeBytes = blockSize * 4; 10261 10262 // Count padding bytes 10263 var nPaddingBytes = blockSizeBytes - dataSigBytes % blockSizeBytes; 10264 10265 // Compute last byte position 10266 var lastBytePos = dataSigBytes + nPaddingBytes - 1; 10267 10268 // Pad 10269 data.clamp(); 10270 data.words[lastBytePos >>> 2] |= nPaddingBytes << (24 - (lastBytePos % 4) * 8); 10271 data.sigBytes += nPaddingBytes; 10272 }, 10273 10274 unpad: function (data) { 10275 // Get number of padding bytes from last byte 10276 var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff; 10277 10278 // Remove padding 10279 data.sigBytes -= nPaddingBytes; 10280 } 10281 }; 10282 10283 10284 return CryptoJS.pad.Ansix923; 10285 10286 })); 10287 },{"./cipher-core":52,"./core":53}],68:[function(require,module,exports){ 10288 ;(function (root, factory, undef) { 10289 if (typeof exports === "object") { 10290 // CommonJS 10291 module.exports = exports = factory(require("./core"), require("./cipher-core")); 10292 } 10293 else if (typeof define === "function" && define.amd) { 10294 // AMD 10295 define(["./core", "./cipher-core"], factory); 10296 } 10297 else { 10298 // Global (browser) 10299 factory(root.CryptoJS); 10300 } 10301 }(this, function (CryptoJS) { 10302 10303 /** 10304 * ISO 10126 padding strategy. 10305 */ 10306 CryptoJS.pad.Iso10126 = { 10307 pad: function (data, blockSize) { 10308 // Shortcut 10309 var blockSizeBytes = blockSize * 4; 10310 10311 // Count padding bytes 10312 var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes; 10313 10314 // Pad 10315 data.concat(CryptoJS.lib.WordArray.random(nPaddingBytes - 1)). 10316 concat(CryptoJS.lib.WordArray.create([nPaddingBytes << 24], 1)); 10317 }, 10318 10319 unpad: function (data) { 10320 // Get number of padding bytes from last byte 10321 var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff; 10322 10323 // Remove padding 10324 data.sigBytes -= nPaddingBytes; 10325 } 10326 }; 10327 10328 10329 return CryptoJS.pad.Iso10126; 10330 10331 })); 10332 },{"./cipher-core":52,"./core":53}],69:[function(require,module,exports){ 10333 ;(function (root, factory, undef) { 10334 if (typeof exports === "object") { 10335 // CommonJS 10336 module.exports = exports = factory(require("./core"), require("./cipher-core")); 10337 } 10338 else if (typeof define === "function" && define.amd) { 10339 // AMD 10340 define(["./core", "./cipher-core"], factory); 10341 } 10342 else { 10343 // Global (browser) 10344 factory(root.CryptoJS); 10345 } 10346 }(this, function (CryptoJS) { 10347 10348 /** 10349 * ISO/IEC 9797-1 Padding Method 2. 10350 */ 10351 CryptoJS.pad.Iso97971 = { 10352 pad: function (data, blockSize) { 10353 // Add 0x80 byte 10354 data.concat(CryptoJS.lib.WordArray.create([0x80000000], 1)); 10355 10356 // Zero pad the rest 10357 CryptoJS.pad.ZeroPadding.pad(data, blockSize); 10358 }, 10359 10360 unpad: function (data) { 10361 // Remove zero padding 10362 CryptoJS.pad.ZeroPadding.unpad(data); 10363 10364 // Remove one more byte -- the 0x80 byte 10365 data.sigBytes--; 10366 } 10367 }; 10368 10369 10370 return CryptoJS.pad.Iso97971; 10371 10372 })); 10373 },{"./cipher-core":52,"./core":53}],70:[function(require,module,exports){ 10374 ;(function (root, factory, undef) { 10375 if (typeof exports === "object") { 10376 // CommonJS 10377 module.exports = exports = factory(require("./core"), require("./cipher-core")); 10378 } 10379 else if (typeof define === "function" && define.amd) { 10380 // AMD 10381 define(["./core", "./cipher-core"], factory); 10382 } 10383 else { 10384 // Global (browser) 10385 factory(root.CryptoJS); 10386 } 10387 }(this, function (CryptoJS) { 10388 10389 /** 10390 * A noop padding strategy. 10391 */ 10392 CryptoJS.pad.NoPadding = { 10393 pad: function () { 10394 }, 10395 10396 unpad: function () { 10397 } 10398 }; 10399 10400 10401 return CryptoJS.pad.NoPadding; 10402 10403 })); 10404 },{"./cipher-core":52,"./core":53}],71:[function(require,module,exports){ 10405 ;(function (root, factory, undef) { 10406 if (typeof exports === "object") { 10407 // CommonJS 10408 module.exports = exports = factory(require("./core"), require("./cipher-core")); 10409 } 10410 else if (typeof define === "function" && define.amd) { 10411 // AMD 10412 define(["./core", "./cipher-core"], factory); 10413 } 10414 else { 10415 // Global (browser) 10416 factory(root.CryptoJS); 10417 } 10418 }(this, function (CryptoJS) { 10419 10420 /** 10421 * Zero padding strategy. 10422 */ 10423 CryptoJS.pad.ZeroPadding = { 10424 pad: function (data, blockSize) { 10425 // Shortcut 10426 var blockSizeBytes = blockSize * 4; 10427 10428 // Pad 10429 data.clamp(); 10430 data.sigBytes += blockSizeBytes - ((data.sigBytes % blockSizeBytes) || blockSizeBytes); 10431 }, 10432 10433 unpad: function (data) { 10434 // Shortcut 10435 var dataWords = data.words; 10436 10437 // Unpad 10438 var i = data.sigBytes - 1; 10439 while (!((dataWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff)) { 10440 i--; 10441 } 10442 data.sigBytes = i + 1; 10443 } 10444 }; 10445 10446 10447 return CryptoJS.pad.ZeroPadding; 10448 10449 })); 10450 },{"./cipher-core":52,"./core":53}],72:[function(require,module,exports){ 10451 ;(function (root, factory, undef) { 10452 if (typeof exports === "object") { 10453 // CommonJS 10454 module.exports = exports = factory(require("./core"), require("./sha1"), require("./hmac")); 10455 } 10456 else if (typeof define === "function" && define.amd) { 10457 // AMD 10458 define(["./core", "./sha1", "./hmac"], factory); 10459 } 10460 else { 10461 // Global (browser) 10462 factory(root.CryptoJS); 10463 } 10464 }(this, function (CryptoJS) { 10465 10466 (function () { 10467 // Shortcuts 10468 var C = CryptoJS; 10469 var C_lib = C.lib; 10470 var Base = C_lib.Base; 10471 var WordArray = C_lib.WordArray; 10472 var C_algo = C.algo; 10473 var SHA1 = C_algo.SHA1; 10474 var HMAC = C_algo.HMAC; 10475 10476 /** 10477 * Password-Based Key Derivation Function 2 algorithm. 10478 */ 10479 var PBKDF2 = C_algo.PBKDF2 = Base.extend({ 10480 /** 10481 * Configuration options. 10482 * 10483 * @property {number} keySize The key size in words to generate. Default: 4 (128 bits) 10484 * @property {Hasher} hasher The hasher to use. Default: SHA1 10485 * @property {number} iterations The number of iterations to perform. Default: 1 10486 */ 10487 cfg: Base.extend({ 10488 keySize: 128/32, 10489 hasher: SHA1, 10490 iterations: 1 10491 }), 10492 10493 /** 10494 * Initializes a newly created key derivation function. 10495 * 10496 * @param {Object} cfg (Optional) The configuration options to use for the derivation. 10497 * 10498 * @example 10499 * 10500 * var kdf = CryptoJS.algo.PBKDF2.create(); 10501 * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8 }); 10502 * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8, iterations: 1000 }); 10503 */ 10504 init: function (cfg) { 10505 this.cfg = this.cfg.extend(cfg); 10506 }, 10507 10508 /** 10509 * Computes the Password-Based Key Derivation Function 2. 10510 * 10511 * @param {WordArray|string} password The password. 10512 * @param {WordArray|string} salt A salt. 10513 * 10514 * @return {WordArray} The derived key. 10515 * 10516 * @example 10517 * 10518 * var key = kdf.compute(password, salt); 10519 */ 10520 compute: function (password, salt) { 10521 // Shortcut 10522 var cfg = this.cfg; 10523 10524 // Init HMAC 10525 var hmac = HMAC.create(cfg.hasher, password); 10526 10527 // Initial values 10528 var derivedKey = WordArray.create(); 10529 var blockIndex = WordArray.create([0x00000001]); 10530 10531 // Shortcuts 10532 var derivedKeyWords = derivedKey.words; 10533 var blockIndexWords = blockIndex.words; 10534 var keySize = cfg.keySize; 10535 var iterations = cfg.iterations; 10536 10537 // Generate key 10538 while (derivedKeyWords.length < keySize) { 10539 var block = hmac.update(salt).finalize(blockIndex); 10540 hmac.reset(); 10541 10542 // Shortcuts 10543 var blockWords = block.words; 10544 var blockWordsLength = blockWords.length; 10545 10546 // Iterations 10547 var intermediate = block; 10548 for (var i = 1; i < iterations; i++) { 10549 intermediate = hmac.finalize(intermediate); 10550 hmac.reset(); 10551 10552 // Shortcut 10553 var intermediateWords = intermediate.words; 10554 10555 // XOR intermediate with block 10556 for (var j = 0; j < blockWordsLength; j++) { 10557 blockWords[j] ^= intermediateWords[j]; 10558 } 10559 } 10560 10561 derivedKey.concat(block); 10562 blockIndexWords[0]++; 10563 } 10564 derivedKey.sigBytes = keySize * 4; 10565 10566 return derivedKey; 10567 } 10568 }); 10569 10570 /** 10571 * Computes the Password-Based Key Derivation Function 2. 10572 * 10573 * @param {WordArray|string} password The password. 10574 * @param {WordArray|string} salt A salt. 10575 * @param {Object} cfg (Optional) The configuration options to use for this computation. 10576 * 10577 * @return {WordArray} The derived key. 10578 * 10579 * @static 10580 * 10581 * @example 10582 * 10583 * var key = CryptoJS.PBKDF2(password, salt); 10584 * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8 }); 10585 * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8, iterations: 1000 }); 10586 */ 10587 C.PBKDF2 = function (password, salt, cfg) { 10588 return PBKDF2.create(cfg).compute(password, salt); 10589 }; 10590 }()); 10591 10592 10593 return CryptoJS.PBKDF2; 10594 10595 })); 10596 },{"./core":53,"./hmac":58,"./sha1":77}],73:[function(require,module,exports){ 10597 ;(function (root, factory, undef) { 10598 if (typeof exports === "object") { 10599 // CommonJS 10600 module.exports = exports = factory(require("./core"), require("./enc-base64"), require("./md5"), require("./evpkdf"), require("./cipher-core")); 10601 } 10602 else if (typeof define === "function" && define.amd) { 10603 // AMD 10604 define(["./core", "./enc-base64", "./md5", "./evpkdf", "./cipher-core"], factory); 10605 } 10606 else { 10607 // Global (browser) 10608 factory(root.CryptoJS); 10609 } 10610 }(this, function (CryptoJS) { 10611 10612 (function () { 10613 // Shortcuts 10614 var C = CryptoJS; 10615 var C_lib = C.lib; 10616 var StreamCipher = C_lib.StreamCipher; 10617 var C_algo = C.algo; 10618 10619 // Reusable objects 10620 var S = []; 10621 var C_ = []; 10622 var G = []; 10623 10624 /** 10625 * Rabbit stream cipher algorithm. 10626 * 10627 * This is a legacy version that neglected to convert the key to little-endian. 10628 * This error doesn't affect the cipher's security, 10629 * but it does affect its compatibility with other implementations. 10630 */ 10631 var RabbitLegacy = C_algo.RabbitLegacy = StreamCipher.extend({ 10632 _doReset: function () { 10633 // Shortcuts 10634 var K = this._key.words; 10635 var iv = this.cfg.iv; 10636 10637 // Generate initial state values 10638 var X = this._X = [ 10639 K[0], (K[3] << 16) | (K[2] >>> 16), 10640 K[1], (K[0] << 16) | (K[3] >>> 16), 10641 K[2], (K[1] << 16) | (K[0] >>> 16), 10642 K[3], (K[2] << 16) | (K[1] >>> 16) 10643 ]; 10644 10645 // Generate initial counter values 10646 var C = this._C = [ 10647 (K[2] << 16) | (K[2] >>> 16), (K[0] & 0xffff0000) | (K[1] & 0x0000ffff), 10648 (K[3] << 16) | (K[3] >>> 16), (K[1] & 0xffff0000) | (K[2] & 0x0000ffff), 10649 (K[0] << 16) | (K[0] >>> 16), (K[2] & 0xffff0000) | (K[3] & 0x0000ffff), 10650 (K[1] << 16) | (K[1] >>> 16), (K[3] & 0xffff0000) | (K[0] & 0x0000ffff) 10651 ]; 10652 10653 // Carry bit 10654 this._b = 0; 10655 10656 // Iterate the system four times 10657 for (var i = 0; i < 4; i++) { 10658 nextState.call(this); 10659 } 10660 10661 // Modify the counters 10662 for (var i = 0; i < 8; i++) { 10663 C[i] ^= X[(i + 4) & 7]; 10664 } 10665 10666 // IV setup 10667 if (iv) { 10668 // Shortcuts 10669 var IV = iv.words; 10670 var IV_0 = IV[0]; 10671 var IV_1 = IV[1]; 10672 10673 // Generate four subvectors 10674 var i0 = (((IV_0 << 8) | (IV_0 >>> 24)) & 0x00ff00ff) | (((IV_0 << 24) | (IV_0 >>> 8)) & 0xff00ff00); 10675 var i2 = (((IV_1 << 8) | (IV_1 >>> 24)) & 0x00ff00ff) | (((IV_1 << 24) | (IV_1 >>> 8)) & 0xff00ff00); 10676 var i1 = (i0 >>> 16) | (i2 & 0xffff0000); 10677 var i3 = (i2 << 16) | (i0 & 0x0000ffff); 10678 10679 // Modify counter values 10680 C[0] ^= i0; 10681 C[1] ^= i1; 10682 C[2] ^= i2; 10683 C[3] ^= i3; 10684 C[4] ^= i0; 10685 C[5] ^= i1; 10686 C[6] ^= i2; 10687 C[7] ^= i3; 10688 10689 // Iterate the system four times 10690 for (var i = 0; i < 4; i++) { 10691 nextState.call(this); 10692 } 10693 } 10694 }, 10695 10696 _doProcessBlock: function (M, offset) { 10697 // Shortcut 10698 var X = this._X; 10699 10700 // Iterate the system 10701 nextState.call(this); 10702 10703 // Generate four keystream words 10704 S[0] = X[0] ^ (X[5] >>> 16) ^ (X[3] << 16); 10705 S[1] = X[2] ^ (X[7] >>> 16) ^ (X[5] << 16); 10706 S[2] = X[4] ^ (X[1] >>> 16) ^ (X[7] << 16); 10707 S[3] = X[6] ^ (X[3] >>> 16) ^ (X[1] << 16); 10708 10709 for (var i = 0; i < 4; i++) { 10710 // Swap endian 10711 S[i] = (((S[i] << 8) | (S[i] >>> 24)) & 0x00ff00ff) | 10712 (((S[i] << 24) | (S[i] >>> 8)) & 0xff00ff00); 10713 10714 // Encrypt 10715 M[offset + i] ^= S[i]; 10716 } 10717 }, 10718 10719 blockSize: 128/32, 10720 10721 ivSize: 64/32 10722 }); 10723 10724 function nextState() { 10725 // Shortcuts 10726 var X = this._X; 10727 var C = this._C; 10728 10729 // Save old counter values 10730 for (var i = 0; i < 8; i++) { 10731 C_[i] = C[i]; 10732 } 10733 10734 // Calculate new counter values 10735 C[0] = (C[0] + 0x4d34d34d + this._b) | 0; 10736 C[1] = (C[1] + 0xd34d34d3 + ((C[0] >>> 0) < (C_[0] >>> 0) ? 1 : 0)) | 0; 10737 C[2] = (C[2] + 0x34d34d34 + ((C[1] >>> 0) < (C_[1] >>> 0) ? 1 : 0)) | 0; 10738 C[3] = (C[3] + 0x4d34d34d + ((C[2] >>> 0) < (C_[2] >>> 0) ? 1 : 0)) | 0; 10739 C[4] = (C[4] + 0xd34d34d3 + ((C[3] >>> 0) < (C_[3] >>> 0) ? 1 : 0)) | 0; 10740 C[5] = (C[5] + 0x34d34d34 + ((C[4] >>> 0) < (C_[4] >>> 0) ? 1 : 0)) | 0; 10741 C[6] = (C[6] + 0x4d34d34d + ((C[5] >>> 0) < (C_[5] >>> 0) ? 1 : 0)) | 0; 10742 C[7] = (C[7] + 0xd34d34d3 + ((C[6] >>> 0) < (C_[6] >>> 0) ? 1 : 0)) | 0; 10743 this._b = (C[7] >>> 0) < (C_[7] >>> 0) ? 1 : 0; 10744 10745 // Calculate the g-values 10746 for (var i = 0; i < 8; i++) { 10747 var gx = X[i] + C[i]; 10748 10749 // Construct high and low argument for squaring 10750 var ga = gx & 0xffff; 10751 var gb = gx >>> 16; 10752 10753 // Calculate high and low result of squaring 10754 var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb; 10755 var gl = (((gx & 0xffff0000) * gx) | 0) + (((gx & 0x0000ffff) * gx) | 0); 10756 10757 // High XOR low 10758 G[i] = gh ^ gl; 10759 } 10760 10761 // Calculate new state values 10762 X[0] = (G[0] + ((G[7] << 16) | (G[7] >>> 16)) + ((G[6] << 16) | (G[6] >>> 16))) | 0; 10763 X[1] = (G[1] + ((G[0] << 8) | (G[0] >>> 24)) + G[7]) | 0; 10764 X[2] = (G[2] + ((G[1] << 16) | (G[1] >>> 16)) + ((G[0] << 16) | (G[0] >>> 16))) | 0; 10765 X[3] = (G[3] + ((G[2] << 8) | (G[2] >>> 24)) + G[1]) | 0; 10766 X[4] = (G[4] + ((G[3] << 16) | (G[3] >>> 16)) + ((G[2] << 16) | (G[2] >>> 16))) | 0; 10767 X[5] = (G[5] + ((G[4] << 8) | (G[4] >>> 24)) + G[3]) | 0; 10768 X[6] = (G[6] + ((G[5] << 16) | (G[5] >>> 16)) + ((G[4] << 16) | (G[4] >>> 16))) | 0; 10769 X[7] = (G[7] + ((G[6] << 8) | (G[6] >>> 24)) + G[5]) | 0; 10770 } 10771 10772 /** 10773 * Shortcut functions to the cipher's object interface. 10774 * 10775 * @example 10776 * 10777 * var ciphertext = CryptoJS.RabbitLegacy.encrypt(message, key, cfg); 10778 * var plaintext = CryptoJS.RabbitLegacy.decrypt(ciphertext, key, cfg); 10779 */ 10780 C.RabbitLegacy = StreamCipher._createHelper(RabbitLegacy); 10781 }()); 10782 10783 10784 return CryptoJS.RabbitLegacy; 10785 10786 })); 10787 },{"./cipher-core":52,"./core":53,"./enc-base64":54,"./evpkdf":56,"./md5":61}],74:[function(require,module,exports){ 10788 ;(function (root, factory, undef) { 10789 if (typeof exports === "object") { 10790 // CommonJS 10791 module.exports = exports = factory(require("./core"), require("./enc-base64"), require("./md5"), require("./evpkdf"), require("./cipher-core")); 10792 } 10793 else if (typeof define === "function" && define.amd) { 10794 // AMD 10795 define(["./core", "./enc-base64", "./md5", "./evpkdf", "./cipher-core"], factory); 10796 } 10797 else { 10798 // Global (browser) 10799 factory(root.CryptoJS); 10800 } 10801 }(this, function (CryptoJS) { 10802 10803 (function () { 10804 // Shortcuts 10805 var C = CryptoJS; 10806 var C_lib = C.lib; 10807 var StreamCipher = C_lib.StreamCipher; 10808 var C_algo = C.algo; 10809 10810 // Reusable objects 10811 var S = []; 10812 var C_ = []; 10813 var G = []; 10814 10815 /** 10816 * Rabbit stream cipher algorithm 10817 */ 10818 var Rabbit = C_algo.Rabbit = StreamCipher.extend({ 10819 _doReset: function () { 10820 // Shortcuts 10821 var K = this._key.words; 10822 var iv = this.cfg.iv; 10823 10824 // Swap endian 10825 for (var i = 0; i < 4; i++) { 10826 K[i] = (((K[i] << 8) | (K[i] >>> 24)) & 0x00ff00ff) | 10827 (((K[i] << 24) | (K[i] >>> 8)) & 0xff00ff00); 10828 } 10829 10830 // Generate initial state values 10831 var X = this._X = [ 10832 K[0], (K[3] << 16) | (K[2] >>> 16), 10833 K[1], (K[0] << 16) | (K[3] >>> 16), 10834 K[2], (K[1] << 16) | (K[0] >>> 16), 10835 K[3], (K[2] << 16) | (K[1] >>> 16) 10836 ]; 10837 10838 // Generate initial counter values 10839 var C = this._C = [ 10840 (K[2] << 16) | (K[2] >>> 16), (K[0] & 0xffff0000) | (K[1] & 0x0000ffff), 10841 (K[3] << 16) | (K[3] >>> 16), (K[1] & 0xffff0000) | (K[2] & 0x0000ffff), 10842 (K[0] << 16) | (K[0] >>> 16), (K[2] & 0xffff0000) | (K[3] & 0x0000ffff), 10843 (K[1] << 16) | (K[1] >>> 16), (K[3] & 0xffff0000) | (K[0] & 0x0000ffff) 10844 ]; 10845 10846 // Carry bit 10847 this._b = 0; 10848 10849 // Iterate the system four times 10850 for (var i = 0; i < 4; i++) { 10851 nextState.call(this); 10852 } 10853 10854 // Modify the counters 10855 for (var i = 0; i < 8; i++) { 10856 C[i] ^= X[(i + 4) & 7]; 10857 } 10858 10859 // IV setup 10860 if (iv) { 10861 // Shortcuts 10862 var IV = iv.words; 10863 var IV_0 = IV[0]; 10864 var IV_1 = IV[1]; 10865 10866 // Generate four subvectors 10867 var i0 = (((IV_0 << 8) | (IV_0 >>> 24)) & 0x00ff00ff) | (((IV_0 << 24) | (IV_0 >>> 8)) & 0xff00ff00); 10868 var i2 = (((IV_1 << 8) | (IV_1 >>> 24)) & 0x00ff00ff) | (((IV_1 << 24) | (IV_1 >>> 8)) & 0xff00ff00); 10869 var i1 = (i0 >>> 16) | (i2 & 0xffff0000); 10870 var i3 = (i2 << 16) | (i0 & 0x0000ffff); 10871 10872 // Modify counter values 10873 C[0] ^= i0; 10874 C[1] ^= i1; 10875 C[2] ^= i2; 10876 C[3] ^= i3; 10877 C[4] ^= i0; 10878 C[5] ^= i1; 10879 C[6] ^= i2; 10880 C[7] ^= i3; 10881 10882 // Iterate the system four times 10883 for (var i = 0; i < 4; i++) { 10884 nextState.call(this); 10885 } 10886 } 10887 }, 10888 10889 _doProcessBlock: function (M, offset) { 10890 // Shortcut 10891 var X = this._X; 10892 10893 // Iterate the system 10894 nextState.call(this); 10895 10896 // Generate four keystream words 10897 S[0] = X[0] ^ (X[5] >>> 16) ^ (X[3] << 16); 10898 S[1] = X[2] ^ (X[7] >>> 16) ^ (X[5] << 16); 10899 S[2] = X[4] ^ (X[1] >>> 16) ^ (X[7] << 16); 10900 S[3] = X[6] ^ (X[3] >>> 16) ^ (X[1] << 16); 10901 10902 for (var i = 0; i < 4; i++) { 10903 // Swap endian 10904 S[i] = (((S[i] << 8) | (S[i] >>> 24)) & 0x00ff00ff) | 10905 (((S[i] << 24) | (S[i] >>> 8)) & 0xff00ff00); 10906 10907 // Encrypt 10908 M[offset + i] ^= S[i]; 10909 } 10910 }, 10911 10912 blockSize: 128/32, 10913 10914 ivSize: 64/32 10915 }); 10916 10917 function nextState() { 10918 // Shortcuts 10919 var X = this._X; 10920 var C = this._C; 10921 10922 // Save old counter values 10923 for (var i = 0; i < 8; i++) { 10924 C_[i] = C[i]; 10925 } 10926 10927 // Calculate new counter values 10928 C[0] = (C[0] + 0x4d34d34d + this._b) | 0; 10929 C[1] = (C[1] + 0xd34d34d3 + ((C[0] >>> 0) < (C_[0] >>> 0) ? 1 : 0)) | 0; 10930 C[2] = (C[2] + 0x34d34d34 + ((C[1] >>> 0) < (C_[1] >>> 0) ? 1 : 0)) | 0; 10931 C[3] = (C[3] + 0x4d34d34d + ((C[2] >>> 0) < (C_[2] >>> 0) ? 1 : 0)) | 0; 10932 C[4] = (C[4] + 0xd34d34d3 + ((C[3] >>> 0) < (C_[3] >>> 0) ? 1 : 0)) | 0; 10933 C[5] = (C[5] + 0x34d34d34 + ((C[4] >>> 0) < (C_[4] >>> 0) ? 1 : 0)) | 0; 10934 C[6] = (C[6] + 0x4d34d34d + ((C[5] >>> 0) < (C_[5] >>> 0) ? 1 : 0)) | 0; 10935 C[7] = (C[7] + 0xd34d34d3 + ((C[6] >>> 0) < (C_[6] >>> 0) ? 1 : 0)) | 0; 10936 this._b = (C[7] >>> 0) < (C_[7] >>> 0) ? 1 : 0; 10937 10938 // Calculate the g-values 10939 for (var i = 0; i < 8; i++) { 10940 var gx = X[i] + C[i]; 10941 10942 // Construct high and low argument for squaring 10943 var ga = gx & 0xffff; 10944 var gb = gx >>> 16; 10945 10946 // Calculate high and low result of squaring 10947 var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb; 10948 var gl = (((gx & 0xffff0000) * gx) | 0) + (((gx & 0x0000ffff) * gx) | 0); 10949 10950 // High XOR low 10951 G[i] = gh ^ gl; 10952 } 10953 10954 // Calculate new state values 10955 X[0] = (G[0] + ((G[7] << 16) | (G[7] >>> 16)) + ((G[6] << 16) | (G[6] >>> 16))) | 0; 10956 X[1] = (G[1] + ((G[0] << 8) | (G[0] >>> 24)) + G[7]) | 0; 10957 X[2] = (G[2] + ((G[1] << 16) | (G[1] >>> 16)) + ((G[0] << 16) | (G[0] >>> 16))) | 0; 10958 X[3] = (G[3] + ((G[2] << 8) | (G[2] >>> 24)) + G[1]) | 0; 10959 X[4] = (G[4] + ((G[3] << 16) | (G[3] >>> 16)) + ((G[2] << 16) | (G[2] >>> 16))) | 0; 10960 X[5] = (G[5] + ((G[4] << 8) | (G[4] >>> 24)) + G[3]) | 0; 10961 X[6] = (G[6] + ((G[5] << 16) | (G[5] >>> 16)) + ((G[4] << 16) | (G[4] >>> 16))) | 0; 10962 X[7] = (G[7] + ((G[6] << 8) | (G[6] >>> 24)) + G[5]) | 0; 10963 } 10964 10965 /** 10966 * Shortcut functions to the cipher's object interface. 10967 * 10968 * @example 10969 * 10970 * var ciphertext = CryptoJS.Rabbit.encrypt(message, key, cfg); 10971 * var plaintext = CryptoJS.Rabbit.decrypt(ciphertext, key, cfg); 10972 */ 10973 C.Rabbit = StreamCipher._createHelper(Rabbit); 10974 }()); 10975 10976 10977 return CryptoJS.Rabbit; 10978 10979 })); 10980 },{"./cipher-core":52,"./core":53,"./enc-base64":54,"./evpkdf":56,"./md5":61}],75:[function(require,module,exports){ 10981 ;(function (root, factory, undef) { 10982 if (typeof exports === "object") { 10983 // CommonJS 10984 module.exports = exports = factory(require("./core"), require("./enc-base64"), require("./md5"), require("./evpkdf"), require("./cipher-core")); 10985 } 10986 else if (typeof define === "function" && define.amd) { 10987 // AMD 10988 define(["./core", "./enc-base64", "./md5", "./evpkdf", "./cipher-core"], factory); 10989 } 10990 else { 10991 // Global (browser) 10992 factory(root.CryptoJS); 10993 } 10994 }(this, function (CryptoJS) { 10995 10996 (function () { 10997 // Shortcuts 10998 var C = CryptoJS; 10999 var C_lib = C.lib; 11000 var StreamCipher = C_lib.StreamCipher; 11001 var C_algo = C.algo; 11002 11003 /** 11004 * RC4 stream cipher algorithm. 11005 */ 11006 var RC4 = C_algo.RC4 = StreamCipher.extend({ 11007 _doReset: function () { 11008 // Shortcuts 11009 var key = this._key; 11010 var keyWords = key.words; 11011 var keySigBytes = key.sigBytes; 11012 11013 // Init sbox 11014 var S = this._S = []; 11015 for (var i = 0; i < 256; i++) { 11016 S[i] = i; 11017 } 11018 11019 // Key setup 11020 for (var i = 0, j = 0; i < 256; i++) { 11021 var keyByteIndex = i % keySigBytes; 11022 var keyByte = (keyWords[keyByteIndex >>> 2] >>> (24 - (keyByteIndex % 4) * 8)) & 0xff; 11023 11024 j = (j + S[i] + keyByte) % 256; 11025 11026 // Swap 11027 var t = S[i]; 11028 S[i] = S[j]; 11029 S[j] = t; 11030 } 11031 11032 // Counters 11033 this._i = this._j = 0; 11034 }, 11035 11036 _doProcessBlock: function (M, offset) { 11037 M[offset] ^= generateKeystreamWord.call(this); 11038 }, 11039 11040 keySize: 256/32, 11041 11042 ivSize: 0 11043 }); 11044 11045 function generateKeystreamWord() { 11046 // Shortcuts 11047 var S = this._S; 11048 var i = this._i; 11049 var j = this._j; 11050 11051 // Generate keystream word 11052 var keystreamWord = 0; 11053 for (var n = 0; n < 4; n++) { 11054 i = (i + 1) % 256; 11055 j = (j + S[i]) % 256; 11056 11057 // Swap 11058 var t = S[i]; 11059 S[i] = S[j]; 11060 S[j] = t; 11061 11062 keystreamWord |= S[(S[i] + S[j]) % 256] << (24 - n * 8); 11063 } 11064 11065 // Update counters 11066 this._i = i; 11067 this._j = j; 11068 11069 return keystreamWord; 11070 } 11071 11072 /** 11073 * Shortcut functions to the cipher's object interface. 11074 * 11075 * @example 11076 * 11077 * var ciphertext = CryptoJS.RC4.encrypt(message, key, cfg); 11078 * var plaintext = CryptoJS.RC4.decrypt(ciphertext, key, cfg); 11079 */ 11080 C.RC4 = StreamCipher._createHelper(RC4); 11081 11082 /** 11083 * Modified RC4 stream cipher algorithm. 11084 */ 11085 var RC4Drop = C_algo.RC4Drop = RC4.extend({ 11086 /** 11087 * Configuration options. 11088 * 11089 * @property {number} drop The number of keystream words to drop. Default 192 11090 */ 11091 cfg: RC4.cfg.extend({ 11092 drop: 192 11093 }), 11094 11095 _doReset: function () { 11096 RC4._doReset.call(this); 11097 11098 // Drop 11099 for (var i = this.cfg.drop; i > 0; i--) { 11100 generateKeystreamWord.call(this); 11101 } 11102 } 11103 }); 11104 11105 /** 11106 * Shortcut functions to the cipher's object interface. 11107 * 11108 * @example 11109 * 11110 * var ciphertext = CryptoJS.RC4Drop.encrypt(message, key, cfg); 11111 * var plaintext = CryptoJS.RC4Drop.decrypt(ciphertext, key, cfg); 11112 */ 11113 C.RC4Drop = StreamCipher._createHelper(RC4Drop); 11114 }()); 11115 11116 11117 return CryptoJS.RC4; 11118 11119 })); 11120 },{"./cipher-core":52,"./core":53,"./enc-base64":54,"./evpkdf":56,"./md5":61}],76:[function(require,module,exports){ 11121 ;(function (root, factory) { 11122 if (typeof exports === "object") { 11123 // CommonJS 11124 module.exports = exports = factory(require("./core")); 11125 } 11126 else if (typeof define === "function" && define.amd) { 11127 // AMD 11128 define(["./core"], factory); 11129 } 11130 else { 11131 // Global (browser) 11132 factory(root.CryptoJS); 11133 } 11134 }(this, function (CryptoJS) { 11135 11136 /** @preserve 11137 (c) 2012 by Cédric Mesnil. All rights reserved. 11138 11139 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 11140 11141 - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 11142 - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 11143 11144 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 11145 */ 11146 11147 (function (Math) { 11148 // Shortcuts 11149 var C = CryptoJS; 11150 var C_lib = C.lib; 11151 var WordArray = C_lib.WordArray; 11152 var Hasher = C_lib.Hasher; 11153 var C_algo = C.algo; 11154 11155 // Constants table 11156 var _zl = WordArray.create([ 11157 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 11158 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8, 11159 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12, 11160 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, 11161 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13]); 11162 var _zr = WordArray.create([ 11163 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, 11164 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2, 11165 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13, 11166 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14, 11167 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11]); 11168 var _sl = WordArray.create([ 11169 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8, 11170 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12, 11171 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5, 11172 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12, 11173 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 ]); 11174 var _sr = WordArray.create([ 11175 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6, 11176 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11, 11177 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, 11178 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8, 11179 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 ]); 11180 11181 var _hl = WordArray.create([ 0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E]); 11182 var _hr = WordArray.create([ 0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000]); 11183 11184 /** 11185 * RIPEMD160 hash algorithm. 11186 */ 11187 var RIPEMD160 = C_algo.RIPEMD160 = Hasher.extend({ 11188 _doReset: function () { 11189 this._hash = WordArray.create([0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]); 11190 }, 11191 11192 _doProcessBlock: function (M, offset) { 11193 11194 // Swap endian 11195 for (var i = 0; i < 16; i++) { 11196 // Shortcuts 11197 var offset_i = offset + i; 11198 var M_offset_i = M[offset_i]; 11199 11200 // Swap 11201 M[offset_i] = ( 11202 (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) | 11203 (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00) 11204 ); 11205 } 11206 // Shortcut 11207 var H = this._hash.words; 11208 var hl = _hl.words; 11209 var hr = _hr.words; 11210 var zl = _zl.words; 11211 var zr = _zr.words; 11212 var sl = _sl.words; 11213 var sr = _sr.words; 11214 11215 // Working variables 11216 var al, bl, cl, dl, el; 11217 var ar, br, cr, dr, er; 11218 11219 ar = al = H[0]; 11220 br = bl = H[1]; 11221 cr = cl = H[2]; 11222 dr = dl = H[3]; 11223 er = el = H[4]; 11224 // Computation 11225 var t; 11226 for (var i = 0; i < 80; i += 1) { 11227 t = (al + M[offset+zl[i]])|0; 11228 if (i<16){ 11229 t += f1(bl,cl,dl) + hl[0]; 11230 } else if (i<32) { 11231 t += f2(bl,cl,dl) + hl[1]; 11232 } else if (i<48) { 11233 t += f3(bl,cl,dl) + hl[2]; 11234 } else if (i<64) { 11235 t += f4(bl,cl,dl) + hl[3]; 11236 } else {// if (i<80) { 11237 t += f5(bl,cl,dl) + hl[4]; 11238 } 11239 t = t|0; 11240 t = rotl(t,sl[i]); 11241 t = (t+el)|0; 11242 al = el; 11243 el = dl; 11244 dl = rotl(cl, 10); 11245 cl = bl; 11246 bl = t; 11247 11248 t = (ar + M[offset+zr[i]])|0; 11249 if (i<16){ 11250 t += f5(br,cr,dr) + hr[0]; 11251 } else if (i<32) { 11252 t += f4(br,cr,dr) + hr[1]; 11253 } else if (i<48) { 11254 t += f3(br,cr,dr) + hr[2]; 11255 } else if (i<64) { 11256 t += f2(br,cr,dr) + hr[3]; 11257 } else {// if (i<80) { 11258 t += f1(br,cr,dr) + hr[4]; 11259 } 11260 t = t|0; 11261 t = rotl(t,sr[i]) ; 11262 t = (t+er)|0; 11263 ar = er; 11264 er = dr; 11265 dr = rotl(cr, 10); 11266 cr = br; 11267 br = t; 11268 } 11269 // Intermediate hash value 11270 t = (H[1] + cl + dr)|0; 11271 H[1] = (H[2] + dl + er)|0; 11272 H[2] = (H[3] + el + ar)|0; 11273 H[3] = (H[4] + al + br)|0; 11274 H[4] = (H[0] + bl + cr)|0; 11275 H[0] = t; 11276 }, 11277 11278 _doFinalize: function () { 11279 // Shortcuts 11280 var data = this._data; 11281 var dataWords = data.words; 11282 11283 var nBitsTotal = this._nDataBytes * 8; 11284 var nBitsLeft = data.sigBytes * 8; 11285 11286 // Add padding 11287 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32); 11288 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = ( 11289 (((nBitsTotal << 8) | (nBitsTotal >>> 24)) & 0x00ff00ff) | 11290 (((nBitsTotal << 24) | (nBitsTotal >>> 8)) & 0xff00ff00) 11291 ); 11292 data.sigBytes = (dataWords.length + 1) * 4; 11293 11294 // Hash final blocks 11295 this._process(); 11296 11297 // Shortcuts 11298 var hash = this._hash; 11299 var H = hash.words; 11300 11301 // Swap endian 11302 for (var i = 0; i < 5; i++) { 11303 // Shortcut 11304 var H_i = H[i]; 11305 11306 // Swap 11307 H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) | 11308 (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00); 11309 } 11310 11311 // Return final computed hash 11312 return hash; 11313 }, 11314 11315 clone: function () { 11316 var clone = Hasher.clone.call(this); 11317 clone._hash = this._hash.clone(); 11318 11319 return clone; 11320 } 11321 }); 11322 11323 11324 function f1(x, y, z) { 11325 return ((x) ^ (y) ^ (z)); 11326 11327 } 11328 11329 function f2(x, y, z) { 11330 return (((x)&(y)) | ((~x)&(z))); 11331 } 11332 11333 function f3(x, y, z) { 11334 return (((x) | (~(y))) ^ (z)); 11335 } 11336 11337 function f4(x, y, z) { 11338 return (((x) & (z)) | ((y)&(~(z)))); 11339 } 11340 11341 function f5(x, y, z) { 11342 return ((x) ^ ((y) |(~(z)))); 11343 11344 } 11345 11346 function rotl(x,n) { 11347 return (x<<n) | (x>>>(32-n)); 11348 } 11349 11350 11351 /** 11352 * Shortcut function to the hasher's object interface. 11353 * 11354 * @param {WordArray|string} message The message to hash. 11355 * 11356 * @return {WordArray} The hash. 11357 * 11358 * @static 11359 * 11360 * @example 11361 * 11362 * var hash = CryptoJS.RIPEMD160('message'); 11363 * var hash = CryptoJS.RIPEMD160(wordArray); 11364 */ 11365 C.RIPEMD160 = Hasher._createHelper(RIPEMD160); 11366 11367 /** 11368 * Shortcut function to the HMAC's object interface. 11369 * 11370 * @param {WordArray|string} message The message to hash. 11371 * @param {WordArray|string} key The secret key. 11372 * 11373 * @return {WordArray} The HMAC. 11374 * 11375 * @static 11376 * 11377 * @example 11378 * 11379 * var hmac = CryptoJS.HmacRIPEMD160(message, key); 11380 */ 11381 C.HmacRIPEMD160 = Hasher._createHmacHelper(RIPEMD160); 11382 }(Math)); 11383 11384 11385 return CryptoJS.RIPEMD160; 11386 11387 })); 11388 },{"./core":53}],77:[function(require,module,exports){ 11389 ;(function (root, factory) { 11390 if (typeof exports === "object") { 11391 // CommonJS 11392 module.exports = exports = factory(require("./core")); 11393 } 11394 else if (typeof define === "function" && define.amd) { 11395 // AMD 11396 define(["./core"], factory); 11397 } 11398 else { 11399 // Global (browser) 11400 factory(root.CryptoJS); 11401 } 11402 }(this, function (CryptoJS) { 11403 11404 (function () { 11405 // Shortcuts 11406 var C = CryptoJS; 11407 var C_lib = C.lib; 11408 var WordArray = C_lib.WordArray; 11409 var Hasher = C_lib.Hasher; 11410 var C_algo = C.algo; 11411 11412 // Reusable object 11413 var W = []; 11414 11415 /** 11416 * SHA-1 hash algorithm. 11417 */ 11418 var SHA1 = C_algo.SHA1 = Hasher.extend({ 11419 _doReset: function () { 11420 this._hash = new WordArray.init([ 11421 0x67452301, 0xefcdab89, 11422 0x98badcfe, 0x10325476, 11423 0xc3d2e1f0 11424 ]); 11425 }, 11426 11427 _doProcessBlock: function (M, offset) { 11428 // Shortcut 11429 var H = this._hash.words; 11430 11431 // Working variables 11432 var a = H[0]; 11433 var b = H[1]; 11434 var c = H[2]; 11435 var d = H[3]; 11436 var e = H[4]; 11437 11438 // Computation 11439 for (var i = 0; i < 80; i++) { 11440 if (i < 16) { 11441 W[i] = M[offset + i] | 0; 11442 } else { 11443 var n = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16]; 11444 W[i] = (n << 1) | (n >>> 31); 11445 } 11446 11447 var t = ((a << 5) | (a >>> 27)) + e + W[i]; 11448 if (i < 20) { 11449 t += ((b & c) | (~b & d)) + 0x5a827999; 11450 } else if (i < 40) { 11451 t += (b ^ c ^ d) + 0x6ed9eba1; 11452 } else if (i < 60) { 11453 t += ((b & c) | (b & d) | (c & d)) - 0x70e44324; 11454 } else /* if (i < 80) */ { 11455 t += (b ^ c ^ d) - 0x359d3e2a; 11456 } 11457 11458 e = d; 11459 d = c; 11460 c = (b << 30) | (b >>> 2); 11461 b = a; 11462 a = t; 11463 } 11464 11465 // Intermediate hash value 11466 H[0] = (H[0] + a) | 0; 11467 H[1] = (H[1] + b) | 0; 11468 H[2] = (H[2] + c) | 0; 11469 H[3] = (H[3] + d) | 0; 11470 H[4] = (H[4] + e) | 0; 11471 }, 11472 11473 _doFinalize: function () { 11474 // Shortcuts 11475 var data = this._data; 11476 var dataWords = data.words; 11477 11478 var nBitsTotal = this._nDataBytes * 8; 11479 var nBitsLeft = data.sigBytes * 8; 11480 11481 // Add padding 11482 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32); 11483 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000); 11484 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal; 11485 data.sigBytes = dataWords.length * 4; 11486 11487 // Hash final blocks 11488 this._process(); 11489 11490 // Return final computed hash 11491 return this._hash; 11492 }, 11493 11494 clone: function () { 11495 var clone = Hasher.clone.call(this); 11496 clone._hash = this._hash.clone(); 11497 11498 return clone; 11499 } 11500 }); 11501 11502 /** 11503 * Shortcut function to the hasher's object interface. 11504 * 11505 * @param {WordArray|string} message The message to hash. 11506 * 11507 * @return {WordArray} The hash. 11508 * 11509 * @static 11510 * 11511 * @example 11512 * 11513 * var hash = CryptoJS.SHA1('message'); 11514 * var hash = CryptoJS.SHA1(wordArray); 11515 */ 11516 C.SHA1 = Hasher._createHelper(SHA1); 11517 11518 /** 11519 * Shortcut function to the HMAC's object interface. 11520 * 11521 * @param {WordArray|string} message The message to hash. 11522 * @param {WordArray|string} key The secret key. 11523 * 11524 * @return {WordArray} The HMAC. 11525 * 11526 * @static 11527 * 11528 * @example 11529 * 11530 * var hmac = CryptoJS.HmacSHA1(message, key); 11531 */ 11532 C.HmacSHA1 = Hasher._createHmacHelper(SHA1); 11533 }()); 11534 11535 11536 return CryptoJS.SHA1; 11537 11538 })); 11539 },{"./core":53}],78:[function(require,module,exports){ 11540 ;(function (root, factory, undef) { 11541 if (typeof exports === "object") { 11542 // CommonJS 11543 module.exports = exports = factory(require("./core"), require("./sha256")); 11544 } 11545 else if (typeof define === "function" && define.amd) { 11546 // AMD 11547 define(["./core", "./sha256"], factory); 11548 } 11549 else { 11550 // Global (browser) 11551 factory(root.CryptoJS); 11552 } 11553 }(this, function (CryptoJS) { 11554 11555 (function () { 11556 // Shortcuts 11557 var C = CryptoJS; 11558 var C_lib = C.lib; 11559 var WordArray = C_lib.WordArray; 11560 var C_algo = C.algo; 11561 var SHA256 = C_algo.SHA256; 11562 11563 /** 11564 * SHA-224 hash algorithm. 11565 */ 11566 var SHA224 = C_algo.SHA224 = SHA256.extend({ 11567 _doReset: function () { 11568 this._hash = new WordArray.init([ 11569 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 11570 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4 11571 ]); 11572 }, 11573 11574 _doFinalize: function () { 11575 var hash = SHA256._doFinalize.call(this); 11576 11577 hash.sigBytes -= 4; 11578 11579 return hash; 11580 } 11581 }); 11582 11583 /** 11584 * Shortcut function to the hasher's object interface. 11585 * 11586 * @param {WordArray|string} message The message to hash. 11587 * 11588 * @return {WordArray} The hash. 11589 * 11590 * @static 11591 * 11592 * @example 11593 * 11594 * var hash = CryptoJS.SHA224('message'); 11595 * var hash = CryptoJS.SHA224(wordArray); 11596 */ 11597 C.SHA224 = SHA256._createHelper(SHA224); 11598 11599 /** 11600 * Shortcut function to the HMAC's object interface. 11601 * 11602 * @param {WordArray|string} message The message to hash. 11603 * @param {WordArray|string} key The secret key. 11604 * 11605 * @return {WordArray} The HMAC. 11606 * 11607 * @static 11608 * 11609 * @example 11610 * 11611 * var hmac = CryptoJS.HmacSHA224(message, key); 11612 */ 11613 C.HmacSHA224 = SHA256._createHmacHelper(SHA224); 11614 }()); 11615 11616 11617 return CryptoJS.SHA224; 11618 11619 })); 11620 },{"./core":53,"./sha256":79}],79:[function(require,module,exports){ 11621 ;(function (root, factory) { 11622 if (typeof exports === "object") { 11623 // CommonJS 11624 module.exports = exports = factory(require("./core")); 11625 } 11626 else if (typeof define === "function" && define.amd) { 11627 // AMD 11628 define(["./core"], factory); 11629 } 11630 else { 11631 // Global (browser) 11632 factory(root.CryptoJS); 11633 } 11634 }(this, function (CryptoJS) { 11635 11636 (function (Math) { 11637 // Shortcuts 11638 var C = CryptoJS; 11639 var C_lib = C.lib; 11640 var WordArray = C_lib.WordArray; 11641 var Hasher = C_lib.Hasher; 11642 var C_algo = C.algo; 11643 11644 // Initialization and round constants tables 11645 var H = []; 11646 var K = []; 11647 11648 // Compute constants 11649 (function () { 11650 function isPrime(n) { 11651 var sqrtN = Math.sqrt(n); 11652 for (var factor = 2; factor <= sqrtN; factor++) { 11653 if (!(n % factor)) { 11654 return false; 11655 } 11656 } 11657 11658 return true; 11659 } 11660 11661 function getFractionalBits(n) { 11662 return ((n - (n | 0)) * 0x100000000) | 0; 11663 } 11664 11665 var n = 2; 11666 var nPrime = 0; 11667 while (nPrime < 64) { 11668 if (isPrime(n)) { 11669 if (nPrime < 8) { 11670 H[nPrime] = getFractionalBits(Math.pow(n, 1 / 2)); 11671 } 11672 K[nPrime] = getFractionalBits(Math.pow(n, 1 / 3)); 11673 11674 nPrime++; 11675 } 11676 11677 n++; 11678 } 11679 }()); 11680 11681 // Reusable object 11682 var W = []; 11683 11684 /** 11685 * SHA-256 hash algorithm. 11686 */ 11687 var SHA256 = C_algo.SHA256 = Hasher.extend({ 11688 _doReset: function () { 11689 this._hash = new WordArray.init(H.slice(0)); 11690 }, 11691 11692 _doProcessBlock: function (M, offset) { 11693 // Shortcut 11694 var H = this._hash.words; 11695 11696 // Working variables 11697 var a = H[0]; 11698 var b = H[1]; 11699 var c = H[2]; 11700 var d = H[3]; 11701 var e = H[4]; 11702 var f = H[5]; 11703 var g = H[6]; 11704 var h = H[7]; 11705 11706 // Computation 11707 for (var i = 0; i < 64; i++) { 11708 if (i < 16) { 11709 W[i] = M[offset + i] | 0; 11710 } else { 11711 var gamma0x = W[i - 15]; 11712 var gamma0 = ((gamma0x << 25) | (gamma0x >>> 7)) ^ 11713 ((gamma0x << 14) | (gamma0x >>> 18)) ^ 11714 (gamma0x >>> 3); 11715 11716 var gamma1x = W[i - 2]; 11717 var gamma1 = ((gamma1x << 15) | (gamma1x >>> 17)) ^ 11718 ((gamma1x << 13) | (gamma1x >>> 19)) ^ 11719 (gamma1x >>> 10); 11720 11721 W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]; 11722 } 11723 11724 var ch = (e & f) ^ (~e & g); 11725 var maj = (a & b) ^ (a & c) ^ (b & c); 11726 11727 var sigma0 = ((a << 30) | (a >>> 2)) ^ ((a << 19) | (a >>> 13)) ^ ((a << 10) | (a >>> 22)); 11728 var sigma1 = ((e << 26) | (e >>> 6)) ^ ((e << 21) | (e >>> 11)) ^ ((e << 7) | (e >>> 25)); 11729 11730 var t1 = h + sigma1 + ch + K[i] + W[i]; 11731 var t2 = sigma0 + maj; 11732 11733 h = g; 11734 g = f; 11735 f = e; 11736 e = (d + t1) | 0; 11737 d = c; 11738 c = b; 11739 b = a; 11740 a = (t1 + t2) | 0; 11741 } 11742 11743 // Intermediate hash value 11744 H[0] = (H[0] + a) | 0; 11745 H[1] = (H[1] + b) | 0; 11746 H[2] = (H[2] + c) | 0; 11747 H[3] = (H[3] + d) | 0; 11748 H[4] = (H[4] + e) | 0; 11749 H[5] = (H[5] + f) | 0; 11750 H[6] = (H[6] + g) | 0; 11751 H[7] = (H[7] + h) | 0; 11752 }, 11753 11754 _doFinalize: function () { 11755 // Shortcuts 11756 var data = this._data; 11757 var dataWords = data.words; 11758 11759 var nBitsTotal = this._nDataBytes * 8; 11760 var nBitsLeft = data.sigBytes * 8; 11761 11762 // Add padding 11763 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32); 11764 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000); 11765 dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal; 11766 data.sigBytes = dataWords.length * 4; 11767 11768 // Hash final blocks 11769 this._process(); 11770 11771 // Return final computed hash 11772 return this._hash; 11773 }, 11774 11775 clone: function () { 11776 var clone = Hasher.clone.call(this); 11777 clone._hash = this._hash.clone(); 11778 11779 return clone; 11780 } 11781 }); 11782 11783 /** 11784 * Shortcut function to the hasher's object interface. 11785 * 11786 * @param {WordArray|string} message The message to hash. 11787 * 11788 * @return {WordArray} The hash. 11789 * 11790 * @static 11791 * 11792 * @example 11793 * 11794 * var hash = CryptoJS.SHA256('message'); 11795 * var hash = CryptoJS.SHA256(wordArray); 11796 */ 11797 C.SHA256 = Hasher._createHelper(SHA256); 11798 11799 /** 11800 * Shortcut function to the HMAC's object interface. 11801 * 11802 * @param {WordArray|string} message The message to hash. 11803 * @param {WordArray|string} key The secret key. 11804 * 11805 * @return {WordArray} The HMAC. 11806 * 11807 * @static 11808 * 11809 * @example 11810 * 11811 * var hmac = CryptoJS.HmacSHA256(message, key); 11812 */ 11813 C.HmacSHA256 = Hasher._createHmacHelper(SHA256); 11814 }(Math)); 11815 11816 11817 return CryptoJS.SHA256; 11818 11819 })); 11820 },{"./core":53}],80:[function(require,module,exports){ 11821 ;(function (root, factory, undef) { 11822 if (typeof exports === "object") { 11823 // CommonJS 11824 module.exports = exports = factory(require("./core"), require("./x64-core")); 11825 } 11826 else if (typeof define === "function" && define.amd) { 11827 // AMD 11828 define(["./core", "./x64-core"], factory); 11829 } 11830 else { 11831 // Global (browser) 11832 factory(root.CryptoJS); 11833 } 11834 }(this, function (CryptoJS) { 11835 11836 (function (Math) { 11837 // Shortcuts 11838 var C = CryptoJS; 11839 var C_lib = C.lib; 11840 var WordArray = C_lib.WordArray; 11841 var Hasher = C_lib.Hasher; 11842 var C_x64 = C.x64; 11843 var X64Word = C_x64.Word; 11844 var C_algo = C.algo; 11845 11846 // Constants tables 11847 var RHO_OFFSETS = []; 11848 var PI_INDEXES = []; 11849 var ROUND_CONSTANTS = []; 11850 11851 // Compute Constants 11852 (function () { 11853 // Compute rho offset constants 11854 var x = 1, y = 0; 11855 for (var t = 0; t < 24; t++) { 11856 RHO_OFFSETS[x + 5 * y] = ((t + 1) * (t + 2) / 2) % 64; 11857 11858 var newX = y % 5; 11859 var newY = (2 * x + 3 * y) % 5; 11860 x = newX; 11861 y = newY; 11862 } 11863 11864 // Compute pi index constants 11865 for (var x = 0; x < 5; x++) { 11866 for (var y = 0; y < 5; y++) { 11867 PI_INDEXES[x + 5 * y] = y + ((2 * x + 3 * y) % 5) * 5; 11868 } 11869 } 11870 11871 // Compute round constants 11872 var LFSR = 0x01; 11873 for (var i = 0; i < 24; i++) { 11874 var roundConstantMsw = 0; 11875 var roundConstantLsw = 0; 11876 11877 for (var j = 0; j < 7; j++) { 11878 if (LFSR & 0x01) { 11879 var bitPosition = (1 << j) - 1; 11880 if (bitPosition < 32) { 11881 roundConstantLsw ^= 1 << bitPosition; 11882 } else /* if (bitPosition >= 32) */ { 11883 roundConstantMsw ^= 1 << (bitPosition - 32); 11884 } 11885 } 11886 11887 // Compute next LFSR 11888 if (LFSR & 0x80) { 11889 // Primitive polynomial over GF(2): x^8 + x^6 + x^5 + x^4 + 1 11890 LFSR = (LFSR << 1) ^ 0x71; 11891 } else { 11892 LFSR <<= 1; 11893 } 11894 } 11895 11896 ROUND_CONSTANTS[i] = X64Word.create(roundConstantMsw, roundConstantLsw); 11897 } 11898 }()); 11899 11900 // Reusable objects for temporary values 11901 var T = []; 11902 (function () { 11903 for (var i = 0; i < 25; i++) { 11904 T[i] = X64Word.create(); 11905 } 11906 }()); 11907 11908 /** 11909 * SHA-3 hash algorithm. 11910 */ 11911 var SHA3 = C_algo.SHA3 = Hasher.extend({ 11912 /** 11913 * Configuration options. 11914 * 11915 * @property {number} outputLength 11916 * The desired number of bits in the output hash. 11917 * Only values permitted are: 224, 256, 384, 512. 11918 * Default: 512 11919 */ 11920 cfg: Hasher.cfg.extend({ 11921 outputLength: 512 11922 }), 11923 11924 _doReset: function () { 11925 var state = this._state = [] 11926 for (var i = 0; i < 25; i++) { 11927 state[i] = new X64Word.init(); 11928 } 11929 11930 this.blockSize = (1600 - 2 * this.cfg.outputLength) / 32; 11931 }, 11932 11933 _doProcessBlock: function (M, offset) { 11934 // Shortcuts 11935 var state = this._state; 11936 var nBlockSizeLanes = this.blockSize / 2; 11937 11938 // Absorb 11939 for (var i = 0; i < nBlockSizeLanes; i++) { 11940 // Shortcuts 11941 var M2i = M[offset + 2 * i]; 11942 var M2i1 = M[offset + 2 * i + 1]; 11943 11944 // Swap endian 11945 M2i = ( 11946 (((M2i << 8) | (M2i >>> 24)) & 0x00ff00ff) | 11947 (((M2i << 24) | (M2i >>> 8)) & 0xff00ff00) 11948 ); 11949 M2i1 = ( 11950 (((M2i1 << 8) | (M2i1 >>> 24)) & 0x00ff00ff) | 11951 (((M2i1 << 24) | (M2i1 >>> 8)) & 0xff00ff00) 11952 ); 11953 11954 // Absorb message into state 11955 var lane = state[i]; 11956 lane.high ^= M2i1; 11957 lane.low ^= M2i; 11958 } 11959 11960 // Rounds 11961 for (var round = 0; round < 24; round++) { 11962 // Theta 11963 for (var x = 0; x < 5; x++) { 11964 // Mix column lanes 11965 var tMsw = 0, tLsw = 0; 11966 for (var y = 0; y < 5; y++) { 11967 var lane = state[x + 5 * y]; 11968 tMsw ^= lane.high; 11969 tLsw ^= lane.low; 11970 } 11971 11972 // Temporary values 11973 var Tx = T[x]; 11974 Tx.high = tMsw; 11975 Tx.low = tLsw; 11976 } 11977 for (var x = 0; x < 5; x++) { 11978 // Shortcuts 11979 var Tx4 = T[(x + 4) % 5]; 11980 var Tx1 = T[(x + 1) % 5]; 11981 var Tx1Msw = Tx1.high; 11982 var Tx1Lsw = Tx1.low; 11983 11984 // Mix surrounding columns 11985 var tMsw = Tx4.high ^ ((Tx1Msw << 1) | (Tx1Lsw >>> 31)); 11986 var tLsw = Tx4.low ^ ((Tx1Lsw << 1) | (Tx1Msw >>> 31)); 11987 for (var y = 0; y < 5; y++) { 11988 var lane = state[x + 5 * y]; 11989 lane.high ^= tMsw; 11990 lane.low ^= tLsw; 11991 } 11992 } 11993 11994 // Rho Pi 11995 for (var laneIndex = 1; laneIndex < 25; laneIndex++) { 11996 // Shortcuts 11997 var lane = state[laneIndex]; 11998 var laneMsw = lane.high; 11999 var laneLsw = lane.low; 12000 var rhoOffset = RHO_OFFSETS[laneIndex]; 12001 12002 // Rotate lanes 12003 if (rhoOffset < 32) { 12004 var tMsw = (laneMsw << rhoOffset) | (laneLsw >>> (32 - rhoOffset)); 12005 var tLsw = (laneLsw << rhoOffset) | (laneMsw >>> (32 - rhoOffset)); 12006 } else /* if (rhoOffset >= 32) */ { 12007 var tMsw = (laneLsw << (rhoOffset - 32)) | (laneMsw >>> (64 - rhoOffset)); 12008 var tLsw = (laneMsw << (rhoOffset - 32)) | (laneLsw >>> (64 - rhoOffset)); 12009 } 12010 12011 // Transpose lanes 12012 var TPiLane = T[PI_INDEXES[laneIndex]]; 12013 TPiLane.high = tMsw; 12014 TPiLane.low = tLsw; 12015 } 12016 12017 // Rho pi at x = y = 0 12018 var T0 = T[0]; 12019 var state0 = state[0]; 12020 T0.high = state0.high; 12021 T0.low = state0.low; 12022 12023 // Chi 12024 for (var x = 0; x < 5; x++) { 12025 for (var y = 0; y < 5; y++) { 12026 // Shortcuts 12027 var laneIndex = x + 5 * y; 12028 var lane = state[laneIndex]; 12029 var TLane = T[laneIndex]; 12030 var Tx1Lane = T[((x + 1) % 5) + 5 * y]; 12031 var Tx2Lane = T[((x + 2) % 5) + 5 * y]; 12032 12033 // Mix rows 12034 lane.high = TLane.high ^ (~Tx1Lane.high & Tx2Lane.high); 12035 lane.low = TLane.low ^ (~Tx1Lane.low & Tx2Lane.low); 12036 } 12037 } 12038 12039 // Iota 12040 var lane = state[0]; 12041 var roundConstant = ROUND_CONSTANTS[round]; 12042 lane.high ^= roundConstant.high; 12043 lane.low ^= roundConstant.low;; 12044 } 12045 }, 12046 12047 _doFinalize: function () { 12048 // Shortcuts 12049 var data = this._data; 12050 var dataWords = data.words; 12051 var nBitsTotal = this._nDataBytes * 8; 12052 var nBitsLeft = data.sigBytes * 8; 12053 var blockSizeBits = this.blockSize * 32; 12054 12055 // Add padding 12056 dataWords[nBitsLeft >>> 5] |= 0x1 << (24 - nBitsLeft % 32); 12057 dataWords[((Math.ceil((nBitsLeft + 1) / blockSizeBits) * blockSizeBits) >>> 5) - 1] |= 0x80; 12058 data.sigBytes = dataWords.length * 4; 12059 12060 // Hash final blocks 12061 this._process(); 12062 12063 // Shortcuts 12064 var state = this._state; 12065 var outputLengthBytes = this.cfg.outputLength / 8; 12066 var outputLengthLanes = outputLengthBytes / 8; 12067 12068 // Squeeze 12069 var hashWords = []; 12070 for (var i = 0; i < outputLengthLanes; i++) { 12071 // Shortcuts 12072 var lane = state[i]; 12073 var laneMsw = lane.high; 12074 var laneLsw = lane.low; 12075 12076 // Swap endian 12077 laneMsw = ( 12078 (((laneMsw << 8) | (laneMsw >>> 24)) & 0x00ff00ff) | 12079 (((laneMsw << 24) | (laneMsw >>> 8)) & 0xff00ff00) 12080 ); 12081 laneLsw = ( 12082 (((laneLsw << 8) | (laneLsw >>> 24)) & 0x00ff00ff) | 12083 (((laneLsw << 24) | (laneLsw >>> 8)) & 0xff00ff00) 12084 ); 12085 12086 // Squeeze state to retrieve hash 12087 hashWords.push(laneLsw); 12088 hashWords.push(laneMsw); 12089 } 12090 12091 // Return final computed hash 12092 return new WordArray.init(hashWords, outputLengthBytes); 12093 }, 12094 12095 clone: function () { 12096 var clone = Hasher.clone.call(this); 12097 12098 var state = clone._state = this._state.slice(0); 12099 for (var i = 0; i < 25; i++) { 12100 state[i] = state[i].clone(); 12101 } 12102 12103 return clone; 12104 } 12105 }); 12106 12107 /** 12108 * Shortcut function to the hasher's object interface. 12109 * 12110 * @param {WordArray|string} message The message to hash. 12111 * 12112 * @return {WordArray} The hash. 12113 * 12114 * @static 12115 * 12116 * @example 12117 * 12118 * var hash = CryptoJS.SHA3('message'); 12119 * var hash = CryptoJS.SHA3(wordArray); 12120 */ 12121 C.SHA3 = Hasher._createHelper(SHA3); 12122 12123 /** 12124 * Shortcut function to the HMAC's object interface. 12125 * 12126 * @param {WordArray|string} message The message to hash. 12127 * @param {WordArray|string} key The secret key. 12128 * 12129 * @return {WordArray} The HMAC. 12130 * 12131 * @static 12132 * 12133 * @example 12134 * 12135 * var hmac = CryptoJS.HmacSHA3(message, key); 12136 */ 12137 C.HmacSHA3 = Hasher._createHmacHelper(SHA3); 12138 }(Math)); 12139 12140 12141 return CryptoJS.SHA3; 12142 12143 })); 12144 },{"./core":53,"./x64-core":84}],81:[function(require,module,exports){ 12145 ;(function (root, factory, undef) { 12146 if (typeof exports === "object") { 12147 // CommonJS 12148 module.exports = exports = factory(require("./core"), require("./x64-core"), require("./sha512")); 12149 } 12150 else if (typeof define === "function" && define.amd) { 12151 // AMD 12152 define(["./core", "./x64-core", "./sha512"], factory); 12153 } 12154 else { 12155 // Global (browser) 12156 factory(root.CryptoJS); 12157 } 12158 }(this, function (CryptoJS) { 12159 12160 (function () { 12161 // Shortcuts 12162 var C = CryptoJS; 12163 var C_x64 = C.x64; 12164 var X64Word = C_x64.Word; 12165 var X64WordArray = C_x64.WordArray; 12166 var C_algo = C.algo; 12167 var SHA512 = C_algo.SHA512; 12168 12169 /** 12170 * SHA-384 hash algorithm. 12171 */ 12172 var SHA384 = C_algo.SHA384 = SHA512.extend({ 12173 _doReset: function () { 12174 this._hash = new X64WordArray.init([ 12175 new X64Word.init(0xcbbb9d5d, 0xc1059ed8), new X64Word.init(0x629a292a, 0x367cd507), 12176 new X64Word.init(0x9159015a, 0x3070dd17), new X64Word.init(0x152fecd8, 0xf70e5939), 12177 new X64Word.init(0x67332667, 0xffc00b31), new X64Word.init(0x8eb44a87, 0x68581511), 12178 new X64Word.init(0xdb0c2e0d, 0x64f98fa7), new X64Word.init(0x47b5481d, 0xbefa4fa4) 12179 ]); 12180 }, 12181 12182 _doFinalize: function () { 12183 var hash = SHA512._doFinalize.call(this); 12184 12185 hash.sigBytes -= 16; 12186 12187 return hash; 12188 } 12189 }); 12190 12191 /** 12192 * Shortcut function to the hasher's object interface. 12193 * 12194 * @param {WordArray|string} message The message to hash. 12195 * 12196 * @return {WordArray} The hash. 12197 * 12198 * @static 12199 * 12200 * @example 12201 * 12202 * var hash = CryptoJS.SHA384('message'); 12203 * var hash = CryptoJS.SHA384(wordArray); 12204 */ 12205 C.SHA384 = SHA512._createHelper(SHA384); 12206 12207 /** 12208 * Shortcut function to the HMAC's object interface. 12209 * 12210 * @param {WordArray|string} message The message to hash. 12211 * @param {WordArray|string} key The secret key. 12212 * 12213 * @return {WordArray} The HMAC. 12214 * 12215 * @static 12216 * 12217 * @example 12218 * 12219 * var hmac = CryptoJS.HmacSHA384(message, key); 12220 */ 12221 C.HmacSHA384 = SHA512._createHmacHelper(SHA384); 12222 }()); 12223 12224 12225 return CryptoJS.SHA384; 12226 12227 })); 12228 },{"./core":53,"./sha512":82,"./x64-core":84}],82:[function(require,module,exports){ 12229 ;(function (root, factory, undef) { 12230 if (typeof exports === "object") { 12231 // CommonJS 12232 module.exports = exports = factory(require("./core"), require("./x64-core")); 12233 } 12234 else if (typeof define === "function" && define.amd) { 12235 // AMD 12236 define(["./core", "./x64-core"], factory); 12237 } 12238 else { 12239 // Global (browser) 12240 factory(root.CryptoJS); 12241 } 12242 }(this, function (CryptoJS) { 12243 12244 (function () { 12245 // Shortcuts 12246 var C = CryptoJS; 12247 var C_lib = C.lib; 12248 var Hasher = C_lib.Hasher; 12249 var C_x64 = C.x64; 12250 var X64Word = C_x64.Word; 12251 var X64WordArray = C_x64.WordArray; 12252 var C_algo = C.algo; 12253 12254 function X64Word_create() { 12255 return X64Word.create.apply(X64Word, arguments); 12256 } 12257 12258 // Constants 12259 var K = [ 12260 X64Word_create(0x428a2f98, 0xd728ae22), X64Word_create(0x71374491, 0x23ef65cd), 12261 X64Word_create(0xb5c0fbcf, 0xec4d3b2f), X64Word_create(0xe9b5dba5, 0x8189dbbc), 12262 X64Word_create(0x3956c25b, 0xf348b538), X64Word_create(0x59f111f1, 0xb605d019), 12263 X64Word_create(0x923f82a4, 0xaf194f9b), X64Word_create(0xab1c5ed5, 0xda6d8118), 12264 X64Word_create(0xd807aa98, 0xa3030242), X64Word_create(0x12835b01, 0x45706fbe), 12265 X64Word_create(0x243185be, 0x4ee4b28c), X64Word_create(0x550c7dc3, 0xd5ffb4e2), 12266 X64Word_create(0x72be5d74, 0xf27b896f), X64Word_create(0x80deb1fe, 0x3b1696b1), 12267 X64Word_create(0x9bdc06a7, 0x25c71235), X64Word_create(0xc19bf174, 0xcf692694), 12268 X64Word_create(0xe49b69c1, 0x9ef14ad2), X64Word_create(0xefbe4786, 0x384f25e3), 12269 X64Word_create(0x0fc19dc6, 0x8b8cd5b5), X64Word_create(0x240ca1cc, 0x77ac9c65), 12270 X64Word_create(0x2de92c6f, 0x592b0275), X64Word_create(0x4a7484aa, 0x6ea6e483), 12271 X64Word_create(0x5cb0a9dc, 0xbd41fbd4), X64Word_create(0x76f988da, 0x831153b5), 12272 X64Word_create(0x983e5152, 0xee66dfab), X64Word_create(0xa831c66d, 0x2db43210), 12273 X64Word_create(0xb00327c8, 0x98fb213f), X64Word_create(0xbf597fc7, 0xbeef0ee4), 12274 X64Word_create(0xc6e00bf3, 0x3da88fc2), X64Word_create(0xd5a79147, 0x930aa725), 12275 X64Word_create(0x06ca6351, 0xe003826f), X64Word_create(0x14292967, 0x0a0e6e70), 12276 X64Word_create(0x27b70a85, 0x46d22ffc), X64Word_create(0x2e1b2138, 0x5c26c926), 12277 X64Word_create(0x4d2c6dfc, 0x5ac42aed), X64Word_create(0x53380d13, 0x9d95b3df), 12278 X64Word_create(0x650a7354, 0x8baf63de), X64Word_create(0x766a0abb, 0x3c77b2a8), 12279 X64Word_create(0x81c2c92e, 0x47edaee6), X64Word_create(0x92722c85, 0x1482353b), 12280 X64Word_create(0xa2bfe8a1, 0x4cf10364), X64Word_create(0xa81a664b, 0xbc423001), 12281 X64Word_create(0xc24b8b70, 0xd0f89791), X64Word_create(0xc76c51a3, 0x0654be30), 12282 X64Word_create(0xd192e819, 0xd6ef5218), X64Word_create(0xd6990624, 0x5565a910), 12283 X64Word_create(0xf40e3585, 0x5771202a), X64Word_create(0x106aa070, 0x32bbd1b8), 12284 X64Word_create(0x19a4c116, 0xb8d2d0c8), X64Word_create(0x1e376c08, 0x5141ab53), 12285 X64Word_create(0x2748774c, 0xdf8eeb99), X64Word_create(0x34b0bcb5, 0xe19b48a8), 12286 X64Word_create(0x391c0cb3, 0xc5c95a63), X64Word_create(0x4ed8aa4a, 0xe3418acb), 12287 X64Word_create(0x5b9cca4f, 0x7763e373), X64Word_create(0x682e6ff3, 0xd6b2b8a3), 12288 X64Word_create(0x748f82ee, 0x5defb2fc), X64Word_create(0x78a5636f, 0x43172f60), 12289 X64Word_create(0x84c87814, 0xa1f0ab72), X64Word_create(0x8cc70208, 0x1a6439ec), 12290 X64Word_create(0x90befffa, 0x23631e28), X64Word_create(0xa4506ceb, 0xde82bde9), 12291 X64Word_create(0xbef9a3f7, 0xb2c67915), X64Word_create(0xc67178f2, 0xe372532b), 12292 X64Word_create(0xca273ece, 0xea26619c), X64Word_create(0xd186b8c7, 0x21c0c207), 12293 X64Word_create(0xeada7dd6, 0xcde0eb1e), X64Word_create(0xf57d4f7f, 0xee6ed178), 12294 X64Word_create(0x06f067aa, 0x72176fba), X64Word_create(0x0a637dc5, 0xa2c898a6), 12295 X64Word_create(0x113f9804, 0xbef90dae), X64Word_create(0x1b710b35, 0x131c471b), 12296 X64Word_create(0x28db77f5, 0x23047d84), X64Word_create(0x32caab7b, 0x40c72493), 12297 X64Word_create(0x3c9ebe0a, 0x15c9bebc), X64Word_create(0x431d67c4, 0x9c100d4c), 12298 X64Word_create(0x4cc5d4be, 0xcb3e42b6), X64Word_create(0x597f299c, 0xfc657e2a), 12299 X64Word_create(0x5fcb6fab, 0x3ad6faec), X64Word_create(0x6c44198c, 0x4a475817) 12300 ]; 12301 12302 // Reusable objects 12303 var W = []; 12304 (function () { 12305 for (var i = 0; i < 80; i++) { 12306 W[i] = X64Word_create(); 12307 } 12308 }()); 12309 12310 /** 12311 * SHA-512 hash algorithm. 12312 */ 12313 var SHA512 = C_algo.SHA512 = Hasher.extend({ 12314 _doReset: function () { 12315 this._hash = new X64WordArray.init([ 12316 new X64Word.init(0x6a09e667, 0xf3bcc908), new X64Word.init(0xbb67ae85, 0x84caa73b), 12317 new X64Word.init(0x3c6ef372, 0xfe94f82b), new X64Word.init(0xa54ff53a, 0x5f1d36f1), 12318 new X64Word.init(0x510e527f, 0xade682d1), new X64Word.init(0x9b05688c, 0x2b3e6c1f), 12319 new X64Word.init(0x1f83d9ab, 0xfb41bd6b), new X64Word.init(0x5be0cd19, 0x137e2179) 12320 ]); 12321 }, 12322 12323 _doProcessBlock: function (M, offset) { 12324 // Shortcuts 12325 var H = this._hash.words; 12326 12327 var H0 = H[0]; 12328 var H1 = H[1]; 12329 var H2 = H[2]; 12330 var H3 = H[3]; 12331 var H4 = H[4]; 12332 var H5 = H[5]; 12333 var H6 = H[6]; 12334 var H7 = H[7]; 12335 12336 var H0h = H0.high; 12337 var H0l = H0.low; 12338 var H1h = H1.high; 12339 var H1l = H1.low; 12340 var H2h = H2.high; 12341 var H2l = H2.low; 12342 var H3h = H3.high; 12343 var H3l = H3.low; 12344 var H4h = H4.high; 12345 var H4l = H4.low; 12346 var H5h = H5.high; 12347 var H5l = H5.low; 12348 var H6h = H6.high; 12349 var H6l = H6.low; 12350 var H7h = H7.high; 12351 var H7l = H7.low; 12352 12353 // Working variables 12354 var ah = H0h; 12355 var al = H0l; 12356 var bh = H1h; 12357 var bl = H1l; 12358 var ch = H2h; 12359 var cl = H2l; 12360 var dh = H3h; 12361 var dl = H3l; 12362 var eh = H4h; 12363 var el = H4l; 12364 var fh = H5h; 12365 var fl = H5l; 12366 var gh = H6h; 12367 var gl = H6l; 12368 var hh = H7h; 12369 var hl = H7l; 12370 12371 // Rounds 12372 for (var i = 0; i < 80; i++) { 12373 // Shortcut 12374 var Wi = W[i]; 12375 12376 // Extend message 12377 if (i < 16) { 12378 var Wih = Wi.high = M[offset + i * 2] | 0; 12379 var Wil = Wi.low = M[offset + i * 2 + 1] | 0; 12380 } else { 12381 // Gamma0 12382 var gamma0x = W[i - 15]; 12383 var gamma0xh = gamma0x.high; 12384 var gamma0xl = gamma0x.low; 12385 var gamma0h = ((gamma0xh >>> 1) | (gamma0xl << 31)) ^ ((gamma0xh >>> 8) | (gamma0xl << 24)) ^ (gamma0xh >>> 7); 12386 var gamma0l = ((gamma0xl >>> 1) | (gamma0xh << 31)) ^ ((gamma0xl >>> 8) | (gamma0xh << 24)) ^ ((gamma0xl >>> 7) | (gamma0xh << 25)); 12387 12388 // Gamma1 12389 var gamma1x = W[i - 2]; 12390 var gamma1xh = gamma1x.high; 12391 var gamma1xl = gamma1x.low; 12392 var gamma1h = ((gamma1xh >>> 19) | (gamma1xl << 13)) ^ ((gamma1xh << 3) | (gamma1xl >>> 29)) ^ (gamma1xh >>> 6); 12393 var gamma1l = ((gamma1xl >>> 19) | (gamma1xh << 13)) ^ ((gamma1xl << 3) | (gamma1xh >>> 29)) ^ ((gamma1xl >>> 6) | (gamma1xh << 26)); 12394 12395 // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16] 12396 var Wi7 = W[i - 7]; 12397 var Wi7h = Wi7.high; 12398 var Wi7l = Wi7.low; 12399 12400 var Wi16 = W[i - 16]; 12401 var Wi16h = Wi16.high; 12402 var Wi16l = Wi16.low; 12403 12404 var Wil = gamma0l + Wi7l; 12405 var Wih = gamma0h + Wi7h + ((Wil >>> 0) < (gamma0l >>> 0) ? 1 : 0); 12406 var Wil = Wil + gamma1l; 12407 var Wih = Wih + gamma1h + ((Wil >>> 0) < (gamma1l >>> 0) ? 1 : 0); 12408 var Wil = Wil + Wi16l; 12409 var Wih = Wih + Wi16h + ((Wil >>> 0) < (Wi16l >>> 0) ? 1 : 0); 12410 12411 Wi.high = Wih; 12412 Wi.low = Wil; 12413 } 12414 12415 var chh = (eh & fh) ^ (~eh & gh); 12416 var chl = (el & fl) ^ (~el & gl); 12417 var majh = (ah & bh) ^ (ah & ch) ^ (bh & ch); 12418 var majl = (al & bl) ^ (al & cl) ^ (bl & cl); 12419 12420 var sigma0h = ((ah >>> 28) | (al << 4)) ^ ((ah << 30) | (al >>> 2)) ^ ((ah << 25) | (al >>> 7)); 12421 var sigma0l = ((al >>> 28) | (ah << 4)) ^ ((al << 30) | (ah >>> 2)) ^ ((al << 25) | (ah >>> 7)); 12422 var sigma1h = ((eh >>> 14) | (el << 18)) ^ ((eh >>> 18) | (el << 14)) ^ ((eh << 23) | (el >>> 9)); 12423 var sigma1l = ((el >>> 14) | (eh << 18)) ^ ((el >>> 18) | (eh << 14)) ^ ((el << 23) | (eh >>> 9)); 12424 12425 // t1 = h + sigma1 + ch + K[i] + W[i] 12426 var Ki = K[i]; 12427 var Kih = Ki.high; 12428 var Kil = Ki.low; 12429 12430 var t1l = hl + sigma1l; 12431 var t1h = hh + sigma1h + ((t1l >>> 0) < (hl >>> 0) ? 1 : 0); 12432 var t1l = t1l + chl; 12433 var t1h = t1h + chh + ((t1l >>> 0) < (chl >>> 0) ? 1 : 0); 12434 var t1l = t1l + Kil; 12435 var t1h = t1h + Kih + ((t1l >>> 0) < (Kil >>> 0) ? 1 : 0); 12436 var t1l = t1l + Wil; 12437 var t1h = t1h + Wih + ((t1l >>> 0) < (Wil >>> 0) ? 1 : 0); 12438 12439 // t2 = sigma0 + maj 12440 var t2l = sigma0l + majl; 12441 var t2h = sigma0h + majh + ((t2l >>> 0) < (sigma0l >>> 0) ? 1 : 0); 12442 12443 // Update working variables 12444 hh = gh; 12445 hl = gl; 12446 gh = fh; 12447 gl = fl; 12448 fh = eh; 12449 fl = el; 12450 el = (dl + t1l) | 0; 12451 eh = (dh + t1h + ((el >>> 0) < (dl >>> 0) ? 1 : 0)) | 0; 12452 dh = ch; 12453 dl = cl; 12454 ch = bh; 12455 cl = bl; 12456 bh = ah; 12457 bl = al; 12458 al = (t1l + t2l) | 0; 12459 ah = (t1h + t2h + ((al >>> 0) < (t1l >>> 0) ? 1 : 0)) | 0; 12460 } 12461 12462 // Intermediate hash value 12463 H0l = H0.low = (H0l + al); 12464 H0.high = (H0h + ah + ((H0l >>> 0) < (al >>> 0) ? 1 : 0)); 12465 H1l = H1.low = (H1l + bl); 12466 H1.high = (H1h + bh + ((H1l >>> 0) < (bl >>> 0) ? 1 : 0)); 12467 H2l = H2.low = (H2l + cl); 12468 H2.high = (H2h + ch + ((H2l >>> 0) < (cl >>> 0) ? 1 : 0)); 12469 H3l = H3.low = (H3l + dl); 12470 H3.high = (H3h + dh + ((H3l >>> 0) < (dl >>> 0) ? 1 : 0)); 12471 H4l = H4.low = (H4l + el); 12472 H4.high = (H4h + eh + ((H4l >>> 0) < (el >>> 0) ? 1 : 0)); 12473 H5l = H5.low = (H5l + fl); 12474 H5.high = (H5h + fh + ((H5l >>> 0) < (fl >>> 0) ? 1 : 0)); 12475 H6l = H6.low = (H6l + gl); 12476 H6.high = (H6h + gh + ((H6l >>> 0) < (gl >>> 0) ? 1 : 0)); 12477 H7l = H7.low = (H7l + hl); 12478 H7.high = (H7h + hh + ((H7l >>> 0) < (hl >>> 0) ? 1 : 0)); 12479 }, 12480 12481 _doFinalize: function () { 12482 // Shortcuts 12483 var data = this._data; 12484 var dataWords = data.words; 12485 12486 var nBitsTotal = this._nDataBytes * 8; 12487 var nBitsLeft = data.sigBytes * 8; 12488 12489 // Add padding 12490 dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32); 12491 dataWords[(((nBitsLeft + 128) >>> 10) << 5) + 30] = Math.floor(nBitsTotal / 0x100000000); 12492 dataWords[(((nBitsLeft + 128) >>> 10) << 5) + 31] = nBitsTotal; 12493 data.sigBytes = dataWords.length * 4; 12494 12495 // Hash final blocks 12496 this._process(); 12497 12498 // Convert hash to 32-bit word array before returning 12499 var hash = this._hash.toX32(); 12500 12501 // Return final computed hash 12502 return hash; 12503 }, 12504 12505 clone: function () { 12506 var clone = Hasher.clone.call(this); 12507 clone._hash = this._hash.clone(); 12508 12509 return clone; 12510 }, 12511 12512 blockSize: 1024/32 12513 }); 12514 12515 /** 12516 * Shortcut function to the hasher's object interface. 12517 * 12518 * @param {WordArray|string} message The message to hash. 12519 * 12520 * @return {WordArray} The hash. 12521 * 12522 * @static 12523 * 12524 * @example 12525 * 12526 * var hash = CryptoJS.SHA512('message'); 12527 * var hash = CryptoJS.SHA512(wordArray); 12528 */ 12529 C.SHA512 = Hasher._createHelper(SHA512); 12530 12531 /** 12532 * Shortcut function to the HMAC's object interface. 12533 * 12534 * @param {WordArray|string} message The message to hash. 12535 * @param {WordArray|string} key The secret key. 12536 * 12537 * @return {WordArray} The HMAC. 12538 * 12539 * @static 12540 * 12541 * @example 12542 * 12543 * var hmac = CryptoJS.HmacSHA512(message, key); 12544 */ 12545 C.HmacSHA512 = Hasher._createHmacHelper(SHA512); 12546 }()); 12547 12548 12549 return CryptoJS.SHA512; 12550 12551 })); 12552 },{"./core":53,"./x64-core":84}],83:[function(require,module,exports){ 12553 ;(function (root, factory, undef) { 12554 if (typeof exports === "object") { 12555 // CommonJS 12556 module.exports = exports = factory(require("./core"), require("./enc-base64"), require("./md5"), require("./evpkdf"), require("./cipher-core")); 12557 } 12558 else if (typeof define === "function" && define.amd) { 12559 // AMD 12560 define(["./core", "./enc-base64", "./md5", "./evpkdf", "./cipher-core"], factory); 12561 } 12562 else { 12563 // Global (browser) 12564 factory(root.CryptoJS); 12565 } 12566 }(this, function (CryptoJS) { 12567 12568 (function () { 12569 // Shortcuts 12570 var C = CryptoJS; 12571 var C_lib = C.lib; 12572 var WordArray = C_lib.WordArray; 12573 var BlockCipher = C_lib.BlockCipher; 12574 var C_algo = C.algo; 12575 12576 // Permuted Choice 1 constants 12577 var PC1 = [ 12578 57, 49, 41, 33, 25, 17, 9, 1, 12579 58, 50, 42, 34, 26, 18, 10, 2, 12580 59, 51, 43, 35, 27, 19, 11, 3, 12581 60, 52, 44, 36, 63, 55, 47, 39, 12582 31, 23, 15, 7, 62, 54, 46, 38, 12583 30, 22, 14, 6, 61, 53, 45, 37, 12584 29, 21, 13, 5, 28, 20, 12, 4 12585 ]; 12586 12587 // Permuted Choice 2 constants 12588 var PC2 = [ 12589 14, 17, 11, 24, 1, 5, 12590 3, 28, 15, 6, 21, 10, 12591 23, 19, 12, 4, 26, 8, 12592 16, 7, 27, 20, 13, 2, 12593 41, 52, 31, 37, 47, 55, 12594 30, 40, 51, 45, 33, 48, 12595 44, 49, 39, 56, 34, 53, 12596 46, 42, 50, 36, 29, 32 12597 ]; 12598 12599 // Cumulative bit shift constants 12600 var BIT_SHIFTS = [1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28]; 12601 12602 // SBOXes and round permutation constants 12603 var SBOX_P = [ 12604 { 12605 0x0: 0x808200, 12606 0x10000000: 0x8000, 12607 0x20000000: 0x808002, 12608 0x30000000: 0x2, 12609 0x40000000: 0x200, 12610 0x50000000: 0x808202, 12611 0x60000000: 0x800202, 12612 0x70000000: 0x800000, 12613 0x80000000: 0x202, 12614 0x90000000: 0x800200, 12615 0xa0000000: 0x8200, 12616 0xb0000000: 0x808000, 12617 0xc0000000: 0x8002, 12618 0xd0000000: 0x800002, 12619 0xe0000000: 0x0, 12620 0xf0000000: 0x8202, 12621 0x8000000: 0x0, 12622 0x18000000: 0x808202, 12623 0x28000000: 0x8202, 12624 0x38000000: 0x8000, 12625 0x48000000: 0x808200, 12626 0x58000000: 0x200, 12627 0x68000000: 0x808002, 12628 0x78000000: 0x2, 12629 0x88000000: 0x800200, 12630 0x98000000: 0x8200, 12631 0xa8000000: 0x808000, 12632 0xb8000000: 0x800202, 12633 0xc8000000: 0x800002, 12634 0xd8000000: 0x8002, 12635 0xe8000000: 0x202, 12636 0xf8000000: 0x800000, 12637 0x1: 0x8000, 12638 0x10000001: 0x2, 12639 0x20000001: 0x808200, 12640 0x30000001: 0x800000, 12641 0x40000001: 0x808002, 12642 0x50000001: 0x8200, 12643 0x60000001: 0x200, 12644 0x70000001: 0x800202, 12645 0x80000001: 0x808202, 12646 0x90000001: 0x808000, 12647 0xa0000001: 0x800002, 12648 0xb0000001: 0x8202, 12649 0xc0000001: 0x202, 12650 0xd0000001: 0x800200, 12651 0xe0000001: 0x8002, 12652 0xf0000001: 0x0, 12653 0x8000001: 0x808202, 12654 0x18000001: 0x808000, 12655 0x28000001: 0x800000, 12656 0x38000001: 0x200, 12657 0x48000001: 0x8000, 12658 0x58000001: 0x800002, 12659 0x68000001: 0x2, 12660 0x78000001: 0x8202, 12661 0x88000001: 0x8002, 12662 0x98000001: 0x800202, 12663 0xa8000001: 0x202, 12664 0xb8000001: 0x808200, 12665 0xc8000001: 0x800200, 12666 0xd8000001: 0x0, 12667 0xe8000001: 0x8200, 12668 0xf8000001: 0x808002 12669 }, 12670 { 12671 0x0: 0x40084010, 12672 0x1000000: 0x4000, 12673 0x2000000: 0x80000, 12674 0x3000000: 0x40080010, 12675 0x4000000: 0x40000010, 12676 0x5000000: 0x40084000, 12677 0x6000000: 0x40004000, 12678 0x7000000: 0x10, 12679 0x8000000: 0x84000, 12680 0x9000000: 0x40004010, 12681 0xa000000: 0x40000000, 12682 0xb000000: 0x84010, 12683 0xc000000: 0x80010, 12684 0xd000000: 0x0, 12685 0xe000000: 0x4010, 12686 0xf000000: 0x40080000, 12687 0x800000: 0x40004000, 12688 0x1800000: 0x84010, 12689 0x2800000: 0x10, 12690 0x3800000: 0x40004010, 12691 0x4800000: 0x40084010, 12692 0x5800000: 0x40000000, 12693 0x6800000: 0x80000, 12694 0x7800000: 0x40080010, 12695 0x8800000: 0x80010, 12696 0x9800000: 0x0, 12697 0xa800000: 0x4000, 12698 0xb800000: 0x40080000, 12699 0xc800000: 0x40000010, 12700 0xd800000: 0x84000, 12701 0xe800000: 0x40084000, 12702 0xf800000: 0x4010, 12703 0x10000000: 0x0, 12704 0x11000000: 0x40080010, 12705 0x12000000: 0x40004010, 12706 0x13000000: 0x40084000, 12707 0x14000000: 0x40080000, 12708 0x15000000: 0x10, 12709 0x16000000: 0x84010, 12710 0x17000000: 0x4000, 12711 0x18000000: 0x4010, 12712 0x19000000: 0x80000, 12713 0x1a000000: 0x80010, 12714 0x1b000000: 0x40000010, 12715 0x1c000000: 0x84000, 12716 0x1d000000: 0x40004000, 12717 0x1e000000: 0x40000000, 12718 0x1f000000: 0x40084010, 12719 0x10800000: 0x84010, 12720 0x11800000: 0x80000, 12721 0x12800000: 0x40080000, 12722 0x13800000: 0x4000, 12723 0x14800000: 0x40004000, 12724 0x15800000: 0x40084010, 12725 0x16800000: 0x10, 12726 0x17800000: 0x40000000, 12727 0x18800000: 0x40084000, 12728 0x19800000: 0x40000010, 12729 0x1a800000: 0x40004010, 12730 0x1b800000: 0x80010, 12731 0x1c800000: 0x0, 12732 0x1d800000: 0x4010, 12733 0x1e800000: 0x40080010, 12734 0x1f800000: 0x84000 12735 }, 12736 { 12737 0x0: 0x104, 12738 0x100000: 0x0, 12739 0x200000: 0x4000100, 12740 0x300000: 0x10104, 12741 0x400000: 0x10004, 12742 0x500000: 0x4000004, 12743 0x600000: 0x4010104, 12744 0x700000: 0x4010000, 12745 0x800000: 0x4000000, 12746 0x900000: 0x4010100, 12747 0xa00000: 0x10100, 12748 0xb00000: 0x4010004, 12749 0xc00000: 0x4000104, 12750 0xd00000: 0x10000, 12751 0xe00000: 0x4, 12752 0xf00000: 0x100, 12753 0x80000: 0x4010100, 12754 0x180000: 0x4010004, 12755 0x280000: 0x0, 12756 0x380000: 0x4000100, 12757 0x480000: 0x4000004, 12758 0x580000: 0x10000, 12759 0x680000: 0x10004, 12760 0x780000: 0x104, 12761 0x880000: 0x4, 12762 0x980000: 0x100, 12763 0xa80000: 0x4010000, 12764 0xb80000: 0x10104, 12765 0xc80000: 0x10100, 12766 0xd80000: 0x4000104, 12767 0xe80000: 0x4010104, 12768 0xf80000: 0x4000000, 12769 0x1000000: 0x4010100, 12770 0x1100000: 0x10004, 12771 0x1200000: 0x10000, 12772 0x1300000: 0x4000100, 12773 0x1400000: 0x100, 12774 0x1500000: 0x4010104, 12775 0x1600000: 0x4000004, 12776 0x1700000: 0x0, 12777 0x1800000: 0x4000104, 12778 0x1900000: 0x4000000, 12779 0x1a00000: 0x4, 12780 0x1b00000: 0x10100, 12781 0x1c00000: 0x4010000, 12782 0x1d00000: 0x104, 12783 0x1e00000: 0x10104, 12784 0x1f00000: 0x4010004, 12785 0x1080000: 0x4000000, 12786 0x1180000: 0x104, 12787 0x1280000: 0x4010100, 12788 0x1380000: 0x0, 12789 0x1480000: 0x10004, 12790 0x1580000: 0x4000100, 12791 0x1680000: 0x100, 12792 0x1780000: 0x4010004, 12793 0x1880000: 0x10000, 12794 0x1980000: 0x4010104, 12795 0x1a80000: 0x10104, 12796 0x1b80000: 0x4000004, 12797 0x1c80000: 0x4000104, 12798 0x1d80000: 0x4010000, 12799 0x1e80000: 0x4, 12800 0x1f80000: 0x10100 12801 }, 12802 { 12803 0x0: 0x80401000, 12804 0x10000: 0x80001040, 12805 0x20000: 0x401040, 12806 0x30000: 0x80400000, 12807 0x40000: 0x0, 12808 0x50000: 0x401000, 12809 0x60000: 0x80000040, 12810 0x70000: 0x400040, 12811 0x80000: 0x80000000, 12812 0x90000: 0x400000, 12813 0xa0000: 0x40, 12814 0xb0000: 0x80001000, 12815 0xc0000: 0x80400040, 12816 0xd0000: 0x1040, 12817 0xe0000: 0x1000, 12818 0xf0000: 0x80401040, 12819 0x8000: 0x80001040, 12820 0x18000: 0x40, 12821 0x28000: 0x80400040, 12822 0x38000: 0x80001000, 12823 0x48000: 0x401000, 12824 0x58000: 0x80401040, 12825 0x68000: 0x0, 12826 0x78000: 0x80400000, 12827 0x88000: 0x1000, 12828 0x98000: 0x80401000, 12829 0xa8000: 0x400000, 12830 0xb8000: 0x1040, 12831 0xc8000: 0x80000000, 12832 0xd8000: 0x400040, 12833 0xe8000: 0x401040, 12834 0xf8000: 0x80000040, 12835 0x100000: 0x400040, 12836 0x110000: 0x401000, 12837 0x120000: 0x80000040, 12838 0x130000: 0x0, 12839 0x140000: 0x1040, 12840 0x150000: 0x80400040, 12841 0x160000: 0x80401000, 12842 0x170000: 0x80001040, 12843 0x180000: 0x80401040, 12844 0x190000: 0x80000000, 12845 0x1a0000: 0x80400000, 12846 0x1b0000: 0x401040, 12847 0x1c0000: 0x80001000, 12848 0x1d0000: 0x400000, 12849 0x1e0000: 0x40, 12850 0x1f0000: 0x1000, 12851 0x108000: 0x80400000, 12852 0x118000: 0x80401040, 12853 0x128000: 0x0, 12854 0x138000: 0x401000, 12855 0x148000: 0x400040, 12856 0x158000: 0x80000000, 12857 0x168000: 0x80001040, 12858 0x178000: 0x40, 12859 0x188000: 0x80000040, 12860 0x198000: 0x1000, 12861 0x1a8000: 0x80001000, 12862 0x1b8000: 0x80400040, 12863 0x1c8000: 0x1040, 12864 0x1d8000: 0x80401000, 12865 0x1e8000: 0x400000, 12866 0x1f8000: 0x401040 12867 }, 12868 { 12869 0x0: 0x80, 12870 0x1000: 0x1040000, 12871 0x2000: 0x40000, 12872 0x3000: 0x20000000, 12873 0x4000: 0x20040080, 12874 0x5000: 0x1000080, 12875 0x6000: 0x21000080, 12876 0x7000: 0x40080, 12877 0x8000: 0x1000000, 12878 0x9000: 0x20040000, 12879 0xa000: 0x20000080, 12880 0xb000: 0x21040080, 12881 0xc000: 0x21040000, 12882 0xd000: 0x0, 12883 0xe000: 0x1040080, 12884 0xf000: 0x21000000, 12885 0x800: 0x1040080, 12886 0x1800: 0x21000080, 12887 0x2800: 0x80, 12888 0x3800: 0x1040000, 12889 0x4800: 0x40000, 12890 0x5800: 0x20040080, 12891 0x6800: 0x21040000, 12892 0x7800: 0x20000000, 12893 0x8800: 0x20040000, 12894 0x9800: 0x0, 12895 0xa800: 0x21040080, 12896 0xb800: 0x1000080, 12897 0xc800: 0x20000080, 12898 0xd800: 0x21000000, 12899 0xe800: 0x1000000, 12900 0xf800: 0x40080, 12901 0x10000: 0x40000, 12902 0x11000: 0x80, 12903 0x12000: 0x20000000, 12904 0x13000: 0x21000080, 12905 0x14000: 0x1000080, 12906 0x15000: 0x21040000, 12907 0x16000: 0x20040080, 12908 0x17000: 0x1000000, 12909 0x18000: 0x21040080, 12910 0x19000: 0x21000000, 12911 0x1a000: 0x1040000, 12912 0x1b000: 0x20040000, 12913 0x1c000: 0x40080, 12914 0x1d000: 0x20000080, 12915 0x1e000: 0x0, 12916 0x1f000: 0x1040080, 12917 0x10800: 0x21000080, 12918 0x11800: 0x1000000, 12919 0x12800: 0x1040000, 12920 0x13800: 0x20040080, 12921 0x14800: 0x20000000, 12922 0x15800: 0x1040080, 12923 0x16800: 0x80, 12924 0x17800: 0x21040000, 12925 0x18800: 0x40080, 12926 0x19800: 0x21040080, 12927 0x1a800: 0x0, 12928 0x1b800: 0x21000000, 12929 0x1c800: 0x1000080, 12930 0x1d800: 0x40000, 12931 0x1e800: 0x20040000, 12932 0x1f800: 0x20000080 12933 }, 12934 { 12935 0x0: 0x10000008, 12936 0x100: 0x2000, 12937 0x200: 0x10200000, 12938 0x300: 0x10202008, 12939 0x400: 0x10002000, 12940 0x500: 0x200000, 12941 0x600: 0x200008, 12942 0x700: 0x10000000, 12943 0x800: 0x0, 12944 0x900: 0x10002008, 12945 0xa00: 0x202000, 12946 0xb00: 0x8, 12947 0xc00: 0x10200008, 12948 0xd00: 0x202008, 12949 0xe00: 0x2008, 12950 0xf00: 0x10202000, 12951 0x80: 0x10200000, 12952 0x180: 0x10202008, 12953 0x280: 0x8, 12954 0x380: 0x200000, 12955 0x480: 0x202008, 12956 0x580: 0x10000008, 12957 0x680: 0x10002000, 12958 0x780: 0x2008, 12959 0x880: 0x200008, 12960 0x980: 0x2000, 12961 0xa80: 0x10002008, 12962 0xb80: 0x10200008, 12963 0xc80: 0x0, 12964 0xd80: 0x10202000, 12965 0xe80: 0x202000, 12966 0xf80: 0x10000000, 12967 0x1000: 0x10002000, 12968 0x1100: 0x10200008, 12969 0x1200: 0x10202008, 12970 0x1300: 0x2008, 12971 0x1400: 0x200000, 12972 0x1500: 0x10000000, 12973 0x1600: 0x10000008, 12974 0x1700: 0x202000, 12975 0x1800: 0x202008, 12976 0x1900: 0x0, 12977 0x1a00: 0x8, 12978 0x1b00: 0x10200000, 12979 0x1c00: 0x2000, 12980 0x1d00: 0x10002008, 12981 0x1e00: 0x10202000, 12982 0x1f00: 0x200008, 12983 0x1080: 0x8, 12984 0x1180: 0x202000, 12985 0x1280: 0x200000, 12986 0x1380: 0x10000008, 12987 0x1480: 0x10002000, 12988 0x1580: 0x2008, 12989 0x1680: 0x10202008, 12990 0x1780: 0x10200000, 12991 0x1880: 0x10202000, 12992 0x1980: 0x10200008, 12993 0x1a80: 0x2000, 12994 0x1b80: 0x202008, 12995 0x1c80: 0x200008, 12996 0x1d80: 0x0, 12997 0x1e80: 0x10000000, 12998 0x1f80: 0x10002008 12999 }, 13000 { 13001 0x0: 0x100000, 13002 0x10: 0x2000401, 13003 0x20: 0x400, 13004 0x30: 0x100401, 13005 0x40: 0x2100401, 13006 0x50: 0x0, 13007 0x60: 0x1, 13008 0x70: 0x2100001, 13009 0x80: 0x2000400, 13010 0x90: 0x100001, 13011 0xa0: 0x2000001, 13012 0xb0: 0x2100400, 13013 0xc0: 0x2100000, 13014 0xd0: 0x401, 13015 0xe0: 0x100400, 13016 0xf0: 0x2000000, 13017 0x8: 0x2100001, 13018 0x18: 0x0, 13019 0x28: 0x2000401, 13020 0x38: 0x2100400, 13021 0x48: 0x100000, 13022 0x58: 0x2000001, 13023 0x68: 0x2000000, 13024 0x78: 0x401, 13025 0x88: 0x100401, 13026 0x98: 0x2000400, 13027 0xa8: 0x2100000, 13028 0xb8: 0x100001, 13029 0xc8: 0x400, 13030 0xd8: 0x2100401, 13031 0xe8: 0x1, 13032 0xf8: 0x100400, 13033 0x100: 0x2000000, 13034 0x110: 0x100000, 13035 0x120: 0x2000401, 13036 0x130: 0x2100001, 13037 0x140: 0x100001, 13038 0x150: 0x2000400, 13039 0x160: 0x2100400, 13040 0x170: 0x100401, 13041 0x180: 0x401, 13042 0x190: 0x2100401, 13043 0x1a0: 0x100400, 13044 0x1b0: 0x1, 13045 0x1c0: 0x0, 13046 0x1d0: 0x2100000, 13047 0x1e0: 0x2000001, 13048 0x1f0: 0x400, 13049 0x108: 0x100400, 13050 0x118: 0x2000401, 13051 0x128: 0x2100001, 13052 0x138: 0x1, 13053 0x148: 0x2000000, 13054 0x158: 0x100000, 13055 0x168: 0x401, 13056 0x178: 0x2100400, 13057 0x188: 0x2000001, 13058 0x198: 0x2100000, 13059 0x1a8: 0x0, 13060 0x1b8: 0x2100401, 13061 0x1c8: 0x100401, 13062 0x1d8: 0x400, 13063 0x1e8: 0x2000400, 13064 0x1f8: 0x100001 13065 }, 13066 { 13067 0x0: 0x8000820, 13068 0x1: 0x20000, 13069 0x2: 0x8000000, 13070 0x3: 0x20, 13071 0x4: 0x20020, 13072 0x5: 0x8020820, 13073 0x6: 0x8020800, 13074 0x7: 0x800, 13075 0x8: 0x8020000, 13076 0x9: 0x8000800, 13077 0xa: 0x20800, 13078 0xb: 0x8020020, 13079 0xc: 0x820, 13080 0xd: 0x0, 13081 0xe: 0x8000020, 13082 0xf: 0x20820, 13083 0x80000000: 0x800, 13084 0x80000001: 0x8020820, 13085 0x80000002: 0x8000820, 13086 0x80000003: 0x8000000, 13087 0x80000004: 0x8020000, 13088 0x80000005: 0x20800, 13089 0x80000006: 0x20820, 13090 0x80000007: 0x20, 13091 0x80000008: 0x8000020, 13092 0x80000009: 0x820, 13093 0x8000000a: 0x20020, 13094 0x8000000b: 0x8020800, 13095 0x8000000c: 0x0, 13096 0x8000000d: 0x8020020, 13097 0x8000000e: 0x8000800, 13098 0x8000000f: 0x20000, 13099 0x10: 0x20820, 13100 0x11: 0x8020800, 13101 0x12: 0x20, 13102 0x13: 0x800, 13103 0x14: 0x8000800, 13104 0x15: 0x8000020, 13105 0x16: 0x8020020, 13106 0x17: 0x20000, 13107 0x18: 0x0, 13108 0x19: 0x20020, 13109 0x1a: 0x8020000, 13110 0x1b: 0x8000820, 13111 0x1c: 0x8020820, 13112 0x1d: 0x20800, 13113 0x1e: 0x820, 13114 0x1f: 0x8000000, 13115 0x80000010: 0x20000, 13116 0x80000011: 0x800, 13117 0x80000012: 0x8020020, 13118 0x80000013: 0x20820, 13119 0x80000014: 0x20, 13120 0x80000015: 0x8020000, 13121 0x80000016: 0x8000000, 13122 0x80000017: 0x8000820, 13123 0x80000018: 0x8020820, 13124 0x80000019: 0x8000020, 13125 0x8000001a: 0x8000800, 13126 0x8000001b: 0x0, 13127 0x8000001c: 0x20800, 13128 0x8000001d: 0x820, 13129 0x8000001e: 0x20020, 13130 0x8000001f: 0x8020800 13131 } 13132 ]; 13133 13134 // Masks that select the SBOX input 13135 var SBOX_MASK = [ 13136 0xf8000001, 0x1f800000, 0x01f80000, 0x001f8000, 13137 0x0001f800, 0x00001f80, 0x000001f8, 0x8000001f 13138 ]; 13139 13140 /** 13141 * DES block cipher algorithm. 13142 */ 13143 var DES = C_algo.DES = BlockCipher.extend({ 13144 _doReset: function () { 13145 // Shortcuts 13146 var key = this._key; 13147 var keyWords = key.words; 13148 13149 // Select 56 bits according to PC1 13150 var keyBits = []; 13151 for (var i = 0; i < 56; i++) { 13152 var keyBitPos = PC1[i] - 1; 13153 keyBits[i] = (keyWords[keyBitPos >>> 5] >>> (31 - keyBitPos % 32)) & 1; 13154 } 13155 13156 // Assemble 16 subkeys 13157 var subKeys = this._subKeys = []; 13158 for (var nSubKey = 0; nSubKey < 16; nSubKey++) { 13159 // Create subkey 13160 var subKey = subKeys[nSubKey] = []; 13161 13162 // Shortcut 13163 var bitShift = BIT_SHIFTS[nSubKey]; 13164 13165 // Select 48 bits according to PC2 13166 for (var i = 0; i < 24; i++) { 13167 // Select from the left 28 key bits 13168 subKey[(i / 6) | 0] |= keyBits[((PC2[i] - 1) + bitShift) % 28] << (31 - i % 6); 13169 13170 // Select from the right 28 key bits 13171 subKey[4 + ((i / 6) | 0)] |= keyBits[28 + (((PC2[i + 24] - 1) + bitShift) % 28)] << (31 - i % 6); 13172 } 13173 13174 // Since each subkey is applied to an expanded 32-bit input, 13175 // the subkey can be broken into 8 values scaled to 32-bits, 13176 // which allows the key to be used without expansion 13177 subKey[0] = (subKey[0] << 1) | (subKey[0] >>> 31); 13178 for (var i = 1; i < 7; i++) { 13179 subKey[i] = subKey[i] >>> ((i - 1) * 4 + 3); 13180 } 13181 subKey[7] = (subKey[7] << 5) | (subKey[7] >>> 27); 13182 } 13183 13184 // Compute inverse subkeys 13185 var invSubKeys = this._invSubKeys = []; 13186 for (var i = 0; i < 16; i++) { 13187 invSubKeys[i] = subKeys[15 - i]; 13188 } 13189 }, 13190 13191 encryptBlock: function (M, offset) { 13192 this._doCryptBlock(M, offset, this._subKeys); 13193 }, 13194 13195 decryptBlock: function (M, offset) { 13196 this._doCryptBlock(M, offset, this._invSubKeys); 13197 }, 13198 13199 _doCryptBlock: function (M, offset, subKeys) { 13200 // Get input 13201 this._lBlock = M[offset]; 13202 this._rBlock = M[offset + 1]; 13203 13204 // Initial permutation 13205 exchangeLR.call(this, 4, 0x0f0f0f0f); 13206 exchangeLR.call(this, 16, 0x0000ffff); 13207 exchangeRL.call(this, 2, 0x33333333); 13208 exchangeRL.call(this, 8, 0x00ff00ff); 13209 exchangeLR.call(this, 1, 0x55555555); 13210 13211 // Rounds 13212 for (var round = 0; round < 16; round++) { 13213 // Shortcuts 13214 var subKey = subKeys[round]; 13215 var lBlock = this._lBlock; 13216 var rBlock = this._rBlock; 13217 13218 // Feistel function 13219 var f = 0; 13220 for (var i = 0; i < 8; i++) { 13221 f |= SBOX_P[i][((rBlock ^ subKey[i]) & SBOX_MASK[i]) >>> 0]; 13222 } 13223 this._lBlock = rBlock; 13224 this._rBlock = lBlock ^ f; 13225 } 13226 13227 // Undo swap from last round 13228 var t = this._lBlock; 13229 this._lBlock = this._rBlock; 13230 this._rBlock = t; 13231 13232 // Final permutation 13233 exchangeLR.call(this, 1, 0x55555555); 13234 exchangeRL.call(this, 8, 0x00ff00ff); 13235 exchangeRL.call(this, 2, 0x33333333); 13236 exchangeLR.call(this, 16, 0x0000ffff); 13237 exchangeLR.call(this, 4, 0x0f0f0f0f); 13238 13239 // Set output 13240 M[offset] = this._lBlock; 13241 M[offset + 1] = this._rBlock; 13242 }, 13243 13244 keySize: 64/32, 13245 13246 ivSize: 64/32, 13247 13248 blockSize: 64/32 13249 }); 13250 13251 // Swap bits across the left and right words 13252 function exchangeLR(offset, mask) { 13253 var t = ((this._lBlock >>> offset) ^ this._rBlock) & mask; 13254 this._rBlock ^= t; 13255 this._lBlock ^= t << offset; 13256 } 13257 13258 function exchangeRL(offset, mask) { 13259 var t = ((this._rBlock >>> offset) ^ this._lBlock) & mask; 13260 this._lBlock ^= t; 13261 this._rBlock ^= t << offset; 13262 } 13263 13264 /** 13265 * Shortcut functions to the cipher's object interface. 13266 * 13267 * @example 13268 * 13269 * var ciphertext = CryptoJS.DES.encrypt(message, key, cfg); 13270 * var plaintext = CryptoJS.DES.decrypt(ciphertext, key, cfg); 13271 */ 13272 C.DES = BlockCipher._createHelper(DES); 13273 13274 /** 13275 * Triple-DES block cipher algorithm. 13276 */ 13277 var TripleDES = C_algo.TripleDES = BlockCipher.extend({ 13278 _doReset: function () { 13279 // Shortcuts 13280 var key = this._key; 13281 var keyWords = key.words; 13282 13283 // Create DES instances 13284 this._des1 = DES.createEncryptor(WordArray.create(keyWords.slice(0, 2))); 13285 this._des2 = DES.createEncryptor(WordArray.create(keyWords.slice(2, 4))); 13286 this._des3 = DES.createEncryptor(WordArray.create(keyWords.slice(4, 6))); 13287 }, 13288 13289 encryptBlock: function (M, offset) { 13290 this._des1.encryptBlock(M, offset); 13291 this._des2.decryptBlock(M, offset); 13292 this._des3.encryptBlock(M, offset); 13293 }, 13294 13295 decryptBlock: function (M, offset) { 13296 this._des3.decryptBlock(M, offset); 13297 this._des2.encryptBlock(M, offset); 13298 this._des1.decryptBlock(M, offset); 13299 }, 13300 13301 keySize: 192/32, 13302 13303 ivSize: 64/32, 13304 13305 blockSize: 64/32 13306 }); 13307 13308 /** 13309 * Shortcut functions to the cipher's object interface. 13310 * 13311 * @example 13312 * 13313 * var ciphertext = CryptoJS.TripleDES.encrypt(message, key, cfg); 13314 * var plaintext = CryptoJS.TripleDES.decrypt(ciphertext, key, cfg); 13315 */ 13316 C.TripleDES = BlockCipher._createHelper(TripleDES); 13317 }()); 13318 13319 13320 return CryptoJS.TripleDES; 13321 13322 })); 13323 },{"./cipher-core":52,"./core":53,"./enc-base64":54,"./evpkdf":56,"./md5":61}],84:[function(require,module,exports){ 13324 ;(function (root, factory) { 13325 if (typeof exports === "object") { 13326 // CommonJS 13327 module.exports = exports = factory(require("./core")); 13328 } 13329 else if (typeof define === "function" && define.amd) { 13330 // AMD 13331 define(["./core"], factory); 13332 } 13333 else { 13334 // Global (browser) 13335 factory(root.CryptoJS); 13336 } 13337 }(this, function (CryptoJS) { 13338 13339 (function (undefined) { 13340 // Shortcuts 13341 var C = CryptoJS; 13342 var C_lib = C.lib; 13343 var Base = C_lib.Base; 13344 var X32WordArray = C_lib.WordArray; 13345 13346 /** 13347 * x64 namespace. 13348 */ 13349 var C_x64 = C.x64 = {}; 13350 13351 /** 13352 * A 64-bit word. 13353 */ 13354 var X64Word = C_x64.Word = Base.extend({ 13355 /** 13356 * Initializes a newly created 64-bit word. 13357 * 13358 * @param {number} high The high 32 bits. 13359 * @param {number} low The low 32 bits. 13360 * 13361 * @example 13362 * 13363 * var x64Word = CryptoJS.x64.Word.create(0x00010203, 0x04050607); 13364 */ 13365 init: function (high, low) { 13366 this.high = high; 13367 this.low = low; 13368 } 13369 13370 /** 13371 * Bitwise NOTs this word. 13372 * 13373 * @return {X64Word} A new x64-Word object after negating. 13374 * 13375 * @example 13376 * 13377 * var negated = x64Word.not(); 13378 */ 13379 // not: function () { 13380 // var high = ~this.high; 13381 // var low = ~this.low; 13382 13383 // return X64Word.create(high, low); 13384 // }, 13385 13386 /** 13387 * Bitwise ANDs this word with the passed word. 13388 * 13389 * @param {X64Word} word The x64-Word to AND with this word. 13390 * 13391 * @return {X64Word} A new x64-Word object after ANDing. 13392 * 13393 * @example 13394 * 13395 * var anded = x64Word.and(anotherX64Word); 13396 */ 13397 // and: function (word) { 13398 // var high = this.high & word.high; 13399 // var low = this.low & word.low; 13400 13401 // return X64Word.create(high, low); 13402 // }, 13403 13404 /** 13405 * Bitwise ORs this word with the passed word. 13406 * 13407 * @param {X64Word} word The x64-Word to OR with this word. 13408 * 13409 * @return {X64Word} A new x64-Word object after ORing. 13410 * 13411 * @example 13412 * 13413 * var ored = x64Word.or(anotherX64Word); 13414 */ 13415 // or: function (word) { 13416 // var high = this.high | word.high; 13417 // var low = this.low | word.low; 13418 13419 // return X64Word.create(high, low); 13420 // }, 13421 13422 /** 13423 * Bitwise XORs this word with the passed word. 13424 * 13425 * @param {X64Word} word The x64-Word to XOR with this word. 13426 * 13427 * @return {X64Word} A new x64-Word object after XORing. 13428 * 13429 * @example 13430 * 13431 * var xored = x64Word.xor(anotherX64Word); 13432 */ 13433 // xor: function (word) { 13434 // var high = this.high ^ word.high; 13435 // var low = this.low ^ word.low; 13436 13437 // return X64Word.create(high, low); 13438 // }, 13439 13440 /** 13441 * Shifts this word n bits to the left. 13442 * 13443 * @param {number} n The number of bits to shift. 13444 * 13445 * @return {X64Word} A new x64-Word object after shifting. 13446 * 13447 * @example 13448 * 13449 * var shifted = x64Word.shiftL(25); 13450 */ 13451 // shiftL: function (n) { 13452 // if (n < 32) { 13453 // var high = (this.high << n) | (this.low >>> (32 - n)); 13454 // var low = this.low << n; 13455 // } else { 13456 // var high = this.low << (n - 32); 13457 // var low = 0; 13458 // } 13459 13460 // return X64Word.create(high, low); 13461 // }, 13462 13463 /** 13464 * Shifts this word n bits to the right. 13465 * 13466 * @param {number} n The number of bits to shift. 13467 * 13468 * @return {X64Word} A new x64-Word object after shifting. 13469 * 13470 * @example 13471 * 13472 * var shifted = x64Word.shiftR(7); 13473 */ 13474 // shiftR: function (n) { 13475 // if (n < 32) { 13476 // var low = (this.low >>> n) | (this.high << (32 - n)); 13477 // var high = this.high >>> n; 13478 // } else { 13479 // var low = this.high >>> (n - 32); 13480 // var high = 0; 13481 // } 13482 13483 // return X64Word.create(high, low); 13484 // }, 13485 13486 /** 13487 * Rotates this word n bits to the left. 13488 * 13489 * @param {number} n The number of bits to rotate. 13490 * 13491 * @return {X64Word} A new x64-Word object after rotating. 13492 * 13493 * @example 13494 * 13495 * var rotated = x64Word.rotL(25); 13496 */ 13497 // rotL: function (n) { 13498 // return this.shiftL(n).or(this.shiftR(64 - n)); 13499 // }, 13500 13501 /** 13502 * Rotates this word n bits to the right. 13503 * 13504 * @param {number} n The number of bits to rotate. 13505 * 13506 * @return {X64Word} A new x64-Word object after rotating. 13507 * 13508 * @example 13509 * 13510 * var rotated = x64Word.rotR(7); 13511 */ 13512 // rotR: function (n) { 13513 // return this.shiftR(n).or(this.shiftL(64 - n)); 13514 // }, 13515 13516 /** 13517 * Adds this word with the passed word. 13518 * 13519 * @param {X64Word} word The x64-Word to add with this word. 13520 * 13521 * @return {X64Word} A new x64-Word object after adding. 13522 * 13523 * @example 13524 * 13525 * var added = x64Word.add(anotherX64Word); 13526 */ 13527 // add: function (word) { 13528 // var low = (this.low + word.low) | 0; 13529 // var carry = (low >>> 0) < (this.low >>> 0) ? 1 : 0; 13530 // var high = (this.high + word.high + carry) | 0; 13531 13532 // return X64Word.create(high, low); 13533 // } 13534 }); 13535 13536 /** 13537 * An array of 64-bit words. 13538 * 13539 * @property {Array} words The array of CryptoJS.x64.Word objects. 13540 * @property {number} sigBytes The number of significant bytes in this word array. 13541 */ 13542 var X64WordArray = C_x64.WordArray = Base.extend({ 13543 /** 13544 * Initializes a newly created word array. 13545 * 13546 * @param {Array} words (Optional) An array of CryptoJS.x64.Word objects. 13547 * @param {number} sigBytes (Optional) The number of significant bytes in the words. 13548 * 13549 * @example 13550 * 13551 * var wordArray = CryptoJS.x64.WordArray.create(); 13552 * 13553 * var wordArray = CryptoJS.x64.WordArray.create([ 13554 * CryptoJS.x64.Word.create(0x00010203, 0x04050607), 13555 * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f) 13556 * ]); 13557 * 13558 * var wordArray = CryptoJS.x64.WordArray.create([ 13559 * CryptoJS.x64.Word.create(0x00010203, 0x04050607), 13560 * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f) 13561 * ], 10); 13562 */ 13563 init: function (words, sigBytes) { 13564 words = this.words = words || []; 13565 13566 if (sigBytes != undefined) { 13567 this.sigBytes = sigBytes; 13568 } else { 13569 this.sigBytes = words.length * 8; 13570 } 13571 }, 13572 13573 /** 13574 * Converts this 64-bit word array to a 32-bit word array. 13575 * 13576 * @return {CryptoJS.lib.WordArray} This word array's data as a 32-bit word array. 13577 * 13578 * @example 13579 * 13580 * var x32WordArray = x64WordArray.toX32(); 13581 */ 13582 toX32: function () { 13583 // Shortcuts 13584 var x64Words = this.words; 13585 var x64WordsLength = x64Words.length; 13586 13587 // Convert 13588 var x32Words = []; 13589 for (var i = 0; i < x64WordsLength; i++) { 13590 var x64Word = x64Words[i]; 13591 x32Words.push(x64Word.high); 13592 x32Words.push(x64Word.low); 13593 } 13594 13595 return X32WordArray.create(x32Words, this.sigBytes); 13596 }, 13597 13598 /** 13599 * Creates a copy of this word array. 13600 * 13601 * @return {X64WordArray} The clone. 13602 * 13603 * @example 13604 * 13605 * var clone = x64WordArray.clone(); 13606 */ 13607 clone: function () { 13608 var clone = Base.clone.call(this); 13609 13610 // Clone "words" array 13611 var words = clone.words = this.words.slice(0); 13612 13613 // Clone each X64Word object 13614 var wordsLength = words.length; 13615 for (var i = 0; i < wordsLength; i++) { 13616 words[i] = words[i].clone(); 13617 } 13618 13619 return clone; 13620 } 13621 }); 13622 }()); 13623 13624 13625 return CryptoJS; 13626 13627 })); 13628 },{"./core":53}],85:[function(require,module,exports){ 13629 /*! https://mths.be/utf8js v2.0.0 by @mathias */ 13630 ;(function(root) { 13631 13632 // Detect free variables `exports` 13633 var freeExports = typeof exports == 'object' && exports; 13634 13635 // Detect free variable `module` 13636 var freeModule = typeof module == 'object' && module && 13637 module.exports == freeExports && module; 13638 13639 // Detect free variable `global`, from Node.js or Browserified code, 13640 // and use it as `root` 13641 var freeGlobal = typeof global == 'object' && global; 13642 if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { 13643 root = freeGlobal; 13644 } 13645 13646 /*--------------------------------------------------------------------------*/ 13647 13648 var stringFromCharCode = String.fromCharCode; 13649 13650 // Taken from https://mths.be/punycode 13651 function ucs2decode(string) { 13652 var output = []; 13653 var counter = 0; 13654 var length = string.length; 13655 var value; 13656 var extra; 13657 while (counter < length) { 13658 value = string.charCodeAt(counter++); 13659 if (value >= 0xD800 && value <= 0xDBFF && counter < length) { 13660 // high surrogate, and there is a next character 13661 extra = string.charCodeAt(counter++); 13662 if ((extra & 0xFC00) == 0xDC00) { // low surrogate 13663 output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000); 13664 } else { 13665 // unmatched surrogate; only append this code unit, in case the next 13666 // code unit is the high surrogate of a surrogate pair 13667 output.push(value); 13668 counter--; 13669 } 13670 } else { 13671 output.push(value); 13672 } 13673 } 13674 return output; 13675 } 13676 13677 // Taken from https://mths.be/punycode 13678 function ucs2encode(array) { 13679 var length = array.length; 13680 var index = -1; 13681 var value; 13682 var output = ''; 13683 while (++index < length) { 13684 value = array[index]; 13685 if (value > 0xFFFF) { 13686 value -= 0x10000; 13687 output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800); 13688 value = 0xDC00 | value & 0x3FF; 13689 } 13690 output += stringFromCharCode(value); 13691 } 13692 return output; 13693 } 13694 13695 function checkScalarValue(codePoint) { 13696 if (codePoint >= 0xD800 && codePoint <= 0xDFFF) { 13697 throw Error( 13698 'Lone surrogate U+' + codePoint.toString(16).toUpperCase() + 13699 ' is not a scalar value' 13700 ); 13701 } 13702 } 13703 /*--------------------------------------------------------------------------*/ 13704 13705 function createByte(codePoint, shift) { 13706 return stringFromCharCode(((codePoint >> shift) & 0x3F) | 0x80); 13707 } 13708 13709 function encodeCodePoint(codePoint) { 13710 if ((codePoint & 0xFFFFFF80) == 0) { // 1-byte sequence 13711 return stringFromCharCode(codePoint); 13712 } 13713 var symbol = ''; 13714 if ((codePoint & 0xFFFFF800) == 0) { // 2-byte sequence 13715 symbol = stringFromCharCode(((codePoint >> 6) & 0x1F) | 0xC0); 13716 } 13717 else if ((codePoint & 0xFFFF0000) == 0) { // 3-byte sequence 13718 checkScalarValue(codePoint); 13719 symbol = stringFromCharCode(((codePoint >> 12) & 0x0F) | 0xE0); 13720 symbol += createByte(codePoint, 6); 13721 } 13722 else if ((codePoint & 0xFFE00000) == 0) { // 4-byte sequence 13723 symbol = stringFromCharCode(((codePoint >> 18) & 0x07) | 0xF0); 13724 symbol += createByte(codePoint, 12); 13725 symbol += createByte(codePoint, 6); 13726 } 13727 symbol += stringFromCharCode((codePoint & 0x3F) | 0x80); 13728 return symbol; 13729 } 13730 13731 function utf8encode(string) { 13732 var codePoints = ucs2decode(string); 13733 var length = codePoints.length; 13734 var index = -1; 13735 var codePoint; 13736 var byteString = ''; 13737 while (++index < length) { 13738 codePoint = codePoints[index]; 13739 byteString += encodeCodePoint(codePoint); 13740 } 13741 return byteString; 13742 } 13743 13744 /*--------------------------------------------------------------------------*/ 13745 13746 function readContinuationByte() { 13747 if (byteIndex >= byteCount) { 13748 throw Error('Invalid byte index'); 13749 } 13750 13751 var continuationByte = byteArray[byteIndex] & 0xFF; 13752 byteIndex++; 13753 13754 if ((continuationByte & 0xC0) == 0x80) { 13755 return continuationByte & 0x3F; 13756 } 13757 13758 // If we end up here, it’s not a continuation byte 13759 throw Error('Invalid continuation byte'); 13760 } 13761 13762 function decodeSymbol() { 13763 var byte1; 13764 var byte2; 13765 var byte3; 13766 var byte4; 13767 var codePoint; 13768 13769 if (byteIndex > byteCount) { 13770 throw Error('Invalid byte index'); 13771 } 13772 13773 if (byteIndex == byteCount) { 13774 return false; 13775 } 13776 13777 // Read first byte 13778 byte1 = byteArray[byteIndex] & 0xFF; 13779 byteIndex++; 13780 13781 // 1-byte sequence (no continuation bytes) 13782 if ((byte1 & 0x80) == 0) { 13783 return byte1; 13784 } 13785 13786 // 2-byte sequence 13787 if ((byte1 & 0xE0) == 0xC0) { 13788 var byte2 = readContinuationByte(); 13789 codePoint = ((byte1 & 0x1F) << 6) | byte2; 13790 if (codePoint >= 0x80) { 13791 return codePoint; 13792 } else { 13793 throw Error('Invalid continuation byte'); 13794 } 13795 } 13796 13797 // 3-byte sequence (may include unpaired surrogates) 13798 if ((byte1 & 0xF0) == 0xE0) { 13799 byte2 = readContinuationByte(); 13800 byte3 = readContinuationByte(); 13801 codePoint = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3; 13802 if (codePoint >= 0x0800) { 13803 checkScalarValue(codePoint); 13804 return codePoint; 13805 } else { 13806 throw Error('Invalid continuation byte'); 13807 } 13808 } 13809 13810 // 4-byte sequence 13811 if ((byte1 & 0xF8) == 0xF0) { 13812 byte2 = readContinuationByte(); 13813 byte3 = readContinuationByte(); 13814 byte4 = readContinuationByte(); 13815 codePoint = ((byte1 & 0x0F) << 0x12) | (byte2 << 0x0C) | 13816 (byte3 << 0x06) | byte4; 13817 if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) { 13818 return codePoint; 13819 } 13820 } 13821 13822 throw Error('Invalid UTF-8 detected'); 13823 } 13824 13825 var byteArray; 13826 var byteCount; 13827 var byteIndex; 13828 function utf8decode(byteString) { 13829 byteArray = ucs2decode(byteString); 13830 byteCount = byteArray.length; 13831 byteIndex = 0; 13832 var codePoints = []; 13833 var tmp; 13834 while ((tmp = decodeSymbol()) !== false) { 13835 codePoints.push(tmp); 13836 } 13837 return ucs2encode(codePoints); 13838 } 13839 13840 /*--------------------------------------------------------------------------*/ 13841 13842 var utf8 = { 13843 'version': '2.0.0', 13844 'encode': utf8encode, 13845 'decode': utf8decode 13846 }; 13847 13848 // Some AMD build optimizers, like r.js, check for specific condition patterns 13849 // like the following: 13850 if ( 13851 typeof define == 'function' && 13852 typeof define.amd == 'object' && 13853 define.amd 13854 ) { 13855 define(function() { 13856 return utf8; 13857 }); 13858 } else if (freeExports && !freeExports.nodeType) { 13859 if (freeModule) { // in Node.js or RingoJS v0.8.0+ 13860 freeModule.exports = utf8; 13861 } else { // in Narwhal or RingoJS v0.7.0- 13862 var object = {}; 13863 var hasOwnProperty = object.hasOwnProperty; 13864 for (var key in utf8) { 13865 hasOwnProperty.call(utf8, key) && (freeExports[key] = utf8[key]); 13866 } 13867 } 13868 } else { // in Rhino or a web browser 13869 root.utf8 = utf8; 13870 } 13871 13872 }(this)); 13873 13874 },{}],86:[function(require,module,exports){ 13875 module.exports = XMLHttpRequest; 13876 13877 },{}],"bignumber.js":[function(require,module,exports){ 13878 /*! bignumber.js v2.0.7 https://github.com/MikeMcl/bignumber.js/LICENCE */ 13879 13880 ;(function (global) { 13881 'use strict'; 13882 13883 /* 13884 bignumber.js v2.0.7 13885 A JavaScript library for arbitrary-precision arithmetic. 13886 https://github.com/MikeMcl/bignumber.js 13887 Copyright (c) 2015 Michael Mclaughlin <M8ch88l@gmail.com> 13888 MIT Expat Licence 13889 */ 13890 13891 13892 var BigNumber, crypto, parseNumeric, 13893 isNumeric = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i, 13894 mathceil = Math.ceil, 13895 mathfloor = Math.floor, 13896 notBool = ' not a boolean or binary digit', 13897 roundingMode = 'rounding mode', 13898 tooManyDigits = 'number type has more than 15 significant digits', 13899 ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_', 13900 BASE = 1e14, 13901 LOG_BASE = 14, 13902 MAX_SAFE_INTEGER = 0x1fffffffffffff, // 2^53 - 1 13903 // MAX_INT32 = 0x7fffffff, // 2^31 - 1 13904 POWS_TEN = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13], 13905 SQRT_BASE = 1e7, 13906 13907 /* 13908 * The limit on the value of DECIMAL_PLACES, TO_EXP_NEG, TO_EXP_POS, MIN_EXP, MAX_EXP, and 13909 * the arguments to toExponential, toFixed, toFormat, and toPrecision, beyond which an 13910 * exception is thrown (if ERRORS is true). 13911 */ 13912 MAX = 1E9; // 0 to MAX_INT32 13913 13914 13915 /* 13916 * Create and return a BigNumber constructor. 13917 */ 13918 function another(configObj) { 13919 var div, 13920 13921 // id tracks the caller function, so its name can be included in error messages. 13922 id = 0, 13923 P = BigNumber.prototype, 13924 ONE = new BigNumber(1), 13925 13926 13927 /********************************* EDITABLE DEFAULTS **********************************/ 13928 13929 13930 /* 13931 * The default values below must be integers within the inclusive ranges stated. 13932 * The values can also be changed at run-time using BigNumber.config. 13933 */ 13934 13935 // The maximum number of decimal places for operations involving division. 13936 DECIMAL_PLACES = 20, // 0 to MAX 13937 13938 /* 13939 * The rounding mode used when rounding to the above decimal places, and when using 13940 * toExponential, toFixed, toFormat and toPrecision, and round (default value). 13941 * UP 0 Away from zero. 13942 * DOWN 1 Towards zero. 13943 * CEIL 2 Towards +Infinity. 13944 * FLOOR 3 Towards -Infinity. 13945 * HALF_UP 4 Towards nearest neighbour. If equidistant, up. 13946 * HALF_DOWN 5 Towards nearest neighbour. If equidistant, down. 13947 * HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour. 13948 * HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity. 13949 * HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity. 13950 */ 13951 ROUNDING_MODE = 4, // 0 to 8 13952 13953 // EXPONENTIAL_AT : [TO_EXP_NEG , TO_EXP_POS] 13954 13955 // The exponent value at and beneath which toString returns exponential notation. 13956 // Number type: -7 13957 TO_EXP_NEG = -7, // 0 to -MAX 13958 13959 // The exponent value at and above which toString returns exponential notation. 13960 // Number type: 21 13961 TO_EXP_POS = 21, // 0 to MAX 13962 13963 // RANGE : [MIN_EXP, MAX_EXP] 13964 13965 // The minimum exponent value, beneath which underflow to zero occurs. 13966 // Number type: -324 (5e-324) 13967 MIN_EXP = -1e7, // -1 to -MAX 13968 13969 // The maximum exponent value, above which overflow to Infinity occurs. 13970 // Number type: 308 (1.7976931348623157e+308) 13971 // For MAX_EXP > 1e7, e.g. new BigNumber('1e100000000').plus(1) may be slow. 13972 MAX_EXP = 1e7, // 1 to MAX 13973 13974 // Whether BigNumber Errors are ever thrown. 13975 ERRORS = true, // true or false 13976 13977 // Change to intValidatorNoErrors if ERRORS is false. 13978 isValidInt = intValidatorWithErrors, // intValidatorWithErrors/intValidatorNoErrors 13979 13980 // Whether to use cryptographically-secure random number generation, if available. 13981 CRYPTO = false, // true or false 13982 13983 /* 13984 * The modulo mode used when calculating the modulus: a mod n. 13985 * The quotient (q = a / n) is calculated according to the corresponding rounding mode. 13986 * The remainder (r) is calculated as: r = a - n * q. 13987 * 13988 * UP 0 The remainder is positive if the dividend is negative, else is negative. 13989 * DOWN 1 The remainder has the same sign as the dividend. 13990 * This modulo mode is commonly known as 'truncated division' and is 13991 * equivalent to (a % n) in JavaScript. 13992 * FLOOR 3 The remainder has the same sign as the divisor (Python %). 13993 * HALF_EVEN 6 This modulo mode implements the IEEE 754 remainder function. 13994 * EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)). 13995 * The remainder is always positive. 13996 * 13997 * The truncated division, floored division, Euclidian division and IEEE 754 remainder 13998 * modes are commonly used for the modulus operation. 13999 * Although the other rounding modes can also be used, they may not give useful results. 14000 */ 14001 MODULO_MODE = 1, // 0 to 9 14002 14003 // The maximum number of significant digits of the result of the toPower operation. 14004 // If POW_PRECISION is 0, there will be unlimited significant digits. 14005 POW_PRECISION = 100, // 0 to MAX 14006 14007 // The format specification used by the BigNumber.prototype.toFormat method. 14008 FORMAT = { 14009 decimalSeparator: '.', 14010 groupSeparator: ',', 14011 groupSize: 3, 14012 secondaryGroupSize: 0, 14013 fractionGroupSeparator: '\xA0', // non-breaking space 14014 fractionGroupSize: 0 14015 }; 14016 14017 14018 /******************************************************************************************/ 14019 14020 14021 // CONSTRUCTOR 14022 14023 14024 /* 14025 * The BigNumber constructor and exported function. 14026 * Create and return a new instance of a BigNumber object. 14027 * 14028 * n {number|string|BigNumber} A numeric value. 14029 * [b] {number} The base of n. Integer, 2 to 64 inclusive. 14030 */ 14031 function BigNumber( n, b ) { 14032 var c, e, i, num, len, str, 14033 x = this; 14034 14035 // Enable constructor usage without new. 14036 if ( !( x instanceof BigNumber ) ) { 14037 14038 // 'BigNumber() constructor call without new: {n}' 14039 if (ERRORS) raise( 26, 'constructor call without new', n ); 14040 return new BigNumber( n, b ); 14041 } 14042 14043 // 'new BigNumber() base not an integer: {b}' 14044 // 'new BigNumber() base out of range: {b}' 14045 if ( b == null || !isValidInt( b, 2, 64, id, 'base' ) ) { 14046 14047 // Duplicate. 14048 if ( n instanceof BigNumber ) { 14049 x.s = n.s; 14050 x.e = n.e; 14051 x.c = ( n = n.c ) ? n.slice() : n; 14052 id = 0; 14053 return; 14054 } 14055 14056 if ( ( num = typeof n == 'number' ) && n * 0 == 0 ) { 14057 x.s = 1 / n < 0 ? ( n = -n, -1 ) : 1; 14058 14059 // Fast path for integers. 14060 if ( n === ~~n ) { 14061 for ( e = 0, i = n; i >= 10; i /= 10, e++ ); 14062 x.e = e; 14063 x.c = [n]; 14064 id = 0; 14065 return; 14066 } 14067 14068 str = n + ''; 14069 } else { 14070 if ( !isNumeric.test( str = n + '' ) ) return parseNumeric( x, str, num ); 14071 x.s = str.charCodeAt(0) === 45 ? ( str = str.slice(1), -1 ) : 1; 14072 } 14073 } else { 14074 b = b | 0; 14075 str = n + ''; 14076 14077 // Ensure return value is rounded to DECIMAL_PLACES as with other bases. 14078 // Allow exponential notation to be used with base 10 argument. 14079 if ( b == 10 ) { 14080 x = new BigNumber( n instanceof BigNumber ? n : str ); 14081 return round( x, DECIMAL_PLACES + x.e + 1, ROUNDING_MODE ); 14082 } 14083 14084 // Avoid potential interpretation of Infinity and NaN as base 44+ values. 14085 // Any number in exponential form will fail due to the [Ee][+-]. 14086 if ( ( num = typeof n == 'number' ) && n * 0 != 0 || 14087 !( new RegExp( '^-?' + ( c = '[' + ALPHABET.slice( 0, b ) + ']+' ) + 14088 '(?:\\.' + c + ')?$',b < 37 ? 'i' : '' ) ).test(str) ) { 14089 return parseNumeric( x, str, num, b ); 14090 } 14091 14092 if (num) { 14093 x.s = 1 / n < 0 ? ( str = str.slice(1), -1 ) : 1; 14094 14095 if ( ERRORS && str.replace( /^0\.0*|\./, '' ).length > 15 ) { 14096 14097 // 'new BigNumber() number type has more than 15 significant digits: {n}' 14098 raise( id, tooManyDigits, n ); 14099 } 14100 14101 // Prevent later check for length on converted number. 14102 num = false; 14103 } else { 14104 x.s = str.charCodeAt(0) === 45 ? ( str = str.slice(1), -1 ) : 1; 14105 } 14106 14107 str = convertBase( str, 10, b, x.s ); 14108 } 14109 14110 // Decimal point? 14111 if ( ( e = str.indexOf('.') ) > -1 ) str = str.replace( '.', '' ); 14112 14113 // Exponential form? 14114 if ( ( i = str.search( /e/i ) ) > 0 ) { 14115 14116 // Determine exponent. 14117 if ( e < 0 ) e = i; 14118 e += +str.slice( i + 1 ); 14119 str = str.substring( 0, i ); 14120 } else if ( e < 0 ) { 14121 14122 // Integer. 14123 e = str.length; 14124 } 14125 14126 // Determine leading zeros. 14127 for ( i = 0; str.charCodeAt(i) === 48; i++ ); 14128 14129 // Determine trailing zeros. 14130 for ( len = str.length; str.charCodeAt(--len) === 48; ); 14131 str = str.slice( i, len + 1 ); 14132 14133 if (str) { 14134 len = str.length; 14135 14136 // Disallow numbers with over 15 significant digits if number type. 14137 // 'new BigNumber() number type has more than 15 significant digits: {n}' 14138 if ( num && ERRORS && len > 15 ) raise( id, tooManyDigits, x.s * n ); 14139 14140 e = e - i - 1; 14141 14142 // Overflow? 14143 if ( e > MAX_EXP ) { 14144 14145 // Infinity. 14146 x.c = x.e = null; 14147 14148 // Underflow? 14149 } else if ( e < MIN_EXP ) { 14150 14151 // Zero. 14152 x.c = [ x.e = 0 ]; 14153 } else { 14154 x.e = e; 14155 x.c = []; 14156 14157 // Transform base 14158 14159 // e is the base 10 exponent. 14160 // i is where to slice str to get the first element of the coefficient array. 14161 i = ( e + 1 ) % LOG_BASE; 14162 if ( e < 0 ) i += LOG_BASE; 14163 14164 if ( i < len ) { 14165 if (i) x.c.push( +str.slice( 0, i ) ); 14166 14167 for ( len -= LOG_BASE; i < len; ) { 14168 x.c.push( +str.slice( i, i += LOG_BASE ) ); 14169 } 14170 14171 str = str.slice(i); 14172 i = LOG_BASE - str.length; 14173 } else { 14174 i -= len; 14175 } 14176 14177 for ( ; i--; str += '0' ); 14178 x.c.push( +str ); 14179 } 14180 } else { 14181 14182 // Zero. 14183 x.c = [ x.e = 0 ]; 14184 } 14185 14186 id = 0; 14187 } 14188 14189 14190 // CONSTRUCTOR PROPERTIES 14191 14192 14193 BigNumber.another = another; 14194 14195 BigNumber.ROUND_UP = 0; 14196 BigNumber.ROUND_DOWN = 1; 14197 BigNumber.ROUND_CEIL = 2; 14198 BigNumber.ROUND_FLOOR = 3; 14199 BigNumber.ROUND_HALF_UP = 4; 14200 BigNumber.ROUND_HALF_DOWN = 5; 14201 BigNumber.ROUND_HALF_EVEN = 6; 14202 BigNumber.ROUND_HALF_CEIL = 7; 14203 BigNumber.ROUND_HALF_FLOOR = 8; 14204 BigNumber.EUCLID = 9; 14205 14206 14207 /* 14208 * Configure infrequently-changing library-wide settings. 14209 * 14210 * Accept an object or an argument list, with one or many of the following properties or 14211 * parameters respectively: 14212 * 14213 * DECIMAL_PLACES {number} Integer, 0 to MAX inclusive 14214 * ROUNDING_MODE {number} Integer, 0 to 8 inclusive 14215 * EXPONENTIAL_AT {number|number[]} Integer, -MAX to MAX inclusive or 14216 * [integer -MAX to 0 incl., 0 to MAX incl.] 14217 * RANGE {number|number[]} Non-zero integer, -MAX to MAX inclusive or 14218 * [integer -MAX to -1 incl., integer 1 to MAX incl.] 14219 * ERRORS {boolean|number} true, false, 1 or 0 14220 * CRYPTO {boolean|number} true, false, 1 or 0 14221 * MODULO_MODE {number} 0 to 9 inclusive 14222 * POW_PRECISION {number} 0 to MAX inclusive 14223 * FORMAT {object} See BigNumber.prototype.toFormat 14224 * decimalSeparator {string} 14225 * groupSeparator {string} 14226 * groupSize {number} 14227 * secondaryGroupSize {number} 14228 * fractionGroupSeparator {string} 14229 * fractionGroupSize {number} 14230 * 14231 * (The values assigned to the above FORMAT object properties are not checked for validity.) 14232 * 14233 * E.g. 14234 * BigNumber.config(20, 4) is equivalent to 14235 * BigNumber.config({ DECIMAL_PLACES : 20, ROUNDING_MODE : 4 }) 14236 * 14237 * Ignore properties/parameters set to null or undefined. 14238 * Return an object with the properties current values. 14239 */ 14240 BigNumber.config = function () { 14241 var v, p, 14242 i = 0, 14243 r = {}, 14244 a = arguments, 14245 o = a[0], 14246 has = o && typeof o == 'object' 14247 ? function () { if ( o.hasOwnProperty(p) ) return ( v = o[p] ) != null; } 14248 : function () { if ( a.length > i ) return ( v = a[i++] ) != null; }; 14249 14250 // DECIMAL_PLACES {number} Integer, 0 to MAX inclusive. 14251 // 'config() DECIMAL_PLACES not an integer: {v}' 14252 // 'config() DECIMAL_PLACES out of range: {v}' 14253 if ( has( p = 'DECIMAL_PLACES' ) && isValidInt( v, 0, MAX, 2, p ) ) { 14254 DECIMAL_PLACES = v | 0; 14255 } 14256 r[p] = DECIMAL_PLACES; 14257 14258 // ROUNDING_MODE {number} Integer, 0 to 8 inclusive. 14259 // 'config() ROUNDING_MODE not an integer: {v}' 14260 // 'config() ROUNDING_MODE out of range: {v}' 14261 if ( has( p = 'ROUNDING_MODE' ) && isValidInt( v, 0, 8, 2, p ) ) { 14262 ROUNDING_MODE = v | 0; 14263 } 14264 r[p] = ROUNDING_MODE; 14265 14266 // EXPONENTIAL_AT {number|number[]} 14267 // Integer, -MAX to MAX inclusive or [integer -MAX to 0 inclusive, 0 to MAX inclusive]. 14268 // 'config() EXPONENTIAL_AT not an integer: {v}' 14269 // 'config() EXPONENTIAL_AT out of range: {v}' 14270 if ( has( p = 'EXPONENTIAL_AT' ) ) { 14271 14272 if ( isArray(v) ) { 14273 if ( isValidInt( v[0], -MAX, 0, 2, p ) && isValidInt( v[1], 0, MAX, 2, p ) ) { 14274 TO_EXP_NEG = v[0] | 0; 14275 TO_EXP_POS = v[1] | 0; 14276 } 14277 } else if ( isValidInt( v, -MAX, MAX, 2, p ) ) { 14278 TO_EXP_NEG = -( TO_EXP_POS = ( v < 0 ? -v : v ) | 0 ); 14279 } 14280 } 14281 r[p] = [ TO_EXP_NEG, TO_EXP_POS ]; 14282 14283 // RANGE {number|number[]} Non-zero integer, -MAX to MAX inclusive or 14284 // [integer -MAX to -1 inclusive, integer 1 to MAX inclusive]. 14285 // 'config() RANGE not an integer: {v}' 14286 // 'config() RANGE cannot be zero: {v}' 14287 // 'config() RANGE out of range: {v}' 14288 if ( has( p = 'RANGE' ) ) { 14289 14290 if ( isArray(v) ) { 14291 if ( isValidInt( v[0], -MAX, -1, 2, p ) && isValidInt( v[1], 1, MAX, 2, p ) ) { 14292 MIN_EXP = v[0] | 0; 14293 MAX_EXP = v[1] | 0; 14294 } 14295 } else if ( isValidInt( v, -MAX, MAX, 2, p ) ) { 14296 if ( v | 0 ) MIN_EXP = -( MAX_EXP = ( v < 0 ? -v : v ) | 0 ); 14297 else if (ERRORS) raise( 2, p + ' cannot be zero', v ); 14298 } 14299 } 14300 r[p] = [ MIN_EXP, MAX_EXP ]; 14301 14302 // ERRORS {boolean|number} true, false, 1 or 0. 14303 // 'config() ERRORS not a boolean or binary digit: {v}' 14304 if ( has( p = 'ERRORS' ) ) { 14305 14306 if ( v === !!v || v === 1 || v === 0 ) { 14307 id = 0; 14308 isValidInt = ( ERRORS = !!v ) ? intValidatorWithErrors : intValidatorNoErrors; 14309 } else if (ERRORS) { 14310 raise( 2, p + notBool, v ); 14311 } 14312 } 14313 r[p] = ERRORS; 14314 14315 // CRYPTO {boolean|number} true, false, 1 or 0. 14316 // 'config() CRYPTO not a boolean or binary digit: {v}' 14317 // 'config() crypto unavailable: {crypto}' 14318 if ( has( p = 'CRYPTO' ) ) { 14319 14320 if ( v === !!v || v === 1 || v === 0 ) { 14321 CRYPTO = !!( v && crypto && typeof crypto == 'object' ); 14322 if ( v && !CRYPTO && ERRORS ) raise( 2, 'crypto unavailable', crypto ); 14323 } else if (ERRORS) { 14324 raise( 2, p + notBool, v ); 14325 } 14326 } 14327 r[p] = CRYPTO; 14328 14329 // MODULO_MODE {number} Integer, 0 to 9 inclusive. 14330 // 'config() MODULO_MODE not an integer: {v}' 14331 // 'config() MODULO_MODE out of range: {v}' 14332 if ( has( p = 'MODULO_MODE' ) && isValidInt( v, 0, 9, 2, p ) ) { 14333 MODULO_MODE = v | 0; 14334 } 14335 r[p] = MODULO_MODE; 14336 14337 // POW_PRECISION {number} Integer, 0 to MAX inclusive. 14338 // 'config() POW_PRECISION not an integer: {v}' 14339 // 'config() POW_PRECISION out of range: {v}' 14340 if ( has( p = 'POW_PRECISION' ) && isValidInt( v, 0, MAX, 2, p ) ) { 14341 POW_PRECISION = v | 0; 14342 } 14343 r[p] = POW_PRECISION; 14344 14345 // FORMAT {object} 14346 // 'config() FORMAT not an object: {v}' 14347 if ( has( p = 'FORMAT' ) ) { 14348 14349 if ( typeof v == 'object' ) { 14350 FORMAT = v; 14351 } else if (ERRORS) { 14352 raise( 2, p + ' not an object', v ); 14353 } 14354 } 14355 r[p] = FORMAT; 14356 14357 return r; 14358 }; 14359 14360 14361 /* 14362 * Return a new BigNumber whose value is the maximum of the arguments. 14363 * 14364 * arguments {number|string|BigNumber} 14365 */ 14366 BigNumber.max = function () { return maxOrMin( arguments, P.lt ); }; 14367 14368 14369 /* 14370 * Return a new BigNumber whose value is the minimum of the arguments. 14371 * 14372 * arguments {number|string|BigNumber} 14373 */ 14374 BigNumber.min = function () { return maxOrMin( arguments, P.gt ); }; 14375 14376 14377 /* 14378 * Return a new BigNumber with a random value equal to or greater than 0 and less than 1, 14379 * and with dp, or DECIMAL_PLACES if dp is omitted, decimal places (or less if trailing 14380 * zeros are produced). 14381 * 14382 * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. 14383 * 14384 * 'random() decimal places not an integer: {dp}' 14385 * 'random() decimal places out of range: {dp}' 14386 * 'random() crypto unavailable: {crypto}' 14387 */ 14388 BigNumber.random = (function () { 14389 var pow2_53 = 0x20000000000000; 14390 14391 // Return a 53 bit integer n, where 0 <= n < 9007199254740992. 14392 // Check if Math.random() produces more than 32 bits of randomness. 14393 // If it does, assume at least 53 bits are produced, otherwise assume at least 30 bits. 14394 // 0x40000000 is 2^30, 0x800000 is 2^23, 0x1fffff is 2^21 - 1. 14395 var random53bitInt = (Math.random() * pow2_53) & 0x1fffff 14396 ? function () { return mathfloor( Math.random() * pow2_53 ); } 14397 : function () { return ((Math.random() * 0x40000000 | 0) * 0x800000) + 14398 (Math.random() * 0x800000 | 0); }; 14399 14400 return function (dp) { 14401 var a, b, e, k, v, 14402 i = 0, 14403 c = [], 14404 rand = new BigNumber(ONE); 14405 14406 dp = dp == null || !isValidInt( dp, 0, MAX, 14 ) ? DECIMAL_PLACES : dp | 0; 14407 k = mathceil( dp / LOG_BASE ); 14408 14409 if (CRYPTO) { 14410 14411 // Browsers supporting crypto.getRandomValues. 14412 if ( crypto && crypto.getRandomValues ) { 14413 14414 a = crypto.getRandomValues( new Uint32Array( k *= 2 ) ); 14415 14416 for ( ; i < k; ) { 14417 14418 // 53 bits: 14419 // ((Math.pow(2, 32) - 1) * Math.pow(2, 21)).toString(2) 14420 // 11111 11111111 11111111 11111111 11100000 00000000 00000000 14421 // ((Math.pow(2, 32) - 1) >>> 11).toString(2) 14422 // 11111 11111111 11111111 14423 // 0x20000 is 2^21. 14424 v = a[i] * 0x20000 + (a[i + 1] >>> 11); 14425 14426 // Rejection sampling: 14427 // 0 <= v < 9007199254740992 14428 // Probability that v >= 9e15, is 14429 // 7199254740992 / 9007199254740992 ~= 0.0008, i.e. 1 in 1251 14430 if ( v >= 9e15 ) { 14431 b = crypto.getRandomValues( new Uint32Array(2) ); 14432 a[i] = b[0]; 14433 a[i + 1] = b[1]; 14434 } else { 14435 14436 // 0 <= v <= 8999999999999999 14437 // 0 <= (v % 1e14) <= 99999999999999 14438 c.push( v % 1e14 ); 14439 i += 2; 14440 } 14441 } 14442 i = k / 2; 14443 14444 // Node.js supporting crypto.randomBytes. 14445 } else if ( crypto && crypto.randomBytes ) { 14446 14447 // buffer 14448 a = crypto.randomBytes( k *= 7 ); 14449 14450 for ( ; i < k; ) { 14451 14452 // 0x1000000000000 is 2^48, 0x10000000000 is 2^40 14453 // 0x100000000 is 2^32, 0x1000000 is 2^24 14454 // 11111 11111111 11111111 11111111 11111111 11111111 11111111 14455 // 0 <= v < 9007199254740992 14456 v = ( ( a[i] & 31 ) * 0x1000000000000 ) + ( a[i + 1] * 0x10000000000 ) + 14457 ( a[i + 2] * 0x100000000 ) + ( a[i + 3] * 0x1000000 ) + 14458 ( a[i + 4] << 16 ) + ( a[i + 5] << 8 ) + a[i + 6]; 14459 14460 if ( v >= 9e15 ) { 14461 crypto.randomBytes(7).copy( a, i ); 14462 } else { 14463 14464 // 0 <= (v % 1e14) <= 99999999999999 14465 c.push( v % 1e14 ); 14466 i += 7; 14467 } 14468 } 14469 i = k / 7; 14470 } else if (ERRORS) { 14471 raise( 14, 'crypto unavailable', crypto ); 14472 } 14473 } 14474 14475 // Use Math.random: CRYPTO is false or crypto is unavailable and ERRORS is false. 14476 if (!i) { 14477 14478 for ( ; i < k; ) { 14479 v = random53bitInt(); 14480 if ( v < 9e15 ) c[i++] = v % 1e14; 14481 } 14482 } 14483 14484 k = c[--i]; 14485 dp %= LOG_BASE; 14486 14487 // Convert trailing digits to zeros according to dp. 14488 if ( k && dp ) { 14489 v = POWS_TEN[LOG_BASE - dp]; 14490 c[i] = mathfloor( k / v ) * v; 14491 } 14492 14493 // Remove trailing elements which are zero. 14494 for ( ; c[i] === 0; c.pop(), i-- ); 14495 14496 // Zero? 14497 if ( i < 0 ) { 14498 c = [ e = 0 ]; 14499 } else { 14500 14501 // Remove leading elements which are zero and adjust exponent accordingly. 14502 for ( e = -1 ; c[0] === 0; c.shift(), e -= LOG_BASE); 14503 14504 // Count the digits of the first element of c to determine leading zeros, and... 14505 for ( i = 1, v = c[0]; v >= 10; v /= 10, i++); 14506 14507 // adjust the exponent accordingly. 14508 if ( i < LOG_BASE ) e -= LOG_BASE - i; 14509 } 14510 14511 rand.e = e; 14512 rand.c = c; 14513 return rand; 14514 }; 14515 })(); 14516 14517 14518 // PRIVATE FUNCTIONS 14519 14520 14521 // Convert a numeric string of baseIn to a numeric string of baseOut. 14522 function convertBase( str, baseOut, baseIn, sign ) { 14523 var d, e, k, r, x, xc, y, 14524 i = str.indexOf( '.' ), 14525 dp = DECIMAL_PLACES, 14526 rm = ROUNDING_MODE; 14527 14528 if ( baseIn < 37 ) str = str.toLowerCase(); 14529 14530 // Non-integer. 14531 if ( i >= 0 ) { 14532 k = POW_PRECISION; 14533 14534 // Unlimited precision. 14535 POW_PRECISION = 0; 14536 str = str.replace( '.', '' ); 14537 y = new BigNumber(baseIn); 14538 x = y.pow( str.length - i ); 14539 POW_PRECISION = k; 14540 14541 // Convert str as if an integer, then restore the fraction part by dividing the 14542 // result by its base raised to a power. 14543 y.c = toBaseOut( toFixedPoint( coeffToString( x.c ), x.e ), 10, baseOut ); 14544 y.e = y.c.length; 14545 } 14546 14547 // Convert the number as integer. 14548 xc = toBaseOut( str, baseIn, baseOut ); 14549 e = k = xc.length; 14550 14551 // Remove trailing zeros. 14552 for ( ; xc[--k] == 0; xc.pop() ); 14553 if ( !xc[0] ) return '0'; 14554 14555 if ( i < 0 ) { 14556 --e; 14557 } else { 14558 x.c = xc; 14559 x.e = e; 14560 14561 // sign is needed for correct rounding. 14562 x.s = sign; 14563 x = div( x, y, dp, rm, baseOut ); 14564 xc = x.c; 14565 r = x.r; 14566 e = x.e; 14567 } 14568 14569 d = e + dp + 1; 14570 14571 // The rounding digit, i.e. the digit to the right of the digit that may be rounded up. 14572 i = xc[d]; 14573 k = baseOut / 2; 14574 r = r || d < 0 || xc[d + 1] != null; 14575 14576 r = rm < 4 ? ( i != null || r ) && ( rm == 0 || rm == ( x.s < 0 ? 3 : 2 ) ) 14577 : i > k || i == k &&( rm == 4 || r || rm == 6 && xc[d - 1] & 1 || 14578 rm == ( x.s < 0 ? 8 : 7 ) ); 14579 14580 if ( d < 1 || !xc[0] ) { 14581 14582 // 1^-dp or 0. 14583 str = r ? toFixedPoint( '1', -dp ) : '0'; 14584 } else { 14585 xc.length = d; 14586 14587 if (r) { 14588 14589 // Rounding up may mean the previous digit has to be rounded up and so on. 14590 for ( --baseOut; ++xc[--d] > baseOut; ) { 14591 xc[d] = 0; 14592 14593 if ( !d ) { 14594 ++e; 14595 xc.unshift(1); 14596 } 14597 } 14598 } 14599 14600 // Determine trailing zeros. 14601 for ( k = xc.length; !xc[--k]; ); 14602 14603 // E.g. [4, 11, 15] becomes 4bf. 14604 for ( i = 0, str = ''; i <= k; str += ALPHABET.charAt( xc[i++] ) ); 14605 str = toFixedPoint( str, e ); 14606 } 14607 14608 // The caller will add the sign. 14609 return str; 14610 } 14611 14612 14613 // Perform division in the specified base. Called by div and convertBase. 14614 div = (function () { 14615 14616 // Assume non-zero x and k. 14617 function multiply( x, k, base ) { 14618 var m, temp, xlo, xhi, 14619 carry = 0, 14620 i = x.length, 14621 klo = k % SQRT_BASE, 14622 khi = k / SQRT_BASE | 0; 14623 14624 for ( x = x.slice(); i--; ) { 14625 xlo = x[i] % SQRT_BASE; 14626 xhi = x[i] / SQRT_BASE | 0; 14627 m = khi * xlo + xhi * klo; 14628 temp = klo * xlo + ( ( m % SQRT_BASE ) * SQRT_BASE ) + carry; 14629 carry = ( temp / base | 0 ) + ( m / SQRT_BASE | 0 ) + khi * xhi; 14630 x[i] = temp % base; 14631 } 14632 14633 if (carry) x.unshift(carry); 14634 14635 return x; 14636 } 14637 14638 function compare( a, b, aL, bL ) { 14639 var i, cmp; 14640 14641 if ( aL != bL ) { 14642 cmp = aL > bL ? 1 : -1; 14643 } else { 14644 14645 for ( i = cmp = 0; i < aL; i++ ) { 14646 14647 if ( a[i] != b[i] ) { 14648 cmp = a[i] > b[i] ? 1 : -1; 14649 break; 14650 } 14651 } 14652 } 14653 return cmp; 14654 } 14655 14656 function subtract( a, b, aL, base ) { 14657 var i = 0; 14658 14659 // Subtract b from a. 14660 for ( ; aL--; ) { 14661 a[aL] -= i; 14662 i = a[aL] < b[aL] ? 1 : 0; 14663 a[aL] = i * base + a[aL] - b[aL]; 14664 } 14665 14666 // Remove leading zeros. 14667 for ( ; !a[0] && a.length > 1; a.shift() ); 14668 } 14669 14670 // x: dividend, y: divisor. 14671 return function ( x, y, dp, rm, base ) { 14672 var cmp, e, i, more, n, prod, prodL, q, qc, rem, remL, rem0, xi, xL, yc0, 14673 yL, yz, 14674 s = x.s == y.s ? 1 : -1, 14675 xc = x.c, 14676 yc = y.c; 14677 14678 // Either NaN, Infinity or 0? 14679 if ( !xc || !xc[0] || !yc || !yc[0] ) { 14680 14681 return new BigNumber( 14682 14683 // Return NaN if either NaN, or both Infinity or 0. 14684 !x.s || !y.s || ( xc ? yc && xc[0] == yc[0] : !yc ) ? NaN : 14685 14686 // Return ±0 if x is ±0 or y is ±Infinity, or return ±Infinity as y is ±0. 14687 xc && xc[0] == 0 || !yc ? s * 0 : s / 0 14688 ); 14689 } 14690 14691 q = new BigNumber(s); 14692 qc = q.c = []; 14693 e = x.e - y.e; 14694 s = dp + e + 1; 14695 14696 if ( !base ) { 14697 base = BASE; 14698 e = bitFloor( x.e / LOG_BASE ) - bitFloor( y.e / LOG_BASE ); 14699 s = s / LOG_BASE | 0; 14700 } 14701 14702 // Result exponent may be one less then the current value of e. 14703 // The coefficients of the BigNumbers from convertBase may have trailing zeros. 14704 for ( i = 0; yc[i] == ( xc[i] || 0 ); i++ ); 14705 if ( yc[i] > ( xc[i] || 0 ) ) e--; 14706 14707 if ( s < 0 ) { 14708 qc.push(1); 14709 more = true; 14710 } else { 14711 xL = xc.length; 14712 yL = yc.length; 14713 i = 0; 14714 s += 2; 14715 14716 // Normalise xc and yc so highest order digit of yc is >= base / 2. 14717 14718 n = mathfloor( base / ( yc[0] + 1 ) ); 14719 14720 // Not necessary, but to handle odd bases where yc[0] == ( base / 2 ) - 1. 14721 // if ( n > 1 || n++ == 1 && yc[0] < base / 2 ) { 14722 if ( n > 1 ) { 14723 yc = multiply( yc, n, base ); 14724 xc = multiply( xc, n, base ); 14725 yL = yc.length; 14726 xL = xc.length; 14727 } 14728 14729 xi = yL; 14730 rem = xc.slice( 0, yL ); 14731 remL = rem.length; 14732 14733 // Add zeros to make remainder as long as divisor. 14734 for ( ; remL < yL; rem[remL++] = 0 ); 14735 yz = yc.slice(); 14736 yz.unshift(0); 14737 yc0 = yc[0]; 14738 if ( yc[1] >= base / 2 ) yc0++; 14739 // Not necessary, but to prevent trial digit n > base, when using base 3. 14740 // else if ( base == 3 && yc0 == 1 ) yc0 = 1 + 1e-15; 14741 14742 do { 14743 n = 0; 14744 14745 // Compare divisor and remainder. 14746 cmp = compare( yc, rem, yL, remL ); 14747 14748 // If divisor < remainder. 14749 if ( cmp < 0 ) { 14750 14751 // Calculate trial digit, n. 14752 14753 rem0 = rem[0]; 14754 if ( yL != remL ) rem0 = rem0 * base + ( rem[1] || 0 ); 14755 14756 // n is how many times the divisor goes into the current remainder. 14757 n = mathfloor( rem0 / yc0 ); 14758 14759 // Algorithm: 14760 // 1. product = divisor * trial digit (n) 14761 // 2. if product > remainder: product -= divisor, n-- 14762 // 3. remainder -= product 14763 // 4. if product was < remainder at 2: 14764 // 5. compare new remainder and divisor 14765 // 6. If remainder > divisor: remainder -= divisor, n++ 14766 14767 if ( n > 1 ) { 14768 14769 // n may be > base only when base is 3. 14770 if (n >= base) n = base - 1; 14771 14772 // product = divisor * trial digit. 14773 prod = multiply( yc, n, base ); 14774 prodL = prod.length; 14775 remL = rem.length; 14776 14777 // Compare product and remainder. 14778 // If product > remainder. 14779 // Trial digit n too high. 14780 // n is 1 too high about 5% of the time, and is not known to have 14781 // ever been more than 1 too high. 14782 while ( compare( prod, rem, prodL, remL ) == 1 ) { 14783 n--; 14784 14785 // Subtract divisor from product. 14786 subtract( prod, yL < prodL ? yz : yc, prodL, base ); 14787 prodL = prod.length; 14788 cmp = 1; 14789 } 14790 } else { 14791 14792 // n is 0 or 1, cmp is -1. 14793 // If n is 0, there is no need to compare yc and rem again below, 14794 // so change cmp to 1 to avoid it. 14795 // If n is 1, leave cmp as -1, so yc and rem are compared again. 14796 if ( n == 0 ) { 14797 14798 // divisor < remainder, so n must be at least 1. 14799 cmp = n = 1; 14800 } 14801 14802 // product = divisor 14803 prod = yc.slice(); 14804 prodL = prod.length; 14805 } 14806 14807 if ( prodL < remL ) prod.unshift(0); 14808 14809 // Subtract product from remainder. 14810 subtract( rem, prod, remL, base ); 14811 remL = rem.length; 14812 14813 // If product was < remainder. 14814 if ( cmp == -1 ) { 14815 14816 // Compare divisor and new remainder. 14817 // If divisor < new remainder, subtract divisor from remainder. 14818 // Trial digit n too low. 14819 // n is 1 too low about 5% of the time, and very rarely 2 too low. 14820 while ( compare( yc, rem, yL, remL ) < 1 ) { 14821 n++; 14822 14823 // Subtract divisor from remainder. 14824 subtract( rem, yL < remL ? yz : yc, remL, base ); 14825 remL = rem.length; 14826 } 14827 } 14828 } else if ( cmp === 0 ) { 14829 n++; 14830 rem = [0]; 14831 } // else cmp === 1 and n will be 0 14832 14833 // Add the next digit, n, to the result array. 14834 qc[i++] = n; 14835 14836 // Update the remainder. 14837 if ( rem[0] ) { 14838 rem[remL++] = xc[xi] || 0; 14839 } else { 14840 rem = [ xc[xi] ]; 14841 remL = 1; 14842 } 14843 } while ( ( xi++ < xL || rem[0] != null ) && s-- ); 14844 14845 more = rem[0] != null; 14846 14847 // Leading zero? 14848 if ( !qc[0] ) qc.shift(); 14849 } 14850 14851 if ( base == BASE ) { 14852 14853 // To calculate q.e, first get the number of digits of qc[0]. 14854 for ( i = 1, s = qc[0]; s >= 10; s /= 10, i++ ); 14855 round( q, dp + ( q.e = i + e * LOG_BASE - 1 ) + 1, rm, more ); 14856 14857 // Caller is convertBase. 14858 } else { 14859 q.e = e; 14860 q.r = +more; 14861 } 14862 14863 return q; 14864 }; 14865 })(); 14866 14867 14868 /* 14869 * Return a string representing the value of BigNumber n in fixed-point or exponential 14870 * notation rounded to the specified decimal places or significant digits. 14871 * 14872 * n is a BigNumber. 14873 * i is the index of the last digit required (i.e. the digit that may be rounded up). 14874 * rm is the rounding mode. 14875 * caller is caller id: toExponential 19, toFixed 20, toFormat 21, toPrecision 24. 14876 */ 14877 function format( n, i, rm, caller ) { 14878 var c0, e, ne, len, str; 14879 14880 rm = rm != null && isValidInt( rm, 0, 8, caller, roundingMode ) 14881 ? rm | 0 : ROUNDING_MODE; 14882 14883 if ( !n.c ) return n.toString(); 14884 c0 = n.c[0]; 14885 ne = n.e; 14886 14887 if ( i == null ) { 14888 str = coeffToString( n.c ); 14889 str = caller == 19 || caller == 24 && ne <= TO_EXP_NEG 14890 ? toExponential( str, ne ) 14891 : toFixedPoint( str, ne ); 14892 } else { 14893 n = round( new BigNumber(n), i, rm ); 14894 14895 // n.e may have changed if the value was rounded up. 14896 e = n.e; 14897 14898 str = coeffToString( n.c ); 14899 len = str.length; 14900 14901 // toPrecision returns exponential notation if the number of significant digits 14902 // specified is less than the number of digits necessary to represent the integer 14903 // part of the value in fixed-point notation. 14904 14905 // Exponential notation. 14906 if ( caller == 19 || caller == 24 && ( i <= e || e <= TO_EXP_NEG ) ) { 14907 14908 // Append zeros? 14909 for ( ; len < i; str += '0', len++ ); 14910 str = toExponential( str, e ); 14911 14912 // Fixed-point notation. 14913 } else { 14914 i -= ne; 14915 str = toFixedPoint( str, e ); 14916 14917 // Append zeros? 14918 if ( e + 1 > len ) { 14919 if ( --i > 0 ) for ( str += '.'; i--; str += '0' ); 14920 } else { 14921 i += e - len; 14922 if ( i > 0 ) { 14923 if ( e + 1 == len ) str += '.'; 14924 for ( ; i--; str += '0' ); 14925 } 14926 } 14927 } 14928 } 14929 14930 return n.s < 0 && c0 ? '-' + str : str; 14931 } 14932 14933 14934 // Handle BigNumber.max and BigNumber.min. 14935 function maxOrMin( args, method ) { 14936 var m, n, 14937 i = 0; 14938 14939 if ( isArray( args[0] ) ) args = args[0]; 14940 m = new BigNumber( args[0] ); 14941 14942 for ( ; ++i < args.length; ) { 14943 n = new BigNumber( args[i] ); 14944 14945 // If any number is NaN, return NaN. 14946 if ( !n.s ) { 14947 m = n; 14948 break; 14949 } else if ( method.call( m, n ) ) { 14950 m = n; 14951 } 14952 } 14953 14954 return m; 14955 } 14956 14957 14958 /* 14959 * Return true if n is an integer in range, otherwise throw. 14960 * Use for argument validation when ERRORS is true. 14961 */ 14962 function intValidatorWithErrors( n, min, max, caller, name ) { 14963 if ( n < min || n > max || n != truncate(n) ) { 14964 raise( caller, ( name || 'decimal places' ) + 14965 ( n < min || n > max ? ' out of range' : ' not an integer' ), n ); 14966 } 14967 14968 return true; 14969 } 14970 14971 14972 /* 14973 * Strip trailing zeros, calculate base 10 exponent and check against MIN_EXP and MAX_EXP. 14974 * Called by minus, plus and times. 14975 */ 14976 function normalise( n, c, e ) { 14977 var i = 1, 14978 j = c.length; 14979 14980 // Remove trailing zeros. 14981 for ( ; !c[--j]; c.pop() ); 14982 14983 // Calculate the base 10 exponent. First get the number of digits of c[0]. 14984 for ( j = c[0]; j >= 10; j /= 10, i++ ); 14985 14986 // Overflow? 14987 if ( ( e = i + e * LOG_BASE - 1 ) > MAX_EXP ) { 14988 14989 // Infinity. 14990 n.c = n.e = null; 14991 14992 // Underflow? 14993 } else if ( e < MIN_EXP ) { 14994 14995 // Zero. 14996 n.c = [ n.e = 0 ]; 14997 } else { 14998 n.e = e; 14999 n.c = c; 15000 } 15001 15002 return n; 15003 } 15004 15005 15006 // Handle values that fail the validity test in BigNumber. 15007 parseNumeric = (function () { 15008 var basePrefix = /^(-?)0([xbo])/i, 15009 dotAfter = /^([^.]+)\.$/, 15010 dotBefore = /^\.([^.]+)$/, 15011 isInfinityOrNaN = /^-?(Infinity|NaN)$/, 15012 whitespaceOrPlus = /^\s*\+|^\s+|\s+$/g; 15013 15014 return function ( x, str, num, b ) { 15015 var base, 15016 s = num ? str : str.replace( whitespaceOrPlus, '' ); 15017 15018 // No exception on ±Infinity or NaN. 15019 if ( isInfinityOrNaN.test(s) ) { 15020 x.s = isNaN(s) ? null : s < 0 ? -1 : 1; 15021 } else { 15022 if ( !num ) { 15023 15024 // basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i 15025 s = s.replace( basePrefix, function ( m, p1, p2 ) { 15026 base = ( p2 = p2.toLowerCase() ) == 'x' ? 16 : p2 == 'b' ? 2 : 8; 15027 return !b || b == base ? p1 : m; 15028 }); 15029 15030 if (b) { 15031 base = b; 15032 15033 // E.g. '1.' to '1', '.1' to '0.1' 15034 s = s.replace( dotAfter, '$1' ).replace( dotBefore, '0.$1' ); 15035 } 15036 15037 if ( str != s ) return new BigNumber( s, base ); 15038 } 15039 15040 // 'new BigNumber() not a number: {n}' 15041 // 'new BigNumber() not a base {b} number: {n}' 15042 if (ERRORS) raise( id, 'not a' + ( b ? ' base ' + b : '' ) + ' number', str ); 15043 x.s = null; 15044 } 15045 15046 x.c = x.e = null; 15047 id = 0; 15048 } 15049 })(); 15050 15051 15052 // Throw a BigNumber Error. 15053 function raise( caller, msg, val ) { 15054 var error = new Error( [ 15055 'new BigNumber', // 0 15056 'cmp', // 1 15057 'config', // 2 15058 'div', // 3 15059 'divToInt', // 4 15060 'eq', // 5 15061 'gt', // 6 15062 'gte', // 7 15063 'lt', // 8 15064 'lte', // 9 15065 'minus', // 10 15066 'mod', // 11 15067 'plus', // 12 15068 'precision', // 13 15069 'random', // 14 15070 'round', // 15 15071 'shift', // 16 15072 'times', // 17 15073 'toDigits', // 18 15074 'toExponential', // 19 15075 'toFixed', // 20 15076 'toFormat', // 21 15077 'toFraction', // 22 15078 'pow', // 23 15079 'toPrecision', // 24 15080 'toString', // 25 15081 'BigNumber' // 26 15082 ][caller] + '() ' + msg + ': ' + val ); 15083 15084 error.name = 'BigNumber Error'; 15085 id = 0; 15086 throw error; 15087 } 15088 15089 15090 /* 15091 * Round x to sd significant digits using rounding mode rm. Check for over/under-flow. 15092 * If r is truthy, it is known that there are more digits after the rounding digit. 15093 */ 15094 function round( x, sd, rm, r ) { 15095 var d, i, j, k, n, ni, rd, 15096 xc = x.c, 15097 pows10 = POWS_TEN; 15098 15099 // if x is not Infinity or NaN... 15100 if (xc) { 15101 15102 // rd is the rounding digit, i.e. the digit after the digit that may be rounded up. 15103 // n is a base 1e14 number, the value of the element of array x.c containing rd. 15104 // ni is the index of n within x.c. 15105 // d is the number of digits of n. 15106 // i is the index of rd within n including leading zeros. 15107 // j is the actual index of rd within n (if < 0, rd is a leading zero). 15108 out: { 15109 15110 // Get the number of digits of the first element of xc. 15111 for ( d = 1, k = xc[0]; k >= 10; k /= 10, d++ ); 15112 i = sd - d; 15113 15114 // If the rounding digit is in the first element of xc... 15115 if ( i < 0 ) { 15116 i += LOG_BASE; 15117 j = sd; 15118 n = xc[ ni = 0 ]; 15119 15120 // Get the rounding digit at index j of n. 15121 rd = n / pows10[ d - j - 1 ] % 10 | 0; 15122 } else { 15123 ni = mathceil( ( i + 1 ) / LOG_BASE ); 15124 15125 if ( ni >= xc.length ) { 15126 15127 if (r) { 15128 15129 // Needed by sqrt. 15130 for ( ; xc.length <= ni; xc.push(0) ); 15131 n = rd = 0; 15132 d = 1; 15133 i %= LOG_BASE; 15134 j = i - LOG_BASE + 1; 15135 } else { 15136 break out; 15137 } 15138 } else { 15139 n = k = xc[ni]; 15140 15141 // Get the number of digits of n. 15142 for ( d = 1; k >= 10; k /= 10, d++ ); 15143 15144 // Get the index of rd within n. 15145 i %= LOG_BASE; 15146 15147 // Get the index of rd within n, adjusted for leading zeros. 15148 // The number of leading zeros of n is given by LOG_BASE - d. 15149 j = i - LOG_BASE + d; 15150 15151 // Get the rounding digit at index j of n. 15152 rd = j < 0 ? 0 : n / pows10[ d - j - 1 ] % 10 | 0; 15153 } 15154 } 15155 15156 r = r || sd < 0 || 15157 15158 // Are there any non-zero digits after the rounding digit? 15159 // The expression n % pows10[ d - j - 1 ] returns all digits of n to the right 15160 // of the digit at j, e.g. if n is 908714 and j is 2, the expression gives 714. 15161 xc[ni + 1] != null || ( j < 0 ? n : n % pows10[ d - j - 1 ] ); 15162 15163 r = rm < 4 15164 ? ( rd || r ) && ( rm == 0 || rm == ( x.s < 0 ? 3 : 2 ) ) 15165 : rd > 5 || rd == 5 && ( rm == 4 || r || rm == 6 && 15166 15167 // Check whether the digit to the left of the rounding digit is odd. 15168 ( ( i > 0 ? j > 0 ? n / pows10[ d - j ] : 0 : xc[ni - 1] ) % 10 ) & 1 || 15169 rm == ( x.s < 0 ? 8 : 7 ) ); 15170 15171 if ( sd < 1 || !xc[0] ) { 15172 xc.length = 0; 15173 15174 if (r) { 15175 15176 // Convert sd to decimal places. 15177 sd -= x.e + 1; 15178 15179 // 1, 0.1, 0.01, 0.001, 0.0001 etc. 15180 xc[0] = pows10[ sd % LOG_BASE ]; 15181 x.e = -sd || 0; 15182 } else { 15183 15184 // Zero. 15185 xc[0] = x.e = 0; 15186 } 15187 15188 return x; 15189 } 15190 15191 // Remove excess digits. 15192 if ( i == 0 ) { 15193 xc.length = ni; 15194 k = 1; 15195 ni--; 15196 } else { 15197 xc.length = ni + 1; 15198 k = pows10[ LOG_BASE - i ]; 15199 15200 // E.g. 56700 becomes 56000 if 7 is the rounding digit. 15201 // j > 0 means i > number of leading zeros of n. 15202 xc[ni] = j > 0 ? mathfloor( n / pows10[ d - j ] % pows10[j] ) * k : 0; 15203 } 15204 15205 // Round up? 15206 if (r) { 15207 15208 for ( ; ; ) { 15209 15210 // If the digit to be rounded up is in the first element of xc... 15211 if ( ni == 0 ) { 15212 15213 // i will be the length of xc[0] before k is added. 15214 for ( i = 1, j = xc[0]; j >= 10; j /= 10, i++ ); 15215 j = xc[0] += k; 15216 for ( k = 1; j >= 10; j /= 10, k++ ); 15217 15218 // if i != k the length has increased. 15219 if ( i != k ) { 15220 x.e++; 15221 if ( xc[0] == BASE ) xc[0] = 1; 15222 } 15223 15224 break; 15225 } else { 15226 xc[ni] += k; 15227 if ( xc[ni] != BASE ) break; 15228 xc[ni--] = 0; 15229 k = 1; 15230 } 15231 } 15232 } 15233 15234 // Remove trailing zeros. 15235 for ( i = xc.length; xc[--i] === 0; xc.pop() ); 15236 } 15237 15238 // Overflow? Infinity. 15239 if ( x.e > MAX_EXP ) { 15240 x.c = x.e = null; 15241 15242 // Underflow? Zero. 15243 } else if ( x.e < MIN_EXP ) { 15244 x.c = [ x.e = 0 ]; 15245 } 15246 } 15247 15248 return x; 15249 } 15250 15251 15252 // PROTOTYPE/INSTANCE METHODS 15253 15254 15255 /* 15256 * Return a new BigNumber whose value is the absolute value of this BigNumber. 15257 */ 15258 P.absoluteValue = P.abs = function () { 15259 var x = new BigNumber(this); 15260 if ( x.s < 0 ) x.s = 1; 15261 return x; 15262 }; 15263 15264 15265 /* 15266 * Return a new BigNumber whose value is the value of this BigNumber rounded to a whole 15267 * number in the direction of Infinity. 15268 */ 15269 P.ceil = function () { 15270 return round( new BigNumber(this), this.e + 1, 2 ); 15271 }; 15272 15273 15274 /* 15275 * Return 15276 * 1 if the value of this BigNumber is greater than the value of BigNumber(y, b), 15277 * -1 if the value of this BigNumber is less than the value of BigNumber(y, b), 15278 * 0 if they have the same value, 15279 * or null if the value of either is NaN. 15280 */ 15281 P.comparedTo = P.cmp = function ( y, b ) { 15282 id = 1; 15283 return compare( this, new BigNumber( y, b ) ); 15284 }; 15285 15286 15287 /* 15288 * Return the number of decimal places of the value of this BigNumber, or null if the value 15289 * of this BigNumber is ±Infinity or NaN. 15290 */ 15291 P.decimalPlaces = P.dp = function () { 15292 var n, v, 15293 c = this.c; 15294 15295 if ( !c ) return null; 15296 n = ( ( v = c.length - 1 ) - bitFloor( this.e / LOG_BASE ) ) * LOG_BASE; 15297 15298 // Subtract the number of trailing zeros of the last number. 15299 if ( v = c[v] ) for ( ; v % 10 == 0; v /= 10, n-- ); 15300 if ( n < 0 ) n = 0; 15301 15302 return n; 15303 }; 15304 15305 15306 /* 15307 * n / 0 = I 15308 * n / N = N 15309 * n / I = 0 15310 * 0 / n = 0 15311 * 0 / 0 = N 15312 * 0 / N = N 15313 * 0 / I = 0 15314 * N / n = N 15315 * N / 0 = N 15316 * N / N = N 15317 * N / I = N 15318 * I / n = I 15319 * I / 0 = I 15320 * I / N = N 15321 * I / I = N 15322 * 15323 * Return a new BigNumber whose value is the value of this BigNumber divided by the value of 15324 * BigNumber(y, b), rounded according to DECIMAL_PLACES and ROUNDING_MODE. 15325 */ 15326 P.dividedBy = P.div = function ( y, b ) { 15327 id = 3; 15328 return div( this, new BigNumber( y, b ), DECIMAL_PLACES, ROUNDING_MODE ); 15329 }; 15330 15331 15332 /* 15333 * Return a new BigNumber whose value is the integer part of dividing the value of this 15334 * BigNumber by the value of BigNumber(y, b). 15335 */ 15336 P.dividedToIntegerBy = P.divToInt = function ( y, b ) { 15337 id = 4; 15338 return div( this, new BigNumber( y, b ), 0, 1 ); 15339 }; 15340 15341 15342 /* 15343 * Return true if the value of this BigNumber is equal to the value of BigNumber(y, b), 15344 * otherwise returns false. 15345 */ 15346 P.equals = P.eq = function ( y, b ) { 15347 id = 5; 15348 return compare( this, new BigNumber( y, b ) ) === 0; 15349 }; 15350 15351 15352 /* 15353 * Return a new BigNumber whose value is the value of this BigNumber rounded to a whole 15354 * number in the direction of -Infinity. 15355 */ 15356 P.floor = function () { 15357 return round( new BigNumber(this), this.e + 1, 3 ); 15358 }; 15359 15360 15361 /* 15362 * Return true if the value of this BigNumber is greater than the value of BigNumber(y, b), 15363 * otherwise returns false. 15364 */ 15365 P.greaterThan = P.gt = function ( y, b ) { 15366 id = 6; 15367 return compare( this, new BigNumber( y, b ) ) > 0; 15368 }; 15369 15370 15371 /* 15372 * Return true if the value of this BigNumber is greater than or equal to the value of 15373 * BigNumber(y, b), otherwise returns false. 15374 */ 15375 P.greaterThanOrEqualTo = P.gte = function ( y, b ) { 15376 id = 7; 15377 return ( b = compare( this, new BigNumber( y, b ) ) ) === 1 || b === 0; 15378 15379 }; 15380 15381 15382 /* 15383 * Return true if the value of this BigNumber is a finite number, otherwise returns false. 15384 */ 15385 P.isFinite = function () { 15386 return !!this.c; 15387 }; 15388 15389 15390 /* 15391 * Return true if the value of this BigNumber is an integer, otherwise return false. 15392 */ 15393 P.isInteger = P.isInt = function () { 15394 return !!this.c && bitFloor( this.e / LOG_BASE ) > this.c.length - 2; 15395 }; 15396 15397 15398 /* 15399 * Return true if the value of this BigNumber is NaN, otherwise returns false. 15400 */ 15401 P.isNaN = function () { 15402 return !this.s; 15403 }; 15404 15405 15406 /* 15407 * Return true if the value of this BigNumber is negative, otherwise returns false. 15408 */ 15409 P.isNegative = P.isNeg = function () { 15410 return this.s < 0; 15411 }; 15412 15413 15414 /* 15415 * Return true if the value of this BigNumber is 0 or -0, otherwise returns false. 15416 */ 15417 P.isZero = function () { 15418 return !!this.c && this.c[0] == 0; 15419 }; 15420 15421 15422 /* 15423 * Return true if the value of this BigNumber is less than the value of BigNumber(y, b), 15424 * otherwise returns false. 15425 */ 15426 P.lessThan = P.lt = function ( y, b ) { 15427 id = 8; 15428 return compare( this, new BigNumber( y, b ) ) < 0; 15429 }; 15430 15431 15432 /* 15433 * Return true if the value of this BigNumber is less than or equal to the value of 15434 * BigNumber(y, b), otherwise returns false. 15435 */ 15436 P.lessThanOrEqualTo = P.lte = function ( y, b ) { 15437 id = 9; 15438 return ( b = compare( this, new BigNumber( y, b ) ) ) === -1 || b === 0; 15439 }; 15440 15441 15442 /* 15443 * n - 0 = n 15444 * n - N = N 15445 * n - I = -I 15446 * 0 - n = -n 15447 * 0 - 0 = 0 15448 * 0 - N = N 15449 * 0 - I = -I 15450 * N - n = N 15451 * N - 0 = N 15452 * N - N = N 15453 * N - I = N 15454 * I - n = I 15455 * I - 0 = I 15456 * I - N = N 15457 * I - I = N 15458 * 15459 * Return a new BigNumber whose value is the value of this BigNumber minus the value of 15460 * BigNumber(y, b). 15461 */ 15462 P.minus = P.sub = function ( y, b ) { 15463 var i, j, t, xLTy, 15464 x = this, 15465 a = x.s; 15466 15467 id = 10; 15468 y = new BigNumber( y, b ); 15469 b = y.s; 15470 15471 // Either NaN? 15472 if ( !a || !b ) return new BigNumber(NaN); 15473 15474 // Signs differ? 15475 if ( a != b ) { 15476 y.s = -b; 15477 return x.plus(y); 15478 } 15479 15480 var xe = x.e / LOG_BASE, 15481 ye = y.e / LOG_BASE, 15482 xc = x.c, 15483 yc = y.c; 15484 15485 if ( !xe || !ye ) { 15486 15487 // Either Infinity? 15488 if ( !xc || !yc ) return xc ? ( y.s = -b, y ) : new BigNumber( yc ? x : NaN ); 15489 15490 // Either zero? 15491 if ( !xc[0] || !yc[0] ) { 15492 15493 // Return y if y is non-zero, x if x is non-zero, or zero if both are zero. 15494 return yc[0] ? ( y.s = -b, y ) : new BigNumber( xc[0] ? x : 15495 15496 // IEEE 754 (2008) 6.3: n - n = -0 when rounding to -Infinity 15497 ROUNDING_MODE == 3 ? -0 : 0 ); 15498 } 15499 } 15500 15501 xe = bitFloor(xe); 15502 ye = bitFloor(ye); 15503 xc = xc.slice(); 15504 15505 // Determine which is the bigger number. 15506 if ( a = xe - ye ) { 15507 15508 if ( xLTy = a < 0 ) { 15509 a = -a; 15510 t = xc; 15511 } else { 15512 ye = xe; 15513 t = yc; 15514 } 15515 15516 t.reverse(); 15517 15518 // Prepend zeros to equalise exponents. 15519 for ( b = a; b--; t.push(0) ); 15520 t.reverse(); 15521 } else { 15522 15523 // Exponents equal. Check digit by digit. 15524 j = ( xLTy = ( a = xc.length ) < ( b = yc.length ) ) ? a : b; 15525 15526 for ( a = b = 0; b < j; b++ ) { 15527 15528 if ( xc[b] != yc[b] ) { 15529 xLTy = xc[b] < yc[b]; 15530 break; 15531 } 15532 } 15533 } 15534 15535 // x < y? Point xc to the array of the bigger number. 15536 if (xLTy) t = xc, xc = yc, yc = t, y.s = -y.s; 15537 15538 b = ( j = yc.length ) - ( i = xc.length ); 15539 15540 // Append zeros to xc if shorter. 15541 // No need to add zeros to yc if shorter as subtract only needs to start at yc.length. 15542 if ( b > 0 ) for ( ; b--; xc[i++] = 0 ); 15543 b = BASE - 1; 15544 15545 // Subtract yc from xc. 15546 for ( ; j > a; ) { 15547 15548 if ( xc[--j] < yc[j] ) { 15549 for ( i = j; i && !xc[--i]; xc[i] = b ); 15550 --xc[i]; 15551 xc[j] += BASE; 15552 } 15553 15554 xc[j] -= yc[j]; 15555 } 15556 15557 // Remove leading zeros and adjust exponent accordingly. 15558 for ( ; xc[0] == 0; xc.shift(), --ye ); 15559 15560 // Zero? 15561 if ( !xc[0] ) { 15562 15563 // Following IEEE 754 (2008) 6.3, 15564 // n - n = +0 but n - n = -0 when rounding towards -Infinity. 15565 y.s = ROUNDING_MODE == 3 ? -1 : 1; 15566 y.c = [ y.e = 0 ]; 15567 return y; 15568 } 15569 15570 // No need to check for Infinity as +x - +y != Infinity && -x - -y != Infinity 15571 // for finite x and y. 15572 return normalise( y, xc, ye ); 15573 }; 15574 15575 15576 /* 15577 * n % 0 = N 15578 * n % N = N 15579 * n % I = n 15580 * 0 % n = 0 15581 * -0 % n = -0 15582 * 0 % 0 = N 15583 * 0 % N = N 15584 * 0 % I = 0 15585 * N % n = N 15586 * N % 0 = N 15587 * N % N = N 15588 * N % I = N 15589 * I % n = N 15590 * I % 0 = N 15591 * I % N = N 15592 * I % I = N 15593 * 15594 * Return a new BigNumber whose value is the value of this BigNumber modulo the value of 15595 * BigNumber(y, b). The result depends on the value of MODULO_MODE. 15596 */ 15597 P.modulo = P.mod = function ( y, b ) { 15598 var q, s, 15599 x = this; 15600 15601 id = 11; 15602 y = new BigNumber( y, b ); 15603 15604 // Return NaN if x is Infinity or NaN, or y is NaN or zero. 15605 if ( !x.c || !y.s || y.c && !y.c[0] ) { 15606 return new BigNumber(NaN); 15607 15608 // Return x if y is Infinity or x is zero. 15609 } else if ( !y.c || x.c && !x.c[0] ) { 15610 return new BigNumber(x); 15611 } 15612 15613 if ( MODULO_MODE == 9 ) { 15614 15615 // Euclidian division: q = sign(y) * floor(x / abs(y)) 15616 // r = x - qy where 0 <= r < abs(y) 15617 s = y.s; 15618 y.s = 1; 15619 q = div( x, y, 0, 3 ); 15620 y.s = s; 15621 q.s *= s; 15622 } else { 15623 q = div( x, y, 0, MODULO_MODE ); 15624 } 15625 15626 return x.minus( q.times(y) ); 15627 }; 15628 15629 15630 /* 15631 * Return a new BigNumber whose value is the value of this BigNumber negated, 15632 * i.e. multiplied by -1. 15633 */ 15634 P.negated = P.neg = function () { 15635 var x = new BigNumber(this); 15636 x.s = -x.s || null; 15637 return x; 15638 }; 15639 15640 15641 /* 15642 * n + 0 = n 15643 * n + N = N 15644 * n + I = I 15645 * 0 + n = n 15646 * 0 + 0 = 0 15647 * 0 + N = N 15648 * 0 + I = I 15649 * N + n = N 15650 * N + 0 = N 15651 * N + N = N 15652 * N + I = N 15653 * I + n = I 15654 * I + 0 = I 15655 * I + N = N 15656 * I + I = I 15657 * 15658 * Return a new BigNumber whose value is the value of this BigNumber plus the value of 15659 * BigNumber(y, b). 15660 */ 15661 P.plus = P.add = function ( y, b ) { 15662 var t, 15663 x = this, 15664 a = x.s; 15665 15666 id = 12; 15667 y = new BigNumber( y, b ); 15668 b = y.s; 15669 15670 // Either NaN? 15671 if ( !a || !b ) return new BigNumber(NaN); 15672 15673 // Signs differ? 15674 if ( a != b ) { 15675 y.s = -b; 15676 return x.minus(y); 15677 } 15678 15679 var xe = x.e / LOG_BASE, 15680 ye = y.e / LOG_BASE, 15681 xc = x.c, 15682 yc = y.c; 15683 15684 if ( !xe || !ye ) { 15685 15686 // Return ±Infinity if either ±Infinity. 15687 if ( !xc || !yc ) return new BigNumber( a / 0 ); 15688 15689 // Either zero? 15690 // Return y if y is non-zero, x if x is non-zero, or zero if both are zero. 15691 if ( !xc[0] || !yc[0] ) return yc[0] ? y : new BigNumber( xc[0] ? x : a * 0 ); 15692 } 15693 15694 xe = bitFloor(xe); 15695 ye = bitFloor(ye); 15696 xc = xc.slice(); 15697 15698 // Prepend zeros to equalise exponents. Faster to use reverse then do unshifts. 15699 if ( a = xe - ye ) { 15700 if ( a > 0 ) { 15701 ye = xe; 15702 t = yc; 15703 } else { 15704 a = -a; 15705 t = xc; 15706 } 15707 15708 t.reverse(); 15709 for ( ; a--; t.push(0) ); 15710 t.reverse(); 15711 } 15712 15713 a = xc.length; 15714 b = yc.length; 15715 15716 // Point xc to the longer array, and b to the shorter length. 15717 if ( a - b < 0 ) t = yc, yc = xc, xc = t, b = a; 15718 15719 // Only start adding at yc.length - 1 as the further digits of xc can be ignored. 15720 for ( a = 0; b; ) { 15721 a = ( xc[--b] = xc[b] + yc[b] + a ) / BASE | 0; 15722 xc[b] %= BASE; 15723 } 15724 15725 if (a) { 15726 xc.unshift(a); 15727 ++ye; 15728 } 15729 15730 // No need to check for zero, as +x + +y != 0 && -x + -y != 0 15731 // ye = MAX_EXP + 1 possible 15732 return normalise( y, xc, ye ); 15733 }; 15734 15735 15736 /* 15737 * Return the number of significant digits of the value of this BigNumber. 15738 * 15739 * [z] {boolean|number} Whether to count integer-part trailing zeros: true, false, 1 or 0. 15740 */ 15741 P.precision = P.sd = function (z) { 15742 var n, v, 15743 x = this, 15744 c = x.c; 15745 15746 // 'precision() argument not a boolean or binary digit: {z}' 15747 if ( z != null && z !== !!z && z !== 1 && z !== 0 ) { 15748 if (ERRORS) raise( 13, 'argument' + notBool, z ); 15749 if ( z != !!z ) z = null; 15750 } 15751 15752 if ( !c ) return null; 15753 v = c.length - 1; 15754 n = v * LOG_BASE + 1; 15755 15756 if ( v = c[v] ) { 15757 15758 // Subtract the number of trailing zeros of the last element. 15759 for ( ; v % 10 == 0; v /= 10, n-- ); 15760 15761 // Add the number of digits of the first element. 15762 for ( v = c[0]; v >= 10; v /= 10, n++ ); 15763 } 15764 15765 if ( z && x.e + 1 > n ) n = x.e + 1; 15766 15767 return n; 15768 }; 15769 15770 15771 /* 15772 * Return a new BigNumber whose value is the value of this BigNumber rounded to a maximum of 15773 * dp decimal places using rounding mode rm, or to 0 and ROUNDING_MODE respectively if 15774 * omitted. 15775 * 15776 * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. 15777 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. 15778 * 15779 * 'round() decimal places out of range: {dp}' 15780 * 'round() decimal places not an integer: {dp}' 15781 * 'round() rounding mode not an integer: {rm}' 15782 * 'round() rounding mode out of range: {rm}' 15783 */ 15784 P.round = function ( dp, rm ) { 15785 var n = new BigNumber(this); 15786 15787 if ( dp == null || isValidInt( dp, 0, MAX, 15 ) ) { 15788 round( n, ~~dp + this.e + 1, rm == null || 15789 !isValidInt( rm, 0, 8, 15, roundingMode ) ? ROUNDING_MODE : rm | 0 ); 15790 } 15791 15792 return n; 15793 }; 15794 15795 15796 /* 15797 * Return a new BigNumber whose value is the value of this BigNumber shifted by k places 15798 * (powers of 10). Shift to the right if n > 0, and to the left if n < 0. 15799 * 15800 * k {number} Integer, -MAX_SAFE_INTEGER to MAX_SAFE_INTEGER inclusive. 15801 * 15802 * If k is out of range and ERRORS is false, the result will be ±0 if k < 0, or ±Infinity 15803 * otherwise. 15804 * 15805 * 'shift() argument not an integer: {k}' 15806 * 'shift() argument out of range: {k}' 15807 */ 15808 P.shift = function (k) { 15809 var n = this; 15810 return isValidInt( k, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER, 16, 'argument' ) 15811 15812 // k < 1e+21, or truncate(k) will produce exponential notation. 15813 ? n.times( '1e' + truncate(k) ) 15814 : new BigNumber( n.c && n.c[0] && ( k < -MAX_SAFE_INTEGER || k > MAX_SAFE_INTEGER ) 15815 ? n.s * ( k < 0 ? 0 : 1 / 0 ) 15816 : n ); 15817 }; 15818 15819 15820 /* 15821 * sqrt(-n) = N 15822 * sqrt( N) = N 15823 * sqrt(-I) = N 15824 * sqrt( I) = I 15825 * sqrt( 0) = 0 15826 * sqrt(-0) = -0 15827 * 15828 * Return a new BigNumber whose value is the square root of the value of this BigNumber, 15829 * rounded according to DECIMAL_PLACES and ROUNDING_MODE. 15830 */ 15831 P.squareRoot = P.sqrt = function () { 15832 var m, n, r, rep, t, 15833 x = this, 15834 c = x.c, 15835 s = x.s, 15836 e = x.e, 15837 dp = DECIMAL_PLACES + 4, 15838 half = new BigNumber('0.5'); 15839 15840 // Negative/NaN/Infinity/zero? 15841 if ( s !== 1 || !c || !c[0] ) { 15842 return new BigNumber( !s || s < 0 && ( !c || c[0] ) ? NaN : c ? x : 1 / 0 ); 15843 } 15844 15845 // Initial estimate. 15846 s = Math.sqrt( +x ); 15847 15848 // Math.sqrt underflow/overflow? 15849 // Pass x to Math.sqrt as integer, then adjust the exponent of the result. 15850 if ( s == 0 || s == 1 / 0 ) { 15851 n = coeffToString(c); 15852 if ( ( n.length + e ) % 2 == 0 ) n += '0'; 15853 s = Math.sqrt(n); 15854 e = bitFloor( ( e + 1 ) / 2 ) - ( e < 0 || e % 2 ); 15855 15856 if ( s == 1 / 0 ) { 15857 n = '1e' + e; 15858 } else { 15859 n = s.toExponential(); 15860 n = n.slice( 0, n.indexOf('e') + 1 ) + e; 15861 } 15862 15863 r = new BigNumber(n); 15864 } else { 15865 r = new BigNumber( s + '' ); 15866 } 15867 15868 // Check for zero. 15869 // r could be zero if MIN_EXP is changed after the this value was created. 15870 // This would cause a division by zero (x/t) and hence Infinity below, which would cause 15871 // coeffToString to throw. 15872 if ( r.c[0] ) { 15873 e = r.e; 15874 s = e + dp; 15875 if ( s < 3 ) s = 0; 15876 15877 // Newton-Raphson iteration. 15878 for ( ; ; ) { 15879 t = r; 15880 r = half.times( t.plus( div( x, t, dp, 1 ) ) ); 15881 15882 if ( coeffToString( t.c ).slice( 0, s ) === ( n = 15883 coeffToString( r.c ) ).slice( 0, s ) ) { 15884 15885 // The exponent of r may here be one less than the final result exponent, 15886 // e.g 0.0009999 (e-4) --> 0.001 (e-3), so adjust s so the rounding digits 15887 // are indexed correctly. 15888 if ( r.e < e ) --s; 15889 n = n.slice( s - 3, s + 1 ); 15890 15891 // The 4th rounding digit may be in error by -1 so if the 4 rounding digits 15892 // are 9999 or 4999 (i.e. approaching a rounding boundary) continue the 15893 // iteration. 15894 if ( n == '9999' || !rep && n == '4999' ) { 15895 15896 // On the first iteration only, check to see if rounding up gives the 15897 // exact result as the nines may infinitely repeat. 15898 if ( !rep ) { 15899 round( t, t.e + DECIMAL_PLACES + 2, 0 ); 15900 15901 if ( t.times(t).eq(x) ) { 15902 r = t; 15903 break; 15904 } 15905 } 15906 15907 dp += 4; 15908 s += 4; 15909 rep = 1; 15910 } else { 15911 15912 // If rounding digits are null, 0{0,4} or 50{0,3}, check for exact 15913 // result. If not, then there are further digits and m will be truthy. 15914 if ( !+n || !+n.slice(1) && n.charAt(0) == '5' ) { 15915 15916 // Truncate to the first rounding digit. 15917 round( r, r.e + DECIMAL_PLACES + 2, 1 ); 15918 m = !r.times(r).eq(x); 15919 } 15920 15921 break; 15922 } 15923 } 15924 } 15925 } 15926 15927 return round( r, r.e + DECIMAL_PLACES + 1, ROUNDING_MODE, m ); 15928 }; 15929 15930 15931 /* 15932 * n * 0 = 0 15933 * n * N = N 15934 * n * I = I 15935 * 0 * n = 0 15936 * 0 * 0 = 0 15937 * 0 * N = N 15938 * 0 * I = N 15939 * N * n = N 15940 * N * 0 = N 15941 * N * N = N 15942 * N * I = N 15943 * I * n = I 15944 * I * 0 = N 15945 * I * N = N 15946 * I * I = I 15947 * 15948 * Return a new BigNumber whose value is the value of this BigNumber times the value of 15949 * BigNumber(y, b). 15950 */ 15951 P.times = P.mul = function ( y, b ) { 15952 var c, e, i, j, k, m, xcL, xlo, xhi, ycL, ylo, yhi, zc, 15953 base, sqrtBase, 15954 x = this, 15955 xc = x.c, 15956 yc = ( id = 17, y = new BigNumber( y, b ) ).c; 15957 15958 // Either NaN, ±Infinity or ±0? 15959 if ( !xc || !yc || !xc[0] || !yc[0] ) { 15960 15961 // Return NaN if either is NaN, or one is 0 and the other is Infinity. 15962 if ( !x.s || !y.s || xc && !xc[0] && !yc || yc && !yc[0] && !xc ) { 15963 y.c = y.e = y.s = null; 15964 } else { 15965 y.s *= x.s; 15966 15967 // Return ±Infinity if either is ±Infinity. 15968 if ( !xc || !yc ) { 15969 y.c = y.e = null; 15970 15971 // Return ±0 if either is ±0. 15972 } else { 15973 y.c = [0]; 15974 y.e = 0; 15975 } 15976 } 15977 15978 return y; 15979 } 15980 15981 e = bitFloor( x.e / LOG_BASE ) + bitFloor( y.e / LOG_BASE ); 15982 y.s *= x.s; 15983 xcL = xc.length; 15984 ycL = yc.length; 15985 15986 // Ensure xc points to longer array and xcL to its length. 15987 if ( xcL < ycL ) zc = xc, xc = yc, yc = zc, i = xcL, xcL = ycL, ycL = i; 15988 15989 // Initialise the result array with zeros. 15990 for ( i = xcL + ycL, zc = []; i--; zc.push(0) ); 15991 15992 base = BASE; 15993 sqrtBase = SQRT_BASE; 15994 15995 for ( i = ycL; --i >= 0; ) { 15996 c = 0; 15997 ylo = yc[i] % sqrtBase; 15998 yhi = yc[i] / sqrtBase | 0; 15999 16000 for ( k = xcL, j = i + k; j > i; ) { 16001 xlo = xc[--k] % sqrtBase; 16002 xhi = xc[k] / sqrtBase | 0; 16003 m = yhi * xlo + xhi * ylo; 16004 xlo = ylo * xlo + ( ( m % sqrtBase ) * sqrtBase ) + zc[j] + c; 16005 c = ( xlo / base | 0 ) + ( m / sqrtBase | 0 ) + yhi * xhi; 16006 zc[j--] = xlo % base; 16007 } 16008 16009 zc[j] = c; 16010 } 16011 16012 if (c) { 16013 ++e; 16014 } else { 16015 zc.shift(); 16016 } 16017 16018 return normalise( y, zc, e ); 16019 }; 16020 16021 16022 /* 16023 * Return a new BigNumber whose value is the value of this BigNumber rounded to a maximum of 16024 * sd significant digits using rounding mode rm, or ROUNDING_MODE if rm is omitted. 16025 * 16026 * [sd] {number} Significant digits. Integer, 1 to MAX inclusive. 16027 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. 16028 * 16029 * 'toDigits() precision out of range: {sd}' 16030 * 'toDigits() precision not an integer: {sd}' 16031 * 'toDigits() rounding mode not an integer: {rm}' 16032 * 'toDigits() rounding mode out of range: {rm}' 16033 */ 16034 P.toDigits = function ( sd, rm ) { 16035 var n = new BigNumber(this); 16036 sd = sd == null || !isValidInt( sd, 1, MAX, 18, 'precision' ) ? null : sd | 0; 16037 rm = rm == null || !isValidInt( rm, 0, 8, 18, roundingMode ) ? ROUNDING_MODE : rm | 0; 16038 return sd ? round( n, sd, rm ) : n; 16039 }; 16040 16041 16042 /* 16043 * Return a string representing the value of this BigNumber in exponential notation and 16044 * rounded using ROUNDING_MODE to dp fixed decimal places. 16045 * 16046 * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. 16047 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. 16048 * 16049 * 'toExponential() decimal places not an integer: {dp}' 16050 * 'toExponential() decimal places out of range: {dp}' 16051 * 'toExponential() rounding mode not an integer: {rm}' 16052 * 'toExponential() rounding mode out of range: {rm}' 16053 */ 16054 P.toExponential = function ( dp, rm ) { 16055 return format( this, 16056 dp != null && isValidInt( dp, 0, MAX, 19 ) ? ~~dp + 1 : null, rm, 19 ); 16057 }; 16058 16059 16060 /* 16061 * Return a string representing the value of this BigNumber in fixed-point notation rounding 16062 * to dp fixed decimal places using rounding mode rm, or ROUNDING_MODE if rm is omitted. 16063 * 16064 * Note: as with JavaScript's number type, (-0).toFixed(0) is '0', 16065 * but e.g. (-0.00001).toFixed(0) is '-0'. 16066 * 16067 * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. 16068 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. 16069 * 16070 * 'toFixed() decimal places not an integer: {dp}' 16071 * 'toFixed() decimal places out of range: {dp}' 16072 * 'toFixed() rounding mode not an integer: {rm}' 16073 * 'toFixed() rounding mode out of range: {rm}' 16074 */ 16075 P.toFixed = function ( dp, rm ) { 16076 return format( this, dp != null && isValidInt( dp, 0, MAX, 20 ) 16077 ? ~~dp + this.e + 1 : null, rm, 20 ); 16078 }; 16079 16080 16081 /* 16082 * Return a string representing the value of this BigNumber in fixed-point notation rounded 16083 * using rm or ROUNDING_MODE to dp decimal places, and formatted according to the properties 16084 * of the FORMAT object (see BigNumber.config). 16085 * 16086 * FORMAT = { 16087 * decimalSeparator : '.', 16088 * groupSeparator : ',', 16089 * groupSize : 3, 16090 * secondaryGroupSize : 0, 16091 * fractionGroupSeparator : '\xA0', // non-breaking space 16092 * fractionGroupSize : 0 16093 * }; 16094 * 16095 * [dp] {number} Decimal places. Integer, 0 to MAX inclusive. 16096 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. 16097 * 16098 * 'toFormat() decimal places not an integer: {dp}' 16099 * 'toFormat() decimal places out of range: {dp}' 16100 * 'toFormat() rounding mode not an integer: {rm}' 16101 * 'toFormat() rounding mode out of range: {rm}' 16102 */ 16103 P.toFormat = function ( dp, rm ) { 16104 var str = format( this, dp != null && isValidInt( dp, 0, MAX, 21 ) 16105 ? ~~dp + this.e + 1 : null, rm, 21 ); 16106 16107 if ( this.c ) { 16108 var i, 16109 arr = str.split('.'), 16110 g1 = +FORMAT.groupSize, 16111 g2 = +FORMAT.secondaryGroupSize, 16112 groupSeparator = FORMAT.groupSeparator, 16113 intPart = arr[0], 16114 fractionPart = arr[1], 16115 isNeg = this.s < 0, 16116 intDigits = isNeg ? intPart.slice(1) : intPart, 16117 len = intDigits.length; 16118 16119 if (g2) i = g1, g1 = g2, g2 = i, len -= i; 16120 16121 if ( g1 > 0 && len > 0 ) { 16122 i = len % g1 || g1; 16123 intPart = intDigits.substr( 0, i ); 16124 16125 for ( ; i < len; i += g1 ) { 16126 intPart += groupSeparator + intDigits.substr( i, g1 ); 16127 } 16128 16129 if ( g2 > 0 ) intPart += groupSeparator + intDigits.slice(i); 16130 if (isNeg) intPart = '-' + intPart; 16131 } 16132 16133 str = fractionPart 16134 ? intPart + FORMAT.decimalSeparator + ( ( g2 = +FORMAT.fractionGroupSize ) 16135 ? fractionPart.replace( new RegExp( '\\d{' + g2 + '}\\B', 'g' ), 16136 '$&' + FORMAT.fractionGroupSeparator ) 16137 : fractionPart ) 16138 : intPart; 16139 } 16140 16141 return str; 16142 }; 16143 16144 16145 /* 16146 * Return a string array representing the value of this BigNumber as a simple fraction with 16147 * an integer numerator and an integer denominator. The denominator will be a positive 16148 * non-zero value less than or equal to the specified maximum denominator. If a maximum 16149 * denominator is not specified, the denominator will be the lowest value necessary to 16150 * represent the number exactly. 16151 * 16152 * [md] {number|string|BigNumber} Integer >= 1 and < Infinity. The maximum denominator. 16153 * 16154 * 'toFraction() max denominator not an integer: {md}' 16155 * 'toFraction() max denominator out of range: {md}' 16156 */ 16157 P.toFraction = function (md) { 16158 var arr, d0, d2, e, exp, n, n0, q, s, 16159 k = ERRORS, 16160 x = this, 16161 xc = x.c, 16162 d = new BigNumber(ONE), 16163 n1 = d0 = new BigNumber(ONE), 16164 d1 = n0 = new BigNumber(ONE); 16165 16166 if ( md != null ) { 16167 ERRORS = false; 16168 n = new BigNumber(md); 16169 ERRORS = k; 16170 16171 if ( !( k = n.isInt() ) || n.lt(ONE) ) { 16172 16173 if (ERRORS) { 16174 raise( 22, 16175 'max denominator ' + ( k ? 'out of range' : 'not an integer' ), md ); 16176 } 16177 16178 // ERRORS is false: 16179 // If md is a finite non-integer >= 1, round it to an integer and use it. 16180 md = !k && n.c && round( n, n.e + 1, 1 ).gte(ONE) ? n : null; 16181 } 16182 } 16183 16184 if ( !xc ) return x.toString(); 16185 s = coeffToString(xc); 16186 16187 // Determine initial denominator. 16188 // d is a power of 10 and the minimum max denominator that specifies the value exactly. 16189 e = d.e = s.length - x.e - 1; 16190 d.c[0] = POWS_TEN[ ( exp = e % LOG_BASE ) < 0 ? LOG_BASE + exp : exp ]; 16191 md = !md || n.cmp(d) > 0 ? ( e > 0 ? d : n1 ) : n; 16192 16193 exp = MAX_EXP; 16194 MAX_EXP = 1 / 0; 16195 n = new BigNumber(s); 16196 16197 // n0 = d1 = 0 16198 n0.c[0] = 0; 16199 16200 for ( ; ; ) { 16201 q = div( n, d, 0, 1 ); 16202 d2 = d0.plus( q.times(d1) ); 16203 if ( d2.cmp(md) == 1 ) break; 16204 d0 = d1; 16205 d1 = d2; 16206 n1 = n0.plus( q.times( d2 = n1 ) ); 16207 n0 = d2; 16208 d = n.minus( q.times( d2 = d ) ); 16209 n = d2; 16210 } 16211 16212 d2 = div( md.minus(d0), d1, 0, 1 ); 16213 n0 = n0.plus( d2.times(n1) ); 16214 d0 = d0.plus( d2.times(d1) ); 16215 n0.s = n1.s = x.s; 16216 e *= 2; 16217 16218 // Determine which fraction is closer to x, n0/d0 or n1/d1 16219 arr = div( n1, d1, e, ROUNDING_MODE ).minus(x).abs().cmp( 16220 div( n0, d0, e, ROUNDING_MODE ).minus(x).abs() ) < 1 16221 ? [ n1.toString(), d1.toString() ] 16222 : [ n0.toString(), d0.toString() ]; 16223 16224 MAX_EXP = exp; 16225 return arr; 16226 }; 16227 16228 16229 /* 16230 * Return the value of this BigNumber converted to a number primitive. 16231 */ 16232 P.toNumber = function () { 16233 var x = this; 16234 16235 // Ensure zero has correct sign. 16236 return +x || ( x.s ? x.s * 0 : NaN ); 16237 }; 16238 16239 16240 /* 16241 * Return a BigNumber whose value is the value of this BigNumber raised to the power n. 16242 * If n is negative round according to DECIMAL_PLACES and ROUNDING_MODE. 16243 * If POW_PRECISION is not 0, round to POW_PRECISION using ROUNDING_MODE. 16244 * 16245 * n {number} Integer, -9007199254740992 to 9007199254740992 inclusive. 16246 * (Performs 54 loop iterations for n of 9007199254740992.) 16247 * 16248 * 'pow() exponent not an integer: {n}' 16249 * 'pow() exponent out of range: {n}' 16250 */ 16251 P.toPower = P.pow = function (n) { 16252 var k, y, 16253 i = mathfloor( n < 0 ? -n : +n ), 16254 x = this; 16255 16256 // Pass ±Infinity to Math.pow if exponent is out of range. 16257 if ( !isValidInt( n, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER, 23, 'exponent' ) && 16258 ( !isFinite(n) || i > MAX_SAFE_INTEGER && ( n /= 0 ) || 16259 parseFloat(n) != n && !( n = NaN ) ) ) { 16260 return new BigNumber( Math.pow( +x, n ) ); 16261 } 16262 16263 // Truncating each coefficient array to a length of k after each multiplication equates 16264 // to truncating significant digits to POW_PRECISION + [28, 41], i.e. there will be a 16265 // minimum of 28 guard digits retained. (Using + 1.5 would give [9, 21] guard digits.) 16266 k = POW_PRECISION ? mathceil( POW_PRECISION / LOG_BASE + 2 ) : 0; 16267 y = new BigNumber(ONE); 16268 16269 for ( ; ; ) { 16270 16271 if ( i % 2 ) { 16272 y = y.times(x); 16273 if ( !y.c ) break; 16274 if ( k && y.c.length > k ) y.c.length = k; 16275 } 16276 16277 i = mathfloor( i / 2 ); 16278 if ( !i ) break; 16279 16280 x = x.times(x); 16281 if ( k && x.c && x.c.length > k ) x.c.length = k; 16282 } 16283 16284 if ( n < 0 ) y = ONE.div(y); 16285 return k ? round( y, POW_PRECISION, ROUNDING_MODE ) : y; 16286 }; 16287 16288 16289 /* 16290 * Return a string representing the value of this BigNumber rounded to sd significant digits 16291 * using rounding mode rm or ROUNDING_MODE. If sd is less than the number of digits 16292 * necessary to represent the integer part of the value in fixed-point notation, then use 16293 * exponential notation. 16294 * 16295 * [sd] {number} Significant digits. Integer, 1 to MAX inclusive. 16296 * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive. 16297 * 16298 * 'toPrecision() precision not an integer: {sd}' 16299 * 'toPrecision() precision out of range: {sd}' 16300 * 'toPrecision() rounding mode not an integer: {rm}' 16301 * 'toPrecision() rounding mode out of range: {rm}' 16302 */ 16303 P.toPrecision = function ( sd, rm ) { 16304 return format( this, sd != null && isValidInt( sd, 1, MAX, 24, 'precision' ) 16305 ? sd | 0 : null, rm, 24 ); 16306 }; 16307 16308 16309 /* 16310 * Return a string representing the value of this BigNumber in base b, or base 10 if b is 16311 * omitted. If a base is specified, including base 10, round according to DECIMAL_PLACES and 16312 * ROUNDING_MODE. If a base is not specified, and this BigNumber has a positive exponent 16313 * that is equal to or greater than TO_EXP_POS, or a negative exponent equal to or less than 16314 * TO_EXP_NEG, return exponential notation. 16315 * 16316 * [b] {number} Integer, 2 to 64 inclusive. 16317 * 16318 * 'toString() base not an integer: {b}' 16319 * 'toString() base out of range: {b}' 16320 */ 16321 P.toString = function (b) { 16322 var str, 16323 n = this, 16324 s = n.s, 16325 e = n.e; 16326 16327 // Infinity or NaN? 16328 if ( e === null ) { 16329 16330 if (s) { 16331 str = 'Infinity'; 16332 if ( s < 0 ) str = '-' + str; 16333 } else { 16334 str = 'NaN'; 16335 } 16336 } else { 16337 str = coeffToString( n.c ); 16338 16339 if ( b == null || !isValidInt( b, 2, 64, 25, 'base' ) ) { 16340 str = e <= TO_EXP_NEG || e >= TO_EXP_POS 16341 ? toExponential( str, e ) 16342 : toFixedPoint( str, e ); 16343 } else { 16344 str = convertBase( toFixedPoint( str, e ), b | 0, 10, s ); 16345 } 16346 16347 if ( s < 0 && n.c[0] ) str = '-' + str; 16348 } 16349 16350 return str; 16351 }; 16352 16353 16354 /* 16355 * Return a new BigNumber whose value is the value of this BigNumber truncated to a whole 16356 * number. 16357 */ 16358 P.truncated = P.trunc = function () { 16359 return round( new BigNumber(this), this.e + 1, 1 ); 16360 }; 16361 16362 16363 16364 /* 16365 * Return as toString, but do not accept a base argument. 16366 */ 16367 P.valueOf = P.toJSON = function () { 16368 return this.toString(); 16369 }; 16370 16371 16372 // Aliases for BigDecimal methods. 16373 //P.add = P.plus; // P.add included above 16374 //P.subtract = P.minus; // P.sub included above 16375 //P.multiply = P.times; // P.mul included above 16376 //P.divide = P.div; 16377 //P.remainder = P.mod; 16378 //P.compareTo = P.cmp; 16379 //P.negate = P.neg; 16380 16381 16382 if ( configObj != null ) BigNumber.config(configObj); 16383 16384 return BigNumber; 16385 } 16386 16387 16388 // PRIVATE HELPER FUNCTIONS 16389 16390 16391 function bitFloor(n) { 16392 var i = n | 0; 16393 return n > 0 || n === i ? i : i - 1; 16394 } 16395 16396 16397 // Return a coefficient array as a string of base 10 digits. 16398 function coeffToString(a) { 16399 var s, z, 16400 i = 1, 16401 j = a.length, 16402 r = a[0] + ''; 16403 16404 for ( ; i < j; ) { 16405 s = a[i++] + ''; 16406 z = LOG_BASE - s.length; 16407 for ( ; z--; s = '0' + s ); 16408 r += s; 16409 } 16410 16411 // Determine trailing zeros. 16412 for ( j = r.length; r.charCodeAt(--j) === 48; ); 16413 return r.slice( 0, j + 1 || 1 ); 16414 } 16415 16416 16417 // Compare the value of BigNumbers x and y. 16418 function compare( x, y ) { 16419 var a, b, 16420 xc = x.c, 16421 yc = y.c, 16422 i = x.s, 16423 j = y.s, 16424 k = x.e, 16425 l = y.e; 16426 16427 // Either NaN? 16428 if ( !i || !j ) return null; 16429 16430 a = xc && !xc[0]; 16431 b = yc && !yc[0]; 16432 16433 // Either zero? 16434 if ( a || b ) return a ? b ? 0 : -j : i; 16435 16436 // Signs differ? 16437 if ( i != j ) return i; 16438 16439 a = i < 0; 16440 b = k == l; 16441 16442 // Either Infinity? 16443 if ( !xc || !yc ) return b ? 0 : !xc ^ a ? 1 : -1; 16444 16445 // Compare exponents. 16446 if ( !b ) return k > l ^ a ? 1 : -1; 16447 16448 j = ( k = xc.length ) < ( l = yc.length ) ? k : l; 16449 16450 // Compare digit by digit. 16451 for ( i = 0; i < j; i++ ) if ( xc[i] != yc[i] ) return xc[i] > yc[i] ^ a ? 1 : -1; 16452 16453 // Compare lengths. 16454 return k == l ? 0 : k > l ^ a ? 1 : -1; 16455 } 16456 16457 16458 /* 16459 * Return true if n is a valid number in range, otherwise false. 16460 * Use for argument validation when ERRORS is false. 16461 * Note: parseInt('1e+1') == 1 but parseFloat('1e+1') == 10. 16462 */ 16463 function intValidatorNoErrors( n, min, max ) { 16464 return ( n = truncate(n) ) >= min && n <= max; 16465 } 16466 16467 16468 function isArray(obj) { 16469 return Object.prototype.toString.call(obj) == '[object Array]'; 16470 } 16471 16472 16473 /* 16474 * Convert string of baseIn to an array of numbers of baseOut. 16475 * Eg. convertBase('255', 10, 16) returns [15, 15]. 16476 * Eg. convertBase('ff', 16, 10) returns [2, 5, 5]. 16477 */ 16478 function toBaseOut( str, baseIn, baseOut ) { 16479 var j, 16480 arr = [0], 16481 arrL, 16482 i = 0, 16483 len = str.length; 16484 16485 for ( ; i < len; ) { 16486 for ( arrL = arr.length; arrL--; arr[arrL] *= baseIn ); 16487 arr[ j = 0 ] += ALPHABET.indexOf( str.charAt( i++ ) ); 16488 16489 for ( ; j < arr.length; j++ ) { 16490 16491 if ( arr[j] > baseOut - 1 ) { 16492 if ( arr[j + 1] == null ) arr[j + 1] = 0; 16493 arr[j + 1] += arr[j] / baseOut | 0; 16494 arr[j] %= baseOut; 16495 } 16496 } 16497 } 16498 16499 return arr.reverse(); 16500 } 16501 16502 16503 function toExponential( str, e ) { 16504 return ( str.length > 1 ? str.charAt(0) + '.' + str.slice(1) : str ) + 16505 ( e < 0 ? 'e' : 'e+' ) + e; 16506 } 16507 16508 16509 function toFixedPoint( str, e ) { 16510 var len, z; 16511 16512 // Negative exponent? 16513 if ( e < 0 ) { 16514 16515 // Prepend zeros. 16516 for ( z = '0.'; ++e; z += '0' ); 16517 str = z + str; 16518 16519 // Positive exponent 16520 } else { 16521 len = str.length; 16522 16523 // Append zeros. 16524 if ( ++e > len ) { 16525 for ( z = '0', e -= len; --e; z += '0' ); 16526 str += z; 16527 } else if ( e < len ) { 16528 str = str.slice( 0, e ) + '.' + str.slice(e); 16529 } 16530 } 16531 16532 return str; 16533 } 16534 16535 16536 function truncate(n) { 16537 n = parseFloat(n); 16538 return n < 0 ? mathceil(n) : mathfloor(n); 16539 } 16540 16541 16542 // EXPORT 16543 16544 16545 BigNumber = another(); 16546 16547 // AMD. 16548 if ( typeof define == 'function' && define.amd ) { 16549 define( function () { return BigNumber; } ); 16550 16551 // Node and other environments that support module.exports. 16552 } else if ( typeof module != 'undefined' && module.exports ) { 16553 module.exports = BigNumber; 16554 if ( !crypto ) try { crypto = require('crypto'); } catch (e) {} 16555 16556 // Browser. 16557 } else { 16558 global.BigNumber = BigNumber; 16559 } 16560 })(this); 16561 16562 },{"crypto":50}],"vnt":[function(require,module,exports){ 16563 var Vnt = require('./lib/vnt'); 16564 16565 // dont override global variable 16566 if (typeof window !== 'undefined' && typeof window.Vnt === 'undefined') { 16567 window.Vnt = Vnt; 16568 } 16569 16570 module.exports = Vnt; 16571 16572 },{"./lib/vnt":23}]},{},["vnt"]) 16573 //# sourceMappingURL=vnt.js.map