add .hash() function for a block

This is the plain old hash, which is a double sha256. The id of a block is the
reverse of this.
This commit is contained in:
Ryan X. Charles 2014-09-19 17:29:40 -07:00
parent aff3992ffb
commit cc3196085f
2 changed files with 15 additions and 1 deletions

View File

@ -94,8 +94,12 @@ Block.prototype.toBufferWriter = function(bw) {
return bw;
};
Block.prototype.hash = function() {
return Hash.sha256sha256(this.blockheader.toBuffer());
};
Block.prototype.id = function() {
return BufferReader(Hash.sha256sha256(this.blockheader.toBuffer())).reverse().read();
return BufferReader(this.hash()).reverse().read();
};
module.exports = Block;

View File

@ -127,6 +127,16 @@ describe('Block', function() {
});
describe('#hash', function() {
it('should return the correct hash of the genesis block', function() {
var block = Block().fromBuffer(genesisbuf);
var blockhash = new Buffer(Array.apply([], new Buffer(genesisidhex, 'hex')).reverse());
block.hash().toString('hex').should.equal(blockhash.toString('hex'));
});
});
describe('#id', function() {
it('should return the correct id of the genesis block', function() {