2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
angular.module('copayApp.controllers').controller('preferencesController',
|
2016-08-29 16:48:15 -03:00
|
|
|
function($scope, $rootScope, $timeout, $log, $stateParams, $ionicHistory, $ionicNavBarDelegate, gettextCatalog, configService, profileService, fingerprintService, walletService) {
|
|
|
|
|
$ionicNavBarDelegate.title(gettextCatalog.getString('Preferences'));
|
2016-08-17 15:53:17 -03:00
|
|
|
var wallet = profileService.getWallet($stateParams.walletId);
|
|
|
|
|
var walletId = wallet.credentials.walletId;
|
|
|
|
|
$scope.wallet = wallet;
|
2015-11-18 15:14:09 -03:00
|
|
|
|
2016-06-13 16:13:35 -03:00
|
|
|
$scope.init = function() {
|
2016-06-15 10:12:36 -03:00
|
|
|
$scope.externalSource = null;
|
|
|
|
|
|
2016-08-29 16:48:15 -03:00
|
|
|
if (!wallet)
|
|
|
|
|
return $ionicHistory.goBack();
|
2016-08-17 15:53:17 -03:00
|
|
|
|
2016-08-29 11:58:23 -03:00
|
|
|
var config = configService.getSync();
|
|
|
|
|
config.aliasFor = config.aliasFor || {};
|
|
|
|
|
$scope.alias = config.aliasFor[walletId] || wallet.credentials.walletName;
|
|
|
|
|
$scope.color = config.colorFor[walletId] || '#4A90E2';
|
2016-06-15 10:12:36 -03:00
|
|
|
|
2016-08-29 11:58:23 -03:00
|
|
|
$scope.encryptEnabled = walletService.isEncrypted(wallet);
|
|
|
|
|
if (wallet.isPrivKeyExternal)
|
|
|
|
|
$scope.externalSource = wallet.getPrivKeyExternalSourceName() == 'ledger' ? 'Ledger' : 'Trezor';
|
2015-10-07 16:17:19 -03:00
|
|
|
|
2016-08-29 11:58:23 -03:00
|
|
|
$scope.touchIdAvailable = fingerprintService.isAvailable();
|
|
|
|
|
$scope.touchIdEnabled = config.touchIdFor ? config.touchIdFor[walletId] : null;
|
2016-06-06 18:26:45 -03:00
|
|
|
|
|
|
|
|
$scope.deleted = false;
|
2016-08-17 15:53:17 -03:00
|
|
|
if (wallet.credentials && !wallet.credentials.mnemonicEncrypted && !wallet.credentials.mnemonic) {
|
2016-06-06 18:26:45 -03:00
|
|
|
$scope.deleted = true;
|
|
|
|
|
}
|
2016-05-09 15:56:44 -03:00
|
|
|
};
|
2016-04-18 09:33:48 -03:00
|
|
|
|
2016-05-17 18:59:31 -03:00
|
|
|
$scope.encryptChange = function() {
|
2016-08-17 15:53:17 -03:00
|
|
|
if (!wallet) return;
|
2016-05-17 18:59:31 -03:00
|
|
|
var val = $scope.encryptEnabled;
|
2015-05-13 12:41:05 -03:00
|
|
|
|
2016-08-17 15:53:17 -03:00
|
|
|
if (val && !walletService.isEncrypted(wallet)) {
|
2016-08-29 15:17:41 -03:00
|
|
|
$log.debug('Encrypting private key for', wallet.name);
|
|
|
|
|
walletService.encrypt(wallet, function(err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
$log.warn(err);
|
|
|
|
|
|
|
|
|
|
// ToDo show error?
|
2016-05-17 18:59:31 -03:00
|
|
|
$scope.encryptEnabled = false;
|
2015-03-06 12:00:10 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2016-08-29 15:17:41 -03:00
|
|
|
profileService.updateCredentials(JSON.parse(wallet.export()), function() {
|
|
|
|
|
$log.debug('Wallet encrypted');
|
|
|
|
|
return;
|
2015-03-06 12:00:10 -03:00
|
|
|
});
|
2016-08-29 15:17:41 -03:00
|
|
|
})
|
|
|
|
|
} else if (!val && walletService.isEncrypted(wallet)) {
|
|
|
|
|
walletService.decrypt(wallet, function(err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
$log.warn(err);
|
|
|
|
|
|
|
|
|
|
// ToDo show error?
|
|
|
|
|
$scope.encryptEnabled = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
profileService.updateCredentials(JSON.parse(wallet.export()), function() {
|
|
|
|
|
$log.debug('Wallet decrypted');
|
|
|
|
|
return;
|
2015-03-06 12:00:10 -03:00
|
|
|
});
|
2016-08-29 15:17:41 -03:00
|
|
|
})
|
2015-03-06 12:00:10 -03:00
|
|
|
}
|
2016-05-17 18:59:31 -03:00
|
|
|
};
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2016-08-29 11:58:23 -03:00
|
|
|
$scope.touchIdChange = function() {
|
|
|
|
|
var newStatus = $scope.touchIdEnabled;
|
2016-09-01 16:23:27 -03:00
|
|
|
walletService.setTouchId(wallet, !!newStatus, function(err) {
|
2015-11-18 15:14:09 -03:00
|
|
|
if (err) {
|
2016-08-29 11:58:23 -03:00
|
|
|
$scope.touchIdEnabled = !newStatus;
|
2016-09-01 16:23:27 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 1);
|
2016-05-09 15:56:44 -03:00
|
|
|
return;
|
2015-10-07 16:17:19 -03:00
|
|
|
}
|
2016-08-29 11:58:23 -03:00
|
|
|
$log.debug('Touch Id status changed: ' + newStatus);
|
2015-10-07 16:17:19 -03:00
|
|
|
});
|
2016-05-17 18:59:31 -03:00
|
|
|
};
|
2015-03-06 12:00:10 -03:00
|
|
|
});
|