EIPs Explained

ERC-6366 · Review · ERC · 2022-01-19

ERC-6366: Permission Token

Stores each address's permissions as bits in a single uint256 inside one token-like contract, so roles can be checked with a bitmask and transferred or delegated like a balance.

What problem does it solve?

Most protocols have privileged roles: owner, operator, manager, validator. Usually each contract keeps its own access-control list, often with string or keccak256 role names. Across a whole ecosystem of contracts this becomes hard to audit: who can do what is spread over many places.

ERC-6366 proposes a permission token: one contract where each address's permissions are a uint256 "balance", and each bit of that number is one permission. Checking access becomes a cheap bitwise operation, and every contract in the ecosystem can ask the same place.

How it works

Permissions follow ERC-6617 (bit-based permission): it is recommended that each base permission is a power of two, so sets of permissions combine with | and are checked with &.

The core interface IEIP6366Core deliberately looks like ERC-20:

Function / event Purpose
permissionOf(owner) The owner's permission bits
transfer(to, permission) Hand a subset of your permissions to to. Should revert if you lack any of them or if to already has any of them
approve(delegatee, permission) Let delegatee act on your behalf with a subset of your permissions; a new call overwrites the old grant
delegated(owner, delegatee) What owner delegated to delegatee
permissionRequire(permission, required) true if required is a subset of permission
hasPermission(owner, actor, required) true if actor holds required itself or via delegation from owner
Transfer, Approval events Emitted on transfer and approve

Optional parts: human-readable descriptions of permissions (the ERC-6617 metadata extension) and custom errors AccessDenied, DuplicatedPermission and OutOfRange.

Sample code

Defining permissions as bits and guarding a function with the permission token:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IEIP6366Core {
    function permissionOf(address owner) external view returns (uint256);
    function hasPermission(address owner, address actor, uint256 required) external view returns (bool);
}

contract Vault {
    uint256 constant PERM_PAUSE    = 1 << 0;
    uint256 constant PERM_WITHDRAW = 1 << 1;
    uint256 constant PERM_UPGRADE  = 1 << 2;

    IEIP6366Core public immutable perms;
    error AccessDenied(address owner, address actor, uint256 permission);

    constructor(IEIP6366Core p) { perms = p; }

    // `owner` is the account whose permissions the caller is using (itself or a delegator)
    modifier need(address owner, uint256 required) {
        if (!perms.hasPermission(owner, msg.sender, required))
            revert AccessDenied(owner, msg.sender, required);
        _;
    }

    function emergencyWithdraw(address owner) external need(owner, PERM_PAUSE | PERM_WITHDRAW) {
        // ...
    }
}

The subset check itself is a single expression:

function permissionRequire(uint256 permission, uint256 required) pure returns (bool) {
    return permission & required == required;
}

Things to know

  • Status: Review. The spec's Rationale and Security Considerations sections still read "Needs discussion", so expect changes.
  • With 256 bits you get at most 256 distinct base permissions per permission token.
  • Permissions are transferable by whoever holds them. Decide carefully who receives powerful bits, since moving them is as easy as an ERC-20 transfer.
  • approve overwrites the previous delegation rather than adding to it.
  • This is an alternative to role-based libraries such as OpenZeppelin AccessControl, not a drop-in compatible replacement.

#permissions #access-control #roles #bitmask #token

Details

Authors
Chiro (@chiro-hiro), Victor Dusart (@vdusart)
Status
Review
Type
Standards Track / ERC
Created
2022-01-19
Requires

Machine-readable: Markdown · JSON