Transaction signing is a formal action on a blockchain. Within the Ethereum Virtual Machine (EVM) ecosystem, you initiate a transaction by calling the eth_sendTransaction method. This action can involve sending the native network currency, transferring tokens, creating a new smart contract, or changing the blockchain's state in any way. All such transactions must be signed by an external account, typically managed by a user's Web3 wallet.
This guide explains the core concepts and parameters for signing transactions when connecting to a browser extension wallet.
How to Initiate a Transaction
Most Web3 wallets provide a method for DApps to request a transaction signature from the user. This is the gateway for any on-chain interaction.
The exact method name might differ slightly between wallets, but the functionality and required parameters are standardized. Developers commonly use a method similar to wallet.request to prompt the user to review and sign a transaction.
Key Transaction Parameters
This section details the common parameters required for an EVM transaction. Wallets often handle the complexity of these parameters, but understanding them is crucial for developers. Transactions are broadly categorized as legacy (traditional) and EIP-1559 transactions.
Legacy Transaction Parameters
A legacy transaction is the original transaction format used on Ethereum and other EVM-compatible chains.
Gas Price [Optional]
This is an optional parameter, often recommended for use on private chains.
In Ethereum, every unit of gas consumed by a transaction has a price. Block producers prioritize transactions with higher gas prices to maximize their profits. A higher gas price usually leads to faster transaction processing but at a higher total cost. Note that this parameter may not apply to some Layer 2 (L2) networks, which might have a constant gas price or none at all.
Most wallets abstract this choice for users on mainnets, offering settings like "slow," "medium," and "fast" to select a gas price premium.
Gas Limit [Optional]
This is an optional parameter used less frequently by DApp developers.
The gas limit defines the maximum amount of gas units a transaction can consume. Wallets automatically calculate a reasonable gas limit for most standard transactions. You should only manually set this if you have specific knowledge that your smart contract interaction might benefit from a custom value.
To [Optional]
This is a hex-encoded Ethereum address. It's required for any transaction that has a recipient, which is all transactions except for contract creation.
If no to value is provided but a data value is present, the transaction will create a new smart contract.
Value [Optional]
This is the amount of the network's native currency (e.g., ETH on Ethereum) to be sent, encoded as a hexadecimal value. The value is denominated in wei, where 1 ether equals 1e18 wei.
The numbers used in Ethereum often exceed the precision of native JavaScript numbers. To avoid unexpected errors, it is highly recommended to use a big number library like BN.js or ethers.js when handling these values.
Data [Optional]
This parameter is required for creating a smart contract.
It is also used to specify which contract function to call and to encode its parameters. You can learn more about how this data is encoded in the Solidity ABI specification.
Chain ID [Currently Ignored]
The chain ID is typically determined by the network the user's wallet is currently connected to, accessible via a property like wallet.networkVersion.
Return Value
DATA, 32 Bytes - the transaction hash, or the zero hash if the transaction is not yet available.
After a contract creation transaction is mined, you can retrieve the new contract's address using eth_getTransactionReceipt.
EIP-1559 Transaction Parameters
The key difference for EIP-1559 transactions is the replacement of gasPrice with two new fields: maxPriorityFeePerGas and maxFeePerGas.
maxPriorityFeePerGas [Optional]
This is a tip, or priority fee, that a user is willing to pay to the miner/validator to incentivize them to prioritize including the transaction in a block.
maxFeePerGas [Optional]
This is the absolute maximum total fee per unit of gas a user is willing to pay. It covers both the base fee (which is burned) and the priority fee (which goes to the miner).
👉 Explore advanced transaction strategies
Connecting to a Browser Wallet
Integrating transaction signing into your DApp involves connecting to the user's wallet, typically a browser extension. The process usually follows these steps:
- Detect the presence of a Web3 wallet provider in the user's browser.
- Request permission to connect to the user's accounts.
- Once connected, you can read the current network and account address.
- To initiate a transaction, you construct a transaction object with the parameters described above.
- You then request the wallet to sign and send the transaction using its standard method (e.g.,
wallet.request({ method: 'eth_sendTransaction', params: [txObject] })). - The wallet presents a confirmation screen to the user for approval.
- Upon approval, the wallet signs the transaction and broadcasts it to the network.
This seamless connection is the foundation of the Web3 user experience, enabling secure and user-controlled interactions with the blockchain.
Frequently Asked Questions
What is the difference between signing a message and signing a transaction?
Signing a message is a free, off-chain action that proves ownership of an address without executing anything on the blockchain. Signing a transaction authorizes a specific action that will be executed on-chain, cost gas, and alter the state of the blockchain.
Why would my transaction fail?
Transactions can fail for several reasons: insufficient gas, insufficient funds for the transaction value + gas fees, a revert inside a smart contract, or an incorrectly low gas price that prevents the transaction from being mined.
How do I get the transaction receipt after signing?
After a transaction is signed and broadcast, you receive a transaction hash. You can use this hash with JSON-RPC methods like eth_getTransactionReceipt to poll the network for details about the transaction's execution, including its status (success or failure) and any logs it generated.
Should I use legacy or EIP-1559 transactions?
For most users and developers on networks that support EIP-1559 (like Ethereum Mainnet), it is recommended to use the new transaction type. It often leads to a more predictable and efficient fee market. However, your wallet or SDK will typically handle this choice for you based on the network.
What is a common mistake when setting the 'value' parameter?
The most common mistake is using units like ether instead of wei. Always remember that the value field must be in wei. Using a library to convert human-readable amounts to wei is the best practice to avoid costly errors.
Can I cancel a transaction after it's signed?
You cannot "unsign" a transaction, but you can effectively cancel it by sending a new transaction with the same nonce from the same account but with a higher gas price and a value of 0. This replacement transaction, if mined first, will override the original one.