EIPs Explained

EIP-3085 · Stagnant · Interface · 2020-11-01

EIP-3085: wallet_addEthereumChain RPC Method

The wallet_addEthereumChain RPC method that lets a dapp ask a wallet to add an EVM chain (chain ID, RPC URLs, currency, explorer), with the user approving the request.

What problem does it solve?

A dapp deployed on an L2 or sidechain needs the user's wallet to know about that network. Without a standard, users had to copy a chain ID, RPC URL and currency symbol into wallet settings by hand, which is error-prone and easy to phish.

ERC-3085 defines one wallet-namespaced method a site can call to suggest a chain. The wallet validates the data, shows it to the user, and adds the chain only if they approve.

How it works

wallet_addEthereumChain takes one object:

interface AddEthereumChainParameter {
  chainId: string;              // hex string, e.g. "0xa"
  chainName?: string;
  rpcUrls?: string[];
  nativeCurrency?: { name: string; symbol: string; decimals: number };
  blockExplorerUrls?: string[];
  iconUrls?: string[];
}

Key rules from the spec:

  • Only chainId is required by the type, but the wallet must reject the request if rpcUrls is missing or empty, and if chainId does not match what eth_chainId returns from the RPC URLs.
  • URLs must be valid and must not use http: or file:.
  • If nativeCurrency is given, all three fields are required.
  • It returns null on success, an error otherwise. The wallet may reject for any reason.
  • Adding a chain does not mean it becomes selected. Re-adding an existing chain should succeed (unless the user declines), and the same chain ID must not be added twice.

Sample code

Adding a chain through an EIP-1193 provider, then switching to it:

const params = {
  chainId: "0xa", // 10
  chainName: "OP Mainnet",
  nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
  rpcUrls: ["https://mainnet.optimism.io"],
  blockExplorerUrls: ["https://optimistic.etherscan.io"],
};

try {
  await window.ethereum.request({ method: "wallet_addEthereumChain", params: [params] });
  // Adding does not select it; ask to switch explicitly (EIP-3326)
  await window.ethereum.request({
    method: "wallet_switchEthereumChain",
    params: [{ chainId: params.chainId }],
  });
} catch (err: any) {
  if (err.code === 4001) console.log("user rejected");
  else throw err;
}

Things to know

  • Status is Stagnant, though the method is widely supported in practice by browser wallets such as MetaMask. Behaviour details (which fields are required, whether the chain is auto-selected) still vary by wallet.
  • Switching is a separate concern, proposed in EIP-3326 wallet_switchEthereumChain.
  • The chain ID is the EIP-155 replay-protection ID, formatted as in EIP-695. Wallets should sign with the submitted chain ID, never one reported by an RPC endpoint.
  • Security guidance in the spec: warn users that RPC endpoints see their IP and activity, treat icons as untrusted, prefer the wallet's own known-chain metadata, and avoid leaking which chains the user already has by auto-approving duplicates.

#wallet #json-rpc #multichain #interface #dapp

Details

Authors
Erik Marks (@rekmarks), Pedro Gomes (@pedrouid), Pandapip1 (@Pandapip1)
Status
Stagnant
Type
Standards Track / Interface
Created
2020-11-01
Requires

Machine-readable: Markdown · JSON