Wallet/src/js/controllers/backup.js

187 lines
4.8 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('backupController',
2015-11-20 14:42:41 -03:00
function($rootScope, $scope, $timeout, $log, $state, $compile, go, lodash, profileService, gettext, bwcService, bwsError) {
var self = this;
2015-10-28 15:57:08 -03:00
var fc = profileService.focusedClient;
2015-11-20 14:38:29 -03:00
var customWords = [];
var mnemonic = null;
2015-11-23 17:53:42 -03:00
function init() {
2015-11-24 12:44:44 -03:00
$scope.passphrase = '';
2015-11-24 12:12:02 -03:00
resetAllButtons();
2015-11-23 17:53:42 -03:00
customWords = [];
mnemonic = null;
self.xPrivKey = null;
2015-11-26 17:42:04 -03:00
self.step = 1;
2015-11-23 17:53:42 -03:00
self.deleted = false;
self.credentialsEncrypted = false;
self.selectComplete = false;
self.backupError = false;
}
init();
2015-11-19 11:36:57 -03:00
if (fc.credentials && !fc.credentials.mnemonicEncrypted && !fc.credentials.mnemonic)
self.deleted = true;
2015-10-28 15:57:08 -03:00
if (fc.isPrivKeyEncrypted()) {
2015-11-18 12:29:56 -03:00
self.credentialsEncrypted = true;
passwordRequest();
} else
2015-11-20 14:38:29 -03:00
setWords(getMnemonic());
function getMnemonic() {
mnemonic = fc.getMnemonic();
self.xPrivKey = fc.credentials.xPrivKey;
profileService.lockFC();
return mnemonic;
}
2015-11-26 17:42:04 -03:00
self.goToStep = function(n) {
self.step = n;
if (self.step == 1)
init();
if (self.step == 3 && !self.mnemonicHasPassphrase)
self.step++;
if (self.step == 4) {
confirm();
2015-11-23 17:53:42 -03:00
}
}
function setWords(words) {
if (words) {
self.mnemonicWords = words.split(/[\u3000\s]+/);
2015-11-26 16:00:28 -03:00
self.shuffledMnemonicWords = lodash.sortBy(self.mnemonicWords);
self.mnemonicHasPassphrase = fc.mnemonicHasPassphrase();
self.useIdeograms = words.indexOf("\u3000") >= 0;
}
};
2015-11-20 14:38:29 -03:00
self.toggle = function() {
self.error = "";
if (self.credentialsEncrypted)
passwordRequest();
$timeout(function() {
$scope.$apply();
}, 1);
};
function passwordRequest() {
2015-10-28 15:57:08 -03:00
try {
2015-11-20 14:38:29 -03:00
setWords(getMnemonic());
2015-10-28 15:57:08 -03:00
} catch (e) {
if (e.message && e.message.match(/encrypted/) && fc.isPrivKeyEncrypted()) {
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 15:57:08 -03:00
self.credentialsEncrypted = false;
2015-11-20 14:38:29 -03:00
setWords(getMnemonic());
$timeout(function() {
$scope.$apply();
}, 1);
2015-10-28 15:57:08 -03:00
});
}
}
}
2015-11-20 14:38:29 -03:00
2015-11-24 12:12:02 -03:00
function resetAllButtons() {
var node = document.getElementById('addWord');
node.innerHTML = '';
lodash.each(self.mnemonicWords, function(d) {
document.getElementById(d).disabled = false;
});
}
2015-11-20 14:38:29 -03:00
self.enableButton = function(word) {
document.getElementById(word).disabled = false;
lodash.remove(customWords, function(v) {
return v == word;
});
}
self.disableButton = function(word) {
document.getElementById(word).disabled = true;
customWords.push(word);
self.addButton(word);
}
self.addButton = function(word) {
2015-11-20 17:30:38 -03:00
var btnhtml = '<button class="button radius tiny words"' +
2015-11-20 14:38:29 -03:00
'data-ng-click="wordsC.removeButton($event)" id="_' + word + '" > ' + word + ' </button>';
var temp = $compile(btnhtml)($scope);
angular.element(document.getElementById('addWord')).append(temp);
self.shouldContinue();
}
self.removeButton = function(event) {
var id = (event.target.id);
var element = document.getElementById(id);
element.remove();
self.enableButton(id.substring(1));
self.shouldContinue();
}
self.shouldContinue = function() {
if (customWords.length == 12)
self.selectComplete = true;
else
self.selectComplete = false;
}
2015-11-26 17:42:04 -03:00
function confirm() {
2015-11-23 12:58:04 -03:00
self.backupError = false;
2015-11-20 14:38:29 -03:00
var walletClient = bwcService.getClient();
try {
var formatedCustomWords = '';
lodash.each(customWords, function(d) {
return formatedCustomWords += ' ' + d;
});
var passphrase = $scope.passphrase || '';
walletClient.seedFromMnemonic(formatedCustomWords.trim(), {
network: fc.credentials.network,
passphrase: passphrase,
account: fc.credentials.account
})
if (walletClient.credentials.xPrivKey != self.xPrivKey)
throw 'Private key mismatch';
} catch (err) {
$log.debug('Failed to verify backup: ', err);
2015-11-23 12:58:04 -03:00
self.backupError = true;
2015-11-20 14:38:29 -03:00
$timeout(function() {
$scope.$apply();
}, 1);
2015-11-23 12:58:04 -03:00
2015-11-20 14:38:29 -03:00
return;
}
var words = lodash.map(mnemonic.split(' '), function(d) {
return d;
});
if (lodash.isEqual(words, customWords)) {
$rootScope.$emit('Local/BackupDone');
}
}
2015-11-11 17:52:29 -03:00
});