This repository has been archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
merkleMultiProof.test.js
48 lines (39 loc) · 1.84 KB
/
merkleMultiProof.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const MerkleMultiProof = artifacts.require('MerkleMultiProof')
const { MerkleTree } = require('merkletreejs')
const keccak256 = require('keccak256')
contract('MerkleMultiProof', (accounts) => {
let contract
before('setup', async () => {
contract = await MerkleMultiProof.new()
})
context('MerkleMultiProof', () => {
describe('verifyMultiProof()', () => {
it('should verify for valid merkle multiproof (example)', async () => {
const leaves = ['a', 'b', 'c', 'd', 'e', 'f'].map(keccak256).sort(Buffer.compare)
const tree = new MerkleTree(leaves, keccak256, { sort: true })
const root = tree.getRoot()
const proofLeaves = ['b', 'f', 'd'].map(keccak256).sort(Buffer.compare)
const proof = tree.getMultiProof(proofLeaves)
const proofFlags = tree.getProofFlags(proofLeaves, proof)
const verified = await contract.verifyMultiProof.call(root, proofLeaves, proof, proofFlags)
assert.equal(verified, true)
})
it('should not verify for invalid merkle multiproof', async () => {
const leaves = ['a', 'b', 'c', 'd', 'e', 'f'].map(keccak256).sort(Buffer.compare)
const tree = new MerkleTree(leaves, keccak256, { sort: true })
const root = tree.getRoot()
const proofLeaves = ['b', 'f', 'd'].map(keccak256).sort(Buffer.compare)
const proof = tree.getMultiProof(proofLeaves)
const proofFlags = tree.getProofFlags(proofLeaves, proof)
proofFlags[proofFlags.length - 1] = !proofFlags[proofFlags.length - 1]
let errMessage = ''
try {
await contract.verifyMultiProof.call(root, proofLeaves, proof, proofFlags)
} catch (err) {
errMessage = err.message
}
assert.equal(errMessage, 'Returned error: VM Exception while processing transaction: invalid opcode')
})
})
})
})