NoncesKeyed.sol 3.1 KB

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