1

I'm trying to check an hashed message inside my Solidity smart contract. In JavaScript I hash the message as follows:

util.sha3(Buffer.concat([
  new Buffer(address1),
  new Buffer(bytes32),
  new Buffer("0"),
  new Buffer(address2),
  new Buffer("0")
])).toString("hex");

When I check in Solidity I do

keccak256(id,ca,dataHash, false, address(0), false);

I've tried both with keccak256 and sha3, but both yield a different hash than the one produced in JavaScript. I'm suspecting some encoding issue (probably due to Buffer doing something different than Solidity), but I can't find anything about that.

Note that I'm not signing anything, so the issue described in this question should not apply (at least, not as I understand the answer and documentation)

0

I had three issues, mostly related to JavaScript and not, as I assumed in the question, with Solidity. For anyone who will encounter the same problem in the future, I list them here:

First of all, on the JS side, sha3 takes one or more Buffers as an input, which by default decode strings as utf-8, while web3.js decodes the same strings as hexes when sending them to the smart contract. The quickest way to solve this was to truncate the strings to remove the 0x part and change the encoding from Buffer:

Buffer.from(mystring.substring(2), "hex")

But after that my tests kept failing. Two of my values were booleans, which meant that I tried to have a Buffer of a single bit. This doesn't seem possible with Buffer, since anything shorter than a byte will be ignored. Luckily, 0x00 and 0x01 work as well for false and true, so I had to change my bool's encoding to:

Buffer.from("00", "hex")

Which output is hashed in the same way as false in Solidity (the same works with "01" and true)

Still, my automated tests kept failing. This was because util.sha3 yields a Buffer, which toString method returns a string like 0x1234567890abcdef, but web3.js' calls return a string without the first two charactes (like 1234567890abcdef).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.