viem-goviem-go

encodedLabelToLabelhash

Converts an encoded label to a labelhash

encodedLabelToLabelhash

Converts an encoded label (e.g., [abc123...]) to a labelhash. Returns empty string if the label is not a valid encoded labelhash.

Import

import "github.com/ChefBingbong/viem-go/utils/ens"

Usage

import "github.com/ChefBingbong/viem-go/utils/ens"
// Convert encoded label to labelhash
hash := ens.EncodedLabelToLabelhash("[4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0]")
// "0x4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0"
// Not an encoded label
hash = ens.EncodedLabelToLabelhash("eth")
// ""

Returns

  • Type: string

The labelhash as a hex string with 0x prefix, or empty string if the input is not a valid encoded label.

Parameters

label (required)

  • Type: string

The encoded label to convert. Must be in the format [<64 hex chars>].

hash := ens.EncodedLabelToLabelhash("[4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0]")
// "0x4f5b812789fc606be1b3b16908db13fc7a9adf7ca72641f84d75b47069d3d7f0"
hash = ens.EncodedLabelToLabelhash("eth")
// ""

Validation

The function validates:

  1. Length must be exactly 66 characters ([ + 64 hex chars + ])
  2. Must start with [
  3. Must end with ]
  4. Middle 64 characters must be valid hex

Notes

  • Returns empty string for invalid encoded labels
  • Used internally by Namehash and Labelhash to handle encoded labels
  • Inverse operation of EncodeLabelhash
  • Encoded labels are used to represent labelhashes in ENS names