9
pragma solidity ^0.5

contract ProofOfExistence {

    // ... some code here

    function proofFor(string memory document) public view returns(bytes32) {
        return sha256(document);
    }

    // ... some more code here
}

Gives following error :

TypeError: Invalid type for argument in function call. Invalid implicit conversion from string memory to bytes memory requested.

I am unable to understand why this error is coming.

UPDATE :

pragma solidity ^0.5;
// Proof of Existence contract, version 3
contract ProofOfExistence3 {
  mapping (bytes32 => bool) private proofs;
  // store a proof of existence in the contract state
  function storeProof(bytes32 proof)  public{
    proofs[proof] = true;
  }
  // calculate and store the proof for a document
  function notarize(string memory document) public {
   **bytes32 proof = proofFor(document);**
    storeProof(proof);
  }
  // helper function to get a document's sha256
  function proofFor(bytes memory document) public pure returns (bytes32)  {
    return sha256(document);
  }
  // check if a document has been notarized
  function checkDocument(string memory document) view public returns (bool) {
    **bytes32 proof = proofFor(document);**
    return hasProof(proof);
  }
  // returns true if proof is stored
  function hasProof(bytes32 proof) view public returns(bool) {
    return proofs[proof];

UPDATE ERROR After the update as suggested I am getting the same error but on the lines having ** ...** this time. Why is it coming now? Thanks

1
  • 2
    It's better if you can copy-paste your code, rather than attaching a screenshot Commented Mar 30, 2019 at 5:05

2 Answers 2

3

Argument to sha256 should be in bytes. But you're passing document which is a string. Following would work.

function proofFor(bytes memory document) public pure returns(bytes32) {
    return sha256(document);
}

And a side note - your function doesn't need to be view type since it is not viewing any storage items. This can be made a pure function which has neither side effects nor storage viewing. More details can be found here.

UPDATE :

If changing string to bytes gives errors at other function calls you can either convert them to bytes as well or else you can cast string to bytes as @Mikhail suggested. Then that would be;

function proofFor(string memory document) public pure returns(bytes32) {
    return sha256(bytes (document));
}
6
  • Thanks for the quick response! after changing the code as u suggested I am getting errors in the other part.
    – Khalid168
    Commented Mar 30, 2019 at 5:44
  • pragma solidity ^0.5; // Proof of Existence contract, version 3 contract ProofOfExistence3 { mapping (bytes32 => bool) private proofs; // store a proof of existence in the contract state function storeProof(bytes32 proof) public{ proofs[proof] = true; } // calculate and store the proof for a document function notarize(string memory document) public { bytes32 proof = proofFor(document); storeProof(proof); }
    – Khalid168
    Commented Mar 30, 2019 at 5:45
  • I have added the code but seems quite ugly here!
    – Khalid168
    Commented Mar 30, 2019 at 5:48
  • 1
    Thanks a lot for the guidance...Now the problem is solved!
    – Khalid168
    Commented Mar 30, 2019 at 6:04
  • 1
    Yeah done now !
    – Khalid168
    Commented Mar 30, 2019 at 6:09
4

Should probably be

return sha256(bytes (document));
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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