Overview

EIP-712 message signing allows you to sign structured data using a human-readable format.

Using the CDP-SDK, developers can enable signing while presenting clear, meaningful messages to users, rather than unintelligible raw hexadecimal hashes.

In this guide, you will learn how to:

  • Create an EVM account
  • Sign an EIP-712 message

Prerequisites

It is assumed you have already completed the Quickstart guide.

1. Create an account

To create an account, see below:

import { CdpClient } from "@coinbase/cdp-sdk";  
import "dotenv/config";  
  
const cdp = new CdpClient();  
  
const account = await cdp.evm.createAccount();  
  
console.log("Successfully created account: ", account.address);  

After running the above snippet, you should see output similar to the following:

Successfully created account: 0x6aAEc7535706ce8B36ea184B2236D53702c1F06A

2. Sign message

The v2 Wallet API supports EIP-712 for typed structured data hashing and signing, providing a way to create human-readable messages that can be signed and verified onchain.

Here is a complete example showing how to sign EIP-712 typed data:

import { CdpClient } from "@coinbase/cdp-sdk";
import "dotenv/config";
  
const cdp = new CdpClient();
  
const account = await cdp.evm.createAccount();
console.log("Created account:", account.address);
  
const signature = await cdp.evm.signTypedData({
  address: account.address,
  domain: {
    name: "MyApp",
    chainId: 1,
    verifyingContract: "0x0000000000000000000000000000000000000000",
  },
  types: {
    EIP712Domain: [
      { name: "name", type: "string" },
      { name: "chainId", type: "uint256" },
      { name: "verifyingContract", type: "address" },
    ],
    Person: [
      { name: "name", type: "string" },
      { name: "age", type: "uint256" }
    ]
  },
  primaryType: "Person",
  message: {
    name: "Alice",
    age: 25
  },
});

console.log("Signature:", signature);  

After running the above snippet, you should see output similar to the following:

Created account: 0x2Ae896e791c9596c7beDeCC3E06Fa6DA9aE2B193  
Signature:  0xa8fdc11edcf120b116d34131159ca356af52b90732425ebec48cac125a449e7d2151d97ca8232bff43568da9ee2249cf1bc187c392f1d1e53c72fc0b5937f5b31b    

To summarize, in the example above, we:

  • Created an EVM account
  • Signed a message using EIP-712 structured data, which includes: a domain separator, type definitions, primary type, and a message payload (a person named Alice who is 25 years old)

The EIP-712 structured data was used to create a human-readable message which we successfully signed cryptographically.

Video: Watch and learn

Watch this video for a walkthrough of EIP-712 message signing:

  • v2 Security: Learn about the security features of v2 Wallet API.
  • API Reference: Explore the complete API reference for v2 Wallet API.