Skip to content
Cive

signTypedData (Local Account)

Signs typed data and calculates an Ethereum-specific signature in CIP-23: sign(keccak256("\x19\x01" โ€– domainSeparator โ€– hashStruct(message)))

Usage

import { ,  } from "./data";
import {  } from "cive/chains";
import {  } from "cive/accounts";
const  = ("0x...", { : . });
 
const  = await .({
  ,
  ,
  : "Mail",
  : {
    : {
      : "Cow",
      : "cfx:...",
    },
    : {
      : "Bob",
      : "cfx:...",
    },
    : "Hello, Bob!",
  },
});

Returns

0x${string}

The signed data.

Parameters

domain

Type: TypedDataDomain

The typed data domain.

const signature = await account.signTypedData({
  domain: {
    name: "Ether Mail",
    version: "1",
    chainId: 1,
    verifyingContract: "cfx:...",
  },
  types,
  primaryType: "Mail",
  message: {
    from: {
      name: "Cow",
      wallet: "cfx:...",
    },
    to: {
      name: "Bob",
      wallet: "cfx:...",
    },
    contents: "Hello, Bob!",
  },
});

types

The type definitions for the typed data.

const signature = await account.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" },
    ],
  },
  primaryType: "Mail",
  message: {
    from: {
      name: "Cow",
      wallet: "cfx:...",
    },
    to: {
      name: "Bob",
      wallet: "cfx:...",
    },
    contents: "Hello, Bob!",
  },
});

primaryType

Type: Inferred string.

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

const signature = await account.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" },
    ],
  },
  primaryType: "Mail", 
  message: {
    from: {
      name: "Cow",
      wallet: "cfx:...",
    },
    to: {
      name: "Bob",
      wallet: "cfx:...",
    },
    contents: "Hello, Bob!",
  },
});

message

Type: Inferred from types & primaryType.

const signature = await account.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" },
    ],
  },
  primaryType: "Mail",
  message: {
    from: {
      name: "Cow",
      wallet: "cfx:...",
    },
    to: {
      name: "Bob",
      wallet: "cfx:...",
    },
    contents: "Hello, Bob!",
  },
});