Laurent Knauss Software Engineer
Nonce illustration

In Ethereum and other blockchains, a nonce is a number that is incremented with every transaction sent from an account.
It ensures that each transaction is unique and prevents replay attacks.

In the context of ERC-4337 and account abstraction, the nonce plays a crucial role in the UserOperation structure. Each UserOperation includes a nonce, which is checked and incremented by the smart account contract to ensure that operations are processed in order and not replayed.


Why is the nonce important?

  • Prevents replay attacks by ensuring each operation is unique.
  • Ensures correct ordering of operations.
  • Allows batching and parallel processing of UserOperations in advanced account abstraction schemes.

How is the nonce managed in ERC-4337?

In ERC-4337, the smart account contract is responsible for managing the nonce. When a UserOperation is validated and executed, the contract checks the nonce and increments it. If the nonce does not match the expected value, the operation is rejected.


Example

function validateUserOp(UserOperation calldata userOp, bytes32 userOpHash, uint256 missingAccountFunds)
    external returns (uint256 validationData) {
    require(userOp.nonce == expectedNonce, "Invalid nonce");
    // ... rest of validation logic ...
    expectedNonce++;
}
    What is a Nonce in ERC-4337? | Laurent