Skip to content

Commit

Permalink
Merge pull request #488 from braydonf/trim-tx
Browse files Browse the repository at this point in the history
address: include options to trim transaction results
  • Loading branch information
matiu authored Jul 20, 2016
2 parents 5e54eac + 5eb32cf commit 802aae2
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 36 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ POST params:
addrs: 2NF2baYuJAkCKo5onjUKEPdARQkZ6SYyKd5,2NAre8sX2povnjy4aeiHKeEh97Qhn97tB1f
from (optional): 0
to (optional): 20
noAsm (optional): 1 (will omit script asm from results)
noScriptSig (optional): 1 (will omit the scriptSig from all inputs)
noSpent (option): 1 (will omit spent information per output)
```

Sample output:
Expand Down
16 changes: 13 additions & 3 deletions lib/addresses.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ AddressController.prototype.transformUtxo = function(utxoArg) {
return utxo;
};

AddressController.prototype._getTransformOptions = function(req) {
return {
noAsm: parseInt(req.query.noAsm) ? true : false,
noScriptSig: parseInt(req.query.noScriptSig) ? true : false,
noSpent: parseInt(req.query.noSpent) ? true : false
};
};

AddressController.prototype.multitxs = function(req, res, next) {
var self = this;

Expand All @@ -183,7 +191,9 @@ AddressController.prototype.multitxs = function(req, res, next) {
return self.common.handleErrors(err, res);
}

self.transformAddressHistoryForMultiTxs(result.items, function(err, items) {
var transformOptions = self._getTransformOptions(req);

self.transformAddressHistoryForMultiTxs(result.items, transformOptions, function(err, items) {
if (err) {
return self.common.handleErrors(err, res);
}
Expand All @@ -198,7 +208,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) {
Expand All @@ -210,7 +220,7 @@ AddressController.prototype.transformAddressHistoryForMultiTxs = function(txinfo
async.map(
items,
function(item, next) {
self.txController.transformTransaction(item, next);
self.txController.transformTransaction(item, options, next);
},
callback
);
Expand Down
47 changes: 30 additions & 17 deletions lib/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -96,19 +100,24 @@ 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 = {
hex: input.script
};
if (!options.noAsm) {
transformed.scriptSig.asm = input.scriptAsm;
}
}

transformed.addr = input.address;
transformed.valueSat = input.satoshis;
transformed.value = input.satoshis / 1e8;
Expand All @@ -120,21 +129,25 @@ 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
//reqSigs: null, // TODO
},
spentTxId: output.spentTxId || null,
spentIndex: _.isUndefined(output.spentIndex) ? null : output.spentIndex,
spentHeight: output.spentHeight || null
//spentTs: undefined // TODO
hex: output.script
}
};

if (!options.noAsm) {
transformed.scriptPubKey.asm = output.scriptAsm;
}

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
Expand Down
Loading

0 comments on commit 802aae2

Please sign in to comment.