I have a major problem/ I've got a simple smart contract that stores data (a text in blockchain and it works well, but when I pass keccak256, it stores only the string passed in as an argument but not the hashed value.

I have tried a lot of code from the internet but I haven't found a solution.

pragma solidity ^0.5.7;
contract store {
    bytes32 data;
    function set(string memory _word) public {
        data = keccak256(abi.encodePacked(_word));
  • 1
    How did you conclude that "it stores only the string passed in as an argument but not the hashed value"? – goodvibration Dec 19 '19 at 16:04

it stores only the string passed in as an argument but not the hashed value

That isn't true.

The statement makes me wonder about your testing methodolody and how you came to that conclusion.

The contract has no way to give back the stored value. I am guessing that you are observing the transaction and seeing the input, so yes, the input would be ... the input.

The stored value is different. That's data and it is in storage, and that's where the value went, but you can't inspect it. That's why I'm not sure what you are doing instead.

Change line 3 to:

bytes32 public data;

This will give you a "free" function, function data() public returns(bytes32) {... so you can inspect the contract state and see your data. Then, you will see that the contract did indeed store the hash where you wanted it go.


Consider playing with Remix where you will see a blue button for data. You can use that to inspect the function and see your data. The client-side testing methodology is another challenge involving JavaScript, command-line, abstractions, etc. That can be a source of confusion.

Use Remix to keep the focus on the contract itself and tackle integration with web sites, command-line and other ways to interact with the contract as separate concerns.

Hope it helps.

  • yeap for sure, it help me a lot. for sure, after save it i will retrieve it by input data. now i will test. – Jeremy El Patron Dec 19 '19 at 16:29

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