---
eip: 5982
label: ERC-5982
title: Role-based Access Control
status: Review
type: Standards Track
category: ERC
created: 2022-11-15
requires: 165, 5750
official: https://eips.ethereum.org/EIPS/eip-5982
source: https://github.com/ethereum/ERCs/blob/master/ERCS/erc-5982.md
canonical: https://eips.huatuo.xyz/zh/eip-5982/
lang: zh
tldr: 基于角色的访问控制的标准接口：bytes32 角色被授予地址，并在执行特权函数之前进行检查。
tags: [access-control, roles, security, interface]
---

# ERC-5982: Role-based Access Control

> An interface for role-based access control for smart contracts.

**TL;DR:** 基于角色的访问控制的标准接口：bytes32 角色被授予地址，并在执行特权函数之前进行检查。

## 它解决什么问题？

许多合约把敏感函数（mint、pause、upgrade）放在单一的 `owner` 之后。这很简单，但很脆弱：一把密钥什么都能做，你也无法只授权某个机器人 mint，而不同时授权它 upgrade。

基于角色的访问控制（RBAC）把地址分配给具名角色，每个角色只能做特定的事，遵循最小权限原则。这种模式已经很常见（OpenZeppelin 的 `AccessControl`），但此前没有 ERC 来描述它，使工具能够跨合约检测和管理角色。ERC-5982 提出了这个接口，并有意与 OpenZeppelin 的方法名保持兼容。

## 它如何运作

角色是一个 `bytes32`，推荐使用 `keccak256("<ROLE_NAME>")`，例如 `keccak256("MINTER")`。

规范分为三层：

- **`IERC_ACL_CORE`（必需）：** `hasRole(role, account)`、`grantRole(role, account)`、`revokeRole(role, account)`。
- **`IERC_ACL_GENERAL`（推荐）：** 上述调用的、带额外 `bytes _data` 参数的版本（遵循 [ERC-5750](/eip-5750/)），外加 `createRole`、`destroyRole`、把角色绑定到函数选择器（`bytes4`）的 `setRolePower`，以及 `canGrantRole` / `canRevokeRole` / `canExecute` 检查。对应的事件（`RoleGranted`、`RoleRevoked`、`RoleCreated` 等）记录变更。
- **`IERC_ACL_METADATA`（可选）：** 用于提供人类可读角色信息的 `roleName`、`roleDescription`、`roleURI`。

合约应通过 [ERC-165](/eip-165/) 声明支持。与 OpenZeppelin 不同，这里没有单独的 `renounceRole`；该情形已并入 `revokeRole`。

## 示例代码

一个实现核心接口的最小合约：

```solidity
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.20;

interface IERC_ACL_CORE {
    function hasRole(bytes32 role, address account) external view returns (bool);
    function grantRole(bytes32 role, address account) external;
    function revokeRole(bytes32 role, address account) external;
}

contract RoleGated is IERC_ACL_CORE {
    bytes32 public constant ADMIN  = keccak256("ADMIN");
    bytes32 public constant MINTER = keccak256("MINTER");
    mapping(bytes32 => mapping(address => bool)) private _roles;

    constructor() { _roles[ADMIN][msg.sender] = true; }

    modifier only(bytes32 role) {
        require(_roles[role][msg.sender], "missing role");
        _;
    }

    function hasRole(bytes32 role, address a) external view returns (bool) { return _roles[role][a]; }
    function grantRole(bytes32 role, address a) external only(ADMIN) { _roles[role][a] = true; }
    function revokeRole(bytes32 role, address a) external {
        require(_roles[ADMIN][msg.sender] || a == msg.sender, "not allowed"); // self-revoke allowed
        _roles[role][a] = false;
    }

    function mint(address to, uint256 amount) external only(MINTER) { /* ... */ }
}
```

## 需要注意

- **状态：Review。** 接口仍可能变化。其 Backwards Compatibility 和 Security Considerations 部分仍写着"Needs discussion"。
- 核心接口与 OpenZeppelin 的 `AccessControl` 函数签名一致，因此现有基于 OZ 的合约已经公开了所需的方法。
- 规范中 general 扩展的代码有一些粗糙之处（例如 `bytes32 calldata` 参数和一个 `view` 的 setter），因此请把它当作草图，而不是可以直接复制粘贴的 Solidity。
- 管理角色的人现在成了你的关键密钥。请用多签或时间锁保护管理员角色。


---

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