Wallet/src/js/controllers/preferences.js

96 lines
2.8 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('preferencesController',
function($scope, $rootScope, $timeout, $log, configService, profileService) {
2015-10-07 16:17:19 -03:00
this.init = function() {
var config = configService.getSync();
var fc = profileService.focusedClient;
if (fc) {
$scope.encrypt = fc.hasPrivKeyEncrypted();
this.externalSource = fc.getPrivKeyExternalSourceName() == 'ledger' ? "Ledger" : null;
// TODO externalAccount
//this.externalIndex = fc.getExternalIndex();
}
if (window.touchidAvailable) {
var walletId = fc.credentials.walletId;
this.touchidAvailable = true;
config.touchIdFor = config.touchIdFor || {};
$scope.touchid = config.touchIdFor[walletId];
}
};
2015-03-06 12:00:10 -03:00
var unwatchEncrypt = $scope.$watch('encrypt', function(val) {
2015-03-06 12:00:10 -03:00
var fc = profileService.focusedClient;
2015-05-13 12:41:05 -03:00
if (!fc) return;
2015-03-06 12:00:10 -03:00
if (val && !fc.hasPrivKeyEncrypted()) {
$rootScope.$emit('Local/NeedsPassword', true, function(err, password) {
if (err || !password) {
$scope.encrypt = false;
return;
}
profileService.setPrivateKeyEncryptionFC(password, function() {
2015-11-10 20:05:05 -03:00
$rootScope.$emit('Local/NewEncryptionSetting');
2015-03-06 12:00:10 -03:00
$scope.encrypt = true;
});
});
} else {
if (!val && fc.hasPrivKeyEncrypted()) {
profileService.unlockFC(function(err){
if (err) {
$scope.encrypt = true;
return;
}
profileService.disablePrivateKeyEncryptionFC(function(err) {
2015-11-10 20:05:05 -03:00
$rootScope.$emit('Local/NewEncryptionSetting');
2015-03-06 12:00:10 -03:00
if (err) {
$scope.encrypt = true;
$log.error(err);
return;
}
$scope.encrypt = false;
});
});
}
}
});
2015-10-07 16:17:19 -03:00
var unwatchRequestTouchid = $scope.$watch('touchid', function(newVal, oldVal) {
if (newVal == oldVal || $scope.touchidError) {
$scope.touchidError = false;
return;
}
var walletId = profileService.focusedClient.credentials.walletId;
var opts = {
touchIdFor: {}
};
opts.touchIdFor[walletId] = newVal;
$rootScope.$emit('Local/RequestTouchid', function(err) {
if (err) {
$log.debug(err);
$timeout(function() {
$scope.touchidError = true;
$scope.touchid = oldVal;
}, 100);
}
else {
configService.set(opts, function(err) {
if (err) {
$log.debug(err);
$scope.touchidError = true;
$scope.touchid = oldVal;
}
});
}
});
});
2015-03-06 12:00:10 -03:00
$scope.$on('$destroy', function() {
unwatchEncrypt();
2015-10-07 16:17:19 -03:00
unwatchRequestTouchid();
2015-03-06 12:00:10 -03:00
});
});