Skip to content

Commit

Permalink
chore: update nonce invalidation interface
Browse files Browse the repository at this point in the history
  • Loading branch information
r0ohafza committed Feb 14, 2024
1 parent 49eddbb commit 0385e85
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
34 changes: 18 additions & 16 deletions packages/splits-v2/src/utils/UnorderedNonces.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ abstract contract UnorderedNonces {
/* EVENTS */
/* -------------------------------------------------------------------------- */

event NonceInvalidation(address indexed owner, uint256 word, uint256 bitMap);
event NonceInvalidation(address indexed owner, uint256 nonce);

/* -------------------------------------------------------------------------- */
/* STORAGE */
Expand All @@ -34,35 +34,37 @@ abstract contract UnorderedNonces {
/* -------------------------------------------------------------------------- */

/**
* @notice Invalidates the bits specified in mask for the bitmap at the word position.
* @dev The word is maxed at type(uint248).max.
* @param _word A number to index the nonceBitmap at.
* @param _mask A bitmap masked against msg.sender's current bitmap at the word position.
* @notice Invalidates the nonce for the msg.sender.
* @param nonce nonce to invalidate.
*/
function invalidateNonces(uint256 _word, uint256 _mask) external {
nonceBitMap[msg.sender][_word] |= _mask;
function invalidateNonce(uint256 nonce) external {
(uint256 word, uint256 bit) = calculateWordAndBit(nonce);

emit NonceInvalidation(msg.sender, _word, _mask);
// flip the bit in the bitmap by taking a bitwise XOR.
nonceBitMap[msg.sender][word] ^= bit;

emit NonceInvalidation(msg.sender, nonce);
}

/* -------------------------------------------------------------------------- */
/* INTERNAL FUNCTIONS */
/* -------------------------------------------------------------------------- */

function useNonce(address from, uint256 nonce) internal {
// word is nonce divided by 256.
uint256 word = uint256(nonce) >> 8;

// bitMap is nonce modulo 256.
uint256 bitMap = uint8(nonce);

// bit is 1 shifted left by the bitMap.
uint256 bit = 1 << bitMap;
(uint256 word, uint256 bit) = calculateWordAndBit(nonce);

// flip the bit in the bitmap by taking a bitwise XOR.
uint256 flipped = nonceBitMap[from][word] ^= bit;

// check if the bit was already flipped.
if (flipped & bit == 0) revert InvalidNonce();
}

function calculateWordAndBit(uint256 nonce) internal pure returns (uint256 word, uint256 bit) {
// word is nonce divided by 256.
word = uint256(nonce) >> 8;

// bit is 1 shifted left by the nonce modulo 256.
bit = 1 << uint8(nonce);
}
}
7 changes: 2 additions & 5 deletions packages/splits-v2/test/erc6909/ERC6909.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,8 @@ contract ERC6909Test is BaseTest {
true, _owner.addr, _owner.key, target, _isOperator, _id, _amount, target, "", _nonce, deadline
);

(uint256 word, uint256 bit) = getMask(_nonce);
vm.prank(_owner.addr);
erc6909.invalidateNonces(word, bit);
erc6909.invalidateNonce(_nonce);

vm.expectRevert(InvalidNonce.selector);
erc6909.temporaryApproveAndCallBySig(
Expand Down Expand Up @@ -467,10 +466,8 @@ contract ERC6909Test is BaseTest {
false, _owner.addr, _owner.key, _spender, isOperator, _id, _value, address(0), "", _nonce, deadline
);

(uint256 word, uint256 bit) = getMask(_nonce);

vm.prank(_owner.addr);
erc6909.invalidateNonces(word, bit);
erc6909.invalidateNonce(_nonce);

vm.expectRevert(InvalidNonce.selector);
erc6909.approveBySig(_owner.addr, _spender, isOperator, _id, _value, _nonce, deadline, signature);
Expand Down

0 comments on commit 0385e85

Please sign in to comment.