Wallet/src/js/controllers/backup.js

126 lines
3.7 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('wordsController',
2015-11-17 15:42:06 -03:00
function($rootScope, $scope, $timeout, $log, $compile, lodash, profileService, go, gettext, confirmDialog, notification, bwsError) {
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-11-17 15:42:06 -03:00
var customSortWords = [];
2015-10-28 15:57:08 -03:00
var fc = profileService.focusedClient;
2015-11-18 12:29:56 -03:00
self.show = false;
self.sorted = false;
2015-10-28 15:57:08 -03:00
2015-11-18 12:29:56 -03:00
if (fc.isPrivKeyEncrypted())
self.credentialsEncrypted = true;
else
2015-10-29 14:45:26 -03:00
setWords(fc.getMnemonic());
2015-11-18 12:29:56 -03:00
if (fc.credentials && !fc.credentials.mnemonicEncrypted && !fc.credentials.mnemonic)
2015-10-28 15:57:08 -03:00
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-11-11 17:52:29 -03:00
if (!self.credentialsEncrypted) {
if (!self.show)
$rootScope.$emit('Local/BackupDone');
2015-10-28 16:29:48 -03:00
self.show = !self.show;
2015-11-11 17:52:29 -03:00
}
2015-10-28 16:29:48 -03:00
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-29 14:45:26 -03:00
$rootScope.$emit('Local/BackupDone');
2015-10-28 15:57:08 -03:00
});
}
}
}
2015-11-17 15:42:06 -03:00
2015-11-18 11:58:43 -03:00
self.enableButton = function(word) {
document.getElementById(word).disabled = false;
lodash.remove(customSortWords, function(v) {
return v == word;
});
}
2015-11-17 15:42:06 -03:00
self.disableButton = function(word) {
document.getElementById(word).disabled = true;
customSortWords.push(word);
self.addButton(word);
}
self.addButton = function(word) {
2015-11-18 11:58:43 -03:00
var asd = 'apospodk';
2015-11-17 15:42:06 -03:00
var btnhtml = '<button class="button radius tiny" style="white-space:nowrap" ' +
2015-11-18 11:58:43 -03:00
'data-ng-click="wordsC.removeButton($event)" id="_' + word + '" > ' + word + ' </button>';
2015-11-17 15:42:06 -03:00
var temp = $compile(btnhtml)($scope);
angular.element(document.getElementById('addWord')).append(temp);
2015-11-18 11:58:43 -03:00
self.shouldContinue(customSortWords);
2015-11-17 15:42:06 -03:00
}
2015-11-18 11:58:43 -03:00
self.removeButton = function(event) {
var id = (event.target.id);
var element = document.getElementById(id);
element.remove();
self.enableButton(id.substring(1));
self.shouldContinue(customSortWords);
2015-11-17 15:42:06 -03:00
}
self.shouldContinue = function(customSortWords) {
if (lodash.isEqual(self.mnemonicWords, customSortWords))
self.sorted = true;
2015-11-18 11:58:43 -03:00
else
self.sorted = false;
2015-11-17 15:42:06 -03:00
}
2015-11-11 17:52:29 -03:00
});