NoncesKeyed.sol 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. abstract contract NoncesKeyed is Nonces {
  10. mapping(address owner => mapping(uint192 key => uint64)) private _nonces;
  11. /// @dev Returns the next unused nonce for an address and key. Result contains the key prefix.
  12. function nonces(address owner, uint192 key) public view virtual returns (uint256) {
  13. return key == 0 ? nonces(owner) : _pack(key, _nonces[owner][key]);
  14. }
  15. /**
  16. * @dev Consumes the next unused nonce for an address and key.
  17. *
  18. * Returns the current value without the key prefix. Consumed nonce is increased, so calling this function twice
  19. * with the same arguments will return different (sequential) results.
  20. */
  21. function _useNonce(address owner, uint192 key) internal virtual returns (uint256) {
  22. // For each account, the nonce has an initial value of 0, can only be incremented by one, and cannot be
  23. // decremented or reset. This guarantees that the nonce never overflows.
  24. unchecked {
  25. // It is important to do x++ and not ++x here.
  26. return key == 0 ? _useNonce(owner) : _pack(key, _nonces[owner][key]++);
  27. }
  28. }
  29. /**
  30. * @dev Same as {_useNonce} but checking that `nonce` is the next valid for `owner`.
  31. *
  32. * This version takes the key and the nonce in a single uint256 parameter:
  33. * - use the first 24 bytes for the key
  34. * - use the last 8 bytes for the nonce
  35. */
  36. function _useCheckedNonce(address owner, uint256 keyNonce) internal virtual override {
  37. (uint192 key, ) = _unpack(keyNonce);
  38. if (key == 0) {
  39. super._useCheckedNonce(owner, keyNonce);
  40. } else {
  41. uint256 current = _useNonce(owner, key);
  42. if (keyNonce != current) revert InvalidAccountNonce(owner, current);
  43. }
  44. }
  45. /**
  46. * @dev Same as {_useNonce} but checking that `nonce` is the next valid for `owner`.
  47. *
  48. * This version takes the key and the nonce as two different parameters.
  49. */
  50. function _useCheckedNonce(address owner, uint192 key, uint64 nonce) internal virtual {
  51. _useCheckedNonce(owner, _pack(key, nonce));
  52. }
  53. /// @dev Pack key and nonce into a keyNonce
  54. function _pack(uint192 key, uint64 nonce) private pure returns (uint256) {
  55. return (uint256(key) << 64) | nonce;
  56. }
  57. /// @dev Unpack a keyNonce into its key and nonce components
  58. function _unpack(uint256 keyNonce) private pure returns (uint192 key, uint64 nonce) {
  59. return (uint192(keyNonce >> 64), uint64(keyNonce));
  60. }
  61. }