Blockheader
This commit is contained in:
parent
7ea66e032f
commit
0d180810de
|
@ -0,0 +1,62 @@
|
||||||
|
var BufferReader = require('./bufferreader');
|
||||||
|
var BufferWriter = require('./bufferwriter');
|
||||||
|
|
||||||
|
var Blockheader = function Blockheader(version, prevblockidbuf, merklerootbuf, time, bits, nonce) {
|
||||||
|
if (!(this instanceof Blockheader))
|
||||||
|
return new Blockheader(version, prevblockidbuf, merklerootbuf, time, bits, nonce);
|
||||||
|
if (typeof version === 'number') {
|
||||||
|
this.set({
|
||||||
|
version: version,
|
||||||
|
prevblockidbuf: prevblockidbuf,
|
||||||
|
merklerootbuf: merklerootbuf,
|
||||||
|
time: time,
|
||||||
|
bits: bits,
|
||||||
|
nonce: nonce
|
||||||
|
});
|
||||||
|
} else if (version) {
|
||||||
|
var obj = version;
|
||||||
|
this.set(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Blockheader.prototype.set = function(obj) {
|
||||||
|
this.version = typeof obj.version !== 'undefined' ? obj.version : this.version;
|
||||||
|
this.prevblockidbuf = obj.prevblockidbuf || this.prevblockidbuf;
|
||||||
|
this.merklerootbuf = obj.merklerootbuf || this.merklerootbuf;
|
||||||
|
this.time = typeof obj.time !== 'undefined' ? obj.time : this.time;
|
||||||
|
this.bits = typeof obj.bits !== 'undefined' ? obj.bits : this.bits;
|
||||||
|
this.nonce = typeof obj.nonce !== 'undefined' ? obj.nonce : this.nonce;
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
Blockheader.prototype.fromBuffer = function(buf) {
|
||||||
|
return this.fromBufferReader(BufferReader(buf));
|
||||||
|
};
|
||||||
|
|
||||||
|
Blockheader.prototype.fromBufferReader = function(br) {
|
||||||
|
this.version = br.readUInt32LE();
|
||||||
|
this.prevblockidbuf = br.buffer(32);
|
||||||
|
this.merklerootbuf = br.buffer(32);
|
||||||
|
this.time = br.readUInt32LE();
|
||||||
|
this.bits = br.readUInt32LE();
|
||||||
|
this.nonce = br.readUInt32LE();
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
|
Blockheader.prototype.toBuffer = function() {
|
||||||
|
return this.toBufferWriter().concat();
|
||||||
|
};
|
||||||
|
|
||||||
|
Blockheader.prototype.toBufferWriter = function(bw) {
|
||||||
|
if (!bw)
|
||||||
|
bw = new BufferWriter();
|
||||||
|
bw.writeUInt32LE(this.version);
|
||||||
|
bw.write(this.prevblockidbuf);
|
||||||
|
bw.write(this.merklerootbuf);
|
||||||
|
bw.writeUInt32LE(this.time);
|
||||||
|
bw.writeUInt32LE(this.bits);
|
||||||
|
bw.writeUInt32LE(this.nonce);
|
||||||
|
return bw;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = Blockheader;
|
|
@ -0,0 +1,88 @@
|
||||||
|
var Blockheader = require('../lib/blockheader');
|
||||||
|
var BufferWriter = require('../lib/bufferwriter');
|
||||||
|
var BufferReader = require('../lib/bufferreader');
|
||||||
|
var should = require('chai').should();
|
||||||
|
|
||||||
|
describe('Blockheader', function() {
|
||||||
|
|
||||||
|
it('should make a new blockheader', function() {
|
||||||
|
var blockheader = new Blockheader();
|
||||||
|
should.exist(blockheader);
|
||||||
|
blockheader = Blockheader();
|
||||||
|
should.exist(blockheader);
|
||||||
|
});
|
||||||
|
|
||||||
|
var bh = new Blockheader();
|
||||||
|
var version = 1;
|
||||||
|
var prevblockidbuf = new Buffer(32);
|
||||||
|
prevblockidbuf.fill(5);
|
||||||
|
var merklerootbuf = new Buffer(32);
|
||||||
|
merklerootbuf.fill(9);
|
||||||
|
var time = 2;
|
||||||
|
var bits = 3;
|
||||||
|
var nonce = 4;
|
||||||
|
bh.set({
|
||||||
|
version: version,
|
||||||
|
prevblockidbuf: prevblockidbuf,
|
||||||
|
merklerootbuf: merklerootbuf,
|
||||||
|
time: time,
|
||||||
|
bits: bits,
|
||||||
|
nonce: nonce
|
||||||
|
});
|
||||||
|
bhhex = '0100000005050505050505050505050505050505050505050505050505050505050505050909090909090909090909090909090909090909090909090909090909090909020000000300000004000000';
|
||||||
|
bhbuf = new Buffer(bhhex, 'hex');
|
||||||
|
|
||||||
|
describe('#set', function() {
|
||||||
|
|
||||||
|
it('should set all the variables', function() {
|
||||||
|
bh.set({
|
||||||
|
version: version,
|
||||||
|
prevblockidbuf: prevblockidbuf,
|
||||||
|
merklerootbuf: merklerootbuf,
|
||||||
|
time: time,
|
||||||
|
bits: bits,
|
||||||
|
nonce: nonce
|
||||||
|
});
|
||||||
|
should.exist(bh.version);
|
||||||
|
should.exist(bh.prevblockidbuf);
|
||||||
|
should.exist(bh.merklerootbuf);
|
||||||
|
should.exist(bh.time);
|
||||||
|
should.exist(bh.bits);
|
||||||
|
should.exist(bh.nonce);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#fromBuffer', function() {
|
||||||
|
|
||||||
|
it('should parse this known buffer', function() {
|
||||||
|
Blockheader().fromBuffer(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#fromBufferReader', function() {
|
||||||
|
|
||||||
|
it('should parse this known buffer', function() {
|
||||||
|
Blockheader().fromBufferReader(BufferReader(bhbuf)).toBuffer().toString('hex').should.equal(bhhex);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#toBuffer', function() {
|
||||||
|
|
||||||
|
it('should output this known buffer', function() {
|
||||||
|
Blockheader().fromBuffer(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#toBufferWriter', function() {
|
||||||
|
|
||||||
|
it('should output this known buffer', function() {
|
||||||
|
Blockheader().fromBuffer(bhbuf).toBufferWriter().concat().toString('hex').should.equal(bhhex);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in New Issue