Discussion: How web3 coerces strings into bytes32
See original GitHub issueWhen calling a public contract function which accepts a bytes32
parameter e.g. function createNetwork(bytes32 name)
with an Ascii string:
rootNetwork.functions.createNetwork('test');
Error is thrown:
Uncaught (in promise) Error: invalid arrayify value
Other contract function return correctly in this instance, implying wiring of the contract is correct.
Issue Analytics
- State:
- Created 6 years ago
- Comments:28 (17 by maintainers)
Top Results From Across the Web
How to convert string to bytes32 in web3js?
For web3 0.x: You need to use web3.fromAscii(val) to write correct bytes32 input to contract. And you are right, anyone can read it...
Read more >How to Interpret Binaries in Solidity - Alchemy Docs
In this article we will discuss why the Ethereum Virtual Machine encodes everything in ... It's nearly impossible to look at a long...
Read more >Why String instead Of Bytes32 In Solidity? - Morioh
Why String instead Of Bytes32 In Solidity? Use string for arbitrary-length string (UTF-8) data that's longer than 32 bytes. Frontends can decode a...
Read more >ConsenSys/truffle - Gitter
The main issue, in my experience, has been a change to type coercion. So instead of passing in strings you have to bytes32...
Read more >Documentation - Ethers.js
A Web3Provider wraps a standard Web3 provider, which is // what MetaMask injects ... To sign a simple string, which are used for...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Oh! The string you sent is the full bytes sent to the network, including the function selector.
To mimic the web3 coercion of short string into bytes32 you can use the following; but please be aware you are relying on incorrect behaviour, so terrible things will happen, if for example the string is over 32 bytes (not necessarily characters) long:
The
web3StringToBytes32
is more a string-to-hex than bytes32 conversion as it returns an arbitrary length hex string.From my part we can close this as I’ve gone up to v1 of
web3.utils
which has autf8ToHex
,asciiToHex
and a handful of other useful conversion functions http://web3js.readthedocs.io/en/1.0/web3-utils.html?highlight=toUTF#utf8tohexAlso, the auto conversion between strings (used as keys) and their encoded bytes32 hex equivalent has been discussed here well before the v1 release above https://github.com/ethereum/web3.js/issues/271 including comments from “the author of Solidity” 😃