概要
HookManagerコントラクトは、ERC7579における Hook モジュールの管理機能を提供します。
Hook モジュールは、他のモジュールの実行前後で別途何らかの処理を実行するコントラクトです。
コントラクトアカウント内で Hook モジュールを事前・事後に呼び出し、Hook モジュールのインストールやアンインストール時には適切な初期化・後処理とイベント発行を行います。
コード
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {IHook} from "../interfaces/IERC7579Modules.sol";
import {ModuleLib} from "../utils/ModuleLib.sol";
import {IERC7579Account} from "../interfaces/IERC7579Account.sol";
import {MODULE_TYPE_HOOK} from "../types/Constants.sol";
abstract contract HookManager {
// NOTE: currently, all install/uninstall calls onInstall/onUninstall
// I assume this does not pose any security risks, but there should be a way to branch if hook needs call to onInstall/onUninstall
// --- Hook ---
// Hook is activated on these scenarios
// - on 4337 flow, userOp.calldata starts with executeUserOp.selector && validator requires hook
// - executeFromExecutor() is invoked and executor requires hook
// - when fallback function has been invoked and fallback requires hook => native functions will not invoke hook
function _doPreHook(IHook hook, uint256 value, bytes calldata callData) internal returns (bytes memory context) {
context = hook.preCheck(msg.sender, value, callData);
}
function _doPostHook(IHook hook, bytes memory context) internal {
// bool success,
// bytes memory result
hook.postCheck(context);
}
// @notice if hook is not initialized before, kernel will call hook.onInstall no matter what flag it shows, with hookData[1:]
// @param hookData is encoded into (1bytes flag + actual hookdata) flag is for identifying if the hook has to be initialized or not
function _installHook(IHook hook, bytes calldata hookData) internal {
if (address(hook) == address(0) || address(hook) == address(1)) {
return;
}
if (!hook.isInitialized(address(this)) || (hookData.length > 0 && bytes1(hookData[0]) == bytes1(0xff))) {
// if hook is not installed, it should call onInstall
// 0xff means you want to explicitly call install hook
hook.onInstall(hookData[1:]);
}
emit IERC7579Account.ModuleInstalled(MODULE_TYPE_HOOK, address(hook));
}
// @param hookData encoded as (1bytes flag + actual hookdata) flag is for identifying if the hook has to be initialized or not
function _uninstallHook(IHook hook, bytes calldata hookData) internal {
if (address(hook) == address(0) || address(hook) == address(1)) {
return;
}
if (bytes1(hookData[0]) == bytes1(0xff)) {
// 0xff means you want to call uninstall hook
ModuleLib.uninstallModule(address(hook), hookData[1:]);
}
emit IERC7579Account.ModuleUninstalled(MODULE_TYPE_HOOK, address(hook));
}
}変数・イベント・構造体
MODULE_TYPE_HOOK
import { MODULE_TYPE_HOOK } from "../types/Constants.sol";Hook モジュールを示すERC7579のモジュールタイプID。
数値 4 を想定し、ModuleInstalled や ModuleUninstalled イベントの引数として使用されます。
ModuleInstalled
event ModuleInstalled(uint256 moduleTypeId, address module);モジュールがインストールされた時に発行されるイベント。
パラメータ
moduleTypeId- インストールされたモジュールのタイプID。
module- インストールされたモジュールコントラクトのアドレス。
ModuleUninstalled
event ModuleUninstalled(uint256 moduleTypeId, address module);モジュールがアンインストールされた時に発行されるイベント。
パラメータ
moduleTypeId- アンインストールされたモジュールのタイプID。
module- アンインストールされたモジュールコントラクトのアドレス。
関数
_doPreHook
function _doPreHook(IHook hook, uint256 value, bytes calldata callData) internal returns (bytes memory context) {
context = hook.preCheck(msg.sender, value, callData);
}Hook モジュールの preCheck を呼び出す関数。
トランザクション実行前に msg.sender、value、callData を渡して事前チェックを行い、返却された context を後続処理に利用します。
引数
hook- 呼び出す
Hookモジュール (IHook) のアドレス。
- 呼び出す
value- 実行時に送信されるETH量。
callData- 実行されるコールデータ。
戻り値
contextpreCheckが返す任意のコンテキストデータ。
_doPostHook
function _doPostHook(IHook hook, bytes memory context) internal {
hook.postCheck(context);
}Hook モジュールの postCheck を呼び出す関数。
トランザクション実行後に、事前チェックで取得した context を渡して後処理を行います。
引数
hook- 呼び出す
Hookモジュール (IHook) のアドレス。
- 呼び出す
context- 事前チェックで取得したコンテキストデータ。
_installHook
function _installHook(IHook hook, bytes calldata hookData) internal {
if (address(hook) == address(0) || address(hook) == address(1)) {
return;
}
if (!hook.isInitialized(address(this)) || (hookData.length > 0 && bytes1(hookData[0]) == bytes1(0xff))) {
hook.onInstall(hookData[1:]);
}
emit IERC7579Account.ModuleInstalled(MODULE_TYPE_HOOK, address(hook));
}Hook モジュールをインストールし、必要に応じて初期化処理を呼び出す関数。
hookがaddress(0)またはaddress(1)の場合は何もしません。Hookモジュールが未初期化またはhookDataの先頭バイトが0xffの場合はonInstallを呼び出します。- 最後に
ModuleInstalledイベントを発行します。
address(0) の場合は Hook モジュールをインストールする必要がないため、早期リターンをしています。
address(1) の場合は、特別な意味を持つ値(Hook モジュールを使用しないことを意味するなど)が入っているだけで実体として Hook モジュールが存在しているわけではないため処理をスキップしています。
引数
hook- インストール対象の
Hookモジュール (IHook) のアドレス。
- インストール対象の
hookData- 初期化フラグと実データを含むバイト列。
_uninstallHook
function _uninstallHook(IHook hook, bytes calldata hookData) internal {
if (address(hook) == address(0) || address(hook) == address(1)) {
return;
}
if (bytes1(hookData[0]) == bytes1(0xff)) {
ModuleLib.uninstallModule(address(hook), hookData[1:]);
}
emit IERC7579Account.ModuleUninstalled(MODULE_TYPE_HOOK, address(hook));
}Hook モジュールをアンインストールし、必要に応じて後処理を呼び出す関数。
hookがaddress(0)またはaddress(1)の場合は何もしません。hookDataの先頭バイトが0xffの場合はModuleLib.uninstallModuleを使ってアンインストール処理を行います。- 最後に
ModuleUninstalledイベントを発行します。
引数
hook- アンインストール対象の
Hookモジュール (IHook) のアドレス。
- アンインストール対象の
hookData- アンインストールフラグと実データを含むバイト列。