# [技術] モジュールの設定

> ERC7579に準拠するコントラクトアカウントは、モジュールの追加・削除・確認を行うためのインターフェースを実装する必要があります。これにより、各モジュールの種類を識別し、必要なアクセス制御や初期化・後処理を安全に行えるようになります。

- URL: https://cardene.dev/books/erc7579/9144f6
- 本: ERC7579 (https://cardene.dev/books/erc7579.md)
- 章: 9 / 16
- 前の章: https://cardene.dev/books/erc7579/e1e798.md
- 次の章: https://cardene.dev/books/erc7579/405064.md
- 公開日: 2025-06-20
- 更新日: 2025-06-20
- トピック: Blockchain
- タグ: ブロックチェーン, solidity, ethereum, smartcontract

## 概要

**ERC7579**に準拠するコントラクトアカウントは、モジュールの追加・削除・確認を行うためのインターフェースを実装する必要があります。
これにより、各モジュールの種類を識別し、必要なアクセス制御や初期化・後処理を安全に行えるようになります。

## モジュールの種類識別について

コントラクトアカウントは、インストールされたモジュールがどのタイプ（バリデータ、エグゼキュータ、フォールバックなど）に属しているかを識別できる必要があります。
例えば、`executeFromExecutor` 関数はExecutorモジュールだけが呼び出せるべきなので、他のモジュールとは明確に区別されている必要があります。

## IERC7579ModuleConfig

```solidity:IERC7579ModuleConfig.sol
interface IERC7579ModuleConfig {
    event ModuleInstalled(uint256 moduleTypeId, address module);
    event ModuleUninstalled(uint256 moduleTypeId, address module);

    /**
     * @dev Installs a Module of a certain type on the smart account
     * @param moduleTypeId the module type ID according to the ERC-7579 spec
     * @param module the module address
     * @param initData arbitrary data that may be required on the module during `onInstall`
     * initialization.
     *
     * MUST implement authorization control
     * MUST call `onInstall` on the module with the `initData` parameter if provided
     * MUST emit ModuleInstalled event
     * MUST revert if the module is already installed or the initialization on the module failed
     */
    function installModule(uint256 moduleTypeId, address module, bytes calldata initData) external;

    /**
     * @dev Uninstalls a Module of a certain type on the smart account
     * @param moduleTypeId the module type ID according the ERC-7579 spec
     * @param module the module address
     * @param deInitData arbitrary data that may be required on the module during `onInstall`
     * initialization.
     *
     * MUST implement authorization control
     * MUST call `onUninstall` on the module with the `deInitData` parameter if provided
     * MUST emit ModuleUninstalled event
     * MUST revert if the module is not installed or the deInitialization on the module failed
     */
    function uninstallModule(uint256 moduleTypeId, address module, bytes calldata deInitData) external;

    /**
     * @dev Returns whether a module is installed on the smart account
     * @param moduleTypeId the module type ID according the ERC-7579 spec
     * @param module the module address
     * @param additionalContext arbitrary data that may be required to determine if the module is installed
     *
     * MUST return true if the module is installed and false otherwise
     */
    function isModuleInstalled(uint256 moduleTypeId, address module, bytes calldata additionalContext) external view returns (bool);
}
```

### `installModule`

```solidity
function installModule(uint256 moduleTypeId, address module, bytes calldata initData) external;
```

指定した種類のモジュールをアカウントに追加する関数。

* `moduleTypeId`
    * モジュールの種類を識別するID（**ERC7579**で定義）。
* `module`
    * モジュールのコントラクトアドレス。
* `initData`
    * モジュール初期化時に渡す任意のデータ。

適切な権限制御が必要です。
`module` の `onInstall(initData)` 関数を必ず呼び出す必要があります。
すでにインストールされている場合、あるいは初期化に失敗した場合は `revert` する必要があります。
成功時には `ModuleInstalled` イベントを発行する必要がります。

### `uninstallModule`

```solidity
function uninstallModule(uint256 moduleTypeId, address module, bytes calldata deInitData) external;
```

指定したモジュールをアカウントから取り除く関数。

* `deInitData`
    * アンインストール時にモジュールへ渡す任意のデータ（クリーンアップ処理などに使用）

適切な権限制御が必要です。
`module` の `onUninstall(deInitData)` を呼び出す必要があります。
モジュールが存在しない場合や、後処理に失敗した場合は `revert` する必要があります。
成功時には `ModuleUninstalled` イベントを発行する必要がります。

### `isModuleInstalled`

```solidity
function isModuleInstalled(uint256 moduleTypeId, address module, bytes calldata additionalContext) external view returns (bool);
```

指定されたモジュールが現在アカウントにインストールされているかを返す関数。
`additionalContext` は、必要に応じてインストール確認のために使用できる追加データです。
モジュールがインストールされていれば `true`、そうでなければ `false` を返します。

:::message
通常のモジュール判定はアドレス単位で行われますが、以下のような複雑な条件がある場合、`additionalContext` での追加情報が必要になります。

- モジュールが「特定の機能だけに有効」
- モジュールが「特定ユーザーに対してだけ有効」
- モジュールが「特定ブロックタイムやチェーンIDのもとでだけ有効」

このようなケースでは、外部から additionalContext を使って補足情報を渡すことで柔軟な判定が可能になります。
:::
