Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merkletreejs hash does not match expected Solidity keccak hash #57

Open
QEDK opened this issue Jun 6, 2022 · 1 comment
Open

Merkletreejs hash does not match expected Solidity keccak hash #57

QEDK opened this issue Jun 6, 2022 · 1 comment

Comments

@QEDK
Copy link

QEDK commented Jun 6, 2022

I presume this is because the hexstring is parsed as text, for e.g. this tree:

└─ 7bd62a75d6e486a759d2060beeaaa851c1bb46427fa155a3b09d7895a761f58b
   ├─ e9e8231adc2ecb81d80587742818d309b6cdfc3fb4abb09eeab4f8b8492bd58a
   │  ├─ 8b117e8553a17ab37aba92ae84cc97a7d4df9aebce486b72741df499cb04a344
   │  └─ 9214d159784576263b97979db3f50143f3314f118237c8ec2c9ed005589e93de
   └─ 2299a49e35aeeb95f3e0466bc143bc6df56ab5e13b8c6d191614a966122a0d63
      ├─ 57b7579accf9fdd32ca51f776a3d032dd6dcfea6a4a83ba304de30e12a7db68e
      └─ 06c6b07877bcb499d6952d8e1503e591a64f476a6c35beb93d601eccd48f4a1a

If you put in the first two leaves concatenated on https://emn178.github.io/online-tools/keccak_256.html it would actually be f68b7c2821ff5479150c5204d6671eb40a33748a1d2e5becc7b34dc17964dad1 which matches the Solidity output of keccak256(abi.encodePacked(leaf1,leaf2))

For some additional context, I have a struct like:

Obj {
 id: uint256,
 ...
}

This is then encoded in Solidity like keccak256(abi.encode(objs)) and is used as the leaf. The leaf encoding is correct (verified using debugger) and the entire process works fine in Solidity, except that each hash does not agree to the one produced by Solidity.

Here's the code I am using to run this:

import { keccak256 } from "keccak256";
...
tree = new MerkleTree(hashes, keccak256); // hashes is array of keccak256(abi.encode(objs)) 
const root = tree.getHexRoot();
console.log(tree.toString());

hashes looks like:

[
  '0x8b117e8553a17ab37aba92ae84cc97a7d4df9aebce486b72741df499cb04a344',
  '0x9214d159784576263b97979db3f50143f3314f118237c8ec2c9ed005589e93de',
  '0x57b7579accf9fdd32ca51f776a3d032dd6dcfea6a4a83ba304de30e12a7db68e',
  '0x06c6b07877bcb499d6952d8e1503e591a64f476a6c35beb93d601eccd48f4a1a'
]
@esorey
Copy link

esorey commented Jul 4, 2022

Ran into something similar - try dropping the object-based import of keccak256, like so:

import keccak256 from "keccak256"

Assuming that that does the trick, this seems to actually be an issue with how keccak256 exports its functions - very surprising, not good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants