From 0567714628fa7a4bdd8bda66c449de51026bda36 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sun, 2 Nov 2014 19:17:55 -0300 Subject: [PATCH] mv filter to Insight --- js/models/Insight.js | 11 +++++++++-- js/models/Wallet.js | 36 ++++++++++-------------------------- 2 files changed, 19 insertions(+), 28 deletions(-) diff --git a/js/models/Insight.js b/js/models/Insight.js index 2a07a56d7..091ccfb47 100644 --- a/js/models/Insight.js +++ b/js/models/Insight.js @@ -5,6 +5,7 @@ var async = require('async'); var request = require('request'); var bitcore = require('bitcore'); var io = require('socket.io-client'); +var _ = require('lodash'); var log = require('../log'); var EventEmitter = require('events').EventEmitter; @@ -310,9 +311,15 @@ Insight.prototype.getUnspent = function(addresses, cb) { this.requestPost('/api/addrs/utxo', { addrs: addresses.join(',') - }, function(err, res, body) { + }, function(err, res, unspentRaw) { if (err || res.statusCode != 200) return cb(err || res); - cb(null, body); + + // This filter out possible broken unspent, as reported on + // https://github.com/bitpay/copay/issues/1585 + // and later gitter conversation. + + var unspent = _.filter(unspentRaw, 'scriptPubKey'); + cb(null, unspent); }); }; diff --git a/js/models/Wallet.js b/js/models/Wallet.js index 5d038277e..37bb4cb42 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -1879,15 +1879,9 @@ Wallet.prototype.createPaymentTxSync = function(options, merchantData, unspent) merchantData.total = merchantData.total.toString(10); - var b; - try { - b = new Builder(opts) - .setUnspent(unspent) - .setOutputs(outs); - } catch (e) { - log.debug(e.message); - return; - }; + var b = new Builder(opts) + .setUnspent(unspent) + .setOutputs(outs); merchantData.pr.pd.outputs.forEach(function(output, i) { var script = { @@ -2172,17 +2166,11 @@ Wallet.prototype.getBalance = function(cb) { var balanceByAddr = {}; var COIN = coinUtil.COIN; - this.getUnspent(function(err, safeUnspent, unspentRaw) { + this.getUnspent(function(err, safeUnspent, unspent) { if (err) { return cb(err); } - // This filter out possible broken unspent, as reported on - // https://github.com/bitpay/copay/issues/1585 - // and later gitter conversation. - - var unspent = _.filter(unspentRaw, 'scriptPubKey'); - for (var i = 0; i < unspent.length; i++) { var u = unspent[i]; var amt = u.amount * COIN; @@ -2371,16 +2359,12 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, comment, utxos opts[k] = Wallet.builderOpts[k]; } - var b = new Builder(opts); - - b.setUnspent(utxos); - -console.log('[Wallet.js.2370:utxos:]',utxos); //TODO - - b.setOutputs([{ - address: toAddress.data, - amountSatStr: amountSatStr, - }]); + var b = new Builder(opts) + .setUnspent(utxos) + .setOutputs([{ + address: toAddress.data, + amountSatStr: amountSatStr, + }]); var selectedUtxos = b.getSelectedUnspent(); var inputChainPaths = selectedUtxos.map(function(utxo) {