Wallet/src/js/controllers/backup.js

194 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-08-25 16:31:47 -03:00
function($rootScope, $scope, $timeout, $log, $state, $stateParams, $ionicPopup, uxLanguage, lodash, fingerprintService, platformInfo, configService, profileService, gettext, bwcService, walletService, ongoingProcess) {
2016-08-19 13:09:27 -03:00
var wallet = profileService.getWallet($stateParams.walletId);
2016-08-29 11:58:23 -03:00
var xPriv6;
2016-08-19 13:09:27 -03:00
$scope.walletName = wallet.credentials.walletName;
2016-08-19 14:38:28 -03:00
$scope.n = wallet.n;
2016-08-29 16:13:18 -03:00
$scope.credentialsEncrypted = wallet.isPrivKeyEncrypted();
2015-11-23 17:53:42 -03:00
2016-08-25 16:31:47 -03:00
var isDeletedSeed = function() {
if (lodash.isEmpty(wallet.credentials.mnemonic) && lodash.isEmpty(wallet.credentials.mnemonicEncrypted))
return true;
return false;
};
2016-08-19 13:07:18 -03:00
$scope.init = function() {
2016-06-16 14:43:10 -03:00
$scope.deleted = isDeletedSeed();
if ($scope.deleted) return;
2016-08-29 16:13:18 -03:00
walletService.getKeys(wallet, function(err, keys) {
2016-06-16 14:43:10 -03:00
if (err) {
2016-08-19 14:01:50 -03:00
$state.go('preferences');
2016-06-16 14:43:10 -03:00
return;
}
2016-08-29 11:58:23 -03:00
$scope.credentialsEncrypted = false;
2016-08-29 16:13:18 -03:00
$scope.initFlow(keys);
});
};
2016-08-25 16:31:47 -03:00
var shuffledWords = function(words) {
2016-06-16 14:43:10 -03:00
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
};
});
};
2016-08-29 16:13:18 -03:00
$scope.initFlow = function(keys) {
var words = keys.mnemonic;
xPriv6 = keys.xPrivKey.substr(wallet.credentials.xPrivKey.length - 6);
2016-08-29 11:58:23 -03:00
2016-06-16 14:43:10 -03:00
$scope.mnemonicWords = words.split(/[\u3000\s]+/);
$scope.shuffledMnemonicWords = shuffledWords($scope.mnemonicWords);
2016-08-19 13:09:27 -03:00
$scope.mnemonicHasPassphrase = wallet.mnemonicHasPassphrase();
2016-06-16 14:43:10 -03:00
$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-08-29 11:58:23 -03:00
words = lodash.repeat('x', 300);
2016-06-16 14:43:10 -03:00
$timeout(function() {
$scope.$apply();
}, 10);
};
2016-08-25 16:31:47 -03:00
$scope.goBack = function() {
if ($scope.step == 1) {
if ($stateParams.fromOnboarding) $state.go('onboarding.backupRequest');
else $state.go('wallet.preferences');
}
else {
$scope.goToStep($scope.step - 1);
2015-11-23 17:53:42 -03:00
}
2016-05-24 13:19:13 -03:00
};
2015-11-23 17:53:42 -03:00
2016-08-25 16:31:47 -03:00
var backupError = function(err) {
ongoingProcess.set('validatingWords', false);
$log.debug('Failed to verify backup: ', err);
$scope.backupError = true;
2015-11-20 14:38:29 -03:00
2016-08-25 16:31:47 -03:00
$timeout(function() {
$scope.$apply();
}, 1);
2016-05-24 13:19:13 -03:00
};
2015-11-20 14:38:29 -03:00
2016-08-25 16:31:47 -03:00
var openPopup = function() {
var confirmBackupPopup = $ionicPopup.show({
templateUrl: "views/includes/confirmBackupPopup.html",
scope: $scope,
});
$scope.closePopup = function(val) {
if (val) {
confirmBackupPopup.close();
if ($stateParams.fromOnboarding) $state.go('onboarding.disclaimer');
else $state.go('tabs.home')
}
else {
confirmBackupPopup.close();
$scope.goToStep(1);
}
};
}
2015-11-20 14:38:29 -03:00
2016-08-25 16:31:47 -03:00
var confirm = function(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, {
2016-08-19 13:09:27 -03:00
network: wallet.credentials.network,
passphrase: passphrase,
2016-08-19 13:09:27 -03:00
account: wallet.credentials.account
});
} catch (err) {
2016-08-29 11:58:23 -03:00
walletClient.credentials.xPrivKey = lodash.repeat('x', 64);
return cb(err);
}
2016-08-29 11:58:23 -03:00
if (walletClient.credentials.xPrivKey.substr(walletClient.credentials.xPrivKey-6) != xPriv6) {
2016-08-29 16:13:18 -03:00
delete walletClient.credentials;
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-08-25 16:31:47 -03:00
var finalStep = function() {
ongoingProcess.set('validatingWords', true);
confirm(function(err) {
ongoingProcess.set('validatingWords', false);
if (err) {
backupError(err);
}
$timeout(function() {
openPopup();
return;
}, 1);
});
};
$scope.goToStep = function(n) {
if (n == 1)
$scope.initFlow();
if (n == 2)
$scope.step = 2;
if (n == 3) {
if (!$scope.mnemonicHasPassphrase)
finalStep();
else
$scope.step = 3;
2016-06-16 14:43:10 -03:00
}
2016-08-25 16:31:47 -03:00
if (n == 4)
finalStep();
};
2016-06-16 14:43:10 -03:00
2016-08-25 16:31:47 -03:00
$scope.addButton = function(index, item) {
var newWord = {
word: item.word,
prevIndex: index
};
$scope.customWords.push(newWord);
$scope.shuffledMnemonicWords[index].selected = true;
$scope.shouldContinue();
2016-06-16 14:43:10 -03:00
};
2016-08-25 16:31:47 -03:00
$scope.removeButton = function(index, item) {
if ($scope.loading) return;
$scope.customWords.splice(index, 1);
$scope.shuffledMnemonicWords[item.prevIndex].selected = false;
$scope.shouldContinue();
};
2015-11-30 17:30:26 -03:00
2016-08-25 16:31:47 -03:00
$scope.shouldContinue = function() {
if ($scope.customWords.length == $scope.shuffledMnemonicWords.length)
$scope.selectComplete = true;
else
$scope.selectComplete = false;
2015-11-30 17:30:26 -03:00
};
2016-08-25 16:31:47 -03:00
2015-11-11 17:52:29 -03:00
});