full json

This commit is contained in:
Christopher Jeffrey 2016-02-22 06:36:54 -08:00
parent 9c7ca8501f
commit ab3fb2e550
6 changed files with 60 additions and 18 deletions

View File

@ -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);
};

View File

@ -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) {

View File

@ -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)

View File

@ -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
});

View File

@ -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'))

View File

@ -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;