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
Name | Description |
---|---|
TypedData.assert | Asserts that EIP-712 Typed Data is valid. |
TypedData.domainSeparator | Creates EIP-712 Typed Data domainSeparator for the provided domain. |
TypedData.encode | Encodes typed data in EIP-712 format: 0x19 ‖ 0x01 ‖ domainSeparator ‖ hashStruct(message) . |
TypedData.encodeType | Encodes EIP-712 Typed Data schema for the provided primaryType. |
TypedData.extractEip712DomainTypes | Gets EIP-712 Typed Data schema for EIP-721 domain. |
TypedData.getSignPayload | Gets the payload to use for signing typed data in EIP-712 format. |
TypedData.hashDomain | Hashes EIP-712 Typed Data domain. |
TypedData.hashStruct | Hashes EIP-712 Typed Data struct. |
TypedData.serialize | Serializes EIP-712 Typed Data schema into string. |
TypedData.validate | Checks if EIP-712 Typed Data is valid. |
Errors
Name | Description |
---|---|
TypedData.BytesSizeMismatchError | Thrown when the bytes size of a typed data value does not match the expected size. |
TypedData.InvalidDomainError | Thrown when the domain is invalid. |
TypedData.InvalidPrimaryTypeError | Thrown when the primary type of a typed data value is invalid. |
TypedData.InvalidStructTypeError | Thrown when the struct type is not a valid type. |
Types
Name | Description |
---|---|
TypedData.Definition | |
TypedData.Domain | |
TypedData.EIP712DomainDefinition | |
TypedData.MessageDefinition | |
TypedData.Parameter | |
TypedData.TypedData |