---
eip: 1207
label: ERC-1207
title: DAuth Access Delegation Standard
status: Stagnant
type: Standards Track
category: ERC
created: 2018-07-10
official: https://eips.ethereum.org/EIPS/eip-1207
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-1207.md
canonical: https://eips.huatuo.xyz/zh/eip-1207/
lang: zh
tldr: 面向合约的 OAuth 式模式：用户授予另一个合约有时限的权限，使其代表自己调用指定的函数，而无需共享私钥。
tags: [authorization, delegation, access-control, interface]
---

# ERC-1207: DAuth Access Delegation Standard

> DAuth is a standard interface for accessing authorization delegation between smart contracts and users.

**TL;DR:** 面向合约的 OAuth 式模式：用户授予另一个合约有时限的权限，使其代表自己调用指定的函数，而无需共享私钥。

## 它解决什么问题？

大多数合约假定用户用自己的密钥亲自调用每个函数，所以 `msg.sender` 始终是所有者。但有时用户希望另一个合约（"客户端"应用）去操作自己在"资源"合约中的数据或资产，例如代表自己授权代币。此前没有一种通用的方式来表达这种委托。

DAuth 借用了 Web 上 OAuth 的思路：用户授予某个特定合约访问一组特定函数的权限，直到过期时间。由于以太坊的存储是公开的，而且 `msg.sender` 可靠，所以不需要访问令牌；资源合约只需记录谁把什么授予了谁。

## 它如何运作

资源合约针对每一对（用户，客户端合约）存储一个 `AuthInfo`：

```solidity
struct AuthInfo {
    string[] funcNames; // functions the client may call
    uint expireAt;      // unix timestamp
}
mapping(address => mapping(address => AuthInfo)) userAuth;
```

接口：

| 函数 / 事件 | 用途 |
| --- | --- |
| `grant(grantee, invokes, expireAt)` | 用户授权一个客户端；`invokes` 是以空格分隔的函数名列表。触发 `Grant` |
| `regrant(...)` | 可选：修改已有的授权 |
| `revoke(grantee)` | 撤销授权。触发 `Revoke` |
| `verify(authorizer, invoke)` | 内部检查，确认调用者被允许为 `authorizer` 调用 `invoke` |
| `callableFuncNames` / `updateCallableFuncNames` | 资源合约究竟向被委托方开放哪些函数 |

每个可被委托的函数都会被**重载**：普通版本为 `msg.sender` 执行，第二个版本多接收一个 `authorizer` 地址，调用 `verify`，并为该用户执行。

## 示例代码

规范中的模式，应用到 ERC-20 风格的 `approve` 上：

```solidity
function approve(address spender, uint256 value) public returns (bool) {
    return _approve(msg.sender, spender, value);
}

// Called by a client contract that the user has granted "approve" to
function approve(address spender, uint256 value, address authorizer) public returns (bool) {
    verify(authorizer, "approve");
    return _approve(authorizer, spender, value);
}

function _approve(address owner, address spender, uint256 value) internal returns (bool) {
    allowed[owner][spender] = value;
    emit Approval(owner, spender, value);
    return true;
}
```

用户只需授权一次：

```solidity
resource.grant(clientContract, "approve transfer", block.timestamp + 7 days);
```

## 需要注意

- 状态是 **Stagnant**。它没有成为被广泛使用的标准。
- 权限以字符串形式存储和比较，gas 开销很大；现代设计更可能使用函数选择器（`bytes4`）。
- 授权范围很宽：在过期或被撤销之前，客户端可以用任意参数调用指定的函数。请把有效期设得短一些，只授予所需的权限。
- 如今解决"别人代我操作"的常见做法是 [ERC-2612](/eip-2612/) 的 `permit` 这类签名授权，以及 [ERC-2771](/eip-2771/) 这类元交易，而不是每个合约各自的授权表。


---

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