跳到主要内容

邮箱

Hyperlane 的 Mailbox 智能合约提供了一个链上 API,用于发送和接收跨链消息。每个 Hyperlane 支持的链上都有一个部署的 Mailbox 合约。

Mailboxes 的网络为区块链之间提供了连接的基础,开发者可以利用这些连接来创建跨链应用,并为现有应用添加跨链功能。

  • 发送 跨链消息,请调用 dispatch 函数。
  • 接收 跨链消息,请实现 handle 函数。

接口

IMailbox 接口公开了两个状态变更函数:dispatch()process(),分别用于发送和接收消息。

IMailbox 接口
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0;

import {IInterchainSecurityModule} from "./IInterchainSecurityModule.sol";
import {IPostDispatchHook} from "./hooks/IPostDispatchHook.sol";

interface IMailbox {
// ============ Events ============
/**
* @notice Emitted when a new message is dispatched via Hyperlane
* @param sender The address that dispatched the message
* @param destination The destination domain of the message
* @param recipient The message recipient address on `destination`
* @param message Raw bytes of message
*/
event Dispatch(
address indexed sender,
uint32 indexed destination,
bytes32 indexed recipient,
bytes message
);

/**
* @notice Emitted when a new message is dispatched via Hyperlane
* @param messageId The unique message identifier
*/
event DispatchId(bytes32 indexed messageId);

/**
* @notice Emitted when a Hyperlane message is processed
* @param messageId The unique message identifier
*/
event ProcessId(bytes32 indexed messageId);

/**
* @notice Emitted when a Hyperlane message is delivered
* @param origin The origin domain of the message
* @param sender The message sender address on `origin`
* @param recipient The address that handled the message
*/
event Process(
uint32 indexed origin,
bytes32 indexed sender,
address indexed recipient
);

function localDomain() external view returns (uint32);

function delivered(bytes32 messageId) external view returns (bool);

function defaultIsm() external view returns (IInterchainSecurityModule);

function defaultHook() external view returns (IPostDispatchHook);

function requiredHook() external view returns (IPostDispatchHook);

function latestDispatchedId() external view returns (bytes32);

function dispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody
) external payable returns (bytes32 messageId);

function quoteDispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody
) external view returns (uint256 fee);

function dispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata body,
bytes calldata defaultHookMetadata
) external payable returns (bytes32 messageId);

function quoteDispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody,
bytes calldata defaultHookMetadata
) external view returns (uint256 fee);

function dispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata body,
bytes calldata customHookMetadata,
IPostDispatchHook customHook
) external payable returns (bytes32 messageId);

function quoteDispatch(
uint32 destinationDomain,
bytes32 recipientAddress,
bytes calldata messageBody,
bytes calldata customHookMetadata,
IPostDispatchHook customHook
) external view returns (uint256 fee);

function process(
bytes calldata metadata,
bytes calldata message
) external payable;

function recipientIsm(
address recipient
) external view returns (IInterchainSecurityModule module);
}

消息 Headers

Mailbox 在消息体前添加一个包含以下字段的头部:

字段描述
versionMailbox 合约的版本
nonce从给定 Mailbox 发送的每条消息的唯一标识符
origin原链的域名
sender原链上发送者的地址
destination目标链的域名
recipient目标链上接收者的地址

有关消息编码的更多信息,请参见 Message

唯一性

nonce 是从给定 Mailbox 发送的每条消息的单调递增整数。每次发送消息时都会递增,以作为其他相同消息的分隔符。

function delivered(bytes32 messageId) external view returns (bool);

messageId 是一个全球唯一的消息标识符,从 dispatch 调用中返回,计算方法是对消息(包括头部)进行 keccak256 哈希。

重放保护

Mailbox 维护一个已交付 messageId 值的映射,以防止重放攻击。如果收到的消息的 messageId 已经交付,则该消息将被拒绝。

function defaultIsm() external view returns (IInterchainSecurityModule);

发送

要发送跨链消息,开发者调用 Mailbox.dispatch()

此函数的参数包括消息内容、目标链 ID 和接收者地址。每条消息作为叶子插入到由 Mailbox 存储的 增量默克尔树 中。

Hyperlane 的权益证明协议使用此默克尔树来验证欺诈证明。

处理

要交付跨链消息,中继器 调用 Mailbox.process()

此函数的参数包括要交付的消息以及中继器可以指定的任意元数据。

Mailbox 将消息和元数据传递给接收者的跨链安全模块进行验证。如果 ISM 成功验证消息,Mailbox 将通过调用 recipient.handle() 将消息交付给接收者。

信息

有关 Hyperlane 消息编码的更多详细信息,请参见 Message.sol