EIPs Explained

ERC-6617 · Review · ERC · 2023-02-27

ERC-6617: Bit Based Permission

A minimal access-control standard where each permission is one bit of a uint256, so roles are just bitmasks and permission checks cost a single AND.

What problem does it solve?

Most contracts do access control with a single owner (ERC-173) or with named bytes32 roles (ERC-5982). A single owner is too coarse, and role mappings need a storage read per role you want to check.

ERC-6617 packs permissions into bits. A user's whole permission set is one uint256, so checking "does this user have permissions A and B?" is a bitwise AND against one stored value.

How it works

  • Each permission is a power of two (exactly one bit set). Every permission is unique, and 0 means "no permission". That gives up to 256 permissions.
  • A role is any OR-combination of permissions, so there are up to 2^256 possible roles, and they don't need to be predefined.
  • Bit position can encode importance: put the most important permission in the most significant bit, and permissions can be compared as integers.
  • Required interface IEIP6617:
    • hasPermission(user, required) returns true if the user's bits are a superset of required.
    • grantPermission(user, perm) and revokePermission(user, perm) return bool.
    • Events PermissionGranted and PermissionRevoked must be emitted.
  • Optional (recommended) IEIP6617Meta attaches a name and description to base permissions and main combinations, since bits alone are not self-describing.

Who may grant or revoke is left to the implementation.

Sample code

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

contract BitPermissions {
    uint256 public constant MINT   = 1 << 0;
    uint256 public constant PAUSE  = 1 << 1;
    uint256 public constant ADMIN  = 1 << 255;      // most important = highest bit
    uint256 public constant MINTER_PAUSER = MINT | PAUSE; // a role

    mapping(address => uint256) private _perm;

    event PermissionGranted(address indexed _grantor, uint256 indexed _permission, address indexed _user);
    event PermissionRevoked(address indexed _revoker, uint256 indexed _permission, address indexed _user);

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

    modifier only(uint256 required) {
        require(hasPermission(msg.sender, required), "forbidden");
        _;
    }

    function hasPermission(address user, uint256 required) public view returns (bool) {
        return _perm[user] & required == required;
    }

    function grantPermission(address user, uint256 p) external only(ADMIN) returns (bool) {
        _perm[user] |= p;
        emit PermissionGranted(msg.sender, p, user);
        return true;
    }

    function revokePermission(address user, uint256 p) external only(ADMIN) returns (bool) {
        _perm[user] &= ~p;
        emit PermissionRevoked(msg.sender, p, user);
        return true;
    }
}

Things to know

  • Status is Review; details may still change before it is finalized.
  • 256 permissions per contract is a hard ceiling, and bit meanings are fixed in code. Changing what bit 3 means later is effectively a migration.
  • Bitmasks are opaque to humans and block explorers. The metadata extension helps, but tooling support is far thinner than for OpenZeppelin-style AccessControl roles.
  • Watch out for hasPermission(user, 0): with the superset rule it is always true, so never use an unset constant as a guard.

#access-control #permissions #roles #solidity

Details

Authors
Chiro (@chiro-hiro), Victor Dusart (@vdusart)
Status
Review
Type
Standards Track / ERC
Created
2023-02-27
Required by

Machine-readable: Markdown · JSON