NoncesKeyed.sol 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.2.0-rc.0) (utils/NoncesKeyed.sol)
  3. pragma solidity ^0.8.20;
  4. import {Nonces} from "./Nonces.sol";
  5. /**
  6. * @dev Alternative to {Nonces}, that supports key-ed nonces.
  7. *
  8. * Follows the https://eips.ethereum.org/EIPS/eip-4337#semi-abstracted-nonce-support[ERC-4337's semi-abstracted nonce system].
  9. *
  10. * NOTE: This contract inherits from {Nonces} and reuses its storage for the first nonce key (i.e. `0`). This
  11. * makes upgrading from {Nonces} to {NoncesKeyed} safe when using their upgradeable versions (e.g. `NoncesKeyedUpgradeable`).
  12. * Doing so will NOT reset the current state of nonces, avoiding replay attacks where a nonce is reused after the upgrade.
  13. */
  14. abstract contract NoncesKeyed is Nonces {
  15. mapping(address owner => mapping(uint192 key => uint64)) private _nonces;
  16. /// @dev Returns the next unused nonce for an address and key. Result contains the key prefix.
  17. function nonces(address owner, uint192 key) public view virtual returns (uint256) {
  18. return key == 0 ? nonces(owner) : _pack(key, _nonces[owner][key]);
  19. }
  20. /**
  21. * @dev Consumes the next unused nonce for an address and key.
  22. *
  23. * Returns the current value without the key prefix. Consumed nonce is increased, so calling this function twice
  24. * with the same arguments will return different (sequential) results.
  25. */
  26. function _useNonce(address owner, uint192 key) internal virtual returns (uint256) {
  27. // For each account, the nonce has an initial value of 0, can only be incremented by one, and cannot be
  28. // decremented or reset. This guarantees that the nonce never overflows.
  29. unchecked {
  30. // It is important to do x++ and not ++x here.
  31. return key == 0 ? _useNonce(owner) : _pack(key, _nonces[owner][key]++);
  32. }
  33. }
  34. /**
  35. * @dev Same as {_useNonce} but checking that `nonce` is the next valid for `owner`.
  36. *
  37. * This version takes the key and the nonce in a single uint256 parameter:
  38. * - use the first 24 bytes for the key
  39. * - use the last 8 bytes for the nonce
  40. */
  41. function _useCheckedNonce(address owner, uint256 keyNonce) internal virtual override {
  42. (uint192 key, ) = _unpack(keyNonce);
  43. if (key == 0) {
  44. super._useCheckedNonce(owner, keyNonce);
  45. } else {
  46. uint256 current = _useNonce(owner, key);
  47. if (keyNonce != current) revert InvalidAccountNonce(owner, current);
  48. }
  49. }
  50. /**
  51. * @dev Same as {_useNonce} but checking that `nonce` is the next valid for `owner`.
  52. *
  53. * This version takes the key and the nonce as two different parameters.
  54. */
  55. function _useCheckedNonce(address owner, uint192 key, uint64 nonce) internal virtual {
  56. _useCheckedNonce(owner, _pack(key, nonce));
  57. }
  58. /// @dev Pack key and nonce into a keyNonce
  59. function _pack(uint192 key, uint64 nonce) private pure returns (uint256) {
  60. return (uint256(key) << 64) | nonce;
  61. }
  62. /// @dev Unpack a keyNonce into its key and nonce components
  63. function _unpack(uint256 keyNonce) private pure returns (uint192 key, uint64 nonce) {
  64. return (uint192(keyNonce >> 64), uint64(keyNonce));
  65. }
  66. }