signTransaction (Local Account)
Signs a transaction with the Account's private key.
Usage
import { parseGwei } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
const account = privateKeyToAccount('0x...')
const signature = await account.signTransaction({
maxFeePerGas: parseGwei('20'),
maxPriorityFeePerGas: parseGwei('3'),
gas: 21000n,
nonce: 69,
to: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
})
// "0x02f850018203118080825208808080c080a04012522854168b27e5dc3d5839bab5e6b39e1a0ffd343901ce1622e3d64b48f1a04e00902ae0502c4728cbf12156290df99c3ed7de85b1dbfe20b5c36931733a33"
Custom serializer
viem has a built-in serializer for Legacy, EIP-2930 (0x01
) and EIP-1559 (0x02
) transaction types. If you would like to serialize on another transaction type that viem does not support internally, you can pass a custom serializer.
import { parseGwei } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
const account = privateKeyToAccount('0x...')
const signature = await account.signTransaction({
maxFeePerGas: parseGwei('20'),
maxPriorityFeePerGas: parseGwei('3'),
gas: 21000n,
nonce: 69,
to: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266'
}, {
serialize(transaction) {
const {
chainId,
nonce,
// ...
} = transaction
return concatHex([
'0x69',
toRlp([
toHex(chainId),
nonce ? toHex(nonce) : '0x',
// ...
]),
])
}
})
Returns
The signed transaction.
Parameters
accessList (optional)
- Type:
AccessList
The access list.
const signature = await account.signTransaction({
accessList: [
{
address: '0x1',
storageKeys: ['0x1'],
},
],
})
chainId (optional)
- Type:
number
The chain ID.
const signature = await account.signTransaction({
chainId: 1,
})
data (optional)
- Type:
0x${string}
Transaction data.
const signature = await account.signTransaction({
data: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
})
gas (optional)
- Type:
bigint
The gas limit for the transaction.
const signature = await account.signTransaction({
gas: 69420n,
})
gasPrice (optional)
- Type:
bigint
The price (in wei) to pay per gas. Only applies to Legacy Transactions.
const signature = await account.signTransaction({
gasPrice: parseGwei('20'),
})
maxFeePerGas (optional)
- Type:
bigint
Total fee per gas (in wei), inclusive of maxPriorityFeePerGas
. Only applies to EIP-1559 Transactions
const signature = await account.signTransaction({
maxFeePerGas: parseGwei('20'),
})
maxPriorityFeePerGas (optional)
- Type:
bigint
Max priority fee per gas (in wei). Only applies to EIP-1559 Transactions
const signature = await account.signTransaction({
maxPriorityFeePerGas: parseGwei('3'),
})
nonce (optional)
- Type:
number
Unique number identifying this transaction.
const signature = await account.signTransaction({
nonce: 69
})
to (optional)
- Type:
number
The transaction recipient.
const signature = await account.signTransaction({
to: '0x...'
})
type (optional)
- Type:
"legacy" | "eip2930" | "eip1559"
The transaction type.
const signature = await account.signTransaction({
type: 'eip1559'
})
value (optional)
- Type:
bigint
Value in wei sent with this transaction.
const signature = await account.signTransaction({
value: parseEther('1'),
})