Skip to content

Normalize labelhash for ENSRainbow Heal request #347

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

djstrong
Copy link
Contributor

@djstrong djstrong commented Mar 6, 2025

@djstrong djstrong added the ensrainbow ENSRainbow related label Mar 6, 2025
@djstrong djstrong self-assigned this Mar 6, 2025
@djstrong djstrong requested a review from a team as a code owner March 6, 2025 17:18
@djstrong djstrong added this to ENSNode Mar 6, 2025
@djstrong djstrong linked an issue Mar 6, 2025 that may be closed by this pull request
Copy link

vercel bot commented Mar 6, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
admin.ensnode.io ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 5, 2025 4:18pm
ensadmin-next ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 5, 2025 4:18pm
ensnode.io ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 5, 2025 4:18pm
ensrainbow.io ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 5, 2025 4:18pm

).rejects.toThrow("Invalid labelhash length 65 characters (expected 66)");
labelByHash("0x0ca5d0b4ef1129e04bfe7d35ac9def2f4f91daeb202cbe6e613f1dd17b2da0"),
).rejects.toThrow(
'Error healing labelhash: "0x0ca5d0b4ef1129e04bfe7d35ac9def2f4f91daeb202cbe6e613f1dd17b2da0". Error (400): Invalid labelhash length: expected 32 bytes (64 hex chars), got 31 bytes: 0x0ca5d0b4ef1129e04bfe7d35ac9def2f4f91daeb202cbe6e613f1dd17b2da0.',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we make these errors non redundant?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't understand the question. Please share more details.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

E.g. labelhash is written twice in this error. I think it should be just:
Error (400): Invalid labelhash length: expected 32 bytes (64 hex chars), got 31 bytes: 0x0ca5d0b4ef1129e04bfe7d35ac9def2f4f91daeb202cbe6e613f1dd17b2da0.

@djstrong djstrong moved this from In Progress to Ready for Review in ENSNode Mar 10, 2025
Copy link
Member

@lightwalker-eth lightwalker-eth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@djstrong Thanks for updates 👍 Reviewed and shared feedback


export namespace EnsRainbow {
export type ApiClientOptions = EnsRainbowApiClientOptions;

export interface ApiClient {
count(): Promise<CountResponse>;

heal(labelhash: Labelhash): Promise<HealResponse>;
heal(labelhash: Labelhash | string): Promise<HealResponse>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please define a type for EncodedLabelhash

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have created it for parseEncodedLabelhash but string needs to stay here because we accept unnormalized labelhashes.

).rejects.toThrow("Invalid labelhash length 65 characters (expected 66)");
labelByHash("0x0ca5d0b4ef1129e04bfe7d35ac9def2f4f91daeb202cbe6e613f1dd17b2da0"),
).rejects.toThrow(
'Error healing labelhash: "0x0ca5d0b4ef1129e04bfe7d35ac9def2f4f91daeb202cbe6e613f1dd17b2da0". Error (400): Invalid labelhash length: expected 32 bytes (64 hex chars), got 31 bytes: 0x0ca5d0b4ef1129e04bfe7d35ac9def2f4f91daeb202cbe6e613f1dd17b2da0.',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't understand the question. Please share more details.

@shrugs
Copy link
Collaborator

shrugs commented Apr 2, 2025

next steps: merge main & re-review

* @returns A normalized labelhash (a 0x-prefixed, lowercased, 64-character hex string)
* @throws {InvalidLabelhashError} If the input cannot be normalized to a valid labelhash
*/
export function parseLabelhash(maybeLabelhash: string): Labelhash {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo we should refactor this entire file to just:

function parseLabelhashOrEncodedLabelhash(maybeLabelHash: string) {
  // 1. ensure is Hex formatted
  let hexLabelhash: Hex;
  try {
    // attempt to decode label hash with ensjs
    // NOTE: decodeLabelhash will throw InvalidEncodedLabelError or return hex-looking string
    hexLabelhash = decodeLabelhash(maybeLabelHash);
  } catch {
    // if not encoded label hash, prefix with 0x
    hexLabelhash = maybeLabelHash.startsWith("0x")
      ? (maybeLabelHash as `0x${string}`)
      : `0x${maybeLabelHash}`;
  }

  // 2. if not hex, throw
  if (!isHex(hexLabelhash, { strict: true })) throw new InvalidLabelhashError();

  // 3. ensure hex part is correctly sized, padding left
  const normalizedHex = pad(hexLabelhash, { dir: "left", size: 32 });
  if (size(normalizedHex) !== 32) throw new InvalidLabelhashError();

  // 4. return lower case string
  return normalizedHex.toLowerCase() as Labelhash;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ensjs isn't already a dependency, we can keep parseEncodedLabelHash in place of ensjs#decodeLabelHash except make sure it returns 0x${maybeEncodedLabelhash.slice(1, -1)} not the raw hex characters

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and the logic can be separated into multiple functions if desired. but we shouldn't be re-implementing hex-related logic that's already available in viem

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have ensjs.
pad(hexLabelhash, { dir: "left", size: 32 }) is not working correctly, e.g. it validates positively 0x00000.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the code to use isHex

@djstrong djstrong requested a review from Copilot April 4, 2025 20:23
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

packages/ensrainbow-sdk/src/utils.ts:28

  • Consider adding an explicit check for an empty hex string (after stripping the '0x' prefix) to provide a clearer error message when the input is empty.
if (!/^[0-9a-fA-F]*$/.test(hexPart)) {

apps/ensindexer/src/lib/graphnode-helpers.ts:40

  • Ensure that the error message format is consistent with other modules to aid debugging and align with test expectations.
throw new Error(`Error (${healResponse.errorCode}): ${healResponse.error}.`);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ensrainbow ENSRainbow related
Projects
Status: Ready for Review
Development

Successfully merging this pull request may close these issues.

Normalize labelhash for ENSRainbow Heal request
3 participants