Pass parameter as bytes32 to Solidity Smart Contract

0 votes

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,170 points
788 views

3 answers to this question.

0 votes

I think you have to encode it

types.ComplexString("myString")
answered Sep 27, 2018 by digger
• 26,550 points
0 votes

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
0 votes

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

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

edited Sep 19 by Omkar

Related Questions In Blockchain

0 votes
1 answer
+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 9, 2018 in Blockchain by Shashank
• 10,350 points
1,542 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,020 points
447 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,020 points
61 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
169 views
0 votes
1 answer
+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,620 points
156 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,550 points
60 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,550 points
23 views