Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article covers ERC5018, a proposal that enables files and directories to be written and read within smart contracts.

This article translates, summarizes, and supplements the material below. The original code examples are retained, including their Japanese comments.

I have also covered various other EIPs.

Overview

ERC5018 proposes standardizing an API for working with files and directories within contracts. Its goal is to provide an EVM interface similar to that of a conventional filesystem. The standard provides basic functions for reading and writing binary objects of arbitrary size, including images, HTML files, and assets. In particular, when an object cannot fit into a single transaction, it can be read or written in chunks (individual portions of the data).

Rather than requiring an entire file to be processed at once, the standard provides a flexible mechanism for dividing large amounts of data into multiple operations.

Motivation

The proposal allows binary objects handled on EVM-based blockchains, including images, HTML, and scripts, to be reused by other DApps. A unified interface is necessary to share and use content seamlessly across different DApps.

In addition, ERC5018 is designed to work with ERC4804.

For more information about ERC4804, see the following article.

[ERC4804] How HTTP-Style Web3 URLs Can Call Contracts

ERC4804 introduced a mechanism for accessing blockchain resources using HTTP-like URIs (for example, https://example.eth/.well-known/resource ). Combined with ERC4804, even web content such as HTML or SVG that uses relative paths to reference other resources can retain the same structure in local and onchain environments.

Specification

ERC5018 defines a standard interface for managing directories and files on EVM-based blockchains. It supports managing large files in chunks and referencing Web3 resources through relative paths, making filesystem-like operations possible onchain.

This API allows data to be handled flexibly on the EVM using a structure similar to conventional Web2 file management. It is particularly suited to the following use cases.

  • Storage and delivery of onchain HTML, SVG, images, and scripts.
  • Onchain web hosting without relying on IPFS or Arweave.
  • Management of large datasets using chunked storage.
  • Web3 access through relative paths and HTTP-style URIs (in combination with ERC4804).

Directory Structure and File Operations

In ERC5018, filenames use the bytes type and can be stored and retrieved as binary data. The standard defines the following basic file operations.

write

js
function write(bytes memory name, bytes memory data) external payable

An account with write permission writes binary data to the specified filename. If the file already exists, it is overwritten.

read

js
function read(bytes memory name) external view returns (bytes memory data, bool exist)

Returns the binary data of the specified file and a bool indicating whether the file exists.

fallback

js
fallback(bytes calldata prefixedName) external returns (bytes memory data)

Retrieves files through a fallback mechanism for names beginning with /. It supports direct external access and similar use cases.

size

js
function size(bytes memory name) external view returns (uint256 size, uint256 chunks)

Returns the total size of the specified file in bytes and its number of chunks.

remove

js
function remove(bytes memory name) external returns (uint256 numOfChunksRemoved)

Deletes the specified file. It returns the number of chunks removed, or 0 if the file does not exist. Write permission is required.

Chunk-Level Operations

Large files can be treated as chunks (portions of data), which can be read, written, and removed individually.

countChunks

js
function countChunks(bytes memory name) external view returns (uint256 numOfChunks);

Returns the number of chunks in the specified file.

writeChunk

js
function writeChunk(bytes memory name, uint256 chunkId, bytes memory chunkData) external payable;

Writes data to the specified chunk of a file. If chunkId exceeds the current chunk count, the operation fails. Only appending or replacing chunks is supported.

readChunk

js
function readChunk(bytes memory name, uint256 chunkId) external view returns (bytes memory chunkData, bool exist);

Returns the specified chunk's data and whether that chunk exists.

chunkSize

js
function chunkSize(bytes memory name, uint256 chunkId) external view returns (uint256 chunkSize, bool exist);

Returns the size of the chunk and whether it exists.

removeChunk

js
function removeChunk(bytes memory name, uint256 chunkId) external returns (bool exist);

Removes an individual chunk. Returns true on success or false if the chunk does not exist. Write permission is required.

truncate

js
function truncate(bytes memory name, uint256 chunkId) external returns (uint256 numOfChunksRemoved);

Removes every chunk from the specified chunk ID onward. When chunkId = 0, it behaves like remove(), deleting the entire file.

Other Utility Functions

getChunkHash

js
function getChunkHash(bytes memory name, uint256 chunkId) external view returns (bytes32);

Returns the keccak256 hash of the specified chunk's data. This can be used to verify data integrity or detect modifications.

Additional Considerations

Large Data That Cannot Fit Into a Single Transaction

When uploading web content such as HTML, images, or scripts to a blockchain, the file may be too large to send in one transaction. The EVM imposes limits on transaction calldata size.

To address this problem, ERC5018 enables files to be divided into multiple chunks and uploaded separately. This allows the entire file to be sent across several transactions.

Unified Chunked Writing and Reading

Even data written chunk by chunk can ultimately be retrieved as an entire file through the read function. The following example illustrates this behavior.

Example

js
read("hello.txt") => ""         // 初期状態。ファイルは空。
writeChunk("hello.txt", 0, "abc") // チャンク0に"abc"を書き込み
read("hello.txt") => "abc"

writeChunk("hello.txt", 1, "efg") // チャンク1に"efg"を書き込み
read("hello.txt") => "abcefg"

writeChunk("hello.txt", 0, "aaa") // チャンク0の内容を"aaa"に上書き
read("hello.txt") => "aaaefg"

writeChunk("hello.txt", 3, "hij") // チャンク2が存在しないため失敗(appendでもreplaceでもない)
→ 書き込み失敗

Partial appends and overwrites are possible through writeChunk, while read returns the complete concatenated contents. The mechanism maintains data integrity and continuity.

How write Works and Its Relationship to writeChunk

The write function writes an entire file in one operation. Internally, it can be understood as two operations.

  • Use writeChunk to write the data passed to write into chunk ID 0.
  • Use truncate to remove chunks from ID 1 onward.

This implementation replaces the contents of an existing file with new data. Unlike overwriting individual chunks, it supports overwriting the entire file.

Benefits of Supporting Large Web Objects

The specification makes it practical to manage large onchain datasets such as the following.

  • HTML pages (accessible through Web3 URIs using ERC4804).
  • Images such as SVG, PNG, and JPG.
  • Collections of assets for web applications consisting of multiple files.

In particular, the read function can retrieve data uploaded in chunks all at once in a single transaction, preserving the user experience when accessing resources through the HTTP-like URIs proposed by ERC4804.

Citation

Qi Zhou (@qizhou), "ERC-5018: Filesystem-like Interface for Contracts [DRAFT]," Ethereum Improvement Proposals, no. 5018, April 2022. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-5018.

Closing Remarks

This article covered ERC5018, a proposal for writing and reading files and directories within smart contracts. I hope it was helpful.

If you have any questions, feel free to reach out via Twitter DM.

Twitter @cardene777

I also publish on other platforms, which you can explore below.

https://chaldene.net/