solidity.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.solidityPackedSha256 = exports.solidityPackedKeccak256 = exports.solidityPacked = void 0;
  4. const index_js_1 = require("../address/index.js");
  5. const index_js_2 = require("../crypto/index.js");
  6. const index_js_3 = require("../utils/index.js");
  7. const regexBytes = new RegExp("^bytes([0-9]+)$");
  8. const regexNumber = new RegExp("^(u?int)([0-9]*)$");
  9. const regexArray = new RegExp("^(.*)\\[([0-9]*)\\]$");
  10. function _pack(type, value, isArray) {
  11. switch (type) {
  12. case "address":
  13. if (isArray) {
  14. return (0, index_js_3.getBytes)((0, index_js_3.zeroPadValue)(value, 32));
  15. }
  16. return (0, index_js_3.getBytes)((0, index_js_1.getAddress)(value));
  17. case "string":
  18. return (0, index_js_3.toUtf8Bytes)(value);
  19. case "bytes":
  20. return (0, index_js_3.getBytes)(value);
  21. case "bool":
  22. value = (!!value ? "0x01" : "0x00");
  23. if (isArray) {
  24. return (0, index_js_3.getBytes)((0, index_js_3.zeroPadValue)(value, 32));
  25. }
  26. return (0, index_js_3.getBytes)(value);
  27. }
  28. let match = type.match(regexNumber);
  29. if (match) {
  30. let signed = (match[1] === "int");
  31. let size = parseInt(match[2] || "256");
  32. (0, index_js_3.assertArgument)((!match[2] || match[2] === String(size)) && (size % 8 === 0) && size !== 0 && size <= 256, "invalid number type", "type", type);
  33. if (isArray) {
  34. size = 256;
  35. }
  36. if (signed) {
  37. value = (0, index_js_3.toTwos)(value, size);
  38. }
  39. return (0, index_js_3.getBytes)((0, index_js_3.zeroPadValue)((0, index_js_3.toBeArray)(value), size / 8));
  40. }
  41. match = type.match(regexBytes);
  42. if (match) {
  43. const size = parseInt(match[1]);
  44. (0, index_js_3.assertArgument)(String(size) === match[1] && size !== 0 && size <= 32, "invalid bytes type", "type", type);
  45. (0, index_js_3.assertArgument)((0, index_js_3.dataLength)(value) === size, `invalid value for ${type}`, "value", value);
  46. if (isArray) {
  47. return (0, index_js_3.getBytes)((0, index_js_3.zeroPadBytes)(value, 32));
  48. }
  49. return value;
  50. }
  51. match = type.match(regexArray);
  52. if (match && Array.isArray(value)) {
  53. const baseType = match[1];
  54. const count = parseInt(match[2] || String(value.length));
  55. (0, index_js_3.assertArgument)(count === value.length, `invalid array length for ${type}`, "value", value);
  56. const result = [];
  57. value.forEach(function (value) {
  58. result.push(_pack(baseType, value, true));
  59. });
  60. return (0, index_js_3.getBytes)((0, index_js_3.concat)(result));
  61. }
  62. (0, index_js_3.assertArgument)(false, "invalid type", "type", type);
  63. }
  64. // @TODO: Array Enum
  65. /**
  66. * Computes the [[link-solc-packed]] representation of %%values%%
  67. * respectively to their %%types%%.
  68. *
  69. * @example:
  70. * addr = "0x8ba1f109551bd432803012645ac136ddd64dba72"
  71. * solidityPacked([ "address", "uint" ], [ addr, 45 ]);
  72. * //_result:
  73. */
  74. function solidityPacked(types, values) {
  75. (0, index_js_3.assertArgument)(types.length === values.length, "wrong number of values; expected ${ types.length }", "values", values);
  76. const tight = [];
  77. types.forEach(function (type, index) {
  78. tight.push(_pack(type, values[index]));
  79. });
  80. return (0, index_js_3.hexlify)((0, index_js_3.concat)(tight));
  81. }
  82. exports.solidityPacked = solidityPacked;
  83. /**
  84. * Computes the [[link-solc-packed]] [[keccak256]] hash of %%values%%
  85. * respectively to their %%types%%.
  86. *
  87. * @example:
  88. * addr = "0x8ba1f109551bd432803012645ac136ddd64dba72"
  89. * solidityPackedKeccak256([ "address", "uint" ], [ addr, 45 ]);
  90. * //_result:
  91. */
  92. function solidityPackedKeccak256(types, values) {
  93. return (0, index_js_2.keccak256)(solidityPacked(types, values));
  94. }
  95. exports.solidityPackedKeccak256 = solidityPackedKeccak256;
  96. /**
  97. * Computes the [[link-solc-packed]] [[sha256]] hash of %%values%%
  98. * respectively to their %%types%%.
  99. *
  100. * @example:
  101. * addr = "0x8ba1f109551bd432803012645ac136ddd64dba72"
  102. * solidityPackedSha256([ "address", "uint" ], [ addr, 45 ]);
  103. * //_result:
  104. */
  105. function solidityPackedSha256(types, values) {
  106. return (0, index_js_2.sha256)(solidityPacked(types, values));
  107. }
  108. exports.solidityPackedSha256 = solidityPackedSha256;
  109. //# sourceMappingURL=solidity.js.map