MerkleProof.test.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const { MerkleTree } = require('../helpers/merkleTree.js');
  2. const { sha3, bufferToHex } = require('ethereumjs-util');
  3. const MerkleProofWrapper = artifacts.require('MerkleProofWrapper');
  4. require('chai')
  5. .should();
  6. contract('MerkleProof', function () {
  7. let merkleProof;
  8. beforeEach(async function () {
  9. merkleProof = await MerkleProofWrapper.new();
  10. });
  11. describe('verifyProof', function () {
  12. it('should return true for a valid Merkle proof', async function () {
  13. const elements = ['a', 'b', 'c', 'd'];
  14. const merkleTree = new MerkleTree(elements);
  15. const root = merkleTree.getHexRoot();
  16. const proof = merkleTree.getHexProof(elements[0]);
  17. const leaf = bufferToHex(sha3(elements[0]));
  18. const result = await merkleProof.verifyProof(proof, root, leaf);
  19. result.should.be.true;
  20. });
  21. it('should return false for an invalid Merkle proof', async function () {
  22. const correctElements = ['a', 'b', 'c'];
  23. const correctMerkleTree = new MerkleTree(correctElements);
  24. const correctRoot = correctMerkleTree.getHexRoot();
  25. const correctLeaf = bufferToHex(sha3(correctElements[0]));
  26. const badElements = ['d', 'e', 'f'];
  27. const badMerkleTree = new MerkleTree(badElements);
  28. const badProof = badMerkleTree.getHexProof(badElements[0]);
  29. const result = await merkleProof.verifyProof(badProof, correctRoot, correctLeaf);
  30. result.should.be.false;
  31. });
  32. it('should return false for a Merkle proof of invalid length', async function () {
  33. const elements = ['a', 'b', 'c'];
  34. const merkleTree = new MerkleTree(elements);
  35. const root = merkleTree.getHexRoot();
  36. const proof = merkleTree.getHexProof(elements[0]);
  37. const badProof = proof.slice(0, proof.length - 5);
  38. const leaf = bufferToHex(sha3(elements[0]));
  39. const result = await merkleProof.verifyProof(badProof, root, leaf);
  40. result.should.be.false;
  41. });
  42. });
  43. });