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'
Usage โ
ts
import { createWalletClient, http } from 'viem'
import { toAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import { getAddress, signMessage, signTransaction } from './sign-utils'
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 })
},
})
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 })
},
})
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 })
},
})
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 })
},
})