EIPs Explained

EIP-7039 · Stagnant · Interface · 2023-05-15

EIP-7039: Scheme-Handler Discovery Option for Wallets

SHADOW: a wallet-discovery idea where a page loads a hidden iframe at web+evm:// and talks EIP-1193 requests over postMessage, letting several wallets coexist without extension permissions.

What problem does it solve?

Browser dApps usually find a wallet through window.ethereum, an object injected by an extension (EIP-1193). That has two problems:

  • Only one wallet can own window.ethereum at a time, so multiple installed wallets fight over it.
  • To inject it, extensions need broad permissions to run scripts on every page.

EIP-7039, nicknamed SHADOW (Scheme-Handler Discovery Option for Wallets), proposes using the browser's custom protocol handlers instead. Any wallet that can serve a web page (an extension, a web wallet, a native app or a hardware wallet companion) registers as the handler for a well-known scheme, and the user picks among them.

How it works

Connecting:

  1. The page listens for "message" events on window.
  2. It creates an <iframe src="web+evm://"> and attaches it to the DOM. The browser loads the registered wallet's handler page inside it.
  3. It waits for a message whose source is that iframe's contentWindow.
  4. The first MessagePort in that message becomes the primary port. The message may carry { name, icon } describing the wallet.

Talking:

  • Either side can send a request on the primary port. The data follows EIP-1193 RequestArguments ({ method, params }).
  • To get an answer, the requester transfers a fresh reply port with the message. The responder posts exactly one { result } or { error } (an EIP-1193 ProviderRpcError) on it.
  • No reply port means "fire and forget", which is not an error.

Handing over a port instead of using the iframe window directly lets the iframe step aside so page and wallet can talk without proxying.

Sample code

function connectShadow(): Promise<MessagePort> {
  return new Promise((resolve) => {
    const iframe = document.createElement("iframe");
    iframe.style.display = "none";
    iframe.src = "web+evm://";

    window.addEventListener("message", function onMsg(ev: MessageEvent) {
      if (!ev.source || ev.source !== iframe.contentWindow) return;
      window.removeEventListener("message", onMsg);
      console.log("wallet:", ev.data?.name);
      resolve(ev.ports[0]); // primary port
    });

    document.body.appendChild(iframe);
  });
}

function request(port: MessagePort, method: string, params?: unknown[]) {
  return new Promise((resolve, reject) => {
    const { port1, port2 } = new MessageChannel();
    port1.onmessage = (ev) => (ev.data.error ? reject(ev.data.error) : resolve(ev.data.result));
    port.postMessage({ method, params }, [port2]); // port2 is the reply port
  });
}

const port = await connectShadow();
const accounts = await request(port, "eth_requestAccounts");

Things to know

  • Status is Stagnant, and the text is incomplete: the icon format, an EIP-1193 compatibility shim and security considerations are marked TODO.
  • Wallet discovery in practice moved to EIP-6963 (Multi Injected Provider Discovery), which solves the "many wallets" problem with window events while still using injected providers.
  • Both sides must verify message origins before trusting anything received.
  • It relies on the browser's custom protocol handler mechanism (registerProtocolHandler for web+ schemes).

#wallet #discovery #provider #browser #interface

Details

Authors
Sam Wilson (@SamWilsn)
Status
Stagnant
Type
Standards Track / Interface
Created
2023-05-15
Requires

Machine-readable: Markdown · JSON