|
|
@ -7,6 +7,7 @@ var BufferReader = require('../encoding/bufferreader'); |
|
|
|
var BufferWriter = require('../encoding/bufferwriter'); |
|
|
|
var Hash = require('../crypto/hash'); |
|
|
|
var JSUtil = require('../util/js'); |
|
|
|
var $ = require('../util/preconditions'); |
|
|
|
|
|
|
|
/** |
|
|
|
* Instantiate a BlockHeader from a Buffer, JSON object, or Object with |
|
|
@ -37,14 +38,7 @@ BlockHeader._from = function _from(arg) { |
|
|
|
} else if (JSUtil.isValidJSON(arg)) { |
|
|
|
info = BlockHeader._fromJSON(arg); |
|
|
|
} else if (_.isObject(arg)) { |
|
|
|
info = { |
|
|
|
version: arg.version, |
|
|
|
prevHash: arg.prevHash, |
|
|
|
merkleRoot: arg.merkleRoot, |
|
|
|
time: arg.time, |
|
|
|
bits: arg.bits, |
|
|
|
nonce: arg.nonce |
|
|
|
}; |
|
|
|
info = BlockHeader._fromObject(arg); |
|
|
|
} else { |
|
|
|
throw new TypeError('Unrecognized argument for BlockHeader'); |
|
|
|
} |
|
|
@ -52,18 +46,35 @@ BlockHeader._from = function _from(arg) { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* @param {String|Object} - A JSON string or object |
|
|
|
* @param {String} - A JSON string |
|
|
|
* @returns {Object} - An object representing block header data |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
BlockHeader._fromJSON = function _fromJSON(data) { |
|
|
|
if (JSUtil.isValidJSON(data)) { |
|
|
|
data = JSON.parse(data); |
|
|
|
$.checkArgument(JSUtil.isValidJSON(data), 'data must be a valid JSON string'); |
|
|
|
data = JSON.parse(data); |
|
|
|
return BlockHeader._fromObject(data); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* @param {Object} - A JSON string |
|
|
|
* @returns {Object} - An object representing block header data |
|
|
|
* @private |
|
|
|
*/ |
|
|
|
BlockHeader._fromObject = function _fromObject(data) { |
|
|
|
$.checkArgument(data, 'data is required'); |
|
|
|
var prevHash = data.prevHash; |
|
|
|
var merkleRoot = data.merkleRoot; |
|
|
|
if (_.isString(data.prevHash)) { |
|
|
|
prevHash = BufferUtil.reverse(new Buffer(data.prevHash, 'hex')); |
|
|
|
} |
|
|
|
if (_.isString(data.merkleRoot)) { |
|
|
|
merkleRoot = BufferUtil.reverse(new Buffer(data.merkleRoot, 'hex')); |
|
|
|
} |
|
|
|
var info = { |
|
|
|
version: data.version, |
|
|
|
prevHash: new Buffer(data.prevHash, 'hex'), |
|
|
|
merkleRoot: new Buffer(data.merkleRoot, 'hex'), |
|
|
|
prevHash: prevHash, |
|
|
|
merkleRoot: merkleRoot, |
|
|
|
time: data.time, |
|
|
|
timestamp: data.time, |
|
|
|
bits: data.bits, |
|
|
@ -73,7 +84,7 @@ BlockHeader._fromJSON = function _fromJSON(data) { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* @param {String|Object} - A JSON string or object |
|
|
|
* @param {String} - A JSON string or object |
|
|
|
* @returns {BlockHeader} - An instance of block header |
|
|
|
*/ |
|
|
|
BlockHeader.fromJSON = function fromJSON(json) { |
|
|
@ -81,6 +92,15 @@ BlockHeader.fromJSON = function fromJSON(json) { |
|
|
|
return new BlockHeader(info); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* @param {Object} - A plain javascript object |
|
|
|
* @returns {BlockHeader} - An instance of block header |
|
|
|
*/ |
|
|
|
BlockHeader.fromObject = function fromObject(obj) { |
|
|
|
var info = BlockHeader._fromObject(obj); |
|
|
|
return new BlockHeader(info); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* @param {Binary} - Raw block binary data or buffer |
|
|
|
* @returns {BlockHeader} - An instance of block header |
|
|
@ -144,8 +164,8 @@ BlockHeader.fromBufferReader = function fromBufferReader(br) { |
|
|
|
BlockHeader.prototype.toObject = function toObject() { |
|
|
|
return { |
|
|
|
version: this.version, |
|
|
|
prevHash: this.prevHash.toString('hex'), |
|
|
|
merkleRoot: this.merkleRoot.toString('hex'), |
|
|
|
prevHash: BufferUtil.reverse(this.prevHash).toString('hex'), |
|
|
|
merkleRoot: BufferUtil.reverse(this.merkleRoot).toString('hex'), |
|
|
|
time: this.time, |
|
|
|
bits: this.bits, |
|
|
|
nonce: this.nonce |
|
|
@ -216,8 +236,8 @@ var idProperty = { |
|
|
|
writeable: false, |
|
|
|
enumerable: true, |
|
|
|
/** |
|
|
|
* @returns {string} - The big endian hash buffer of the header |
|
|
|
*/ |
|
|
|
* @returns {string} - The big endian hash buffer of the header |
|
|
|
*/ |
|
|
|
get: function() { |
|
|
|
if (!this._id) { |
|
|
|
this._id = BufferReader(this._getHash()).readReverse().toString('hex'); |
|
|
|