index.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. import { BigNumber } from "@ethersproject/bignumber";
  3. import { arrayify, concat, hexlify, zeroPad } from "@ethersproject/bytes";
  4. import { keccak256 as hashKeccak256 } from "@ethersproject/keccak256";
  5. import { sha256 as hashSha256 } from "@ethersproject/sha2";
  6. import { toUtf8Bytes } from "@ethersproject/strings";
  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. const Zeros = "0000000000000000000000000000000000000000000000000000000000000000";
  11. import { Logger } from "@ethersproject/logger";
  12. import { version } from "./_version";
  13. const logger = new Logger(version);
  14. function _pack(type: string, value: any, isArray?: boolean): Uint8Array {
  15. switch(type) {
  16. case "address":
  17. if (isArray) { return zeroPad(value, 32); }
  18. return arrayify(value);
  19. case "string":
  20. return toUtf8Bytes(value);
  21. case "bytes":
  22. return arrayify(value);
  23. case "bool":
  24. value = (value ? "0x01": "0x00");
  25. if (isArray) { return zeroPad(value, 32); }
  26. return arrayify(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. if ((match[2] && String(size) !== match[2]) || (size % 8 !== 0) || size === 0 || size > 256) {
  33. logger.throwArgumentError("invalid number type", "type", type)
  34. }
  35. if (isArray) { size = 256; }
  36. value = BigNumber.from(value).toTwos(size);
  37. return zeroPad(value, size / 8);
  38. }
  39. match = type.match(regexBytes);
  40. if (match) {
  41. const size = parseInt(match[1]);
  42. if (String(size) !== match[1] || size === 0 || size > 32) {
  43. logger.throwArgumentError("invalid bytes type", "type", type)
  44. }
  45. if (arrayify(value).byteLength !== size) {
  46. logger.throwArgumentError(`invalid value for ${ type }`, "value", value)
  47. }
  48. if (isArray) { return arrayify((value + Zeros).substring(0, 66)); }
  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. if (count != value.length) {
  56. logger.throwArgumentError(`invalid array length for ${ type }`, "value", value)
  57. }
  58. const result: Array<Uint8Array> = [];
  59. value.forEach(function(value) {
  60. result.push(_pack(baseType, value, true));
  61. });
  62. return concat(result);
  63. }
  64. return logger.throwArgumentError("invalid type", "type", type)
  65. }
  66. // @TODO: Array Enum
  67. export function pack(types: ReadonlyArray<string>, values: ReadonlyArray<any>) {
  68. if (types.length != values.length) {
  69. logger.throwArgumentError("wrong number of values; expected ${ types.length }", "values", values)
  70. }
  71. const tight: Array<Uint8Array> = [];
  72. types.forEach(function(type, index) {
  73. tight.push(_pack(type, values[index]));
  74. });
  75. return hexlify(concat(tight));
  76. }
  77. export function keccak256(types: ReadonlyArray<string>, values: ReadonlyArray<any>) {
  78. return hashKeccak256(pack(types, values));
  79. }
  80. export function sha256(types: ReadonlyArray<string>, values: ReadonlyArray<any>) {
  81. return hashSha256(pack(types, values));
  82. }