diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 7c9b1523..72401260 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -543,8 +543,6 @@ Block.fromCompact = function fromCompact(json) { Block.prototype.toRaw = function toRaw(enc) { var data; - assert(this.subtype === 'block'); - data = this.render(); if (enc === 'hex') @@ -553,17 +551,28 @@ Block.prototype.toRaw = function toRaw(enc) { return data; }; -Block._fromRaw = function _fromRaw(data, enc) { +Block._fromRaw = function _fromRaw(data, enc, subtype) { var parser = new bcoin.protocol.parser(); + if (!subtype) + subtype = 'block'; + if (enc === 'hex') data = new Buffer(data, 'hex'); + if (subtype === 'headers') + return parser.parseHeaders(data); + + if (subtype === 'merkleblock') + return parser.parseMerkleBlock(data); + return parser.parseBlock(data); }; -Block.fromRaw = function fromRaw(data, enc) { - return new Block(Block._fromRaw(data, enc), 'block'); +Block.fromRaw = function fromRaw(data, enc, subtype) { + if (!subtype) + subtype = 'block'; + return new Block(Block._fromRaw(data, enc, subtype), subtype); }; /**