diff --git a/src/block.js b/src/block.js index 5181265..e6d2365 100644 --- a/src/block.js +++ b/src/block.js @@ -116,15 +116,20 @@ Block.prototype.toHex = function (headersOnly) { return this.toBuffer(headersOnly).toString('hex') } -var ZEROS = '0000000000000000000000000000000000000000000000000000000000000000' - Block.calculateTarget = function (bits) { var exponent = ((bits & 0xff000000) >> 24) - 3 - var mantissa = bits & 0x00ffffff - var target = mantissa * Math.pow(2, 8 * exponent) - var targetHex = Math.floor(target).toString(16) + var mantissa = bits & 0x007fffff + var i = 31 - exponent + + var target = new Buffer(32) + target.fill(0) + + target[i] = mantissa & 0xff + target[i - 1] = mantissa >> 8 + target[i - 2] = mantissa >> 16 + target[i - 3] = mantissa >> 24 - return new Buffer(ZEROS.slice(0, 64 - targetHex.length) + targetHex, 'hex') + return target } Block.prototype.verifyPow = function () { diff --git a/test/fixtures/block.json b/test/fixtures/block.json index abd7fbc..a18fca5 100644 --- a/test/fixtures/block.json +++ b/test/fixtures/block.json @@ -6,7 +6,7 @@ }, { "bits": "1b80ffff", - "expected": "000000000080ffff000000000000000000000000000000000000000000000000" + "expected": "000000000000ffff000000000000000000000000000000000000000000000000" }, { "bits": "1b0404cb", @@ -15,6 +15,10 @@ { "bits": "1814dd04", "expected": "000000000000000014dd04000000000000000000000000000000000000000000" + }, + { + "bits": "cffca00", + "expected": "00000000000000000000000000000000000000007fca00000000000000000000" } ], "valid": [