4

I have 3 contracts that I want to implement. The contracts are master , admin and customer. So the master contract is initiated by a single person, the owner of the private chain (based on geth). The admin contract provides access to certain addresses. These admin instances are created by the master. I am using a modifier to ensure this. The customer contract can be interacted with only by the admin and master contract.

I have mapped these addresses to accounts (with username and pw) stored on my local Redis db. Snipper of my contract structure is below:

Master Contract

contract MasterContract{
    bool public state = false;
    address public masterAddress;
    bool permCheck;


    // ACL
    // Subject(Role) - Object/Subject - permission - On/Off
    mapping (uint => mapping(uint => perm)) public acl;

    // Init Function
    function masterContractInit(string name,string detail){
      if(msg.sender!="0x0"){
        throw;
      }else{
        masterAddress = msg.sender;
      }
    }

    modifier onlyMasterAccess{
      if (msg.sender == masterAddress) _
      else throw;
    }
}

The admin contract

contract AdminContract{
  bool state=false;
  struct Admin{
      bool status;
      string name;
  }
  mapping (address => admin ) public admins;

  // How to reference this masterAddress?
  modifier checkMasterAndAdminAccess{
    if (msg.sender == masterAddress || admins[msg.sender].status) _
    else throw;
  }

  function initAdminContract() onlyMasterAccess{    
    state = true;
  }

  function addAdmin(address addr, string name) onlyMasterAccess{
    admins[addr]=Admin(true, name);
  }

  function revokeAdmin(address addr) onlyMasterAccess{
    if(addr!="0x0"){
      throw;
    }else{
      admins[addr].status = false;
    }
  }
}

Customer contract

contract Customer{
  bool public state = false;

  function initCustomerContract() onlyMasterAccess{    
    state = true;
  }

  function foo() checkMasterAndAdminAccess{
    // Do something here
    // only admin or master can call this function
  }

  function bar(){
    // Do something, but allow others to modify this
  }   
}

Now I need to further understand how to combine these contracts and reference the master and admin address in customer contract to use the modifier checkMasterAndAdminAccess. Ideally when compiling into ABI and using it in my Nodejs app, I would want to place these contracts into separate files as they will become massive.

Is this a recommended practice?

There will only be one instance of master contract controlled by only person (so only one address will have access to it). The other contracts may be instantiated more than once

Can I get some guidance on how to achieve the above.

1

If you just want to allow other contracts to access your code you should use inheritance:

Customer is Admin:

Though this doesn't make much sense to me. I think your issue is less about sharing functionality and more about the way you're organizing your code. The easiest way to deal with permissions is to have a phone book contract that keeps track of user permissions. Then have all your functions inherit from that contract so they can use the necessary modifiers. Like this:

contract PhoneBook {
    modifier onlyMasterAccess() {
        require(msg.sender == master);
        _;
    }
}


contract Customer is PhoneBook{
    function initCustomerContract() onlyMasterAccess {    
        state = true;
  }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.