EIPs Explained

ERC-5267 · Final · ERC · 2022-07-14

ERC-5267: Retrieval of EIP-712 domain

Adds a standard eip712Domain() view so wallets and apps can read exactly which EIP-712 domain a contract uses, letting them build correct typed-data signatures (like permits) for any contract without custom code.

What problem does it solve?

EIP-712 typed-data signatures include a domain (name, version, chain ID, verifying contract, salt) so a signature is only valid for one contract on one chain. But EIP-712 never said how a contract announces which domain fields it uses or their values.

Contracts with ERC-2612 permit expose DOMAIN_SEPARATOR(), but that is just a hash: an app cannot turn it back into the domain object that eth_signTypedData_v4 needs, and a wallet cannot show the user what they are signing for. So every integration hardcoded domains per token. ERC-5267 fixes this with one standard getter.

How it works

Compliant contracts implement exactly:

function eip712Domain() external view returns (
    bytes1 fields,
    string name,
    string version,
    uint256 chainId,
    address verifyingContract,
    bytes32 salt,
    uint256[] extensions
);
  • fields is a bitmap saying which domain fields are present. Bit 0 = name, 1 = version, 2 = chainId, 3 = verifyingContract, 4 = salt. For example 0x0f means name, version, chainId and verifyingContract.
  • All values are always returned (so the ABI decodes), but only those flagged in fields are meaningful.
  • extensions lists EIP numbers that add extra domain fields; clients that do not understand one should refuse to proceed.
  • The domain may change over time (e.g. chainId after a fork, per EIP-155); contracts may emit EIP712DomainChanged() to signal this.

Sample code

Building a domain and signing a permit with ethers v6:

import { Contract, Wallet, JsonRpcProvider, TypedDataDomain } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const wallet = new Wallet(process.env.PRIVATE_KEY!, provider);
const token = new Contract(process.env.TOKEN!, [
  "function eip712Domain() view returns (bytes1,string,string,uint256,address,bytes32,uint256[])",
  "function nonces(address) view returns (uint256)",
], provider);

async function getDomain(c: Contract): Promise<TypedDataDomain> {
  const [fields, name, version, chainId, verifyingContract, salt, ext] = await c.eip712Domain();
  if (ext.length) throw new Error("unsupported EIP-712 domain extensions");
  const f = Number(fields);
  const d: TypedDataDomain = {};
  if (f & 1) d.name = name;
  if (f & 2) d.version = version;
  if (f & 4) d.chainId = chainId;
  if (f & 8) d.verifyingContract = verifyingContract;
  if (f & 16) d.salt = salt;
  return d;
}

const domain = await getDomain(token);
const types = { Permit: [
  { name: "owner", type: "address" }, { name: "spender", type: "address" },
  { name: "value", type: "uint256" }, { name: "nonce", type: "uint256" },
  { name: "deadline", type: "uint256" },
]};
const sig = await wallet.signTypedData(domain, types, {
  owner: wallet.address, spender: process.env.SPENDER!, value: 1000n,
  nonce: await token.nonces(wallet.address), deadline: 2n ** 32n,
});

In Solidity, OpenZeppelin's EIP712 base contract implements eip712Domain() for you.

Things to know

  • Status is Final.
  • Before asking a user to sign, wallets and apps should check that the returned chainId and verifyingContract match the current chain and contract; the spec allows them to differ, which could be abused.
  • Older immutable contracts cannot add this function. The spec suggests falling back to hardcoded domains, or guessing common domains and checking which one hashes to the contract's DOMAIN_SEPARATOR().
  • fields is a bytes1; convert it to a number before applying bit masks, as above.

#signatures #eip-712 #permit #wallets

Details

Authors
Francisco Giordano (@frangio)
Status
Final
Type
Standards Track / ERC
Created
2022-07-14
Requires
Required by

Machine-readable: Markdown · JSON