Check for consitency with block header argument hash and calculated hash.

This commit is contained in:
Braydon Fuller 2015-08-12 20:45:57 -04:00
parent e7c7a9ad89
commit 496edf9109
2 changed files with 31 additions and 1 deletions

View File

@ -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,

View File

@ -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() {