balance Service

This commit is contained in:
Matias Alejo Garcia 2014-11-29 18:35:48 -03:00
commit 57299d675e
31 changed files with 585 additions and 645 deletions

View file

@ -1,7 +1,8 @@
'use strict';
angular.module('copayApp.controllers').controller('CopayersController',
function($scope, $rootScope, $location, controllerUtils) {
function($scope, $rootScope, $location) {
if (!$rootScope.wallet.isReady()) {
$rootScope.title = 'Waiting copayers for ' + $rootScope.wallet.getName();
}
@ -9,9 +10,7 @@ angular.module('copayApp.controllers').controller('CopayersController',
$scope.secret = $rootScope.wallet.getSecret();
$scope.goToWallet = function() {
controllerUtils.updateAddressList();
$location.path('/homeWallet');
};
$scope.copayersList = function() {

View file

@ -1,10 +1,9 @@
'use strict';
angular.module('copayApp.controllers').controller('CreateController',
function($scope, $rootScope, $location, $timeout, controllerUtils, backupService, notification, defaults) {
function($scope, $rootScope, $location, $timeout, identityService, backupService, notification, defaults) {
$rootScope.fromSetup = true;
$rootScope.starting = false;
$scope.loading = false;
$scope.walletPassword = $rootScope.walletPassword;
$scope.isMobile = !!window.cordova;
@ -53,10 +52,8 @@ angular.module('copayApp.controllers').controller('CreateController',
privateKeyHex: $scope.private,
networkName: $scope.networkName,
};
$rootScope.iden.createWallet(opts, function(err, w) {
identityService.createWallet(opts, function(){
$scope.loading = false;
controllerUtils.installWalletHandlers($scope, w);
controllerUtils.setFocusedWallet(w);
});
};
});

View file

@ -1,14 +1,15 @@
'use strict';
angular.module('copayApp.controllers').controller('CreateProfileController', function($scope, $rootScope, $location, notification, controllerUtils, pluginManager, identityService) {
controllerUtils.redirIfLogged();
angular.module('copayApp.controllers').controller('CreateProfileController', function($scope, $rootScope, $location, notification, pluginManager, identityService) {
identityService.goWalletHome();
$scope.loading = false;
$scope.createProfile = function(form) {
if (form && form.$invalid) {
$scope.error('Error', 'Please enter the required fields');
return;
}
$rootScope.starting = true;
identityService.create($scope, form);
}

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('HeadController', function($scope, $rootScope, $filter, $timeout, notification, controllerUtils) {
angular.module('copayApp.controllers').controller('HeadController', function($scope, $rootScope, $filter, $timeout, notification, identityService, balanceService) {
$scope.username = $rootScope.iden.getName();
$scope.hoverMenu = false;
@ -14,7 +14,7 @@ angular.module('copayApp.controllers').controller('HeadController', function($sc
$scope.signout = function() {
$rootScope.signingOut = true;
controllerUtils.logout();
identityService.logout();
};
$scope.refresh = function() {
@ -23,12 +23,10 @@ angular.module('copayApp.controllers').controller('HeadController', function($sc
if (w.isReady()) {
w.sendWalletReady();
if ($rootScope.addrInfos.length > 0) {
controllerUtils.clearBalanceCache(w);
controllerUtils.updateBalance(w, function() {
$rootScope.$digest();
});
}
balanceService.clearBalanceCache(w);
balanceService.update(w, function() {
$rootScope.$digest();
}, true);
}
};

View file

@ -2,9 +2,7 @@
var bitcore = require('bitcore');
angular.module('copayApp.controllers').controller('HistoryController',
function($scope, $rootScope, $timeout, controllerUtils, notification, rateService) {
controllerUtils.redirIfNotComplete();
function($scope, $rootScope) {
var w = $rootScope.wallet;
$rootScope.title = 'History';
@ -19,15 +17,11 @@ angular.module('copayApp.controllers').controller('HistoryController',
$scope.blockchain_txs = [];
$scope.alternativeCurrency = [];
$scope.selectPage = function(page) {
$scope.currentPage = page;
$scope.update();
};
$scope.downloadHistory = function() {
var w = $rootScope.wallet;
if (!w) return;

View file

@ -1,8 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, notification, controllerUtils, pluginManager, identityService, Compatibility) {
controllerUtils.redirIfLogged();
angular.module('copayApp.controllers').controller('HomeController', function($scope, $rootScope, $location, notification, identityService, Compatibility) {
// This is only for backwards compat, insight api should link to #!/confirmed directly
if (getParam('confirmed')) {
var hashIndex = window.location.href.indexOf('/?');
@ -24,7 +22,6 @@ angular.module('copayApp.controllers').controller('HomeController', function($sc
$scope.error = 'Please enter the required fields';
return;
}
$rootScope.starting = true;
identityService.open($scope, form);
}

View file

@ -1,11 +1,13 @@
'use strict';
angular.module('copayApp.controllers').controller('HomeWalletController',
function($scope, $rootScope, $timeout, $modal, controllerUtils) {
controllerUtils.redirIfNotComplete();
$rootScope.starting = false;
function($scope, $rootScope) {
$rootScope.title = 'Home';
$scope.addr = _.last($rootScope.wallet.getReceiveAddresses());
// This is necesarry, since wallet can change in homeWallet, without running init() again.
$rootScope.$watch('wallet', function() {
$scope.addr = _.last($rootScope.wallet.getReceiveAddresses());
});
}
);

View file

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('ImportController',
function($scope, $rootScope, $location, controllerUtils, notification, isMobile, Compatibility) {
function($scope, $rootScope, $location, identityService, notification, isMobile, Compatibility) {
$rootScope.title = 'Import wallet';
$scope.importStatus = 'Importing wallet - Reading backup...';
@ -18,28 +18,18 @@ angular.module('copayApp.controllers').controller('ImportController',
$scope.$digest();
}
$scope._doImport = function(encryptedObj, password) {
updateStatus('Importing wallet - Procesing backup...');
copay.Compatibility.importEncryptedWallet($rootScope.iden, encryptedObj,
$scope.password, $scope.importOpts, function(err, wallet) {
if (err) {
$scope.loading = false;
$scope.error = 'Could not read wallet. Please check your password';
} else {
controllerUtils.installWalletHandlers($scope, wallet);
controllerUtils.setFocusedWallet(wallet);
}
}
);
};
$scope.getFile = function() {
// If we use onloadend, we need to check the readyState.
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
var encryptedObj = evt.target.result;
$scope._doImport(encryptedObj, $scope.password);
updateStatus('Importing wallet - Procesing backup...');
identityService.importWallet(encryptedObj, $scope.password, {}, function(err){
if (err) {
$scope.loading = false;
$scope.error = 'Could not read wallet. Please check your password';
}
});
}
};
};
@ -85,8 +75,14 @@ angular.module('copayApp.controllers').controller('ImportController',
if (backupFile) {
reader.readAsBinaryString(backupFile);
} else {
$scope._doImport(backupText, $scope.password);
copay.Compatibility.deleteOldWallet(backupOldWallet);
updateStatus('Importing wallet - Procesing backup...');
identityService.importWallet(encryptedObj, $scope.password, $scope.importOpts, function(err){
if (err) {
$scope.loading = false;
$scope.error = 'Could not read wallet. Please check your password';
}
copay.Compatibility.deleteOldWallet(backupOldWallet);
});
}
};
});

View file

@ -1,9 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('ImportProfileController',
function($scope, $rootScope, $location, controllerUtils, notification, isMobile, pluginManager, identityService) {
controllerUtils.redirIfLogged();
function($scope, $rootScope, $location, notification, isMobile, pluginManager, identityService) {
$scope.title = 'Import a backup';
$scope.importStatus = 'Importing wallet - Reading backup...';
$scope.hideAdv = true;
@ -41,7 +39,7 @@ angular.module('copayApp.controllers').controller('ImportProfileController',
} else {
var firstWallet = iden.getLastFocusedWallet();
controllerUtils.bindProfile($scope, iden, firstWallet);
root.bind($scope, iden, firstWallet);
}
});
};

View file

@ -1,7 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('JoinController',
function($scope, $rootScope, $timeout, isMobile, controllerUtils, notification) {
function($scope, $rootScope, $timeout, isMobile, notification) {
$rootScope.fromSetup = false;
$scope.loading = false;
$scope.isMobile = isMobile.any();
@ -119,31 +119,12 @@ angular.module('copayApp.controllers').controller('JoinController',
}
$scope.loading = true;
$rootScope.iden.joinWallet({
identityService.joinWallet({
secret: $scope.connectionId,
nickname: $scope.nickname,
privateHex: $scope.private,
}, function(err, w) {
}, function(err) {
$scope.loading = false;
if (err || !w) {
if (err === 'joinError')
notification.error('Fatal error connecting to Insight server');
else if (err === 'walletFull')
notification.error('The wallet is full');
else if (err === 'badNetwork')
notification.error('Network Error', 'Wallet network configuration missmatch');
else if (err === 'badSecret')
notification.error('Bad secret', 'The secret string you entered is invalid');
else {
notification.error('Error', err.message || err);
}
controllerUtils.onErrorDigest();
} else {
controllerUtils.installWalletHandlers($scope, w);
controllerUtils.setFocusedWallet(w);
}
});
}
});

View file

@ -1,8 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('MoreController',
function($scope, $rootScope, $location, $filter, controllerUtils, notification, rateService) {
controllerUtils.redirIfNotComplete();
function($scope, $rootScope, $location, $filter, balanceService, notification, rateService) {
var w = $rootScope.wallet;
$scope.isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
@ -76,7 +75,7 @@ angular.module('copayApp.controllers').controller('MoreController',
alternativeIsoCode: $scope.selectedAlternative.isoCode,
});
notification.success('Success', $filter('translate')('settings successfully updated'));
controllerUtils.updateBalance(w, function() {
balanceService.update(w, function() {
$rootScope.$digest();
});
};
@ -84,9 +83,9 @@ angular.module('copayApp.controllers').controller('MoreController',
$scope.purge = function(deleteAll) {
var removed = w.purgeTxProposals(deleteAll);
if (removed) {
controllerUtils.updateBalance(w, function() {
balanceService.update(w, function() {
$rootScope.$digest();
});
}, true);
}
notification.info('Transactions Proposals Purged', removed + ' ' + $filter('translate')('transaction proposal purged'));
};
@ -99,12 +98,11 @@ angular.module('copayApp.controllers').controller('MoreController',
if (err) {
notification.error('Error', $filter('translate')('Error updating indexes: ') + err);
}
controllerUtils.updateAddressList();
controllerUtils.updateBalance(w, function() {
balanceService.update(w, function() {
notification.info('Finished', 'The balance is updated using the derived addresses');
w.sendIndexes();
$rootScope.$digest();
});
}, true);
});
};
});

View file

@ -1,26 +1,17 @@
'use strict';
var bitcore = require('bitcore');
angular.module('copayApp.controllers').controller('PaymentIntentController', function($rootScope, $scope, $modal, $location, controllerUtils) {
angular.module('copayApp.controllers').controller('PaymentIntentController', function($rootScope, $scope, $modal, $location, balanceService) {
$scope.wallets = [];
$rootScope.title = 'Payment intent';
$rootScope.starting = true;
$scope.wallets = rootScope.iden.listWallets();
var wids = _.pluck($rootScope.iden.listWallets(), 'id');
_.each(wids, function(wid) {
var w = $rootScope.iden.getWalletById(wid);
if (w && w.isReady()) {
$scope.wallets.push(w);
$rootScope.starting = false;
controllerUtils.clearBalanceCache(w);
controllerUtils.updateBalance(w, function() {
var l = $scope.wallet.length;
_.each($scope.wallets, function(w, i) {
balanceService.update(w, function(){
if (i === l-1)
$rootScope.$digest();
}, true);
}
});
});
$scope.open = function() {
@ -39,10 +30,10 @@ angular.module('copayApp.controllers').controller('PaymentIntentController', fun
// Please note that $modalInstance represents a modal window (instance) dependency.
// It is not the same as the $modal service used above.
var ModalInstanceCtrl = function($scope, $modalInstance, items, controllerUtils) {
var ModalInstanceCtrl = function($scope, $modalInstance, items, identityService) {
$scope.wallets = items;
$scope.ok = function(selectedItem) {
controllerUtils.setPaymentWallet(selectedItem);
identityService.setPaymentWallet(selectedItem);
$modalInstance.close();
};

View file

@ -1,5 +1,5 @@
'use strict';
angular.module('copayApp.controllers').controller('ProfileController', function($scope, $rootScope, $location, $modal, controllerUtils, backupService) {
angular.module('copayApp.controllers').controller('ProfileController', function($scope, $rootScope, $location, $modal, backupService) {
$scope.username = $rootScope.iden.getName();
$scope.isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
@ -14,28 +14,11 @@ angular.module('copayApp.controllers').controller('ProfileController', function(
$scope.hideViewProfileBackup = true;
};
$scope.getWallets = function() {
if (!$rootScope.iden) return;
$scope.wallets = [];
var wids = _.pluck($rootScope.iden.listWallets(), 'id');
_.each(wids, function(wid) {
var w = $rootScope.iden.getWalletById(wid);
$scope.wallets.push(w);
controllerUtils.updateBalance(w, function() {
$rootScope.$digest();
}, true);
});
};
$scope.deleteWallet = function(w) {
if (!w) return;
$scope.loading = w.id;
controllerUtils.deleteWallet($scope, w, function() {
if ($rootScope.wallet.id === w.id) {
$rootScope.wallet = null;
var lastFocused = $rootScope.iden.getLastFocusedWallet();
controllerUtils.bindProfile($scope, $rootScope.iden, lastFocused);
}
identityService.deleteWallet(w.id,function() {
$scope.loading = false;
$scope.getWallets();
});

View file

@ -1,9 +1,7 @@
'use strict';
angular.module('copayApp.controllers').controller('ReceiveController',
function($scope, $rootScope, $timeout, $modal, controllerUtils) {
controllerUtils.redirIfNotComplete();
function($scope, $rootScope, $timeout, $modal) {
$rootScope.title = 'Receive';
$scope.loading = false;
$scope.showAll = false;
@ -15,7 +13,6 @@ angular.module('copayApp.controllers').controller('ReceiveController',
$scope.isNewAddr = false;
w.generateAddress(null);
$timeout(function() {
controllerUtils.updateAddressList();
$scope.loading = false;
$scope.isNewAddr = true;
}, 1);
@ -74,20 +71,22 @@ angular.module('copayApp.controllers').controller('ReceiveController',
$scope.addressList = function() {
$scope.addresses = [];
var w = $rootScope.wallet;
var balance = $rootScope.balanceByAddr;
if ($rootScope.addrInfos) {
var addrInfos = $rootScope.addrInfos;
$scope.addrLength = addrInfos.length;
for (var i = 0; i < addrInfos.length; i++) {
var addrinfo = addrInfos[i];
var addresses = w.getAddresses();
if (addresses) {
$scope.addrLength = addresses.length;
_.each(addresses, function(address, index){
$scope.addresses.push({
'index': i,
'address': addrinfo.addressStr,
'balance': $rootScope.balanceByAddr ? $rootScope.balanceByAddr[addrinfo.addressStr] : 0,
'isChange': addrinfo.isChange,
'owned': addrinfo.owned
'index': index,
'address': address,
'balance': balance ? balance[address] : 0,
'isChange': w.addressIsChange(address),
// TODO
'owned': w.addressIsOwn(address),
});
}
});
$scope.addresses = $scope.limitAddress($scope.addresses, $scope.isNewAddr);
}
};

View file

@ -3,10 +3,7 @@ var bitcore = require('bitcore');
var preconditions = require('preconditions').singleton();
angular.module('copayApp.controllers').controller('SendController',
function($scope, $rootScope, $window, $timeout, $modal, isMobile, notification, controllerUtils, rateService) {
controllerUtils.redirIfNotComplete();
function($scope, $rootScope, $window, $timeout, $modal, isMobile, notification, rateService) {
var w = $rootScope.wallet;
preconditions.checkState(w);
preconditions.checkState(w.settings.unitToSatoshi);
@ -33,6 +30,34 @@ angular.module('copayApp.controllers').controller('SendController',
$scope.$digest();
});
$scope.setAlternativeAmount = 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);
});
};
$scope.updateTxs = function() {
var w = $rootScope.wallet;
if (!w) return;
var res = w.getPendingTxProposals();
_.each(res.txs, function(tx) {
$scope.setAlternativeAmount(w, tx);
if (tx.merchant) {
var url = tx.merchant.request_url;
var domain = /^(?:https?)?:\/\/([^\/:]+).*$/.exec(url)[1];
tx.merchant.domain = domain;
}
});
$scope.txps = res.txs;
// TODO
// $rootScope.pendingTxCount = res.pendingForUs;
};
/**
* Setting the two related amounts as properties prevents an infinite
@ -76,7 +101,7 @@ angular.module('copayApp.controllers').controller('SendController',
$scope.loadTxs = function() {
controllerUtils.updateTxs();
$scope.updateTxs();
setTimeout(function() {
$scope.loading = false;
$rootScope.$digest();

View file

@ -1,8 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('SettingsController', function($scope, $rootScope, $window, $route, $location, $anchorScroll, controllerUtils, notification) {
controllerUtils.redirIfLogged();
angular.module('copayApp.controllers').controller('SettingsController', function($scope, $rootScope, $window, $route, $location, $anchorScroll, notification) {
$scope.title = 'Settings';
$scope.defaultLanguage = config.defaultLanguage || 'en';
$scope.insightLivenet = config.network.livenet.url;

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('SidebarController', function($scope, $rootScope, $location, controllerUtils) {
angular.module('copayApp.controllers').controller('SidebarController', function($scope, $rootScope, $location, $timeout, identityService) {
$scope.menu = [{
'title': 'Home',
@ -24,21 +24,6 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
'link': 'more'
}];
$scope.refresh = function() {
var w = $rootScope.wallet;
if (!w) return;
if (w.isReady()) {
w.sendWalletReady();
if ($rootScope.addrInfos.length > 0) {
controllerUtils.clearBalanceCache(w);
controllerUtils.updateBalance(w, function() {
$rootScope.$digest();
});
}
}
};
$scope.signout = function() {
$scope.$emit('signout');
};
@ -47,35 +32,32 @@ angular.module('copayApp.controllers').controller('SidebarController', function(
return item.link && item.link == $location.path().split('/')[1];
};
if ($rootScope.wallet) {
$rootScope.$watch('wallet.id', function() {
$scope.walletSelection = false;
$scope.getWallets();
});
}
$scope.switchWallet = function(wid) {
controllerUtils.setFocusedWallet(wid);
identityService.setFocusedWallet(wid);
};
$scope.toggleWalletSelection = function() {
$scope.walletSelection = !$scope.walletSelection;
if (!$scope.walletSelection) return;
$scope.getWallets();
$scope.setWallets();
};
$scope.getWallets = function() {
$scope.init = function() {
if ($rootScope.wallet) {
$rootScope.$watch('wallet', function() {
$scope.walletSelection = false;
$scope.setWallets();
});
}
};
$scope.setWallets = function() {
if (!$rootScope.iden) return;
$scope.wallets = [];
var wids = _.pluck($rootScope.iden.listWallets(), 'id');
_.each(wids, function(wid) {
if (controllerUtils.isFocusedWallet(wid)) return;
var w = $rootScope.iden.getWalletById(wid);
$scope.wallets.push(w);
controllerUtils.updateBalance(w, function() {
$rootScope.$digest();
})
var ret = _.filter($rootScope.iden.listWallets(), function(w) {
return !identityService.isFocused(w.getId());
});
$scope.wallets = ret;
};
});

View file

@ -1,16 +1,14 @@
'use strict';
angular.module('copayApp.controllers').controller('WarningController', function($scope, $rootScope, $location, controllerUtils) {
angular.module('copayApp.controllers').controller('WarningController', function($scope, $rootScope, $location, identityService) {
$scope.checkLock = function() {
if (!$rootScope.tmp || !$rootScope.tmp.getLock()) {
controllerUtils.redirIfLogged();
console.log('[warning.js.7] TODO LOCK'); //TODO
}
};
$scope.signout = function() {
controllerUtils.logout();
identityService.logout();
};
$scope.ignoreLock = function() {
@ -22,7 +20,8 @@ angular.module('copayApp.controllers').controller('WarningController', function(
} else {
w.ignoreLock = 1;
$scope.loading = true;
controllerUtils.startNetwork(w, $scope);
//controllerUtils.startNetwork(w, $scope);
// TODO
}
};
});