question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Discussion: How web3 coerces strings into bytes32

See original GitHub issue

When 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:closed
  • Created 6 years ago
  • Comments:28 (17 by maintainers)

github_iconTop GitHub Comments

3reactions
ricmoocommented, Nov 20, 2017

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:

function web3StringToBytes32(text) {
    var result = ethers.utils.hexlify(ethers.utils.toUtf8Bytes(text));
    while (result.length < 66) { result += '0'; }
    if (result.length !== 66) { throw new Error("invalid web3 implicit bytes32"); }
    return result;
}
2reactions
elenadimitrovacommented, Nov 19, 2017

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 a utf8ToHex, asciiToHex and a handful of other useful conversion functions http://web3js.readthedocs.io/en/1.0/web3-utils.html?highlight=toUTF#utf8tohex

Also, 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” 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found