diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index d6c75aa6..3e8987c0 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -692,6 +692,31 @@ Block.fromJSON = function fromJSON(json) { return block; }; +Block.prototype.toRaw = function toRaw(enc) { + var data; + + assert(this.subtype === 'block'); + + if (this.network && this._raw && this._raw.length > 80) + data = this._raw; + else + data = new Buffer(this.render()); + + if (enc === 'hex') + data = utils.toHex(data); + + return data; +}; + +Block.fromRaw = function fromRaw(data, enc) { + var parser = new bcoin.protocol.parser(); + + if (enc === 'hex') + data = utils.toArray(data, 'hex'); + + return new Block(parser.parseBlock(data), 'block'); +}; + /** * Expose */ diff --git a/lib/bcoin/coin.js b/lib/bcoin/coin.js index 5338b9e3..df439fac 100644 --- a/lib/bcoin/coin.js +++ b/lib/bcoin/coin.js @@ -139,9 +139,9 @@ Coin.prototype.toRaw = function toRaw(enc) { height = 0xffffffff; utils.copy(utils.toArray(this.hash, 'hex'), data, 0); - utils.writeU32BE(data, this.index, 32); - utils.writeU32BE(data, height, 36); - utils.write64BE(data, this.value, 40); + utils.writeU32(data, this.index, 32); + utils.writeU32(data, height, 36); + utils.write64(data, this.value, 40); utils.copy(script, data, 48); if (enc === 'hex') @@ -156,16 +156,16 @@ Coin.fromRaw = function fromRaw(data, enc) { if (enc === 'hex') data = utils.toArray(data, 'hex'); - height = utils.readU32BE(data, 36); + height = utils.readU32(data, 36); if (height === 0xffffffff) height = -1; return new Coin({ hash: utils.toHex(data.slice(0, 32)), - index: utils.readU32BE(data, 32), + index: utils.readU32(data, 32), height: height, - value: utils.read64BE(data, 40), + value: utils.read64(data, 40), script: bcoin.script.decode(utils.toArray(data.slice(48))) }); }; diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index ed4171ff..9bfc8080 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -281,7 +281,7 @@ Parser.prototype.parseMerkleBlock = function parseMerkleBlock(p) { totalTX: utils.readU32(p, 80), hashes: hashes, flags: flags, - _raw: p.slice(0, 80), + _raw: p, _size: p.length }; }; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 909d5a1f..036afe93 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -1827,18 +1827,26 @@ TX.fromJSON = function fromJSON(json) { }; TX.prototype.toRaw = function toRaw(enc) { - var raw = this.render(); + var data; + + if (this.network && this._raw) + data = this._raw; + else + data = new Buffer(this.render()); if (enc === 'hex') - return utils.toHex(raw); + data = utils.toHex(data); - return raw; + return data; }; -TX.fromRaw = function fromRaw(raw, enc) { +TX.fromRaw = function fromRaw(data, enc) { + var parser = new bcoin.protocol.parser(); + if (enc === 'hex') - raw = utils.toArray(raw, 'hex'); - return new bcoin.tx(new bcoin.protocol.parser().parseTX(raw)); + data = utils.toArray(data, 'hex'); + + return new bcoin.tx(parser.parseTX(data)); }; /**