MerkleProof.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. var MerkleProof = artifacts.require("./MerkleProof.sol");
  2. import { sha3 } from "ethereumjs-util";
  3. import MerkleTree from "./helpers/merkleTree.js";
  4. contract('MerkleProof', function(accounts) {
  5. let merkleProof;
  6. before(async function() {
  7. merkleProof = await MerkleProof.new();
  8. });
  9. describe("verifyProof", function() {
  10. it("should return true for a valid Merkle proof", async function() {
  11. const elements = ["a", "b", "c", "d"].map(el => sha3(el));
  12. const merkleTree = new MerkleTree(elements);
  13. const root = merkleTree.getHexRoot();
  14. const proof = merkleTree.getHexProof(elements[0]);
  15. const leaf = merkleTree.bufToHex(elements[0]);
  16. const result = await merkleProof.verifyProof(proof, root, leaf);
  17. assert.isOk(result, "verifyProof did not return true for a valid proof");
  18. });
  19. it("should return false for an invalid Merkle proof", async function() {
  20. const elements = ["a", "b", "c"].map(el => sha3(el));
  21. const merkleTree = new MerkleTree(elements);
  22. const root = merkleTree.getHexRoot();
  23. const proof = merkleTree.getHexProof(elements[0]);
  24. const badProof = proof.slice(0, proof.length - 32);
  25. const leaf = merkleTree.bufToHex(elements[0]);
  26. const result = await merkleProof.verifyProof(badProof, root, leaf);
  27. assert.isNotOk(result, "verifyProof did not return false for an invalid proof");
  28. });
  29. });
  30. });