improve functions and variables

This commit is contained in:
Javier 2015-11-30 17:30:26 -03:00
commit 9e73ca83fd
2 changed files with 68 additions and 57 deletions

View file

@ -118,9 +118,10 @@
<div class="panel words text-left" ng-class="{'enable_text_select': index.network == 'testnet'}"> <div class="panel words text-left" ng-class="{'enable_text_select': index.network == 'testnet'}">
<div id="addWord"></div> <div id="addWord"></div>
</div> </div>
<div class="text-left m20b" ng-class="{'enable_text_select': index.network == 'testnet'}"> <div class="text-left m20b" ng-class="{'enable_text_select': index.network == 'testnet'}" id="buttons">
<span ng-repeat="word in wordsC.shuffledMnemonicWords track by $index"> <span ng-repeat="word in wordsC.shuffledMnemonicWords track by $index">
<button class="button radius tiny words" ng-click="wordsC.disableButton(word)" id="{{word}}">{{word}}</button> <button class="button radius tiny words" ng-if="$index > 9" ng-click="wordsC.disableButton($index, word)" id="{{$index + word}}">{{word}}</button>
<button class="button radius tiny words" ng-if="$index <= 9" ng-click="wordsC.disableButton('0' + $index, word)" id="{{'0' + $index + word}}">{{word}}</button>
</span> </span>
</div> </div>
</div> </div>

View file

@ -6,14 +6,11 @@ angular.module('copayApp.controllers').controller('backupController',
var self = this; var self = this;
var fc = profileService.focusedClient; var fc = profileService.focusedClient;
var customWords = []; var customWords = [];
var mnemonic = null;
function init() { function init() {
$scope.passphrase = ''; $scope.passphrase = '';
resetAllButtons(); resetAllButtons();
customWords = []; customWords = [];
mnemonic = null;
self.xPrivKey = null;
self.step = 1; self.step = 1;
self.deleted = false; self.deleted = false;
self.credentialsEncrypted = false; self.credentialsEncrypted = false;
@ -26,11 +23,13 @@ angular.module('copayApp.controllers').controller('backupController',
if (fc.credentials && !fc.credentials.mnemonicEncrypted && !fc.credentials.mnemonic) if (fc.credentials && !fc.credentials.mnemonicEncrypted && !fc.credentials.mnemonic)
self.deleted = true; self.deleted = true;
if (fc.isPrivKeyEncrypted()) { if (fc.isPrivKeyEncrypted() && !self.deleted) {
self.credentialsEncrypted = true; self.credentialsEncrypted = true;
passwordRequest(); passwordRequest();
} else } else {
setWords(getMnemonic()); if (!self.deleted)
initWords();
}
function getMnemonic() { function getMnemonic() {
mnemonic = fc.getMnemonic(); mnemonic = fc.getMnemonic();
@ -50,13 +49,21 @@ angular.module('copayApp.controllers').controller('backupController',
} }
} }
function setWords(words) { function initWords() {
if (words) { var words = fc.getMnemonic();
self.mnemonicWords = words.split(/[\u3000\s]+/); console.log('words: ', fc.getMnemonic());
self.shuffledMnemonicWords = lodash.sortBy(self.mnemonicWords); console.log('credentials: ', fc.credentials);
self.mnemonicHasPassphrase = fc.mnemonicHasPassphrase(); self.xPrivKey = fc.credentials.xPrivKey;
self.useIdeograms = words.indexOf("\u3000") >= 0; profileService.lockFC();
}
self.mnemonicWords = words.split(/[\u3000\s]+/);
self.shuffledMnemonicWords = lodash.sortBy(self.mnemonicWords);;
self.mnemonicHasPassphrase = fc.mnemonicHasPassphrase();
self.useIdeograms = words.indexOf("\u3000") >= 0;
console.log('self.mnemonicWords: ', self.mnemonicWords);
console.log('self.shuffledMnemonicWords: ', self.shuffledMnemonicWords);
console.log('self.mnemonicHasPassphrase: ', self.mnemonicHasPassphrase);
}; };
self.toggle = function() { self.toggle = function() {
@ -72,7 +79,7 @@ angular.module('copayApp.controllers').controller('backupController',
function passwordRequest() { function passwordRequest() {
try { try {
setWords(getMnemonic()); initWords();
} catch (e) { } catch (e) {
if (e.message && e.message.match(/encrypted/) && fc.isPrivKeyEncrypted()) { if (e.message && e.message.match(/encrypted/) && fc.isPrivKeyEncrypted()) {
@ -88,7 +95,7 @@ angular.module('copayApp.controllers').controller('backupController',
} }
self.credentialsEncrypted = false; self.credentialsEncrypted = false;
setWords(getMnemonic()); initWords();
$timeout(function() { $timeout(function() {
$scope.$apply(); $scope.$apply();
@ -99,10 +106,10 @@ angular.module('copayApp.controllers').controller('backupController',
} }
function resetAllButtons() { function resetAllButtons() {
var node = document.getElementById('addWord'); document.getElementById('addWord').innerHTML = '';
node.innerHTML = ''; var nodes = document.getElementById("buttons").getElementsByTagName('button');
lodash.each(self.mnemonicWords, function(d) { lodash.each(nodes, function(n) {
document.getElementById(d).disabled = false; document.getElementById(n.id).disabled = false;
}); });
} }
@ -113,15 +120,19 @@ angular.module('copayApp.controllers').controller('backupController',
}); });
} }
self.disableButton = function(word) { self.disableButton = function(index, word) {
document.getElementById(word).disabled = true; var element = {
customWords.push(word); index: index,
self.addButton(word); word: word
};
document.getElementById(index + word).disabled = true;
customWords.push(element);
self.addButton(index, word);
} }
self.addButton = function(word) { self.addButton = function(index, word) {
var btnhtml = '<button class="button radius tiny words"' + var btnhtml = '<button class="button radius tiny words" ng-disabled="wordsC.disableButtons"' +
'data-ng-click="wordsC.removeButton($event)" id="_' + word + '" > ' + word + ' </button>'; 'data-ng-click="wordsC.removeButton($event)" id="_' + index + word + '" > ' + word + ' </button>';
var temp = $compile(btnhtml)($scope); var temp = $compile(btnhtml)($scope);
angular.element(document.getElementById('addWord')).append(temp); angular.element(document.getElementById('addWord')).append(temp);
self.shouldContinue(); self.shouldContinue();
@ -129,9 +140,11 @@ angular.module('copayApp.controllers').controller('backupController',
self.removeButton = function(event) { self.removeButton = function(event) {
var id = (event.target.id); var id = (event.target.id);
var element = document.getElementById(id); document.getElementById(id).remove();
element.remove();
self.enableButton(id.substring(1)); self.enableButton(id.substring(1));
lodash.remove(customWords, function(d) {
return d.index == id.substring(1, 3);
});
self.shouldContinue(); self.shouldContinue();
} }
@ -145,43 +158,40 @@ angular.module('copayApp.controllers').controller('backupController',
function confirm() { function confirm() {
self.backupError = false; self.backupError = false;
console.log('Original words: ', self.mnemonicWords);
var walletClient = bwcService.getClient(); var walletClient = bwcService.getClient();
var separator = self.useIdeograms ? '\u3000' : ' ';
var customSentence = lodash.pluck(customWords, 'word').join(separator);
var passphrase = $scope.passphrase || '';
console.log('Custom: ', customSentence);
try { try {
var formatedCustomWords = ''; walletClient.seedFromMnemonic(customSentence, {
lodash.each(customWords, function(d) {
return formatedCustomWords += ' ' + d;
});
var passphrase = $scope.passphrase || '';
walletClient.seedFromMnemonic(formatedCustomWords.trim(), {
network: fc.credentials.network, network: fc.credentials.network,
passphrase: passphrase, passphrase: passphrase,
account: fc.credentials.account account: fc.credentials.account
}) })
if (walletClient.credentials.xPrivKey != self.xPrivKey)
throw 'Private key mismatch';
} catch (err) { } catch (err) {
$log.debug('Failed to verify backup: ', err); return backupError(err);
self.backupError = true;
$timeout(function() {
$scope.$apply();
}, 1);
return;
} }
var words = lodash.map(mnemonic.split(' '), function(d) { if (walletClient.credentials.xPrivKey != self.xPrivKey) {
return d; console.log('wc privKey: ', walletClient.credentials.xPrivKey);
}); console.log('self.xPrivKey: ', self.xPrivKey);
return backupError('Private key mismatch');
if (lodash.isEqual(words, customWords)) {
$rootScope.$emit('Local/BackupDone');
} }
$rootScope.$emit('Local/BackupDone');
} }
function backupError(err) {
$log.debug('Failed to verify backup: ', err);
self.backupError = true;
$timeout(function() {
$scope.$apply();
}, 1);
};
}); });