From 26ccb43047ada05e7fc3a5a4e1ca71352837796f Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 8 Dec 2015 18:51:35 +1100 Subject: [PATCH] block: add Block.prototype.verifyPow --- src/block.js | 11 +++++++++++ test/block.js | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/block.js b/src/block.js index b11dc37..bc2b531 100644 --- a/src/block.js +++ b/src/block.js @@ -1,3 +1,4 @@ +var BigInteger = require('bigi') var bufferutils = require('./bufferutils') var bcrypto = require('./crypto') @@ -115,4 +116,14 @@ Block.prototype.toHex = function (headersOnly) { return this.toBuffer(headersOnly).toString('hex') } +Block.prototype.verifyPow = function () { + var hash = BigInteger.fromBuffer([].reverse.call(this.getHash())) + var mov = ((this.bits >>> 24) - 3) << 3 + var target = new BigInteger() + target.fromInt(this.bits & 0x000ffffff) + target = target.shiftLeft(mov) + + return hash.compareTo(target) <= 0 +} + module.exports = Block diff --git a/test/block.js b/test/block.js index f65da64..49db52f 100644 --- a/test/block.js +++ b/test/block.js @@ -87,4 +87,18 @@ describe('Block', function () { }) }) }) + + describe('verifyPow', function () { + fixtures.valid.forEach(function (f) { + var block + + beforeEach(function () { + block = Block.fromHex(f.hex) + }) + + it('returns ' + f.valid + ' for ' + f.id, function () { + assert.strictEqual(block.verifyPow(), f.valid) + }) + }) + }) })