跨链安全模块接口
Hyperlane 通过通用的智能合约接口模块化跨链消息安全。实现负责验证在目标链上传递的消息确实是在源链上发送的,这需要使用某些证明元数据。
消息接收者可以通过指定 InterchainSecurityModule
地址来设置自定义的安全约束。这个实现可以根据应用程序的需求进行配置、组合和定制。
IInterchainSecurityModule
接口
- Solidity
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.11;
interface IInterchainSecurityModule {
enum Types {
UNUSED,
ROUTING,
AGGREGATION,
LEGACY_MULTISIG,
MERKLE_ROOT_MULTISIG,
MESSAGE_ID_MULTISIG,
NULL, // used with relayer carrying no metadata
CCIP_READ,
ARB_L2_TO_L1,
WEIGHTED_MERKLE_ROOT_MULTISIG,
WEIGHTED_MESSAGE_ID_MULTISIG,
OP_L2_TO_L1
}
/**
* @notice Returns an enum that represents the type of security model
* encoded by this ISM.
* @dev Relayers infer how to fetch and format metadata.
*/
function moduleType() external view returns (uint8);
/**
* @notice Defines a security model responsible for verifying interchain
* messages based on the provided metadata.
* @param _metadata Off-chain metadata provided by a relayer, specific to
* the security model encoded by the module (e.g. validator signatures)
* @param _message Hyperlane encoded interchain message
* @return True if the message was verified
*/
function verify(
bytes calldata _metadata,
bytes calldata _message
) external returns (bool);
}
interface ISpecifiesInterchainSecurityModule {
function interchainSecurityModule()
external
view
returns (IInterchainSecurityModule);
}
验证
* the security model encoded by the module (e.g. validator signatures)
* @param _message Hyperlane encoded interchain message
* @return True if the message was verified
*/
Mailbox 在向接收者传递消息之前会调用 verify
。如果 verify
回滚或返回 false
,消息将不会被传递。
-
_metadata
由中继器提供的任意字节组成。通常,这些字节是特定于 ISM 的。例如,对于多重签名 ISM,_metadata
必须包含验证者签名。 -
_message
由待验证的 Hyperlane 消息组成。ISM 可以使 用它来检查待验证消息的详细信息。例如,多重签名 ISM 可以根据消息的源链更改验证者集合。
模块类型
* @notice Returns an enum that represents the type of security model
这用于向中继器表明如何编码 _metadata
。ISM 必须返回支持的模块类型之一:
enum Types {
UNUSED,
ROUTING,
AGGREGATION,
LEGACY_MULTISIG,
MERKLE_ROOT_MULTISIG,
MESSAGE_ID_MULTISIG,
NULL, // used with relayer carrying no metadata
CCIP_READ,
ARB_L2_TO_L1,
为了实现这一点,所有 ISM 合约都实现了 ISM 接口,该接口要求定义 moduleType
。
中继器根据这个类型来确定该 ISM 所需的元数据。
有关模块类型及其元数据格式的更多信息,请参见 协议。
指定 ISM
要指定想要使用的 ISM,开发者需要在任何通过 handle()
接收跨链消息的合约中实现 ISpecifiesInterchainSecurityModule
接口。
interface ISpecifiesInterchainSecurityModule {
function interchainSecurityModule()
external
view
returns (IInterchainSecurityModule);
}
如果未指定 ISM,或者指定的 ISM 是空地址,将使用在目标链 Mailbox 上配置的默认 ISM。
时序图
以下是跨链消息在目标链上被验证和传递的详细时序图。
如果接收者没有实现 ISpecifiesInterchainSecurityModule
或 recipient.interchainSecurityModule()
返回 address(0)
,将使用在 Mailbox 上配置的默认 ISM 来验证消息。