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,45 +79,29 @@ 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.blockheight = transaction.__height;
transformed.confirmations = confirmations;
var time = transaction.__timestamp ? transaction.__timestamp : Math.round(Date.now() / 1000);
transformed.time = time;
if (transformed.confirmations) {
transformed.blocktime = transformed.time;
}
transformed.blockhash = transaction.__blockHash; if(transaction.isCoinbase()) {
transformed.blockheight = transaction.__height; transformed.isCoinBase = true;
transformed.confirmations = confirmations; }
var time = transaction.__timestamp ? transaction.__timestamp : Math.round(Date.now() / 1000);
transformed.time = time;
if (transformed.confirmations) {
transformed.blocktime = transformed.time;
}
if(transaction.isCoinbase()) { transformed.valueOut = transaction.outputAmount / 1e8;
transformed.isCoinBase = true; transformed.size = transaction.toBuffer().length;
} if(transaction.hasAllUtxoInfo()) {
transformed.valueIn = transaction.inputAmount / 1e8;
transformed.valueOut = transaction.outputAmount / 1e8; transformed.fees = transaction.getFee() / 1e8;
transformed.size = transaction.toBuffer().length; }
if(transaction.hasAllUtxoInfo()) {
transformed.valueIn = transaction.inputAmount / 1e8;
transformed.fees = transaction.getFee() / 1e8;
}
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,11 +246,20 @@ 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);
} }
self.transformTransaction(tx, next);
// get spent info
tx.populateSpentInfo(self.node.services.bitcoind, {}, function(err) {
if (err) {
return common.handleErrors(err, res);
}
self.transformTransaction(tx, next);
});
}); });
}, function(err, transformed) { }, function(err, transformed) {
if(err) { if(err) {