namehash.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.dnsEncode = exports.namehash = exports.isValidName = void 0;
  4. var bytes_1 = require("@ethersproject/bytes");
  5. var strings_1 = require("@ethersproject/strings");
  6. var keccak256_1 = require("@ethersproject/keccak256");
  7. var logger_1 = require("@ethersproject/logger");
  8. var _version_1 = require("./_version");
  9. var logger = new logger_1.Logger(_version_1.version);
  10. var Zeros = new Uint8Array(32);
  11. Zeros.fill(0);
  12. var Partition = new RegExp("^((.*)\\.)?([^.]+)$");
  13. function isValidName(name) {
  14. try {
  15. var comps = name.split(".");
  16. for (var i = 0; i < comps.length; i++) {
  17. if ((0, strings_1.nameprep)(comps[i]).length === 0) {
  18. throw new Error("empty");
  19. }
  20. }
  21. return true;
  22. }
  23. catch (error) { }
  24. return false;
  25. }
  26. exports.isValidName = isValidName;
  27. function namehash(name) {
  28. /* istanbul ignore if */
  29. if (typeof (name) !== "string") {
  30. logger.throwArgumentError("invalid ENS name; not a string", "name", name);
  31. }
  32. var current = name;
  33. var result = Zeros;
  34. while (current.length) {
  35. var partition = current.match(Partition);
  36. if (partition == null || partition[2] === "") {
  37. logger.throwArgumentError("invalid ENS address; missing component", "name", name);
  38. }
  39. var label = (0, strings_1.toUtf8Bytes)((0, strings_1.nameprep)(partition[3]));
  40. result = (0, keccak256_1.keccak256)((0, bytes_1.concat)([result, (0, keccak256_1.keccak256)(label)]));
  41. current = partition[2] || "";
  42. }
  43. return (0, bytes_1.hexlify)(result);
  44. }
  45. exports.namehash = namehash;
  46. function dnsEncode(name) {
  47. return (0, bytes_1.hexlify)((0, bytes_1.concat)(name.split(".").map(function (comp) {
  48. // We jam in an _ prefix to fill in with the length later
  49. // Note: Nameprep throws if the component is over 63 bytes
  50. var bytes = (0, strings_1.toUtf8Bytes)("_" + (0, strings_1.nameprep)(comp));
  51. bytes[0] = bytes.length - 1;
  52. return bytes;
  53. }))) + "00";
  54. }
  55. exports.dnsEncode = dnsEncode;
  56. //# sourceMappingURL=namehash.js.map