From 38e061e0c5bbb4444414e5015578a646d4b8cca3 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Tue, 19 Jul 2016 20:33:31 -0400 Subject: [PATCH] address: include options to trim transaction results --- lib/addresses.js | 12 +++++++++--- lib/transactions.js | 38 ++++++++++++++++++++++++-------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/lib/addresses.js b/lib/addresses.js index c4838bc..48bb6e0 100644 --- a/lib/addresses.js +++ b/lib/addresses.js @@ -183,7 +183,13 @@ AddressController.prototype.multitxs = function(req, res, next) { return self.common.handleErrors(err, res); } - self.transformAddressHistoryForMultiTxs(result.items, function(err, items) { + var transformOptions = { + noAsm: req.query.noAsm ? true : false, + noScriptSig: req.query.noScriptSig ? true : false, + noSpent: req.query.noSpent ? true : false + }; + + self.transformAddressHistoryForMultiTxs(result.items, transformOptions, function(err, items) { if (err) { return self.common.handleErrors(err, res); } @@ -198,7 +204,7 @@ AddressController.prototype.multitxs = function(req, res, next) { }); }; -AddressController.prototype.transformAddressHistoryForMultiTxs = function(txinfos, callback) { +AddressController.prototype.transformAddressHistoryForMultiTxs = function(txinfos, options, callback) { var self = this; var items = txinfos.map(function(txinfo) { @@ -210,7 +216,7 @@ AddressController.prototype.transformAddressHistoryForMultiTxs = function(txinfo async.map( items, function(item, next) { - self.txController.transformTransaction(item, next); + self.txController.transformTransaction(item, options, next); }, callback ); diff --git a/lib/transactions.js b/lib/transactions.js index 9217800..a3c51e5 100644 --- a/lib/transactions.js +++ b/lib/transactions.js @@ -44,7 +44,11 @@ TxController.prototype.transaction = function(req, res, next) { }); }; -TxController.prototype.transformTransaction = function(transaction, callback) { +TxController.prototype.transformTransaction = function(transaction, options, callback) { + if (_.isFunction(options)) { + callback = options; + options = {}; + } $.checkArgument(_.isFunction(callback)); var confirmations = 0; @@ -67,10 +71,10 @@ TxController.prototype.transformTransaction = function(transaction, callback) { } ]; } else { - transformed.vin = transaction.inputs.map(this.transformInput.bind(this)); + transformed.vin = transaction.inputs.map(this.transformInput.bind(this, options)); } - transformed.vout = transaction.outputs.map(this.transformOutput.bind(this)); + transformed.vout = transaction.outputs.map(this.transformOutput.bind(this, options)); transformed.blockhash = transaction.blockHash; transformed.blockheight = transaction.height; @@ -96,19 +100,22 @@ TxController.prototype.transformTransaction = function(transaction, callback) { callback(null, transformed); }; -TxController.prototype.transformInput = function(input, index) { +TxController.prototype.transformInput = function(options, input, index) { // Input scripts are validated and can be assumed to be valid var transformed = { txid: input.prevTxId, vout: input.outputIndex, - scriptSig: { - asm: input.scriptAsm, - hex: input.script - }, sequence: input.sequence, n: index }; + if (!options.noScriptSig) { + transformed.scriptSig = { + asm: options.noAsm ? undefined : input.scriptAsm, + hex: input.script + }; + } + transformed.addr = input.address; transformed.valueSat = input.satoshis; transformed.value = input.satoshis / 1e8; @@ -120,21 +127,24 @@ TxController.prototype.transformInput = function(input, index) { return transformed; }; -TxController.prototype.transformOutput = function(output, index) { +TxController.prototype.transformOutput = function(options, output, index) { var transformed = { value: (output.satoshis / 1e8).toFixed(8), n: index, scriptPubKey: { hex: output.script, - asm: output.scriptAsm + asm: options.noAsm ? undefined : output.scriptAsm //reqSigs: null, // TODO - }, - spentTxId: output.spentTxId || null, - spentIndex: _.isUndefined(output.spentIndex) ? null : output.spentIndex, - spentHeight: output.spentHeight || null + } //spentTs: undefined // TODO }; + if (!options.noSpent) { + transformed.spentTxId = output.spentTxId || null; + transformed.spentIndex = _.isUndefined(output.spentIndex) ? null : output.spentIndex; + transformed.spentHeight = output.spentHeight || null; + } + if (output.address) { transformed.scriptPubKey.addresses = [output.address]; var address = bitcore.Address(output.address); //TODO return type from bitcore-node