From bce422af4af95e241da2a29bcc00a25de3681cf3 Mon Sep 17 00:00:00 2001 From: Kristen Pire Date: Thu, 16 May 2024 12:13:01 +0200 Subject: [PATCH] feat(Validator): adding CONTRACT_ID() to the blacklistValidator --- script/connectV2toV1.s.sol | 2 +- src/BlacklistValidatorUpgradeable.sol | 7 ++++++ src/IValidator.sol | 6 +++++ src/Token.sol | 2 ++ src/tests/NotAMoneriumValidator.sol | 32 +++++++++++++++++++++++++++ src/{ => tests}/tokenfrontend.sol | 0 test/Blacklist.t.sol | 23 +++++++++++++++++++ test/ControllerToken.t.sol | 16 +------------- 8 files changed, 72 insertions(+), 16 deletions(-) create mode 100644 src/tests/NotAMoneriumValidator.sol rename src/{ => tests}/tokenfrontend.sol (100%) diff --git a/script/connectV2toV1.s.sol b/script/connectV2toV1.s.sol index 79c5923..5b2cf5e 100644 --- a/script/connectV2toV1.s.sol +++ b/script/connectV2toV1.s.sol @@ -3,7 +3,7 @@ pragma solidity ^0.8.20; import "forge-std/Script.sol"; import "../src/ControllerToken.sol"; -import "../src/TokenFrontend.sol"; +import "../src/tests/TokenFrontend.sol"; import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; contract All is Script { diff --git a/src/BlacklistValidatorUpgradeable.sol b/src/BlacklistValidatorUpgradeable.sol index a79d027..21ef611 100644 --- a/src/BlacklistValidatorUpgradeable.sol +++ b/src/BlacklistValidatorUpgradeable.sol @@ -20,6 +20,8 @@ contract BlacklistValidatorUpgradeable is IValidator { bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE"); + // keccak256("monerium.validator") + bytes32 private constant ID = 0x5341d189213c4172d0c7256f80bc5f8e6350af3aaff7a029625d8dd94f0f82a5; struct BlacklistValidatorStorage { mapping(address => bool) blacklist; @@ -83,6 +85,11 @@ contract BlacklistValidatorUpgradeable is address newImplementation ) internal override onlyOwner {} + // CONTRACT_ID returns the contract identifier. + function CONTRACT_ID() public pure returns (bytes32) { + return ID; + } + /** * @dev Adds an address to the blacklist. * @param adversary Address to add. diff --git a/src/IValidator.sol b/src/IValidator.sol index f69c9c6..7df02f2 100644 --- a/src/IValidator.sol +++ b/src/IValidator.sol @@ -32,4 +32,10 @@ interface IValidator { address to, uint256 amount ) external returns (bool valid); + + /** + * @dev Returns the contract identifier. + */ + function CONTRACT_ID() external view returns (bytes32); + } diff --git a/src/Token.sol b/src/Token.sol index 93880ab..b73c55e 100644 --- a/src/Token.sol +++ b/src/Token.sol @@ -56,6 +56,7 @@ contract Token is __UUPSUpgradeable_init(); __SystemRole_init(); validator = IValidator(_validator); + require(validator.CONTRACT_ID() == keccak256("monerium.validator"), "Not Monerium Validator Contract"); } // _authorizeUpgrade is a crucial part of the UUPS upgrade pattern in OpenZeppelin. @@ -109,6 +110,7 @@ contract Token is // Function to set the validator, restricted to owner function setValidator(address _validator) public onlyOwner { validator = IValidator(_validator); + require(validator.CONTRACT_ID() == keccak256("monerium.validator"), "Not Monerium Validator Contract"); } // Override transfer function to invoke validator diff --git a/src/tests/NotAMoneriumValidator.sol b/src/tests/NotAMoneriumValidator.sol new file mode 100644 index 0000000..6a3b70e --- /dev/null +++ b/src/tests/NotAMoneriumValidator.sol @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.8.20; + +import "../IValidator.sol"; + +/** + * @title NotAMoneriumValidator + * @dev This contract is not a Monerium validator. + */ +contract NotAMoneriumValidator is IValidator { + bytes32 private constant ID = + 0x0000000000000000000000000000000000000000000000000000000000000000; + + /** + * @dev returns the contract identifier. + */ + function CONTRACT_ID() public pure returns (bytes32) { + return ID; + } + + /** + * @dev Validates token transfer. + * Implements IValidator interface. + */ + function validate( + address , + address , + uint256 + ) external returns (bool valid) {} + +} + diff --git a/src/tokenfrontend.sol b/src/tests/tokenfrontend.sol similarity index 100% rename from src/tokenfrontend.sol rename to src/tests/tokenfrontend.sol diff --git a/test/Blacklist.t.sol b/test/Blacklist.t.sol index 6c69b81..3666e4d 100644 --- a/test/Blacklist.t.sol +++ b/test/Blacklist.t.sol @@ -4,6 +4,7 @@ pragma solidity ^0.8.20; import "forge-std/Test.sol"; import "../src/Token.sol"; import "../src/BlacklistValidatorUpgradeable.sol"; +import "../src/tests/NotAMoneriumValidator.sol"; import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; import "forge-std/console.sol"; @@ -13,6 +14,7 @@ contract BlackListValidatorTest is Test { Token public token; ERC1967Proxy public proxy; BlacklistValidatorUpgradeable public validator; + NotAMoneriumValidator public notAMoneriumValidator; address owner = address(this); @@ -25,6 +27,7 @@ contract BlackListValidatorTest is Test { // Deploy the implementation contract Token implementation = new Token(); BlacklistValidatorUpgradeable blacklistValidator = new BlacklistValidatorUpgradeable(); + notAMoneriumValidator = new NotAMoneriumValidator(); // Deploy the proxy contract bytes memory initDataProxy = abi.encodeWithSelector( @@ -68,6 +71,26 @@ contract BlackListValidatorTest is Test { vm.stopPrank(); } + function test_shouldNotAcceptANotMoneriumValidator() public { + vm.expectRevert("Not Monerium Validator Contract"); + token.setValidator(address(notAMoneriumValidator)); + } + + function test_shouldNotAcceptANotMoneriumValidatorAtInitialization() public { + // Deploy the implementation contract + Token implementation = new Token(); + + bytes memory initData = abi.encodeWithSelector( + Token.initialize.selector, + "token", + "EURE", + address(notAMoneriumValidator) + ); + + vm.expectRevert("Not Monerium Validator Contract"); + new ERC1967Proxy(address(implementation), initData); + } + function test_ban() public { // Add user1 to blacklist vm.prank(admin); diff --git a/test/ControllerToken.t.sol b/test/ControllerToken.t.sol index 7c1ac79..a086dc7 100644 --- a/test/ControllerToken.t.sol +++ b/test/ControllerToken.t.sol @@ -4,7 +4,7 @@ pragma solidity ^0.8.20; import "forge-std/Test.sol"; import "../src/ControllerToken.sol"; import "../src/BlacklistValidatorUpgradeable.sol"; -import "../src/tokenfrontend.sol"; +import "../src/tests/tokenfrontend.sol"; import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; import "forge-std/console.sol"; @@ -123,20 +123,6 @@ contract ControllerTokenTest is Test { frontend.transferFrom(user1, user2, 1); } - function test_from_banned_user_should_not_transferFrom() public { - // Add user1 to blacklist - vm.prank(admin); - validator.ban(user1); - assertTrue(validator.isBan(user1)); - - vm.prank(user1); - frontend.approve(user2, 1e18); - - vm.prank(user2); - vm.expectRevert("Transfer not validated"); - frontend.transferFrom(user1, user2, 1); - } - function testFail_shouldNotTransferIfNotFromFrontend() public { vm.prank(user1); token.transfer_withCaller(user1, user2, 1e18);