3
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

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

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));
}
  • Thanks for the quick response! after changing the code as u suggested I am getting errors in the other part. – Khalid168 Mar 30 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 Mar 30 at 5:45
  • I have added the code but seems quite ugly here! – Khalid168 Mar 30 at 5:48
  • 1
    Thanks a lot for the guidance...Now the problem is solved! – Khalid168 Mar 30 at 6:04
  • 1
    Yeah done now ! – Khalid168 Mar 30 at 6:09
1

Should probably be

return sha256(bytes (document));
  • Thanks a lot for help ! It's solved like this – Khalid168 Mar 30 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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