ERC-7618 · Draft · ERC · 2024-02-08
ERC-7618: Content encoding in ERC-5219 mode Web3 URL
A Draft extension to web3:// URLs in ERC-5219 resolve mode: if a contract returns a Content-Encoding header (gzip or brotli) the client cannot handle, the protocol decompresses the data before serving it, so sites can store compressed assets on chain.
What problem does it solve?
web3:// URLs (ERC-6860) let a browser or gateway load a website straight from smart contracts. In the ERC-6944 "resolve mode", a contract implementing ERC-5219's request() returns a status code, a body and HTTP-style headers, much like a web server.
On-chain storage is expensive, so storing HTML, JavaScript and images compressed saves a lot of gas. But simply returning Content-Encoding: gzip is unsafe: the requesting client might not support that algorithm. Full HTTP-style negotiation (the client sends Accept-Encoding, the server picks) does not fit well either, because the contract would need to store several versions of every file.
ERC-7618 solves this by making the web3:// protocol layer responsible for decompressing when needed.
How it works
- The contract stores the resource compressed once, and its
request()returns the compressed bytes plus aContent-Encodingheader (for examplebr). - The
web3://client or gateway compares that encoding with what the end client advertised inAccept-Encoding. - If the end client supports it, the bytes can be passed through compressed.
- If it does not, or no
Accept-Encodingwas given, the protocol must decode the content itself and remove theContent-Encodingheader before forwarding it. - Protocol implementations must support
gzipandbr(brotli). Any other encoding that would need decoding results in an "unsupported content encoding" error.
No interface changes are needed: it reuses the existing ERC-5219 request() return value and the standard HTTP header name.
Sample code
A contract serving a brotli-compressed page in resolve mode (simplified):
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract CompressedSite {
struct KeyValue { string key; string value; }
bytes private indexBr; // brotli-compressed index.html, uploaded by the owner
function resolveMode() external pure returns (bytes32) {
return "5219";
}
function request(string[] memory resource, KeyValue[] memory /* params */)
external view
returns (uint statusCode, string memory body, KeyValue[] memory headers)
{
if (resource.length == 0) {
headers = new KeyValue[](2);
headers[0] = KeyValue("Content-Type", "text/html");
headers[1] = KeyValue("Content-Encoding", "br");
return (200, string(indexBr), headers);
}
return (404, "", new KeyValue[](0));
}
}
A client fetching web3://<contract-address>/ receives plain HTML if it does not accept br.
Things to know
- Status: Draft, as are the related ERC-6944 and ERC-6860-based extensions.
- The proposal changes behaviour of
web3://clients and gateways, not of the contracts; contracts only need to set the header correctly. - It combines naturally with ERC-7617 (chunked responses) for large files.
- Decompression happens off chain in the client, so it adds no gas cost; it only reduces on-chain storage size.