EIPs Explained

ERC-3668 · Final · ERC · 2020-07-19

ERC-3668: CCIP Read—Secure offchain data retrieval

Lets a contract answer a call by reverting with a pointer to an offchain gateway, so clients fetch the data and hand it back to the contract for verification.

What problem does it solve?

Storing data onchain is expensive, so many applications keep data elsewhere (Merkle trees, L2s, plain servers) and store only enough onchain to verify it. Before this ERC, every such project invented its own lookup scheme, and wallets or dapps had to integrate each one separately.

CCIP Read (also called "Durin") defines one generic flow: the contract tells the client where to get the missing data, the client fetches it over HTTPS, and the contract checks the answer. A client that implements the flow once can read from any contract using any storage backend, without knowing the details. ENS uses it to resolve names whose records live offchain or on L2s.

How it works

  1. Call the contract. Instead of returning, the function reverts with:
    error OffchainLookup(address sender, string[] urls, bytes callData, bytes4 callbackFunction, bytes extraData);
    
  2. Query a gateway. The client picks a URL template from urls, substituting {sender} (lowercase hex address) and {data} (hex callData). If the template contains {data} it sends a GET; otherwise it POSTs JSON {"data": ..., "sender": ...}. The gateway replies with JSON {"data": "0x..."}.
  3. Call back. The client calls callbackFunction(response, extraData) on the same contract. The callback verifies the response (signature, Merkle proof, storage proof...) and returns the result with the same return type as the original function. It may also revert with another OffchainLookup, restarting the loop.

Key rules:

  • sender must equal the contract the client called; otherwise the client refuses. This stops a lookup raised deep inside a nested call from being mistaken for the top-level answer. CCIP-aware contracts that call other contracts must catch and re-wrap such errors rather than letting them bubble up.
  • extraData is opaque and must be passed back unchanged. The contract should put anything it needs to verify the answer there, rather than trusting the gateway to echo the query.
  • On a 4xx response the client stops with an error; on 5xx it tries the next URL.
  • Clients must cap the number of chained lookups per call (at least 4 is recommended).
  • The same flow can preflight a transaction: run the lookup during eth_call/eth_estimateGas, then send the callback as the real transaction.

Sample code

Contract side (verification logic omitted):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract OffchainBalances {
    error OffchainLookup(address sender, string[] urls, bytes callData, bytes4 callbackFunction, bytes extraData);

    string[] public urls = ["https://gateway.example/{sender}/{data}.json"];

    function balanceOf(address addr) external view returns (uint256) {
        revert OffchainLookup(
            address(this),
            urls,
            abi.encode(addr),                     // sent to the gateway
            this.balanceOfWithProof.selector,
            abi.encode(addr)                      // kept by the client, returned untouched
        );
    }

    function balanceOfWithProof(bytes calldata response, bytes calldata extraData)
        external view returns (uint256)
    {
        address addr = abi.decode(extraData, (address));
        (uint256 balance, bytes memory proof) = abi.decode(response, (uint256, bytes));
        require(_verify(addr, balance, proof), "bad proof");
        return balance;
    }

    function _verify(address, uint256, bytes memory) internal pure returns (bool) {
        return true; // replace with a signature or Merkle proof check
    }
}

Client side with ethers v6, which implements the lookup loop when asked:

import { Interface, JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const iface = new Interface(["function balanceOf(address) view returns (uint256)"]);
const raw = await provider.call({
  to: contractAddress,
  data: iface.encodeFunctionData("balanceOf", [user]),
  enableCcipRead: true, // follow OffchainLookup reverts
});
const [bal] = iface.decodeFunctionResult("balanceOf", raw);

Things to know

  • Status: Final.
  • The trust model is set by the callback, not by the protocol. A callback that skips verification simply trusts whatever server answers.
  • Gateways see every query, which has privacy implications, and an unavailable gateway makes the function unusable; listing several URLs helps.
  • ENS name resolution through offchain or L2 resolvers relies on this flow together with wildcard resolution (ERC-2544). ERC-5559 (Stagnant) sketched a similar deferral pattern for writes.

#offchain #ccip-read #gateway #ens #l2

Details

Authors
Nick Johnson (@arachnid)
Status
Final
Type
Standards Track / ERC
Created
2020-07-19
Required by

Machine-readable: Markdown · JSON