---
eip: 5573
label: ERC-5573
title: Sign-In with Ethereum Capabilities, ReCaps
status: Draft
type: Standards Track
category: ERC
created: 2021-07-20
requires: 4361
official: https://eips.ethereum.org/EIPS/eip-5573
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-5573.md
canonical: https://eips.huatuo.xyz/zh/eip-5573/
lang: zh
tldr: ReCap 扩展了 Sign-In with Ethereum：一条签名消息既能让你登录，又能授予应用有范围限定、人类可读的权限，使其在其他服务上代表你行事，就像建立在 SIWE 之上的 OAuth。
tags: [siwe, authorization, capabilities, wallet, offchain]
---

# ERC-5573: Sign-In with Ethereum Capabilities, ReCaps

> Mechanism on top of Sign-In with Ethereum for informed consent to delegate capabilities with an extensible scope mechanism

**TL;DR:** ReCap 扩展了 Sign-In with Ethereum：一条签名消息既能让你登录，又能授予应用有范围限定、人类可读的权限，使其在其他服务上代表你行事，就像建立在 SIWE 之上的 OAuth。

## 它解决什么问题？

[ERC-4361](/eip-4361/)（Sign-In with Ethereum，SIWE）向网站证明你控制某个地址。这是认证。但应用往往还需要*授权*：代表你在第三方服务（例如存储提供商）读取或写入你数据的权限。

没有标准的话，每个服务都会发明自己的委托格式，钱包向用户展示的也是看不懂的数据块。ReCap 为 SIWE 消息加入了基于能力（capability）的授权，这样用户能以明文看到自己究竟授予了什么，服务也能确定性地验证它。规范把这一组合比作 OpenID Connect（SIWE）加 OAuth2（ReCap）。

## 它如何运作

**ReCap** 是一条普通的 SIWE 消息，外加两处改动：

1. `Resources` 中的**最后一项**是一个 ReCap URI：`urn:recap:` 后面跟着 JSON **Details Object** 的无填充 base64url 编码。
2. `statement` 以一句自动生成的、人类可读的句子结尾，用于描述这些权限。

Details Object：

- `att`：资源 URI → 能力字符串（`namespace/ability`，例如 `crud/update`）→ 限制对象数组 的映射。`[{}]` 表示"允许，无限制"；空数组表示不可用。键必须按字典序排序。
- `prf`（可选）：父级能力的 CID，用于签名者并非资源的直接控制者的情况。

SIWE 的 `URI` 字段指明接收这些权利的依赖方（relying party）；`Issued At`、`Expiration Time` 和 `Not Before` 限定它们的有效时间。

**转换算法**会把 `att` 转成以 `I further authorize the stated URI to perform the following actions on my behalf:` 开头的文本，后面按资源和命名空间列出编号条目。

**验证：**检查 SIWE 签名，把最后一个资源解码为 ReCap，重新生成 statement，并确认已签名的 `statement` 以它结尾。这保证了用户看到的权限与实际执行的权限一致。

## 示例代码

用 TypeScript 构建 ReCap URI 及对应的 statement：

```ts
type Att = Record<string, Record<string, object[]>>;

function sortKeys<T extends Record<string, any>>(o: T): T {
  return Object.fromEntries(Object.keys(o).sort().map(k => [k, o[k]])) as T;
}

export function buildRecap(att: Att) {
  const sorted: Att = sortKeys(Object.fromEntries(
    Object.entries(att).map(([res, abil]) => [res, sortKeys(abil)]),
  ));
  const json = JSON.stringify({ att: sorted });
  const uri = "urn:recap:" + Buffer.from(json).toString("base64url"); // unpadded

  let n = 1;
  let text = "I further authorize the stated URI to perform the following actions on my behalf:";
  for (const [resource, abilities] of Object.entries(sorted)) {
    const byNs: Record<string, string[]> = {};
    for (const key of Object.keys(abilities)) {
      const [ns, name] = key.split("/");
      (byNs[ns] ??= []).push(name);
    }
    for (const [ns, names] of Object.entries(byNs)) {
      text += ` (${n++}) '${ns}': ${names.map(x => `'${x}'`).join(", ")} for '${resource}'.`;
    }
  }
  return { uri, statement: text };
}

// buildRecap({ "https://example.com/pictures/": { "crud/update": [{}], "crud/delete": [{}] } })
// statement: "... (1) 'crud': 'delete', 'update' for 'https://example.com/pictures/'."
```

把 `uri` 作为 `Resources` 的最后一项，并把 `statement` 追加到已有的 SIWE statement 之后。

## 需要注意

- **状态：Draft。** 若干章节（实现者指南、设计理由）仍标记为 TBD。
- ReCap 不是不记名令牌（bearer token）。资源服务仍须另行认证依赖方；具体如何做不在范围之内。
- 每种能力和限制的含义由各资源服务定义，而不是由本标准定义。
- 排序规则很重要：`att` 的顺序不同，会产生不同的 URI 和 statement，验证就会失败。


---

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