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.

About ERC-7303

This article examines ERC7303, a proposal for implementing role-based access control using control tokens based on ERC721 and ERC1155.

This article translates, summarizes, and expands on the specification below.

I have also covered a range of other EIPs.

I previously wrote about the same proposal. Reading that article alongside this one may help clarify the mechanism.

[ERC7303] Access Control Based on NFT Ownership

Overview

This Ethereum proposal introduces an access control scheme known as Token-Controlled Token Circulation (TCTC). It represents the privileges associated with a particular role as ERC721 or ERC1155 tokens, known as control tokens, making it easier to grant or revoke that role by issuing (mint) or destroying (burn) the corresponding tokens.

In other words, roles and privileges are represented by digital tokens, and token management determines who holds which privileges. Two token standards are central to this scheme: ERC721 and ERC1155.

  • ERC721
    • Commonly used to represent unique assets, ERC721 is widely known as an NFT (non-fungible token) standard.
    • Each token is unique and is not interchangeable with another token.
    • In this scheme, particular roles or privileges are uniquely identified, and corresponding tokens are issued.
  • ERC1155
    • This multi-token standard can manage several token types within a single smart contract.
    • It therefore supports both fungible (interchangeable) tokens and non-fungible tokens (NFTs).
    • It is suitable when multiple entities hold the same role or privilege.

For more information about ERC721, see the following article.

https://chaldene.net/erc721

For more information about ERC1155, see the following article.

https://chaldene.net/erc1155

Under TCTC, privileges are granted and revoked by issuing and burning tokens, so the corresponding transactions are recorded transparently and irreversibly on the blockchain. This can enable more secure and efficient access control than conventional systems.

TCTC uses digital tokens to manage who has access to what. Tokenizing roles and privileges makes them easier to manage and inspect.

Motivation

Among the many ways to implement access control for privileged operations, this proposal focuses on role-based access control. Defined in ERC5982, this approach grants users specific access privileges according to their roles.

For more information about ERC5982, see the following article.

[ERC5982] Role-Based Access Control

However, this approach has several drawbacks.

Need for offchain administration tools

External administration tools are needed to grant and revoke roles. Managing these operations outside the blockchain requires additional effort and makes it harder to take full advantage of blockchain transparency and security.

Wallet UI limitations

Many wallets, which are applications for managing blockchain addresses, do not provide an interface that clearly displays the privileges assigned to a user. As a result, users cannot easily determine which privileges they hold.

Role-based access control is useful, but administering it requires additional tools, and users may have difficulty inspecting their own privileges. These are areas where the access control implementation can be improved.

Use cases

This ERC can be applied to a range of scenarios involving role-based access control, particularly the scheme described in ERC5982. Examples include the following.

Minting and burning privileges

  • Application
    • Applications that circulate tickets, coupons, membership cards, or website access rights as tokens.
  • Requirement
    • System administrators must have permission to issue (mint) or destroy (burn) these tokens.
  • Implementation
    • The scheme represents these privileges as control tokens.

transfer privileges

  • Application
    • Restricting the ability to transfer tokens to specific institutions.
  • Implementation
    • Issue institutional credentials as control tokens.
    • Ownership of these control tokens determines whether token transfer operations are permitted.

Address verification

  • Application
    • Applications that need to verify addresses to avoid sending tokens to an incorrect recipient during mint or transfer operations.
  • Implementation
    • Issue users control tokens as proof of address verification.
    • Require recipients to hold the corresponding control token when a mint or transfer transaction is executed to help prevent misdelivery.
  • Additional considerations
    • Government agencies or designated companies may issue these address verification control tokens after an identity verification process.

By representing specific privileges as digital tokens, this ERC provides an access control scheme that is more transparent and easier to administer. It can be used for mint and burn permissions, token transfer restrictions, and address verification, among other applications. The aim is to address the limitations of conventional role-based access control through a more efficient and secure approach.

Specification

The standard requires privileges associated with a role to be represented by ERC721 or ERC1155 tokens. These tokens are called control tokens and are used to manage roles and privileges.

Control tokens

  • Definition
    • Tokens representing the privileges required for a role.
  • Types
    • ERC721 or ERC1155 tokens.
  • Properties
    • Their transactions may themselves be controlled recursively by other control tokens.

Associating tokens with roles

Associate the required control tokens with a role using the contract addresses of already deployed control tokens.

Checking privileges

To verify that an account holds the required role, use the balanceOf method defined by ERC721 or ERC1155 to check whether its control token balance exceeds 0. When using an ERC1155 token, the balanceOf method must also specify the typeId.

Granting and revoking roles

  • Granting
    • Use the safeMint method defined in ERC5679 to mint the control token representing the privilege to the account.
  • Revoking
    • Use the burn method defined in ERC5679 to destroy the control token representing the privilege.

For more information about ERC5679, see the following article.

[EIP5679] Standardizing Mint and Burn Operations for ERC20, ERC721, and ERC1155

Representing roles

Roles are represented as bytes32 values. The recommended approach is to calculate the keccak256 hash of the role name string. For example, bytes32 role = keccak256("MINTER");.

ERC7303 defines how smart contracts manage roles and privileges through control tokens. Privileges are granted and revoked through token mint and burn operations, improving the transparency and efficiency of privilege management.

Rationale

This section examines the advantages of using ERC721 or ERC1155 tokens as control tokens to manage privileges. Using these token standards makes privileges more visible in wallets and allows users to manage their own privileges more easily.

Tokenizing privileges

  • Using soulbound tokens
    • When privileges are represented as tokens, specifications for soulbound tokens, such as ERC5192, are commonly used.
    • Soulbound tokens are non-transferable tokens whose ownership does not change.
  • Choosing ERC721
    • The original article describes ERC721 as necessary for control tokens in this context because ERC5192 extends ERC721. The specification section above also permits ERC1155 control tokens.

For more information about ERC5192, see the following article.

[ERC5192] Soulbound Tokens: Making NFTs Non-Transferable

Transferable control tokens

  • Delegating roles
    • Transferable control tokens allow privileges to be passed to other members of an organization when delegation is required, for example, when an authorized person changes roles or takes a vacation.
  • Choosing according to application requirements
    • Whether control tokens should be transferable depends on the specific needs of the application.

Using ERC721 or ERC1155 control tokens allows users to inspect and manage their privileges in a wallet. Applications that require delegation can also support this scenario by choosing transferable control tokens.

Backward compatibility

This ERC is designed to work with ERC721, ERC1155, and ERC5679.

Implementation

ERC7303 provides a modifier that makes it easier to implement TCTC access control in an application. The modifier checks whether an account holds the required role. ERC7303 also includes functions for granting specific roles to accounts.

ts
// 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;
    }
}

ERC721Token

javascript
struct ERC721Token {
    address contractId;
}

Overview

A struct that stores information about an ERC721 token.

Details

The struct holds the address of the contract associated with an ERC721 token. It is primarily used to manage information about ERC721 control tokens associated with a particular role.

Parameters

  • address contractId
    • The contract address of the ERC721 token.

ERC1155Token

javascript
struct ERC1155Token {
    address contractId;
    uint256 typeId;
}

Overview

A struct that stores information about an ERC1155 token.

Details

The struct holds the contract address of an ERC1155 token and its typeId, which identifies the token type. It manages detailed information about ERC1155 control tokens associated with a particular role.

Parameters

  • address contractId
    • The contract address of the ERC1155 token.
  • uint256 typeId
    • The ID identifying the token type.

_ERC721_Contracts

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

Overview

A mapping of arrays that manages the contract addresses of all ERC721 control tokens associated with a particular role.

Details

For each role, identified by a bytes32 key, the mapping holds a list of associated ERC721 tokens (ERC721Token[]). It is used to verify access privileges and manage information about ERC721 tokens associated with specific roles.

Parameters

  • bytes32
    • A unique key identifying the role.
  • ERC721Token[]
    • A list of ERC721 tokens associated with the role.

_ERC1155_Contracts

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

Overview

A mapping of arrays that manages the contract addresses and typeId values of all ERC1155 control tokens associated with a particular role.

Details

For each role, identified by a bytes32 key, the mapping holds a list of detailed information about associated ERC1155 tokens (ERC1155Token[]). It manages information about ERC1155 tokens associated with specific roles and is used to verify access privileges.

Parameters

  • bytes32
    • A unique key identifying the role.
  • ERC1155Token[]
    • A list of detailed information about ERC1155 tokens associated with the role.

onlyHasToken

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

Overview

onlyHasToken is a modifier that checks whether an account has the privileges associated with a particular role.

Details

Before a function executes, the modifier verifies whether the specified account has the privileges for the specified role (whether it holds a control token). It calls _checkHasToken to perform this verification and reverts if the account does not hold a required token.

Parameters

  • bytes32 role
    • The role to check.
  • address account
    • The account address to check.

_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

A function that grants a role to users who own a control token from a specified ERC721 contract.

Details

The function verifies that the specified contract supports the ERC721 interface, then adds the corresponding ERC721 contract address to the internal mapping for the role. Holding at least one token from that specified contract satisfies this configured ownership requirement for the role.

Arguments

  • bytes32 role
    • The role to grant.
  • address contractId
    • The contract address of the ERC721 token the user must own.

_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

A function that grants a role to users who own a control token with a specified typeId from a particular ERC1155 contract.

Details

The function verifies that the specified contract supports the ERC1155 interface, then adds the corresponding ERC1155 contract address and typeId to the internal mapping for the role. Holding at least one ERC1155 token of the specified typeId satisfies this configured ownership requirement for the role.

Arguments

  • bytes32 role
    • The role to grant.
  • address contractId
    • The contract address of the ERC1155 token the user must own.
  • uint256 typeId
    • The typeId of the token the user must own.

_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

A function that checks whether the specified account owns a control token required for a particular role.

Details

The function checks the lists of ERC721 and ERC1155 tokens associated with the specified role to determine whether the account owns any of them (whether balanceOf is greater than 0). It returns true if the account owns at least one of these tokens, or false otherwise.

Arguments

  • bytes32 role
    • The role to check.
  • address account
    • The account address to check.

Return value

  • bool
    • Whether the specified account owns a token required for the role.

The following example uses ERC7303 within an ERC721 token contract to define the minter and burner roles. By configuring the appropriate ERC721 or ERC1155 control tokens, accounts holding these roles can create new tokens and destroy existing ones.

ts
// 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);
    }
}

Security considerations

References

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

This article examined ERC7303, which proposes control tokens based on ERC721 and ERC1155 to implement role-based access control.

I hope you found it useful.

If you have questions, feel free to contact me via Twitter DM.

Twitter @cardene777

I also publish content on the following platforms.

https://chaldene.net/