2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2015-11-18 18:37:42 -03:00
|
|
|
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) {
|
2015-08-08 10:58:30 -03:00
|
|
|
|
2015-09-25 17:24:15 -03:00
|
|
|
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 = [];
|
2016-02-23 09:47:38 -03:00
|
|
|
self.walletName = fc.credentials.walletName;
|
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 = [];
|
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
|
|
|
|
2015-11-30 17:30:26 -03:00
|
|
|
if (fc.isPrivKeyEncrypted() && !self.deleted) {
|
2015-11-18 12:29:56 -03:00
|
|
|
self.credentialsEncrypted = true;
|
2015-11-18 18:37:42 -03:00
|
|
|
passwordRequest();
|
2015-11-30 17:30:26 -03:00
|
|
|
} else {
|
|
|
|
|
if (!self.deleted)
|
|
|
|
|
initWords();
|
|
|
|
|
}
|
2015-11-20 14:38:29 -03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-30 17:30:26 -03:00
|
|
|
function initWords() {
|
|
|
|
|
var words = fc.getMnemonic();
|
|
|
|
|
self.xPrivKey = fc.credentials.xPrivKey;
|
|
|
|
|
profileService.lockFC();
|
|
|
|
|
self.mnemonicWords = words.split(/[\u3000\s]+/);
|
|
|
|
|
self.shuffledMnemonicWords = lodash.sortBy(self.mnemonicWords);;
|
|
|
|
|
self.mnemonicHasPassphrase = fc.mnemonicHasPassphrase();
|
|
|
|
|
self.useIdeograms = words.indexOf("\u3000") >= 0;
|
2015-09-25 17:24:15 -03:00
|
|
|
};
|
|
|
|
|
|
2015-11-20 14:38:29 -03:00
|
|
|
self.toggle = function() {
|
|
|
|
|
self.error = "";
|
|
|
|
|
|
|
|
|
|
if (self.credentialsEncrypted)
|
|
|
|
|
passwordRequest();
|
|
|
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 1);
|
|
|
|
|
};
|
|
|
|
|
|
2015-11-18 18:37:42 -03:00
|
|
|
function passwordRequest() {
|
2015-10-28 15:57:08 -03:00
|
|
|
try {
|
2015-11-30 17:30:26 -03:00
|
|
|
initWords();
|
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
|
|
|
|
2016-04-13 14:08:03 -03:00
|
|
|
profileService.unlockFC({}, function(err) {
|
2015-10-28 15:57:08 -03:00
|
|
|
if (err) {
|
|
|
|
|
self.error = bwsError.msg(err, gettext('Could not decrypt'));
|
|
|
|
|
$log.warn('Error decrypting credentials:', self.error); //TODO
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-11-18 18:37:42 -03:00
|
|
|
|
2015-10-28 15:57:08 -03:00
|
|
|
self.credentialsEncrypted = false;
|
2015-11-30 17:30:26 -03:00
|
|
|
initWords();
|
2015-11-20 14:38:29 -03:00
|
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 1);
|
2015-10-28 15:57:08 -03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-25 17:24:15 -03:00
|
|
|
}
|
2015-11-20 14:38:29 -03:00
|
|
|
|
2015-11-24 12:12:02 -03:00
|
|
|
function resetAllButtons() {
|
2015-11-30 17:30:26 -03:00
|
|
|
document.getElementById('addWord').innerHTML = '';
|
|
|
|
|
var nodes = document.getElementById("buttons").getElementsByTagName('button');
|
|
|
|
|
lodash.each(nodes, function(n) {
|
|
|
|
|
document.getElementById(n.id).disabled = false;
|
2015-11-24 12:12:02 -03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-30 17:30:26 -03:00
|
|
|
self.disableButton = function(index, word) {
|
|
|
|
|
var element = {
|
|
|
|
|
index: index,
|
|
|
|
|
word: word
|
|
|
|
|
};
|
|
|
|
|
document.getElementById(index + word).disabled = true;
|
|
|
|
|
customWords.push(element);
|
|
|
|
|
self.addButton(index, word);
|
2015-11-20 14:38:29 -03:00
|
|
|
}
|
|
|
|
|
|
2015-11-30 17:30:26 -03:00
|
|
|
self.addButton = function(index, word) {
|
|
|
|
|
var btnhtml = '<button class="button radius tiny words" ng-disabled="wordsC.disableButtons"' +
|
|
|
|
|
'data-ng-click="wordsC.removeButton($event)" id="_' + index + word + '" > ' + word + ' </button>';
|
2015-11-20 14:38:29 -03:00
|
|
|
var temp = $compile(btnhtml)($scope);
|
|
|
|
|
angular.element(document.getElementById('addWord')).append(temp);
|
|
|
|
|
self.shouldContinue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.removeButton = function(event) {
|
|
|
|
|
var id = (event.target.id);
|
2015-11-30 17:30:26 -03:00
|
|
|
document.getElementById(id).remove();
|
2015-11-20 14:38:29 -03:00
|
|
|
self.enableButton(id.substring(1));
|
2015-11-30 17:30:26 -03:00
|
|
|
lodash.remove(customWords, function(d) {
|
|
|
|
|
return d.index == id.substring(1, 3);
|
|
|
|
|
});
|
2015-11-20 14:38:29 -03:00
|
|
|
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
|
|
|
|
2015-11-30 17:30:26 -03:00
|
|
|
var walletClient = bwcService.getClient();
|
|
|
|
|
var separator = self.useIdeograms ? '\u3000' : ' ';
|
|
|
|
|
var customSentence = lodash.pluck(customWords, 'word').join(separator);
|
|
|
|
|
var passphrase = $scope.passphrase || '';
|
2015-11-20 14:38:29 -03:00
|
|
|
|
2015-11-30 17:30:26 -03:00
|
|
|
try {
|
|
|
|
|
walletClient.seedFromMnemonic(customSentence, {
|
2015-11-20 14:38:29 -03:00
|
|
|
network: fc.credentials.network,
|
|
|
|
|
passphrase: passphrase,
|
|
|
|
|
account: fc.credentials.account
|
|
|
|
|
})
|
|
|
|
|
} catch (err) {
|
2015-11-30 17:30:26 -03:00
|
|
|
return backupError(err);
|
2015-11-20 14:38:29 -03:00
|
|
|
}
|
|
|
|
|
|
2015-11-30 17:30:26 -03:00
|
|
|
if (walletClient.credentials.xPrivKey != self.xPrivKey) {
|
|
|
|
|
return backupError('Private key mismatch');
|
2015-11-20 14:38:29 -03:00
|
|
|
}
|
2015-11-30 17:30:26 -03:00
|
|
|
|
|
|
|
|
$rootScope.$emit('Local/BackupDone');
|
2015-11-20 14:38:29 -03:00
|
|
|
}
|
2015-11-30 17:30:26 -03:00
|
|
|
|
|
|
|
|
function backupError(err) {
|
|
|
|
|
$log.debug('Failed to verify backup: ', err);
|
|
|
|
|
self.backupError = true;
|
|
|
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 1);
|
|
|
|
|
};
|
2015-11-11 17:52:29 -03:00
|
|
|
});
|