From 3cd14985125631011252a875f9ba97ed5f180af2 Mon Sep 17 00:00:00 2001 From: Javier Date: Fri, 20 Nov 2015 14:38:29 -0300 Subject: [PATCH] one view/controller --- public/views/backup.html | 208 +++++++++++++++++-------- public/views/backupConfirm.html | 39 ----- public/views/backupPassphrase.html | 70 --------- public/views/backupWords.html | 63 -------- src/js/controllers/backup.js | 159 ++++++++++++++++++- src/js/controllers/backupConfirm.js | 22 --- src/js/controllers/backupPassphrase.js | 56 ------- src/js/controllers/backupWords.js | 57 ------- src/js/routes.js | 22 +-- 9 files changed, 306 insertions(+), 390 deletions(-) delete mode 100644 public/views/backupConfirm.html delete mode 100644 public/views/backupPassphrase.html delete mode 100644 public/views/backupWords.html delete mode 100644 src/js/controllers/backupConfirm.js delete mode 100644 src/js/controllers/backupPassphrase.js delete mode 100644 src/js/controllers/backupWords.js diff --git a/public/views/backup.html b/public/views/backup.html index 66fc58114..d45c82790 100644 --- a/public/views/backup.html +++ b/public/views/backup.html @@ -13,16 +13,6 @@
-
-
- - You need the wallet seed to restore this personal wallet. - - - Write it down and keep them somewhere safe. - -
-
@@ -52,77 +42,159 @@
- Wallet seed not available. You can still export it from Advanced > Export. + Wallet seed not available. You can still export it from Advanced > Export.
- - - - -
-
- Your Wallet Seed -
-
- - -
-
-
- {{word}}  -
-
-
- - -
-
- - - - WARNING: This seed was created with a passphrase. To recover this wallet both the mnemonic and passphrase are needed. - +
+
+ + You need the wallet seed to restore this personal wallet. + + + Write it down and keep them somewhere safe.
+ - - +
+
+
+
+ {{word}}  +
+
+
+ +
+
+ + - Once you have copied your wallet seed down, it is recommended to delete it from this device. + WARNING: This seed was created with a passphrase. To recover this wallet both the mnemonic and passphrase are needed. + + +
+
+ +
+ +
+
+ + +
+
+
+
+
+
+
+ +
-
-
--> +
-
- +
+ +
+
+ + +
+
+
+ Passphrase needed +
+
+ +
+
+ +
+ +
+
+ + +
+
+ + Failed to verify backup. Please try again + +
+ +
+
+
+
+
+
+
+
+
+ Verifying... +
+
+ +
+
+ Congratulations +
+
+ +
+ +
diff --git a/public/views/backupConfirm.html b/public/views/backupConfirm.html deleted file mode 100644 index e1f7aa4b6..000000000 --- a/public/views/backupConfirm.html +++ /dev/null @@ -1,39 +0,0 @@ -
-
- -
-

-
- -
-
- Congratulations -
-
- -
- -
- -
-
-
- You can safely install your wallet on another device and use it from multiple devices at the same time. - - Learn more about Copay backups - -
-
-
- -
-
-
diff --git a/public/views/backupPassphrase.html b/public/views/backupPassphrase.html deleted file mode 100644 index 70fb264fe..000000000 --- a/public/views/backupPassphrase.html +++ /dev/null @@ -1,70 +0,0 @@ -
-
- -
-

-
- - {{backupPassphraseC.error|translate}} - -
- -
-
-
-
-
-
-
-
-
- Verifying passphrase... -
-
- -
-
-
- Passphrase needed -
-
- -
-
- -
- - -
- -
-
-
- You can safely install your wallet on another device and use it from multiple devices at the same time. - - Learn more about Copay backups - -
-
-
- -
-
-
diff --git a/public/views/backupWords.html b/public/views/backupWords.html deleted file mode 100644 index d670474b0..000000000 --- a/public/views/backupWords.html +++ /dev/null @@ -1,63 +0,0 @@ -
-
- -
-

- -
- -
-
- Your Wallet Seed -
-
- -
-
-
-
-
-
- - - -
-
-
- -
- - -
- -
-
-
- You can safely install your wallet on another device and use it from multiple devices at the same time. - - Learn more about Copay backups - -
-
-
- -
-
-
diff --git a/src/js/controllers/backup.js b/src/js/controllers/backup.js index e2302f342..200868834 100644 --- a/src/js/controllers/backup.js +++ b/src/js/controllers/backup.js @@ -1,11 +1,21 @@ 'use strict'; angular.module('copayApp.controllers').controller('backupController', - function($rootScope, $scope, $timeout, $log, $compile, lodash, profileService, go, gettext, confirmDialog, notification, bwsError) { + function($rootScope, $scope, $timeout, $log, $state, $compile, go, lodash, profileService, bwcService, bwsError) { var self = this; var fc = profileService.focusedClient; + var customWords = []; + var mnemonic = null; + self.xPrivKey = null; + self.step1 = true; + self.step2 = false; + self.step3 = false; + self.step4 = false; self.deleted = false; + self.credentialsEncrypted = false; + self.selectComplete = false; + self.backupNoOk = false; if (fc.credentials && !fc.credentials.mnemonicEncrypted && !fc.credentials.mnemonic) self.deleted = true; @@ -14,7 +24,52 @@ angular.module('copayApp.controllers').controller('backupController', self.credentialsEncrypted = true; passwordRequest(); } else - setWords(fc.getMnemonic()); + setWords(getMnemonic()); + + function getMnemonic() { + mnemonic = fc.getMnemonic(); + self.xPrivKey = fc.credentials.xPrivKey; + profileService.lockFC(); + return mnemonic; + } + + self.goToStep2 = function() { + self.step1 = false; + self.step2 = true; + self.step3 = false; + self.step4 = false; + $timeout(function() { + $scope.$apply(); + }, 1); + } + + self.goToStep3 = function() { + if (self.mnemonicHasPassphrase) { + self.step1 = false; + self.step2 = false; + self.step3 = true; + self.step4 = false; + } else { + self.step1 = false; + self.step2 = false; + self.step3 = false; + self.step4 = true; + } + + $timeout(function() { + $scope.$apply(); + }, 1); + } + + self.goToStep4 = function() { + self.step1 = false; + self.step2 = false; + self.step3 = false; + self.step4 = true; + $timeout(function() { + $scope.$apply(); + }, 1); + } function setWords(words) { if (words) { @@ -24,9 +79,20 @@ angular.module('copayApp.controllers').controller('backupController', } }; + self.toggle = function() { + self.error = ""; + + if (self.credentialsEncrypted) + passwordRequest(); + + $timeout(function() { + $scope.$apply(); + }, 1); + }; + function passwordRequest() { try { - setWords(fc.getMnemonic()); + setWords(getMnemonic()); } catch (e) { if (e.message && e.message.match(/encrypted/) && fc.isPrivKeyEncrypted()) { @@ -42,9 +108,94 @@ angular.module('copayApp.controllers').controller('backupController', } self.credentialsEncrypted = false; - setWords(fc.getMnemonic()); + setWords(getMnemonic()); + + $timeout(function() { + $scope.$apply(); + }, 1); }); } } } + + self.enableButton = function(word) { + document.getElementById(word).disabled = false; + lodash.remove(customWords, function(v) { + return v == word; + }); + } + + self.disableButton = function(word) { + document.getElementById(word).disabled = true; + customWords.push(word); + self.addButton(word); + } + + self.addButton = function(word) { + var btnhtml = ''; + var temp = $compile(btnhtml)($scope); + angular.element(document.getElementById('addWord')).append(temp); + self.shouldContinue(); + } + + self.removeButton = function(event) { + var id = (event.target.id); + var element = document.getElementById(id); + element.remove(); + self.enableButton(id.substring(1)); + self.shouldContinue(); + } + + self.shouldContinue = function() { + if (customWords.length == 12) + self.selectComplete = true; + else + self.selectComplete = false; + } + + self.confirm = function() { + self.backupNoOk = false; + + var walletClient = bwcService.getClient(); + + try { + var formatedCustomWords = ''; + + lodash.each(customWords, function(d) { + return formatedCustomWords += ' ' + d; + }); + + var passphrase = $scope.passphrase || ''; + + walletClient.seedFromMnemonic(formatedCustomWords.trim(), { + network: fc.credentials.network, + passphrase: passphrase, + account: fc.credentials.account + }) + + if (walletClient.credentials.xPrivKey != self.xPrivKey) + throw 'Private key mismatch'; + + } catch (err) { + $log.debug('Failed to verify backup: ', err); + self.backupNoOk = true; + $timeout(function() { + $scope.$apply(); + }, 1); + return; + } + + console.log('OK'); + + var words = lodash.map(mnemonic.split(' '), function(d) { + return d; + }); + + if (lodash.isEqual(words, customWords)) { + $rootScope.$emit('Local/BackupDone'); + go.walletHome(); + } + } }); diff --git a/src/js/controllers/backupConfirm.js b/src/js/controllers/backupConfirm.js deleted file mode 100644 index 609186249..000000000 --- a/src/js/controllers/backupConfirm.js +++ /dev/null @@ -1,22 +0,0 @@ -'use strict'; - -angular.module('copayApp.controllers').controller('backupConfirmController', - function($rootScope, $scope, $timeout, $log, $compile, lodash, profileService, go, gettext, confirmDialog, notification, bwsError) { - - var self = this; - var fc = profileService.focusedClient; - - setWords(fc.getMnemonic()); - - function setWords(words) { - if (words) { - self.mnemonicWords = words.split(/[\u3000\s]+/); - self.mnemonicHasPassphrase = fc.mnemonicHasPassphrase(); - self.useIdeograms = words.indexOf("\u3000") >= 0; - } - }; - - self.confirm = function() { - $rootScope.$emit('Local/BackupDone'); - } - }); diff --git a/src/js/controllers/backupPassphrase.js b/src/js/controllers/backupPassphrase.js deleted file mode 100644 index da4fda063..000000000 --- a/src/js/controllers/backupPassphrase.js +++ /dev/null @@ -1,56 +0,0 @@ -'use strict'; - -angular.module('copayApp.controllers').controller('backupPassphraseController', - function($rootScope, $scope, $timeout, $log, $compile, bwcService, lodash, profileService, go, gettext, confirmDialog, notification, bwsError) { - - var self = this; - var fc = profileService.focusedClient; - self.passphraseSuccess = false; - self.checkingPassphrase = false; - self.error = ""; - - setWords(fc.getMnemonic()); - var words = fc.getMnemonic(); - - self.changePassphrase = function() { - self.passphraseSuccess = false; - $timeout(function() { - $rootScope.$apply(); - }, 1); - } - - function setWords(words) { - if (words) { - self.mnemonicWords = words.split(/[\u3000\s]+/); - self.mnemonicHasPassphrase = fc.mnemonicHasPassphrase(); - self.useIdeograms = words.indexOf("\u3000") >= 0; - } - }; - - self.confirm = function() { - self.checkingPassphrase = true; - self.error = ""; - - var walletClient = bwcService.getClient(); - - walletClient.importFromMnemonic(words, { - network: 'livenet', - passphrase: $scope.passphrase, - account: 0, - }, function(err) { - self.checkingPassphrase = false; - if (err) { - self.error = err.message; - $timeout(function() { - $rootScope.$apply(); - }, 1); - return; - } - - self.passphraseSuccess = true; - $timeout(function() { - $rootScope.$apply(); - }, 1); - }); - } - }); diff --git a/src/js/controllers/backupWords.js b/src/js/controllers/backupWords.js deleted file mode 100644 index d9bef409e..000000000 --- a/src/js/controllers/backupWords.js +++ /dev/null @@ -1,57 +0,0 @@ -'use strict'; - -angular.module('copayApp.controllers').controller('backupWordsController', - function($rootScope, $scope, $timeout, $log, $compile, lodash, profileService, go, gettext, confirmDialog, notification, bwsError) { - - var self = this; - var fc = profileService.focusedClient; - var customSortWords = []; - self.sorted = false; - - setWords(fc.getMnemonic()); - - function setWords(words) { - if (words) { - self.mnemonicWords = words.split(/[\u3000\s]+/); - self.mnemonicHasPassphrase = fc.mnemonicHasPassphrase(); - self.useIdeograms = words.indexOf("\u3000") >= 0; - } - }; - - self.enableButton = function(word) { - document.getElementById(word).disabled = false; - lodash.remove(customSortWords, function(v) { - return v == word; - }); - } - - self.disableButton = function(word) { - document.getElementById(word).disabled = true; - customSortWords.push(word); - self.addButton(word); - } - - self.addButton = function(word) { - var btnhtml = ''; - var temp = $compile(btnhtml)($scope); - angular.element(document.getElementById('addWord')).append(temp); - self.shouldContinue(customSortWords); - } - - self.removeButton = function(event) { - var id = (event.target.id); - var element = document.getElementById(id); - element.remove(); - self.enableButton(id.substring(1)); - self.shouldContinue(customSortWords); - } - - self.shouldContinue = function(customSortWords) { - if (lodash.isEqual(self.mnemonicWords, customSortWords)) - self.sorted = true; - else - self.sorted = false; - } - }); diff --git a/src/js/routes.js b/src/js/routes.js index eaea05692..7b963db06 100644 --- a/src/js/routes.js +++ b/src/js/routes.js @@ -445,17 +445,6 @@ angular }, } }) - .state('backupConfirm', { - url: '/backupConfirm', - templateUrl: 'views/backupConfirm.html', - walletShouldBeComplete: true, - needProfile: true, - views: { - 'main': { - templateUrl: 'views/backupConfirm.html' - }, - } - }) .state('backupPassphrase', { url: '/backupPassphrase', templateUrl: 'views/backupPassphrase.html', @@ -467,6 +456,17 @@ angular }, } }) + .state('backupConfirm', { + url: '/backupConfirm', + templateUrl: 'views/backupConfirm.html', + walletShouldBeComplete: true, + needProfile: true, + views: { + 'main': { + templateUrl: 'views/backupConfirm.html' + }, + } + }) .state('preferencesGlobal', { url: '/preferencesGlobal', needProfile: true,