copay/js/controllers/signin.js

75 lines
2.6 KiB
JavaScript
Raw Normal View History

'use strict';
2014-06-03 13:42:36 -07:00
angular.module('copayApp.controllers').controller('SigninController',
2014-06-26 14:37:30 -07:00
function($scope, $rootScope, $location, walletFactory, controllerUtils, Passphrase, backupService, notification) {
2014-06-16 12:46:17 -07:00
var cmp = function(o1, o2) {
var v1 = o1.show.toLowerCase(),
v2 = o2.show.toLowerCase();
return v1 > v2 ? 1 : (v1 < v2) ? -1 : 0;
};
$rootScope.videoInfo = {};
$scope.loading = false;
$scope.wallets = walletFactory.getWallets().sort(cmp);
$scope.selectedWalletId = $scope.wallets.length ? $scope.wallets[0].id : null;
2014-05-01 10:21:55 -07:00
$scope.openPassword = '';
$scope.open = function(form) {
2014-05-06 10:46:26 -07:00
if (form && form.$invalid) {
2014-06-26 13:22:41 -07:00
notification.error('Error', 'Please enter the required fields');
return;
2014-05-01 10:21:55 -07:00
}
2014-06-16 12:46:17 -07:00
$scope.loading = true;
var password = form.openPassword.$modelValue;
2014-06-16 12:46:17 -07:00
Passphrase.getBase64Async(password, function(passphrase) {
var w, errMsg;
2014-06-16 12:46:17 -07:00
try {
var w = walletFactory.open($scope.selectedWalletId, {
passphrase: passphrase
});
} catch (e) {
errMsg = e.message;
};
2014-05-07 14:48:56 -07:00
if (!w) {
$scope.loading = false;
2014-06-26 13:22:41 -07:00
notification.error('Error', errMsg || 'Wrong password');
$rootScope.$digest();
2014-05-07 14:48:56 -07:00
return;
}
2014-06-12 07:03:24 -07:00
controllerUtils.startNetwork(w, $scope);
2014-05-07 14:48:56 -07:00
});
};
$scope.join = function(form) {
2014-05-06 10:46:26 -07:00
if (form && form.$invalid) {
2014-06-26 13:22:41 -07:00
notification.error('Error', 'Please enter the required fields');
return;
}
2014-04-20 08:41:28 -07:00
2014-05-07 14:48:56 -07:00
$scope.loading = true;
2014-06-16 12:46:17 -07:00
walletFactory.network.on('badSecret', function() {});
2014-04-20 08:41:28 -07:00
2014-06-16 12:46:17 -07:00
Passphrase.getBase64Async($scope.joinPassword, function(passphrase) {
walletFactory.joinCreateSession($scope.connectionId, $scope.nickname, passphrase, function(err, w) {
2014-05-07 14:48:56 -07:00
$scope.loading = false;
if (err || !w) {
2014-06-16 12:46:17 -07:00
if (err === 'joinError')
2014-06-26 13:22:41 -07:00
notification.error('Can\'t find peer.');
else if (err === 'walletFull')
2014-06-26 13:22:41 -07:00
notification.error('The wallet is full');
else if (err === 'badNetwork')
2014-06-26 13:22:41 -07:00
notification.error('Network Error', 'The wallet your are trying to join uses a different Bitcoin Network. Check your settings.');
2014-06-16 12:46:17 -07:00
else if (err === 'badSecret')
2014-06-26 13:22:41 -07:00
notification.error('Bad secret', 'The secret string you entered is invalid');
2014-06-16 12:46:17 -07:00
else
2014-06-26 13:22:41 -07:00
notification.error('Unknown error');
controllerUtils.onErrorDigest();
2014-05-07 14:48:56 -07:00
} else {
2014-06-16 12:46:17 -07:00
controllerUtils.startNetwork(w, $scope);
2014-05-07 14:48:56 -07:00
}
});
2014-04-16 16:58:57 -07:00
});
}
});