Skip to content

Commit

Permalink
feat: Invalidate nonce (#8)
Browse files Browse the repository at this point in the history
* feat: invalidate nonce

* fix: typo

* add require & test
  • Loading branch information
gpylypchuk authored Feb 16, 2024
1 parent 2ca9591 commit 5d4157b
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/NftReward.sol
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,17 @@ contract NftReward is Initializable, ERC721Upgradeable, OwnableUpgradeable, Paus
* @notice Upgrades contract to new implementation
* @param newImplementation New implementation address
*/

function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}

/**
* @notice Invalidates nonce value to prevent mint request reusage
* @param _nonceValue Nonce value to invalidate
*/
function invalidateNonce(uint256 _nonceValue) external onlyOwner {
require(!nonceRedeemed[_nonceValue], "Already minted");
nonceRedeemed[_nonceValue] = true;
}

//====================
// Internal methods
//====================
Expand Down
92 changes: 92 additions & 0 deletions test/NftReward.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -527,4 +527,96 @@ contract NftRewardTest is Test {
// after
assertFalse(nftReward.paused());
}

function testInvalidateNonce_ShouldInvalidateNonce() public {
// prepare arbitrary data keys
bytes32[] memory keys = new bytes32[](1);
keys[0] = keccak256("GITHUB_ORGANIZATION_NAME");
// prepare arbitrary data values
string[] memory values = new string[](1);
values[0] = "ubiquity";
// prepare mint request
NftReward.MintRequest memory mintRequest = NftReward.MintRequest({
beneficiary: user1,
deadline: block.timestamp + 1,
keys: keys,
nonce: 1,
values: values
});
// get mint request digest which should be signed
bytes32 digest = nftReward.getMintRequestDigest(mintRequest);
// minter signs mint request digest
(uint8 v, bytes32 r, bytes32 s) = vm.sign(minterPrivateKey, digest);
// get minter's signature
bytes memory signature = abi.encodePacked(r, s, v);

uint tokenId = 0;

// before
vm.expectRevert();
nftReward.ownerOf(tokenId);
assertEq(nftReward.nonceRedeemed(1), false);
assertEq(nftReward.tokenDataKeyExists(keccak256("GITHUB_ORGANIZATION_NAME")), false);

// owner invalidates
vm.prank(owner);
nftReward.invalidateNonce(1);

// user try to mint
vm.prank(user1);
vm.expectRevert("Already minted");
nftReward.safeMint(mintRequest, signature);

// after
assertEq(nftReward.nonceRedeemed(1), true);
assertEq(nftReward.tokenIdCounter(), 0);
}

function testRequireInvalidateNonce_ShouldRevert_IfNonceIsRedeemed() public {
// prepare arbitrary data keys
bytes32[] memory keys = new bytes32[](1);
keys[0] = keccak256("GITHUB_ORGANIZATION_NAME");
// prepare arbitrary data values
string[] memory values = new string[](1);
values[0] = "ubiquity";
// prepare mint request
NftReward.MintRequest memory mintRequest = NftReward.MintRequest({
beneficiary: user1,
deadline: block.timestamp + 1,
keys: keys,
nonce: 1,
values: values
});
// get mint request digest which should be signed
bytes32 digest = nftReward.getMintRequestDigest(mintRequest);
// minter signs mint request digest
(uint8 v, bytes32 r, bytes32 s) = vm.sign(minterPrivateKey, digest);
// get minter's signature
bytes memory signature = abi.encodePacked(r, s, v);

uint tokenId = 0;

// before
vm.expectRevert();
nftReward.ownerOf(tokenId);
assertEq(nftReward.nonceRedeemed(1), false);
assertEq(nftReward.tokenDataKeyExists(keccak256("GITHUB_ORGANIZATION_NAME")), false);

// user1 mints
vm.prank(user1);
nftReward.safeMint(mintRequest, signature);

// owner try to invalidate
vm.prank(owner);
vm.expectRevert("Already minted");
nftReward.invalidateNonce(1);

// after
assertEq(nftReward.nonceRedeemed(1), true);
assertEq(nftReward.tokenDataKeys(0), keccak256("GITHUB_ORGANIZATION_NAME"));
assertEq(nftReward.tokenDataKeyExists(keccak256("GITHUB_ORGANIZATION_NAME")), true);
assertEq(nftReward.ownerOf(tokenId), user1);
assertEq(nftReward.tokenIdCounter(), 1);
assertEq(nftReward.tokenData(0, keccak256("GITHUB_ORGANIZATION_NAME")), "ubiquity");
}
}

0 comments on commit 5d4157b

Please sign in to comment.