Wallet/src/js/controllers/backup.js

85 lines
2.4 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('wordsController',
function($rootScope, $scope, $timeout, profileService, go, gettext, confirmDialog, notification, bwsError, $log) {
2015-09-09 17:53:05 -03:00
var msg = gettext('Are you sure you want to delete the backup words?');
var successMsg = gettext('Backup words deleted');
var self = this;
2015-10-28 15:57:08 -03:00
self.show = false;
var fc = profileService.focusedClient;
if (fc.isPrivKeyEncrypted()) self.credentialsEncrypted = true;
else setWords(fc.getMnemonic());
2015-10-28 15:57:08 -03:00
if (fc.credentials && !fc.credentials.mnemonicEncrypted && !fc.credentials.mnemonic) {
self.deleted = true;
}
2015-09-21 10:18:43 -03:00
2015-10-28 15:57:08 -03:00
self.toggle = function() {
self.error = "";
2015-10-28 16:29:48 -03:00
if (!self.credentialsEncrypted)
self.show = !self.show;
2015-10-28 15:57:08 -03:00
if (self.show)
2015-09-21 10:18:43 -03:00
$rootScope.$emit('Local/BackupDone');
2015-10-28 15:57:08 -03:00
if (self.credentialsEncrypted)
self.passwordRequest();
$timeout(function() {
2015-09-21 10:18:43 -03:00
$scope.$apply();
}, 1);
2015-08-08 10:10:44 -03:00
};
2015-10-28 15:57:08 -03:00
self.delete = function() {
2015-09-18 10:27:36 -03:00
confirmDialog.show(msg, function(ok) {
if (ok) {
fc.clearMnemonic();
profileService.updateCredentialsFC(function() {
2015-10-28 15:57:08 -03:00
self.deleted = true;
notification.success(successMsg);
go.walletHome();
});
}
});
};
$scope.$on('$destroy', function() {
profileService.lockFC();
});
function setWords(words) {
if (words) {
self.mnemonicWords = words.split(/[\u3000\s]+/);
self.mnemonicHasPassphrase = fc.mnemonicHasPassphrase();
self.useIdeograms = words.indexOf("\u3000") >= 0;
}
};
2015-10-28 15:57:08 -03:00
self.passwordRequest = function() {
try {
setWords(fc.getMnemonic());
} catch (e) {
if (e.message && e.message.match(/encrypted/) && fc.isPrivKeyEncrypted()) {
self.credentialsEncrypted = true;
2015-09-03 16:49:46 -03:00
2015-10-28 15:57:08 -03:00
$timeout(function() {
$scope.$apply();
}, 1);
2015-09-03 16:49:46 -03:00
2015-10-28 15:57:08 -03:00
profileService.unlockFC(function(err) {
if (err) {
self.error = bwsError.msg(err, gettext('Could not decrypt'));
$log.warn('Error decrypting credentials:', self.error); //TODO
return;
}
2015-10-28 16:29:48 -03:00
if (!self.show && self.credentialsEncrypted)
self.show = !self.show;
2015-10-28 15:57:08 -03:00
self.credentialsEncrypted = false;
setWords(fc.getMnemonic());
});
}
}
}
2015-10-28 15:57:08 -03:00
});