1

I am working on a Solidity smart contract for a lottery system using Merkle trees to verify winners. However, I consistently encounter an issue where the verify method of the Merkle Tree returns false during testing, indicating a mismatch between the provided proofs and the leaves.

Contract Overview: The smart contract, involves users buying lottery tickets, participating in draws, and claiming rewards. The contract uses Merkle trees to manage and verify winners.

Problem:

During testing with Hardhat, the verify method of the Merkle Tree (using merkletreejs and keccak256 for hashing) always returns false. This occurs even when I am confident that the proofs, leaves, and roots should correctly align.

Smart Contract Function for Claiming Rewards:

function claimPreviousRoundRewards( uint256 _ticketId, uint256 _roundId, address _user, bytes32[] calldata _merkleProof ) public { // ... [initialization and setup code] ... bytes32 leaf = keccak256(abi.encodePacked(_user)); if (MerkleProof.verify(_merkleProof, trees.grandPriceWinners, leaf)) { // process claim } // ... [more verification for other prize types] ... } 

Test Setup and Verification Logic:

// Generating Merkle Tree and proofs const leaves = winners.map(w => ethers.utils.keccak256(w.winner)); const tree = new MerkleTree(leaves, ethers.utils.keccak256, { sort: true }); const proof = tree.getProof(ethers.utils.keccak256(winner.winner)); const flattenedProof = proof.map(p => p[0]); // Verifying in tests const isVerified = tree.verify(proof, leaf, root); 

Attempts to Resolve:

  • Ensured consistent hashing (keccak256) in both contract and tests.
  • Checked that leaf generation aligns with the contract's expectations.
  • Verified the order and encoding of data for leaves.

Questions

  • Why might the Merkle Tree verification consistently fail in this context?
  • Are there specific considerations in Solidity or JavaScript that I might be overlooking related to Merkle tree implementation?

Any insights or suggestions would be greatly appreciated!

1
  • How do you store the tree on-chain Commented May 25, 2024 at 13:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.