2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
angular.module('copayApp.controllers').controller('preferencesController',
|
2016-08-17 13:07:48 -03:00
|
|
|
function($scope, $rootScope, $timeout, $log, $stateParams, configService, profileService, fingerprintService, walletService) {
|
2015-11-18 15:14:09 -03:00
|
|
|
|
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-17 15:53:17 -03:00
|
|
|
if (wallet) {
|
|
|
|
|
var config = configService.getSync();
|
2016-08-17 13:07:48 -03:00
|
|
|
config.aliasFor = config.aliasFor || {};
|
2016-08-17 15:53:17 -03:00
|
|
|
$scope.alias = config.aliasFor[walletId] || wallet.credentials.walletName;
|
|
|
|
|
$scope.color = config.colorFor[walletId] || '#4A90E2';
|
|
|
|
|
|
|
|
|
|
$scope.encryptEnabled = walletService.isEncrypted(wallet);
|
|
|
|
|
if (wallet.isPrivKeyExternal)
|
|
|
|
|
$scope.externalSource = wallet.getPrivKeyExternalSourceName() == 'ledger' ? 'Ledger' : 'Trezor';
|
2016-06-15 10:12:36 -03:00
|
|
|
|
2015-10-07 16:17:19 -03:00
|
|
|
// TODO externalAccount
|
2016-08-17 15:53:17 -03:00
|
|
|
//this.externalIndex = wallet.getExternalIndex();
|
2015-10-07 16:17:19 -03:00
|
|
|
}
|
|
|
|
|
|
2016-06-13 16:13:35 -03:00
|
|
|
$scope.touchidAvailable = fingerprintService.isAvailable();
|
2016-08-17 15:53:17 -03:00
|
|
|
$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-06-06 18:26:45 -03:00
|
|
|
var handleEncryptedWallet = function(cb) {
|
2016-05-09 15:56:44 -03:00
|
|
|
$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));
|
2016-05-09 15:56:44 -03:00
|
|
|
});
|
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-05-17 18:59:31 -03:00
|
|
|
$scope.touchidChange = function() {
|
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
|
|
|
|
2016-08-17 15:53:17 -03:00
|
|
|
fingerprintService.check(wallet, 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;
|
2016-06-09 18:46:50 -03:00
|
|
|
$scope.touchidEnabled = true;
|
2015-10-07 16:17:19 -03:00
|
|
|
}, 100);
|
2016-05-09 15:56:44 -03:00
|
|
|
return;
|
2015-10-07 16:17:19 -03:00
|
|
|
}
|
2016-05-09 15:56:44 -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;
|
2016-05-09 15:56:44 -03:00
|
|
|
}
|
|
|
|
|
});
|
2015-10-07 16:17:19 -03:00
|
|
|
});
|
2016-05-17 18:59:31 -03:00
|
|
|
};
|
2015-03-06 12:00:10 -03:00
|
|
|
});
|