0

First time poster so please go easy!

I'm new to Solidity. Have been programming about 2-3 years & I'm working on an Ethereum supply chain dapp for a college project. I've got to the point where I want to assign roles to accounts/actors within my system and hit a wall..

These roles being: SupplyChainAdmin (Admin), Producer (User), Handler (User), Retailer (User). Pretty straightforward in that SupplyChainAdmin will deploy the main contract as msg.sender and then assign the roles to each address afterward.

I've been researching and found this https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/AccessControl.sol contract that enables it, and based on this I found an additional https://github.com/HQ20/contracts/blob/master/contracts/access/Administered.sol that seems to enable what I need... But I'm struggling with how I need to set it up.

Here is my code so far based on this...

pragma solidity >=0.6.0 <0.8.0;

import "github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/AccessControl.sol";

contract RoleManager is AccessControl {
    
    // Edit -- this line is copy/paste from the Administered.sol, I have no idea what it does!
    bytes32 public constant USER_ROLE = keccak256("USER");
    
    
    constructor (address root) public {
        _setupRole(DEFAULT_ADMIN_ROLE, root);
        _setRoleAdmin(USER_ROLE, DEFAULT_ADMIN_ROLE);
    }
    
    
    /// @dev Restricted to members of the admin role.
    modifier onlyAdmin() {
        require(isAdmin(msg.sender), "Restricted to admins.");
        _;
    }

    /// @dev Return `true` if the account belongs to the admin role.
    function isAdmin(address account) public virtual view returns (bool) {
        return hasRole(DEFAULT_ADMIN_ROLE, account);
    }
    
    
    
    
    
    
    /// @dev Restricted to members of the Producer role.
    modifier onlyProducer() {
        require(isProducer(msg.sender), "Restricted to users.");
        _;
    }

    
    /// @dev Return `true` if the account belongs to the Producer role.
    function isProducer(address account) public virtual view returns (bool) {
        return hasRole(USER_ROLE, account);
    }
    
    
    
    
    
    /// @dev Restricted to members of the user role.
    modifier onlyHandler() {
        require(isHandler(msg.sender), "Restricted to users.");
        _;
    }

    
    /// @dev Return `true` if the account belongs to the user role.
    function isHandler(address account) public virtual view returns (bool) {
        return hasRole(USER_ROLE, account);
    }
    
    
    
    
    
    
    /// @dev Restricted to members of the user role.
    modifier onlyRetailer() {
        require(isRetailer(msg.sender), "Restricted to users.");
        _;
    }

    
    /// @dev Return `true` if the account belongs to the user role.
    function isRetailer(address account) public virtual view returns (bool) {
        return hasRole(USER_ROLE, account);
    }
    
    
    
    
    // Add roles:
    
    function addProducerRole(bytes32 _role, address _account) public  {
        grantRole(_role, _account);         // (from OZ AccessControl)  // emits a RoleGranted(role, account, sender) event
    }
    
    function addHandlerRole(bytes32 _role, address _account) public  {
        grantRole(_role, _account); 
    }
    
    function addRetailerRole(bytes32 _role, address _account) public  {
        grantRole(_role, _account);
    }
    
    
    
    
    
    // Removes roles: 
    
    function removeProducer(address account) public virtual onlyAdmin {
        revokeRole(USER_ROLE, account);
    }
    
    function removeHandler(address account) public virtual onlyAdmin {
        revokeRole(USER_ROLE, account);
    }
    
    function removeRetailer(address account) public virtual onlyAdmin {
        revokeRole(USER_ROLE, account);
    }
    
    
}

So my to boil it down.. My questions are:

  • What is the purpose of the line bytes32 public constant USER_ROLE = keccak256("USER");? I have not seen this keccak256 so far in Solidity

  • I understand this contract is based on AccessControl.sol - but I'm struggling reviewing that with how roles of type eg; Producer, Retailer etc. are actually "declared"... Do I need to create a seperate contract for each type or something?

Thanks for reading

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.