Skip to content
Cive

signTypedData

Signs typed data and calculates an Ethereum-specific signature in CIP-23 format:

Usage

import { ,  } from "./config";
import { ,  } from "./data";
 
const  = await .({
  : "cfx:...",
  ,
  ,
  : "Mail",
  : {
    : {
      : "Cow",
      : "cfx:...",
    },
    : {
      : "Bob",
      : "cfx:...",
    },
    : "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 { walletClient } from "./config";
import { domain, types } from "./data";
 
const signature = await walletClient.signTypedData({
  account: "cfx:...",
  domain,
  types,
  primaryType: "Mail",
  message: {
    from: {
      name: "Cow",
      wallet: "cfx:...",
    },
    to: {
      name: "Bob",
      wallet: "cfx:...",
    },
    contents: "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 .({
  : "cfx:...", 
  : {
    : "CFX Mail",
    : "1",
    : 1029,
    : "cfx:...",
  },
  : {
    : [
      { : "name", : "string" },
      { : "wallet", : "address" },
    ],
    : [
      { : "from", : "Person" },
      { : "to", : "Person" },
      { : "contents", : "string" },
    ],
  },
  : "Mail",
  : {
    : {
      : "Cow",
      : "cfx:...",
    },
    : {
      : "Bob",
      : "cfx:...",
    },
    : "Hello, Bob!",
  },
});

domain

Type: TypedDataDomain

The typed data domain.

const  = await .({
  : "cfx:...",
  : {
    : "CFX Mail",
    : "1",
    : 1029,
    : "cfx:...",
  },
  : {
    : [
      { : "name", : "string" },
      { : "wallet", : "address" },
    ],
    : [
      { : "from", : "Person" },
      { : "to", : "Person" },
      { : "contents", : "string" },
    ],
  },
  : "Mail",
  : {
    : {
      : "Cow",
      : "cfx:...",
    },
    : {
      : "Bob",
      : "cfx:...",
    },
    : "Hello, Bob!",
  },
});

types

The type definitions for the typed data.

const  = await .({
  : "cfx:...",
  : {
    : "CFX Mail",
    : "1",
    : 1029,
    : "cfx:...",
  },
  : {
    : [
      { : "name", : "string" },
      { : "wallet", : "address" },
    ],
    : [
      { : "from", : "Person" },
      { : "to", : "Person" },
      { : "contents", : "string" },
    ],
  },
  : "Mail",
  : {
    : {
      : "Cow",
      : "cfx:...",
    },
    : {
      : "Bob",
      : "cfx:...",
    },
    : "Hello, Bob!",
  },
});

primaryType

Type: Inferred string.

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

const  = await .({
  : "cfx:...",
  : {
    : "CFX Mail",
    : "1",
    : 1029,
    : "cfx:...",
  },
  : {
    : [
      { : "name", : "string" },
      { : "wallet", : "address" },
    ],
    : [
      { : "from", : "Person" },
      { : "to", : "Person" },
      { : "contents", : "string" },
    ],
  },
  : "Mail", 
  : {
    : {
      : "Cow",
      : "cfx:...",
    },
    : {
      : "Bob",
      : "cfx:...",
    },
    : "Hello, Bob!",
  },
});

message

Type: Inferred from types & primaryType.

const  = await .({
  : "cfx:...",
  : {
    : "CFX Mail",
    : "1",
    : 1029,
    : "cfx:...",
  },
  : {
    : [
      { : "name", : "string" },
      { : "wallet", : "address" },
    ],
    : [
      { : "from", : "Person" },
      { : "to", : "Person" },
      { : "contents", : "string" },
    ],
  },
  : "Mail",
  : {
    : {
      : "Cow",
      : "cfx:...",
    },
    : {
      : "Bob",
      : "cfx:...",
    },
    : "Hello, Bob!",
  },
});

JSON-RPC Methods