EIPs Explained

ERC-6821 · Draft · ERC · 2023-04-02

ERC-6821: Support ENS Name for Web3 URL

Defines how a web3:// URL that uses an ENS name finds the contract to call: first an ENS contentcontract text record, otherwise the name's normal address record.

What problem does it solve?

ERC-4804 defines web3:// URLs that call a smart contract directly, like web3://<contract>/<method>?..., so on-chain apps can be addressed like websites. Typing a raw contract address is unfriendly, so the URL may use a name such as example.eth. But a name needs a precise rule for turning it into the contract address to call, and that rule should let the name point somewhere other than its owner's wallet address.

ERC-6821 is that resolution rule for ENS names.

How it works

Given the contract name and chain ID from a web3:// URL:

  1. Look up the contentcontract text record on the name's ENS resolver (text records come from ERC-634), on the given chain. If that chain has no ENS, or the record is not a valid address, return an error.
  2. If there is no contentcontract record, fall back to the name's ordinary resolved address from ERC-137.
  3. If that address is the zero address, return "address not found".

The contentcontract value may be either:

  • a plain 0x-prefixed hex address, or
  • an ERC-3770 chain-specific address such as arb1:0x.... In that case the chain prefix overrides the chain ID, so a name registered on Ethereum can point to a contract on another chain.

A text record was chosen over contenthash because it is human-readable (a design principle of web3://) and leaves room for extra fields later, such as a time-to-live.

Sample code

Resolving a name the ERC-6821 way (ethers v6):

import { JsonRpcProvider, isAddress, ZeroAddress } from "ethers";

async function resolveWeb3Name(provider: JsonRpcProvider, name: string, chainId: number) {
  const resolver = await provider.getResolver(name);
  if (!resolver) throw new Error("no ENS resolver");

  const record = await resolver.getText("contentcontract");
  if (record) {
    const m = record.match(/^([a-z0-9-]+):(0x[0-9a-fA-F]{40})$/); // ERC-3770 "shortName:address"
    if (m) return { address: m[2], chainShortName: m[1] };    // chain overridden by prefix
    if (!isAddress(record)) throw new Error("invalid contentcontract record");
    return { address: record, chainId };
  }

  const addr = await resolver.getAddress();
  if (!addr || addr === ZeroAddress) throw new Error("address not found");
  return { address: addr, chainId };
}

A URL such as web3://example.eth/balanceOf/0x... would then be sent to the returned address.

Things to know

  • Status is Draft. ERC-6860, a newer (also Draft) version of the web3:// URL translation, likewise points to ERC-6821 for resolving ENS names.
  • ERC-3770 short names (like arb1) must be mapped to chain IDs using a chain registry; that mapping is outside this proposal.
  • Whoever controls the ENS name controls which contract a web3:// link actually calls. Treat a changed contentcontract record like a changed DNS record.

#ens #web3-url #naming #uri

Details

Authors
Qi Zhou (@qizhou), Qiang Zhu (@qzhodl)
Status
Draft
Type
Standards Track / ERC
Created
2023-04-02
Requires

Machine-readable: Markdown · JSON