Pass parameter as bytes32 to Solidity Smart Contract

+1 vote

I am using this Ethereum Go Client and trying to pass a string / bytes32 to Solidity.

The function in the smart contract is very simple (for testing now):

  function vote(bytes32 id) {
    //id has the value 0x0000000000000000000000000000000000000000000000000000000000000000
  }

calling

hash, err := contract.Send(transaction, "vote", "myString")

will result in 0x0000000000000000000000000000000000000000000000000000000000000000

for the bytes32 param id...

How would I have to pass in the parameter to my Smart Contract from Go so that solidity will have the correct value?

Sep 27, 2018 in Blockchain by slayer
• 29,350 points
9,127 views

3 answers to this question.

+1 vote

I think you have to encode it

types.ComplexString("myString")
answered Sep 27, 2018 by digger
• 26,740 points
+1 vote

To convert a string to a bytes32 for solidity all you have to do is create a fixed length byte array in Go and copy the string the value to it.

value := [32]byte{}
copy(key[:], []byte("hello"))

Then you may pass the value to the smart contract function:

hash, err := contract.Send(transaction, "vote", value)
answered Oct 12, 2018 by Firoz
+1 vote

Use the common.HexToHash package and pass the string value.

convertedValue := common.HexToHash("myString")
hash, err := contract.Send(transaction, "vote", convertedValue)
answered Sep 19, 2019 by Grashias

edited Sep 19, 2019 by Omkar

Related Questions In Blockchain

0 votes
1 answer

Is the msg.sender address changed while passing to another contract as a parameter?

I tried your contracts in Remix, but ...READ MORE

answered Oct 10, 2018 in Blockchain by Omkar
• 69,210 points
1,141 views
+1 vote
4 answers

How to estimate the cost for deploying smart contract on mainnet?

Since you have already deployed the contract ...READ MORE

answered Apr 10, 2018 in Blockchain by Shashank
• 10,400 points
14,723 views
0 votes
1 answer

How do I send back ethers to the sender of the tokens in a smart contract?

Whenever a smart contract receives ether via ...READ MORE

answered May 30, 2018 in Blockchain by Perry
• 17,100 points
3,366 views
0 votes
1 answer

How is a request sent from an app to a smart contract?

Yes, the contract is distributed by every node ...READ MORE

answered Jun 4, 2018 in Blockchain by Perry
• 17,100 points
563 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,691 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,232 views
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
960 views
0 votes
1 answer

How to use real world/live data in my smart contract?

You cant access/embed real world data using ...READ MORE

answered Jul 12, 2018 in Blockchain by digger
• 26,740 points
902 views
0 votes
1 answer

How to limit execution of smart contract only to my dapp?

You can use modifiers to do it ...READ MORE

answered Jul 17, 2018 in Blockchain by digger
• 26,740 points
494 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP