diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index b822e5ad..8e9b33c7 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -81,7 +81,7 @@ Block.fromOptions = function fromOptions(data) { * @returns {Buffer} */ -Block.prototype.render = function render(writer) { +Block.prototype.toRaw = function toRaw(writer) { var raw = this.getRaw(); if (writer) { writer.writeBytes(raw); @@ -95,7 +95,7 @@ Block.prototype.render = function render(writer) { * @returns {Buffer} */ -Block.prototype.renderNormal = function renderNormal(writer) { +Block.prototype.toNormal = function toNormal(writer) { var raw; if (!this.hasWitness()) { raw = this.getRaw(); @@ -113,7 +113,7 @@ Block.prototype.renderNormal = function renderNormal(writer) { * @returns {Buffer} */ -Block.prototype.renderWitness = function renderWitness(writer) { +Block.prototype.toWitness = function toWitness(writer) { var raw; if (this.hasWitness()) { raw = this.getRaw(); @@ -178,7 +178,7 @@ Block.prototype.getSizes = function getSizes() { } writer = new bcoin.writer(); - this.render(writer); + this.toRaw(writer); return { size: writer.written, @@ -685,17 +685,6 @@ Block.fromJSON = function fromJSON(json) { return new Block().fromJSON(json); }; -/** - * Serialize the block. - * @see {Block#render} - * @param {String?} enc - Encoding, can be `'hex'` or null. - * @returns {Buffer|String} - */ - -Block.prototype.toRaw = function toRaw(writer) { - return this.render(writer); -}; - /** * Parse a serialized block. * @param {Buffer} data @@ -772,18 +761,18 @@ Block.prototype.frame = function frame(witness, writer) { for (i = 0; i < this.txs.length; i++) { tx = this.txs[i]; if (witness) { - tx.render(p); + tx.toRaw(p); witnessSize += tx._lastWitnessSize; } else { - tx.renderNormal(p); + tx.toNormal(p); } } + this._lastWitnessSize = witnessSize; + if (!writer) p = p.render(); - this._lastWitnessSize = witnessSize; - return p; }; @@ -803,7 +792,7 @@ Block.prototype.frameWitness = function frameWitness(writer) { Block.isBlock = function isBlock(obj) { return obj - && typeof obj.merkleRoot === 'string' + && obj.merkleRoot !== undefined && typeof obj.getCommitmentHash === 'function'; }; diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index 31c76829..a80163ca 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -798,7 +798,7 @@ ChainDB.prototype.saveBlock = function saveBlock(block, view, batch, connect, ca if (this.options.spv) return utils.nextTick(callback); - batch.put(layout.b(block.hash()), block.render()); + batch.put(layout.b(block.hash()), block.toRaw()); if (!connect) return utils.nextTick(callback); diff --git a/lib/bcoin/coin.js b/lib/bcoin/coin.js index bbd16a92..68c6f602 100644 --- a/lib/bcoin/coin.js +++ b/lib/bcoin/coin.js @@ -300,8 +300,8 @@ Coin.fromTX = function fromTX(tx, index) { Coin.isCoin = function isCoin(obj) { return obj - && typeof obj.version === 'number' - && typeof obj.script === 'object' + && obj.version !== undefined + && obj.script !== undefined && typeof obj.getConfirmations === 'function'; }; diff --git a/lib/bcoin/headers.js b/lib/bcoin/headers.js index b03e8e6e..e1169cf0 100644 --- a/lib/bcoin/headers.js +++ b/lib/bcoin/headers.js @@ -39,15 +39,6 @@ function Headers(data) { utils.inherits(Headers, bcoin.abstractblock); -/** - * Serialize the header into a `headers` packet. - * @returns {Buffer} - */ - -Headers.prototype.render = function render(writer) { - return this.getRaw(writer); -}; - /** * Do non-contextual verification on the headers. * @alias Headers#verify diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index 59728a38..9732855d 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -502,9 +502,9 @@ Input.fromExtended = function fromExtended(data, enc) { Input.isInput = function isInput(obj) { return obj - && obj.prevout - && obj.script - && obj.witness + && obj.prevout !== undefined + && obj.script !== undefined + && obj.witness !== undefined && typeof obj.getAddress === 'function'; }; diff --git a/lib/bcoin/memblock.js b/lib/bcoin/memblock.js index f1747545..1a694028 100644 --- a/lib/bcoin/memblock.js +++ b/lib/bcoin/memblock.js @@ -144,8 +144,13 @@ MemBlock.fromRaw = function fromRaw(data) { return new MemBlock().fromRaw(data); }; +MemBlock.prototype.toRaw = function toRaw() { + return this.raw; +}; + /** - * Parse the serialized block data and create an actual {@link Block}. + * Parse the serialized block data + * and create an actual {@link Block}. * @returns {Block} * @throws Parse error */ diff --git a/lib/bcoin/merkleblock.js b/lib/bcoin/merkleblock.js index a0b2c1c9..2e3ccc70 100644 --- a/lib/bcoin/merkleblock.js +++ b/lib/bcoin/merkleblock.js @@ -76,15 +76,6 @@ MerkleBlock.fromOptions = function fromOptions(data) { return new MerkleBlock().fromOptions(data); }; -/** - * Serialize the merkleblock. - * @returns {Buffer} - */ - -MerkleBlock.prototype.render = function render(writer) { - return this.toRaw(writer); -}; - /** * Get merkleblock size. * @returns {Number} Size. @@ -563,7 +554,7 @@ MerkleBlock.fromBlock = function fromBlock(block, filter) { MerkleBlock.isMerkleBlock = function isMerkleBlock(obj) { return obj - && Buffer.isBuffer(obj.flags) + && obj.flags !== undefined && typeof obj.verifyPartial === 'function'; }; diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index a4562f9f..90013544 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -689,7 +689,7 @@ MinerBlock.prototype.toRaw = function toRaw(writer) { p.writeVarint(this.block.txs.length - 1); for (i = 1; i < this.block.txs.length; i++) - p.writeBytes(this.block.txs[i].render()); + p.writeBytes(this.block.txs[i].toRaw()); if (!writer) p = p.render(); diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index 78788341..4522a477 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -279,8 +279,8 @@ Output.fromRaw = function fromRaw(data, enc) { Output.isOutput = function isOutput(obj) { return obj - && obj.value - && obj.script + && obj.value !== undefined + && obj.script !== undefined && typeof obj.getAddress === 'function'; }; diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 09924850..c89fa08c 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -653,53 +653,53 @@ Framer._getBlocks = function _getBlocks(data, writer, headers) { /** * Serialize transaction without witness. - * @param {NakedTX|TX} tx + * @param {TX} tx * @param {BufferWriter?} writer - A buffer writer to continue writing from. * @returns {Buffer} Returns a BufferWriter if `writer` was passed in. */ Framer.tx = function _tx(tx, writer) { - return tx.renderNormal(writer); + return tx.toNormal(writer); }; /** * Serialize transaction with witness. Calculates the witness * size as it is framing (exposed on return value as `_witnessSize`). - * @param {NakedTX|TX} tx + * @param {TX} tx * @param {BufferWriter?} writer - A buffer writer to continue writing from. * @returns {Buffer} Returns a BufferWriter if `writer` was passed in. */ Framer.witnessTX = function _witnessTX(tx, writer) { - return tx.renderWitness(writer); + return tx.toWitness(writer); }; /** * Serialize a block without witness data. - * @param {NakedBlock|Block} block + * @param {Block} block * @param {BufferWriter?} writer - A buffer writer to continue writing from. * @returns {Buffer} Returns a BufferWriter if `writer` was passed in. */ Framer.block = function _block(block, writer) { - return block.renderNormal(block); + return block.toNormal(block); }; /** * Serialize a block with witness data. Calculates the witness * size as it is framing (exposed on return value as `_witnessSize`). - * @param {NakedBlock|Block} block + * @param {Block} block * @param {BufferWriter?} writer - A buffer writer to continue writing from. * @returns {Buffer} Returns a BufferWriter if `writer` was passed in. */ Framer.witnessBlock = function _witnessBlock(block, writer) { - return block.renderWitness(writer); + return block.toWitness(writer); }; /** * Serialize a merkle block. - * @param {NakedBlock|MerkleBlock} block + * @param {MerkleBlock} block * @param {BufferWriter?} writer - A buffer writer to continue writing from. * @returns {Buffer} Returns a BufferWriter if `writer` was passed in. */ @@ -710,7 +710,7 @@ Framer.merkleBlock = function _merkleBlock(block, writer) { /** * Serialize headers. - * @param {NakedBlock[]|Headers[]} headers + * @param {Headers[]} headers * @param {BufferWriter?} writer - A buffer writer to continue writing from. * @returns {Buffer} Returns a BufferWriter if `writer` was passed in. */ diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index 535886da..554a6f89 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -634,7 +634,7 @@ Parser.parseInv = function parseInv(p) { /** * Parse merkleblock packet. * @param {Buffer|BufferReader} p - * @returns {NakedBlock} + * @returns {Block} */ Parser.parseMerkleBlock = function parseMerkleBlock(p) { @@ -644,7 +644,7 @@ Parser.parseMerkleBlock = function parseMerkleBlock(p) { /** * Parse headers packet. * @param {Buffer|BufferReader} p - * @returns {NakedBlock[]} + * @returns {Headers[]} */ Parser.parseHeaders = function parseHeaders(p) { @@ -664,7 +664,7 @@ Parser.parseHeaders = function parseHeaders(p) { /** * Parse block packet. * @param {Buffer|BufferReader} p - * @returns {NakedBlock} + * @returns {Block} */ Parser.parseBlock = function parseBlock(p) { @@ -674,7 +674,7 @@ Parser.parseBlock = function parseBlock(p) { /** * Parse block packet. * @param {Buffer|BufferReader} p - * @returns {NakedBlock} + * @returns {Block} */ Parser.parseMemBlock = function parseMemBlock(p) { @@ -685,7 +685,7 @@ Parser.parseMemBlock = function parseMemBlock(p) { * Parse tx packet (will automatically switch to witness * parsing if a witness transaction is detected). * @param {Buffer|BufferReader} p - * @returns {NakedTX} + * @returns {TX} */ Parser.parseTX = function parseTX(p) { diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index ff0bbb31..6dd7bc8c 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -197,7 +197,7 @@ TX.prototype.hash = function _hash(enc) { var hash = this._hash; if (!hash) { - hash = utils.dsha256(this.renderNormal()); + hash = utils.dsha256(this.toNormal()); if (!this.mutable) this._hash = hash; } @@ -227,7 +227,7 @@ TX.prototype.witnessHash = function witnessHash(enc) { return this.hash(enc); if (!hash) { - hash = utils.dsha256(this.renderWitness()); + hash = utils.dsha256(this.toWitness()); if (!this.mutable) this._whash = hash; } @@ -243,7 +243,7 @@ TX.prototype.witnessHash = function witnessHash(enc) { * @returns {Buffer} Serialized transaction. */ -TX.prototype.render = function render(writer) { +TX.prototype.toRaw = function toRaw(writer) { var raw = this.getRaw(); if (writer) { writer.writeBytes(raw); @@ -259,7 +259,7 @@ TX.prototype.render = function render(writer) { * @returns {Buffer} Serialized transaction. */ -TX.prototype.renderNormal = function renderNormal(writer) { +TX.prototype.toNormal = function toNormal(writer) { var raw = this.getRaw(); if (!TX.isWitness(raw)) { if (writer) { @@ -278,7 +278,7 @@ TX.prototype.renderNormal = function renderNormal(writer) { * @returns {Buffer} Serialized transaction. */ -TX.prototype.renderWitness = function renderWitness(writer) { +TX.prototype.toWitness = function toWitness(writer) { var raw = this.getRaw(); if (TX.isWitness(raw)) { if (writer) { @@ -1918,17 +1918,6 @@ TX.fromJSON = function fromJSON(json) { return new TX().fromJSON(json); }; -/** - * Serialize the transaction. - * @see {TX#render} - * @param {String?} enc - Encoding, can be `'hex'` or null. - * @returns {Buffer|String} - */ - -TX.prototype.toRaw = function toRaw(writer) { - return this.render(writer); -}; - /** * Instantiate a transaction from a serialized Buffer. * @param {Buffer} data @@ -2124,11 +2113,11 @@ TX.prototype.frameWitness = function frameWitness(writer) { p.writeU32(this.locktime); + this._lastWitnessSize = witnessSize + 2; + if (!writer) p = p.render(); - this._lastWitnessSize = witnessSize + 2; - return p; }; diff --git a/lib/bcoin/types.js b/lib/bcoin/types.js index 9bc8c3ea..df009f6e 100644 --- a/lib/bcoin/types.js +++ b/lib/bcoin/types.js @@ -190,7 +190,7 @@ /** * @typedef {Object} SubmitOrderPacket * @property {Hash} hash - * @property {NakedTX} tx + * @property {TX} tx * @global */ @@ -246,7 +246,7 @@ * @property {Buffer?} data.map - Hit map. * @property {Object} data.height - Chain height. * @property {Hash} data.tip - Chain tip hash. - * @property {NakedCoin[]} data.coins + * @property {Coin[]} data.coins * @global */ diff --git a/lib/bcoin/workers.js b/lib/bcoin/workers.js index 93c2d62a..ea72076d 100644 --- a/lib/bcoin/workers.js +++ b/lib/bcoin/workers.js @@ -817,7 +817,7 @@ Framer.item = function _item(item, p) { } else { if (item instanceof bcoin.block) { p.writeU8(40); - item.render(p); + item.toRaw(p); } else if (item instanceof bcoin.tx) { p.writeU8(41); item.toExtended(true, p); diff --git a/test/block-test.js b/test/block-test.js index 27dc0855..11ab6280 100644 --- a/test/block-test.js +++ b/test/block-test.js @@ -70,7 +70,7 @@ describe('Block', function() { it('should decode/encode with parser/framer', function() { var b = bcoin.merkleblock.fromRaw(raw, 'hex'); - assert.equal(b.render().toString('hex'), raw); + assert.equal(b.toRaw().toString('hex'), raw); assert.equal(raw, raw2); }); @@ -82,7 +82,7 @@ describe('Block', function() { it('should be serialized and deserialized and still verify', function() { var raw = mblock.toRaw(); var b = bcoin.merkleblock.fromRaw(raw); - assert.deepEqual(b.render(), raw); + assert.deepEqual(b.toRaw(), raw); assert(b.verify()); }); @@ -129,7 +129,7 @@ describe('Block', function() { filter.add(item2, 'hex'); var mblock2 = bcoin.merkleblock.fromBlock(block, filter); assert(mblock2.verifyPartial()); - assert.deepEqual(mblock2.render(), mblock.render()); + assert.deepEqual(mblock2.toRaw(), mblock.toRaw()); }); it('should verify a historical block', function() { diff --git a/test/tx-test.js b/test/tx-test.js index 947dd17d..c5a11c3e 100644 --- a/test/tx-test.js +++ b/test/tx-test.js @@ -113,7 +113,7 @@ describe('TX', function() { it('should decode/encode with parser/framer' + suffix, function() { var tx = bcoin.tx.fromRaw(raw, 'hex'); clearCache(tx, nocache); - assert.equal(tx.render().toString('hex'), raw); + assert.equal(tx.toRaw().toString('hex'), raw); }); it('should be verifiable' + suffix, function() { @@ -163,9 +163,9 @@ describe('TX', function() { assert.equal(wtx.getSize(), 62138); assert.equal(wtx.getVirtualSize(), 61813); assert.equal(wtx.getCost(), 247250); - var raw1 = wtx.render(); + var raw1 = wtx.toRaw(); clearCache(wtx, true); - var raw2 = wtx.render(); + var raw2 = wtx.toRaw(); assert.deepEqual(raw1, raw2); var wtx2 = bcoin.tx.fromRaw(raw2); clearCache(wtx2, nocache); diff --git a/test/wallet-test.js b/test/wallet-test.js index f72a09c7..880c64ed 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -177,7 +177,7 @@ describe('Wallet', function() { var maxSize = tx.maxSize(); w.sign(tx, function(err) { assert.ifError(err); - assert(tx.render().length <= maxSize); + assert(tx.toRaw().length <= maxSize); assert(tx.verify()); }); });