Skip to main content

Mailbox

Hyperlane的通用消息传递(GMP)接口是以一种名为邮箱(Mailbox)的智能合约的形式实现的。该合约对message headers进行编码和解码,确保全局消息的uniqueness,并防止replay attacks

To send 跨链消息,调用dispatch函数。

To receive 要实现链间消息,需要执行 handle 函数。

IMailbox Interface
// 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);
}

信息头

邮箱在邮件正文前加上一个包含以下字段的标头:

  • version: 邮箱合同的版本
  • nonce: 从给定邮箱发送的每个邮件的唯一标识符
  • origin: 原始链的域
  • sender: 原始链上发送方的地址
  • destination: 目标链的地址
  • recipient: 目标链上收件人的地址

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

独特性

对于从给定邮箱发送的每个消息,nonce是一个单独递增的整数。它在每次发送消息时递增,作为其他相同消息的分隔符。

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

messageId是一个全局唯一的消息标识符,从dispatch调用返回,计算为消息的keccak256哈希值(带headers)。

Replay Protection

邮箱维护一个已经传递的messageId值的映射,以防止重播攻击。如果接收到的消息包含已经发送的messageId,则会拒绝该消息。

function defaultIsm() external view returns (IInterchainSecurityModule);