Custom Account
A Custom Account is an interface that has the ability to sign transactions and messages with your own custom implementation of the signable methods.
Import
ts
import { toAccount } from 'viem/accounts'
import { toAccount } from 'viem/accounts'
Usage
ts
import { createWalletClient, http } from 'viem'
import {
signMessage,
signTransaction,
signTypedData,
privateKeyToAddress,
toAccount
} from 'viem/accounts'
import { mainnet } from 'viem/chains'
const privateKey = '0x...'
const account = toAccount({
address: getAddress(privateKey),
async signMessage({ message }) {
return signMessage({ message, privateKey })
},
async signTransaction(transaction, { serializer }) {
return signTransaction({ privateKey, transaction, serializer })
},
async signTypedData(typedData) {
return signTypedData({ ...typedData, privateKey })
},
})
const client = createWalletClient({
account,
chain: mainnet,
transport: http()
})
import { createWalletClient, http } from 'viem'
import {
signMessage,
signTransaction,
signTypedData,
privateKeyToAddress,
toAccount
} from 'viem/accounts'
import { mainnet } from 'viem/chains'
const privateKey = '0x...'
const account = toAccount({
address: getAddress(privateKey),
async signMessage({ message }) {
return signMessage({ message, privateKey })
},
async signTransaction(transaction, { serializer }) {
return signTransaction({ privateKey, transaction, serializer })
},
async signTypedData(typedData) {
return signTypedData({ ...typedData, privateKey })
},
})
const client = createWalletClient({
account,
chain: mainnet,
transport: http()
})
Parameters
address
- Type:
Address
The Address of the Account.
ts
const account = toAccount({
address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
async signMessage({ message }) {
return signMessage({ message, privateKey })
},
async signTransaction(transaction, { serializer }) {
return signTransaction({ privateKey, transaction, serializer })
},
async signTypedData(typedData) {
return signTypedData({ ...typedData, privateKey })
},
})
const account = toAccount({
address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
async signMessage({ message }) {
return signMessage({ message, privateKey })
},
async signTransaction(transaction, { serializer }) {
return signTransaction({ privateKey, transaction, serializer })
},
async signTypedData(typedData) {
return signTypedData({ ...typedData, privateKey })
},
})
signMessage
Function to sign a message in EIP-191 format.
ts
const account = toAccount({
address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
async signMessage({ message }) {
return signMessage({ message, privateKey })
},
async signTransaction(transaction, { serializer }) {
return signTransaction({ privateKey, transaction, serializer })
},
async signTypedData(typedData) {
return signTypedData({ ...typedData, privateKey })
},
})
const account = toAccount({
address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
async signMessage({ message }) {
return signMessage({ message, privateKey })
},
async signTransaction(transaction, { serializer }) {
return signTransaction({ privateKey, transaction, serializer })
},
async signTypedData(typedData) {
return signTypedData({ ...typedData, privateKey })
},
})
signTransaction
Function to sign a transaction.
ts
const account = toAccount({
address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
async signMessage({ message }) {
return signMessage({ message, privateKey })
},
async signTransaction(transaction, { serializer }) {
return signTransaction({ privateKey, transaction, serializer })
},
async signTypedData(typedData) {
return signTypedData({ ...typedData, privateKey })
},
})
const account = toAccount({
address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
async signMessage({ message }) {
return signMessage({ message, privateKey })
},
async signTransaction(transaction, { serializer }) {
return signTransaction({ privateKey, transaction, serializer })
},
async signTypedData(typedData) {
return signTypedData({ ...typedData, privateKey })
},
})
signTypedData
Function to sign EIP-712 typed data.
ts
const account = toAccount({
address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
async signMessage({ message }) {
return signMessage({ message, privateKey })
},
async signTransaction(transaction, { serializer }) {
return signTransaction({ privateKey, transaction, serializer })
},
async signTypedData(typedData) {
return signTypedData({ ...typedData, privateKey })
},
})
const account = toAccount({
address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
async signMessage({ message }) {
return signMessage({ message, privateKey })
},
async signTransaction(transaction, { serializer }) {
return signTransaction({ privateKey, transaction, serializer })
},
async signTypedData(typedData) {
return signTypedData({ ...typedData, privateKey })
},
})