EIPs Explained

EIP-2015 · Stagnant · Interface · 2019-05-12

EIP-2015: wallet_updateEthereumChain RPC Method

Proposes wallet_updateEthereumChain, one RPC method that asks a wallet to switch to an EVM chain and add it first if the wallet does not know it yet.

What problem does it solve?

A dapp deployed on several EVM chains often needs the user's wallet to be on a specific one. Without a standard, the dapp could only display "please switch to network X" and hope the user found the right settings. If the wallet had never heard of that chain, the user also had to type in an RPC URL, chain ID and currency symbol by hand, which is error-prone and a phishing risk.

EIP-2015 defines a single wallet method that covers both cases: switch to a known chain, or register a new one and switch to it.

How it works

The dapp calls wallet_updateEthereumChain through the wallet's provider with one object:

Field Required Meaning
chainId yes 0x-prefixed EIP-155 chain ID
chainName no Display name
rpcUrls no RPC endpoints for the chain
nativeCurrency no { name, symbol, decimals }, read like ERC-20 metadata
blockExplorerUrl no Block explorer URL

Wallet behaviour:

  • Everything except chainId is only a suggestion; the wallet may ignore or override it.
  • The wallet should ask the user before switching or adding a chain, and should ship its own defaults for well-known chains to resist phishing.
  • The wallet must sanitize each RPC URL, including checking that it answers net_version and eth_chainId correctly.
  • On success, it returns true once the active chain matches the request. If the user declines, it returns error code 4001.

Sample code

Calling it from a dapp through an EIP-1193 provider:

const ethereum = (window as any).ethereum;

try {
  await ethereum.request({
    method: "wallet_updateEthereumChain",
    params: [{
      chainId: "0x2a",                       // example values only
      chainName: "Example Chain",
      rpcUrls: ["https://rpc.example.org"],
      nativeCurrency: { name: "Example Ether", symbol: "EETH", decimals: 18 },
      blockExplorerUrl: "https://explorer.example.org",
    }],
  });
} catch (err: any) {
  if (err.code === 4001) console.log("User rejected the switch");
  else throw err;
}

What most wallets support today instead:

try {
  await ethereum.request({ method: "wallet_switchEthereumChain", params: [{ chainId: "0x2a" }] });
} catch (err: any) {
  if (err.code === 4902) { // chain not added yet (wallet-specific code)
    await ethereum.request({ method: "wallet_addEthereumChain", params: [chainParams] });
  }
}

Things to know

  • Status: Stagnant. Wallets did not adopt wallet_updateEthereumChain.
  • The ecosystem standardized on two separate methods instead: EIP-3085 wallet_addEthereumChain and EIP-3326 wallet_switchEthereumChain. Both are themselves marked Stagnant but are widely implemented.
  • Never trust RPC URLs supplied by a dapp blindly. The spec specifically warns of server-side request forgery and phishing.
  • The 4902 "unrecognized chain" code in the second snippet is a common wallet convention, not part of this EIP.

#wallet #json-rpc #chain-switching #dapp #interface

Details

Authors
Pedro Gomes (@pedrouid), Erik Marks (@rekmarks), Pandapip1 (@Pandapip1)
Status
Stagnant
Type
Standards Track / Interface
Created
2019-05-12
Requires

Machine-readable: Markdown · JSON