Ive been trying to whitelist huge amount of addresses with merkle tree.

Here is my json:


Here is my js:

const { MerkleTree } = require('merkletreejs');
const whitelist = require('./whitelist.json');
const keccak256 = require('keccak256');

const leafNodes = whitelist.map(addr => keccak256(addr));
const tree = new MerkleTree(leafNodes, keccak256, { softPairs: true });
const root = tree.getHexRoot();
const claimingAdress = leafNodes[0];
const proof = tree.getHexProof(claimingAdress);
console.log(tree.verify(proof, leafNodes, root));

When I run this I get "TypeError [ERR_INVALID_ARG_TYPE]: The "list[1]" argument must be an instance of Buffer or Uint8Array."

What am I doing wrong here?


edit: I changed the code a bit and Im getting proof array with 3 elements.

New code:

const { MerkleTree } = require('merkletreejs');
const keccak256 = require('keccak256');
const whitelistAddresses = require('./whitelist.json');
const leafNodes = whitelistAddresses.map(addr => keccak256(addr));
const tree = new MerkleTree(leafNodes, keccak256, { sortPairs: true});
const root = tree.getRoot();
const claimingAccount = leafNodes[0];
const hexProof = tree.getHexProof(claimingAccount);

Is proof sent to smart contract as an array? or do I have to do something before I send?

edit 2: Yes proof sent as an array and it works.

  • What commands to you use to run this in visual, I did npm install merkletree and npm install keccak256 but unsure what command I use to run the final thing? Cheers
    – user86079
    Commented Nov 20, 2021 at 10:04
  • If you have node js installed you can run it with: "node scriptname.js" Just make sure that you are in same directory with that script. Commented Nov 22, 2021 at 19:33
  • Here is a link to Using Merkel tree for whitelisting medium.com/@ItsCuzzo/… I am supprised you haven't found this.
    – Sky
    Commented Feb 20, 2022 at 16:36