概要
ERC7579では、コントラクトアカウントの機能をモジュールとして外部化し、役割ごとに分類・管理できるように設計されています。 モジュールにはそれぞれタイプIDが割り振られており、コントラクトアカウントや他のモジュールが種類を識別するために使用されます。
モジュールの種類とタイプID
モジュールは以下の4種類に分類され、それぞれ固有のタイプIDを持ちます。
- バリデータ(Validator)
type id: 1
- エグゼキュータ(Executor)
type id: 2
- フォールバックハンドラ(Fallback)
type id: 3
- フック(Hook)
type id: 4
1つのモジュールが複数のタイプを持つことも可能です。
IERC7579Module
全てのモジュールは以下の3つの関数を実装する必要があります。
interface IERC7579Module {
/**
* @dev This function is called by the smart account during installation of the module
* @param data arbitrary data that may be required on the module during `onInstall` initialization
*
* MUST revert on error (e.g. if module is already enabled)
*/
function onInstall(bytes calldata data) external;
/**
* @dev This function is called by the smart account during uninstallation of the module
* @param data arbitrary data that may be required on the module during `onUninstall` de-initialization
*
* MUST revert on error
*/
function onUninstall(bytes calldata data) external;
/**
* @dev Returns boolean value if module is a certain type
* @param moduleTypeId the module type ID according the ERC-7579 spec
*
* MUST return true if the module is of the given type and false otherwise
*/
function isModuleType(uint256 moduleTypeId) external view returns(bool);
}onInstall
コントラクトアカウントにモジュールを追加する関数。
モジュールは data を受け取り初期化処理を行います。
すでにインストールされている場合などは revert する必要があります。
onUninstall
コントラクトアカウントからモジュールを取り除く関数。
data によってステートの後処理が可能です。
isModuleType
特定のモジュールが引数として与えられた moduleTypeId であるかを確認する関数。
データ構造の工夫
1つのモジュールが複数のタイプを持つ場合、onInstall や onUninstall に渡される data に moduleTypeId を含めることで、タイプごとの初期化処理を柔軟に制御できます。
例:
// Module.sol
function onInstall(bytes calldata data) external {
// ...
(uint256 moduleTypeId, bytes memory otherData) = abi.decode(data, (uint256, bytes));
// ...
}バリデータ(Validator)
必ず IERC7579Module と IERC7579Validator を実装する必要があります。
type id: 1
interface IERC7579Validator is IERC7579Module {
/**
* @dev Validates a UserOperation
* @param userOp the ERC-4337 PackedUserOperation
* @param userOpHash the hash of the ERC-4337 PackedUserOperation
*
* MUST validate that the signature is a valid signature of the userOpHash
* SHOULD return ERC-4337's SIG_VALIDATION_FAILED (and not revert) on signature mismatch
*/
function validateUserOp(PackedUserOperation calldata userOp, bytes32 userOpHash) external returns (uint256);
/**
* @dev Validates a signature using ERC-1271
* @param sender the address that sent the ERC-1271 request to the smart account
* @param hash the hash of the ERC-1271 request
* @param signature the signature of the ERC-1271 request
*
* MUST return the ERC-1271 `MAGIC_VALUE` if the signature is valid
* MUST NOT modify state
*/
function isValidSignatureWithSender(address sender, bytes32 hash, bytes calldata signature) external view returns (bytes4);
}validateUserOp
ERC4337の UserOperation を検証します。
不正な署名の場合は revert せず、定義された戻り値(SIG_VALIDATION_FAILED)を返すことが推奨されます。
isValidSignatureWithSender
ERC1271に基づく署名検証を行う関数。
MAGIC_VALUE を返すことで署名が有効であることを示します。
Executor
IERC7579Moduleのみを実装します。
type id: 2
コントラクトアカウントの executeFromExecutor (コントラクトアカウントの代わりに処理を実行する関数)を実行するために使用されます。
フォールバックハンドラ(Fallback Handler)
IERC7579Moduleを実装します。
type id: 3
アクセス制御に関する注意
アクセス制御を行う場合、msg.sender を使わず、ERC2771に基づいた _msgSender() を使う必要があります。
これはフォワードされたトランザクションの元の送信者を正しく識別するためです。
Hooks
IERC7579ModuleとIERC7579Hookを実装します。
type id: 4
interface IERC7579Hook is IERC7579Module {
/**
* @dev Called by the smart account before execution
* @param msgSender the address that called the smart account
* @param value the value that was sent to the smart account
* @param msgData the data that was sent to the smart account
*
* MAY return arbitrary data in the `hookData` return value
*/
function preCheck(address msgSender, uint256 value, bytes calldata msgData) external returns (bytes memory hookData);
/**
* @dev Called by the smart account after execution
* @param hookData the data that was returned by the `preCheck` function
*
* MAY validate the `hookData` to validate transaction context of the `preCheck` function
*/
function postCheck(bytes calldata hookData) external;
}preCheck
実行前に呼び出され、任意のデータ(hookData)を返す関数。
postCheck
実行後に呼び出され、preCheck で返した hookData に基づいて検証や後処理を行う関数。