Solidity: How to return id from hash value?

0 votes

I have a solidity contract with a mapping table. I am mapping an id(uint) to hash values. I am able to return hash value from id but now I wanna do it the other way around, i.e., get id from hash value. Here's the code that I am using:

pragma solidity ^0.4.18;

contract Hash {
  bytes32 comphash;

  struct hashstruct {   
    bytes32 fhash;
  }

  mapping (uint => hashstruct) hashstructs;
  uint[] public hashAccts;

  function setinstructor(uint _uint,string _fhash) public {
    var a = hashstructs[_uint];

    a.fhash = sha256(_fhash);  

    hashAccts.push(_uint) -1;              
  }

  function getInstructor(uint ins) view public returns (bytes32) {
    return (hashstructs[ins].fhash);
  }

  function count() view public returns (uint) {
    return hashAccts.length;
  }            
}

How can i do it?

Oct 22, 2018 in Blockchain by digger
• 27,620 points
69 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can do it by storing the hash of your struct into another mapping like this:

mapping(bytes32 => uint) _map;

function setinstructor(uint _uint,string _fhash) public {
  var a = hashstructs[_uint];

  a.fhash = sha256(_fhash);  

  hashAccts.push(_uint) -1;              

  _map[keccak256(a.fhash)] = _uint; // Can pass in other struct members as well
}
answered Oct 22, 2018 by Omkar
• 66,880 points

Related Questions In Blockchain

0 votes
1 answer

How to get return value from solidity contract?

Once you call a constant function, you ...READ MORE

answered Aug 16, 2018 in Blockchain by slayer
• 29,040 points
330 views
+1 vote
2 answers

How to return value from a chaincode in Hyperledger Fabric?

Hyperledger Fabric supports only 2 types of ...READ MORE

answered Jun 13, 2018 in Blockchain by Perry
• 16,450 points
634 views
0 votes
1 answer

How to get value from solidity contract using java?

web3j caters for this very use case. It ...READ MORE

answered Sep 28, 2018 in Blockchain by slayer
• 29,040 points
135 views
0 votes
1 answer

How to set web3 solidity variable value?

The setLog transaction has not yet been mined by ...READ MORE

answered Jul 6, 2018 in Blockchain by Christine
• 15,790 points
86 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
81 views
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 27,620 points
1,754 views
0 votes
1 answer
0 votes
1 answer

Ethereum solidity: Unable to get value from another contract

I have faced similar issues when compiling ...READ MORE

answered Oct 15, 2018 in Blockchain by Omkar
• 66,880 points
101 views
0 votes
1 answer

How to read the ETH value and other token values from an account?

You can do this eth.accounts shows you all known ...READ MORE

answered Oct 22, 2018 in Blockchain by Omkar
• 66,880 points
14 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.