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!",
},
});// All properties on a domain are optional
export const = {
: "CFX Mail",
: "1",
: 1029,
: "cfx:...",
} as ;
// The named list of all type definitions
export const = {
: [
{ : "name", : "string" },
{ : "wallet", : "address" },
],
: [
{ : "from", : "Person" },
{ : "to", : "Person" },
{ : "contents", : "string" },
],
} as ;import { , } from 'cive'
import { } from 'cive/chains'
export const = ({
: ,
: (.!),
})
export const [] = await .();
↑ JSON-RPC Account // export const account = privateKeyToAccount("0x...", { networkId: mainnet.id });
↑ Local AccountAccount 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).
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!",
},
});import { createWalletClient, custom } from "cive";
// Retrieve Account from an EIP-1193 Provider.
const [account] = await window.ethereum.request({
method: "eth_requestAccounts",
});
export const walletClient = createWalletClient({
account,
transport: custom(window.ethereum!),
});import { createWalletClient, http } from "cive";
import { privateKeyToAccount } from "cive/accounts";
import { mainnet } from "cive/chains";
export const walletClient = createWalletClient({
account: privateKeyToAccount("0x...", { networkId: mainnet.id }),
transport: http(),
});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
- JSON-RPC Accounts:
- Local Accounts
- Signs locally. No JSON-RPC request.