signTransaction
Signs a transaction, with EIP712 transaction support.
Usage
import { account, walletClient } from './config'
const request = await walletClient.prepareTransactionRequest({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n
})
const signature = await walletClient.signTransaction(request)
// 0x02f850018203118080825208808080c080a04012522854168b27e5dc3d5839bab5e6b39e1a0ffd343901ce1622e3d64b48f1a04e00902ae0502c4728cbf12156290df99c3ed7de85b1dbfe20b5c36931733a33
const hash = await walletClient.sendRawTransaction(signature)
Account Hoisting
If you do not wish to pass an account
to every prepareTransactionRequest
, you can also hoist the Account on the Wallet Client (see config.ts
).
import { walletClient } from './config'
const request = await walletClient.prepareTransactionRequest({
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n
})
const signature = await walletClient.signTransaction(request)
// 0x02f850018203118080825208808080c080a04012522854168b27e5dc3d5839bab5e6b39e1a0ffd343901ce1622e3d64b48f1a04e00902ae0502c4728cbf12156290df99c3ed7de85b1dbfe20b5c36931733a33
const hash = await client.sendRawTransaction(signature)
Returns
The signed serialized transaction.
Parameters
account
- Type:
Account | Address
The Account to send the transaction from.
Accepts a JSON-RPC Account or Local Account (Private Key, etc).
const signature = await walletClient.signTransaction({
account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n
})
to
- Type:
0x${string}
The transaction recipient or contract address.
const signature = await walletClient.signTransaction({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n,
nonce: 69
})
accessList (optional)
- Type:
AccessList
The access list.
const signature = await walletClient.signTransaction({
accessList: [
{
address: '0x1',
storageKeys: ['0x1'],
},
],
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
})
chain (optional)
- Type:
Chain
- Default:
walletClient.chain
The target chain. If there is a mismatch between the wallet's current chain & the target chain, an error will be thrown.
The chain is also used to infer its request type (e.g. the Celo chain has a gatewayFee
that you can pass through to signTransaction
).
import { zksync } from 'viem/chains'
const signature = await walletClient.signTransaction({
chain: zksync,
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n
})
data (optional)
- Type:
0x${string}
A contract hashed method call with encoded args.
const signature = await walletClient.signTransaction({
data: '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2',
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n
})
gasPrice (optional)
- Type:
bigint
The price (in wei) to pay per gas. Only applies to Legacy Transactions.
const signature = await walletClient.signTransaction({
account,
gasPrice: parseGwei('20'),
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1')
})
nonce (optional)
- Type:
number
Unique number identifying this transaction.
const signature = await walletClient.signTransaction({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: 1000000000000000000n,
nonce: 69
})
value (optional)
- Type:
bigint
Value in wei sent with this transaction.
const signature = await walletClient.signTransaction({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
value: parseEther('1'),
nonce: 69
})
gasPerPubdata (optional)
- Type:
bigint
The amount of gas for publishing one byte of data on Ethereum.
const hash = await walletClient.signTransaction({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
gasPerPubdata: 50000,
nonce: 69
})
factoryDeps (optional)
- Type:
[0x${string}]
Contains bytecode of the deployed contract.
const hash = await walletClient.signTransaction({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
factoryDeps: ['0xcde...'],
nonce: 69
})
paymaster (optional)
- Type:
Account | Address
Address of the paymaster account that will pay the fees. The paymasterInput
field is required with this one.
const hash = await walletClient.signTransaction({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
paymaster: '0x4B5DF730c2e6b28E17013A1485E5d9BC41Efe021',
paymasterInput: '0x8c5a...'
nonce: 69
})
paymasterInput (optional)
- Type:
0x${string}
Input data to the paymaster. The paymaster
field is required with this one.
const hash = await walletClient.signTransaction({
account,
to: '0x70997970c51812dc3a010c7d01b50e0d17dc79c8',
paymaster: '0x4B5DF730c2e6b28E17013A1485E5d9BC41Efe021',
paymasterInput: '0x8c5a...'
nonce: 69
})