question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: Cannot read property '_isHexString' of undefined

See original GitHub issue

Hi,

I’m using merkletreejs for the following test:

    it("return a baseURI + tokenURI for tokenId", async () => {
      const [redeemer, usr1, usr2, usr3, usr4] = await ethers.getSigners()
      const accounts = [redeemer.address, usr1.address, usr2.address, usr3.address, usr4.address]
      const merkleTree = new MerkleTree(accounts, keccak256, {
        hashLeaves: true,
        sortPairs: true,
      })

      const root = merkleTree.getHexRoot()
      const rootHashes = [root, "0x00", "0x00", "0x00", "0x00"]
      const leaf = keccak256(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(redeemer).activateBadge(proof, templateId, tokenURI)
      const tokenId = await makerbadges.tokenOfOwnerByIndex(redeemer.address, index1)
      expect(await makerbadges.tokenURI(tokenId)).to.be.eq("https://badges.makerdao.com/token/" + tokenURI)
    })

and I get the following error:

  1) MakerBadges
       ERC721 metadata
         return a baseURI + tokenURI for tokenId:
     TypeError: Cannot read property '_isHexString' of undefined
      at /home/sencha/maker-badges/node_modules/merkletreejs/dist/MerkleTree.js:961:22
      at Array.map (<anonymous>)
      at new MerkleTree (node_modules/merkletreejs/dist/MerkleTree.js:51:29)
      at Context.<anonymous> (test/MakerBadges.ts:75:26)
      at step (test/MakerBadges.ts:34:23)
      at Object.next (test/MakerBadges.ts:15:53)
      at fulfilled (test/MakerBadges.ts:6:58)
      at runMicrotasks (<anonymous>)
      at processTicksAndRejections (node:internal/process/task_queues:94:5)

I’ve also tried migrating to @nomiclabs/hardhat-truffle5 and @typechain/truffle-v5, I’ve got the same issue.

OS: Ubuntu 20.04 Source Code: https://github.com/naszam/maker-badges/blob/xdai/test/MakerBadges.ts

Thanks!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
miguelmotacommented, Mar 30, 2021

Awesome!

1reaction
miguelmotacommented, Mar 29, 2021

Hey @naszam, can you try updating to 0.2.15 or newer. The input errors you’re were seeing should be fixed.

Then in your use the sortPairs: true option. The reason this is needed is because the solidity library is sorting them before hashing as seen here:

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/243adff49ce1700e0ecb99fe522fb16cff1d1ddc/contracts/utils/cryptography/MerkleProof.sol#L27

if (computedHash <= proofElement) {
    computedHash = keccak256(abi.encodePacked(computedHash, proofElement));
} else {
    computedHash = keccak256(abi.encodePacked(proofElement, computedHash));
}

I also noticed that keccak256 was undefined so I had to import it as:

import * as keccak256 from "keccak256"

To have it import regularly you need to update a setting in tsconfig.json

{
  "compilerOptions": {
    "esModuleInterop": true
  }
}

And importing as expected worked:

import keccak256 from "keccak256"

However you can also use soliditySha3 as the hashing function to minimize dependencies.

This should definitely work now!

    it("return a baseURI + tokenURI for tokenId", async () => {
      const { soliditySha3 } = web3.utils
      const hash0 = soliditySha3(signers.redeemer.address)
      const hash1 = soliditySha3(signers.deployer.address)
      const hash2 = soliditySha3(signers.templater.address)
      const hash3 = soliditySha3(signers.random.address)
      const leaves = [hash0, hash1, hash2, hash3]
      const merkleTree = new MerkleTree(leaves, soliditySha3, {sortPairs: true})

      const root = merkleTree.getHexRoot()
      const rootHashes = [root]
      const leaf = soliditySha3(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)
    })

Test results:

  MakerBadges
    Setup
      ✓ deployer has the default admin role (40ms)
      ✓ deployer has the templater role
      ✓ deployer has the pauser role
    ERC721 metadata
      ✓ has a name
      ✓ has a symbol
      ✓ has a baseURI
      ✓ return an updated baseURI
      ✓ reverts when querying metadata for non existent tokenId
      ✓ return a baseURI + tokenURI for tokenId (73ms)
    createTemplate()
      ✓ templater should be able to create a template
      ✓ should emit the appropriate event when a template is created
      ✓ should not allow create a new template from random user


  12 passing (3s)
Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: Cannot read property '_isHexString' of undefined #35
Hi, I'm using merkletreejs for the following test: it("return a baseURI + tokenURI for tokenId", async () => { const [redeemer, usr1, usr2, ......
Read more >
Cannot read properties of undefined (reading 'toHexString') in ...
If you are using JavaScript and getting this error then You made a mistake while importing dotenv. you are using ".config" as property...
Read more >
Uncaught TypeError: Cannot read property of undefined In
Uncaught TypeError: Cannot read property of undefined error occurs in Chrome when you read a property or call a method on an undefined...
Read more >
How to Prevent cannot read property split of ... - Gopi Gorantala
The "cannot read property split of undefined" error occurs when we try to call the split() method on a variable that stores an...
Read more >
hardhat - TypeError: Cannot read properties 'toString'
The error log means that chainId is undefined. When you run the deploy script, you can specify which network config you want to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found