Remove merkleTree.js in favor of merkletreejs dependency (#2578)

This commit is contained in:
Francisco Giordano
2021-03-10 14:30:16 -03:00
committed by GitHub
parent 5171e46c47
commit 508a879ef0
4 changed files with 187 additions and 150 deletions

View File

@ -1,7 +1,7 @@
require('@openzeppelin/test-helpers');
const { MerkleTree } = require('../../helpers/merkleTree.js');
const { keccakFromString, bufferToHex } = require('ethereumjs-util');
const { MerkleTree } = require('merkletreejs');
const keccak256 = require('keccak256');
const { expect } = require('chai');
@ -15,43 +15,43 @@ contract('MerkleProof', function (accounts) {
describe('verify', function () {
it('returns true for a valid Merkle proof', async function () {
const elements = ['a', 'b', 'c', 'd'];
const merkleTree = new MerkleTree(elements);
const merkleTree = new MerkleTree(elements, keccak256, { hashLeaves: true });
const root = merkleTree.getHexRoot();
const proof = merkleTree.getHexProof(elements[0]);
const leaf = keccak256(elements[0]);
const leaf = bufferToHex(keccakFromString(elements[0]));
const proof = merkleTree.getHexProof(leaf);
expect(await this.merkleProof.verify(proof, root, leaf)).to.equal(true);
});
it('returns false for an invalid Merkle proof', async function () {
const correctElements = ['a', 'b', 'c'];
const correctMerkleTree = new MerkleTree(correctElements);
const correctMerkleTree = new MerkleTree(correctElements, keccak256, { hashLeaves: true });
const correctRoot = correctMerkleTree.getHexRoot();
const correctLeaf = bufferToHex(keccakFromString(correctElements[0]));
const correctLeaf = keccak256(correctElements[0]);
const badElements = ['d', 'e', 'f'];
const badMerkleTree = new MerkleTree(badElements);
const badProof = badMerkleTree.getHexProof(badElements[0]);
const badProof = badMerkleTree.getHexProof(badElements[0], keccak256, { hashLeaves: true });
expect(await this.merkleProof.verify(badProof, correctRoot, correctLeaf)).to.equal(false);
});
it('returns false for a Merkle proof of invalid length', async function () {
const elements = ['a', 'b', 'c'];
const merkleTree = new MerkleTree(elements);
const merkleTree = new MerkleTree(elements, keccak256, { hashLeaves: true });
const root = merkleTree.getHexRoot();
const proof = merkleTree.getHexProof(elements[0]);
const badProof = proof.slice(0, proof.length - 5);
const leaf = keccak256(elements[0]);
const leaf = bufferToHex(keccakFromString(elements[0]));
const proof = merkleTree.getHexProof(leaf);
const badProof = proof.slice(0, proof.length - 5);
expect(await this.merkleProof.verify(badProof, root, leaf)).to.equal(false);
});