Testing MerkeProof

Hi @abcoathup,

I’ve migrated to hardhat (waffle & ethers) and merkletreejs and I’m having some issue testing the MerkeProof.sol code with addresses as leaves.
I’ve opened an issue here.
Any idea?

Thanks!

1 Like

Hello @naszam.

As an answer, I’ll provide some code that hopefully will help you:

Its a (modified to use address as leaf, which in my case in unsafe, but shows you how to do) snapshot from a workshop I’m preparing. Don’t miss it :slight_smile:

2 Likes

Hi @Amxx,

Thanks for your help!

I’ve tried your suggestions as follow:

  function hash(account) {
    return Buffer.from(account.slice(2).padStart(64, "0"), "hex")
  }
    it("return a baseURI + tokenURI for tokenId", async () => {
      const leaves = [
        signers.redeemer.address,
        signers.deployer.address,
        signers.templater.address,
        signers.random.address,
      ]
      const merkleTree = new MerkleTree(
        Object.entries(leaves).map((v) => hash(v)),
        keccak256,
        { sortPairs: true },
      )

      const root = merkleTree.getHexRoot()
      const rootHashes = [root]
      const leaf = hash(signers.redeemer.address)

      const proof = merkleTree.getHexProof(leaf)
      await makerbadges.connect(signers.deployer).setRootHashes(rootHashes)
      await makerbadges.connect(signers.deployer).createTemplate(template_name, template_description, template_image)
      await makerbadges.connect(signers.redeemer).activateBadge(proof, templateId, tokenURI)
      const tokenId = await makerbadges.tokenOfOwnerByIndex(signers.redeemer.address, index1)
      expect(await makerbadges.tokenURI(tokenId)).to.be.eq("https://badges.makerdao.com/token/" + tokenURI)
    })

but I get:

  1) MakerBadges
       ERC721 metadata
         return a baseURI + tokenURI for tokenId:
     TypeError: account.slice(...).padStart is not a function
      at hash (test/MakerBadges.ts:35:41)
      at /home/sencha/maker-badges/test/MakerBadges.ts:82:43
      at Array.map (<anonymous>)
      at /home/sencha/maker-badges/test/MakerBadges.ts:82:32
      at step (test/MakerBadges.ts:34:23)
      at Object.next (test/MakerBadges.ts:15:53)
      at /home/sencha/maker-badges/test/MakerBadges.ts:9:71
      at new Promise (<anonymous>)
      at __awaiter (test/MakerBadges.ts:5:12)
      at Context.<anonymous> (test/MakerBadges.ts:75:51)

Btw, I’m testing in typescript.

Thanks!

1 Like

If leave is an array of addresses, you should not do Object.entries(leaves).map((v) => hash(v)),. Instead, leaves.map((v) => hash(v)), should be enough!

2 Likes

Hi @Amxx,

It appears there was an issue with the v0.2.13 of merkletreejs.
For more info you can have a look here.

Thanks!

1 Like