Update docs

This commit is contained in:
Miguel Mota 2022-11-10 05:28:51 -08:00
parent cf066ac963
commit 8f34b24945
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9
2 changed files with 108 additions and 29 deletions

View File

@ -36,6 +36,7 @@ Class reprensenting a Merkle Tree
* [getHexLeaves](_src_merkletree_.merkletree.md#gethexleaves)
* [getHexMultiProof](_src_merkletree_.merkletree.md#gethexmultiproof)
* [getHexProof](_src_merkletree_.merkletree.md#gethexproof)
* [getHexProofs](_src_merkletree_.merkletree.md#gethexproofs)
* [getHexRoot](_src_merkletree_.merkletree.md#gethexroot)
* [getLayerCount](_src_merkletree_.merkletree.md#getlayercount)
* [getLayers](_src_merkletree_.merkletree.md#getlayers)
@ -50,6 +51,8 @@ Class reprensenting a Merkle Tree
* [getProof](_src_merkletree_.merkletree.md#getproof)
* [getProofFlags](_src_merkletree_.merkletree.md#getproofflags)
* [getProofIndices](_src_merkletree_.merkletree.md#getproofindices)
* [getProofs](_src_merkletree_.merkletree.md#getproofs)
* [getProofsDFS](_src_merkletree_.merkletree.md#getproofsdfs)
* [getRoot](_src_merkletree_.merkletree.md#getroot)
* [isUnevenTree](_src_merkletree_.merkletree.md#isuneventree)
* [print](_src_merkletree_.merkletree.md#print)
@ -468,6 +471,25 @@ Name | Type | Description |
___
### getHexProofs
**getHexProofs**(): *string[]*
getHexProofs
**`desc`** Returns the proofs for all leaves as hex strings.
**`example`**
```js
const proofs = tree.getHexProofs()
```
**Returns:** *string[]*
- Proofs array as hex strings.
___
### getHexRoot
**getHexRoot**(): *string*
@ -783,6 +805,63 @@ Name | Type | Description |
___
### getProofs
**getProofs**(): *any[]*
getProofs
**`desc`** Returns the proofs for all leaves.
**`example`**
```js
const proofs = tree.getProofs()
```
**`example`**
```js
const leaves = ['a', 'b', 'a'].map(value => keccak(value))
const tree = new MerkleTree(leaves, keccak)
const proofs = tree.getProofs()
```
**Returns:** *any[]*
- Array of objects containing a position property of type string
with values of 'left' or 'right' and a data property of type Buffer for all leaves.
___
### getProofsDFS
**getProofsDFS**(`currentLayer`: any, `index`: any, `proof`: any, `proofs`: any): *any[]*
getProofsDFS
**`desc`** Get all proofs through single traverse
**`example`**
```js
const layers = tree.getLayers()
const index = 0;
let proof = [];
let proofs = [];
const proof = tree.getProofsDFS(layers, index, proof, proofs)
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`currentLayer` | any | Current layer index in traverse. |
`index` | any | Current tarvese node index in traverse. |
`proof` | any | Proof chain for single leaf. |
`proofs` | any | Proofs for all leaves |
**Returns:** *any[]*
___
### getRoot
**getRoot**(): *Buffer*

View File

@ -588,17 +588,17 @@ export class MerkleTree extends Base {
*const proofs = tree.getProofs()
*```
*/
getProofs ():any[] {
let proof = [], proofs = [];
this.getProofsDFS(this.layers.length - 1, 0, proof, proofs);
getProofs ():any[] {
const proof = []; const proofs = []
this.getProofsDFS(this.layers.length - 1, 0, proof, proofs)
return proofs
}
/**
* getProofsDFS
* @desc Get all proofs through single traverse
* @desc Get all proofs through single traverse
* @param {Number} currentLayer - Current layer index in traverse.
* @param {Number} index - Current tarvese node index in traverse.
* @param {Object[]} proof - Proof chain for single leaf.
@ -612,33 +612,33 @@ export class MerkleTree extends Base {
*const proof = tree.getProofsDFS(layers, index, proof, proofs)
*```
*/
getProofsDFS(currentLayer, index, proof, proofs):any[] {
const isRightNode = index % 2;
if (currentLayer == -1) {
if (!isRightNode) proofs.push([...proof].reverse());
return;
getProofsDFS (currentLayer, index, proof, proofs):any[] {
const isRightNode = index % 2
if (currentLayer === -1) {
if (!isRightNode) proofs.push([...proof].reverse())
return
}
if(index >= this.layers[currentLayer].length) return;
const layer = this.layers[currentLayer];
const pairIndex = isRightNode ? index - 1 : index + 1;
let pushed = false;
if (index >= this.layers[currentLayer].length) return
const layer = this.layers[currentLayer]
const pairIndex = isRightNode ? index - 1 : index + 1
let pushed = false
if (pairIndex < layer.length) {
pushed = true;
pushed = true
proof.push({
position: isRightNode ? 'left' : 'right',
data: layer[pairIndex],
});
data: layer[pairIndex]
})
}
let leftchildIndex = index * 2;
let rightchildIndex = index * 2 + 1;
this.getProofsDFS(currentLayer - 1, leftchildIndex, proof, proofs);
this.getProofsDFS(currentLayer - 1, rightchildIndex, proof, proofs);
if (pushed) proof.splice(proof.length - 1, 1);
const leftchildIndex = index * 2
const rightchildIndex = index * 2 + 1
this.getProofsDFS(currentLayer - 1, leftchildIndex, proof, proofs)
this.getProofsDFS(currentLayer - 1, rightchildIndex, proof, proofs)
if (pushed) proof.splice(proof.length - 1, 1)
}
/**
@ -650,7 +650,7 @@ export class MerkleTree extends Base {
*const proofs = tree.getHexProofs()
*```
*/
getHexProofs ():string[] {
getHexProofs ():string[] {
return this.getProofs().map(item => this.bufferToHex(item.data))
}