Skip to content

Commit

Permalink
fix: use bools
Browse files Browse the repository at this point in the history
  • Loading branch information
kopy-kat committed Feb 24, 2024
1 parent 4a15b39 commit 3ed365d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 39 deletions.
37 changes: 17 additions & 20 deletions examples/src/PermissionsHook/PermissionsHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,22 @@ contract PermissionsHook is ERC7579HookDestruct {
CONSTANTS
//////////////////////////////////////////////////////////////////////////*/

bytes1 internal constant FALSE_CONSTANT = 0x00;
bytes1 internal constant TRUE_CONSTANT = 0x01;

error InvalidPermission();

struct ModulePermissions {
// Execution permissions
// - Target permissions
bytes1 selfCall; // 0x00 - false, 0x01 - true
bytes1 moduleCall; // 0x00 - false, 0x01 - true
bytes1 hasAllowedTargets; // 0x00 - false, 0x01 - true
bool selfCall;
bool moduleCall;
bool hasAllowedTargets;
// - Value permissions
bytes1 sendValue; // 0x00 - false, 0x01 - true
bool sendValue;
// - Calldata permissions
bytes1 hasAllowedFunctions; // 0x00 - false, 0x01 - true
bytes1 erc20Transfer; // 0x00 - false, 0x01 - true
bytes1 erc721Transfer; // 0x00 - false, 0x01 - true
bool hasAllowedFunctions;
bool erc20Transfer;
bool erc721Transfer;
// Module configuration permissions
bytes1 moduleConfig; // 0x00 - false, 0x01 - true
bool moduleConfig;
bytes4[] allowedFunctions;
address[] allowedTargets;
}
Expand Down Expand Up @@ -186,7 +183,7 @@ contract PermissionsHook is ERC7579HookDestruct {

ModulePermissions storage modulePermissions = permissions[msg.sender][msgSender];

if (modulePermissions.moduleConfig != TRUE_CONSTANT) {
if (!modulePermissions.moduleConfig) {
revert InvalidPermission();
}
}
Expand All @@ -212,7 +209,7 @@ contract PermissionsHook is ERC7579HookDestruct {

ModulePermissions storage modulePermissions = permissions[msg.sender][msgSender];

if (modulePermissions.moduleConfig != TRUE_CONSTANT) {
if (!modulePermissions.moduleConfig) {
revert InvalidPermission();
}
}
Expand All @@ -230,17 +227,17 @@ contract PermissionsHook is ERC7579HookDestruct {
internal
{
// Target permissions
if (target == msg.sender && modulePermissions.selfCall != TRUE_CONSTANT) {
if (target == msg.sender && !modulePermissions.selfCall) {
revert InvalidPermission();
}

if (modulePermissions.moduleCall != TRUE_CONSTANT) {
if (!modulePermissions.moduleCall) {
if (IERC7579Account(msg.sender).isModuleInstalled(TYPE_EXECUTOR, target, "")) {
revert InvalidPermission();
}
}

if (modulePermissions.hasAllowedTargets == TRUE_CONSTANT) {
if (modulePermissions.hasAllowedTargets) {
bool isAllowedTarget = false;
uint256 allowedTargetsLength = modulePermissions.allowedTargets.length;
for (uint256 i = 0; i < allowedTargetsLength; i++) {
Expand All @@ -256,20 +253,20 @@ contract PermissionsHook is ERC7579HookDestruct {
}

// Value permissions
if (value > 0 && modulePermissions.sendValue != TRUE_CONSTANT) {
if (value > 0 && !modulePermissions.sendValue) {
revert InvalidPermission();
}

// Calldata permissions
if (_isErc20Transfer(callData) && modulePermissions.erc20Transfer != TRUE_CONSTANT) {
if (_isErc20Transfer(callData) && !modulePermissions.erc20Transfer) {
revert InvalidPermission();
}

if (_isErc721Transfer(callData) && modulePermissions.erc721Transfer != TRUE_CONSTANT) {
if (_isErc721Transfer(callData) && !modulePermissions.erc721Transfer) {
revert InvalidPermission();
}

if (modulePermissions.hasAllowedFunctions == TRUE_CONSTANT) {
if (modulePermissions.hasAllowedFunctions) {
bool isAllowedFunction = false;
uint256 allowedFunctionsLength = modulePermissions.allowedFunctions.length;
for (uint256 i = 0; i < allowedFunctionsLength; i++) {
Expand Down
35 changes: 16 additions & 19 deletions examples/test/PermissionsHook/PermissionsHook.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ contract PermissionsHookTest is RhinestoneModuleKit, Test {
using ModuleKitHelpers for *;
using ModuleKitUserOp for *;

bytes1 internal constant FALSE_CONSTANT = 0x00;
bytes1 internal constant TRUE_CONSTANT = 0x01;

// Account instance and hook
AccountInstance internal instance;
PermissionsHook internal permissionsHook;
Expand Down Expand Up @@ -68,27 +65,27 @@ contract PermissionsHookTest is RhinestoneModuleKit, Test {
PermissionsHook.ModulePermissions[] memory permissions =
new PermissionsHook.ModulePermissions[](2);
permissions[0] = PermissionsHook.ModulePermissions({
selfCall: FALSE_CONSTANT,
moduleCall: FALSE_CONSTANT,
hasAllowedTargets: TRUE_CONSTANT,
sendValue: FALSE_CONSTANT,
hasAllowedFunctions: TRUE_CONSTANT,
erc20Transfer: FALSE_CONSTANT,
erc721Transfer: FALSE_CONSTANT,
moduleConfig: FALSE_CONSTANT,
selfCall: false,
moduleCall: false,
hasAllowedTargets: true,
sendValue: false,
hasAllowedFunctions: true,
erc20Transfer: false,
erc721Transfer: false,
moduleConfig: false,
allowedFunctions: new bytes4[](0),
allowedTargets: new address[](0)
});

permissions[1] = PermissionsHook.ModulePermissions({
selfCall: TRUE_CONSTANT,
moduleCall: TRUE_CONSTANT,
hasAllowedTargets: FALSE_CONSTANT,
sendValue: TRUE_CONSTANT,
hasAllowedFunctions: FALSE_CONSTANT,
erc20Transfer: TRUE_CONSTANT,
erc721Transfer: TRUE_CONSTANT,
moduleConfig: TRUE_CONSTANT,
selfCall: true,
moduleCall: true,
hasAllowedTargets: false,
sendValue: true,
hasAllowedFunctions: false,
erc20Transfer: true,
erc721Transfer: true,
moduleConfig: true,
allowedFunctions: new bytes4[](0),
allowedTargets: new address[](0)
});
Expand Down

0 comments on commit 3ed365d

Please sign in to comment.