Skip to content
LogoLogo

signMessage

Signs an EIP-191 personal sign message via ERC-7739 PersonalSign format.

This Action is suitable to sign messages for contracts (e.g. ERC-4337 Smart Accounts) that implement (or conform to) ERC-7739 (e.g. Solady's ERC1271.sol).

With the calculated signature, you can use verifyMessage to verify the signature

Usage

// @filename: config.ts
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { erc7739Actions } from 'viem/experimental'
 
export const walletClient = createWalletClient({
  account: privateKeyToAccount('0x...'),
  transport: http()
}).extend(erc7739Actions({ 
  verifier: '0xCB9fA1eA9b8A3bf422a8639f23Df77ea66020eC2' 
}))
// @filename: example.ts
// ---cut---
import { account, walletClient } from './config'
 
const signature_1 = await walletClient.signMessage({ 
  // Account used for signing.
  account,
  message: 'hello world',
  // Verifying contract address (e.g. ERC-4337 Smart Account).
  verifier: '0xCB9fA1eA9b8A3bf422a8639f23Df77ea66020eC2'
})
 
const signature_2 = await walletClient.signMessage({
  // Account used for signing.
  account,
  // Hex data representation of message.
  message: { raw: '0x68656c6c6f20776f726c64' },
  // Verifying contract address (e.g. ERC-4337 Smart Account)
  verifier: '0xCB9fA1eA9b8A3bf422a8639f23Df77ea66020eC2'
})

Account and/or Verifier Hoisting

If you do not wish to pass an account and/or verifier to every signMessage, you can also hoist the Account and/or Verifier on the Wallet Client (see config.ts).

Learn more.

import {  } from './config'
 
const  = await .({ 
  : 'hello world',
})

Returns

Hex

The signed message.

Parameters

account

  • Type: Account | Address

Account to used to sign the message.

Accepts a JSON-RPC Account or Local Account (Private Key, etc).

import {  } from './config'
 
const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266', 
  : 'hello world',
  : '0xCB9fA1eA9b8A3bf422a8639f23Df77ea66020eC2'
})

message

  • Type: string | { raw: Hex | ByteArray }

Message to sign.

By default, viem signs the UTF-8 representation of the message.

import {  } from './config'
 
const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : 'hello world', 
  : '0xCB9fA1eA9b8A3bf422a8639f23Df77ea66020eC2',
})

To sign the data representation of the message, you can use the raw attribute.

import {  } from './config'
 
const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : { : '0x68656c6c6f20776f726c64' }, 
  : '0xCB9fA1eA9b8A3bf422a8639f23Df77ea66020eC2',
})

verifier

  • Type: Address

The address of the verifying contract (e.g. a ERC-4337 Smart Account). Required if verifierDomain is not passed.

import {  } from './config'
 
const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : 'hello world',
  : '0xCB9fA1eA9b8A3bf422a8639f23Df77ea66020eC2', 
})

verifierDomain

  • Type: TypedDataDomain

Account domain separator. Required if verifier is not passed.

import {  } from './config'
 
const  = await .({
  : '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
  : 'hello world',
  : { 
    : 'SoladyAccount', 
    : '1', 
    : 1, 
    : '0xCB9fA1eA9b8A3bf422a8639f23Df77ea66020eC2'
  }, 
})