Wallet/src/js/controllers/preferences.js

131 lines
3.9 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, fingerprintService, walletService) {
2016-06-06 18:26:45 -03:00
var self = this;
var fc;
var config = configService.getSync();
2016-05-17 18:59:31 -03:00
2016-06-06 18:26:45 -03:00
var disableFocusListener = $rootScope.$on('Local/NewFocusedWalletReady', function() {
self.init();
});
$scope.$on('$destroy', function() {
disableFocusListener();
});
2015-10-07 16:17:19 -03:00
this.init = function() {
2016-06-06 18:26:45 -03:00
fc = profileService.focusedClient;
2015-10-07 16:17:19 -03:00
if (fc) {
2016-05-17 18:59:31 -03:00
$scope.encryptEnabled = walletService.isEncrypted(fc);
2015-10-07 16:17:19 -03:00
this.externalSource = fc.getPrivKeyExternalSourceName() == 'ledger' ? "Ledger" : null;
// TODO externalAccount
//this.externalIndex = fc.getExternalIndex();
}
this.touchidAvailable = fingerprintService.isAvailable();
2016-05-17 18:59:31 -03:00
$scope.touchidEnabled = config.touchIdFor ? config.touchIdFor[fc.credentials.walletId] : null;
2016-06-06 18:26:45 -03:00
$scope.deleted = false;
if (fc.credentials && !fc.credentials.mnemonicEncrypted && !fc.credentials.mnemonic) {
$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-06-06 18:26:45 -03:00
return cb(walletService.unlock(fc, 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-06-06 18:26:45 -03:00
var self = this;
2015-05-13 12:41:05 -03:00
if (!fc) 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) {
$log.debug('Encrypting private key for', fc.credentials.walletName);
fc.setPrivateKeyEncryption(password);
fc.lock();
profileService.updateCredentials(fc.export(), function() {
$log.debug('Wallet encrypted');
return cb();
});
};
var disablePrivateKeyEncryption = function(cb) {
$log.debug('Disabling private key encryption for', fc.credentials.walletName);
try {
fc.disablePrivateKeyEncryption();
} catch (e) {
return cb(e);
}
profileService.updateCredentials(fc.export(), function() {
$log.debug('Wallet encryption disabled');
return cb();
});
};
if (val && !walletService.isEncrypted(fc)) {
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 {
if (!val && walletService.isEncrypted(fc)) {
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-05-17 18:59:31 -03:00
$scope.touchidChange = function() {
var walletId = fc.credentials.walletId;
2015-10-07 16:17:19 -03:00
var opts = {
touchIdFor: {}
};
2016-05-17 18:59:31 -03:00
opts.touchIdFor[walletId] = $scope.touchidEnabled;
2015-10-07 16:17:19 -03:00
fingerprintService.check(fc, function(err) {
if (err) {
2015-10-07 16:17:19 -03:00
$log.debug(err);
$timeout(function() {
$scope.touchidError = true;
2016-06-09 18:46:50 -03:00
$scope.touchidEnabled = true;
2015-10-07 16:17:19 -03:00
}, 100);
return;
2015-10-07 16:17:19 -03:00
}
configService.set(opts, function(err) {
if (err) {
$log.debug(err);
$scope.touchidError = true;
2016-05-17 18:59:31 -03:00
$scope.touchidEnabled = false;
}
});
2015-10-07 16:17:19 -03:00
});
2016-05-17 18:59:31 -03:00
};
2015-03-06 12:00:10 -03:00
});