address: include options to trim transaction results

This commit is contained in:
Braydon Fuller 2016-07-19 20:33:31 -04:00
parent 967445c14f
commit 38e061e0c5
2 changed files with 33 additions and 17 deletions

View File

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

View File

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