2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2015-11-18 18:37:42 -03:00
|
|
|
angular.module('copayApp.controllers').controller('backupController',
|
2016-05-24 13:19:13 -03:00
|
|
|
function($rootScope, $scope, $timeout, $log, lodash, profileService, gettext, bwcService, bwsError, walletService) {
|
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;
|
2016-05-30 13:37:15 -03:00
|
|
|
self.customWords = [];
|
2016-02-23 09:47:38 -03:00
|
|
|
self.walletName = fc.credentials.walletName;
|
2015-11-23 17:53:42 -03:00
|
|
|
|
2016-05-09 15:56:44 -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;
|
2015-11-18 18:37:42 -03:00
|
|
|
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
|
|
|
|
2016-05-30 13:37:15 -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();
|
2016-05-30 13:37:15 -03:00
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
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
|
|
|
}
|
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;
|
2016-05-09 15:56:44 -03:00
|
|
|
walletService.lock(fc);
|
2015-11-30 17:30:26 -03:00
|
|
|
self.mnemonicWords = words.split(/[\u3000\s]+/);
|
2016-05-30 13:37:15 -03:00
|
|
|
self.shuffledMnemonicWords = shuffledWords(self.mnemonicWords);
|
2015-11-30 17:30:26 -03:00
|
|
|
self.mnemonicHasPassphrase = fc.mnemonicHasPassphrase();
|
|
|
|
|
self.useIdeograms = words.indexOf("\u3000") >= 0;
|
2015-09-25 17:24:15 -03:00
|
|
|
};
|
|
|
|
|
|
2016-05-30 13:37:15 -03:00
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
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-05-09 15:56:44 -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-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
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
};
|
2016-05-30 13:37:15 -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) {
|
2016-05-30 13:37:15 -03:00
|
|
|
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() {
|
2016-05-30 13:37:15 -03:00
|
|
|
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
|
|
|
|
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' : ' ';
|
2016-05-30 13:37:15 -03:00
|
|
|
var customSentence = lodash.pluck(self.customWords, 'word').join(separator);
|
2015-11-30 17:30:26 -03:00
|
|
|
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');
|
2016-05-24 13:19:13 -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
|
|
|
});
|