---
eip: 7700
label: ERC-7700
title: Cross-chain Storage Router Protocol
status: Draft
type: Standards Track
category: ERC
created: 2024-04-30
requires: 155
official: https://eips.ethereum.org/EIPS/eip-7700
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-7700.md
canonical: https://eips.huatuo.xyz/zh/eip-7700/
lang: zh
tldr: "\"CCIP-Store\"：合约以标准错误回滚，告诉客户端把写入发送到另一个 L1 合约、某个 L2，或签名的链下数据库，是 CCIP-Read 在写入方向上的配套标准。"
tags: [crosschain, storage, offchain, ccip, ens]
---

# ERC-7700: Cross-chain Storage Router Protocol

> Provides a mechanism to replace L1 storage with L2 and databases through cross-chain routers

**TL;DR:** "CCIP-Store"：合约以标准错误回滚，告诉客户端把写入发送到另一个 L1 合约、某个 L2，或签名的链下数据库，是 CCIP-Read 在写入方向上的配套标准。

## 它解决什么问题？

在以太坊主网上存储数据很昂贵。[EIP-3668](/eip-3668/)（CCIP-Read）已经让合约可以声明"答案在别处，请从这个网关获取"，许多域名记录就是这样从 L2 或数据库提供的。但如何**写入**这些数据，以及如何让数据库写入在 L1 上读回时可被验证，每个项目仍然各搞一套。

ERC-7700 昵称为 **CCIP-Store**，对写入方向做了标准化。L1 合约通过以一个众所周知的自定义错误回滚，来表明存储操作应该发往*哪里*。能识别这些错误的钱包和库无需应用专属代码即可完成写入。

## 它如何运作

规范定义了三种路由器，每一种都是一个自定义错误：

| 错误 | 含义 |
| --- | --- |
| `StorageRoutedToL1(address contractL1)` | 把同样的 calldata 发送到另一个 L1 合约 |
| `StorageRoutedToL2(address contractL2, uint256 chainId)` | 把同样的 calldata 发送到该 L2 上的合约 |
| `StorageRoutedToDatabase(string gatewayUrl)` | 对数据签名，并 POST 到链下网关 |

对于 L1 和 L2 路由，客户端只需把**完全相同的 calldata** 重新发送到指定的合约和链上。

数据库路由需要额外的安全措施，因为数据库本身没有内置的信任模型：

1. 钱包对一条固定消息签名，并由该签名派生出一对**数据签名者（data signer）**密钥（可选的密码先用 PBKDF2 拉伸，再经 HKDF）。
2. 数据签名者为每条记录签名，因此可以更新许多记录而无需钱包反复弹窗。
3. 所有者/管理者对数据签名者签署一份**授权（approval）**。
4. 客户端以兼容 CCIP-Read 的编码，把数据、签名、授权和签名者 POST 到网关。

之后通过 CCIP-Read 读取数据时，L1 合约会检查授权确实来自所有者/管理者，且数据签名来自被授权的签名者。未来的路由器（IPFS、Arweave、Solana 等）预计将通过单独的 ERC 添加。

## 示例代码

合约端：把写入路由到 L2。

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

contract RecordsL1 {
    error StorageRoutedToL2(address contractL2, uint256 chainId);

    address public immutable l2Records;
    uint256 public immutable l2ChainId;

    constructor(address _l2Records, uint256 _l2ChainId) {
        l2Records = _l2Records;
        l2ChainId = _l2ChainId;
    }

    function setValue(bytes32 node, bytes32 key, bytes32 value) external view {
        revert StorageRoutedToL2(l2Records, l2ChainId);
    }
}
```

客户端（ethers v6）：捕获错误并在 L2 上重放调用。

```ts
import { Contract, Interface } from "ethers";

const iface = new Interface([
  "function setValue(bytes32,bytes32,bytes32)",
  "error StorageRoutedToL2(address contractL2, uint256 chainId)",
]);
const l1 = new Contract(l1Address, iface, l1Provider);

try {
  await l1.setValue.staticCall(node, key, value);
} catch (e: any) {
  const err = iface.parseError(e.data);
  if (err?.name === "StorageRoutedToL2") {
    const [target, chainId] = err.args;
    const l2 = new Contract(target, iface, signerOn(chainId)); // your own signer lookup
    await l2.setValue(node, key, value); // exact same calldata
  }
}
```

## 需要注意

- **状态：Draft。**错误名称、消息格式和数据库流程仍可能变化。
- L1 和 L2 路由假定目标合约以相同的 calldata 暴露相同的函数；路由器*只*说明发送到哪里。
- 数据库路由器的安全性取决于客户端严格按规范派生数据签名者，以及读取端合约同时验证两个签名。网关本身不被信任。
- 读回数据使用 [EIP-3668](/eip-3668/)；这个 ERC 只涉及写入。


---

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