-
Notifications
You must be signed in to change notification settings - Fork 15
feat: add labelhash parsing and normalization utilities #1688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
djstrong
merged 7 commits into
main
from
211-normalize-labelhash-for-ensrainbow-heal-request-2
Feb 27, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c3ec3d0
feat: add label hash parsing and normalization utilities
djstrong 1dc8e2b
test: update labelByLabelHash tests for improved validation and norma…
djstrong b60c049
test: enhance labelByLabelHash and parse-labelhash tests for error ha…
djstrong 3565932
Merge branch 'main' into 211-normalize-labelhash-for-ensrainbow-heal-…
djstrong c9f0176
Merge branch 'main' into 211-normalize-labelhash-for-ensrainbow-heal-…
djstrong 9a92b99
add changeset
djstrong 95621e8
Merge branch 'main' into 211-normalize-labelhash-for-ensrainbow-heal-…
lightwalker-eth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| "@ensnode/ensnode-sdk": patch | ||
| "@ensnode/ensrainbow-sdk": patch | ||
| --- | ||
|
|
||
| `EnsRainbowApiClient.heal()` now accepts labelhashes in any common format — with or without a `0x` prefix, uppercase hex characters, bracket-enclosed encoded labelhashes, or odd-length hex strings — and normalizes them automatically. Invalid inputs return a `HealBadRequestError` rather than throwing. | ||
|
|
||
| The underlying normalization utilities (`parseLabelHash`, `parseEncodedLabelHash`, `parseLabelHashOrEncodedLabelHash`) are also exported from `@ensnode/ensnode-sdk` for use in other contexts. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| parseEncodedLabelHash, | ||
| parseLabelHash, | ||
| parseLabelHashOrEncodedLabelHash, | ||
| } from "./parse-labelhash"; | ||
|
|
||
| describe("parseLabelHash", () => { | ||
| it("normalizes a valid 64-char labelHash with 0x prefix", () => { | ||
| expect( | ||
| parseLabelHash("0x0000000000000000000000000000000000000000000000000000000000000000"), | ||
| ).toBe("0x0000000000000000000000000000000000000000000000000000000000000000"); | ||
| }); | ||
|
|
||
| it("adds 0x prefix when missing (64 hex chars)", () => { | ||
| expect(parseLabelHash("0000000000000000000000000000000000000000000000000000000000000000")).toBe( | ||
| "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
| ); | ||
| }); | ||
|
|
||
| it("pads a 63-char hex string to 64 chars (0x prefix present)", () => { | ||
| expect( | ||
| parseLabelHash("0x000000000000000000000000000000000000000000000000000000000000000"), | ||
| ).toBe("0x0000000000000000000000000000000000000000000000000000000000000000"); | ||
| }); | ||
|
|
||
| it("pads a 63-char hex string to 64 chars (no 0x prefix)", () => { | ||
| expect(parseLabelHash("000000000000000000000000000000000000000000000000000000000000000")).toBe( | ||
| "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
| ); | ||
| }); | ||
|
|
||
| it("normalizes uppercase hex to lowercase (64 chars)", () => { | ||
| expect(parseLabelHash("A000000000000000000000000000000000000000000000000000000000000000")).toBe( | ||
| "0xa000000000000000000000000000000000000000000000000000000000000000", | ||
| ); | ||
| }); | ||
|
|
||
| it("pads and normalizes uppercase hex (63 chars)", () => { | ||
| expect(parseLabelHash("A00000000000000000000000000000000000000000000000000000000000000")).toBe( | ||
| "0x0a00000000000000000000000000000000000000000000000000000000000000", | ||
| ); | ||
| }); | ||
|
|
||
| it("normalizes a known labelhash (vitalik, uppercase input)", () => { | ||
| expect( | ||
| parseLabelHash("0xAf2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc"), | ||
| ).toBe("0xaf2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc"); | ||
| }); | ||
|
|
||
| it("normalizes a known labelhash (vitalik, no 0x prefix)", () => { | ||
| expect(parseLabelHash("af2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc")).toBe( | ||
| "0xaf2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc", | ||
| ); | ||
| }); | ||
|
|
||
| it("throws for non-hex characters", () => { | ||
| expect(() => | ||
| parseLabelHash("0xG000000000000000000000000000000000000000000000000000000000000000"), | ||
| ).toThrow(Error); | ||
| }); | ||
|
|
||
| it("throws for too short input (e.g. 5 hex chars)", () => { | ||
| expect(() => parseLabelHash("0x00000")).toThrow(Error); | ||
| }); | ||
|
|
||
| it("throws for too long input (65 hex chars)", () => { | ||
| expect(() => | ||
| parseLabelHash("0x00000000000000000000000000000000000000000000000000000000000000000"), | ||
| ).toThrow(Error); | ||
| }); | ||
|
|
||
| it("throws for 62-char hex (even, but wrong length)", () => { | ||
| expect(() => | ||
| parseLabelHash("0x00000000000000000000000000000000000000000000000000000000000000"), | ||
| ).toThrow(Error); | ||
| }); | ||
|
|
||
| it("throws for uppercase 0X prefix", () => { | ||
| expect(() => | ||
| parseLabelHash("0X0000000000000000000000000000000000000000000000000000000000000000"), | ||
| ).toThrow(Error); | ||
| }); | ||
|
|
||
| it("throws for empty string", () => { | ||
| expect(() => parseLabelHash("")).toThrow(Error); | ||
| }); | ||
| }); | ||
djstrong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| describe("parseEncodedLabelHash", () => { | ||
| it("normalizes a valid encoded labelHash with 64 hex chars", () => { | ||
| expect( | ||
| parseEncodedLabelHash("[0000000000000000000000000000000000000000000000000000000000000000]"), | ||
| ).toBe("0x0000000000000000000000000000000000000000000000000000000000000000"); | ||
| }); | ||
|
|
||
| it("normalizes an encoded labelHash with 0x prefix inside brackets", () => { | ||
| expect( | ||
| parseEncodedLabelHash("[0x0000000000000000000000000000000000000000000000000000000000000000]"), | ||
| ).toBe("0x0000000000000000000000000000000000000000000000000000000000000000"); | ||
| }); | ||
|
|
||
| it("pads a 63-char encoded labelHash (no 0x prefix inside)", () => { | ||
| expect( | ||
| parseEncodedLabelHash("[000000000000000000000000000000000000000000000000000000000000000]"), | ||
| ).toBe("0x0000000000000000000000000000000000000000000000000000000000000000"); | ||
| }); | ||
|
|
||
| it("pads a 63-char encoded labelHash (0x prefix inside)", () => { | ||
| expect( | ||
| parseEncodedLabelHash("[0x000000000000000000000000000000000000000000000000000000000000000]"), | ||
| ).toBe("0x0000000000000000000000000000000000000000000000000000000000000000"); | ||
| }); | ||
|
|
||
| it("normalizes uppercase encoded labelHash (64 chars)", () => { | ||
| expect( | ||
| parseEncodedLabelHash("[A000000000000000000000000000000000000000000000000000000000000000]"), | ||
| ).toBe("0xa000000000000000000000000000000000000000000000000000000000000000"); | ||
| }); | ||
|
|
||
| it("pads and normalizes uppercase encoded labelHash (63 chars)", () => { | ||
| expect( | ||
| parseEncodedLabelHash("[A00000000000000000000000000000000000000000000000000000000000000]"), | ||
| ).toBe("0x0a00000000000000000000000000000000000000000000000000000000000000"); | ||
| }); | ||
|
|
||
| it("normalizes a known encoded labelhash (vitalik)", () => { | ||
| expect( | ||
| parseEncodedLabelHash("[af2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc]"), | ||
| ).toBe("0xaf2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc"); | ||
| }); | ||
|
|
||
| it("throws when missing opening bracket", () => { | ||
| expect(() => | ||
| parseEncodedLabelHash("0000000000000000000000000000000000000000000000000000000000000000]"), | ||
| ).toThrow(Error); | ||
| }); | ||
|
|
||
| it("throws when missing closing bracket", () => { | ||
| expect(() => | ||
| parseEncodedLabelHash("[0000000000000000000000000000000000000000000000000000000000000000"), | ||
| ).toThrow(Error); | ||
| }); | ||
|
|
||
| it("throws when missing both brackets", () => { | ||
| expect(() => | ||
| parseEncodedLabelHash("0000000000000000000000000000000000000000000000000000000000000000"), | ||
| ).toThrow(Error); | ||
| }); | ||
|
|
||
| it("throws for 62 hex chars inside brackets (too short, even length)", () => { | ||
| expect(() => | ||
| parseEncodedLabelHash("[00000000000000000000000000000000000000000000000000000000000000]"), | ||
| ).toThrow(Error); | ||
| }); | ||
|
|
||
| it("throws for 65 hex chars inside brackets (too long)", () => { | ||
| expect(() => | ||
| parseEncodedLabelHash("[00000000000000000000000000000000000000000000000000000000000000000]"), | ||
| ).toThrow(Error); | ||
| }); | ||
|
|
||
| it("throws for uppercase 0X prefix inside brackets", () => { | ||
| expect(() => | ||
| parseEncodedLabelHash("[0X0000000000000000000000000000000000000000000000000000000000000000]"), | ||
| ).toThrow(Error); | ||
| }); | ||
|
|
||
| it("throws for invalid content inside brackets", () => { | ||
| expect(() => parseEncodedLabelHash("[00000]")).toThrow(Error); | ||
| expect(() => | ||
| parseEncodedLabelHash("[0xG000000000000000000000000000000000000000000000000000000000000000]"), | ||
| ).toThrow(Error); | ||
| }); | ||
|
|
||
| it("throws for empty string", () => { | ||
| expect(() => parseEncodedLabelHash("")).toThrow(Error); | ||
| }); | ||
|
|
||
| it("throws for empty brackets", () => { | ||
| expect(() => parseEncodedLabelHash("[]")).toThrow(Error); | ||
| }); | ||
| }); | ||
|
|
||
| describe("parseLabelHashOrEncodedLabelHash", () => { | ||
| it("parses a plain labelHash", () => { | ||
| expect( | ||
| parseLabelHashOrEncodedLabelHash( | ||
| "0xaf2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc", | ||
| ), | ||
| ).toBe("0xaf2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc"); | ||
| }); | ||
|
|
||
| it("parses an encoded labelHash", () => { | ||
| expect( | ||
| parseLabelHashOrEncodedLabelHash( | ||
| "[af2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc]", | ||
| ), | ||
| ).toBe("0xaf2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc"); | ||
| }); | ||
|
|
||
| it("normalizes a plain labelHash missing 0x prefix", () => { | ||
| expect( | ||
| parseLabelHashOrEncodedLabelHash( | ||
| "af2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc", | ||
| ), | ||
| ).toBe("0xaf2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc"); | ||
| }); | ||
|
|
||
| it("normalizes a plain labelHash with uppercase chars", () => { | ||
| expect( | ||
| parseLabelHashOrEncodedLabelHash( | ||
| "0xAf2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc", | ||
| ), | ||
| ).toBe("0xaf2caa1c2ca1d027f1ac823b529d0a67cd144264b2789fa2ea4d63a67c7103cc"); | ||
| }); | ||
|
|
||
| it("throws for invalid input", () => { | ||
| expect(() => parseLabelHashOrEncodedLabelHash("0xinvalid")).toThrow(Error); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.