2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
angular.module('copayApp.controllers').controller('preferencesController',
|
2016-02-11 14:06:30 -05:00
|
|
|
function($scope, $rootScope, $timeout, $log, configService, profileService, txService) {
|
2015-11-18 15:14:09 -03:00
|
|
|
|
|
|
|
|
var fc = profileService.focusedClient;
|
|
|
|
|
$scope.deleted = false;
|
|
|
|
|
if (fc.credentials && !fc.credentials.mnemonicEncrypted && !fc.credentials.mnemonic) {
|
|
|
|
|
$scope.deleted = true;
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-18 09:33:48 -03:00
|
|
|
var walletId = fc.credentials.walletId;
|
|
|
|
|
config.touchIdFor = config.touchIdFor || {};
|
|
|
|
|
$scope.touchid = config.touchIdFor[walletId];
|
|
|
|
|
|
|
|
|
|
if (window.touchidAvailable)
|
2015-10-07 16:17:19 -03:00
|
|
|
this.touchidAvailable = true;
|
|
|
|
|
};
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2015-11-09 15:20:15 -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 {
|
2015-11-18 15:14:09 -03:00
|
|
|
if (!val && fc.hasPrivKeyEncrypted()) {
|
2016-04-13 14:08:03 -03:00
|
|
|
profileService.unlockFC({}, function(err) {
|
2015-03-06 12:00:10 -03:00
|
|
|
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;
|
|
|
|
|
|
2016-02-11 14:06:30 -05:00
|
|
|
txService.setTouchId(function(err) {
|
2015-11-18 15:14:09 -03:00
|
|
|
if (err) {
|
2015-10-07 16:17:19 -03:00
|
|
|
$log.debug(err);
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.touchidError = true;
|
|
|
|
|
$scope.touchid = oldVal;
|
|
|
|
|
}, 100);
|
2015-11-18 15:14:09 -03:00
|
|
|
} else {
|
2015-10-07 16:17:19 -03:00
|
|
|
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() {
|
2015-11-09 15:20:15 -03:00
|
|
|
unwatchEncrypt();
|
2015-10-07 16:17:19 -03:00
|
|
|
unwatchRequestTouchid();
|
2015-03-06 12:00:10 -03:00
|
|
|
});
|
|
|
|
|
});
|