EIPs Explained

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);
}
  • resource is the path split into segments: /asdf/1234 becomes ["asdf", "1234"].
  • params are the query parameters: ?foo=bar becomes [{key: "foo", value: "bar"}].
  • The response mirrors HTTP: a status code such as 200, 404 or 307, headers such as Content-Type, and the body.

Design choices:

  • Only GET-like semantics. The function is view, so requests are free eth_calls with no waiting for blocks. State changes go through ordinary transactions, possibly signalled with a 307 redirect.
  • Storing large assets on-chain is expensive, so the spec recommends message/external-body responses 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 3xx redirects can leak privacy or send users off-chain.
  • view calls 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 a resolveMode() returning "5219" so those URLs are routed through request().

#web3-url #frontend #decentralization #http

Details

Authors
Gavin John (@Pandapip1)
Status
Final
Type
Standards Track / ERC
Created
2022-07-10
Required by

Machine-readable: Markdown · JSON