Merge branch 'friedger-feat/test'
This commit is contained in:
commit
9351d56554
|
@ -396,46 +396,28 @@ export class MerkleTree extends Base {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isBitcoinTree && index === (this.leaves.length - 1)) {
|
for (let i = 0; i < this.layers.length; i++) {
|
||||||
// Proof Generation for Bitcoin Trees
|
const layer = this.layers[i]
|
||||||
|
const isRightNode = index % 2
|
||||||
|
const pairIndex = (isRightNode ? index - 1
|
||||||
|
: this.isBitcoinTree && index === layer.length - 1 && i < this.layers.length - 1
|
||||||
|
// Proof Generation for Bitcoin Trees
|
||||||
|
? index
|
||||||
|
// Proof Generation for Non-Bitcoin Trees
|
||||||
|
: index + 1)
|
||||||
|
|
||||||
for (let i = 0; i < this.layers.length - 1; i++) {
|
if (pairIndex < layer.length) {
|
||||||
const layer = this.layers[i]
|
proof.push({
|
||||||
const isRightNode = index % 2
|
position: isRightNode ? 'left' : 'right',
|
||||||
const pairIndex = (isRightNode ? index - 1 : index)
|
data: layer[pairIndex]
|
||||||
|
})
|
||||||
if (pairIndex < layer.length) {
|
|
||||||
proof.push({
|
|
||||||
data: layer[pairIndex]
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// set index to parent index
|
|
||||||
index = (index / 2) | 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return proof
|
// set index to parent index
|
||||||
} else {
|
index = (index / 2) | 0
|
||||||
// Proof Generation for Non-Bitcoin Trees
|
|
||||||
|
|
||||||
for (let i = 0; i < this.layers.length; i++) {
|
|
||||||
const layer = this.layers[i]
|
|
||||||
const isRightNode = index % 2
|
|
||||||
const pairIndex = (isRightNode ? index - 1 : index + 1)
|
|
||||||
|
|
||||||
if (pairIndex < layer.length) {
|
|
||||||
proof.push({
|
|
||||||
position: isRightNode ? 'left' : 'right',
|
|
||||||
data: layer[pairIndex]
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// set index to parent index
|
|
||||||
index = (index / 2) | 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return proof
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return proof
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -351,7 +351,7 @@ test('solidity keccak256 with duplicate leaves', t => {
|
||||||
})
|
})
|
||||||
|
|
||||||
test('sha-256 with option.isBitcoinTree', t => {
|
test('sha-256 with option.isBitcoinTree', t => {
|
||||||
t.plan(2)
|
t.plan(100)
|
||||||
|
|
||||||
/* Derived from:
|
/* Derived from:
|
||||||
* http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html
|
* http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html
|
||||||
|
@ -464,9 +464,10 @@ test('sha-256 with option.isBitcoinTree', t => {
|
||||||
const root = Buffer.from('871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a', 'hex')
|
const root = Buffer.from('871714dcbae6c8193a2bb9b2a69fe1c0440399f38d94b3a0f1b447275a29978a', 'hex')
|
||||||
t.equal(tree.getRoot().toString('hex'), root.toString('hex'))
|
t.equal(tree.getRoot().toString('hex'), root.toString('hex'))
|
||||||
|
|
||||||
const proof_0 = tree.getProof(leaves[0])
|
for (let i = 0; i < leaves.length; i++) {
|
||||||
|
const proof_0 = tree.getProof(leaves[i])
|
||||||
t.true(tree.verify(proof_0, leaves[0], root))
|
t.true(tree.verify(proof_0, leaves[i], root), 'proof verification for ' + i)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
test('keccak256 - hex strings', t => {
|
test('keccak256 - hex strings', t => {
|
||||||
|
|
Loading…
Reference in New Issue