ERC-5219 · Final · ERC · 2022-07-10
ERC-5219: Contract Resource Requests
Lets a smart contract answer HTTP-like GET requests through a single view function, so a dApp's front end can be served directly from the chain.
What problem does it solve?
Most "decentralized" applications are reached through a centralized website. The contracts may be unstoppable, but the front end can be censored, taken down, silently changed or simply disappear, and users must trust whoever hosts it.
ERC-5219 gives contracts a standard way to serve resources themselves. A client (a browser extension, gateway or wallet) asks the contract for a path, and the contract returns a status code, headers and a body, much like an HTTP server. The interface can then be as permanent and censorship-resistant as the contract.
How it works
A compliant contract implements one read-only function:
struct KeyValue { string key; string value; }
interface IDecentralizedApp {
function request(string[] memory resource, KeyValue[] memory params)
external view
returns (uint16 statusCode, string memory body, KeyValue[] memory headers);
}
resourceis the path split into segments:/asdf/1234becomes["asdf", "1234"].paramsare the query parameters:?foo=barbecomes[{key: "foo", value: "bar"}].- The response mirrors HTTP: a status code such as
200,404or307, headers such asContent-Type, and the body.
Design choices:
- Only
GET-like semantics. The function isview, so requests are freeeth_calls with no waiting for blocks. State changes go through ordinary transactions, possibly signalled with a307redirect. - Storing large assets on-chain is expensive, so the spec recommends
message/external-bodyresponses that point to data elsewhere (for example IPFS). - It is recommended to keep the front-end contract separate from the application logic contract.
- Name resolution is left to other standards. Clients may also support ordinary DNS.
Sample code
A tiny on-chain site:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
struct KeyValue { string key; string value; }
contract HelloSite {
function request(string[] memory resource, KeyValue[] memory)
external pure
returns (uint16 statusCode, string memory body, KeyValue[] memory headers)
{
headers = new KeyValue[](1);
headers[0] = KeyValue("Content-Type", "text/html");
if (resource.length == 0) {
return (200, "<h1>Hello from a contract</h1>", headers);
}
return (404, "<h1>Not found</h1>", headers);
}
}
Calling it from a client (ethers v6):
const site = new Contract(address, [
"function request(string[] resource, tuple(string key, string value)[] params) view returns (uint16, string, tuple(string key, string value)[])",
], provider);
const [status, body, headers] = await site.request([], []);
console.log(status, headers, body);
Things to know
- Status is Final.
- The usual URL caveats apply: following
3xxredirects can leak privacy or send users off-chain. viewcalls still hit node limits on gas and response size, so large pages need chunking or off-chain storage.- Web3 URL standards build on this: ERC-4804 and its Draft successor ERC-6860 map
web3://URLs to contract calls, and ERC-6944 (Draft) adds aresolveMode()returning"5219"so those URLs are routed throughrequest().