Wallet/src/js/controllers/backup.js

211 lines
5.5 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('backupController',
2016-06-13 15:25:40 -03:00
function($rootScope, $scope, $timeout, $log, lodash, profileService, gettext, bwcService, bwsError, walletService, ongoingProcess) {
var self = this;
2015-10-28 15:57:08 -03:00
var fc = profileService.focusedClient;
self.customWords = [];
2016-02-23 09:47:38 -03:00
self.walletName = fc.credentials.walletName;
2015-11-23 17:53:42 -03:00
var handleEncryptedWallet = function(client, cb) {
if (!walletService.isEncrypted(client)) return cb();
$rootScope.$emit('Local/NeedsPassword', false, function(err, password) {
if (err) return cb(err);
return cb(walletService.unlock(client, password));
});
};
2016-06-02 18:17:31 -03:00
if (fc.isPrivKeyEncrypted() && !isDeletedSeed()) {
2015-11-18 12:29:56 -03:00
self.credentialsEncrypted = true;
passwordRequest();
2015-11-30 17:30:26 -03:00
} else {
2016-06-02 18:17:31 -03:00
if (!isDeletedSeed())
2015-11-30 17:30:26 -03:00
initWords();
}
2015-11-20 14:38:29 -03:00
init();
function init() {
$scope.passphrase = '';
self.shuffledMnemonicWords = shuffledWords(self.mnemonicWords);
self.customWords = [];
self.step = 1;
2016-06-02 18:17:31 -03:00
self.deleted = isDeletedSeed();
self.credentialsEncrypted = false;
self.selectComplete = false;
self.backupError = false;
};
2016-06-02 18:17:31 -03:00
function isDeletedSeed() {
if (lodash.isEmpty(fc.credentials.mnemonic) && lodash.isEmpty(fc.credentials.mnemonicEncrypted))
return true;
return false;
};
self.backTo = function(state) {
console.log(state);
if (state == 'walletHome')
go.walletHome();
else
go.preferences();
2016-06-02 18:17:31 -03:00
};
2015-11-26 17:42:04 -03:00
self.goToStep = function(n) {
if (n == 1)
2015-11-26 17:42:04 -03:00
init();
if (n == 2)
self.step = 2;
if (n == 3) {
if (!self.mnemonicHasPassphrase)
finalStep();
else
self.step = 3;
2015-11-23 17:53:42 -03:00
}
if (n == 4)
finalStep();
function finalStep() {
2016-06-13 15:25:40 -03:00
ongoingProcess.set('validatingWords', true);
confirm(function(err) {
2016-06-13 15:25:40 -03:00
ongoingProcess.set('validatingWords', false);
if (err) {
backupError(err);
}
$timeout(function() {
self.step = 4;
return;
}, 1);
});
};
2016-05-24 13:19:13 -03:00
};
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;
walletService.lock(fc);
2015-11-30 17:30:26 -03:00
self.mnemonicWords = words.split(/[\u3000\s]+/);
self.shuffledMnemonicWords = shuffledWords(self.mnemonicWords);
2015-11-30 17:30:26 -03:00
self.mnemonicHasPassphrase = fc.mnemonicHasPassphrase();
self.useIdeograms = words.indexOf("\u3000") >= 0;
};
function shuffledWords(words) {
var sort = lodash.sortBy(words);
return lodash.map(sort, function(w) {
2016-05-24 13:19:13 -03:00
return {
word: w,
selected: false
};
});
};
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-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
handleEncryptedWallet(fc, 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-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
});
}
}
2016-05-24 13:19:13 -03:00
};
2015-11-20 14:38:29 -03:00
2016-05-24 13:19:13 -03:00
$scope.addButton = function(index, item) {
var newWord = {
word: item.word,
prevIndex: index
2015-11-30 17:30:26 -03:00
};
self.customWords.push(newWord);
2016-05-24 13:19:13 -03:00
self.shuffledMnemonicWords[index].selected = true;
2015-11-20 14:38:29 -03:00
self.shouldContinue();
2016-05-24 13:19:13 -03:00
};
2015-11-20 14:38:29 -03:00
2016-05-24 13:19:13 -03:00
$scope.removeButton = function(index, item) {
self.customWords.splice(index, 1);
2016-05-24 13:19:13 -03:00
self.shuffledMnemonicWords[item.prevIndex].selected = false;
2015-11-20 14:38:29 -03:00
self.shouldContinue();
2016-05-24 13:19:13 -03:00
};
2015-11-20 14:38:29 -03:00
self.shouldContinue = function() {
if (self.customWords.length == 12)
2015-11-20 14:38:29 -03:00
self.selectComplete = true;
else
self.selectComplete = false;
2016-05-24 13:19:13 -03:00
};
2015-11-20 14:38:29 -03:00
function confirm(cb) {
2015-11-23 12:58:04 -03:00
self.backupError = false;
2015-11-20 14:38:29 -03:00
2016-06-09 14:45:28 -03:00
var customWordList = lodash.pluck(self.customWords, 'word');
2015-11-20 14:38:29 -03:00
2016-06-09 14:45:28 -03:00
if (!lodash.isEqual(self.mnemonicWords, customWordList)) {
return cb('Mnemonic string mismatch');
2015-11-20 14:38:29 -03:00
}
$timeout(function() {
if (self.mnemonicHasPassphrase) {
var walletClient = bwcService.getClient();
var separator = self.useIdeograms ? '\u3000' : ' ';
var customSentence = customWordList.join(separator);
var passphrase = $scope.passphrase || '';
try {
walletClient.seedFromMnemonic(customSentence, {
network: fc.credentials.network,
passphrase: passphrase,
account: fc.credentials.account
});
} catch (err) {
return cb(err);
}
if (walletClient.credentials.xPrivKey != self.xPrivKey) {
return cb('Private key mismatch');
}
2016-06-09 14:45:28 -03:00
}
2015-11-30 17:30:26 -03:00
$rootScope.$emit('Local/BackupDone');
return cb();
}, 1);
2016-05-24 13:19:13 -03:00
};
2015-11-30 17:30:26 -03:00
function backupError(err) {
2016-06-13 15:25:40 -03:00
ongoingProcess.set('validatingWords', false);
2015-11-30 17:30:26 -03:00
$log.debug('Failed to verify backup: ', err);
self.backupError = true;
$timeout(function() {
$scope.$apply();
}, 1);
};
2015-11-11 17:52:29 -03:00
});