1.9 KiB

signMessage (Smart Account)

Calculates an Ethereum-specific signature in EIP-191 format: keccak256("\x19Ethereum Signed Message:\n" + len(message) + message)).

Uses the Smart Account's Owner to sign the message.

Usage

:::code-group

import { toCoinbaseSmartAccount } from 'viem/account-abstraction'
import { client, owner } from './config.js'

const account = await toSmartAccount({
  client,
  owners: [owner],
})

const signature = await account.signMessage({ // [!code focus]
  message: 'hello world', // [!code focus]
}) // [!code focus]
import { http, createPublicClient } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'

export const owner = privateKeyToAccount('0x...')
 
export const client = createPublicClient({
  chain: mainnet,
  transport: http(),
})

:::

Returns

Hex

The signed message.

Parameters

message

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

Message to sign.

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

import { toCoinbaseSmartAccount } from 'viem/account-abstraction'
import { client, owner } from './config.js'

const account = await toSmartAccount({
  client,
  owners: [owner],
})
// ---cut---
const signature = await account.signMessage({
  message: 'hello world', // [!code focus:1]
})

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

import { toCoinbaseSmartAccount } from 'viem/account-abstraction'
import { client, owner } from './config.js'

const account = await toSmartAccount({
  client,
  owners: [owner],
})
// ---cut---
const signature = await account.signMessage({
  message: { raw: '0x68656c6c6f20776f726c64' }, // [!code focus:1]
})