# TypedData

Utility functions for working with [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712)

## Examples

### Getting Sign Payloads

Typed Data can be converted to a sign payload using [`TypedData.getSignPayload`](/api/TypedData/getSignPayload):

```ts twoslash
import { Secp256k1, TypedData, Hash } from 'ox'

const payload = TypedData.getSignPayload({
  // [!code focus:99]
  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

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`TypedData.assert`](/api/TypedData/assert) | Asserts that [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) is valid. |
| [`TypedData.domainSeparator`](/api/TypedData/domainSeparator) | Creates [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) [`domainSeparator`](https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator) for the provided domain. |
| [`TypedData.encode`](/api/TypedData/encode) | Encodes typed data in [EIP-712 format](https://eips.ethereum.org/EIPS/eip-712): `0x19 ‖ 0x01 ‖ domainSeparator ‖ hashStruct(message)`. |
| [`TypedData.encodeType`](/api/TypedData/encodeType) | Encodes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) schema for the provided primaryType. |
| [`TypedData.extractEip712DomainTypes`](/api/TypedData/extractEip712DomainTypes) | Gets [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) schema for EIP-721 domain. |
| [`TypedData.getSignPayload`](/api/TypedData/getSignPayload) | Gets the payload to use for signing typed data in [EIP-712 format](https://eips.ethereum.org/EIPS/eip-712). |
| [`TypedData.hashDomain`](/api/TypedData/hashDomain) | Hashes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) domain. |
| [`TypedData.hashStruct`](/api/TypedData/hashStruct) | Hashes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) struct. |
| [`TypedData.serialize`](/api/TypedData/serialize) | Serializes [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) schema into string. |
| [`TypedData.validate`](/api/TypedData/validate) | Checks if [EIP-712 Typed Data](https://eips.ethereum.org/EIPS/eip-712) is valid. |

## Errors

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`TypedData.BytesSizeMismatchError`](/api/TypedData/errors#typeddatabytessizemismatcherror) | Thrown when the bytes size of a typed data value does not match the expected size. |
| [`TypedData.InvalidArrayError`](/api/TypedData/errors#typeddatainvalidarrayerror) | Thrown when an array-typed value is not an array. |
| [`TypedData.InvalidArrayLengthError`](/api/TypedData/errors#typeddatainvalidarraylengtherror) | Thrown when a fixed-length array does not match its declared length. |
| [`TypedData.InvalidDomainError`](/api/TypedData/errors#typeddatainvaliddomainerror) | Thrown when the domain is invalid. |
| [`TypedData.InvalidPrimaryTypeError`](/api/TypedData/errors#typeddatainvalidprimarytypeerror) | Thrown when the primary type of a typed data value is invalid. |
| [`TypedData.InvalidStructTypeError`](/api/TypedData/errors#typeddatainvalidstructtypeerror) | Thrown when the struct type is not a valid type. |

## Types

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`TypedData.Definition`](/api/TypedData/types#typeddatadefinition) |  |
| [`TypedData.Domain`](/api/TypedData/types#typeddatadomain) |  |
| [`TypedData.EIP712DomainDefinition`](/api/TypedData/types#typeddataeip712domaindefinition) |  |
| [`TypedData.MessageDefinition`](/api/TypedData/types#typeddatamessagedefinition) |  |
| [`TypedData.Parameter`](/api/TypedData/types#typeddataparameter) |  |
| [`TypedData.TypedData`](/api/TypedData/types#typeddatatypeddata) |  |
