Wallet/js/controllers/open.js

67 lines
1.9 KiB
JavaScript
Raw Normal View History

'use strict';
2014-08-14 18:46:42 -04:00
angular.module('copayApp.controllers').controller('OpenController', function($scope, $rootScope, $location, walletFactory, controllerUtils, Passphrase, notification) {
controllerUtils.redirIfLogged();
2014-08-04 11:27:46 -03:00
if ($rootScope.pendingPayment) {
notification.info('Login Required', 'Please open wallet to complete payment');
}
2014-08-14 18:46:42 -04:00
var cmp = function(o1, o2) {
var v1 = o1.show.toLowerCase(),
v2 = o2.show.toLowerCase();
return v1 > v2 ? 1 : (v1 < v2) ? -1 : 0;
};
$rootScope.fromSetup = false;
$scope.loading = false;
2014-09-04 16:23:37 -03:00
$scope.retreiving = true;
2014-09-17 13:03:12 -03:00
2014-09-18 19:20:00 -03:00
walletFactory.getWallets(function(err, wallets) {
2014-09-03 01:25:08 -03:00
2014-09-18 19:20:00 -03:00
if (err || !wallets || !wallets.length) {
2014-09-03 15:43:27 -03:00
$location.path('/');
2014-09-17 13:03:12 -03:00
} else {
2014-09-18 19:20:00 -03:00
$scope.retreiving = false;
2014-09-17 13:03:12 -03:00
$scope.wallets = wallets.sort(cmp);
2014-09-03 15:43:27 -03:00
2014-09-17 13:03:12 -03:00
walletFactory.storage.getLastOpened(function(ret) {
2014-09-18 19:20:00 -03:00
if (ret && _.indexOf(_.pluck($scope.wallets, 'id')) == -1)
ret = null;
2014-09-17 13:03:12 -03:00
$scope.selectedWalletId = ret || ($scope.wallets[0] && $scope.wallets[0].id);
2014-09-18 19:20:00 -03:00
setTimeout(function() {
$rootScope.$digest();
}, 0);
2014-09-17 13:03:12 -03:00
});
}
2014-09-03 01:25:08 -03:00
});
2014-08-14 18:46:42 -04:00
$scope.openPassword = '';
$scope.isMobile = !!window.cordova;
2014-08-14 18:46:42 -04:00
$scope.open = function(form) {
if (form && form.$invalid) {
notification.error('Error', 'Please enter the required fields');
return;
}
2014-08-14 18:46:42 -04:00
$scope.loading = true;
var password = form.openPassword.$modelValue;
2014-08-14 18:46:42 -04:00
Passphrase.getBase64Async(password, function(passphrase) {
var w, errMsg;
2014-09-03 01:25:08 -03:00
walletFactory.open($scope.selectedWalletId, passphrase, function(err, w) {
if (!w) {
$scope.loading = false;
notification.error('Error', err.errMsg || 'Wrong password');
$rootScope.$digest();
2014-09-18 19:20:00 -03:00
} else {
$rootScope.updatingBalance = true;
controllerUtils.startNetwork(w, $scope);
2014-09-03 01:25:08 -03:00
}
});
2014-08-14 18:46:42 -04:00
});
};
2014-08-14 18:46:42 -04:00
});