3

I have a solidity function which looks like this-

function issueCertificate(address _recipient, bytes32 _certi_name)

When I call the function using truffle console, I am able to run it using-

issueCertificate("0x0213e3852b8afeb08929a0f448f2f693b0fc3ebe", "random")

But when I run it using web3 and forms with same data in string format, it gives error-

Error: Given parameter is not bytes: "random"

6

If you're using web3.js version 1.0, you can use:

web3.utils.asciiToHex("random")

See the documentation here:

https://web3js.readthedocs.io/en/1.0/web3-utils.html#asciitohex

4

Extend answers above to more complete picture:

Conversion from string to bytes32

web3.utils.fromAscii

Conversion from bytes32 to string

web3.utils.toUtf8()
1

Duplicate- https://stackoverflow.com/questions/46491123/string-parameter-not-automatically-parsing-into-bytes32-when-used-with-form

Try:

issueCertificate("0x0213e3852b8afeb08929a0f448f2f693b0fc3ebe", bytes32("random"))

Basically, wrap the string with bytes32()

Edit, missed the call being made from Web3 try:

issueCertificate("0x0213e3852b8afeb08929a0f448f2f693b0fc3ebe", web3.fromAscii("random"))

Basically, in Web3 wrap the string with web3.fromAscii()

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.