-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Separate nonce verifiers (#16)
* feat: Separate nonce verifiers * feat: Delete NonceVerifiable * chore: Update README * feat: Update events
- Loading branch information
Showing
7 changed files
with
153 additions
and
125 deletions.
There are no files selected for viewing
This file contains 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 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 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,57 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.7; | ||
|
||
import "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol"; | ||
|
||
abstract contract AssetNonceVerifiable is ContextUpgradeable { | ||
/// @notice Current nonce per asset per signer. | ||
/// Updating it will invalidate all signatures created with the previous value on an asset level. | ||
/// @custom:schema (contract address -> token id -> signer address -> nonce) | ||
mapping(address => mapping(uint256 => mapping(address => uint256))) private assetNonce; | ||
|
||
event AssetNonceUpdated(address indexed _signer, address indexed _contractAddress, uint256 indexed _tokenId, uint256 _newNonce, address _sender); | ||
|
||
function __AssetNonceVerifiable_init() internal onlyInitializing {} | ||
|
||
function __AssetNonceVerifiable_init_unchained() internal onlyInitializing {} | ||
|
||
/// @notice Get the signer nonce for a given ERC721 token. | ||
/// @param _contractAddress The address of the ERC721 contract. | ||
/// @param _tokenId The id of the ERC721 token. | ||
/// @param _signer The address of the signer. | ||
/// @return The nonce of the given signer for the provided asset. | ||
function getAssetNonce( | ||
address _contractAddress, | ||
uint256 _tokenId, | ||
address _signer | ||
) external view returns (uint256) { | ||
return assetNonce[_contractAddress][_tokenId][_signer]; | ||
} | ||
|
||
/// @notice Increase the asset nonce of the sender by 1. | ||
/// @param _contractAddress The contract address of the asset. | ||
/// @param _tokenId The token id of the asset. | ||
function bumpAssetNonce(address _contractAddress, uint256 _tokenId) external { | ||
_bumpAssetNonce(_contractAddress, _tokenId, _msgSender()); | ||
} | ||
|
||
/// @dev Increase the asset nonce by 1 | ||
function _bumpAssetNonce( | ||
address _contractAddress, | ||
uint256 _tokenId, | ||
address _signer | ||
) internal { | ||
emit AssetNonceUpdated(_signer, _contractAddress, _tokenId, ++assetNonce[_contractAddress][_tokenId][_signer], _msgSender()); | ||
} | ||
|
||
/// @dev Reverts if the provided nonce does not match the asset nonce. | ||
function _verifyAssetNonce( | ||
address _contractAddress, | ||
uint256 _tokenId, | ||
address _signer, | ||
uint256 _nonce | ||
) internal view { | ||
require(_nonce == assetNonce[_contractAddress][_tokenId][_signer], "AssetNonceVerifiable#_verifyAssetNonce: ASSET_NONCE_MISMATCH"); | ||
} | ||
} |
This file contains 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,40 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.7; | ||
|
||
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; | ||
|
||
abstract contract ContractNonceVerifiable is OwnableUpgradeable { | ||
/// @notice Current nonce at a contract level. Only updatable by the owner of the contract. | ||
/// Updating it will invalidate all signatures created with the previous value on a contract level. | ||
uint256 private contractNonce; | ||
|
||
event ContractNonceUpdated(uint256 _newNonce, address _sender); | ||
|
||
function __ContractNonceVerifiable_init() internal onlyInitializing { | ||
__Ownable_init(); | ||
} | ||
|
||
function __ContractNonceVerifiable_init_unchained() internal onlyInitializing {} | ||
|
||
/// @notice Get the current contract nonce. | ||
/// @return The current contract nonce. | ||
function getContractNonce() external view returns (uint256) { | ||
return contractNonce; | ||
} | ||
|
||
/// @notice As the owner of the contract, increase the contract nonce by 1. | ||
function bumpContractNonce() external onlyOwner { | ||
_bumpContractNonce(); | ||
} | ||
|
||
/// @dev Increase the contract nonce by 1 | ||
function _bumpContractNonce() internal { | ||
emit ContractNonceUpdated(++contractNonce, _msgSender()); | ||
} | ||
|
||
/// @dev Reverts if the provided nonce does not match the contract nonce. | ||
function _verifyContractNonce(uint256 _nonce) internal view { | ||
require(_nonce == contractNonce, "ContractNonceVerifiable#_verifyContractNonce: CONTRACT_NONCE_MISMATCH"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,40 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.7; | ||
|
||
import "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol"; | ||
|
||
abstract contract SignerNonceVerifiable is ContextUpgradeable { | ||
/// @notice Current nonce per signer. | ||
/// Updating it will invalidate all signatures created with the previous value on a signer level. | ||
/// @custom:schema (signer address -> nonce) | ||
mapping(address => uint256) private signerNonce; | ||
|
||
event SignerNonceUpdated(address indexed _signer, uint256 _newNonce, address _sender); | ||
|
||
function __SignerNonceVerifiable_init() internal onlyInitializing {} | ||
|
||
function __SignerNonceVerifiable_init_unchained() internal onlyInitializing {} | ||
|
||
/// @notice Get the current signer nonce. | ||
/// @param _signer The address of the signer. | ||
/// @return The nonce of the given signer. | ||
function getSignerNonce(address _signer) external view returns (uint256) { | ||
return signerNonce[_signer]; | ||
} | ||
|
||
/// @notice Increase the signer nonce of the sender by 1. | ||
function bumpSignerNonce() external { | ||
_bumpSignerNonce(_msgSender()); | ||
} | ||
|
||
/// @dev Increase the signer nonce by 1 | ||
function _bumpSignerNonce(address _signer) internal { | ||
emit SignerNonceUpdated(_signer, ++signerNonce[_signer], _msgSender()); | ||
} | ||
|
||
/// @dev Reverts if the provided nonce does not match the signer nonce. | ||
function _verifySignerNonce(address _signer, uint256 _nonce) internal view { | ||
require(_nonce == signerNonce[_signer], "SignerNonceVerifiable#_verifySignerNonce: SIGNER_NONCE_MISMATCH"); | ||
} | ||
} |
This file contains 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