Merge branch 'williamberman-master'

* williamberman-master:
  changed recursive createHashes into loop
This commit is contained in:
Miguel Mota 2018-08-20 23:12:59 -07:00
commit e681ded3ee
2 changed files with 38 additions and 38 deletions

View File

@ -39,52 +39,53 @@ class MerkleTree {
}
createHashes(nodes) {
if (nodes.length === 1) {
return false
}
const layerIndex = this.layers.length
while (nodes.length !== 1) {
this.layers.push([])
const layerIndex = this.layers.length
for (let i = 0; i < nodes.length - 1; i += 2) {
const left = nodes[i]
const right = nodes[i+1]
let data = null
this.layers.push([])
if (this.isBitcoinTree) {
data = Buffer.concat([reverse(left), reverse(right)])
} else {
data = Buffer.concat([left, right])
for (let i = 0; i < nodes.length - 1; i += 2) {
const left = nodes[i]
const right = nodes[i+1]
let data = null
if (this.isBitcoinTree) {
data = Buffer.concat([reverse(left), reverse(right)])
} else {
data = Buffer.concat([left, right])
}
let hash = this.hashAlgo(data)
// double hash if bitcoin tree
if (this.isBitcoinTree) {
hash = reverse(this.hashAlgo(hash))
}
this.layers[layerIndex].push(hash)
}
let hash = this.hashAlgo(data)
// is odd number of nodes
if (nodes.length % 2 === 1) {
let data = nodes[nodes.length-1]
let hash = data
// double hash if bitcoin tree
if (this.isBitcoinTree) {
hash = reverse(this.hashAlgo(hash))
// is bitcoin tree
if (this.isBitcoinTree) {
// Bitcoin method of duplicating the odd ending nodes
data = Buffer.concat([reverse(data), reverse(data)])
hash = this.hashAlgo(data)
hash = reverse(this.hashAlgo(hash))
}
this.layers[layerIndex].push(hash)
}
this.layers[layerIndex].push(hash)
nodes = this.layers[layerIndex]
}
// is odd number of nodes
if (nodes.length % 2 === 1) {
let data = nodes[nodes.length-1]
let hash = data
// is bitcoin tree
if (this.isBitcoinTree) {
// Bitcoin method of duplicating the odd ending nodes
data = Buffer.concat([reverse(data), reverse(data)])
hash = this.hashAlgo(data)
hash = reverse(this.hashAlgo(hash))
}
this.layers[layerIndex].push(hash)
}
this.createHashes(this.layers[layerIndex])
}
/**

5
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "merkletreejs",
"version": "0.0.7",
"version": "0.0.9",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -347,8 +347,7 @@
"crypto-js": {
"version": "3.1.9-1",
"resolved": "https://registry.npmjs.org/crypto-js/-/crypto-js-3.1.9-1.tgz",
"integrity": "sha1-/aGedh/Ad+Af+/3G6f38WeiAbNg=",
"dev": true
"integrity": "sha1-/aGedh/Ad+Af+/3G6f38WeiAbNg="
},
"decamelize": {
"version": "1.2.0",