From 496edf91097f27fb9492fb9bb20a5fd16d72dc49 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 12 Aug 2015 20:45:57 -0400 Subject: [PATCH] Check for consitency with block header argument hash and calculated hash. --- lib/block/blockheader.js | 18 +++++++++++++++++- test/block/blockheader.js | 14 ++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/block/blockheader.js b/lib/block/blockheader.js index cec95bb..eb64a39 100644 --- a/lib/block/blockheader.js +++ b/lib/block/blockheader.js @@ -21,7 +21,22 @@ var BlockHeader = function BlockHeader(arg) { if (!(this instanceof BlockHeader)) { return new BlockHeader(arg); } - _.extend(this, BlockHeader._from(arg)); + var info = BlockHeader._from(arg); + this.version = info.version; + this.prevHash = info.prevHash; + this.merkleRoot = info.merkleRoot; + this.time = info.time; + this.timestamp = info.time; + this.bits = info.bits; + this.nonce = info.nonce; + + if (info.hash) { + $.checkState( + this.hash === info.hash, + 'Argument object hash property does not match block hash.' + ); + } + return this; }; @@ -72,6 +87,7 @@ BlockHeader._fromObject = function _fromObject(data) { merkleRoot = BufferUtil.reverse(new Buffer(data.merkleRoot, 'hex')); } var info = { + hash: data.hash, version: data.version, prevHash: prevHash, merkleRoot: merkleRoot, diff --git a/test/block/blockheader.js b/test/block/blockheader.js index a35ce64..58b8be7 100644 --- a/test/block/blockheader.js +++ b/test/block/blockheader.js @@ -63,6 +63,20 @@ describe('BlockHeader', function() { should.exist(bh.nonce); }); + it('will throw an error if the argument object hash property doesn\'t match', function() { + (function() { + var bh = new BlockHeader({ + hash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f', + version: version, + prevHash: prevblockidbuf, + merkleRoot: merklerootbuf, + time: time, + bits: bits, + nonce: nonce + }); + }).should.throw('Argument object hash property does not match block hash.'); + }); + }); describe('#fromJSON', function() {