EIPs Explained

EIP-712 · Final · Interface · 2017-09-12

EIP-712: Typed structured data hashing and signing

A standard way to hash and sign typed, structured data, so wallets can show users readable fields instead of a hex blob and contracts can verify the signature cheaply.

What problem does it solve?

Off-chain signatures save gas. You sign a message, and someone else submits it to a contract later. Gasless approvals, order books and meta-transactions all depend on this. But with plain eth_sign, a user only sees an opaque hex string and has no idea what they are authorizing. Each app also hashed its data in its own ad-hoc way, which is easy to get wrong.

EIP-712 defines one peer-reviewed encoding for structured data. Wallets can display the fields ("Permit: spender X, value 100, deadline …"), and signatures from one app or chain cannot be reused in another.

How it works

The final digest to sign is:

keccak256("\x19\x01" ‖ domainSeparator ‖ hashStruct(message))

The 0x19 0x01 prefix follows ERC-191 and can never be mistaken for an RLP transaction.

  • hashStruct(s) = keccak256(typeHash ‖ encodeData(s))
  • typeHash is keccak256 of the type string, for example Mail(address from,address to,string contents). Referenced struct types are appended, sorted by name.
  • encodeData encodes each member as 32 bytes. Atomic types are ABI-style. string and bytes are hashed. Arrays are hashed. Nested structs are encoded with their own hashStruct.
  • domainSeparator is the hashStruct of an EIP712Domain with any of these fields: name, version, chainId (EIP-155), verifyingContract, salt. It ties the signature to one app, version, chain and contract.

The RPC method is eth_signTypedData, which takes { types, primaryType, domain, message }.

Sample code

Signing and verifying with ethers v6:

import { Wallet, verifyTypedData } from "ethers";

const wallet = Wallet.createRandom();

const domain = {
  name: "Ether Mail",
  version: "1",
  chainId: 1,
  verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
};
const types = {
  Person: [{ name: "name", type: "string" }, { name: "wallet", type: "address" }],
  Mail: [
    { name: "from", type: "Person" },
    { name: "to", type: "Person" },
    { name: "contents", type: "string" },
  ],
};
const message = {
  from: { name: "Cow", wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" },
  to: { name: "Bob", wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" },
  contents: "Hello, Bob!",
};

const sig = await wallet.signTypedData(domain, types, message);
console.log(verifyTypedData(domain, types, message, sig) === wallet.address); // true

On-chain, Solidity computes the same digest:

bytes32 constant MAIL_TYPEHASH = keccak256("Mail(Person from,Person to,string contents)Person(string name,address wallet)");
bytes32 digest = keccak256(abi.encodePacked("\x19\x01", DOMAIN_SEPARATOR, structHash));
address signer = ecrecover(digest, v, r, s);

Things to know

  • No replay protection is built in. Include a nonce and a deadline in your struct, and mark them used on-chain.
  • Wallets commonly expose this as eth_signTypedData_v4. Earlier "v1/v3" variants differ in how they handle arrays and nested structs.
  • Well-known users include ERC-2612 permit and many DEX order formats. Smart-contract wallets verify these signatures with ERC-1271. ERC-5267 lets a contract publish its domain.
  • Typed-data signatures are a favorite phishing vector, because one signed Permit can drain a token. Always read what you sign.
  • Use an audited helper (OpenZeppelin EIP712) rather than hand-rolling the encoding. This EIP is Final.

#signatures #wallet #offchain #permit #interface

Details

Authors
Remco Bloemen (@Recmo), Leonid Logvinov (@LogvinovLeon), Jacob Evans (@dekz)
Status
Final
Type
Standards Track / Interface
Created
2017-09-12
Requires
Required by

Machine-readable: Markdown · JSON