---
eip: 2255
label: EIP-2255
title: Wallet Permissions System
status: Final
type: Standards Track
category: Interface
created: 2019-08-22
requires: 1193
official: https://eips.ethereum.org/EIPS/eip-2255
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2255.md
canonical: https://eips.huatuo.xyz/zh/eip-2255/
lang: zh
tldr: 为钱包提供者 API 增加 wallet_requestPermissions 和 wallet_getPermissions，让 dapp 一次性申请具名权限（例如查看你的账户），而不是每个操作都弹出一次提示。
tags: [wallet, permissions, rpc, provider, interface]
---

# EIP-2255: Wallet Permissions System

> An interface to restrict access to sensitive methods

**TL;DR:** 为钱包提供者 API 增加 wallet_requestPermissions 和 wallet_getPermissions，让 dapp 一次性申请具名权限（例如查看你的账户），而不是每个操作都弹出一次提示。

## 它解决什么问题？

钱包位于不可信的网站和用户的密钥之间。安全的默认做法是凡事都弹窗确认，但这会让新用户上手变成一连串弹窗：暴露你的地址、切换网络、签署挑战、授权代币、发送交易。

网页用户从 OAuth（“使用……登录”）中已经熟悉了更好的模式：一个页面列出网站想要什么，用户可以批准、拒绝或缩小范围。EIP-2255 通过一个小而通用的权限 API，把这种模式带到钱包中。

## 它如何运作

在 [EIP-1193](/eip-1193/) 提供者上增加两个方法：

**`wallet_requestPermissions`** 接受一个把方法名映射到 caveat 对象的对象：

```ts
interface PermissionRequest {
  [methodName: string]: { [caveatName: string]: any };
}
```

钱包会显示提示。如果用户批准，它返回一个 `{ parentCapability, date? }` 数组。如果用户拒绝，它会抛出错误码为 `4001` 的错误（按 EIP-1193，表示用户拒绝）。

**`wallet_getPermissions`** 不接受参数，返回当前网站持有的权限（默认为空）：

```ts
interface Permission {
  invoker: string;           // origin of the dapp, e.g. "https://app.example"
  parentCapability: string;  // the permitted method, e.g. "eth_accounts"
  caveats: { type: string; value: any }[]; // restrictions on that permission
}
```

在授予相应权限之前，对受限方法的调用都会失败。**Caveat** 可以缩小权限范围；例如，网站可以只请求支持某种签名方法的账户，或者用户可以添加有效期。

## 示例代码

显式请求账户访问权限，并检查授予了什么（浏览器，EIP-1193 提供者）：

```ts
const provider = (window as any).ethereum;

try {
  await provider.request({
    method: "wallet_requestPermissions",
    params: [{ eth_accounts: {} }],
  });
} catch (err: any) {
  if (err.code === 4001) console.log("User rejected the request");
  else throw err;
}

const permissions = await provider.request({ method: "wallet_getPermissions" });
const hasAccounts = permissions.some((p: any) => p.parentCapability === "eth_accounts");

if (hasAccounts) {
  const accounts = await provider.request({ method: "eth_accounts" });
  console.log(accounts);
}
```

## 需要注意

- 状态为 **Final**。MetaMask 支持这两个方法；其他钱包情况不一，所以请做功能检测，并回退到 `eth_requestAccounts`。
- 该 EIP 定义的是请求和响应的格式，而不是存在哪些权限或 caveat。这些由各钱包自行决定。
- 钱包显示网站图标时，应当小心获取任意 URL（规范对服务端请求伪造发出了警告）。
- [ERC-7715](/eip-7715/)（Draft）在这一思路的基础上，提供了更丰富、范围更明确的权限，例如会话密钥。相关的钱包 RPC 还有用于添加和切换链的 [EIP-3085](/eip-3085/) 和 [EIP-3326](/eip-3326/)。


---

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