EIPs Explained

ERC-7786 · Final · ERC · 2024-10-14

ERC-7786: Cross-Chain Messaging Gateway

One gateway interface for sending and receiving arbitrary cross-chain messages, so contracts can talk across chains through any bridge (natively or via adapters) without locking into a single vendor's API.

What problem does it solve?

Every bridge or cross-chain messaging protocol has its own interface, its own workflow and its own trust model. A contract built for one protocol cannot switch to another without being rewritten, and no one can write a generic "cross-chain app" that works over any bridge.

ERC-7786 defines a small common interface: a source gateway to send a message and a recipient callback to receive one. Protocols can implement it directly, or someone can build an adapter around an existing protocol. Bridge-specific features are passed as optional attributes. It also works with non-EVM chains, because addresses use the chain-agnostic ERC-7930 format.

How it works

A message has a sender, a recipient, an opaque payload, an optional native-token value, and a list of attributes. Sender and recipient are ERC-7930 Interoperable Addresses (binary, CAIP-350 serialised), which encode both the chain and the account.

Sending. The source chain's gateway implements IERC7786GatewaySource:

  • sendMessage(recipient, payload, attributes) emits MessageSent and may return a non-zero sendId. It must revert with UnsupportedAttribute for unknown attributes, and must revert if ETH is attached but the gateway does not support value.
  • supportsAttribute(bytes4) tells callers which features exist. Each attribute is bytes4 key ++ value; keys are recommended to be function selectors, e.g. minGasLimit(uint256) → 0x39f87ba1, so each element can be built with abi.encodeWithSignature.
  • Extra steps such as paying for destination gas ("post-processing") may be needed; how is out of scope.

Receiving. The destination side's protocol calls the recipient's receiveMessage(receiveId, sender, payload), which must return its selector (0x2432ef26). The recipient must check that the caller is a gateway it trusts.

Required guarantees: safety (delivered only if sent and finalised, and only once) and liveness (eventually delivered if fees are paid). Timeliness and decentralised trust are recommended.

Sample code

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IERC7786GatewaySource {
    function sendMessage(bytes calldata recipient, bytes calldata payload, bytes[] calldata attributes)
        external payable returns (bytes32 sendId);
}

interface IERC7786Recipient {
    function receiveMessage(bytes32 receiveId, bytes calldata sender, bytes calldata payload)
        external payable returns (bytes4);
}

contract PingPong is IERC7786Recipient {
    address public immutable gateway;
    constructor(address g) { gateway = g; }

    function ping(bytes calldata remote /* ERC-7930 address */) external payable {
        bytes[] memory attrs = new bytes[](0);
        IERC7786GatewaySource(gateway).sendMessage{value: msg.value}(remote, "ping", attrs);
    }

    function receiveMessage(bytes32, bytes calldata, bytes calldata payload)
        external payable returns (bytes4)
    {
        require(msg.sender == gateway, "untrusted gateway");
        // handle payload ...
        return IERC7786Recipient.receiveMessage.selector;
    }
}

Things to know

  • Status is Final.
  • The recipient's check on msg.sender is your whole security model: you inherit the trust assumptions of every gateway you accept.
  • How a native value shows up on the destination chain is unspecified.
  • Use canonical ERC-7930 encodings; the spec warns non-canonical ones can make delivery fail silently.
  • Related work: ERC-5164 (an earlier cross-chain execution interface), ERC-7683 (cross-chain intents) and ERC-7841 (message format and mailbox).

#cross-chain #bridges #messaging #interoperability

Details

Authors
Francisco Giordano (@frangio), Hadrien Croubois (@Amxx), Ernesto García (@ernestognw), CJ Cobb (@cjcobb23), Sergey Gorbunov (@sergeynog), joxes (@Joxess)
Status
Final
Type
Standards Track / ERC
Created
2024-10-14
Requires
Required by

Machine-readable: Markdown · JSON