Solidity! Invalid implicit conversion from string memory to bytes memory requested The Next...

Is it possible to search for a directory/file combination?

Is there a difference between "Fahrstuhl" and "Aufzug"

Why do remote companies require working in the US?

If a black hole is created from light, can this black hole then move at speed of light?

Is it ever safe to open a suspicious html file (e.g. email attachment)?

Contours of a clandestine nature

Real integral using residue theorem - why doesn't this work?

Example of a Mathematician/Physicist whose Other Publications during their PhD eclipsed their PhD Thesis

Make solar eclipses exceedingly rare, but still have new moons

How do scammers retract money, while you can’t?

What's the best way to handle refactoring a big file?

How do I transpose the first and deepest levels of an arbitrarily nested array?

Why am I allowed to create multiple unique pointers from a single object?

What does "Its cash flow is deeply negative" mean?

sp_blitzCache results Memory grants

WOW air has ceased operation, can I get my tickets refunded?

Do I need to enable Dev Hub in my PROD Org?

Why do we use the plural of movies in this phrase "We went to the movies last night."?

"In the right combination" vs "with the right combination"?

What is "(CFMCC)" on an ILS approach chart?

Complex fractions

Sending manuscript to multiple publishers

Can you replace a racial trait cantrip when leveling up?

How to count occurrences of text in a file?



Solidity! Invalid implicit conversion from string memory to bytes memory requested



The Next CEO of Stack OverflowWhen to use “View” and “Pure” in place of “Constant”Car Rental System Compile ErrorHow to type-cast a function pointer in Solidity?Need help with the following type for agrument in function call:Invalid implicit conversion from address to contractTypeError: This type is only supported in the new experimental ABI encoderHow to handle dynamic size string array in solidity?Solidity: data type conversion from bytes to address/string/uint/int [ any library available? ]Invalid implicit conversion from bytes32 to bytes memory requestedHow to compare two bytes32?Invalid implicit conversion from uint256 to bytes memory requested












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










share|improve this question









New contributor




Khalid168 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    It's better if you can copy-paste your code, rather than attaching a screenshot

    – Achala Dissanayake
    1 hour ago
















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










share|improve this question









New contributor




Khalid168 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    It's better if you can copy-paste your code, rather than attaching a screenshot

    – Achala Dissanayake
    1 hour ago














3












3








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










share|improve this question









New contributor




Khalid168 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












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







solidity error string bytes data-types






share|improve this question









New contributor




Khalid168 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Khalid168 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 14 mins ago







Khalid168













New contributor




Khalid168 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









Khalid168Khalid168

184




184




New contributor




Khalid168 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Khalid168 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Khalid168 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2





    It's better if you can copy-paste your code, rather than attaching a screenshot

    – Achala Dissanayake
    1 hour ago














  • 2





    It's better if you can copy-paste your code, rather than attaching a screenshot

    – Achala Dissanayake
    1 hour ago








2




2





It's better if you can copy-paste your code, rather than attaching a screenshot

– Achala Dissanayake
1 hour ago





It's better if you can copy-paste your code, rather than attaching a screenshot

– Achala Dissanayake
1 hour ago










2 Answers
2






active

oldest

votes


















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));
}





share|improve this answer


























  • Thanks for the quick response! after changing the code as u suggested I am getting errors in the other part.

    – Khalid168
    30 mins ago











  • 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
    29 mins ago











  • I have added the code but seems quite ugly here!

    – Khalid168
    26 mins ago






  • 1





    Thanks a lot for the guidance...Now the problem is solved!

    – Khalid168
    9 mins ago






  • 1





    Yeah done now !

    – Khalid168
    4 mins ago



















1














Should probably be



return sha256(bytes (document));





share|improve this answer
























  • Thanks a lot for help ! It's solved like this

    – Khalid168
    9 mins ago












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "642"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Khalid168 is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f69055%2fsolidity-invalid-implicit-conversion-from-string-memory-to-bytes-memory-request%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









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));
}





share|improve this answer


























  • Thanks for the quick response! after changing the code as u suggested I am getting errors in the other part.

    – Khalid168
    30 mins ago











  • 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
    29 mins ago











  • I have added the code but seems quite ugly here!

    – Khalid168
    26 mins ago






  • 1





    Thanks a lot for the guidance...Now the problem is solved!

    – Khalid168
    9 mins ago






  • 1





    Yeah done now !

    – Khalid168
    4 mins ago
















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));
}





share|improve this answer


























  • Thanks for the quick response! after changing the code as u suggested I am getting errors in the other part.

    – Khalid168
    30 mins ago











  • 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
    29 mins ago











  • I have added the code but seems quite ugly here!

    – Khalid168
    26 mins ago






  • 1





    Thanks a lot for the guidance...Now the problem is solved!

    – Khalid168
    9 mins ago






  • 1





    Yeah done now !

    – Khalid168
    4 mins ago














1












1








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));
}





share|improve this answer















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));
}






share|improve this answer














share|improve this answer



share|improve this answer








edited 7 mins ago

























answered 1 hour ago









Achala DissanayakeAchala Dissanayake

3,78781629




3,78781629













  • Thanks for the quick response! after changing the code as u suggested I am getting errors in the other part.

    – Khalid168
    30 mins ago











  • 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
    29 mins ago











  • I have added the code but seems quite ugly here!

    – Khalid168
    26 mins ago






  • 1





    Thanks a lot for the guidance...Now the problem is solved!

    – Khalid168
    9 mins ago






  • 1





    Yeah done now !

    – Khalid168
    4 mins ago



















  • Thanks for the quick response! after changing the code as u suggested I am getting errors in the other part.

    – Khalid168
    30 mins ago











  • 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
    29 mins ago











  • I have added the code but seems quite ugly here!

    – Khalid168
    26 mins ago






  • 1





    Thanks a lot for the guidance...Now the problem is solved!

    – Khalid168
    9 mins ago






  • 1





    Yeah done now !

    – Khalid168
    4 mins ago

















Thanks for the quick response! after changing the code as u suggested I am getting errors in the other part.

– Khalid168
30 mins ago





Thanks for the quick response! after changing the code as u suggested I am getting errors in the other part.

– Khalid168
30 mins ago













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
29 mins ago





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
29 mins ago













I have added the code but seems quite ugly here!

– Khalid168
26 mins ago





I have added the code but seems quite ugly here!

– Khalid168
26 mins ago




1




1





Thanks a lot for the guidance...Now the problem is solved!

– Khalid168
9 mins ago





Thanks a lot for the guidance...Now the problem is solved!

– Khalid168
9 mins ago




1




1





Yeah done now !

– Khalid168
4 mins ago





Yeah done now !

– Khalid168
4 mins ago











1














Should probably be



return sha256(bytes (document));





share|improve this answer
























  • Thanks a lot for help ! It's solved like this

    – Khalid168
    9 mins ago
















1














Should probably be



return sha256(bytes (document));





share|improve this answer
























  • Thanks a lot for help ! It's solved like this

    – Khalid168
    9 mins ago














1












1








1







Should probably be



return sha256(bytes (document));





share|improve this answer













Should probably be



return sha256(bytes (document));






share|improve this answer












share|improve this answer



share|improve this answer










answered 21 mins ago









Mikhail VladimirovMikhail Vladimirov

1,003314




1,003314













  • Thanks a lot for help ! It's solved like this

    – Khalid168
    9 mins ago



















  • Thanks a lot for help ! It's solved like this

    – Khalid168
    9 mins ago

















Thanks a lot for help ! It's solved like this

– Khalid168
9 mins ago





Thanks a lot for help ! It's solved like this

– Khalid168
9 mins ago










Khalid168 is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Khalid168 is a new contributor. Be nice, and check out our Code of Conduct.













Khalid168 is a new contributor. Be nice, and check out our Code of Conduct.












Khalid168 is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Ethereum Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fethereum.stackexchange.com%2fquestions%2f69055%2fsolidity-invalid-implicit-conversion-from-string-memory-to-bytes-memory-request%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Gersau Kjelder | Navigasjonsmeny46°59′0″N 8°31′0″E46°59′0″N...

Hestehale Innhaldsliste Hestehale på kvinner | Hestehale på menn | Galleri | Sjå òg |...

What is the “three and three hundred thousand syndrome”?Who wrote the book Arena?What five creatures were...