namehash.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.dnsEncode = exports.namehash = exports.isValidName = exports.ensNormalize = void 0;
  4. const index_js_1 = require("../crypto/index.js");
  5. const index_js_2 = require("../utils/index.js");
  6. const ens_normalize_1 = require("@adraffy/ens-normalize");
  7. const Zeros = new Uint8Array(32);
  8. Zeros.fill(0);
  9. function checkComponent(comp) {
  10. (0, index_js_2.assertArgument)(comp.length !== 0, "invalid ENS name; empty component", "comp", comp);
  11. return comp;
  12. }
  13. function ensNameSplit(name) {
  14. const bytes = (0, index_js_2.toUtf8Bytes)(ensNormalize(name));
  15. const comps = [];
  16. if (name.length === 0) {
  17. return comps;
  18. }
  19. let last = 0;
  20. for (let i = 0; i < bytes.length; i++) {
  21. const d = bytes[i];
  22. // A separator (i.e. "."); copy this component
  23. if (d === 0x2e) {
  24. comps.push(checkComponent(bytes.slice(last, i)));
  25. last = i + 1;
  26. }
  27. }
  28. // There was a stray separator at the end of the name
  29. (0, index_js_2.assertArgument)(last < bytes.length, "invalid ENS name; empty component", "name", name);
  30. comps.push(checkComponent(bytes.slice(last)));
  31. return comps;
  32. }
  33. /**
  34. * Returns the ENS %%name%% normalized.
  35. */
  36. function ensNormalize(name) {
  37. try {
  38. if (name.length === 0) {
  39. throw new Error("empty label");
  40. }
  41. return (0, ens_normalize_1.ens_normalize)(name);
  42. }
  43. catch (error) {
  44. (0, index_js_2.assertArgument)(false, `invalid ENS name (${error.message})`, "name", name);
  45. }
  46. }
  47. exports.ensNormalize = ensNormalize;
  48. /**
  49. * Returns ``true`` if %%name%% is a valid ENS name.
  50. */
  51. function isValidName(name) {
  52. try {
  53. return (ensNameSplit(name).length !== 0);
  54. }
  55. catch (error) { }
  56. return false;
  57. }
  58. exports.isValidName = isValidName;
  59. /**
  60. * Returns the [[link-namehash]] for %%name%%.
  61. */
  62. function namehash(name) {
  63. (0, index_js_2.assertArgument)(typeof (name) === "string", "invalid ENS name; not a string", "name", name);
  64. (0, index_js_2.assertArgument)(name.length, `invalid ENS name (empty label)`, "name", name);
  65. let result = Zeros;
  66. const comps = ensNameSplit(name);
  67. while (comps.length) {
  68. result = (0, index_js_1.keccak256)((0, index_js_2.concat)([result, (0, index_js_1.keccak256)((comps.pop()))]));
  69. }
  70. return (0, index_js_2.hexlify)(result);
  71. }
  72. exports.namehash = namehash;
  73. /**
  74. * Returns the DNS encoded %%name%%.
  75. *
  76. * This is used for various parts of ENS name resolution, such
  77. * as the wildcard resolution.
  78. */
  79. function dnsEncode(name, _maxLength) {
  80. const length = (_maxLength != null) ? _maxLength : 63;
  81. (0, index_js_2.assertArgument)(length <= 255, "DNS encoded label cannot exceed 255", "length", length);
  82. return (0, index_js_2.hexlify)((0, index_js_2.concat)(ensNameSplit(name).map((comp) => {
  83. (0, index_js_2.assertArgument)(comp.length <= length, `label ${JSON.stringify(name)} exceeds ${length} bytes`, "name", name);
  84. const bytes = new Uint8Array(comp.length + 1);
  85. bytes.set(comp, 1);
  86. bytes[0] = bytes.length - 1;
  87. return bytes;
  88. }))) + "00";
  89. }
  90. exports.dnsEncode = dnsEncode;
  91. //# sourceMappingURL=namehash.js.map