Skip to content

Commit 802aae2

Browse files
authored
Merge pull request #488 from braydonf/trim-tx
address: include options to trim transaction results
2 parents 5e54eac + 5eb32cf commit 802aae2

File tree

4 files changed

+234
-36
lines changed

4 files changed

+234
-36
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ POST params:
292292
addrs: 2NF2baYuJAkCKo5onjUKEPdARQkZ6SYyKd5,2NAre8sX2povnjy4aeiHKeEh97Qhn97tB1f
293293
from (optional): 0
294294
to (optional): 20
295+
noAsm (optional): 1 (will omit script asm from results)
296+
noScriptSig (optional): 1 (will omit the scriptSig from all inputs)
297+
noSpent (option): 1 (will omit spent information per output)
295298
```
296299

297300
Sample output:

lib/addresses.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ AddressController.prototype.transformUtxo = function(utxoArg) {
169169
return utxo;
170170
};
171171

172+
AddressController.prototype._getTransformOptions = function(req) {
173+
return {
174+
noAsm: parseInt(req.query.noAsm) ? true : false,
175+
noScriptSig: parseInt(req.query.noScriptSig) ? true : false,
176+
noSpent: parseInt(req.query.noSpent) ? true : false
177+
};
178+
};
179+
172180
AddressController.prototype.multitxs = function(req, res, next) {
173181
var self = this;
174182

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

186-
self.transformAddressHistoryForMultiTxs(result.items, function(err, items) {
194+
var transformOptions = self._getTransformOptions(req);
195+
196+
self.transformAddressHistoryForMultiTxs(result.items, transformOptions, function(err, items) {
187197
if (err) {
188198
return self.common.handleErrors(err, res);
189199
}
@@ -198,7 +208,7 @@ AddressController.prototype.multitxs = function(req, res, next) {
198208
});
199209
};
200210

201-
AddressController.prototype.transformAddressHistoryForMultiTxs = function(txinfos, callback) {
211+
AddressController.prototype.transformAddressHistoryForMultiTxs = function(txinfos, options, callback) {
202212
var self = this;
203213

204214
var items = txinfos.map(function(txinfo) {
@@ -210,7 +220,7 @@ AddressController.prototype.transformAddressHistoryForMultiTxs = function(txinfo
210220
async.map(
211221
items,
212222
function(item, next) {
213-
self.txController.transformTransaction(item, next);
223+
self.txController.transformTransaction(item, options, next);
214224
},
215225
callback
216226
);

lib/transactions.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ TxController.prototype.transaction = function(req, res, next) {
4444
});
4545
};
4646

47-
TxController.prototype.transformTransaction = function(transaction, callback) {
47+
TxController.prototype.transformTransaction = function(transaction, options, callback) {
48+
if (_.isFunction(options)) {
49+
callback = options;
50+
options = {};
51+
}
4852
$.checkArgument(_.isFunction(callback));
4953

5054
var confirmations = 0;
@@ -67,10 +71,10 @@ TxController.prototype.transformTransaction = function(transaction, callback) {
6771
}
6872
];
6973
} else {
70-
transformed.vin = transaction.inputs.map(this.transformInput.bind(this));
74+
transformed.vin = transaction.inputs.map(this.transformInput.bind(this, options));
7175
}
7276

73-
transformed.vout = transaction.outputs.map(this.transformOutput.bind(this));
77+
transformed.vout = transaction.outputs.map(this.transformOutput.bind(this, options));
7478

7579
transformed.blockhash = transaction.blockHash;
7680
transformed.blockheight = transaction.height;
@@ -96,19 +100,24 @@ TxController.prototype.transformTransaction = function(transaction, callback) {
96100
callback(null, transformed);
97101
};
98102

99-
TxController.prototype.transformInput = function(input, index) {
103+
TxController.prototype.transformInput = function(options, input, index) {
100104
// Input scripts are validated and can be assumed to be valid
101105
var transformed = {
102106
txid: input.prevTxId,
103107
vout: input.outputIndex,
104-
scriptSig: {
105-
asm: input.scriptAsm,
106-
hex: input.script
107-
},
108108
sequence: input.sequence,
109109
n: index
110110
};
111111

112+
if (!options.noScriptSig) {
113+
transformed.scriptSig = {
114+
hex: input.script
115+
};
116+
if (!options.noAsm) {
117+
transformed.scriptSig.asm = input.scriptAsm;
118+
}
119+
}
120+
112121
transformed.addr = input.address;
113122
transformed.valueSat = input.satoshis;
114123
transformed.value = input.satoshis / 1e8;
@@ -120,21 +129,25 @@ TxController.prototype.transformInput = function(input, index) {
120129
return transformed;
121130
};
122131

123-
TxController.prototype.transformOutput = function(output, index) {
132+
TxController.prototype.transformOutput = function(options, output, index) {
124133
var transformed = {
125134
value: (output.satoshis / 1e8).toFixed(8),
126135
n: index,
127136
scriptPubKey: {
128-
hex: output.script,
129-
asm: output.scriptAsm
130-
//reqSigs: null, // TODO
131-
},
132-
spentTxId: output.spentTxId || null,
133-
spentIndex: _.isUndefined(output.spentIndex) ? null : output.spentIndex,
134-
spentHeight: output.spentHeight || null
135-
//spentTs: undefined // TODO
137+
hex: output.script
138+
}
136139
};
137140

141+
if (!options.noAsm) {
142+
transformed.scriptPubKey.asm = output.scriptAsm;
143+
}
144+
145+
if (!options.noSpent) {
146+
transformed.spentTxId = output.spentTxId || null;
147+
transformed.spentIndex = _.isUndefined(output.spentIndex) ? null : output.spentIndex;
148+
transformed.spentHeight = output.spentHeight || null;
149+
}
150+
138151
if (output.address) {
139152
transformed.scriptPubKey.addresses = [output.address];
140153
var address = bitcore.Address(output.address); //TODO return type from bitcore-node

0 commit comments

Comments
 (0)