forked from storyprotocol/protocol-core
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce IPGraph Access Control Support (#208)
* Add IPGraphACL contract * Implement IPGraphACL to hold a control flag
- Loading branch information
1 parent
bd2b989
commit 47d8fd1
Showing
9 changed files
with
200 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity 0.8.23; | ||
|
||
import { AccessManaged } from "@openzeppelin/contracts/access/manager/AccessManaged.sol"; | ||
import { Errors } from "../lib/Errors.sol"; | ||
|
||
/// @title IPGraphACL | ||
/// @notice This contract is used to manage access to the IPGraph contract. | ||
/// It allows the access manager to whitelist addresses that can allow or disallow access to the IPGraph contract. | ||
/// It allows whitelisted addresses to allow or disallow access to the IPGraph contract. | ||
/// IPGraph precompiled check if the IPGraphACL contract allows access to the IPGraph. | ||
contract IPGraphACL is AccessManaged { | ||
// keccak256(abi.encode(uint256(keccak256("story-protocol.IPGraphACL")) - 1)) & ~bytes32(uint256(0xff)); | ||
bytes32 private constant IP_GRAPH_ACL_SLOT = 0xaf99b37fdaacca72ee7240cb1435cc9e498aee6ef4edc19c8cc0cd787f4e6800; | ||
|
||
/// @notice Whitelisted addresses that can allow or disallow access to the IPGraph contract. | ||
mapping(address => bool) public whitelist; | ||
|
||
modifier onlyWhitelisted() { | ||
if (!whitelist[msg.sender]) { | ||
revert Errors.IPGraphACL__NotWhitelisted(msg.sender); | ||
} | ||
_; | ||
} | ||
|
||
constructor(address accessManager) AccessManaged(accessManager) {} | ||
|
||
/// @notice Allow access to the IPGraph contract. | ||
function allow() external onlyWhitelisted { | ||
bytes32 slot = IP_GRAPH_ACL_SLOT; | ||
bool value = true; | ||
|
||
assembly { | ||
sstore(slot, value) | ||
} | ||
} | ||
|
||
/// @notice Disallow access to the IPGraph contract. | ||
function disallow() external onlyWhitelisted { | ||
bytes32 slot = IP_GRAPH_ACL_SLOT; | ||
bool value = false; | ||
|
||
assembly { | ||
sstore(slot, value) | ||
} | ||
} | ||
|
||
/// @notice Check if access to the IPGraph contract is allowed. | ||
function isAllowed() external view returns (bool) { | ||
bytes32 slot = IP_GRAPH_ACL_SLOT; | ||
bool value; | ||
|
||
assembly { | ||
value := sload(slot) | ||
} | ||
|
||
return value; | ||
} | ||
|
||
/// @notice Whitelist an address that can allow or disallow access to the IPGraph contract. | ||
/// @param addr The address to whitelist. | ||
function whitelistAddress(address addr) external restricted { | ||
whitelist[addr] = true; | ||
} | ||
|
||
/// @notice Revoke whitelisted address. | ||
/// @param addr The address to revoke. | ||
function revokeWhitelistedAddress(address addr) external restricted { | ||
whitelist[addr] = false; | ||
} | ||
|
||
/// @notice Check if an address is whitelisted. | ||
/// @param addr The address to check. | ||
function isWhitelisted(address addr) external view returns (bool) { | ||
return whitelist[addr]; | ||
} | ||
} |
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
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,52 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
pragma solidity 0.8.23; | ||
|
||
import { Errors } from "../../../contracts/lib/Errors.sol"; | ||
import { BaseTest } from "../utils/BaseTest.t.sol"; | ||
|
||
contract IPGraphACLTest is BaseTest { | ||
function setUp() public override { | ||
super.setUp(); | ||
} | ||
|
||
// test allow/disallow | ||
// test add/remove whitelist | ||
// onlyWhitelisted modifier | ||
|
||
function test_IPGraphACL_initialized_not_allow() public { | ||
assertFalse(ipGraphACL.isAllowed()); | ||
} | ||
|
||
function test_IPGraphACL_allow() public { | ||
vm.prank(address(licenseRegistry)); | ||
ipGraphACL.allow(); | ||
assertTrue(ipGraphACL.isAllowed()); | ||
} | ||
|
||
function test_IPGraphACL_disallow() public { | ||
vm.prank(address(licenseRegistry)); | ||
ipGraphACL.disallow(); | ||
assertFalse(ipGraphACL.isAllowed()); | ||
} | ||
|
||
function test_IPGraphACL_addToWhitelist() public { | ||
vm.prank(admin); | ||
ipGraphACL.whitelistAddress(address(0x123)); | ||
vm.prank(address(0x123)); | ||
ipGraphACL.allow(); | ||
assertTrue(ipGraphACL.isAllowed()); | ||
} | ||
|
||
function test_IPGraphACL_revert_removeFromWhitelist() public { | ||
vm.prank(admin); | ||
ipGraphACL.whitelistAddress(address(0x123)); | ||
vm.prank(address(0x123)); | ||
ipGraphACL.allow(); | ||
assertTrue(ipGraphACL.isAllowed()); | ||
vm.prank(admin); | ||
ipGraphACL.revokeWhitelistedAddress(address(0x123)); | ||
vm.prank(address(0x123)); | ||
vm.expectRevert(abi.encodeWithSelector(Errors.IPGraphACL__NotWhitelisted.selector, address(0x123))); | ||
ipGraphACL.disallow(); | ||
} | ||
} |
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