---
eip: 7039
label: EIP-7039
title: Scheme-Handler Discovery Option for Wallets
status: Stagnant
type: Standards Track
category: Interface
created: 2023-05-15
requires: 1193
official: https://eips.ethereum.org/EIPS/eip-7039
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7039.md
canonical: https://eips.huatuo.xyz/zh/eip-7039/
lang: zh
tldr: "SHADOW：一种钱包发现思路，页面在 web+evm:// 加载隐藏的 iframe，并通过 postMessage 传递 EIP-1193 请求，让多个钱包无需扩展权限即可共存。"
tags: [wallet, discovery, provider, browser, interface]
---

# EIP-7039: Scheme-Handler Discovery Option for Wallets

> Using custom protocol handlers to initiate connections between web pages and wallets.

**TL;DR:** SHADOW：一种钱包发现思路，页面在 web+evm:// 加载隐藏的 iframe，并通过 postMessage 传递 EIP-1193 请求，让多个钱包无需扩展权限即可共存。

## 它解决什么问题？

浏览器中的 dApp 通常通过 `window.ethereum` 发现钱包，这是由扩展注入的对象（[EIP-1193](/eip-1193/)）。这带来两个问题：

- 同一时间只有 **一个** 钱包能占有 `window.ethereum`，所以安装了多个钱包时它们会互相争抢。
- 为了注入它，扩展需要在每个页面上运行脚本的广泛权限。

EIP-7039 昵称为 **SHADOW**（Scheme-Handler Discovery Option for Wallets），提议改用浏览器的自定义协议处理器。任何能够提供网页的钱包（扩展、网页钱包、原生应用或硬件钱包配套应用）都可以注册为某个约定 scheme 的处理器，由用户在其中选择。

## 它如何运作

**连接：**

1. 页面监听 `window` 上的 `"message"` 事件。
2. 创建 `<iframe src="web+evm://">` 并添加到 DOM。浏览器会在其中加载已注册钱包的处理器页面。
3. 等待一条 `source` 为该 iframe 的 `contentWindow` 的消息。
4. 该消息中的第一个 `MessagePort` 成为 **主端口**。消息中可能带有描述钱包的 `{ name, icon }`。

**通信：**

- 任何一方都可以在主端口上发送请求。数据遵循 EIP-1193 的 `RequestArguments`（`{ method, params }`）。
- 要获得答复，请求方随消息转移一个新的 **回复端口**。响应方在其上恰好发送一次 `{ result }` 或 `{ error }`（EIP-1193 的 `ProviderRpcError`）。
- 没有回复端口表示"发出即不管"，这不算错误。

通过移交端口而不是直接使用 iframe 窗口，iframe 可以让开，页面和钱包无需代理即可直接通信。

## 示例代码

```ts
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");
```

## 需要注意

- 状态为 **Stagnant**，文本也不完整：图标格式、EIP-1193 兼容垫片和安全考量都标注为 TODO。
- 实际中的钱包发现已转向 [EIP-6963](/eip-6963/)（Multi Injected Provider Discovery），它用 window 事件解决"多个钱包"的问题，同时仍使用注入式 provider。
- 双方在信任收到的任何内容之前，都必须验证消息的来源（origin）。
- 它依赖浏览器的自定义协议处理器机制（针对 `web+` scheme 的 `registerProtocolHandler`）。


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-7039
