Wallet/src/js/controllers/preferences.js

113 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('preferencesController',
2016-08-29 11:58:23 -03:00
function($scope, $rootScope, $timeout, $log, $stateParams, configService, profileService, fingerprintService, walletService, $state) {
2016-08-17 15:53:17 -03:00
var wallet = profileService.getWallet($stateParams.walletId);
var walletId = wallet.credentials.walletId;
$scope.wallet = wallet;
$scope.init = function() {
$scope.externalSource = null;
2016-08-29 11:58:23 -03:00
if (!wallet)
return $state.go('tabs.home');
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-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-04-18 09:33:48 -03:00
2016-06-06 18:26:45 -03:00
var handleEncryptedWallet = function(cb) {
$rootScope.$emit('Local/NeedsPassword', false, function(err, password) {
if (err) return cb(err);
2016-08-17 15:53:17 -03:00
return cb(walletService.unlock(wallet, password));
});
2015-10-07 16:17:19 -03:00
};
2015-03-06 12:00:10 -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-06-06 18:26:45 -03:00
var setPrivateKeyEncryption = function(password, cb) {
2016-08-17 15:53:17 -03:00
$log.debug('Encrypting private key for', wallet.credentials.walletName);
2016-06-06 18:26:45 -03:00
2016-08-17 15:53:17 -03:00
wallet.setPrivateKeyEncryption(password);
wallet.lock();
profileService.updateCredentials(JSON.parse(wallet.export()), function() {
2016-06-06 18:26:45 -03:00
$log.debug('Wallet encrypted');
return cb();
});
};
var disablePrivateKeyEncryption = function(cb) {
2016-08-17 15:53:17 -03:00
$log.debug('Disabling private key encryption for', wallet.credentials.walletName);
2016-06-06 18:26:45 -03:00
try {
2016-08-17 15:53:17 -03:00
wallet.disablePrivateKeyEncryption();
2016-06-06 18:26:45 -03:00
} catch (e) {
return cb(e);
}
2016-08-17 15:53:17 -03:00
profileService.updateCredentials(JSON.parse(wallet.export()), function() {
2016-06-06 18:26:45 -03:00
$log.debug('Wallet encryption disabled');
return cb();
});
};
2016-08-17 15:53:17 -03:00
if (val && !walletService.isEncrypted(wallet)) {
2015-03-06 12:00:10 -03:00
$rootScope.$emit('Local/NeedsPassword', true, function(err, password) {
if (err || !password) {
2016-05-17 18:59:31 -03:00
$scope.encryptEnabled = false;
2015-03-06 12:00:10 -03:00
return;
}
2016-06-06 18:26:45 -03:00
setPrivateKeyEncryption(password, function() {
2015-11-10 20:05:05 -03:00
$rootScope.$emit('Local/NewEncryptionSetting');
2016-05-17 18:59:31 -03:00
$scope.encryptEnabled = true;
2015-03-06 12:00:10 -03:00
});
});
} else {
2016-08-17 15:53:17 -03:00
if (!val && walletService.isEncrypted(wallet)) {
2016-06-06 18:26:45 -03:00
handleEncryptedWallet(function(err) {
2015-03-06 12:00:10 -03:00
if (err) {
2016-05-17 18:59:31 -03:00
$scope.encryptEnabled = true;
2015-03-06 12:00:10 -03:00
return;
}
2016-06-06 18:26:45 -03:00
disablePrivateKeyEncryption(function(err) {
2015-11-10 20:05:05 -03:00
$rootScope.$emit('Local/NewEncryptionSetting');
2015-03-06 12:00:10 -03:00
if (err) {
2016-05-17 18:59:31 -03:00
$scope.encryptEnabled = true;
2015-03-06 12:00:10 -03:00
$log.error(err);
return;
}
2016-05-17 18:59:31 -03:00
$scope.encryptEnabled = false;
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;
walletService.setTouchId(wallet, newStatus, function(err) {
if (err) {
2016-08-29 11:58:23 -03:00
$log.warn(err);
$scope.touchIdEnabled = !newStatus;
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
});