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
// @filename: config.ts
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(),
})
// @filename: example.ts
// ---cut---
import { toCoinbaseSmartAccount } from 'viem/account-abstraction'
import { client, owner } from './config.js'
const account = await toSmartAccount({
client,
owners: [owner],
})
const signature = await account.signMessage({
message: 'hello world',
}) Returns
The signed message.
Parameters
message
- Type:
string | { raw: Hex | ByteArray }
Message to sign.
By default, viem signs the UTF-8 representation of the message.
// @filename: config.ts
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(),
})
// @filename: example.ts
// ---cut---
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',
})To sign the data representation of the message, you can use the raw attribute.
// @filename: config.ts
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(),
})
// @filename: example.ts
// ---cut---
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' },
})