Solidity Form – String Parameter Not Converting to bytes32 Automatically

javascriptsolidityweb3js

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"

Best Answer

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