diff --git a/js/models/blockchain/Insight.js b/js/models/blockchain/Insight.js index 21e4262ca..50c5ac834 100644 --- a/js/models/blockchain/Insight.js +++ b/js/models/blockchain/Insight.js @@ -71,7 +71,6 @@ Insight.prototype.getTransactions = function(addresses, cb) { scheme: self.scheme, method: 'GET', path: '/api/addr/' + addr, - headers: { 'Access-Control-Request-Headers': '' } @@ -108,7 +107,7 @@ Insight.prototype.getTransactions = function(addresses, cb) { }; Insight.prototype.getUnspent = function(addresses, cb) { - if (!addresses || !addresses.length) return cb([]); + if (!addresses || !addresses.length) return cb(null, []); var all = []; @@ -125,10 +124,15 @@ Insight.prototype.getUnspent = function(addresses, cb) { }; this._request(options, function(err, res) { + if (err) { + return cb(err); + } + if (res && res.length > 0) { all = all.concat(res); } - return cb(all); + + return cb(null, all); }); }; @@ -142,7 +146,7 @@ Insight.prototype.sendRawTransaction = function(rawtx, cb) { path: '/api/tx/send', data: 'rawtx=' + rawtx, headers: { - 'content-type': 'application/x-www-form-urlencoded' + 'Access-Control-Request-Headers': '' } }; this._request(options, function(err, res) { @@ -172,10 +176,24 @@ Insight.prototype._request = function(options, callback) { } request.open(options.method, url, true); + request.timeout = 10000; + request.ontimeout = function() { + return callback({ + message: 'Insight request timeout. Please check your Insight settings or the Insight server status.' + }); + }; + request.onreadystatechange = function() { if (request.readyState === 4) { - if (request.status === 200) { - return callback(null, JSON.parse(request.responseText)); + if (request.status === 200 || request.status === 304 || request.status === 0) { + try { + var ret = JSON.parse(request.responseText); + return callback(null, ret); + } catch (e) { + return callback({ + message: 'Wrong response from insight' + }); + } } else { return callback({ message: 'Error code: ' + request.status + ' - Status: ' + request.statusText + ' - Description: ' + request.responseText @@ -186,42 +204,39 @@ Insight.prototype._request = function(options, callback) { if (options.method === 'POST') { request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); - request.send(options.data); - } else { - request.send(null); } + + request.send(options.data || null); } else { var http = require('http'); var req = http.request(options, function(response) { var ret; - if (response.statusCode == 200) { + if (response.statusCode == 200 || response.status === 304) { response.on('data', function(chunk) { try { ret = JSON.parse(chunk); } catch (e) { - callback({ - message: "Wrong response from insight" + return callback({ + message: 'Wrong response from insight' }); - return; } }); response.on('end', function() { - callback(undefined, ret); - return; + return callback(null, ret); }); } else { - callback({ + return callback({ message: 'Error ' + response.statusCode }); - return; } }); + if (options.data) { req.write(options.data); } + req.end(); } -} - +}; module.exports = require('soop')(Insight); diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index 58008ae79..65827aecc 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -501,7 +501,11 @@ Wallet.prototype.getBalance = function(cb) { var balanceByAddr = {}; var COIN = bitcore.util.COIN; - this.getUnspent(function(safeUnspent, unspent) { + this.getUnspent(function(err, safeUnspent, unspent) { + if (err) { + return cb(err); + } + for (var i = 0; i < unspent.length; i++) { var u = unspent[i]; var amt = u.amount * COIN; @@ -520,14 +524,19 @@ Wallet.prototype.getBalance = function(cb) { var amt = u.amount * COIN; safeBalance += amt; } + safeBalance = safeBalance / COIN; - return cb(balance, balanceByAddr, safeBalance); + return cb(null, balance, balanceByAddr, safeBalance); }); }; Wallet.prototype.getUnspent = function(cb) { var self = this; - this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) { + this.blockchain.getUnspent(this.getAddressesStr(), function(err, unspentList) { + + if (err) { + return cb(err); + } var safeUnspendList = []; var maxRejectCount = self.totalCopayers - self.requiredCopayers; @@ -537,7 +546,8 @@ Wallet.prototype.getUnspent = function(cb) { if (uu.indexOf(unspentList[i].txid) === -1) safeUnspendList.push(unspentList[i]); } - return cb(safeUnspendList, unspentList); + + return cb(null, safeUnspendList, unspentList); }); }; @@ -554,7 +564,7 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) { opts.spendUnconfirmed = this.spendUnconfirmed; } - self.getUnspent(function(safeUnspent) { + this.getUnspent(function(err, safeUnspent) { var ntxid = self.createTxSync(toAddress, amountSatStr, safeUnspent, opts); if (ntxid) { self.sendPublicKeyRing(); diff --git a/js/services/controllerUtils.js b/js/services/controllerUtils.js index d0298f0d7..4548017c6 100644 --- a/js/services/controllerUtils.js +++ b/js/services/controllerUtils.js @@ -114,7 +114,19 @@ angular.module('copay.controllerUtils') $rootScope.balanceByAddr = {}; $rootScope.updatingBalance = true; - w.getBalance(function(balance, balanceByAddr, safeBalance) { + w.getBalance(function(err, balance, balanceByAddr, safeBalance) { + if (err) { + $rootScope.$flashMessage = { + type: 'error', + message: 'Error: ' + err.message + }; + + $rootScope.$digest(); + console.error('Error: ' + err.message); //TODO + + return null; + } + $rootScope.totalBalance = balance; $rootScope.balanceByAddr = balanceByAddr; $rootScope.availableBalance = safeBalance;