mirror of
https://github.com/tornadocash/fixed-merkle-tree.git
synced 2024-11-21 17:27:08 +01:00
add getters
This commit is contained in:
parent
3d6356b596
commit
107217f74d
37
src/index.ts
37
src/index.ts
@ -6,6 +6,14 @@ const DEFAULT_ZERO = '2166383900441693294538235590879059922526650182290791145750
|
||||
const defaultHash = (left: Element, right: Element): string => mimcsponge.multiHash([BigInt(left), BigInt(right)]).toString()
|
||||
|
||||
export default class MerkleTree {
|
||||
get layers(): Array<Element[]> {
|
||||
return this._layers.slice()
|
||||
}
|
||||
|
||||
set layers(value: Array<Element[]>) {
|
||||
this._layers = value
|
||||
}
|
||||
|
||||
levels: number
|
||||
capacity: number
|
||||
private _hash: HashFunction
|
||||
@ -186,17 +194,42 @@ export default class MerkleTree {
|
||||
/**
|
||||
* Returns a copy of non-zero tree elements.
|
||||
*/
|
||||
elements() {
|
||||
get elements() {
|
||||
return this._layers[0].slice()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of n-th zero elements array
|
||||
*/
|
||||
zeros() {
|
||||
get zeros() {
|
||||
return this._zeros.slice()
|
||||
}
|
||||
|
||||
getLayersAsObject() {
|
||||
const layers = this.layers
|
||||
const objs = []
|
||||
for (let i = 0; i < this.levels; i++) {
|
||||
const arr = []
|
||||
for (let j = 0; j < layers[i].length; j++) {
|
||||
const obj = { [layers[i][j]]: null }
|
||||
if (objs.length) {
|
||||
obj[layers[i][j]] = {}
|
||||
const a = objs.shift()
|
||||
const akey = Object.keys(a)[0]
|
||||
obj[layers[i][j]][akey] = a[akey]
|
||||
if (objs.length) {
|
||||
const b = objs.shift()
|
||||
const bkey = Object.keys(b)[0]
|
||||
obj[layers[i][j]][bkey] = b[bkey]
|
||||
}
|
||||
}
|
||||
arr.push(obj)
|
||||
}
|
||||
objs.push(...arr)
|
||||
}
|
||||
return objs[0]
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize entire tree state including intermediate layers into a plain object
|
||||
* Deserializing it back will not require to recompute any hashes
|
||||
|
@ -1,5 +1,6 @@
|
||||
import MerkleTree from '../src'
|
||||
import { assert, should } from 'chai'
|
||||
import { it } from 'mocha'
|
||||
|
||||
describe('MerkleTree', () => {
|
||||
|
||||
@ -232,6 +233,48 @@ describe('MerkleTree', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('#getters', () => {
|
||||
const elements = [1, 2, 3, 4, 5]
|
||||
const layers = [
|
||||
[1, 2, 3, 4, 5],
|
||||
[
|
||||
'19814528709687996974327303300007262407299502847885145507292406548098437687919',
|
||||
'9256022917525827637821171443533757190340579068025270193352322268529570863974',
|
||||
'21652272025144185891702495507858700052653521882982711576347377471507927142323',
|
||||
],
|
||||
[
|
||||
'6464294476958346139385024074008223400825166653076969388043746597957512245037',
|
||||
'21305827034995891902714687670641862055126514524916463201449278400604999416145',
|
||||
],
|
||||
['13482485030738390475684870940688026655293260583748749623167469686138055064771'],
|
||||
['5734482689596254215043546232260442114954448597263096309106433719494772338840'],
|
||||
]
|
||||
|
||||
it('should return same elements in array', () => {
|
||||
const tree = new MerkleTree(10, elements)
|
||||
assert.deepEqual(tree.elements, elements)
|
||||
})
|
||||
it('should return copy of elements array', () => {
|
||||
const tree = new MerkleTree(10, elements)
|
||||
const elements1 = tree.elements
|
||||
tree.insert(6)
|
||||
const elements2 = tree.elements
|
||||
should().not.equal(elements1, elements2)
|
||||
})
|
||||
|
||||
it('should return same layers in array', () => {
|
||||
const tree = new MerkleTree(4, elements)
|
||||
assert.deepEqual(tree.layers, layers)
|
||||
})
|
||||
it('should return copy of elements array', () => {
|
||||
const tree = new MerkleTree(4, elements)
|
||||
const layers1 = tree.layers
|
||||
tree.insert(6)
|
||||
const layers2 = tree.layers
|
||||
should().not.equal(layers1, layers2)
|
||||
})
|
||||
})
|
||||
|
||||
describe('#serialize', () => {
|
||||
it('should work', () => {
|
||||
const src = new MerkleTree(10, [1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
|
Loading…
Reference in New Issue
Block a user