最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - String parameter not automatically parsing into bytes32 when used with form - Stack Overflow

programmeradmin1浏览0评论

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"

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"

Share Improve this question edited Jul 6, 2022 at 14:28 TylerH 21.1k78 gold badges79 silver badges113 bronze badges asked Sep 29, 2017 at 14:34 Shubham ChaudharyShubham Chaudhary 1,4824 gold badges20 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

If you're using web3.js version 1.0, you can wrap the string as shown here:

web3.utils.asciiToHex("random")

See the documentation here:

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

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()

Update:

Latest version uses:

issueCertificate("0x0213e3852b8afeb08929a0f448f2f693b0fc3ebe", web3.utils.fromAscii("random"))
发布评论

评论列表(0)

  1. 暂无评论