transaction: spent info api updates

This commit is contained in:
Braydon Fuller 2016-04-12 15:01:27 -04:00
parent 7ded22fb2c
commit 38bbbc07dc

View File

@ -79,19 +79,7 @@ TxController.prototype.transformTransaction = function(transaction, callback) {
transformed.vin = txObj.inputs.map(this.transformInput.bind(this)); transformed.vin = txObj.inputs.map(this.transformInput.bind(this));
} }
async.map( transformed.vout = transaction.outputs.map(this.transformOutput.bind(this));
Object.keys(txObj.outputs),
function(outputIndex, next) {
outputIndex = parseInt(outputIndex);
var output = txObj.outputs[outputIndex];
self.transformOutput(txid, output, outputIndex, next);
},
function(err, vout) {
if (err) {
return callback(err);
}
transformed.vout = vout;
transformed.blockhash = transaction.__blockHash; transformed.blockhash = transaction.__blockHash;
transformed.blockheight = transaction.__height; transformed.blockheight = transaction.__height;
@ -114,10 +102,6 @@ TxController.prototype.transformTransaction = function(transaction, callback) {
} }
callback(null, transformed); callback(null, transformed);
}
);
}; };
TxController.prototype.transformInput = function(input, index) { TxController.prototype.transformInput = function(input, index) {
@ -147,25 +131,20 @@ TxController.prototype.transformInput = function(input, index) {
return transformed; return transformed;
}; };
TxController.prototype.transformOutput = function(txid, output, index, callback) { TxController.prototype.transformOutput = function(output, index) {
var self = this;
var transformed = { var transformed = {
value: (output.satoshis / 1e8).toFixed(8), value: (output.satoshis / 1e8).toFixed(8),
n: index, n: index,
scriptPubKey: { scriptPubKey: {
hex: output.script, hex: output._scriptBuffer.toString('hex'),
//reqSigs: null, // TODO //reqSigs: null, // TODO
} },
spentTxId: output.__spentTxId,
spentIndex: output.__spentIndex
//spentTs: undefined // TODO //spentTs: undefined // TODO
}; };
var script; var script = output.script;
try {
// Output scripts can be invalid, so we need to try/catch
script = new bitcore.Script(output.script);
} catch (err) {
script = false;
}
if (script) { if (script) {
transformed.scriptPubKey.asm = script.toASM(); transformed.scriptPubKey.asm = script.toASM();
var address = script.toAddress(this.node.network); var address = script.toAddress(this.node.network);
@ -174,26 +153,7 @@ TxController.prototype.transformOutput = function(txid, output, index, callback)
transformed.scriptPubKey.type = address.type; transformed.scriptPubKey.type = address.type;
} }
} }
return transformed;
var options = {
queryMempool: true
};
self.node.services.bitcoind.getInputForOutput(
txid,
index,
options,
function(err, inputResult) {
if (err) {
return callback(err);
}
if (inputResult) {
transformed.spentTxId = inputResult.inputTxId;
transformed.spentIndex = inputResult.inputIndex;
}
callback(null, transformed);
}
);
}; };
TxController.prototype.transformInvTransaction = function(transaction) { TxController.prototype.transformInvTransaction = function(transaction) {
@ -286,12 +246,21 @@ TxController.prototype.list = function(req, res) {
tx.__height = blockInfo.height; tx.__height = blockInfo.height;
tx.__timestamp = block.header.time; tx.__timestamp = block.header.time;
// get previous outputs
tx.populateInputs(self.node.services.bitcoind, [], function(err) { tx.populateInputs(self.node.services.bitcoind, [], function(err) {
if(err) { if(err) {
return next(err); return common.handleErrors(err, res);
}
// get spent info
tx.populateSpentInfo(self.node.services.bitcoind, {}, function(err) {
if (err) {
return common.handleErrors(err, res);
} }
self.transformTransaction(tx, next); self.transformTransaction(tx, next);
}); });
});
}, function(err, transformed) { }, function(err, transformed) {
if(err) { if(err) {
return common.handleErrors(err, res); return common.handleErrors(err, res);