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

MerkleTree always returns [] #60

Open
damianlluch opened this issue Jul 12, 2022 · 2 comments
Open

MerkleTree always returns [] #60

damianlluch opened this issue Jul 12, 2022 · 2 comments

Comments

@damianlluch
Copy link

damianlluch commented Jul 12, 2022

Hi, I am trying to use this library to manage a whiteList of Ethereum address.

Question, I have tried many ways, and I can not get it to work.

I had to modify a little bit the leafNodes part, because when using Typescript it was giving me problems.

I'm using MongoDB. I clarify.
The merkleRoot generation function seems to work correctly like this:

const arrayUsers = [];
    users.map(function (element) {
      arrayUsers.push(element.address);
    });
    const leafNodes = arrayUsers.map(x => keccak256(Buffer.from(x)));
    const tree = new MerkleTree(leafNodes, keccak256, { sortPairs: true });
    const root = '0x' + tree.getRoot().toString('hex');
    whiteList.merkleRoot = root;

Now for the merkleProff, I'm running it like this (based on examples I saw in the tests section).

    const leafNodes = whiteList.addresses.map(x => keccak256(Buffer.from(x)));
    const tree = new MerkleTree(leafNodes, keccak256, { sortPairs: true });
    const eligibleProof = tree.getHexProof(Buffer.from(leafNodes[0]));

In my code, I create the list, add an address to the whitelist, close the list (I create merkleRoot) and then use a function for the merkleProff to check if the user is in the whitelist.

@damianlluch
Copy link
Author

damianlluch commented Jul 12, 2022

I have also tried the following way, and I get the message:


Error: leaf is required

private async generateHexProof(_AddressSentToEndpoint, _whiteListAddresses) {
    const index = _whiteListAddresses.findIndex(element => {
      element == _AddressSentToEndpoint;
    });

    console.log(_whiteListAddresses, 'address');
    const leaves = _whiteListAddresses.map(v => keccak256(Buffer.from(v)))


    const merkleTree = new MerkleTree(leaves, keccak256, {
      sortPairs: true,
    });

    const claimingAddress = leaves[index]
    return merkleTree.getProof(claimingAddress);
  }

```



```
private async generateRootHashNormalized(_whiteListAddresses) {
    const leafNodes = _whiteListAddresses.map(addr =>
      keccak256(Buffer.from(addr)),
    );

    const merkleTree = new MerkleTree(leafNodes, keccak256, {
      sortPairs: true,
    });
    const rootHash = merkleTree.getRoot(); //buffer
    let rootHashNormalized = '0x';

    // @ts-ignore
    rootHash.map(element => {
      if (element.toString(16).length < 2) {
        rootHashNormalized += '0';
      }
      rootHashNormalized += element.toString(16);
    });

    return rootHashNormalized;
  }
```

@gaetan-hexadog
Copy link

On my side to make it work I have to add an empty entry in the original array:

[
    "",
    "0xAddress1",
    "0xAddress2"
]

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