Wallet/src/js/controllers/backup.js

189 lines
5.2 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.controllers').controller('backupController',
2016-07-11 11:46:48 -03:00
function($rootScope, $scope, $timeout, $log, go, lodash, fingerprintService, platformInfo, configService, profileService, gettext, bwcService, walletService, ongoingProcess) {
2015-10-28 15:57:08 -03:00
var fc = profileService.focusedClient;
2016-06-16 13:53:44 -03:00
var prevState;
$scope.customWords = [];
$scope.walletName = fc.credentials.walletName;
2016-06-16 14:43:10 -03:00
$scope.credentialsEncrypted = fc.isPrivKeyEncrypted;
2015-11-23 17:53:42 -03:00
2016-06-16 14:43:10 -03:00
$scope.init = function(state) {
prevState = state || 'walletHome';
$scope.step = 1;
$scope.deleted = isDeletedSeed();
if ($scope.deleted) return;
fingerprintService.check(fc, function(err) {
if (err) {
go.path(prevState);
return;
}
handleEncryptedWallet(fc, function(err) {
if (err) {
2016-06-29 17:04:40 -03:00
$log.warn('Error decrypting credentials:', $scope.error);
go.path(prevState);
2016-06-16 14:43:10 -03:00
return;
}
$scope.credentialsEncrypted = false;
$scope.initFlow();
});
});
};
2016-06-16 14:43:10 -03:00
function shuffledWords(words) {
var sort = lodash.sortBy(words);
2015-11-20 14:38:29 -03:00
2016-06-16 14:43:10 -03:00
return lodash.map(sort, function(w) {
return {
word: w,
selected: false
};
});
};
$scope.initFlow = function() {
var words = fc.getMnemonic();
$scope.xPrivKey = fc.credentials.xPrivKey;
$scope.mnemonicWords = words.split(/[\u3000\s]+/);
$scope.shuffledMnemonicWords = shuffledWords($scope.mnemonicWords);
2016-06-16 14:43:10 -03:00
$scope.mnemonicHasPassphrase = fc.mnemonicHasPassphrase();
$scope.useIdeograms = words.indexOf("\u3000") >= 0;
$scope.passphrase = '';
$scope.customWords = [];
$scope.step = 1;
$scope.selectComplete = false;
$scope.backupError = false;
2016-06-16 13:53:44 -03:00
2016-06-16 14:43:10 -03:00
$timeout(function() {
$scope.$apply();
}, 10);
};
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;
};
2016-06-16 14:43:10 -03:00
$scope.goBack = function() {
go.path(prevState || 'walletHome');
2016-06-02 18:17:31 -03:00
};
$scope.$on('$destroy', function() {
walletService.lock(fc);
});
$scope.goToStep = function(n) {
if (n == 1)
2016-06-16 14:43:10 -03:00
$scope.initFlow();
if (n == 2)
$scope.step = 2;
if (n == 3) {
if (!$scope.mnemonicHasPassphrase)
finalStep();
else
$scope.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() {
$scope.step = 4;
return;
}, 1);
});
};
2016-05-24 13:19:13 -03:00
};
2015-11-23 17:53:42 -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
};
$scope.customWords.push(newWord);
$scope.shuffledMnemonicWords[index].selected = true;
$scope.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) {
if ($scope.loading) return;
$scope.customWords.splice(index, 1);
$scope.shuffledMnemonicWords[item.prevIndex].selected = false;
$scope.shouldContinue();
2016-05-24 13:19:13 -03:00
};
2015-11-20 14:38:29 -03:00
$scope.shouldContinue = function() {
if ($scope.customWords.length == $scope.shuffledMnemonicWords.length)
$scope.selectComplete = true;
2015-11-20 14:38:29 -03:00
else
$scope.selectComplete = false;
2016-05-24 13:19:13 -03:00
};
2015-11-20 14:38:29 -03:00
function confirm(cb) {
$scope.backupError = false;
2015-11-20 14:38:29 -03:00
var customWordList = lodash.pluck($scope.customWords, 'word');
2015-11-20 14:38:29 -03:00
if (!lodash.isEqual($scope.mnemonicWords, customWordList)) {
return cb('Mnemonic string mismatch');
2015-11-20 14:38:29 -03:00
}
$timeout(function() {
if ($scope.mnemonicHasPassphrase) {
var walletClient = bwcService.getClient();
var separator = $scope.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 != $scope.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
2016-06-16 14:43:10 -03:00
function handleEncryptedWallet(client, cb) {
if (!walletService.isEncrypted(client)) {
$scope.credentialsEncrypted = false;
return cb();
}
$rootScope.$emit('Local/NeedsPassword', false, function(err, password) {
if (err) return cb(err);
return cb(walletService.unlock(client, password));
});
};
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);
$scope.backupError = true;
2015-11-30 17:30:26 -03:00
$timeout(function() {
$scope.$apply();
}, 1);
};
2015-11-11 17:52:29 -03:00
});