Skip to content
LogoLogo

signTypedData

Signs typed data and calculates an Ethereum-specific signature in https://eips.ethereum.org/EIPS/eip-712: sign(keccak256("\x19\x01" ‖ domainSeparator ‖ hashStruct(message)))

Usage

// @filename: config.ts
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
 
export const walletClient = createWalletClient({
  account: privateKeyToAccount('0x...'),
  transport: http()
})
// @filename: example.ts
// ---cut---
import { account, walletClient } from './config'
import { domain, types } from './data'
 
const signature = await walletClient.signTypedData({
  account,
  domain,
  types,
  primaryType: 'Mail',
  message: {
    from: {
      name: 'Cow',
      wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
    },
    to: {
      name: 'Bob',
      wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
    },
    contents: 'Hello, Bob!',
  },
})

Account Hoisting

If you do not wish to pass an account to every signTypedData, you can also hoist the Account on the Wallet Client (see config.ts).

Learn more.

import {  } from './config'
import { ,  } from './data'
 
const  = await .({
  ,
  ,
  : 'Mail',
  : {
    : {
      : 'Cow',
      : '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
    },
    : {
      : 'Bob',
      : '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
    },
    : 'Hello, Bob!',
  },
})

Returns

0x${string}

The signed data.

Parameters

account

  • Type: Account | Address

The Account to use for signing.

Accepts a JSON-RPC Account or Local Account (Private Key, etc).

const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
  : {
    : 'Ether Mail',
    : '1',
    : 1,
    : '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
  },
  : {
    : [
      { : 'name', : 'string' },
      { : 'wallet', : 'address' },
    ],
    : [
      { : 'from', : 'Person' },
      { : 'to', : 'Person' },
      { : 'contents', : 'string' },
    ],
  },
  : 'Mail',
  : {
    : {
      : 'Cow',
      : '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
    },
    : {
      : 'Bob',
      : '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
    },
    : 'Hello, Bob!',
  },
})

domain

Type: TypedDataDomain

The typed data domain.

const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : { 
    : 'Ether Mail',
    : '1',
    : 1,
    : '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
  },
  : {
    : [
      { : 'name', : 'string' },
      { : 'wallet', : 'address' },
    ],
    : [
      { : 'from', : 'Person' },
      { : 'to', : 'Person' },
      { : 'contents', : 'string' },
    ],
  },
  : 'Mail',
  : {
    : {
      : 'Cow',
      : '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
    },
    : {
      : 'Bob',
      : '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
    },
    : 'Hello, Bob!',
  },
})

types

The type definitions for the typed data.

const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : { 
    : 'Ether Mail',
    : '1',
    : 1,
    : '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
  },
  : { 
    : [
      { : 'name', : 'string' },
      { : 'wallet', : 'address' },
    ],
    : [
      { : 'from', : 'Person' },
      { : 'to', : 'Person' },
      { : 'contents', : 'string' },
    ],
  },
  : 'Mail',
  : {
    : {
      : 'Cow',
      : '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
    },
    : {
      : 'Bob',
      : '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
    },
    : 'Hello, Bob!',
  },
})

primaryType

Type: Inferred string.

The primary type to extract from types and use in value.

const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : { 
    : 'Ether Mail',
    : '1',
    : 1,
    : '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
  },
  : {
    : [
      { : 'name', : 'string' },
      { : 'wallet', : 'address' },
    ],
    : [ 
      { : 'from', : 'Person' },
      { : 'to', : 'Person' },
      { : 'contents', : 'string' },
    ],
  },
  : 'Mail', 
  : {
    : {
      : 'Cow',
      : '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
    },
    : {
      : 'Bob',
      : '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
    },
    : 'Hello, Bob!',
  },
})

message

Type: Inferred from types & primaryType.

const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : { 
    : 'Ether Mail',
    : '1',
    : 1,
    : '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
  },
  : {
    : [
      { : 'name', : 'string' },
      { : 'wallet', : 'address' },
    ],
    : [
      { : 'from', : 'Person' },
      { : 'to', : 'Person' },
      { : 'contents', : 'string' },
    ],
  },
  : 'Mail', 
  : { 
    : {
      : 'Cow',
      : '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
    },
    : {
      : 'Bob',
      : '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
    },
    : 'Hello, Bob!',
  },
})

Live Example

Check out the usage of signTypedData in the live Sign Typed Data Example below.

JSON-RPC Methods