Implement Zcash modifications to block header

This commit is contained in:
Jack Grigg 2016-08-27 23:27:00 +12:00
parent bc8e5f4121
commit 8740d9e964
1 changed files with 28 additions and 4 deletions

View File

@ -27,10 +27,12 @@ var BlockHeader = function BlockHeader(arg) {
this.version = info.version;
this.prevHash = info.prevHash;
this.merkleRoot = info.merkleRoot;
this.reserved = info.reserved;
this.time = info.time;
this.timestamp = info.time;
this.bits = info.bits;
this.nonce = info.nonce;
this.solution = info.solution;
if (info.hash) {
$.checkState(
@ -69,21 +71,35 @@ BlockHeader._fromObject = function _fromObject(data) {
$.checkArgument(data, 'data is required');
var prevHash = data.prevHash;
var merkleRoot = data.merkleRoot;
var reserved = data.reserved;
var nonce = data.nonce;
var solution = data.solution;
if (_.isString(data.prevHash)) {
prevHash = BufferUtil.reverse(new Buffer(data.prevHash, 'hex'));
}
if (_.isString(data.merkleRoot)) {
merkleRoot = BufferUtil.reverse(new Buffer(data.merkleRoot, 'hex'));
}
if (_.isString(data.reserved)) {
reserved = BufferUtil.reverse(new Buffer(data.reserved, 'hex'));
}
if (_.isString(data.nonce)) {
nonce = BufferUtil.reverse(new Buffer(data.nonce, 'hex'));
}
if (_.isString(data.solution)) {
solution = new Buffer(data.solution, 'hex');
}
var info = {
hash: data.hash,
version: data.version,
prevHash: prevHash,
merkleRoot: merkleRoot,
reserved: reserved,
time: data.time,
timestamp: data.time,
bits: data.bits,
nonce: data.nonce
nonce: nonce,
solution: solution
};
return info;
};
@ -139,9 +155,12 @@ BlockHeader._fromBufferReader = function _fromBufferReader(br) {
info.version = br.readUInt32LE();
info.prevHash = br.read(32);
info.merkleRoot = br.read(32);
info.reserved = br.read(32);
info.time = br.readUInt32LE();
info.bits = br.readUInt32LE();
info.nonce = br.readUInt32LE();
info.nonce = br.read(32);
var lenSolution = br.readVarintNum();
info.solution = br.read(lenSolution);
return info;
};
@ -163,9 +182,11 @@ BlockHeader.prototype.toObject = BlockHeader.prototype.toJSON = function toObjec
version: this.version,
prevHash: BufferUtil.reverse(this.prevHash).toString('hex'),
merkleRoot: BufferUtil.reverse(this.merkleRoot).toString('hex'),
reserved: BufferUtil.reverse(this.reserved).toString('hex'),
time: this.time,
bits: this.bits,
nonce: this.nonce
nonce: BufferUtil.reverse(this.nonce).toString('hex'),
solution: this.solution.toString('hex')
};
};
@ -194,9 +215,12 @@ BlockHeader.prototype.toBufferWriter = function toBufferWriter(bw) {
bw.writeUInt32LE(this.version);
bw.write(this.prevHash);
bw.write(this.merkleRoot);
bw.write(this.reserved);
bw.writeUInt32LE(this.time);
bw.writeUInt32LE(this.bits);
bw.writeUInt32LE(this.nonce);
bw.write(this.nonce);
bw.writeVarintNum(this.solution.length);
bw.write(this.solution);
return bw;
};