diff --git a/lib/explorer.js b/lib/explorer.js index b337ce0e..5c5be8c3 100644 --- a/lib/explorer.js +++ b/lib/explorer.js @@ -73,7 +73,7 @@ function processVoutAddresses(address_list, vout_value, arr_vout, cb) { // check if there are any addresses to process if (address_list != null && address_list.length > 0) { // check if vout address is unique, if so add to array, if not add its amount to existing index - module.exports.is_unique(arr_vout, address_list[0], function(unique, index) { + module.exports.is_unique(arr_vout, address_list[0], 'addresses', function(unique, index) { if (unique == true) { // unique vout module.exports.convert_to_satoshi(parseFloat(vout_value), function(amount_sat) { @@ -980,14 +980,14 @@ module.exports = { }); }, - is_unique: function(array, object, cb) { + is_unique: function(array, object, key_name, cb) { var unique = true; var index = null; module.exports.syncLoop(array.length, function (loop) { var i = loop.iteration(); - if (array[i].addresses == object) { + if (array[i][key_name] == object) { unique = false; index = i; loop.break(true); @@ -1044,18 +1044,24 @@ module.exports = { }); } else { // could not decipher the address, save as unknown and move to next vout - module.exports.convert_to_satoshi(parseFloat(vout[i].value), function(amount_sat) { - console.log('Failed to find vout address from tx ' + txid); - arr_vout.push({addresses: 'unknown_address', amount: amount_sat}); + console.log('Failed to find vout address from tx ' + txid); + // process vout addresses + processVoutAddresses(['unknown_address'], vout[i].value, arr_vout, function(vout_array) { + // save updated array + arr_vout = vout_array; + // move to next vout loop.next(); }); } }); } else { // could not decipher the address, save as unknown and move to next vout - module.exports.convert_to_satoshi(parseFloat(vout[i].value), function(amount_sat) { - console.log('Failed to find vout address from tx ' + txid); - arr_vout.push({addresses: 'unknown_address', amount: amount_sat}); + console.log('Failed to find vout address from tx ' + txid); + // process vout addresses + processVoutAddresses(['unknown_address'], vout[i].value, arr_vout, function(vout_array) { + // save updated array + arr_vout = vout_array; + // move to next vout loop.next(); }); } @@ -1105,7 +1111,7 @@ module.exports = { // save updated array arr_vout = vout_array; // add a private send address with the known amount sent - module.exports.is_unique(arr_vin, 'hidden_address', function(unique, index) { + module.exports.is_unique(arr_vin, 'hidden_address', 'addresses', function(unique, index) { if (unique == true) { module.exports.convert_to_satoshi(parseFloat(vhidden[i].vpub_new), function(amount_sat) { arr_vin.push({addresses: 'hidden_address', amount: amount_sat}); @@ -1122,7 +1128,7 @@ module.exports = { }); }); } else { - module.exports.is_unique(arr_vin, 'hidden_address', function(unique, index) { + module.exports.is_unique(arr_vin, 'hidden_address', 'addresses', function(unique, index) { if (unique == true) { module.exports.convert_to_satoshi(parseFloat(vhidden[i].vpub_new), function(amount_sat) { arr_vin.push({addresses: 'hidden_address', amount: amount_sat}); @@ -1185,10 +1191,15 @@ module.exports = { if (tx.vout[i].n == input.vout) { if (tx.vout[i].scriptPubKey.addresses) { - addresses.push({hash: tx.vout[i].scriptPubKey.addresses[0], amount:tx.vout[i].value}); + module.exports.is_unique(addresses, tx.vout[i].scriptPubKey.addresses[0], 'hash', function(unique, index) { + if (unique == true) + addresses.push({hash: tx.vout[i].scriptPubKey.addresses[0], amount: tx.vout[i].value}); + else + addresses[index].amount = addresses[index].amount + tx.vout[i].value; - loop.break(true); - loop.next(); + loop.break(true); + loop.next(); + }); } else { // no addresses defined // check if bitcoin features are enabled @@ -1200,23 +1211,41 @@ module.exports = { // mark this tx as p2pk tx_type = 'p2pk'; // save the P2PKH address - addresses.push({hash: p2pkh_address, amount: tx.vout[i].value}); + module.exports.is_unique(addresses, p2pkh_address, 'hash', function(unique, index) { + if (unique == true) + addresses.push({hash: p2pkh_address, amount: tx.vout[i].value}); + else + addresses[index].amount = addresses[index].amount + tx.vout[i].value; + + loop.break(true); + loop.next(); + }); } else { // could not decipher the address, save as unknown and move to next vin - addresses.push({hash: 'unknown_address', amount: tx.vout[i].value}); console.log('Failed to find vin address from tx ' + input.txid); - } + module.exports.is_unique(addresses, 'unknown_address', 'hash', function(unique, index) { + if (unique == true) + addresses.push({hash: 'unknown_address', amount: tx.vout[i].value}); + else + addresses[index].amount = addresses[index].amount + tx.vout[i].value; - loop.break(true); - loop.next(); + loop.break(true); + loop.next(); + }); + } }); } else { // could not decipher the address, save as unknown and move to next vin - addresses.push({hash: 'unknown_address', amount: tx.vout[i].value}); console.log('Failed to find vin address from tx ' + input.txid); + module.exports.is_unique(addresses, 'unknown_address', 'hash', function(unique, index) { + if (unique == true) + addresses.push({hash: 'unknown_address', amount: tx.vout[i].value}); + else + addresses[index].amount = addresses[index].amount + tx.vout[i].value; - loop.break(true); - loop.next(); + loop.break(true); + loop.next(); + }); } } } else @@ -1245,10 +1274,10 @@ module.exports = { } if (addresses && addresses.length) { - module.exports.is_unique(arr_vin, addresses[0].hash, function(unique, index) { + module.exports.is_unique(arr_vin, addresses[0].hash, 'addresses', function(unique, index) { if (unique == true) { module.exports.convert_to_satoshi(parseFloat(addresses[0].amount), function(amount_sat) { - arr_vin.push({addresses:addresses[0].hash, amount:amount_sat}); + arr_vin.push({addresses: addresses[0].hash, amount: amount_sat}); loop.next(); }); } else { @@ -1261,8 +1290,12 @@ module.exports = { } else { // could not decipher the address, save as unknown and move to next vin console.log('Failed to find vin address from tx ' + tx.txid); - arr_vin.push({addresses: 'unknown_address', amount: 0}); - loop.next(); + module.exports.is_unique(arr_vin, 'unknown_address', 'addresses', function(unique, index) { + if (unique == true) + arr_vin.push({addresses: 'unknown_address', amount: 0}); + + loop.next(); + }); } }); }, function() { diff --git a/test/explorerSpec.js b/test/explorerSpec.js index becc6547..15abbec7 100644 --- a/test/explorerSpec.js +++ b/test/explorerSpec.js @@ -38,28 +38,28 @@ describe('explorer', function() { ]; it('should return index of matching string object', function() { - lib.is_unique(arrayStrMap, arrayStrMap[2].addresses, function(unique, index) { + lib.is_unique(arrayStrMap, arrayStrMap[2].addresses, 'addresses', function(unique, index) { expect(index).toEqual(2); expect(unique).toEqual(false); }); }); it('should return index of matching array object', function() { - lib.is_unique(arrayArrMap, arrayArrMap[2].addresses, function(unique, index) { + lib.is_unique(arrayArrMap, arrayArrMap[2].addresses, 'addresses', function(unique, index) { expect(index).toEqual(2); expect(unique).toEqual(false); }); }); it('should return true if no matching string object', function() { - lib.is_unique(arrayStrMap, 'unique', function(unique, index) { + lib.is_unique(arrayStrMap, 'unique', 'addresses', function(unique, index) { expect(index).toEqual(null); expect(unique).toEqual(true); }); }); it('should return true if no matching array object', function() { - lib.is_unique(arrayArrMap, ['unique'], function(unique, index) { + lib.is_unique(arrayArrMap, ['unique'], 'addresses', function(unique, index) { expect(index).toEqual(null); expect(unique).toEqual(true); });