---
eip: 5018
label: ERC-5018
title: Filesystem-like Interface for Contracts
status: Stagnant
type: Standards Track
category: ERC
created: 2022-04-18
official: https://eips.ethereum.org/EIPS/eip-5018
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-5018.md
canonical: https://eips.huatuo.xyz/zh/eip-5018/
lang: zh
tldr: 一个文件系统风格的合约接口，用于以分块方式在链上存储任意大小的命名文件，使网站等资源可以分多笔交易上传，并通过一次调用读回。
tags: [storage, web3-url, files, onchain-web]
---

# ERC-5018: Filesystem-like Interface for Contracts

> An interface to provide access to binary objects similar to filesystems.

**TL;DR:** 一个文件系统风格的合约接口，用于以分块方式在链上存储任意大小的命名文件，使网站等资源可以分多笔交易上传，并通过一次调用读回。

## 它解决什么问题？

[ERC-4804](/eip-4804/) 让 `web3://` URL 指向某个合约，并像网页一样从中获取内容。然而真实的网站是由文件（HTML、CSS、图片）组成的目录，这些文件通过相对路径互相引用，而单个文件也可能大于一笔交易所能携带的数据量。

ERC-5018 标准化了一种**目录合约**：文件以名称为键，可一次调用读取，可分块写入。这样本地文件夹与其链上副本看起来一致，相对链接也能继续工作。

## 它如何运作

每个文件由一个 `bytes` 类型的名称标识，并存储为有序的分块列表。

整文件操作：

- `write(name, data)`：替换文件内容（payable，仅限有写权限者）。
- `read(name) → (data, exist)`：返回拼接后的所有分块。
- `size(name) → (size, chunks)` 和 `remove(name) → chunksRemoved`。
- 一个 `fallback`，把 calldata 视为以 `/` 开头的路径并返回对应文件，这正是让 `web3://` 路径式访问得以工作的原因。

面向大文件的分块操作：

- `writeChunk(name, chunkId, data)`：只能替换已有分块或追加下一个分块；`chunkId > countChunks` 会失败。
- `readChunk`、`chunkSize`、`countChunks`、`removeChunk`、`truncate(name, fromChunk)`。
- `getChunkHash(name, chunkId)`：让客户端可以跳过未改动分块的重复上传。

`write` 可以实现为 `writeChunk(name, 0, data)` 再接 `truncate(name, 1)`。谁拥有写权限由具体实现决定。

## 示例代码

接口，以及使用 ethers v6 分块上传大文件：

```solidity
interface IERC5018 {
    function write(bytes memory name, bytes memory data) external payable;
    function read(bytes memory name) external view returns (bytes memory data, bool exist);
    function size(bytes memory name) external view returns (uint256 size, uint256 chunks);
    function remove(bytes memory name) external returns (uint256 numOfChunksRemoved);
    function countChunks(bytes memory name) external view returns (uint256);
    function writeChunk(bytes memory name, uint256 chunkId, bytes memory chunkData) external payable;
    function readChunk(bytes memory name, uint256 chunkId) external view returns (bytes memory, bool);
    function chunkSize(bytes memory name, uint256 chunkId) external view returns (uint256, bool);
    function removeChunk(bytes memory name, uint256 chunkId) external returns (bool);
    function truncate(bytes memory name, uint256 chunkId) external returns (uint256);
    function getChunkHash(bytes memory name, uint256 chunkId) external view returns (bytes32);
}
```

```ts
import { Contract, toUtf8Bytes, keccak256 } from "ethers";

const CHUNK = 24 * 1024;
async function upload(dir: Contract, path: string, data: Uint8Array) {
  const name = toUtf8Bytes(path);
  for (let i = 0; i * CHUNK < data.length; i++) {
    const part = data.slice(i * CHUNK, (i + 1) * CHUNK);
    if ((await dir.getChunkHash(name, i)) === keccak256(part)) continue; // unchanged (if impl hashes this way)
    await (await dir.writeChunk(name, i, part)).wait();
  }
  await (await dir.truncate(name, Math.ceil(data.length / CHUNK))).wait(); // drop leftovers
}
```

## 需要注意

- 状态为 **Stagnant**：该提案没有朝最终定稿推进。
- 规范没有定义分块如何存储或哈希；上面的哈希比较假设使用原始分块的 keccak256，应对照具体实现进行检查。
- 链上存储很昂贵；实现可以使用其他存储后端，但接口并未说明是哪一种。
- 除了"拥有写权限的账户"之外，没有定义任何权限模型。
- URL 方面请参阅 [ERC-4804](/eip-4804/) 及其后继草案 [ERC-6860](/eip-6860/)；[ERC-7617](/eip-7617/) 涵盖了 Web3 URL 的分块响应。


---

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