diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 88886375..62921eac 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -71,11 +71,9 @@ function Block(data, subtype) { if (this.subtype === 'block') { this.txs = this.txs.map(function(data) { - // assert(!(data instanceof bcoin.tx)); - if (data instanceof bcoin.tx) { - // assert(data.ts === self.ts); + if (data instanceof bcoin.tx) return data; - } + return bcoin.tx(data, self); }); } @@ -483,7 +481,6 @@ Block.prototype.inspect = function inspect() { Block.prototype.toJSON = function toJSON() { return { - v: 1, type: 'block', subtype: this.subtype, hash: this.hash('hex'), @@ -499,7 +496,6 @@ Block.prototype.toJSON = function toJSON() { Block.fromJSON = function fromJSON(json) { var raw, parser, data, block; - assert.equal(json.v, 1); assert.equal(json.type, 'block'); raw = new Buffer(json.block, 'hex'); @@ -524,7 +520,6 @@ Block.fromJSON = function fromJSON(json) { Block.prototype.toFullJSON = function toFullJSON() { return { - v: 1, type: 'block', subtype: this.subtype, height: this.height, diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 2c184927..a5821d09 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -60,18 +60,11 @@ function TX(data, block) { } if (block && !data.ts) { - this.network = true; - this.relayedBy = block.relayedBy; if (block.subtype === 'merkleblock') { - if (block.hasTX(this.hash('hex'))) { - this.ts = block.ts; - this.block = block.hash('hex'); - this.height = block.height; - } + if (block.hasTX(this.hash('hex'))) + this.setBlock(block); } else { - this.ts = block.ts; - this.block = block.hash('hex'); - this.height = block.height; + this.setBlock(block); } } @@ -81,6 +74,15 @@ function TX(data, block) { this.ps = this.ts === 0 ? utils.now() : 0; } +TX.prototype.setBlock = function setBlock(block) { + this.network = true; + this.relayedBy = block.relayedBy; + this.ts = block.ts; + this.block = block.hash('hex'); + this.height = block.height; + this.ps = 0; +}; + // Legacy TX.prototype.__defineSetter__('lock', function(locktime) { return this.locktime = locktime; @@ -1798,9 +1800,7 @@ TX.prototype.inspect = function inspect() { }; TX.prototype.toJSON = function toJSON(coins) { - // Compact representation return { - v: 1, type: 'tx', ts: this.ts, ps: this.ps, @@ -1810,15 +1810,48 @@ TX.prototype.toJSON = function toJSON(coins) { relayedBy: this.relayedBy, changeIndex: this.changeIndex, coins: coins ? this.inputs.map(function(input) { - return input.output ? input.output.toJSON() : null; + return input.output ? input.output.toRaw('hex') : null; }) : null, tx: utils.toHex(this.render()) }; }; +TX.fromJSON = function fromJSON(json) { + var raw, data, tx; + + assert.equal(json.type, 'tx'); + + raw = new Buffer(json.tx, 'hex'); + data = new bcoin.protocol.parser().parseTX(raw); + + data.network = json.network; + data.relayedBy = json.relayedBy; + + data.changeIndex = json.changeIndex; + + data._raw = raw; + data._size = raw.length; + + tx = new TX(data); + tx.height = json.height; + tx.ts = json.ts; + tx.block = json.block || null; + tx.ps = json.ps; + + if (json.coins) { + json.coins.forEach(function(output, i) { + if (!output) + return; + + tx.inputs[i].output = bcoin.coin.fromRaw(output, 'hex'); + }); + } + + return tx; +}; + TX.prototype.toFullJSON = function toFullJSON() { return { - v: 1, type: 'tx', ts: this.ts, ps: this.ps, @@ -1859,41 +1892,6 @@ TX.fromFullJSON = function fromFullJSON(json) { }); }; -TX.fromJSON = function fromJSON(json) { - var raw, data, tx; - - assert.equal(json.v, 1); - assert.equal(json.type, 'tx'); - - raw = new Buffer(json.tx, 'hex'); - data = new bcoin.protocol.parser().parseTX(raw); - - data.network = json.network; - data.relayedBy = json.relayedBy; - - data.changeIndex = json.changeIndex; - - data._raw = raw; - data._size = raw.length; - - tx = new TX(data); - tx.height = json.height; - tx.ts = json.ts; - tx.block = json.block || null; - tx.ps = json.ps; - - if (json.coins) { - json.coins.forEach(function(output, i) { - if (!output) - return; - - tx.inputs[i].output = bcoin.coin.fromJSON(output); - }); - } - - return tx; -}; - TX.prototype.toRaw = function toRaw(enc) { var data;