namehash.ts 2.8 KB

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