diff --git a/.npmignore b/.npmignore deleted file mode 100644 index a6d89df..0000000 --- a/.npmignore +++ /dev/null @@ -1,4 +0,0 @@ -.github -.eslintrc -.editorconfig -test diff --git a/package.json b/package.json index 097e51e..4ac27f4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fixed-merkle-tree", - "version": "0.1.0", + "version": "0.2.0", "description": "Fixed depth merkle tree implementation with sequential inserts", "main": "src/merkleTree.js", "scripts": { @@ -10,6 +10,7 @@ "keywords": ["merkle", "tree", "merkleTree"], "author": "Roman Semenov ", "license": "ISC", + "files": ["src/*"], "dependencies": { "snarkjs": "git+https://github.com/tornadocash/snarkjs.git#869181cfaf7526fe8972073d31655493a04326d5", "circomlib": "git+https://github.com/tornadocash/circomlib.git#c372f14d324d57339c88451834bf2824e73bbdbc" diff --git a/src/merkleTree.js b/src/merkleTree.js index e37a7f7..9aaf508 100644 --- a/src/merkleTree.js +++ b/src/merkleTree.js @@ -18,10 +18,11 @@ class MerkleTree { * Constructor * @param {number} levels Number of levels in the tree * @param {Array} [elements] Initial elements - * @param [zeroElement] Value for non-existent leaves - * @param {hashFunction} [hashFunction] Function used to hash 2 leaves + * @param {Object} options + * @param {hashFunction} [options.hashFunction] Function used to hash 2 leaves + * @param [options.zeroElement] Value for non-existent leaves */ - constructor(levels, elements = [], zeroElement = DEFAULT_ZERO, hashFunction) { + constructor(levels, elements = [], { hashFunction, zeroElement = DEFAULT_ZERO } = {}) { this.levels = levels this.capacity = 2 << levels this.zeroElement = zeroElement @@ -113,9 +114,9 @@ class MerkleTree { throw new Error('Index out of bounds: ' + index) } const pathElements = [] - const pathIndex = [] + const pathIndices = [] for (let level = 0; level < this.levels; level++) { - pathIndex[level] = index % 2 + pathIndices[level] = index % 2 pathElements[level] = (index ^ 1) < this._layers[level].length ? this._layers[level][index ^ 1] : this._zeros[level] @@ -123,7 +124,7 @@ class MerkleTree { } return { pathElements, - pathIndex, + pathIndices, } } @@ -135,6 +136,14 @@ class MerkleTree { indexOf(element) { return this._layers[0].indexOf(element) } + + /** + * Returns a copy of non-zero tree elements + * @returns {Object[]} + */ + elements() { + return this._layers[0].slice() + } } module.exports = MerkleTree diff --git a/test/merkleTree.test.js b/test/merkleTree.test.js index e2e54d1..63ec932 100644 --- a/test/merkleTree.test.js +++ b/test/merkleTree.test.js @@ -89,7 +89,7 @@ describe('MerkleTree', () => { it('should work for even index', () => { const tree = new MerkleTree(10, [1, 2, 3, 4, 5]) const proof = tree.proof(2) - proof.pathIndex.should.be.deep.equal([0, 1, 0, 0, 0, 0, 0, 0, 0, 0]) + proof.pathIndices.should.be.deep.equal([0, 1, 0, 0, 0, 0, 0, 0, 0, 0]) proof.pathElements.should.be.deep.equal([ 4, '19814528709687996974327303300007262407299502847885145507292406548098437687919', @@ -107,7 +107,7 @@ describe('MerkleTree', () => { it('should work for odd index', () => { const tree = new MerkleTree(10, [1, 2, 3, 4, 5]) const proof = tree.proof(3) - proof.pathIndex.should.be.deep.equal([1, 1, 0, 0, 0, 0, 0, 0, 0, 0]) + proof.pathIndices.should.be.deep.equal([1, 1, 0, 0, 0, 0, 0, 0, 0, 0]) proof.pathElements.should.be.deep.equal([ 3, '19814528709687996974327303300007262407299502847885145507292406548098437687919',