mirror of
https://github.com/tornadocash/fixed-merkle-tree.git
synced 2024-12-28 07:37:57 +01:00
serialization
This commit is contained in:
parent
53cb08d9b9
commit
96e254a46b
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "fixed-merkle-tree",
|
||||
"version": "0.3.4",
|
||||
"version": "0.4.0",
|
||||
"description": "Fixed depth merkle tree implementation with sequential inserts",
|
||||
"main": "src/merkleTree.js",
|
||||
"scripts": {
|
||||
|
@ -152,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
|
||||
|
@ -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())
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user