---
eip: 1102
label: EIP-1102
title: Opt-in account exposure
status: Stagnant
type: Standards Track
category: Interface
created: 2018-05-04
requires: 1474
official: https://eips.ethereum.org/EIPS/eip-1102
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1102.md
canonical: https://eips.huatuo.xyz/zh/eip-1102/
lang: zh
tldr: 通过如今每个 dapp 在连接时都会调用的 eth_requestAccounts RPC 方法，让浏览器钱包在你批准某个网站之前隐藏你的账户。
tags: [wallet, provider, privacy, dapp, interface]
---

# EIP-1102: Opt-in account exposure

> This proposal describes a communication protocol between dapps and Ethereum-enabled DOM environments that allows the Ethereum-enabled DOM environment to choose what information to supply the dapp with and when.

**TL;DR:** 通过如今每个 dapp 在连接时都会调用的 eth_requestAccounts RPC 方法，让浏览器钱包在你批准某个网站之前隐藏你的账户。

## 它解决什么问题？

早期的以太坊浏览器和扩展会向每个页面注入一个 provider（通常是 `web3`），并且用户的账户已经暴露。你访问的任何网站都可以读取你的地址，查询你的余额和历史记录，并且不经询问就弹出交易请求。大多数用户会拒绝意料之外的交易，但页面本来就不该能看到这些账户，甚至不该能对账户发起提示。

EIP-1102 改变了默认行为：在用户明确批准该网站的访问之前，钱包**不暴露任何账户**。

## 它如何运作

- 浏览器或扩展在 `window.ethereum` 上暴露一个 provider。
- provider 新增了一个 RPC 方法 `eth_requestAccounts`。调用它可能会弹出钱包提示，要求用户批准或拒绝该网站。
- 如果用户批准，调用会以至少包含一个账户的数组完成。
- 如果出于任何原因没有可用账户（最常见的原因是用户拒绝），调用会以一条说明性的错误被拒绝。
- dapp 应该在该调用完成之后，才继续使用依赖账户的功能。

提议的启动流程很简单：

```
START dapp
IF provider is defined
    REQUEST account access (eth_requestAccounts)
    IF user approves -> CONTINUE dapp
    IF user rejects  -> STOP (or degrade gracefully)
IF provider is undefined
    STOP dapp
```

该提案的早期版本使用了一个名为 `ethereum.enable()` 的方法。规范本身将其标记为**已弃用**，改用 `eth_requestAccounts`。

## 示例代码

规范中的示例使用较旧的 `ethereum.send(...)` 调用风格。如今的 provider 遵循 [EIP-1193](/eip-1193/)，因此同一方法通过 `request` 调用。使用 ethers v6：

```ts
import { BrowserProvider } from "ethers";

async function connect() {
  if (!window.ethereum) throw new Error("No wallet found");

  try {
    // Opens the wallet's "connect this site?" prompt if needed
    const accounts: string[] = await window.ethereum.request({
      method: "eth_requestAccounts",
    });

    const provider = new BrowserProvider(window.ethereum);
    const signer = await provider.getSigner(accounts[0]);
    console.log("connected as", await signer.getAddress());
    return signer;
  } catch (err) {
    // User rejected, or no accounts available
    console.warn("account access denied", err);
  }
}
```

请注意，`eth_accounts`（不带"request"）只会返回该网站已经有权访问的账户，在批准之前是一个空数组。

## 需要注意

- **状态：Stagnant。** 即便如此，它引入的 `eth_requestAccounts` 方法实际上已经通用：浏览器钱包都支持它，dapp 库在你点击"连接钱包"时就会调用它。
- provider 接口本身（`request`，以及 `accountsChanged` 等事件）由 [EIP-1193](/eip-1193/) 单独标准化。更细粒度的权限后来由 [EIP-2255](/eip-2255/) 提出。
- 规范假定 `window.ethereum` 上只有一个 provider。当安装了多个钱包扩展时，它们会争抢这个全局变量；[EIP-6963](/eip-6963/) 定义了基于事件的多 provider 发现机制。
- 批准是按网站进行的，并由钱包记住。不要在没有用户操作的情况下于页面加载时调用 `eth_requestAccounts`；请通过按钮触发，让用户明白为什么会出现提示。


---

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