Merge branch 'add_position_hex_proof' of https://github.com/petscheit/merkletreejs into petscheit-add_position_hex_proof

This commit is contained in:
Miguel Mota 2020-12-07 14:09:58 -08:00
commit a380106a5d
3 changed files with 76 additions and 0 deletions

View File

@ -365,6 +365,32 @@ Name | Type | Description |
___
### getPositionalHexProof
**getPositionalHexProof**(`leaf`: Buffer, `index?`: number): *(string | number)[][]*
getPositionalHexProof
**`desc`** Returns the proof for a target leaf as hex strings and corresponding position as binary.
**`example`**
```js
const proof = tree.getPositionalHexProof(leaves[2])
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`leaf` | Buffer | Target leaf |
`index?` | number | - |
**Returns:** *(string | number)[][]*
- Proof array as hex strings with position.
___
### getHexRoot
**getHexRoot**(): *string*

View File

@ -425,6 +425,27 @@ export class MerkleTree {
return this.getProof(leaf, index).map(x => this.bufferToHex(x.data))
}
/**
* getPositionalHexProof
* @desc Returns the proof for a target leaf as hex strings and the position in binary (left == 0).
* @param {Buffer} leaf - Target leaf
* @param {Number} [index] - Target leaf index in leaves array.
* Use if there are leaves containing duplicate data in order to distinguish it.
* @return {(string | number)[][]} - Proof array as hex strings. position at index 0
* @example
* ```js
*const proof = tree.getPositionalHexProof(leaves[2])
*```
*/
getPositionalHexProof (leaf: Buffer, index?: number): (string | number)[][] {
return this.getProof(leaf, index).map(x => {
return [
x.position === 'left' ? 0 : 1,
this.bufferToHex(x.data)
]
})
}
/**
* marshalProof
* @desc Returns proof array as JSON string.
@ -691,6 +712,9 @@ export class MerkleTree {
if (typeof node === 'string') {
data = this.bufferify(node)
isLeftNode = true
} else if (Array.isArray(node)) {
isLeftNode = (node[0] === 0)
data = this.bufferify(node[1])
} else if (node instanceof Object) {
data = this.bufferify(node.data)
isLeftNode = (node.position === 'left')

View File

@ -88,6 +88,32 @@ test('sha256 with sort pairs option', t => {
t.equal(tree.getRoot().toString('hex'), root)
})
test('sha256 verify with positional hex proof and no pairSort', t => {
t.plan(1)
const leaves = ['a', 'b', 'c', 'd', 'e', 'f'].map(x => sha256(x))
const tree = new MerkleTree(leaves, sha256, { sortPairs: false })
t.true(tree.verify(tree.getPositionalHexProof(leaves[1], 1), leaves[1], tree.getHexRoot()))
})
test('sha256 verify with non-hex proof and no pairSort', t => {
t.plan(1)
const leaves = ['a', 'b', 'c', 'd', 'e', 'f'].map(x => sha256(x))
const tree = new MerkleTree(leaves, sha256, { sortPairs: false })
t.true(tree.verify(tree.getProof(leaves[1], 1), leaves[1], tree.getHexRoot()))
})
test('sha256 verify with hex proof and pairSort', t => {
t.plan(1)
const leaves = ['a', 'b', 'c', 'd', 'e', 'f'].map(x => sha256(x))
const tree = new MerkleTree(leaves, sha256, { sortPairs: true })
t.true(tree.verify(tree.getHexProof(leaves[1], 1), leaves[1], tree.getHexRoot()))
})
test('keccak with sort leaves and sort pairs option', t => {
t.plan(1)