mirror of
https://github.com/tornadocash/fixed-merkle-tree.git
synced 2024-12-27 15:17:56 +01:00
add getTreeEdge method
This commit is contained in:
parent
4501cae4d6
commit
3d6356b596
31
src/index.ts
31
src/index.ts
@ -130,15 +130,23 @@ export default class MerkleTree {
|
||||
let elIndex = +index
|
||||
const pathElements: Element[] = []
|
||||
const pathIndices: number[] = []
|
||||
const layerIndices: number[] = []
|
||||
for (let level = 0; level < this.levels; level++) {
|
||||
pathIndices[level] = elIndex % 2
|
||||
pathElements[level] =
|
||||
(elIndex ^ 1) < this._layers[level].length ? this._layers[level][elIndex ^ 1] : this._zeros[level]
|
||||
const leafIndex = elIndex ^ 1
|
||||
if (leafIndex < this._layers[level].length) {
|
||||
pathElements[level] = this._layers[level][leafIndex]
|
||||
layerIndices[level] = leafIndex
|
||||
} else {
|
||||
pathElements[level] = this._zeros[level]
|
||||
layerIndices[level] = 0
|
||||
}
|
||||
elIndex >>= 1
|
||||
}
|
||||
return {
|
||||
pathElements,
|
||||
pathIndices,
|
||||
layerIndices,
|
||||
}
|
||||
}
|
||||
|
||||
@ -156,6 +164,25 @@ export default class MerkleTree {
|
||||
}
|
||||
}
|
||||
|
||||
getTreeEdge(edgeElement: Element, index?: number) {
|
||||
if (edgeElement === 'undefined') {
|
||||
throw new Error('element is required')
|
||||
}
|
||||
let edgeIndex: number
|
||||
if (!Number.isInteger(index)) {
|
||||
index = -1
|
||||
const leaves = this._layers[0]
|
||||
index = leaves.indexOf(edgeElement)
|
||||
edgeIndex = index
|
||||
}
|
||||
|
||||
if (index <= -1) {
|
||||
return []
|
||||
}
|
||||
const edgePath = this.path(index)
|
||||
return { edgePath, edgeElement, edgeIndex }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a copy of non-zero tree elements.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user