diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index a8a5ab0f..c7b6ade7 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -530,10 +530,10 @@ Block.prototype.toFullJSON = function toFullJSON() { height: this.height, network: this.network, relayedBy: this.relayedBy, - hash: this.hash('hex'), + hash: utils.revHex(this.hash('hex')), version: this.version, - prevBlock: this.prevBlock, - merkleRoot: this.merkleRoot, + prevBlock: utils.revHex(this.prevBlock), + merkleRoot: utils.revHex(this.merkleRoot), ts: this.ts, bits: this.bits, nonce: this.nonce, @@ -544,6 +544,15 @@ Block.prototype.toFullJSON = function toFullJSON() { }; Block.fromFullJSON = function fromFullJSON(json) { + json.prevBlock = utils.revHex(json.prevBlock); + json.merkleRoot = utils.revHex(json.merkleRoot); + json.txs = json.txs.map(function(tx) { + tx = bcoin.tx.fromFullJSON(tx); + tx.ts = block.ts; + tx.block = block.hash('hex'); + tx.height = block.height; + return tx; + }); return new Block(json, json.subtype); }; diff --git a/lib/bcoin/coin.js b/lib/bcoin/coin.js index 8cf51726..707506a1 100644 --- a/lib/bcoin/coin.js +++ b/lib/bcoin/coin.js @@ -144,6 +144,32 @@ Coin.fromJSON = function fromJSON(json) { }); }; +Coin.prototype.toFullJSON = function toFullJSON() { + return { + version: this.version, + height: this.height, + value: utils.btc(this.value), + script: utils.toHex(bcoin.script.encode(this.script)), + hash: utils.revHex(this.hash), + index: this.index, + spent: this.spent, + address: this.getAddress() + }; +}; + +Coin.fromFullJSON = function fromFullJSON(json) { + return new Coin({ + version: json.version, + height: json.height, + value: utils.satoshi(json.value), + script: bcoin.script.decode(utils.toArray(json.script, 'hex')), + hash: utils.revHex(json.hash), + index: json.index, + spent: json.spent, + address: json.address + }); +}; + // This is basically BIP64 with some // extra fields tacked on the end. Coin.prototype.toRaw = function toRaw(enc, strict) { diff --git a/lib/bcoin/http.js b/lib/bcoin/http.js index 1d445045..f690725e 100644 --- a/lib/bcoin/http.js +++ b/lib/bcoin/http.js @@ -77,6 +77,7 @@ HTTPServer.prototype._init = function _init() { // UTXO by id this.get('/utxo/:hash/:index', function(req, res, next, send) { + req.params.hash = utils.revHex(req.params.hash); self.node.getCoin(req.params.hash, +req.params.index, function(err, coin) { if (err) return next(err); @@ -99,6 +100,7 @@ HTTPServer.prototype._init = function _init() { // TX by hash this.get('/tx/:hash', function(req, res, next, send) { + req.params.hash = utils.revHex(req.params.hash); self.node.getTX(req.params.hash, function(err, tx) { if (err) return next(err); @@ -137,6 +139,8 @@ HTTPServer.prototype._init = function _init() { if (utils.isInt(hash)) hash = +hash; + else + hash = utils.revHex(hash); self.node.getBlock(hash, function(err, block) { if (err) diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index 4dd78e82..1818c75a 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -365,22 +365,25 @@ Input.prototype.inspect = function inspect() { }; }; -Input.prototype.toJSON = function toJSON() { +Input.prototype.toFullJSON = function toFullJSON() { return { prevout: { - hash: this.prevout.hash, + hash: utils.revHex(this.prevout.hash), index: this.prevout.index }, - output: this.output ? this.output.toJSON() : null, + output: this.output ? this.output.toFullJSON() : null, script: utils.toHex(bcoin.script.encode(this.script)), sequence: this.sequence }; }; -Input.fromJSON = function fromJSON(json) { +Input.fromFullJSON = function fromFullJSON(json) { return new Input({ - prevout: json.prevout, - output: json.output, + prevout: { + hash: utils.revHex(json.prevout.hash), + index: json.prevout.index + }, + output: json.output ? bcoin.coin.fromFullJSON(json.output) : null, script: bcoin.script.decode(utils.toArray(json.script, 'hex')), sequence: json.sequence }); diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index 706566be..0ec496d4 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -259,14 +259,14 @@ Output.prototype.inspect = function inspect() { }; }; -Output.prototype.toJSON = function toJSON() { +Output.prototype.toFullJSON = function toFullJSON() { return { value: utils.btc(this.value), script: utils.toHex(bcoin.script.encode(this.script)) }; }; -Output.fromJSON = function fromJSON(json) { +Output.fromFullJSON = function fromFullJSON(json) { return new Output({ value: utils.satoshi(json.value), script: bcoin.script.decode(utils.toArray(json.script, 'hex')) diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 05f108dd..99c3d397 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -1828,17 +1828,18 @@ TX.prototype.toFullJSON = function toFullJSON() { type: 'tx', ts: this.ts, ps: this.ps, - block: this.block, + block: this.block ? utils.revHex(this.block) : null, height: this.height, network: this.network, relayedBy: this.relayedBy, changeIndex: this.changeIndex, + hash: utils.revHex(this.hash('hex')), version: this.version, inputs: this.inputs.map(function(input) { - return input.toJSON(); + return input.toFullJSON(); }), outputs: this.outputs.map(function(output) { - return output.toJSON(); + return output.toFullJSON(); }), locktime: this.locktime }; @@ -1848,23 +1849,22 @@ TX.fromFullJSON = function fromFullJSON(json) { return new TX({ ts: json.ts, ps: json.ps, - block: json.block, + block: json.block ? utils.revHex(json.block) : null, height: json.height, network: json.network, relayedBy: json.relayedBy, changeIndex: json.changeIndex, version: json.version, inputs: json.inputs.map(function(input) { - return bcoin.input.fromJSON(input); + return bcoin.input.fromFullJSON(input); }), outputs: json.outputs.map(function(output) { - return bcoin.output.fromJSON(output); + return bcoin.output.fromFullJSON(output); }), locktime: json.locktime }); }; - TX.fromJSON = function fromJSON(json) { var raw, data, tx;