Skip to content

TypedData

Utility functions for working with EIP-712 Typed Data

Examples

Getting Sign Payloads

Typed Data can be converted to a sign payload using TypedData.getSignPayload:

import { Secp256k1, TypedData, Hash } from 'ox'
 
const payload = TypedData.getSignPayload({ 
  domain: {
    name: 'Ether Mail',
    version: '1',
    chainId: 1,
    verifyingContract: '0x0000000000000000000000000000000000000000',
  },
  types: {
    Person: [
      { name: 'name', type: 'string' },
      { name: 'wallet', type: 'address' },
    ],
    Mail: [
      { name: 'from', type: 'Person' },
      { name: 'to', type: 'Person' },
      { name: 'contents', type: 'string' },
    ],
  },
  primaryType: 'Mail',
  message: {
    from: {
      name: 'Cow',
      wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
    },
    to: {
      name: 'Bob',
      wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
    },
    contents: 'Hello, Bob!',
  },
})
 
const signature = Secp256k1.sign({ payload, privateKey: '0x...' })

Functions

NameDescription
TypedData.assertAsserts that EIP-712 Typed Data is valid.
TypedData.domainSeparatorCreates EIP-712 Typed Data domainSeparator for the provided domain.
TypedData.encodeEncodes typed data in EIP-712 format: 0x19 ‖ 0x01 ‖ domainSeparator ‖ hashStruct(message).
TypedData.encodeTypeEncodes EIP-712 Typed Data schema for the provided primaryType.
TypedData.extractEip712DomainTypesGets EIP-712 Typed Data schema for EIP-721 domain.
TypedData.getSignPayloadGets the payload to use for signing typed data in EIP-712 format.
TypedData.hashDomainHashes EIP-712 Typed Data domain.
TypedData.hashStructHashes EIP-712 Typed Data struct.
TypedData.serializeSerializes EIP-712 Typed Data schema into string.
TypedData.validateChecks if EIP-712 Typed Data is valid.

Errors

NameDescription
TypedData.BytesSizeMismatchErrorThrown when the bytes size of a typed data value does not match the expected size.
TypedData.InvalidDomainErrorThrown when the domain is invalid.
TypedData.InvalidPrimaryTypeErrorThrown when the primary type of a typed data value is invalid.
TypedData.InvalidStructTypeErrorThrown when the struct type is not a valid type.

Types

NameDescription
TypedData.Definition
TypedData.Domain
TypedData.EIP712DomainDefinition
TypedData.MessageDefinition
TypedData.Parameter
TypedData.TypedData