Browse Source

block: use merkle-lib instead of inline calculation

hk-custom-address
Daniel Cousens 8 years ago
committed by Daniel Cousens
parent
commit
dd4596a44a
  1. 1
      package.json
  2. 24
      src/block.js

1
package.json

@ -57,6 +57,7 @@
"create-hash": "^1.1.0",
"create-hmac": "^1.1.3",
"ecurve": "^1.0.0",
"merkle-lib": "^1.0.0",
"randombytes": "^2.0.1",
"typeforce": "^1.8.7",
"varuint-bitcoin": "^1.0.4",

24
src/block.js

@ -1,7 +1,7 @@
var createHash = require('create-hash')
var bufferutils = require('./bufferutils')
var bcrypto = require('./crypto')
var bufferReverse = require('buffer-reverse')
var fastMerkleRoot = require('merkle-lib/fastRoot')
var Transaction = require('./transaction')
@ -134,25 +134,13 @@ Block.calculateTarget = function (bits) {
}
Block.calculateMerkleRoot = function (transactions) {
var length = transactions.length
if (length === 0) throw TypeError('Cannot compute merkle root for zero transactions')
if (transactions.length === 0) throw TypeError('Cannot compute merkle root for zero transactions')
var hashes = transactions.map(function (transaction) { return transaction.getHash() })
while (length > 1) {
var j = 0
for (var i = 0; i < length; i += 2, ++j) {
var hasher = createHash('sha256')
hasher.update(hashes[i])
hasher.update(i + 1 !== length ? hashes[i + 1] : hashes[i])
hashes[j] = bcrypto.sha256(hasher.digest())
}
length = j
}
var hashes = transactions.map(function (transaction) {
return transaction.getHash()
})
return hashes[0]
return fastMerkleRoot(hashes, bcrypto.hash256)
}
Block.prototype.checkMerkleRoot = function () {

Loading…
Cancel
Save