• Core
  • Actions
  • signTypedData

signTypedData

⚠️

This hook uses an experimental ethers feature. If using it, please specify the exact version of ethers you are using (e.g. specify "5.6.0", not "^5.6.0").

Action for signing typed data with connected account.

import { signTypedData } from '@wagmi/core'

Usage

The following examples use the typed data:

// All properties on a domain are optional
const domain = {
  name: 'Ether Mail',
  version: '1',
  chainId: 1,
  verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
} as const
 
// The named list of all type definitions
const types = {
  Person: [
    { name: 'name', type: 'string' },
    { name: 'wallet', type: 'address' },
  ],
  Mail: [
    { name: 'from', type: 'Person' },
    { name: 'to', type: 'Person' },
    { name: 'contents', type: 'string' },
  ],
} as const
 
const value = {
  from: {
    name: 'Cow',
    wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
  },
  to: {
    name: 'Bob',
    wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
  },
  contents: 'Hello, Bob!',
} as const

From now on, the typed data variables (above) are referenced only by name for brevity.

import { signTypedData } from '@wagmi/core'
 
const signature = await signTypedData({
  domain,
  types,
  value,
})

Return Value

string

Configuration

domain

Typed data domain.

import { signTypedData } from '@wagmi/core'
 
const signature = await signTypedData({
  domain: {
    name: 'Ether Mail',
    version: '1',
    chainId: 1,
    verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
  },
  types,
  value,
})

types

Typed data type definition.

By defining inline or adding a const assertion to types, TypeScript will infer the correct types value. See the wagmi TypeScript docs for more information.

import { signTypedData } from '@wagmi/core'
 
const signature = signTypedData({
  domain,
  types: {
    Person: [
      { name: 'name', type: 'string' },
      { name: 'wallet', type: 'address' },
    ],
    Mail: [
      { name: 'from', type: 'Person' },
      { name: 'to', type: 'Person' },
      { name: 'contents', type: 'string' },
    ],
  },
  value,
})

value

Typed data value.

import { signTypedData } from '@wagmi/core'
 
const signature = signTypedData({
  domain,
  types,
  value: {
    from: {
      name: 'Cow',
      wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
    },
    to: {
      name: 'Bob',
      wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
    },
    contents: 'Hello, Bob!',
  },
})