MerkleProof.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { PANIC_CODES } = require('@nomicfoundation/hardhat-chai-matchers/panic');
  4. const { SimpleMerkleTree } = require('@openzeppelin/merkle-tree');
  5. // generate bytes32 leaves from a string
  6. const toLeaves = (str, separator = '') => str.split(separator).map(e => ethers.keccak256(ethers.toUtf8Bytes(e)));
  7. // internal node hashes
  8. const concatSorted = (...elements) => Buffer.concat(elements.map(ethers.getBytes).sort(Buffer.compare));
  9. const defaultHash = (a, b) => ethers.keccak256(concatSorted(a, b));
  10. const customHash = (a, b) => ethers.sha256(concatSorted(a, b));
  11. describe('MerkleProof', function () {
  12. for (const { title, contractName, nodeHash } of [
  13. { title: 'default hash', contractName: '$MerkleProof', nodeHash: defaultHash },
  14. { title: 'custom hash', contractName: '$MerkleProofCustomHashMock', nodeHash: customHash },
  15. ]) {
  16. describe(title, function () {
  17. // stateless: no need for a fixture, just use before
  18. before(async function () {
  19. this.mock = await ethers.deployContract(contractName);
  20. this.makeTree = str => SimpleMerkleTree.of(toLeaves(str), { nodeHash });
  21. });
  22. describe('verify', function () {
  23. it('returns true for a valid Merkle proof', async function () {
  24. const merkleTree = this.makeTree('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=');
  25. const root = merkleTree.root;
  26. const hash = merkleTree.at(0);
  27. const proof = merkleTree.getProof(0);
  28. expect(await this.mock.$processProof(proof, hash)).to.equal(root);
  29. expect(await this.mock.$processProofCalldata(proof, hash)).to.equal(root);
  30. expect(await this.mock.$verify(proof, root, hash)).to.be.true;
  31. expect(await this.mock.$verifyCalldata(proof, root, hash)).to.be.true;
  32. // For demonstration, it is also possible to create valid proofs for certain 64-byte values *not* in elements:
  33. const noSuchLeaf = nodeHash(hash, proof.at(0));
  34. expect(await this.mock.$processProof(proof.slice(1), noSuchLeaf)).to.equal(root);
  35. expect(await this.mock.$processProofCalldata(proof.slice(1), noSuchLeaf)).to.equal(root);
  36. expect(await this.mock.$verify(proof.slice(1), root, noSuchLeaf)).to.be.true;
  37. expect(await this.mock.$verifyCalldata(proof.slice(1), root, noSuchLeaf)).to.be.true;
  38. });
  39. it('returns false for an invalid Merkle proof', async function () {
  40. const correctMerkleTree = this.makeTree('abc');
  41. const otherMerkleTree = this.makeTree('def');
  42. const root = correctMerkleTree.root;
  43. const hash = correctMerkleTree.at(0);
  44. const proof = otherMerkleTree.getProof(0);
  45. expect(await this.mock.$processProof(proof, hash)).to.not.equal(root);
  46. expect(await this.mock.$processProofCalldata(proof, hash)).to.not.equal(root);
  47. expect(await this.mock.$verify(proof, root, hash)).to.be.false;
  48. expect(await this.mock.$verifyCalldata(proof, root, hash)).to.be.false;
  49. });
  50. it('returns false for a Merkle proof of invalid length', async function () {
  51. const merkleTree = this.makeTree('abc');
  52. const root = merkleTree.root;
  53. const hash = merkleTree.at(0);
  54. const proof = merkleTree.getProof(0);
  55. const badProof = proof.slice(0, -1);
  56. expect(await this.mock.$processProof(badProof, hash)).to.not.equal(root);
  57. expect(await this.mock.$processProofCalldata(badProof, hash)).to.not.equal(root);
  58. expect(await this.mock.$verify(badProof, root, hash)).to.be.false;
  59. expect(await this.mock.$verifyCalldata(badProof, root, hash)).to.be.false;
  60. });
  61. });
  62. describe('multiProofVerify', function () {
  63. it('returns true for a valid Merkle multi proof', async function () {
  64. const merkleTree = this.makeTree('abcdef');
  65. const root = merkleTree.root;
  66. const { proof, proofFlags, leaves } = merkleTree.getMultiProof(toLeaves('bdf'));
  67. const hashes = leaves.map(e => merkleTree.leafHash(e));
  68. expect(await this.mock.$processMultiProof(proof, proofFlags, hashes)).to.equal(root);
  69. expect(await this.mock.$processMultiProofCalldata(proof, proofFlags, hashes)).to.equal(root);
  70. expect(await this.mock.$multiProofVerify(proof, proofFlags, root, hashes)).to.be.true;
  71. expect(await this.mock.$multiProofVerifyCalldata(proof, proofFlags, root, hashes)).to.be.true;
  72. });
  73. it('returns false for an invalid Merkle multi proof', async function () {
  74. const merkleTree = this.makeTree('abcdef');
  75. const otherMerkleTree = this.makeTree('ghi');
  76. const root = merkleTree.root;
  77. const { proof, proofFlags, leaves } = otherMerkleTree.getMultiProof(toLeaves('ghi'));
  78. const hashes = leaves.map(e => merkleTree.leafHash(e));
  79. expect(await this.mock.$processMultiProof(proof, proofFlags, hashes)).to.not.equal(root);
  80. expect(await this.mock.$processMultiProofCalldata(proof, proofFlags, hashes)).to.not.equal(root);
  81. expect(await this.mock.$multiProofVerify(proof, proofFlags, root, hashes)).to.be.false;
  82. expect(await this.mock.$multiProofVerifyCalldata(proof, proofFlags, root, hashes)).to.be.false;
  83. });
  84. it('revert with invalid multi proof #1', async function () {
  85. const merkleTree = this.makeTree('abcd');
  86. const root = merkleTree.root;
  87. const hashA = merkleTree.at(0);
  88. const hashB = merkleTree.at(1);
  89. const hashCD = nodeHash(merkleTree.at(2), merkleTree.at(3));
  90. const hashE = ethers.randomBytes(32); // incorrect (not part of the tree)
  91. const fill = ethers.randomBytes(32);
  92. await expect(
  93. this.mock.$processMultiProof([hashB, fill, hashCD], [false, false, false], [hashA, hashE]),
  94. ).to.be.revertedWithCustomError(this.mock, 'MerkleProofInvalidMultiproof');
  95. await expect(
  96. this.mock.$processMultiProofCalldata([hashB, fill, hashCD], [false, false, false], [hashA, hashE]),
  97. ).to.be.revertedWithCustomError(this.mock, 'MerkleProofInvalidMultiproof');
  98. await expect(
  99. this.mock.$multiProofVerify([hashB, fill, hashCD], [false, false, false], root, [hashA, hashE]),
  100. ).to.be.revertedWithCustomError(this.mock, 'MerkleProofInvalidMultiproof');
  101. await expect(
  102. this.mock.$multiProofVerifyCalldata([hashB, fill, hashCD], [false, false, false], root, [hashA, hashE]),
  103. ).to.be.revertedWithCustomError(this.mock, 'MerkleProofInvalidMultiproof');
  104. });
  105. it('revert with invalid multi proof #2', async function () {
  106. const merkleTree = this.makeTree('abcd');
  107. const root = merkleTree.root;
  108. const hashA = merkleTree.at(0);
  109. const hashB = merkleTree.at(1);
  110. const hashCD = nodeHash(merkleTree.at(2), merkleTree.at(3));
  111. const hashE = ethers.randomBytes(32); // incorrect (not part of the tree)
  112. const fill = ethers.randomBytes(32);
  113. await expect(
  114. this.mock.$processMultiProof([hashB, fill, hashCD], [false, false, false, false], [hashE, hashA]),
  115. ).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
  116. await expect(
  117. this.mock.$processMultiProofCalldata([hashB, fill, hashCD], [false, false, false, false], [hashE, hashA]),
  118. ).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
  119. await expect(
  120. this.mock.$multiProofVerify([hashB, fill, hashCD], [false, false, false, false], root, [hashE, hashA]),
  121. ).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
  122. await expect(
  123. this.mock.$multiProofVerifyCalldata([hashB, fill, hashCD], [false, false, false, false], root, [
  124. hashE,
  125. hashA,
  126. ]),
  127. ).to.be.revertedWithPanic(PANIC_CODES.ARRAY_ACCESS_OUT_OF_BOUNDS);
  128. });
  129. it('limit case: works for tree containing a single leaf', async function () {
  130. const merkleTree = this.makeTree('a');
  131. const root = merkleTree.root;
  132. const { proof, proofFlags, leaves } = merkleTree.getMultiProof(toLeaves('a'));
  133. const hashes = leaves.map(e => merkleTree.leafHash(e));
  134. expect(await this.mock.$processMultiProof(proof, proofFlags, hashes)).to.equal(root);
  135. expect(await this.mock.$processMultiProofCalldata(proof, proofFlags, hashes)).to.equal(root);
  136. expect(await this.mock.$multiProofVerify(proof, proofFlags, root, hashes)).to.be.true;
  137. expect(await this.mock.$multiProofVerifyCalldata(proof, proofFlags, root, hashes)).to.be.true;
  138. });
  139. it('limit case: can prove empty leaves', async function () {
  140. const merkleTree = this.makeTree('abcd');
  141. const root = merkleTree.root;
  142. expect(await this.mock.$processMultiProof([root], [], [])).to.equal(root);
  143. expect(await this.mock.$processMultiProofCalldata([root], [], [])).to.equal(root);
  144. expect(await this.mock.$multiProofVerify([root], [], root, [])).to.be.true;
  145. expect(await this.mock.$multiProofVerifyCalldata([root], [], root, [])).to.be.true;
  146. });
  147. it('reverts processing manipulated proofs with a zero-value node at depth 1', async function () {
  148. // Create a merkle tree that contains a zero leaf at depth 1
  149. const leave = ethers.id('real leaf');
  150. const root = nodeHash(leave, ethers.ZeroHash);
  151. // Now we can pass any **malicious** fake leaves as valid!
  152. const maliciousLeaves = ['malicious', 'leaves'].map(ethers.id).map(ethers.toBeArray).sort(Buffer.compare);
  153. const maliciousProof = [leave, leave];
  154. const maliciousProofFlags = [true, true, false];
  155. await expect(
  156. this.mock.$processMultiProof(maliciousProof, maliciousProofFlags, maliciousLeaves),
  157. ).to.be.revertedWithCustomError(this.mock, 'MerkleProofInvalidMultiproof');
  158. await expect(
  159. this.mock.$processMultiProofCalldata(maliciousProof, maliciousProofFlags, maliciousLeaves),
  160. ).to.be.revertedWithCustomError(this.mock, 'MerkleProofInvalidMultiproof');
  161. await expect(
  162. this.mock.$multiProofVerify(maliciousProof, maliciousProofFlags, root, maliciousLeaves),
  163. ).to.be.revertedWithCustomError(this.mock, 'MerkleProofInvalidMultiproof');
  164. await expect(
  165. this.mock.$multiProofVerifyCalldata(maliciousProof, maliciousProofFlags, root, maliciousLeaves),
  166. ).to.be.revertedWithCustomError(this.mock, 'MerkleProofInvalidMultiproof');
  167. });
  168. });
  169. });
  170. }
  171. });