Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

ERC-7303 proposes an access control mechanism based on token ownership.

This article explains the proposal with additional context and examples.

ERC-7303 was in the Draft stage as of October 12, 2023.

Overview

Ethereum Requests for Comments (ERCs) include standards for tokens used on Ethereum. This particular ERC introduces an access control mechanism called Token-Controlled Token Circulation (TCTC).

The idea behind TCTC is to represent particular roles or permissions using ERC-721 or ERC-1155 tokens. These are called control tokens. They simplify granting and revoking roles through operations such as minting and burning the corresponding control tokens.

For more on ERC-721, see the following article.

https://chaldene.net/erc721

For more on ERC-1155, see the following article.

https://chaldene.net/erc1155

Suppose a website grants access to a particular user by minting a special ERC-721 token to that user. As long as the user owns the token, they can access the website, assuming the website checks token ownership. To revoke access, an authorized party can burn that token if the token contract permits it.

This approach allows access control to be managed on-chain and changes to permissions to be recorded as transactions. It provides a transparent way to manage permissions using tokens.

Motivation

One common way to manage privileges is role-based access control (RBAC). It determines permissions according to whether a particular user or account holds a particular role. The following points explain how this model works and where it can be improved.

Role-based access control

Role-based access control assigns different privileges to different roles, such as administrators, moderators, and ordinary users. Each role carries permission to perform specific operations, and access decisions are made accordingly.

ERC-5982

ERC-5982 describes a standard interface for role-based access control in Ethereum smart contracts.

For more on ERC-5982, see the following article.

[ERC-5982] Understanding Role-Based Access Control

Off-chain administration tools

Implementing this approach typically requires tools outside the blockchain to grant or revoke roles for users and accounts. These external tools provide an interface for configuring permissions to perform privileged operations.

Wallet limitations

Many wallets do not provide an appropriate user interface for displaying role-based privileges. Users may therefore find it difficult to understand which privileges have been assigned to them.

Although role-based access control provides a useful permission model, administration tools and wallet interfaces can be improved. In particular, users need interfaces that make it easy to understand and manage their privileges.

Use cases

ERC-7303 is particularly useful in scenarios requiring role-based access control of the kind described by ERC-5982. The following examples illustrate how control tokens can be applied.

Minting and burning privileges

When an application represents tickets, coupons, membership cards, or website access rights as tokens, system administrators need permission to mint or burn those tokens.

Under this proposal, those permissions can themselves be represented by control tokens. Holding the appropriate special token enables an administrator to mint or burn application tokens, provided the application enforces the corresponding ownership check.

Transfer privileges

Some applications may need to restrict the ability to transfer tokens to designated institutions. In this case, control tokens can be minted to those institutions.

Ownership of a control token can be used to govern and restrict transfers of the application tokens.

Address verification

When minting or transferring tokens, an application may need to verify that a recipient address is eligible to receive them.

Under ERC-7303, a control token can be issued as evidence that an address has been verified. The recipient must hold that control token when application tokens are minted or transferred to the recipient address. This can help prevent transfers to unintended or unverified addresses; it does not, by itself, guarantee that an intended recipient address was entered correctly.

In some cases, a government agency or company might issue the address-verification control token after completing an identity verification process.

The proposal offers a flexible approach to meeting different access control requirements. Applications can select control tokens appropriate to their particular privilege management needs.

Specification

Smart contracts implementing ERC-7303 represent the privileges required for roles using ERC-721 or ERC-1155 tokens. The proposal calls these tokens control tokens. Its main provisions are described below.

Using control tokens

ERC-721 or ERC-1155 tokens can represent the privileges associated with a role. Holding the relevant control token indicates possession of the corresponding privilege.

Types of control tokens

The broader concept of a control token can accommodate different token types, including ERC-20, ERC-721, and ERC-1155. The NFT-based approach and reference implementation discussed in this article focus on ERC-721 and ERC-1155.

Recursive control

Token transactions can themselves be controlled recursively by other control tokens. In other words, one control token can govern operations involving another control token.

Associating control tokens with roles

To associate a control token with a particular role, use the address of its previously deployed token contract.

Checking an account's role

To check whether an account holds a particular role, determine whether its balance of the corresponding control token is greater than zero. Use the balanceOf method defined by ERC-721 or ERC-1155. For ERC-1155, also specify the token's typeId.

Granting roles

To grant a role to an account, mint (mint) the control token representing the privilege to that account. The proposal refers to the safeMint method defined by ERC-5679 for this purpose.

Revoking roles

To revoke a role from an account, burn (burn) the control token representing the privilege. The proposal refers to the burn method defined by ERC-5679. If several tokens confer the same role, the account must no longer satisfy the ownership condition for the role to be fully revoked.

Representing roles

Within a smart contract, roles are represented as bytes32 values. The keccak256 hash of a role name can serve as the identifier. For example, bytes32 role = keccak256("MINTER").

ERC-7303 provides a framework for managing roles and privileges by representing them as tokens. Token-based access checks can support security and functionality in smart contracts when the contracts correctly enforce the intended permission model.

Additional considerations

Using ERC-721 or ERC-1155 tokens to represent privileges can make those privileges visible in wallets and easier for users to understand and manage.

Soulbound token specifications, such as ERC-5192, are often considered when privileges are represented by tokens. ERC-5192 extends ERC-721, which is one reason ERC-721 is used as a control token type in this proposal.

Transferable control tokens can support scenarios requiring role delegation. For example, privileges can be passed from one member of an organization to another. Whether a control token should be transferable depends on the application's specific requirements.

Representing privileges using ERC-721 or ERC-1155 control tokens can make them easier to inspect in compatible wallets. Transferable control tokens also provide flexibility when delegating privileges. The appropriate token type and transfer policy depend on the application's requirements.

Backward compatibility

The proposal is designed to work with ERC-721, ERC-1155, and ERC-5679.

For more on ERC-5679, see the following article.

[EIP-5679] Standardizing Minting and Burning Across ERC-20, ERC-721, and ERC-1155

Reference implementation

The ERC-7303 reference implementation provides tools for implementing TCTC-based access control within applications. It includes two main capabilities.

Modifier

The reference implementation provides a modifier for enforcing access control. Before a particular action or function is executed, the modifier checks whether the specified account holds the role required to perform that action. If it does not, execution is rejected.

Role-granting functions

The implementation also includes functions for associating a role with control token requirements. Accounts holding a qualifying token thereby obtain the corresponding role. This determines which accounts can perform privileged actions.

ERC-7303 simplifies checking whether accounts hold the required privileges and configuring the conditions under which those privileges are granted. These mechanisms can make application access management more efficient and help enforce the application's security policy.

javascript
// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

abstract contract ERC7303 {
    struct ERC721Token {
        address contractId;
    }

    struct ERC1155Token {
        address contractId;
        uint256 typeId;
    }

    mapping (bytes32 => ERC721Token[]) private _ERC721_Contracts;
    mapping (bytes32 => ERC1155Token[]) private _ERC1155_Contracts;

    modifier onlyHasToken(bytes32 role, address account) {
        require(_checkHasToken(role, account), "ERC7303: not has a required token");
        _;
    }

    /**
     * @notice Grant a role to user who owns a control token specified by the ERC-721 contractId. 
     * Multiple calls are allowed, in this case the user must own at least one of the specified token.
     * @param role byte32 The role which you want to grant.
     * @param contractId address The address of contractId of which token the user required to own.
     */
    function _grantRoleByERC721(bytes32 role, address contractId) internal {
        require(
            IERC165(contractId).supportsInterface(type(IERC721).interfaceId),
            "ERC7303: provided contract does not support ERC721 interface"
        );
        _ERC721_Contracts[role].push(ERC721Token(contractId));
    }

    /**
     * @notice Grant a role to user who owns a control token specified by the ERC-1155 contractId. 
     * Multiple calls are allowed, in this case the user must own at least one of the specified token.
     * @param role byte32 The role which you want to grant.
     * @param contractId address The address of contractId of which token the user required to own.
     * @param typeId uint256 The token type id that the user required to own.
     */
    function _grantRoleByERC1155(bytes32 role, address contractId, uint256 typeId) internal {
        require(
            IERC165(contractId).supportsInterface(type(IERC1155).interfaceId),
            "ERC7303: provided contract does not support ERC1155 interface"
        );
        _ERC1155_Contracts[role].push(ERC1155Token(contractId, typeId));
    }

    function _checkHasToken(bytes32 role, address account) internal view returns (bool) {
        ERC721Token[] memory ERC721Tokens = _ERC721_Contracts[role];
        for (uint i = 0; i < ERC721Tokens.length; i++) {
            if (IERC721(ERC721Tokens[i].contractId).balanceOf(account) > 0) return true;
        }

        ERC1155Token[] memory ERC1155Tokens = _ERC1155_Contracts[role];
        for (uint i = 0; i < ERC1155Tokens.length; i++) {
            if (IERC1155(ERC1155Tokens[i].contractId).balanceOf(account, ERC1155Tokens[i].typeId) > 0) return true;
        }

        return false;
    }
}

The following is a simple example of using ERC-7303 in an ERC-721 token contract to define minter and burner roles. Accounts with these roles can create new tokens or destroy existing ones according to the ERC-721 or ERC-1155 control token requirements configured for each role.

javascript
// SPDX-License-Identifier: Apache-2.0

pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "./ERC7303.sol";

contract MyToken is ERC721, ERC7303 {
    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
    bytes32 public constant BURNER_ROLE = keccak256("BURNER_ROLE");

    constructor() ERC721("MyToken", "MTK") {
        // Specifies the deployed contractId of ERC721 control token.
        _grantRoleByERC721(MINTER_ROLE, 0x...);
        _grantRoleByERC721(BURNER_ROLE, 0x...);

        // Specifies the deployed contractId and typeId of ERC1155 control token.
        _grantRoleByERC1155(MINTER_ROLE, 0x..., ...);
        _grantRoleByERC1155(BURNER_ROLE, 0x..., ...);
    }

    function safeMint(address to, uint256 tokenId, string memory uri)
        public onlyHasToken(MINTER_ROLE, msg.sender)
    {
        _safeMint(to, tokenId);
    }

    function burn(uint256 tokenId) 
        public onlyHasToken(BURNER_ROLE, msg.sender) 
    {
        _burn(tokenId);
    }
}

_ERC721_Contracts

javascript
mapping (bytes32 => ERC721Token[]) private _ERC721_Contracts;

Overview

A mapping that stores arrays of ERC-721 token contract information.

Details

It manages ERC-721 contract information associated with a particular role identified by a bytes32 value. Each ERC-721 token contract has its own address (contractId). The mapping supports storing multiple ERC-721 contract references for a single role.

Parameters

  • role
    • Role identifier.
  • contractId
    • Address of the ERC-721 token contract.

_ERC1155_Contracts

javascript
mapping (bytes32 => ERC1155Token[]) private _ERC1155_Contracts;

Overview

A mapping that stores arrays of ERC-1155 token contract information.

Details

It manages ERC-1155 contract information associated with a particular role identified by a bytes32 value. Each entry associates an ERC-1155 contract address (address) with a token type ID (uint256). The mapping supports storing multiple ERC-1155 token references for a single role.

Parameters

  • role
    • Role identifier.
  • contractId
    • Address of the ERC-1155 token contract.
  • typeId
    • ERC-1155 token type ID.

onlyHasToken

javascript
modifier onlyHasToken(bytes32 role, address account) {
    require(_checkHasToken(role, account), "ERC7303: not has a required token");
    _;
}

Overview

A modifier that checks whether a specified account holds a token required for a given role.

Details

Before an action or function is executed, the modifier checks whether the specified account holds a token that qualifies it for the required role. If the ownership condition is not met, execution is rejected. This enforces the access control requirement.

Parameters

  • role
    • Role identifier.
  • account
    • Account address whose token ownership is being checked.

_grantRoleByERC721

javascript
function _grantRoleByERC721(bytes32 role, address contractId) internal {
    require(
        IERC165(contractId).supportsInterface(type(IERC721).interfaceId),
        "ERC7303: provided contract does not support ERC721 interface"
    );
    _ERC721_Contracts[role].push(ERC721Token(contractId));
}

Overview

Associates a role with ownership of control tokens from a specified ERC-721 contract.

Details

The function checks whether the specified contract supports the ERC-721 interface and adds the ERC-721 contract information for the role to the _ERC721_Contracts mapping. The function can be called multiple times, allowing a user to qualify by owning at least one of the specified control tokens. The function configures the role's ownership requirement rather than minting a token to an individual user.

Parameters

  • role
    • Role to associate with the control token.
  • contractId
    • Address of the ERC-721 contract whose tokens qualify the user for the role.

_grantRoleByERC1155

javascript
function _grantRoleByERC1155(bytes32 role, address contractId, uint256 typeId) internal {
    require(
        IERC165(contractId).supportsInterface(type(IERC1155).interfaceId),
        "ERC7303: provided contract does not support ERC1155 interface"
    );
    _ERC1155_Contracts[role].push(ERC1155Token(contractId, typeId));
}

Overview

Associates a role with ownership of control tokens from a specified ERC-1155 contract.

Details

The function checks whether the specified contract supports the ERC-1155 interface and adds the ERC-1155 token information for the role to the _ERC1155_Contracts mapping. The function can be called multiple times, allowing a user to qualify by owning at least one of the specified control tokens.

Parameters

  • role
    • Role to associate with the control token.
  • contractId
    • Address of the ERC-1155 contract whose tokens qualify the user for the role.
  • typeId
    • Token type ID required for the ownership check.

_checkHasToken

javascript
function _checkHasToken(bytes32 role, address account) internal view returns (bool) {
    ERC721Token[] memory ERC721Tokens = _ERC721_Contracts[role];
    for (uint i = 0; i < ERC721Tokens.length; i++) {
        if (IERC721(ERC721Tokens[i].contractId).balanceOf(account) > 0) return true;
    }

    ERC1155Token[] memory ERC1155Tokens = _ERC1155_Contracts[role];
    for (uint i = 0; i < ERC1155Tokens.length; i++) {
        if (IERC1155(ERC1155Tokens[i].contractId).balanceOf(account, ERC1155Tokens[i].typeId) > 0) return true;
    }

    return false;
}

Overview

Checks whether a given account holds a token that qualifies it for the specified role.

Details

The function retrieves the ERC-721 and ERC-1155 control token information associated with the specified role and checks whether the account owns a qualifying token. It returns true when the ownership requirement is met and false otherwise.

Parameters

  • role
    • Role to check.
  • account
    • Account address whose token ownership is being checked.

Return value

  • bool
    • true if the specified account holds a qualifying token; otherwise false.

Security considerations

The proposal indicates that further discussion is needed.

Citation

Ko Fujimura (@kofujimura), "ERC-7303: Token-Controlled Token Circulation [DRAFT]," Ethereum Improvement Proposals, no. 7303, July 2023. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-7303.

Conclusion

ERC-7303 proposes an access control mechanism based on token ownership.