Wallet/js/services/controllerUtils.js

375 lines
11 KiB
JavaScript
Raw Normal View History

2014-04-17 11:46:49 -03:00
'use strict';
2014-06-12 17:42:26 -03:00
var bitcore = require('bitcore');
2014-04-17 11:46:49 -03:00
2014-06-03 17:42:36 -03:00
angular.module('copayApp.services')
.factory('controllerUtils', function($rootScope, $sce, $location, $filter, notification, $timeout, uriHandler, rateService) {
2014-08-22 13:13:04 -04:00
var root = {};
2014-05-07 19:04:36 -03:00
2014-10-10 17:58:19 -03:00
root.redirIfNotComplete = function() {
var w = $rootScope.wallet;
if (w) {
if (!w.isReady()) {
$location.path('/copayers');
}
2014-10-10 17:58:19 -03:00
} else {
$location.path('/');
}
};
2014-08-22 13:13:04 -04:00
root.redirIfLogged = function() {
var w = $rootScope.wallet;
if (w) {
if (!w.isReady()) {
$location.path('/copayers');
} else {
2014-10-31 11:49:52 -03:00
$location.path('homeWallet');
}
2014-08-22 13:13:04 -04:00
}
};
2014-08-06 18:41:37 -03:00
2014-08-22 13:13:04 -04:00
root.logout = function() {
2014-10-29 23:23:16 -03:00
if ($rootScope.iden) {
2014-10-30 16:20:25 -03:00
$rootScope.iden.store(null, function() {
2014-10-29 23:23:16 -03:00
$rootScope.iden.close();
2014-06-24 12:57:15 -03:00
2014-10-30 16:20:25 -03:00
delete $rootScope['wallet'];
delete $rootScope['iden'];
2014-04-23 18:07:20 -03:00
2014-10-31 16:14:52 -03:00
// Go home reloading the application
var hashIndex = window.location.href.indexOf('#!/');
window.location = window.location.href.substr(0, hashIndex);
2014-10-30 16:20:25 -03:00
});
}
2014-08-22 13:13:04 -04:00
};
2014-06-12 11:03:24 -03:00
2014-08-22 13:13:04 -04:00
root.onError = function(scope) {
if (scope) scope.loading = false;
2014-08-12 15:26:15 -04:00
}
2014-08-22 13:13:04 -04:00
root.onErrorDigest = function(scope, msg) {
root.onError(scope);
if (msg) {
notification.error('Error', msg);
2014-08-12 15:26:15 -04:00
}
2014-08-22 13:13:04 -04:00
};
2014-07-08 08:58:24 -03:00
root.isFocusedWallet = function(wid) {
return $rootScope.wallet && wid === $rootScope.wallet.getId();
};
root.updateTxsAndBalance = _.debounce(function(w) {
root.updateTxs();
root.updateBalance(w, function() {
$rootScope.$digest();
});
}, 3000);
root.installWalletHandlers = function($scope, w) {
var wid = w.getId();
w.on('connectionError', function() {
if (root.isFocusedWallet(wid)) {
var message = "Could not connect to the Insight server. Check your settings and network configuration";
notification.error('Networking Error', message);
root.onErrorDigest($scope);
}
2014-08-22 13:13:04 -04:00
});
2014-04-24 23:13:55 -03:00
2014-08-22 13:13:04 -04:00
w.on('corrupt', function(peerId) {
if (root.isFocusedWallet(wid)) {
notification.error('Error', $filter('translate')('Received corrupt message from ') + peerId);
}
2014-08-22 13:13:04 -04:00
});
w.on('ready', function(myPeerID) {
$scope.loading = false;
if ($rootScope.initialConnection) {
$rootScope.initialConnection = false;
if ($rootScope.pendingPayment) {
$location.path('paymentIntent');
} else {
root.redirIfLogged();
}
2014-08-22 13:13:04 -04:00
}
});
2014-05-16 18:48:17 -03:00
w.on('tx', function(address, isChange) {
if (!isChange) {
notification.funds('Funds received on ' + w.getName(), address);
}
root.updateBalance(w, function() {
$rootScope.$digest();
});
});
w.on('balanceUpdated', function() {
root.updateBalance(w, function() {
$rootScope.$digest();
});
});
2014-09-09 20:55:51 -03:00
w.on('insightReconnected', function() {
$rootScope.reconnecting = false;
root.updateAddressList(w.getId());
root.updateBalance(w, function() {
$rootScope.$digest();
});
});
2014-09-09 20:55:51 -03:00
w.on('insightError', function() {
if (root.isFocusedWallet(wid)) {
$rootScope.reconnecting = true;
$rootScope.$digest();
}
});
2014-10-27 14:53:40 -03:00
w.on('newAddresses', function() {
root.updateTxsAndBalance(w);
});
2014-10-27 14:53:40 -03:00
w.on('txProposalsUpdated', function() {
root.updateTxsAndBalance(w);
2014-08-22 13:13:04 -04:00
});
2014-08-22 13:13:04 -04:00
w.on('txProposalEvent', function(e) {
root.updateTxsAndBalance(w);
// TODO: add wallet name notification
2014-08-22 13:13:04 -04:00
var user = w.publicKeyRing.nicknameForCopayer(e.cId);
var name = w.getName();
2014-08-22 13:13:04 -04:00
switch (e.type) {
case 'new':
notification.info('['+ name +'] New Transaction',
$filter('translate')('You received a transaction proposal from') + ' ' + user);
break;
2014-08-22 13:13:04 -04:00
case 'signed':
notification.info('['+ name +'] Transaction Signed',
$filter('translate')('A transaction was signed by') + ' ' + user);
2014-08-22 13:13:04 -04:00
break;
case 'rejected':
notification.info('['+ name +'] Transaction Rejected',
$filter('translate')('A transaction was rejected by') + ' ' + user);
2014-08-22 13:13:04 -04:00
break;
case 'corrupt':
notification.error('['+ name +'] Transaction Error',
$filter('translate')('Received corrupt transaction from') + ' ' + user);
2014-08-22 13:13:04 -04:00
break;
}
$rootScope.$digest();
2014-08-22 13:13:04 -04:00
});
w.on('addressBookUpdated', function(dontDigest) {
if (root.isFocusedWallet(wid)) {
if (!dontDigest) {
$rootScope.$digest();
}
2014-08-22 13:13:04 -04:00
}
});
w.on('connect', function(peerID) {
$rootScope.$digest();
2014-08-22 13:13:04 -04:00
});
w.on('close', root.onErrorDigest);
w.on('locked', root.onErrorDigest.bind(this));
};
root.setupGlobalVariables = function(iden) {
notification.enableHtml5Mode(); // for chrome: if support, enable it
uriHandler.register();
$rootScope.unitName = config.unitName;
$rootScope.txAlertCount = 0;
$rootScope.initialConnection = true;
$rootScope.reconnecting = false;
$rootScope.isCollapsed = true;
$rootScope.iden = iden;
// TODO
// $rootScope.$watch('txAlertCount', function(txAlertCount) {
// if (txAlertCount && txAlertCount > 0) {
//
// notification.info('New Transaction', ($rootScope.txAlertCount == 1) ? 'You have a pending transaction proposal' : $filter('translate')('You have') + ' ' + $rootScope.txAlertCount + ' ' + $filter('translate')('pending transaction proposals'), txAlertCount);
// }
// });
};
root.rebindWallets = function($scope, iden) {
_.each(iden.listWallets(), function(wallet) {
preconditions.checkState(wallet);
root.installWalletHandlers($scope, wallet);
});
};
root.setPaymentWallet = function(w) {
root.setFocusedWallet(w);
$location.path('/send');
};
root.setFocusedWallet = function(w) {
if (!_.isObject(w))
w = $rootScope.iden.getWalletById(w);
2014-10-31 12:27:22 -03:00
preconditions.checkState(w && _.isObject(w));
$rootScope.wallet = w;
2014-10-31 12:27:22 -03:00
w.updateFocusedTimestamp(Date.now());
root.redirIfLogged();
root.updateTxs();
2014-10-31 12:27:22 -03:00
root.updateBalance(w, function() {
$rootScope.$digest();
})
2014-08-22 13:13:04 -04:00
};
root.bindProfile = function($scope, iden, w) {
root.setupGlobalVariables(iden);
root.rebindWallets($scope, iden);
2014-10-15 17:18:30 -03:00
if (w) {
root.setFocusedWallet(w);
} else {
$location.path('/manage');
}
2014-11-01 21:49:49 -03:00
$timeout(function() {
$rootScope.$digest()
2014-11-01 21:56:58 -03:00
}, 1);
};
// On the focused wallet
root.updateAddressList = function(wid) {
if (!wid || root.isFocusedWallet(wid)) {
var w = $rootScope.wallet;
if (w && w.isReady()) {
$rootScope.addrInfos = w.getAddressesInfo();
}
}
2014-08-22 13:13:04 -04:00
};
2014-10-16 15:06:39 -03:00
var _balanceCache = {};
2014-10-16 16:02:32 -03:00
root.clearBalanceCache = function(w) {
delete _balanceCache[w.getId()];
};
2014-11-03 15:47:35 -03:00
root._fetchBalance = function(w, cb) {
2014-10-20 10:29:43 -03:00
cb = cb || function() {};
var satToUnit = 1 / w.settings.unitToSatoshi;
var COIN = bitcore.util.COIN;
2014-10-16 15:06:39 -03:00
2014-11-03 15:47:35 -03:00
w.getBalance(function(err, balanceSat, balanceByAddrSat, safeBalanceSat, safeUnspentCount) {
2014-10-20 10:29:43 -03:00
if (err) return cb(err);
2014-10-16 15:06:39 -03:00
2014-10-20 10:29:43 -03:00
var r = {};
r.totalBalance = balanceSat * satToUnit;
r.totalBalanceBTC = (balanceSat / COIN);
r.availableBalance = safeBalanceSat * satToUnit;
2014-11-03 15:47:35 -03:00
r.safeUnspentCount = safeUnspentCount;
2014-10-20 10:29:43 -03:00
r.availableBalanceBTC = (safeBalanceSat / COIN);
2014-10-20 10:29:43 -03:00
r.lockedBalance = (balanceSat - safeBalanceSat) * satToUnit;
r.lockedBalanceBTC = (balanceSat - safeBalanceSat) / COIN;
var balanceByAddr = {};
for (var ii in balanceByAddrSat) {
balanceByAddr[ii] = balanceByAddrSat[ii] * satToUnit;
}
r.balanceByAddr = balanceByAddr;
root.updateAddressList();
2014-10-20 10:29:43 -03:00
if (rateService.isAvailable()) {
2014-10-20 10:29:43 -03:00
r.totalBalanceAlternative = rateService.toFiat(balanceSat, w.settings.alternativeIsoCode);
r.alternativeIsoCode = w.settings.alternativeIsoCode;
r.lockedBalanceAlternative = rateService.toFiat(balanceSat - safeBalanceSat, w.settings.alternativeIsoCode);
r.alternativeConversionRate = rateService.toFiat(100000000, w.settings.alternativeIsoCode);
r.alternativeBalanceAvailable = true;
};
r.updatingBalance = false;
return cb(null, r)
2014-10-20 10:29:43 -03:00
});
};
2014-11-03 15:47:35 -03:00
root._updateScope = function(w, data, scope, cb) {
_.each(data, function(v, k) {
scope[k] = data[k];
})
2014-10-20 10:29:43 -03:00
if (cb) return cb();
};
2014-11-06 12:47:54 -03:00
root.updateBalance = function(w, cb, refreshAll) {
2014-10-16 15:06:39 -03:00
w = w || $rootScope.wallet;
if (!w) return root.onErrorDigest();
if (!w.isReady()) return;
2014-10-20 10:29:43 -03:00
w.balanceInfo = {};
2014-11-06 12:47:54 -03:00
var scope = root.isFocusedWallet(w.id) && !refreshAll ? $rootScope : w.balanceInfo;
2014-10-20 10:29:43 -03:00
2014-10-17 13:14:41 -03:00
root.updateAddressList();
2014-10-16 15:06:39 -03:00
var wid = w.getId();
if (_balanceCache[wid]) {
2014-10-20 10:29:43 -03:00
root._updateScope(w, _balanceCache[wid], scope, function() {
2014-11-06 12:47:54 -03:00
if (root.isFocusedWallet(w.id) && !refreshAll) {
2014-10-20 10:29:43 -03:00
setTimeout(function() {
$rootScope.$digest();
}, 1);
}
});
2014-10-16 15:06:39 -03:00
} else {
2014-10-20 10:29:43 -03:00
scope.updatingBalance = true;
2014-10-16 15:06:39 -03:00
}
2014-11-03 15:47:35 -03:00
root._fetchBalance(w, function(err, res) {
2014-10-16 15:06:39 -03:00
if (err) throw err;
2014-10-20 10:29:43 -03:00
_balanceCache[wid] = res;
root._updateScope(w, _balanceCache[wid], scope, function() {
scope.updatingBalance = false;
2014-10-17 13:14:41 -03:00
if (cb) cb();
2014-10-16 15:06:39 -03:00
});
2014-08-22 13:13:04 -04:00
});
};
root.computeAlternativeAmount = function(w, tx, cb) {
rateService.whenAvailable(function() {
_.each(tx.outs, function(out) {
var valueSat = out.value * w.settings.unitToSatoshi;
out.alternativeAmount = rateService.toFiat(valueSat, w.settings.alternativeIsoCode);
out.alternativeIsoCode = w.settings.alternativeIsoCode;
});
if (cb) return cb(tx);
2014-08-22 13:13:04 -04:00
});
};
2014-09-29 17:36:34 -03:00
root.updateTxs = function() {
var w = $rootScope.wallet;
if (!w) return root.onErrorDigest();
var res = w.getPendingTxProposals();
_.each(res.txs, function(tx) {
root.computeAlternativeAmount(w, tx);
});
$rootScope.txps = res.txs;
if ($rootScope.pendingTxCount < res.pendingForUs) {
$rootScope.txAlertCount = res.pendingForUs;
2014-08-12 15:26:15 -04:00
}
$rootScope.pendingTxCount = res.pendingForUs;
2014-08-22 13:13:04 -04:00
};
2014-08-12 15:26:15 -04:00
root.deleteWallet = function($scope, w, skipBind) {
2014-10-21 14:26:58 -03:00
w = w || $rootScope.wallet;
2014-11-10 16:41:07 -03:00
var name = w.getName();
2014-10-21 14:26:58 -03:00
$rootScope.iden.deleteWallet(w.id, function() {
2014-11-10 16:41:07 -03:00
notification.info(name + ' deleted', $filter('translate')('Wallet deleted'));
if (skipBind) return skipBind();
2014-10-21 14:26:58 -03:00
$rootScope.wallet = null;
var lastFocused = $rootScope.iden.getLastFocusedWallet();
2014-10-21 14:26:58 -03:00
root.bindProfile($scope, $rootScope.iden, lastFocused);
});
};
2014-08-22 13:13:04 -04:00
return root;
});