diff --git a/lib/transactions.js b/lib/transactions.js index 657caa6..505695a 100644 --- a/lib/transactions.js +++ b/lib/transactions.js @@ -79,45 +79,29 @@ TxController.prototype.transformTransaction = function(transaction, callback) { transformed.vin = txObj.inputs.map(this.transformInput.bind(this)); } - async.map( - 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 = transaction.outputs.map(this.transformOutput.bind(this)); - 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; - 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; - } + if(transaction.isCoinbase()) { + transformed.isCoinBase = true; + } - if(transaction.isCoinbase()) { - transformed.isCoinBase = true; - } - - transformed.valueOut = transaction.outputAmount / 1e8; - transformed.size = transaction.toBuffer().length; - if(transaction.hasAllUtxoInfo()) { - transformed.valueIn = transaction.inputAmount / 1e8; - transformed.fees = transaction.getFee() / 1e8; - } - - callback(null, transformed); - - } - ); + transformed.valueOut = transaction.outputAmount / 1e8; + transformed.size = transaction.toBuffer().length; + if(transaction.hasAllUtxoInfo()) { + transformed.valueIn = transaction.inputAmount / 1e8; + transformed.fees = transaction.getFee() / 1e8; + } + callback(null, transformed); }; TxController.prototype.transformInput = function(input, index) { @@ -147,25 +131,20 @@ TxController.prototype.transformInput = function(input, index) { return transformed; }; -TxController.prototype.transformOutput = function(txid, output, index, callback) { - var self = this; +TxController.prototype.transformOutput = function(output, index) { var transformed = { value: (output.satoshis / 1e8).toFixed(8), n: index, scriptPubKey: { - hex: output.script, + hex: output._scriptBuffer.toString('hex'), //reqSigs: null, // TODO - } + }, + spentTxId: output.__spentTxId, + spentIndex: output.__spentIndex //spentTs: undefined // TODO }; - var script; - try { - // Output scripts can be invalid, so we need to try/catch - script = new bitcore.Script(output.script); - } catch (err) { - script = false; - } + var script = output.script; if (script) { transformed.scriptPubKey.asm = script.toASM(); var address = script.toAddress(this.node.network); @@ -174,26 +153,7 @@ TxController.prototype.transformOutput = function(txid, output, index, callback) transformed.scriptPubKey.type = address.type; } } - - 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); - } - ); + return transformed; }; TxController.prototype.transformInvTransaction = function(transaction) { @@ -286,11 +246,20 @@ TxController.prototype.list = function(req, res) { tx.__height = blockInfo.height; tx.__timestamp = block.header.time; + // get previous outputs tx.populateInputs(self.node.services.bitcoind, [], function(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) { if(err) {