Skip to main content

Receive a message

Contracts can receive interchain messages by implementing the handle function.

Handle

This function is called by the Mailbox contract when a message is received.

danger

To ensure only valid interchain messages are accepted, it is important to restrict access control to the Mailbox address.

function handle(
uint32 _origin,
bytes32 _sender,
bytes calldata _message
) external payable;

Parameters

  • origin: Domain of origin chain
  • sender: Address of sender on origin chain as bytes32
  • messageBody: Raw bytes content of message body
info

Sender addresses are left-padded to bytes32 for compatibility with virtual machines that are addressed differently. The following utility is provided in the TypeCasts library for convenience.

// alignment preserving cast
function bytes32ToAddress(bytes32 _buf) internal pure returns (address) {
return address(uint160(uint256(_buf)));
}

Access Control

The handle function should be restricted to the Mailbox address if the contract should only accept calls from interchain messages.

The following utility is provided in the MailboxClient library for convenience.

 * @notice Only accept messages from an Hyperlane Mailbox contract
*/
modifier onlyMailbox() {
require(
msg.sender == address(mailbox),
"MailboxClient: sender not mailbox"
);

Examples

function handle(
uint32 _origin,
bytes32 _sender,
bytes calldata _data
) external payable virtual override {
emit ReceivedMessage(_origin, _sender, msg.value, string(_data));
lastSender = _sender;
lastData = _data;
}

Verify

When a message is received, the Mailbox will verify security with an Interchain Security Module before calling the message recipient's handle.

Default Security

To query the default ISM address, you can call the defaultIsm function.

function defaultHook() external view returns (IPostDispatchHook);

Modular Security

To leverage Hyperlane's modular security, message recipients can specify a custom Interchain Security Module to verify anything about incoming messages. The Mailbox will defer to this ISM when specified.

    ) external returns (bool);
}

interface ISpecifiesInterchainSecurityModule {