diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 4799552c..776d4272 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -127,37 +127,6 @@ Block.prototype._verifyMerkle = function verifyMerkle() { } }; -Block.prototype.toJSON = function toJSON() { - return { - v: '1', - type: 'block', - subtype: this.subtype, - hash: this._hash || this.hash('hex'), - prevBlock: this.prevBlock, - ts: this.ts, - block: utils.toHex(bcoin.protocol.framer.block(this, this.subtype)) - }; -}; - -Block.fromJSON = function fromJSON(json) { - utils.assert.equal(json.v, 1); - utils.assert.equal(json.type, 'block'); - - var raw = utils.toArray(json.block, 'hex'); - - var parser = new bcoin.protocol.parser(); - - var data = json.subtype === 'merkleblock' ? - parser.parseMerkleBlock(raw) : - parser.parseBlock(raw); - - var block = new bcoin.block(data, json.subtype); - - block._hash = json.hash; - - return block; -}; - Block.prototype._buildMerkle = function buildMerkle() { var merkleTree = []; for (var i = 0; i < this.txs.length; i++) { @@ -218,3 +187,34 @@ Block.prototype._checkBlock = function checkBlock() { // Check merkle root return this.merkleTree[this.merkleTree.length - 1] === this.merkleRoot; }; + +Block.prototype.toJSON = function toJSON() { + return { + v: '1', + type: 'block', + subtype: this.subtype, + hash: this._hash || this.hash('hex'), + prevBlock: this.prevBlock, + ts: this.ts, + block: utils.toHex(bcoin.protocol.framer.block(this, this.subtype)) + }; +}; + +Block.fromJSON = function fromJSON(json) { + utils.assert.equal(json.v, 1); + utils.assert.equal(json.type, 'block'); + + var raw = utils.toArray(json.block, 'hex'); + + var parser = new bcoin.protocol.parser(); + + var data = json.subtype === 'merkleblock' ? + parser.parseMerkleBlock(raw) : + parser.parseBlock(raw); + + var block = new bcoin.block(data, json.subtype); + + block._hash = json.hash; + + return block; +};