MenuUse / EIP-712

EIP-712

fe_codec_eip712 hashes values as EIP-712 typed structured data, and writes the eth_signTypedData_v4 request a wallet signs. Hashes cannot be decoded, so it has its own API rather than being a Format.

Static hashing

Mark the structs you sign #[eip712], and run the generator a second time in its EIP-712 mode:

#[serializable]
pub struct Person { pub name: Text<16>, pub wallet: Address }

#[serializable]
#[eip712]
pub struct Mail { pub from: Person, pub to: Person, pub contents: Text<32> }
fe_codec_gen --source src/models.fe --eip712 --out src/eip712.fe

The generator computes each struct’s encodeType string and emits its type hash as a core::keccak constant, which the compiler evaluates. Each struct’s hash_struct is then one keccak_words over its members, as hand-written code would be. Structs and enums they reference are included automatically. For Mail, encodeType is Mail(Person from,Person to,string contents)Person(string name,address wallet).

use fe_codec_eip712::{Domain, Eip712Struct}

// In a contract: name and version by compile-time hash.
let domain = Domain::new()
    .name_hash(core::keccak("Ether Mail"))
    .version_hash(core::keccak("1"))
    .chain_id(1)
    .verifying_contract(verifier)
let digest = domain.digest(mail.hash_struct())
let signer = std::evm::crypto::ecrecover(hash: digest, v, r, s)

// Off-chain: name and version as text, and the request for the wallet.
let domain = Domain::new().name(Text<64>::from_array("Ether Mail")?).version(Text<64>::from_array("1")?).chain_id(1)
let request: BoundedBuffer<1024> = fe_codec_eip712::typed_data_json(domain, mail)?

The generic path

The functions hash_struct(value), signing_hash(domain, value), type_hash<T>() and encode_type<T, N>() work for any Serialize + Describe type, including handwritten ones, without the EIP-712 generator. They give the same results, at a higher cost on the EVM.

Gas, measured on the EVM for the EIP’s example mail, signature digest included:

Path Gas
Generated, domain by hash 13,900
Hand-written, constant type hashes 14,600
Generic 70,900

Types

Fe type EIP-712 type
bool bool
u8–u256, i8–i256 uint8–uint256, int8–int256
Text<N> string
BoundedBuffer<N> bytes
PackedBytes<N>, N from 1 to 32 bytesN
Address address
[T; N] T[N]
List<T, N> T[], whatever N is
Unit enums, whose variants have no payload uint8, holding the variant ID
Records The struct’s name
  • Field order. Members follow the field order, which is declaration order unless the type assigns #[codec_id]s.
  • Rejected types. EIP-712 has no optional values, so Option fields are rejected. So are a variant ID above 255 and a struct named EIP712Domain. The generator reports these at generation time. The generic path reports them as Unsupported at runtime, naming the field, along with a root that isn’t a struct and two different structs with one name.
  • What the generator needs. It must see every struct an #[eip712] struct uses, so pass their modules with --source. It rejects generic structs, whose type depends on their arguments; use the generic path for those.
  • Codec fields. The generator takes a struct’s type string from its field types, so a #[codec_with] field in an #[eip712] struct, or in a struct one reaches, is rejected. The generic path asks the codec: the type is whatever its describe states, and Unsupported where the codec keeps the default.
  • Lists. A List<T, N> is a dynamic array T[]: its hash is the keccak256 of the words of the elements it holds, so N appears in no type string. A list of lists, or of arrays, nests as T[][], T[2][], and an array of lists is T[][2]. Its elements must be Copy and have an EIP-712 type themselves, so a list of optional values or of tuples is rejected as those are. The wallet request writes a JSON array.
  • Maps. EIP-712 has no map type, so a Map<K, V, N> field, alone or inside a list or array, is rejected by the generator, and by the generic path as Unsupported naming the field.
  • Enums with payloads. EIP-712 has no union type, so an enum with a tuple or record variant is rejected by the generator, where an #[eip712] struct uses it directly, in an array or list, or through another struct, and by the generic path as Unsupported naming the field. Unit enums are unaffected, whatever other enums do.
  • Tuples. EIP-712 has no tuple type, so a tuple field, alone or inside an array, is rejected by the generator, and by the generic path as Unsupported naming the field.
  • Wallet JSON. The request writes integers as decimal strings, which JavaScript reads without losing precision, and addresses and bytes as lowercase 0x hex. It needs the domain’s name and version as text, not hashes.
  • Generic path limits. It handles up to 8 struct types per message and uses 4 KB of working memory, which Eip712::new().with_capacity(..) changes.

How it is checked

The test suite checks the EIP’s own reference values, and a contract that recovers the EIP’s example signer. Every vector is checked through both the generated and the generic path. The vectors were computed by an independent transcription of the specification and re-checked with viem, including the digest viem computes from each wallet request. For the example mail, hashStruct is 0xc52c0ee5d84264471806290a3f2c4cecfc5490626bf912d01f240d7a274b371e and the signing digest is 0xbe609aee343fb3c4b28e1df9e632fca64fcfaede20f02e86244efddf30957bd2.