---
eip: 6170
label: ERC-6170
title: Cross-Chain Messaging Interface
status: Draft
type: Standards Track
category: ERC
created: 2022-12-19
official: https://eips.ethereum.org/EIPS/eip-6170
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-6170.md
canonical: https://eips.huatuo.xyz/zh/eip-6170/
lang: zh
tldr: 一个面向跨链消息桥的草案通用接口，只有一个 sendMessage 和一个 receiveMessage 函数，并全部使用 bytes，使应用可以切换桥并接入非 EVM 链。
tags: [cross-chain, bridge, messaging, interoperability]
---

# ERC-6170: Cross-Chain Messaging Interface

> A common smart contract interface for interacting with messaging protocols.

**TL;DR:** 一个面向跨链消息桥的草案通用接口，只有一个 sendMessage 和一个 receiveMessage 函数，并全部使用 bytes，使应用可以切换桥并接入非 EVM 链。

## 它解决什么问题？

跨链应用通过任意消息桥（AMB）发送消息，而每个桥都有自己的 API、自己的重放保护和自己的安全假设。针对某一个桥编写的应用实际上被锁定了：如果该桥宕机或被攻破，迁移到另一个桥就意味着重写集成代码，还可能需要迁移状态。

ERC-6170 为桥提出了一个最小的共享接口，使应用可以用同样的方式与任何合规的桥交互，并通过更换地址来切换或组合多个桥。

## 它如何运作

合规的桥公开两个函数和两个事件：

| 成员 | 作用 |
| --- | --- |
| `sendMessage(chainId, receiver, message, data)` | 向链 `chainId` 上的 `receiver` 发送 `message`。为支付中继费用而为 payable。 |
| `receiveMessage(chainId, sender, message, data)` | 投递来自链 `chainId` 上 `sender` 的传入消息。 |
| `MessageSent(to, toChainId, message, extraData)` | 每次发送都必须触发，即使是零字节的消息 |
| `MessageReceived(from, fromChainId, message)` | 每次成功接收都必须触发 |

设计选择：

- **一切都是 `bytes`。** 链 ID 和地址使用 `bytes` 而不是 `uint256` / `address`，因此同一个接口可以寻址地址不是 20 字节的非 EVM 链。草案建议把链 ID 编码为其原生代币名称的 ABI 编码（例如 `abi.encode("ETH")`）。
- **`data` 因桥而异。** 发送时它携带桥的中继者所需的任何内容；接收时它可以携带 nonce 或证明等安全数据。
- 两个函数都返回一个 `bool` 状态。

认证不在范围之内：接收方在采取行动之前，必须自行验证发送者和消息。

## 示例代码

接口，以及一个只接受来自已知对端的消息的应用：

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

interface IEIP6170 {
    event MessageSent(bytes to, bytes toChainId, bytes message, bytes extraData);
    event MessageReceived(bytes from, bytes fromChainId, bytes message);

    function sendMessage(bytes memory chainId_, bytes memory receiver_, bytes memory message_, bytes memory data_)
        external payable returns (bool);
    function receiveMessage(bytes memory chainId_, bytes memory sender_, bytes memory message_, bytes memory data_)
        external payable returns (bool);
}

contract Pinger {
    IEIP6170 public bridge;
    bytes public peerChain;
    bytes public peer;

    constructor(IEIP6170 b, bytes memory chain, bytes memory p) { bridge = b; peerChain = chain; peer = p; }

    function ping(bytes calldata relayerData) external payable {
        bridge.sendMessage{value: msg.value}(peerChain, peer, abi.encode("ping"), relayerData);
    }

    // called by the bridge on delivery
    function onMessage(bytes calldata fromChain, bytes calldata from, bytes calldata message) external {
        require(msg.sender == address(bridge), "only bridge");
        require(keccak256(fromChain) == keccak256(peerChain) && keccak256(from) == keccak256(peer), "unknown peer");
        // handle message...
    }
}
```

桥如何调用目标应用（上面的 `onMessage`），草案没有规定；每个桥定义自己的投递钩子。

## 需要注意

- 状态为 **Draft**：接口尚未定稿，可能会变化。在依赖它之前，请确认具体的桥是否真的实现了它。
- 没有发送者认证，任何人都可以向接收合约注入消息。务必检查来源链和发送者。
- 通过一个接口使用多个桥，只有在应用同时检查每个桥的安全模型时，才能降低单点故障的风险。
- 与之相关的、致力于标准跨链执行接口的工作是 [ERC-5164](/eip-5164/)。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-6170
