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
• 26,670 points
560 views

1 answer to this question.

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
• 69,020 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,280 points
1,319 views
0 votes
0 answers

Solidity: How i can return data from hash value ?

I have a solidity contract with two ...READ MORE

Feb 13 in Blockchain by simone
• 120 points
105 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
• 17,090 points
1,353 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,280 points
588 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,810 points
355 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
• 26,670 points
16,269 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
• 69,020 points
491 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
• 69,020 points
70 views