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,740 points
2,032 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,210 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,350 points
4,742 views
0 votes
0 answers

Solidity: How i can return data from hash value ?

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

Feb 14, 2020 in Blockchain by simone
• 120 points
1,067 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,100 points
2,585 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,350 points
1,735 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
1,663 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,740 points
43,813 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,215 views
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,210 points
1,469 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,210 points
584 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP