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 = [];
|
|
|
|
|
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;
|
|
|
|
|
self.step1 = true;
|
|
|
|
|
self.step2 = false;
|
|
|
|
|
self.step3 = false;
|
|
|
|
|
self.step4 = false;
|
|
|
|
|
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-18 18:37:42 -03:00
|
|
|
if (fc.isPrivKeyEncrypted()) {
|
2015-11-18 12:29:56 -03:00
|
|
|
self.credentialsEncrypted = true;
|
2015-11-18 18:37:42 -03:00
|
|
|
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-23 17:53:42 -03:00
|
|
|
self.goToStep = function() {
|
|
|
|
|
if (self.step2) {
|
|
|
|
|
self.goToStep1();
|
|
|
|
|
} else if (self.step3) {
|
|
|
|
|
self.goToStep2();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.goToStep1 = function() {
|
|
|
|
|
init();
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-20 14:38:29 -03:00
|
|
|
self.goToStep2 = function() {
|
|
|
|
|
self.step1 = false;
|
|
|
|
|
self.step2 = true;
|
|
|
|
|
self.step3 = false;
|
|
|
|
|
self.step4 = false;
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.goToStep3 = function() {
|
|
|
|
|
if (self.mnemonicHasPassphrase) {
|
|
|
|
|
self.step1 = false;
|
|
|
|
|
self.step2 = false;
|
|
|
|
|
self.step3 = true;
|
|
|
|
|
self.step4 = false;
|
|
|
|
|
} else {
|
2015-11-23 12:58:04 -03:00
|
|
|
self.goToStep4();
|
2015-11-20 14:38:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.goToStep4 = function() {
|
2015-11-23 12:58:04 -03:00
|
|
|
self.confirm();
|
|
|
|
|
|
2015-11-20 14:38:29 -03:00
|
|
|
self.step1 = false;
|
|
|
|
|
self.step2 = false;
|
|
|
|
|
self.step3 = false;
|
|
|
|
|
self.step4 = true;
|
2015-11-23 12:58:04 -03:00
|
|
|
|
2015-11-20 14:38:29 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 1);
|
|
|
|
|
}
|
2015-11-18 12:29:56 -03:00
|
|
|
|
2015-09-25 17:24:15 -03:00
|
|
|
function setWords(words) {
|
|
|
|
|
if (words) {
|
|
|
|
|
self.mnemonicWords = words.split(/[\u3000\s]+/);
|
2015-11-20 16:04:11 -03:00
|
|
|
self.shuffledMnemonicWords = lodash.shuffle(self.mnemonicWords);
|
2015-09-25 17:24:15 -03:00
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
2015-11-18 18:37:42 -03:00
|
|
|
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-11-18 18:37:42 -03:00
|
|
|
|
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-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() {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.confirm = function() {
|
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
|
|
|
});
|