Merge branch 'friedger-feat/test'
This commit is contained in:
commit
9351d56554
|
@ -396,32 +396,15 @@ export class MerkleTree extends Base {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.isBitcoinTree && index === (this.leaves.length - 1)) {
|
|
||||||
// Proof Generation for Bitcoin Trees
|
|
||||||
|
|
||||||
for (let i = 0; i < this.layers.length - 1; i++) {
|
|
||||||
const layer = this.layers[i]
|
|
||||||
const isRightNode = index % 2
|
|
||||||
const pairIndex = (isRightNode ? index - 1 : index)
|
|
||||||
|
|
||||||
if (pairIndex < layer.length) {
|
|
||||||
proof.push({
|
|
||||||
data: layer[pairIndex]
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// set index to parent index
|
|
||||||
index = (index / 2) | 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return proof
|
|
||||||
} else {
|
|
||||||
// Proof Generation for Non-Bitcoin Trees
|
|
||||||
|
|
||||||
for (let i = 0; i < this.layers.length; i++) {
|
for (let i = 0; i < this.layers.length; i++) {
|
||||||
const layer = this.layers[i]
|
const layer = this.layers[i]
|
||||||
const isRightNode = index % 2
|
const isRightNode = index % 2
|
||||||
const pairIndex = (isRightNode ? index - 1 : index + 1)
|
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)
|
||||||
|
|
||||||
if (pairIndex < layer.length) {
|
if (pairIndex < layer.length) {
|
||||||
proof.push({
|
proof.push({
|
||||||
|
@ -436,7 +419,6 @@ export class MerkleTree extends Base {
|
||||||
|
|
||||||
return proof
|
return proof
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* getHexProof
|
* getHexProof
|
||||||
|
|
|
@ -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