⛓️Transaction Nonce
Ethereum's enhanced nonce system, breaking it into a 192-bit key and a 64-bit sequence. This offers flexibility in transaction management and replay protection.
In Ethereum, every transaction has a unique identifier called a nonce
. This nonce serves three main purposes:
Replay Protection: Ensures a transaction can't be re-sent or "replayed" unintentionally.
Ordering: Determines the correct order in which transactions are included in blocks.
Uniqueness: Contributes to making every transaction distinct. A single transaction, from the same sender with the same nonce, cannot be added to the chain twice.
But here's a problem: This nonce system, which is just a single growing number, limits the ability of senders to set their own rules or logic for how transactions should be ordered or protected.
A New Approach to Nonce
To offer more flexibility, instead of using a single growing number, a new nonce mechanism has been introduced. This new nonce breaks the single value into two parts:
Key (192-bit): Think of this as a customizable label or identifier.
Sequence (64-bit): This is a growing number, just like the traditional nonce.
On the Ethereum chain, these values are visible and managed within the EntryPoint
contract, which is a kind of central hub for managing these UserOperations
.
To help users access and understand these values, there's a function:
This function lets anyone check the current nonce (which includes both the key and sequence) for any sender and key combo.
For every distinct "key", the sequence grows in order. However, if someone wants to use a new key, they can start its associated sequence at any number they choose.
The Benefit
This design preserves the core function of ensuring each UserOperation
(a kind of transaction) is unique on the chain. At the same time, it gives wallets a wide, 192-bit space to play with and set their own rules or logic using the "key", all while keeping things compact within the standard 32-byte Ethereum word size.
Using the Enhanced Nonce Mechanism
The new nonce mechanism we discussed divides the nonce into a "key" and a "sequence". Here are some practical ways this can be used.
Classic Sequential Nonce
If you want your Ethereum wallet to behave just like it always has with a simple, increasing number for the nonce, you can ensure this by adding a check in the validation function:
This line ensures that the nonce stays within the range of a classic 64-bit unsigned integer, keeping things traditional and sequential.
Ordered Administrative Events
Imagine you have an account that needs two channels: one for regular actions and another for special "administrative" actions. With the new nonce mechanism, you can achieve this by using the "key" part of the nonce to differentiate between the two:
Here's the breakdown:
First, the method signature from the call data is extracted.
Then, the "key" part of the nonce is retrieved.
Depending on the method signature, the system checks if the correct "key" has been used. Admin actions should use the
ADMIN_KEY
, while regular actions should use a key of0
.
This way, with the enhanced nonce mechanism, users have flexibility in how they structure and order their transactions, while still ensuring security and orderliness.
Last updated