EIPs Explained

EIP-1193 · Final · Interface · 2018-06-30

EIP-1193: Ethereum Provider JavaScript API

The standard JavaScript interface that browser wallets expose to web apps: one `request()` method plus a handful of events, so any dapp can talk to any wallet the same way.

What problem does it solve?

A web app (dapp) cannot sign transactions itself; it asks the user's wallet. Wallets do this by putting a JavaScript object, "the Provider", into the page. Early on every wallet shaped that object differently (send, sendAsync, enable, callbacks vs. promises), so dapps had to special-case each wallet.

EIP-1193 defines one minimal, promise-based, event-driven Provider API. Libraries such as ethers, web3.js and viem accept any EIP-1193 provider, and wallets such as MetaMask implement it.

How it works

One method:

interface RequestArguments {
  readonly method: string;
  readonly params?: readonly unknown[] | object;
}
provider.request(args: RequestArguments): Promise<unknown>;

The promise resolves with the RPC method's result or rejects with a ProviderRpcError ({ code, message, data? }). Standard error codes:

Code Meaning
4001 User rejected the request
4100 Method or account not authorised
4200 Method not supported
4900 Disconnected from all chains
4901 Not connected to the requested chain

Five events via Node-style on / removeListener:

  • connect with { chainId } (hex string, as returned by eth_chainId, EIP-695)
  • disconnect with a ProviderRpcError
  • chainChanged with the new hex chainId
  • accountsChanged with the new eth_accounts array
  • message for anything else, e.g. eth_subscription notifications

The spec is transport-agnostic: it says nothing about JSON-RPC over HTTP, and putting the provider at window.ethereum is a convention, not part of the standard.

Sample code

Raw provider usage in the browser:

const eth = (window as any).ethereum; // an EIP-1193 provider

try {
  const accounts = await eth.request({ method: "eth_requestAccounts" });
  const chainId = await eth.request({ method: "eth_chainId" });
  console.log(accounts[0], parseInt(chainId, 16));
} catch (err: any) {
  if (err.code === 4001) console.log("user said no");
  else throw err;
}

eth.on("accountsChanged", (accounts: string[]) => console.log("now", accounts[0]));
eth.on("chainChanged", () => window.location.reload());

Wrapping it with ethers v6:

import { BrowserProvider, parseEther } from "ethers";

const provider = new BrowserProvider(window.ethereum); // accepts any EIP-1193 provider
const signer = await provider.getSigner();
await signer.sendTransaction({ to: recipient, value: parseEther("0.01") });

Things to know

  • Accounts should not be exposed by default. Dapps request access explicitly with eth_requestAccounts (EIP-1102) or wallet_requestPermissions (EIP-2255).
  • Treat the provider object as untrusted: any script on the page can read or overwrite it. Wallets must validate everything they receive.
  • When several wallet extensions are installed they fight over window.ethereum. EIP-6963 adds an event-based discovery mechanism so a dapp can list every injected provider.
  • Older wallets may still expose the legacy send / sendAsync / enable methods; the spec documents them in an appendix but new code should only use request.

#wallet #provider #javascript #dapp #interface

Details

Authors
Fabian Vogelsteller (@frozeman), Ryan Ghods (@ryanio), Victor Maia (@MaiaVictor), Marc Garreau (@wolovim), Erik Marks (@rekmarks)
Status
Final
Type
Standards Track / Interface
Created
2018-06-30
Requires
Required by

Machine-readable: Markdown · JSON