diff --git a/js/controllers/import.js b/js/controllers/import.js index 22b4a50ae..9c802fa0c 100644 --- a/js/controllers/import.js +++ b/js/controllers/import.js @@ -3,10 +3,11 @@ angular.module('copayApp.controllers').controller('ImportController', function($scope, $rootScope, $location, controllerUtils, notification, isMobile, Compatibility) { - $rootScope.title = 'Import a backup'; + $rootScope.title = 'Import wallet'; $scope.importStatus = 'Importing wallet - Reading backup...'; $scope.hideAdv = true; $scope.is_iOS = isMobile.iOS(); + $scope.importOpts = {}; Compatibility.check($scope); @@ -17,44 +18,6 @@ angular.module('copayApp.controllers').controller('ImportController', $scope.$digest(); } - var _importBackup = function(encryptedObj) { - var password = $scope.password; - updateStatus('Importing wallet - Setting things up...'); - var skipFields = []; - if ($scope.skipPublicKeyRing) - skipFields.push('publicKeyRing'); - - if ($scope.skipTxProposals) - skipFields.push('txProposals'); - - $rootScope.iden.importEncryptedWallet(encryptedObj, password, skipFields, opts, function(err, w) { - if (!w) { - $scope.loading = false; - $scope.error = 'Wrong password'; - $rootScope.$digest(); - return; - } - - // if wallet was never used, we're done - if (!w.isReady()) { - controllerUtils.installWalletHandlers($scope, w); - controllerUtils.setFocusedWallet(w); - return; - } - - // if it was used, we need to scan for indices - w.updateIndexes(function(err) { - updateStatus('Importing wallet - We are almost there...'); - if (err) { - $scope.loading = false; - $scope.error = 'Error updating indexes: ' + err; - } - controllerUtils.installWalletHandlers($scope, w); - controllerUtils.setFocusedWallet(w); - }); - }); - }; - $scope.openFileDialog = function() { if (window.cshell) { return cshell.send('backup:import'); @@ -62,72 +25,76 @@ angular.module('copayApp.controllers').controller('ImportController', $scope.choosefile = !$scope.choosefile; }; - $scope.getFile = function() { - // If we use onloadend, we need to check the readyState. - reader.onloadend = function(evt) { - if (evt.target.readyState == FileReader.DONE) { // DONE == 2 - var encryptedObj = evt.target.result; - copay.Compatibility.importEncryptedWallet($rootScope.iden, encryptedObj, $scope.password, {}, - function(err, wallet) { - if (err) { - $scope.loading = false; - $scope.error = 'Could not read wallet. Please check your password'; - } else { - controllerUtils.installWalletHandlers($scope, wallet); - controllerUtils.setFocusedWallet(wallet); - return; - } - } - ); - } - }; - }; + $scope._doImport = function(encryptedObj, password) { + updateStatus('Importing wallet - Procesing backup...'); - $scope.import = function(form) { - $scope.loading = true; - - if (form.$invalid) { - $scope.loading = false; - $scope.error = 'There is an error in the form'; - return; - } - - var backupFile = $scope.file; - var backupText = form.backupText.$modelValue; - var backupOldWallet = form.backupOldWallet.$modelValue; - var password = form.password.$modelValue; - - if (backupOldWallet) { - backupText = backupOldWallet.value; - } - - if (!backupFile && !backupText) { - $scope.loading = false; - $scope.error = 'Please, select your backup file'; - return; - } - - if (backupFile) { - reader.readAsBinaryString(backupFile); - } else { - copay.Compatibility.importEncryptedWallet($rootScope.iden, backupText, $scope.password, {}, - function(err, wallet) { - if (err) { - $scope.error = 'Could not read wallet. Please check your password'; - } else { - copay.Compatibility.deleteOldWallet(backupOldWallet); - controllerUtils.installWalletHandlers($scope, wallet); - controllerUtils.setFocusedWallet(wallet); - return; - } + copay.Compatibility.importEncryptedWallet($rootScope.iden, encryptedObj, + $scope.password, $scope.importOpts, function(err, wallet) { + if (err) { + $scope.loading = false; + $scope.error = 'Could not read wallet. Please check your password'; + } else { + controllerUtils.installWalletHandlers($scope, wallet); + controllerUtils.setFocusedWallet(wallet); } - ); - try { - _importBackup(backupText); - } catch (e) { - copay.Compatibility.importEncryptedWallet(backupText, $scope.password, $scope.skipPublicKeyRing, $scope.skipTxProposals); } + ); + }; + + $scope.getFile = function() { + // If we use onloadend, we need to check the readyState. + reader.onloadend = function(evt) { + if (evt.target.readyState == FileReader.DONE) { // DONE == 2 + var encryptedObj = evt.target.result; + $scope._doImport(encryptedObj, $scope.password); } }; - }); + }; + + $scope.import = function(form) { + $scope.loading = true; + + if (form.$invalid) { + $scope.loading = false; + $scope.error = 'There is an error in the form'; + return; + } + + var backupFile = $scope.file; + var backupText = form.backupText.$modelValue; + var backupOldWallet = form.backupOldWallet.$modelValue; + var password = form.password.$modelValue; + + if (backupOldWallet) { + backupText = backupOldWallet.value; + } + + if (!backupFile && !backupText) { + $scope.loading = false; + $scope.error = 'Please, select your backup file'; + return; + } + + $scope.importOpts = {}; + + var skipFields = []; + + if ($scope.skipPublicKeyRing) + skipFields.push('publicKeyRing'); + + if ($scope.skipTxProposals) + skipFields.push('txProposals'); + + if (skipFields) + $scope.importOpts.skipFields = skipFields; + + + if (backupFile) { + reader.readAsBinaryString(backupFile); + } else { + $scope._doImport(backupText, $scope.password); + copay.Compatibility.deleteOldWallet(backupOldWallet); + } + }; +}); diff --git a/js/controllers/importProfile.js b/js/controllers/importProfile.js index 358259790..a9a841a71 100644 --- a/js/controllers/importProfile.js +++ b/js/controllers/importProfile.js @@ -28,6 +28,7 @@ angular.module('copayApp.controllers').controller('ImportProfileController', passphraseConfig: config.passphraseConfig, }, function(err, iden) { if (err && !iden) { + $scope.loading = false; $scope.error = (err.toString() || '').match('BADSTR') ? 'Bad password or corrupt profile file' : 'Unknown error'; } else { var firstWallet = iden.getLastFocusedWallet(); @@ -54,14 +55,13 @@ angular.module('copayApp.controllers').controller('ImportProfileController', }; $scope.import = function(form) { + $scope.loading = true; if (form.$invalid) { $scope.loading = false; $scope.error = 'Please enter the required fields'; return; } - $rootScope.starting = true; - var backupFile = $scope.file; var backupText = form.backupText.$modelValue; var password = form.password.$modelValue; diff --git a/js/controllers/join.js b/js/controllers/join.js index 0a97826b1..799b28447 100644 --- a/js/controllers/join.js +++ b/js/controllers/join.js @@ -5,7 +5,7 @@ angular.module('copayApp.controllers').controller('JoinController', $rootScope.fromSetup = false; $scope.loading = false; $scope.isMobile = !!window.cordova; - $rootScope.title = 'Join an existent wallet'; + $rootScope.title = 'Join shared wallet'; // QR code Scanner var cameraInput; diff --git a/js/models/Identity.js b/js/models/Identity.js index 1c71beeba..f516ce1e6 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -275,6 +275,9 @@ Identity.prototype.close = function(cb) { }, cb); }; + +// TODO: Add feedback function +// Identity.prototype.importWalletFromObj = function(obj, opts, cb) { var self = this; preconditions.checkArgument(cb); @@ -288,17 +291,21 @@ Identity.prototype.importWalletFromObj = function(obj, opts, cb) { var w = importFunction(obj, readOpts); if (!w) return cb(new Error('Could not decrypt')); + log.debug('Wallet decryped:' + w.getName()); - this._checkVersion(w.version); - this.addWallet(w); - self.bindWallet(w); - self.storeWallet(w, function(err) { - if (err) return cb(err); - - self.store({ - noWallets: true - }, function(err) { - return cb(err, w); + self._checkVersion(w.version); + log.debug('Updating Indexes for wallet:' + w.getName()); + w.updateIndexes(function(err) { + log.debug('Adding wallet to profile:' + w.getName()); + self.addWallet(w); + self.bindWallet(w); + self.storeWallet(w, function(err) { + if (err) return cb(err); + self.store({ + noWallets: true + }, function(err) { + return cb(err, w); + }); }); }); }; @@ -348,11 +355,16 @@ Identity.importFromFullJson = function(str, password, opts, cb) { opts.email = email; opts.password = password; + if (!email) + return cb('BADSTR'); + var iden = new Identity(opts); json.wallets = json.wallets || {}; async.map(json.wallets, function(walletData, callback) { + if (!walletData) + return callback(); iden.importWalletFromObj(walletData, opts, function(err, w) { if (err) return callback(err); diff --git a/js/models/Wallet.js b/js/models/Wallet.js index a7cbffef0..f8cc9875b 100644 --- a/js/models/Wallet.js +++ b/js/models/Wallet.js @@ -2487,7 +2487,8 @@ Wallet.prototype.createTxSync = function(toAddress, amountSatStr, comment, utxos }; /** - * @desc Updates all the indexes for the current publicKeyRing + * @desc Updates all the indexes for the current publicKeyRing. This scans + * the blockchain looking for transactions on derived addresses. * * Triggers a wallet {@link Wallet#store} call * @param {Function} callback - called when all indexes have been updated. Receives an error, if any, as first argument diff --git a/views/import.html b/views/import.html index e5f49ed3a..d604661c3 100644 --- a/views/import.html +++ b/views/import.html @@ -1,7 +1,6 @@