solidity.js 3.8 KB

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