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

# ERC-5267 Domain Retrieval

> Standard method for retrieving EIP-712 domain

Standardized contract method for exposing EIP-712 domain parameters.

<Tip title="ERC Reference">
  Implements [ERC-5267](https://eips.ethereum.org/EIPS/eip-5267) for retrieving domain separator fields.
</Tip>

## Overview

ERC-5267 defines `eip712Domain()` function that contracts implement to expose their EIP-712 domain parameters. This enables:

* Wallets to verify domain before signing
* Tools to discover domain without hardcoding
* Standardized domain querying across contracts

## Function Signature

```solidity theme={null}
function eip712Domain() external view returns (
  bytes1 fields,
  string name,
  string version,
  uint256 chainId,
  address verifyingContract,
  bytes32 salt,
  uint256[] extensions
)
```

## Fields Bitmap

The `fields` byte indicates which domain fields are defined:

| Bit | Hex  | Field             | Description              |
| --- | ---- | ----------------- | ------------------------ |
| 0   | 0x01 | name              | Domain name present      |
| 1   | 0x02 | version           | Version present          |
| 2   | 0x04 | chainId           | Chain ID present         |
| 3   | 0x08 | verifyingContract | Contract address present |
| 4   | 0x10 | salt              | Salt present             |

**Example:**

* `0x0f` = name + version + chainId + verifyingContract
* `0x1f` = all fields present
* `0x0d` = name + chainId + verifyingContract (no version)

## API

### toErc5267Response

**`toErc5267Response(domain: DomainType): ERC5267Response`**

Converts Domain to ERC-5267 response format with field bitmap.

**Example:**

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

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

const response = Domain.toErc5267Response(domain);

console.log(response);
// {
//   fields: Uint8Array([0x0f]),  // 0x0f = name + version + chainId + verifyingContract
//   name: "MyDApp",
//   version: "1",
//   chainId: 1n,
//   verifyingContract: AddressType,
//   salt: Uint8Array(32),  // zeros
//   extensions: []
// }
```

### getFieldsBitmap

**`getFieldsBitmap(domain: DomainType): Uint8Array`**

Calculates fields bitmap from domain.

**Example:**

```typescript theme={null}
import { getFieldsBitmap } from '@tevm/voltaire/Domain';

const domain = {
  name: "MyDApp",
  chainId: 1,
  verifyingContract: Address.from("0x1234...")
  // No version
};

const bitmap = getFieldsBitmap(domain);
console.log(bitmap[0].toString(16)); // "0d" (name + chainId + verifyingContract)
```

## Usage Patterns

### Query Contract Domain

```typescript theme={null}
import { toErc5267Response } from '@tevm/voltaire/Domain';

async function getContractDomain(contractAddress: string) {
  const contract = getContract(contractAddress);

  try {
    // Call eip712Domain() if contract implements ERC-5267
    const response = await contract.eip712Domain();

    console.log('Fields bitmap:', '0x' + response.fields.toString(16));
    console.log('Name:', response.name);
    console.log('Version:', response.version);
    console.log('Chain ID:', response.chainId);
    console.log('Verifying Contract:', response.verifyingContract);

    return response;
  } catch (err) {
    console.error('Contract does not implement ERC-5267');
    throw err;
  }
}
```

### Verify Domain Match

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

async function verifyDomainMatch(
  contractAddress: string,
  expectedDomain: DomainType
) {
  const contract = getContract(contractAddress);
  const onChainResponse = await contract.eip712Domain();

  const expectedResponse = toErc5267Response(expectedDomain);

  // Compare fields
  if (onChainResponse.fields !== expectedResponse.fields[0]) {
    throw new Error('Field bitmap mismatch');
  }

  if (onChainResponse.name !== expectedResponse.name) {
    throw new Error('Name mismatch');
  }

  if (onChainResponse.version !== expectedResponse.version) {
    throw new Error('Version mismatch');
  }

  if (onChainResponse.chainId !== expectedResponse.chainId) {
    throw new Error('Chain ID mismatch');
  }

  console.log('Domain verified!');
}
```

### Wallet Domain Display

```typescript theme={null}
import { toErc5267Response } from '@tevm/voltaire/Domain';

async function displaySigningDomain(contractAddress: string) {
  const contract = getContract(contractAddress);
  const domain = await contract.eip712Domain();

  // Show user what they're signing for
  return {
    'Contract': contractAddress,
    'Name': domain.name || 'Unknown',
    'Version': domain.version || 'Unknown',
    'Network': getNetworkName(domain.chainId),
    'Domain Hash': calculateDomainSeparator(domain)
  };
}
```

## Implementation Helper

Solidity contract implementing ERC-5267:

```solidity theme={null}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract ERC5267Example {
    string private constant NAME = "MyDApp";
    string private constant VERSION = "1";

    function eip712Domain() external view returns (
        bytes1 fields,
        string memory name,
        string memory version,
        uint256 chainId,
        address verifyingContract,
        bytes32 salt,
        uint256[] memory extensions
    ) {
        return (
            0x0f,  // name + version + chainId + verifyingContract
            NAME,
            VERSION,
            block.chainid,
            address(this),
            bytes32(0),
            new uint256[](0)
        );
    }
}
```

## Specification

**Defined in:** [src/primitives/Domain/toErc5267Response.js](https://github.com/evmts/voltaire/blob/main/src/primitives/Domain/toErc5267Response.js)

**See also:**

* [ERC-5267 Specification](https://eips.ethereum.org/EIPS/eip-5267)
* [EIP-712 Domain Separator](/primitives/domain)
* [Typed Data](/primitives/typed-data)
