Files
openzeppelin-contracts/test/cryptography/MerkleProof.test.js
Nicolás Venturo 3e82db2f6f Migration to truffle 5 (and web3 1.0 (and BN)) (#1601)
* Now compiling using truffle 5.

* Migrated some test files, missing BN scientific notation usage.

* Now using BN time values.

* Migrate ERC20 tests.

* Migrate all ERC20 tests.

* Migrate utils, payment and ownership tests.

* All tests save ERC721 migrated.

* Migrated ERC721 tests.

* Fix lint errors.

* Delete old test helpers.

* Fix remaining crowdsale tests.

* Fix signature bouncer tests.

* Update how constants is used.

* Compile script pre-removes the build dir.

* Fix SafeMath tests.

* Revert "Compile script pre-removes the build dir."

This reverts commit 247e745113.

* Fix linter errors.

* Upgrade openzeppelin-test-helpers dependency.

* Update openzeppelin-test-helpers dependency.

* Define math constants globally.

* Remove unnecessary ether unit.

* Roll back reduced ether amounts in tests.

* Remove unnecessary toNumber conversions.

* Delete compile script.

* Fixed failing test.
2019-01-14 19:11:55 -03:00

58 lines
1.9 KiB
JavaScript

require('openzeppelin-test-helpers');
const { MerkleTree } = require('../helpers/merkleTree.js');
const { keccak256, bufferToHex } = require('ethereumjs-util');
const MerkleProofWrapper = artifacts.require('MerkleProofWrapper');
contract('MerkleProof', function () {
beforeEach(async function () {
this.merkleProof = await MerkleProofWrapper.new();
});
describe('verify', function () {
it('should return true for a valid Merkle proof', async function () {
const elements = ['a', 'b', 'c', 'd'];
const merkleTree = new MerkleTree(elements);
const root = merkleTree.getHexRoot();
const proof = merkleTree.getHexProof(elements[0]);
const leaf = bufferToHex(keccak256(elements[0]));
(await this.merkleProof.verify(proof, root, leaf)).should.equal(true);
});
it('should return false for an invalid Merkle proof', async function () {
const correctElements = ['a', 'b', 'c'];
const correctMerkleTree = new MerkleTree(correctElements);
const correctRoot = correctMerkleTree.getHexRoot();
const correctLeaf = bufferToHex(keccak256(correctElements[0]));
const badElements = ['d', 'e', 'f'];
const badMerkleTree = new MerkleTree(badElements);
const badProof = badMerkleTree.getHexProof(badElements[0]);
(await this.merkleProof.verify(badProof, correctRoot, correctLeaf)).should.equal(false);
});
it('should return false for a Merkle proof of invalid length', async function () {
const elements = ['a', 'b', 'c'];
const merkleTree = new MerkleTree(elements);
const root = merkleTree.getHexRoot();
const proof = merkleTree.getHexProof(elements[0]);
const badProof = proof.slice(0, proof.length - 5);
const leaf = bufferToHex(keccak256(elements[0]));
(await this.merkleProof.verify(badProof, root, leaf)).should.equal(false);
});
});
});