Skip to content

Commit

Permalink
feat: implemented safe hook
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroknots committed Feb 20, 2024
1 parent 3ae5239 commit dfb252d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ out/
/broadcast/*/31337/
/broadcast/**/dry-run/

gas_calculations/*.json
packages/modulekit/gas_calculations/*.json

# Docs
docs/
Expand Down
10 changes: 1 addition & 9 deletions accounts/safe7579/src/SafeERC7579.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
} from "erc7579/interfaces/IERC7579Module.sol";
import { AccessControl } from "./core/AccessControl.sol";
import { HookManager } from "./core/HookManager.sol";
import { ExecutionHelper } from "./core/ExecutionHelper.sol";
import { ISafeOp, SAFE_OP_TYPEHASH } from "./interfaces/ISafeOp.sol";
import { ISafe } from "./interfaces/ISafe.sol";
import {
Expand All @@ -33,14 +32,7 @@ import "forge-std/console2.sol";
* this contract creates full ERC7579 compliance to Safe accounts
* @author zeroknots.eth | rhinestone.wtf
*/
contract SafeERC7579 is
ISafeOp,
IERC7579Account,
AccessControl,
ExecutionHelper,
IMSA,
HookManager
{
contract SafeERC7579 is ISafeOp, IERC7579Account, AccessControl, IMSA, HookManager {
using UserOperationLib for PackedUserOperation;
using ModeLib for ModeCode;
using ExecutionLib for bytes;
Expand Down
2 changes: 1 addition & 1 deletion accounts/safe7579/src/core/ExecutionHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract ExecutionHelper {
address safe,
address target,
uint256 value,
bytes calldata callData
bytes memory callData
)
internal
returns (bytes memory returnData)
Expand Down
19 changes: 17 additions & 2 deletions accounts/safe7579/src/core/HookManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,24 @@ abstract contract HookManager is ModuleManager {
if (hook == address(0)) {
_;
} else {
bytes memory hookData = IHook(hook).preCheck(_msgSender(), msg.data);
bytes memory retData = _executeReturnData({
safe: msg.sender,
target: hook,
value: 0,
callData: abi.encodeCall(IHook.preCheck, (_msgSender(), msg.data))
});
bytes memory hookPreContext = abi.decode(retData, (bytes));

_;
if (!IHook(hook).postCheck(hookData)) revert HookPostCheckFailed();
retData = _executeReturnData({
safe: msg.sender,
target: hook,
value: 0,
callData: abi.encodeCall(IHook.postCheck, (hookPreContext))
});
bool success = abi.decode(retData, (bool));

if (!success) revert HookPostCheckFailed();
}
}

Expand Down
3 changes: 2 additions & 1 deletion accounts/safe7579/src/core/ModuleManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.23;

import { SentinelListLib, SENTINEL } from "sentinellist/SentinelList.sol";
import { IExecutor, IValidator, IFallback } from "erc7579/interfaces/IERC7579Module.sol";
import { ExecutionHelper } from "./ExecutionHelper.sol";
import { Receiver } from "erc7579/core/Receiver.sol";
import { AccessControl } from "./AccessControl.sol";
import { ISafe } from "../interfaces/ISafe.sol";
Expand All @@ -27,7 +28,7 @@ bytes32 constant MODULEMANAGER_STORAGE_LOCATION =
* Contract that implements ERC7579 Module compatibility for Safe accounts
* @author zeroknots.eth | rhinestone.wtf
*/
abstract contract ModuleManager is AccessControl, Receiver {
abstract contract ModuleManager is AccessControl, Receiver, ExecutionHelper {
using SentinelListLib for SentinelListLib.SentinelList;
using ValidatorStorageLib for SentinelListLib.SentinelList;

Expand Down

0 comments on commit dfb252d

Please sign in to comment.