solidity.ts 3.9 KB

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