verifyTypedData โ
Verify that typed data was signed by the provided address.
๐ก Why should I use this over the verifyTypedData
util?
This Action supports verifying typed data that was signed by either a Smart Contract Account or Externally Owned Account (via ERC-6492). The verifyTypedData
util only supports Externally Owned Accounts. This is getting increasingly important as more wallets implement Account Abstraction.
Usage โ
import { account, walletClient, publicClient } from './client'
const message = {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
} as const
const signature = await walletClient.signTypedData({
account,
domain,
types,
primaryType: 'Mail',
message,
})
const valid = await publicClient.verifyTypedData({
address: account.address,
domain,
types,
primaryType: 'Mail',
message,
signature,
})
// true
// All properties on a domain are optional
export const domain = {
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
} as const
// The named list of all type definitions
export const types = {
Person: [
{ name: 'name', type: 'string' },
{ name: 'wallet', type: 'address' },
],
Mail: [
{ name: 'from', type: 'Person' },
{ name: 'to', type: 'Person' },
{ name: 'contents', type: 'string' },
],
} as const
import { createPublicClient, http } from 'viem'
import { mainnet } from 'viem/chains'
export const publicClient = createPublicClient({
chain: mainnet,
transport: http()
})
export const walletClient = createWalletClient({
transport: custom(window.ethereum)
})
// JSON-RPC Account
export const [account] = await walletClient.getAddresses()
// Local Account
export const account = privateKeyToAccount(...)
Returns โ
boolean
Wheather the signed message is valid for the given address.
Parameters โ
address โ
- Type:
Address
The Ethereum address that signed the original message.
const valid = await verifyTypedData({
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
domain: {
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
},
types,
primaryType: 'Mail',
message: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
signature: '0x...',
})
domain โ
Type: TypedDataDomain
The typed data domain.
const valid = await verifyTypedData({
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
domain: {
name: 'Ether Mail',
version: '1',
chainId: 1,
verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC',
},
types,
primaryType: 'Mail',
message: {
from: {
name: 'Cow',
wallet: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
signature: '0x...',
})
types โ
The type definitions for the typed data.
const valid = await verifyTypedData({
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
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: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
signature: '0x...',
})
primaryType โ
Type: Inferred string
.
The primary type to extract from types
and use in value
.
const valid = await verifyTypedData({
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
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: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
signature: '0x...',
})
message โ
Type: Inferred from types
& primaryType
.
const valid = await verifyTypedData({
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
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: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
signature: '0x...',
})
signature โ
- Type:
Hex | ByteArray
The signature of the typed data.
const valid = await verifyTypedData({
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
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: '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826',
},
to: {
name: 'Bob',
wallet: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
},
contents: 'Hello, Bob!',
},
signature: '0x...',
})
JSON-RPC Method โ
eth_call
to a deployless universal signature validator contract.