-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Add NoncesKeyed variant #5272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Amxx
merged 10 commits into
OpenZeppelin:master
from
Amxx:features/semi-abstracted-nonces
Oct 23, 2024
Merged
Add NoncesKeyed variant #5272
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bebc77d
Add semi-abstracted nonce variant
Amxx 5d92abc
Update README.adoc
Amxx dd82bbd
update
Amxx 0cd63aa
cleanup tests
Amxx d9ea82e
rename
Amxx 38cee68
rewrite as a proper override to Nonces (that calls super)
Amxx bbaf337
Update lovely-dodos-lay.md
Amxx c4a95c5
Update NoncesKeyed.sol
Amxx 86718f6
add () for clarity
Amxx 7fabf37
Merge branch 'features/semi-abstracted-nonces' of https://github.com/…
Amxx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'openzeppelin-solidity': minor | ||
| --- | ||
|
|
||
| `NoncesSemiAbstracted`: Add a variant of `Nonces` that implements ERC-4337 semi-abstracted nonce system. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.20; | ||
|
|
||
| import {Nonces} from "./Nonces.sol"; | ||
|
|
||
| /** | ||
| * @dev Alternative to {Nonces}, that support key-ed nonces. | ||
| * | ||
| * Follows the https://eips.ethereum.org/EIPS/eip-4337#semi-abstracted-nonce-support[ERC-4337's semi-abstracted nonce system]. | ||
| */ | ||
| abstract contract NoncesSemiAbstracted is Nonces { | ||
frangio marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| mapping(address owner => mapping(uint192 key => uint64)) private _nonce; | ||
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @dev Returns the next unused nonce for an address. | ||
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| function nonces(address owner) public view virtual override returns (uint256) { | ||
| return nonces(owner, 0); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns the next unused nonce for an address and key. Result contains the key prefix. | ||
| */ | ||
| function nonces(address owner, uint192 key) public view virtual returns (uint256) { | ||
| return (uint256(key) << 64) | _nonce[owner][key]; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Consumes a nonce from the default key. | ||
| * | ||
| * Returns the current value and increments nonce. | ||
| */ | ||
| function _useNonce(address owner) internal virtual override returns (uint256) { | ||
| return _useNonce(owner, 0); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Consumes a nonce from the given key. | ||
| * | ||
| * Returns the current value and increments nonce. | ||
| */ | ||
| function _useNonce(address owner, uint192 key) internal virtual returns (uint256) { | ||
| // TODO: use unchecked here? Do we expect 2**64 nonce ever be used for a single owner? | ||
| return _nonce[owner][key]++; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Same as {_useNonce} but checking that `nonce` is the next valid for `owner`. | ||
| * | ||
| * This version takes a the key and the nonce in a single uint256 parameter: | ||
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * - use the first 8 bytes for the key | ||
| * - use the last 24 bytes for the nonce | ||
| */ | ||
| function _useCheckedNonce(address owner, uint256 keyNonce) internal virtual override { | ||
| _useCheckedNonce(owner, uint192(keyNonce >> 64), uint64(keyNonce)); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Same as {_useNonce} but checking that `nonce` is the next valid for `owner`. | ||
| * | ||
| * This version takes a the key and the nonce as two different parameters. | ||
| */ | ||
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| function _useCheckedNonce(address owner, uint192 key, uint64 nonce) internal virtual { | ||
| uint256 current = _useNonce(owner, key); | ||
| if (nonce != current) { | ||
| revert InvalidAccountNonce(owner, current); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| const { ethers } = require('hardhat'); | ||
| const { expect } = require('chai'); | ||
|
|
||
| function shouldBehaveLikeNonces() { | ||
| describe('should behave like Nonces', function () { | ||
| const sender = ethers.Wallet.createRandom(); | ||
| const other = ethers.Wallet.createRandom(); | ||
|
|
||
| it('gets a nonce', async function () { | ||
| expect(await this.mock.nonces(sender)).to.equal(0n); | ||
| }); | ||
|
|
||
| describe('_useNonce', function () { | ||
| it('increments a nonce', async function () { | ||
| expect(await this.mock.nonces(sender)).to.equal(0n); | ||
|
|
||
| const eventName = ['return$_useNonce', 'return$_useNonce_address'].find(name => | ||
| this.mock.interface.getEvent(name), | ||
| ); | ||
|
|
||
| await expect(await this.mock.$_useNonce(sender)) | ||
| .to.emit(this.mock, eventName) | ||
| .withArgs(0n); | ||
|
|
||
| expect(await this.mock.nonces(sender)).to.equal(1n); | ||
| }); | ||
|
|
||
| it("increments only sender's nonce", async function () { | ||
| expect(await this.mock.nonces(sender)).to.equal(0n); | ||
| expect(await this.mock.nonces(other)).to.equal(0n); | ||
|
|
||
| await this.mock.$_useNonce(sender); | ||
|
|
||
| expect(await this.mock.nonces(sender)).to.equal(1n); | ||
| expect(await this.mock.nonces(other)).to.equal(0n); | ||
| }); | ||
| }); | ||
|
|
||
| describe('_useCheckedNonce', function () { | ||
| it('increments a nonce', async function () { | ||
| const currentNonce = await this.mock.nonces(sender); | ||
|
|
||
| expect(currentNonce).to.equal(0n); | ||
|
|
||
| await this.mock.$_useCheckedNonce(sender, currentNonce); | ||
|
|
||
| expect(await this.mock.nonces(sender)).to.equal(1n); | ||
| }); | ||
|
|
||
| it("increments only sender's nonce", async function () { | ||
| const currentNonce = await this.mock.nonces(sender); | ||
|
|
||
| expect(currentNonce).to.equal(0n); | ||
| expect(await this.mock.nonces(other)).to.equal(0n); | ||
|
|
||
| await this.mock.$_useCheckedNonce(sender, currentNonce); | ||
|
|
||
| expect(await this.mock.nonces(sender)).to.equal(1n); | ||
| expect(await this.mock.nonces(other)).to.equal(0n); | ||
| }); | ||
|
|
||
| it('reverts when nonce is not the expected', async function () { | ||
| const currentNonce = await this.mock.nonces(sender); | ||
|
|
||
| await expect(this.mock.$_useCheckedNonce(sender, currentNonce + 1n)) | ||
| .to.be.revertedWithCustomError(this.mock, 'InvalidAccountNonce') | ||
| .withArgs(sender, currentNonce); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| function shouldBehaveLikeNoncesSemiAbstracted() { | ||
| describe("should implement ERC-4337's semi-abstracted nonces", function () { | ||
| const sender = ethers.Wallet.createRandom(); | ||
|
|
||
| const keyOffset = key => key << 64n; | ||
|
|
||
| it('gets a nonce', async function () { | ||
| expect(await this.mock.nonces(sender, ethers.Typed.uint192(0n))).to.equal(keyOffset(0n) + 0n); | ||
| expect(await this.mock.nonces(sender, ethers.Typed.uint192(17n))).to.equal(keyOffset(17n) + 0n); | ||
| }); | ||
|
|
||
| describe('_useNonce', function () { | ||
| it('default variant uses key 0', async function () { | ||
| expect(await this.mock.nonces(sender, ethers.Typed.uint192(0n))).to.equal(keyOffset(0n) + 0n); | ||
| expect(await this.mock.nonces(sender, ethers.Typed.uint192(17n))).to.equal(keyOffset(17n) + 0n); | ||
|
|
||
| await expect(await this.mock.$_useNonce(sender)) | ||
| .to.emit(this.mock, 'return$_useNonce_address') | ||
| .withArgs(0n); | ||
|
|
||
| await expect(await this.mock.$_useNonce(sender, ethers.Typed.uint192(0n))) | ||
| .to.emit(this.mock, 'return$_useNonce_address_uint192') | ||
| .withArgs(1n); | ||
|
|
||
| expect(await this.mock.nonces(sender, ethers.Typed.uint192(0n))).to.equal(keyOffset(0n) + 2n); | ||
| expect(await this.mock.nonces(sender, ethers.Typed.uint192(17n))).to.equal(keyOffset(17n) + 0n); | ||
| }); | ||
|
|
||
| it('use nonce at another key', async function () { | ||
| expect(await this.mock.nonces(sender, ethers.Typed.uint192(0n))).to.equal(keyOffset(0n) + 0n); | ||
| expect(await this.mock.nonces(sender, ethers.Typed.uint192(17n))).to.equal(keyOffset(17n) + 0n); | ||
|
|
||
| await expect(await this.mock.$_useNonce(sender, ethers.Typed.uint192(17n))) | ||
| .to.emit(this.mock, 'return$_useNonce_address_uint192') | ||
| .withArgs(0n); | ||
|
|
||
| await expect(await this.mock.$_useNonce(sender, ethers.Typed.uint192(17n))) | ||
| .to.emit(this.mock, 'return$_useNonce_address_uint192') | ||
| .withArgs(1n); | ||
|
|
||
| expect(await this.mock.nonces(sender, ethers.Typed.uint192(0n))).to.equal(keyOffset(0n) + 0n); | ||
| expect(await this.mock.nonces(sender, ethers.Typed.uint192(17n))).to.equal(keyOffset(17n) + 2n); | ||
| }); | ||
| }); | ||
|
|
||
| describe('_useCheckedNonce', function () { | ||
| it('default variant uses key 0', async function () { | ||
| const currentNonce = await this.mock.nonces(sender, ethers.Typed.uint192(0n)); | ||
|
|
||
| await this.mock.$_useCheckedNonce(sender, currentNonce); | ||
|
|
||
| expect(await this.mock.nonces(sender, ethers.Typed.uint192(0n))).to.equal(currentNonce + 1n); | ||
| }); | ||
|
|
||
| it('use nonce at another key', async function () { | ||
| const currentNonce = await this.mock.nonces(sender, ethers.Typed.uint192(17n)); | ||
|
|
||
| await this.mock.$_useCheckedNonce(sender, currentNonce); | ||
|
|
||
| expect(await this.mock.nonces(sender, ethers.Typed.uint192(17n))).to.equal(currentNonce + 1n); | ||
| }); | ||
|
|
||
| it('reverts when nonce is not the expected', async function () { | ||
| const currentNonce = await this.mock.nonces(sender, ethers.Typed.uint192(42n)); | ||
|
|
||
| // use and increment | ||
| await this.mock.$_useCheckedNonce(sender, currentNonce); | ||
|
|
||
| // reuse same nonce | ||
| await expect(this.mock.$_useCheckedNonce(sender, currentNonce)) | ||
| .to.be.revertedWithCustomError(this.mock, 'InvalidAccountNonce') | ||
| .withArgs(sender, 1); | ||
|
|
||
| // use "future" nonce too early | ||
| await expect(this.mock.$_useCheckedNonce(sender, currentNonce + 10n)) | ||
| .to.be.revertedWithCustomError(this.mock, 'InvalidAccountNonce') | ||
| .withArgs(sender, 1); | ||
| }); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { | ||
| shouldBehaveLikeNonces, | ||
| shouldBehaveLikeNoncesSemiAbstracted, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,75 +1,16 @@ | ||
| const { ethers } = require('hardhat'); | ||
| const { expect } = require('chai'); | ||
| const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); | ||
| const { shouldBehaveLikeNonces } = require('./Nonces.behavior'); | ||
|
|
||
| async function fixture() { | ||
| const [sender, other] = await ethers.getSigners(); | ||
|
|
||
| const mock = await ethers.deployContract('$Nonces'); | ||
|
|
||
| return { sender, other, mock }; | ||
| return { mock }; | ||
| } | ||
|
|
||
| describe('Nonces', function () { | ||
| beforeEach(async function () { | ||
| Object.assign(this, await loadFixture(fixture)); | ||
| }); | ||
|
|
||
| it('gets a nonce', async function () { | ||
| expect(await this.mock.nonces(this.sender)).to.equal(0n); | ||
| }); | ||
|
|
||
| describe('_useNonce', function () { | ||
| it('increments a nonce', async function () { | ||
| expect(await this.mock.nonces(this.sender)).to.equal(0n); | ||
|
|
||
| await expect(await this.mock.$_useNonce(this.sender)) | ||
| .to.emit(this.mock, 'return$_useNonce') | ||
| .withArgs(0n); | ||
|
|
||
| expect(await this.mock.nonces(this.sender)).to.equal(1n); | ||
| }); | ||
|
|
||
| it("increments only sender's nonce", async function () { | ||
| expect(await this.mock.nonces(this.sender)).to.equal(0n); | ||
| expect(await this.mock.nonces(this.other)).to.equal(0n); | ||
|
|
||
| await this.mock.$_useNonce(this.sender); | ||
|
|
||
| expect(await this.mock.nonces(this.sender)).to.equal(1n); | ||
| expect(await this.mock.nonces(this.other)).to.equal(0n); | ||
| }); | ||
| }); | ||
|
|
||
| describe('_useCheckedNonce', function () { | ||
| it('increments a nonce', async function () { | ||
| const currentNonce = await this.mock.nonces(this.sender); | ||
|
|
||
| expect(currentNonce).to.equal(0n); | ||
|
|
||
| await this.mock.$_useCheckedNonce(this.sender, currentNonce); | ||
|
|
||
| expect(await this.mock.nonces(this.sender)).to.equal(1n); | ||
| }); | ||
|
|
||
| it("increments only sender's nonce", async function () { | ||
| const currentNonce = await this.mock.nonces(this.sender); | ||
|
|
||
| expect(currentNonce).to.equal(0n); | ||
| expect(await this.mock.nonces(this.other)).to.equal(0n); | ||
|
|
||
| await this.mock.$_useCheckedNonce(this.sender, currentNonce); | ||
|
|
||
| expect(await this.mock.nonces(this.sender)).to.equal(1n); | ||
| expect(await this.mock.nonces(this.other)).to.equal(0n); | ||
| }); | ||
|
|
||
| it('reverts when nonce is not the expected', async function () { | ||
| const currentNonce = await this.mock.nonces(this.sender); | ||
|
|
||
| await expect(this.mock.$_useCheckedNonce(this.sender, currentNonce + 1n)) | ||
| .to.be.revertedWithCustomError(this.mock, 'InvalidAccountNonce') | ||
| .withArgs(this.sender, currentNonce); | ||
| }); | ||
| }); | ||
| shouldBehaveLikeNonces(); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| const { ethers } = require('hardhat'); | ||
| const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); | ||
| const { shouldBehaveLikeNonces, shouldBehaveLikeNoncesSemiAbstracted } = require('./Nonces.behavior'); | ||
|
|
||
| async function fixture() { | ||
| const mock = await ethers.deployContract('$NoncesSemiAbstracted'); | ||
| return { mock }; | ||
| } | ||
|
|
||
| describe('NoncesSemiAbstracted', function () { | ||
| beforeEach(async function () { | ||
| Object.assign(this, await loadFixture(fixture)); | ||
| }); | ||
|
|
||
| shouldBehaveLikeNonces(); | ||
| shouldBehaveLikeNoncesSemiAbstracted(); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.