I am writing a Smart Contract that saves a certain random hash given by the owner that is used for a 3rd party reason. I would like the owner of the contract to specify some Ethereum addresses that is allowed to access the hash.

These are the steps:

  1. The owner creates the Contract with the description on what the hash is used for.

  2. The Contract allows the owner to upload the hash.

  3. The hash is stored privately.
  4. The owner only can authorize certain Ethereum addresses to call the hashvalue which was "saved" by the owner.
  5. If the hash is requested by an authorized person, it is given to that peson otherwise rejected.

Here are my code, but I am struggling a bit. (see comments for more info} /* This contract stores a Random Hash with its description provided by the owner, The owner has the ability to authorize individuals to gain access to the hash which they can then call the RequestHash function and it will be provided

This is what I have so far but I need some help

My main problems are:

a. returning the hash

b. knowing what format to save the hash

c. getting this thing to work haha

start of code

pragma solidity>0.1.6<0.6.0;

contract DataLocationHashC {
/*Start declaring variables*/

    struct DataUser {
    /* This is to keep track of each person who uploads tries to request the hash*/
    bool authorized; /* True if owner authorized them, False if not*/
    bool reqhash; /*This is too see if the user has requested the has or not, True if yes, False if no*/
    string hashreq;/*This is to indicate which hash is requested, it will obviosuly be this contracts hash*/
    uint reqquant;/* How much times a particular user has requested the hash*/

        string DescrptionHash;
        uint HashValue;

    address public owner;/*owner of this contract (whoeveer deploys contract)*/

    string public description;/*Describes what the hash is for*/

    mapping( address => DataUser)  datauser; /* Basically you can get a corresponding DataUser (struct) value for a given 
    address from the new variable which as almost like an array of structs in this case called datauser. To get a corresponding
    value here you need to call datauser[address] and that will return the struct DataUser. */ 
/*End declaring variables*/

/*Start declaring constructor*/
/* the constructor captures certain values and performs certain functions as soon as the contract is deployed*/
    constructor  (string memory _description) public {
        owner = msg.sender; /* This asssigns the address of the user account that deploys this contract as the owner of the contarct*/
        description = _description;
        /* This asssigns the name recieved by the constructor to the name of the election or vote*/
/*End declaring constructor*/

/*Start declaring modifiers*/
/* modifiers gives certain permission to certain addresses*/
    modifier ownerOnly() {
    require(msg.sender == owner);
    _; /* This undescore semicolon means after the modifier is accepted and executed, execute the rest of the function beneath it*/
    /* We must now thus add the modifier in the name of every function/cmd we want only the modifier may access or change, such as add candidates*/
/*End declaring modifiers*/

/*Start Declaring Functions*/
    /* Authorize people(adresses) that is allowed to get the transaction hash*/
    /*This function authorizes specific adresses that may request and recievethe data location hash*/
    function authorizePerson(address _person) ownerOnly public {
        datauser[_person].authorized = true;
    /* Note that we have not yet populized the datauser array, in solidity when mapping, it already populizes the field with 0*/

    /*uint private Hash;*/

    function addHash(uint _ActualHash) ownerOnly public {

            HashValue = _ActualHash;


      function RequestHash() public constant returns(uint ){

    /*must be authorized to gain access*/    

        datauser[msg.sender].hashreq = description;
        datauser[msg.sender].reqhash = true;

       return (HashValue);


    /* This contract will destroy the contract and no state changes can follow anymore*/
    function end() ownerOnly public {

        /* the reason we add msg.sender to selfdestruct is that if there is any remaining Ether in this
        contract, it will be sent back to the owners address, only owner can access end function thus he is msg sender*/

/*End declaring other functions*/

I think this should be pretty basic, but I am new at this. Thanks a lot

  • I think that your problem starts with stored privately, because there is no such thing on Ethereum. You can restrict write-access, but you cannot really restrict read-access. – goodvibration Sep 23 '19 at 14:21
  • I agree, I would just like to store it on the Ethereum chain in this case. When I say private, I just mean, for this contract in regards to requesting it. The thing is the hash would be on the chain, but no one will know what it means. And the people using it would prob not know where to find it other than this contract which runs on the back end of a Dapp. – AvocadoChocolate Sep 23 '19 at 14:30
  • So what's your actual question then? (i.e., try to ask one at a time, so start with the most basic one please). – goodvibration Sep 23 '19 at 14:42
  • I would like a owner to upload a random hash into the Smart Contract. The owner will identify addresses that may access this hash via this smart contract. So the function "RequestHash" must return this hash to authorized adresses and reject unauthorized addresses. I have tried this with my code above, but I cannot seem to get it working. How do I do this? – AvocadoChocolate Sep 23 '19 at 14:47
  • So the function "RequestHash" must return this hash to authorized adresses and reject unauthorized addresses - What exactly is the point, when anyone can read this hash in other ways (i.e., not via your function)??? – goodvibration Sep 23 '19 at 15:05

More questions followed in the comments that lead to this answer.

I have found my mistake. The owner has to Authorize himself/herself too. The Contract was denying access to the hash the whole time due to authorization.

  • The owner has to authorize itself only if you want to use it in order to call function RequestHash. So you really haven't answered anything here, just explained what should be done in order to allow yourself to call this function with the owner (deployer) of the contract. – goodvibration Sep 23 '19 at 17:09
  • My question was, "I cannot seem to get it working. How do I do this?" And this answers it. I know the fault was not in the code, but in the implementation. It still answers the question though. – AvocadoChocolate Sep 23 '19 at 17:15
  • function RequestHash() public constant is not supposed to compile, due to the constant. So the code that you've posted is probably not the same as what you have on machine (either you've removed that keyword, or you've removed some of the code inside that function). – goodvibration Sep 23 '19 at 18:13
  • Yes, changed constant to view...my bad. Thank You! – AvocadoChocolate Sep 23 '19 at 18:53
  • Are you joking? They're the same! view wasn't available in older compiler versions, constant might be unavailable in newer compiler versions. The problem is those two lines datauser[msg.sender].hashreq = description; datauser[msg.sender].reqhash = true;, which cannot be present in a constant function. – goodvibration Sep 23 '19 at 18:59

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.