Skip to main content

Try it Live

Run ENS examples in the interactive playground

Branded Ens Type

Definition

Ens is a plain JavaScript string at runtime. The brand exists only in the type system, so TypeScript rejects an arbitrary string where an Ens is required. The source type is EnsType in src/primitives/Ens/EnsType.ts; the module exports it as Ens.

What the brand guarantees

The brand records how a value was produced, not that it is valid. Each producer gives a different guarantee:
An Ens value from from() or is() can contain uppercase letters, invisible characters, or confusables. Only normalize() output is safe to hash or compare.

Constructors and conversion

from(name)

Brands a string without validation or normalization. It returns the same string.
Use from() for names you already normalized, for example names read from your own database, or to call the internal _normalize, _beautify, _namehash, and _labelhash functions.

normalize(name)

The constructor to use for untrusted input. Throws DisallowedCharacterError on invalid names. See Normalization.

toString(name)

Returns the same string typed as string, dropping the brand.
Because Ens extends string, you can also pass it anywhere a string is accepted without calling toString().

is(value)

Narrows unknown to Ens when value is a non-empty string. See Validation.

Public wrappers and internal functions

Each operation has a public wrapper that accepts string | Ens and an internal function that takes Ens only: The wrappers call from(String(name)) and then the internal function. The results are identical.

Factory functions

Namehash and Labelhash build hash functions from an injected keccak256 implementation:
The returned functions take Ens, not string. See Encoding.

See Also