Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c79653ccba | |||
|
|
96e254a46b | ||
|
|
53cb08d9b9 | ||
|
|
0eb38e0b2d | ||
|
|
1bc8dfa338 | ||
|
|
3d95212b1d | ||
|
|
523094bdc8 |
@@ -1,4 +1,4 @@
|
||||
name: Node.js CI
|
||||
name: build
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
@@ -16,6 +16,14 @@ jobs:
|
||||
node-version: 14.7.0
|
||||
- run: npm ci
|
||||
- run: npm run test
|
||||
- name: Telegram Failure Notification
|
||||
uses: appleboy/telegram-action@0.0.7
|
||||
if: failure()
|
||||
with:
|
||||
message: ❗ Build failed for [${{ github.repository }}](https://github.com/${{ github.repository }}/actions) because of ${{ github.actor }}
|
||||
format: markdown
|
||||
to: ${{ secrets.TELEGRAM_CHAT_ID }}
|
||||
token: ${{ secrets.TELEGRAM_BOT_TOKEN }}
|
||||
|
||||
lint:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -26,6 +34,14 @@ jobs:
|
||||
node-version: 14.7.0
|
||||
- run: npm ci
|
||||
- run: npm run lint
|
||||
- name: Telegram Failure Notification
|
||||
uses: appleboy/telegram-action@0.0.7
|
||||
if: failure()
|
||||
with:
|
||||
message: ❗ Build failed for [${{ github.repository }}](https://github.com/${{ github.repository }}/actions) because of ${{ github.actor }}
|
||||
format: markdown
|
||||
to: ${{ secrets.TELEGRAM_CHAT_ID }}
|
||||
token: ${{ secrets.TELEGRAM_BOT_TOKEN }}
|
||||
|
||||
publish:
|
||||
runs-on: ubuntu-latest
|
||||
@@ -65,3 +81,12 @@ jobs:
|
||||
message: 🚀 Published [${{ steps.vars.outputs.repo_name }}](https://github.com/${{ github.repository }}) version [${{ steps.vars.outputs.version }}](https://www.npmjs.com/package/${{ steps.vars.outputs.repo_name }}/v/${{ steps.vars.outputs.version }}) to npm
|
||||
debug: true
|
||||
format: markdown
|
||||
|
||||
- name: Telegram Failure Notification
|
||||
uses: appleboy/telegram-action@0.0.7
|
||||
if: failure()
|
||||
with:
|
||||
message: ❗ Failed to publish [${{ steps.vars.outputs.repo_name }}](https://github.com/${{ github.repository }}/actions) because of ${{ env.GITHUB_ACTOR }}
|
||||
format: markdown
|
||||
to: ${{ secrets.TELEGRAM_CHAT_ID }}
|
||||
token: ${{ secrets.TELEGRAM_BOT_TOKEN }}
|
||||
1
.npmrc
Normal file
1
.npmrc
Normal file
@@ -0,0 +1 @@
|
||||
@tornado:registry=https://git.tornado.ws/api/packages/tornado-packages/npm/
|
||||
@@ -1,4 +1,4 @@
|
||||
# Merkle Tree [](https://github.com/tornadocash/fixed-merkle-tree/actions) [](https://www.npmjs.com/package/fixed-merkle-tree)
|
||||
# Merkle Tree [](https://github.com/tornadocash/fixed-merkle-tree/actions) [](https://www.npmjs.com/package/fixed-merkle-tree)
|
||||
|
||||
This is a fixed depth merkle tree implementation with sequential inserts
|
||||
|
||||
|
||||
6847
package-lock.json
generated
6847
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
@@ -1,19 +1,29 @@
|
||||
{
|
||||
"name": "fixed-merkle-tree",
|
||||
"version": "0.3.1",
|
||||
"name": "@tornado/fixed-merkle-tree",
|
||||
"version": "0.4.0",
|
||||
"description": "Fixed depth merkle tree implementation with sequential inserts",
|
||||
"main": "src/merkleTree.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://git.tornado.ws/tornado-packages/fixed-merkle-tree.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha",
|
||||
"lint": "eslint ."
|
||||
},
|
||||
"keywords": ["merkle", "tree", "merkleTree"],
|
||||
"keywords": [
|
||||
"merkle",
|
||||
"tree",
|
||||
"merkleTree"
|
||||
],
|
||||
"author": "Roman Semenov <semenov.roma@gmail.com>",
|
||||
"license": "ISC",
|
||||
"files": ["src/*"],
|
||||
"files": [
|
||||
"src/*"
|
||||
],
|
||||
"dependencies": {
|
||||
"snarkjs": "git+https://github.com/tornadocash/snarkjs.git#869181cfaf7526fe8972073d31655493a04326d5",
|
||||
"circomlib": "git+https://github.com/tornadocash/circomlib.git#c372f14d324d57339c88451834bf2824e73bbdbc"
|
||||
"@tornado/circomlib": "^0.0.21",
|
||||
"@tornado/snarkjs": "^0.1.20"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-eslint": "^10.1.0",
|
||||
|
||||
@@ -134,11 +134,16 @@ class MerkleTree {
|
||||
/**
|
||||
* Find an element in the tree
|
||||
* @param element An element to find
|
||||
* @param comparator A function that checks leaf value equality
|
||||
* @returns {number} Index if element is found, otherwise -1
|
||||
*/
|
||||
indexOf(element) {
|
||||
indexOf(element, comparator) {
|
||||
if (comparator) {
|
||||
return this._layers[0].findIndex((el) => comparator(element, el))
|
||||
} else {
|
||||
return this._layers[0].indexOf(element)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of non-zero tree elements
|
||||
@@ -147,6 +152,36 @@ class MerkleTree {
|
||||
elements() {
|
||||
return this._layers[0].slice()
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize entire tree state including intermediate layers into a plain object
|
||||
* Deserializing it back will not require to recompute any hashes
|
||||
* Elements are not converted to a plain type, this is responsibility of the caller
|
||||
*/
|
||||
serialize() {
|
||||
return {
|
||||
levels: this.levels,
|
||||
capacity: this.capacity,
|
||||
zeroElement: this.zeroElement,
|
||||
_zeros: this._zeros,
|
||||
_layers: this._layers,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize data into a MerkleTree instance
|
||||
* Make sure to provide the same hashFunction as was used in the source tree,
|
||||
* otherwise the tree state will be invalid
|
||||
*
|
||||
* @param data
|
||||
* @param hashFunction
|
||||
* @returns {MerkleTree}
|
||||
*/
|
||||
static deserialize(data, hashFunction) {
|
||||
const instance = Object.assign(Object.create(this.prototype), data)
|
||||
instance._hash = hashFunction || defaultHash
|
||||
return instance
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MerkleTree
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
const { mimcsponge } = require('circomlib')
|
||||
const { bigInt } = require('snarkjs')
|
||||
module.exports = (left, right) => mimcsponge.multiHash([bigInt(left), bigInt(right)]).toString()
|
||||
const { mimcsponge } = require("@tornado/circomlib");
|
||||
const { bigInt } = require("@tornado/snarkjs");
|
||||
module.exports = (left, right) =>
|
||||
mimcsponge.multiHash([bigInt(left), bigInt(right)]).toString();
|
||||
|
||||
@@ -202,4 +202,19 @@ describe('MerkleTree', () => {
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('#serialize', () => {
|
||||
it('should work', () => {
|
||||
const src = new MerkleTree(10, [1, 2, 3])
|
||||
const data = src.serialize()
|
||||
const dst = MerkleTree.deserialize(data)
|
||||
|
||||
src.root().should.equal(dst.root())
|
||||
|
||||
src.insert(10)
|
||||
dst.insert(10)
|
||||
|
||||
src.root().should.equal(dst.root())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user