description |
---|
Receive an inbound message from any Hyperlane supported network |
{% hint style="warning" %}
Developers must implement the IMessageRecipient
interface in order to be able to receive interchain messages.
{% endhint %}
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.11;
interface IMessageRecipient {
/**
* @notice Handle an interchain message
* @param _origin Domain ID of the chain from which the message came
* @param _sender Address of the message sender on the origin chain as bytes32
* @param _body Raw bytes content of message body
*/
function handle(
uint32 _origin,
bytes32 _sender,
bytes calldata _body
) external;
}
{% hint style="danger" %}
To ensure only valid interchain messages are accepted, it is important to require that msg.sender
is a known Hyperlane messaging.md.
{% endhint %}
Developers must implement access control on handle()
in order to ensure the security of interchain messages. Only a Hyperlane messaging.md contract should have permission to call this function. An example of this access control is shown below.
address constant mailbox = 0x0E3239277501d215e17a4d31c487F86a425E110B;
// for access control on handle implementations
modifier onlyMailbox() {
require(msg.sender == mailbox);
_;
}
function handle(
uint32 _origin,
bytes32 _sender,
bytes calldata _body
) external onlyMailbox {};
Developers can override Hyperlane's default multisig-ism.md-based security model by specifying their own sovereign-consensus.
See the sovereign-consensus documentation for more info on how to set the ISM used by your application.
{% hint style="info" %}
Hyperlane message senders are left-padded to bytes32
for compatibility with virtual machines that are addressed differently.
{% endhint %}
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)));
}
An example handle()
implementation for receiving messages is provided below.
event Received(uint32 origin, address sender, bytes body);
function handle(
uint32 _origin,
bytes32 _sender,
bytes memory _body
) external onlyMailbox() {
address sender = bytes32ToAddress(_sender);
emit Received(_origin, sender, _body);
}