> ## Documentation Index
> Fetch the complete documentation index at: https://voltaire.tevm.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Branded Ens Type

> The Ens branded string type, its constructors, and what the brand does and does not guarantee

> [card] [Try it Live](https://playground.tevm.sh?example=primitives/ens.ts)
>
> Run ENS examples in the interactive playground

# Branded Ens Type

## Definition

```typescript theme={null}
import type { brand } from '@tevm/voltaire';

type Ens = string & { readonly [brand]: "Ens" };
```

`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`.

```typescript theme={null}
import * as Ens from '@tevm/voltaire/Ens';

const name: Ens.Ens = Ens.normalize("Vitalik.eth");
typeof name; // "string"
```

## What the brand guarantees

The brand records how a value was produced, not that it is valid. Each producer gives a different guarantee:

| Producer | Validates | Normalizes | Guarantee |
| - | - | - | - |
| `normalize(name)` | Yes | Yes | Canonical ENSIP-15 name (or `""` for `""`) |
| `beautify(name)` | Yes | Display form | Valid, but not canonical. Do not hash. |
| `from(name)` | No | No | None. A typed cast. |
| `is(value)` narrowing | No | No | Non-empty string |

> [!WARNING]
>
> 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)

```typescript theme={null}
from(name: string): Ens
```

Brands a string without validation or normalization. It returns the same string.

```typescript theme={null}
import * as Ens from '@tevm/voltaire/Ens';

const branded = Ens.from("vitalik.eth"); // "vitalik.eth"
Ens.from("ABC");                         // "ABC" (unchanged)
```

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)

```typescript theme={null}
normalize(name: string | Ens): Ens
```

The constructor to use for untrusted input. Throws `DisallowedCharacterError` on invalid names. See [Normalization](https://voltaire.tevm.sh/primitives/ens/normalization.md).

### toString(name)

```typescript theme={null}
toString(name: Ens): string
```

Returns the same string typed as `string`, dropping the brand.

```typescript theme={null}
import * as Ens from '@tevm/voltaire/Ens';

const branded = Ens.from("vitalik.eth");
const plain: string = Ens.toString(branded); // "vitalik.eth"
```

Because `Ens` extends `string`, you can also pass it anywhere a `string` is accepted without calling `toString()`.

### is(value)

```typescript theme={null}
is(value: unknown): value is Ens
```

Narrows `unknown` to `Ens` when `value` is a non-empty string. See [Validation](https://voltaire.tevm.sh/primitives/ens/validation.md#isvalue).

## Public wrappers and internal functions

Each operation has a public wrapper that accepts `string | Ens` and an internal function that takes `Ens` only:

| Public | Internal | Returns |
| - | - | - |
| `normalize(name: string \| Ens)` | `_normalize(name: Ens)` | `Ens` |
| `beautify(name: string \| Ens)` | `_beautify(name: Ens)` | `Ens` |
| `namehash(name: string \| Ens)` | `_namehash(name: Ens)` | `Uint8Array` (32 bytes) |
| `labelhash(label: string \| Ens)` | `_labelhash(label: Ens)` | `Uint8Array` (32 bytes) |

The wrappers call `from(String(name))` and then the internal function. The results are identical.

```typescript theme={null}
import * as Ens from '@tevm/voltaire/Ens';

const a = Ens.namehash("vitalik.eth");
const b = Ens._namehash(Ens.from("vitalik.eth"));
// a and b contain the same 32 bytes
```

## Factory functions

`Namehash` and `Labelhash` build hash functions from an injected keccak256 implementation:

```typescript theme={null}
Namehash(deps: { keccak256: (data: Uint8Array) => Uint8Array }): (name: Ens) => Uint8Array
Labelhash(deps: { keccak256: (data: Uint8Array) => Uint8Array }): (label: Ens) => Uint8Array
```

```typescript theme={null}
import { Namehash, from } from '@tevm/voltaire/Ens';
import { hash as keccak256 } from '@tevm/voltaire/Keccak256';

const namehash = Namehash({ keccak256 });
namehash(from("vitalik.eth")); // Uint8Array(32)
```

The returned functions take `Ens`, not `string`. See [Encoding](https://voltaire.tevm.sh/primitives/ens/encoding.md).

## See Also

* [ENS Overview](https://voltaire.tevm.sh/primitives/ens.md)
* [Branded Types](https://voltaire.tevm.sh/getting-started/branded-types.md) - Voltaire's branding pattern
* [Normalization](https://voltaire.tevm.sh/primitives/ens/normalization.md)
* [Validation](https://voltaire.tevm.sh/primitives/ens/validation.md)
* [Encoding](https://voltaire.tevm.sh/primitives/ens/encoding.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.