copay/js/controllers/signin.js

98 lines
3.0 KiB
JavaScript
Raw Normal View History

'use strict';
2014-06-03 13:42:36 -07:00
angular.module('copayApp.controllers').controller('SigninController',
2014-06-25 13:14:12 -07:00
function($scope, $rootScope, $location, walletFactory, controllerUtils, Passphrase) {
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-16 12:46:17 -07:00
$rootScope.$flashMessage = {
message: 'Please, enter required fields',
type: 'error'
};
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-16 12:46:17 -07:00
$rootScope.$flashMessage = {
message: errMsg || 'Wrong password',
type: 'error'
};
$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-16 12:46:17 -07:00
$rootScope.$flashMessage = {
message: 'Please, enter required fields',
type: 'error'
};
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')
$rootScope.$flashMessage = {
message: 'Can\'t find peer'
};
else if (err === 'walletFull')
2014-06-16 12:46:17 -07:00
$rootScope.$flashMessage = {
message: 'The wallet is full',
type: 'error'
};
else if (err === 'badNetwork')
2014-06-16 12:46:17 -07:00
$rootScope.$flashMessage = {
message: 'The wallet your are trying to join uses a different Bitcoin Network. Check your settings.',
type: 'error'
};
else if (err === 'badSecret')
$rootScope.$flashMessage = {
message: 'Bad secret secret string',
type: 'error'
};
else
$rootScope.$flashMessage = {
message: 'Unknown error',
type: '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
});
}
});