Skip to main content

DomainSeparator

EIP-712 domain separator - a 32-byte keccak256 hash used for domain-specific signature verification.

Overview

DomainSeparator is a branded Hash type representing the keccak256 hash of an EIP-712 domain separator. It ensures signatures are valid only within a specific domain context, preventing replay attacks across different contracts or chains.

Type Definition

type DomainSeparatorType = Uint8Array & {
  readonly [brand]: "DomainSeparator";
  readonly length: 32;
};

Usage

Create from Hex

import * as DomainSeparator from './primitives/DomainSeparator/index.js';

const sep = DomainSeparator.fromHex(
  '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef'
);

Create from Bytes

const bytes = new Uint8Array(32);
const sep = DomainSeparator.fromBytes(bytes);

Convert to Hex

const hex = DomainSeparator.toHex(sep);
console.log(hex); // '0x1234...'

Compare Separators

const sep1 = DomainSeparator.fromHex('0x1234...');
const sep2 = DomainSeparator.fromHex('0x5678...');
const equal = DomainSeparator.equals(sep1, sep2); // false

Computing Domain Separators

Use Domain.toHash() to compute domain separators:
import * as Domain from './primitives/Domain/index.js';
import { keccak256 } from './crypto/keccak256/index.js';

const domain = Domain.from({
  name: 'MyDApp',
  version: '1',
  chainId: 1,
  verifyingContract: '0x123...',
});

const domainSep = Domain.toHash(domain, { keccak256 });

Error Handling

DomainSeparator operations throw typed errors for precise error handling:

InvalidDomainSeparatorLengthError

Thrown when the input bytes length is not exactly 32 bytes.
import * as DomainSeparator from './primitives/DomainSeparator/index.js';
import { InvalidDomainSeparatorLengthError } from './primitives/DomainSeparator/errors.js';

try {
  DomainSeparator.fromBytes(new Uint8Array(16));
} catch (e) {
  if (e instanceof InvalidDomainSeparatorLengthError) {
    console.log(e.name);     // "InvalidDomainSeparatorLengthError"
    console.log(e.code);     // "INVALID_DOMAIN_SEPARATOR_LENGTH"
    console.log(e.value);    // The invalid bytes
    console.log(e.expected); // "32 bytes"
  }
}

Specification

See Also

  • Domain - Domain separator structure
  • TypedData - Complete EIP-712 typed data
  • Hash - Hash primitive