From 551da51c41bb0e5a9ae3c6e16e24218350bfe3c2 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Wed, 19 Nov 2014 11:15:09 -0300 Subject: [PATCH] Import profile now checks if profile exists --- js/controllers/importProfile.js | 8 +++---- js/models/Identity.js | 42 +++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 22 deletions(-) diff --git a/js/controllers/importProfile.js b/js/controllers/importProfile.js index c8e34d386..fe9a292a3 100644 --- a/js/controllers/importProfile.js +++ b/js/controllers/importProfile.js @@ -28,17 +28,17 @@ angular.module('copayApp.controllers').controller('ImportProfileController', passphraseConfig: config.passphraseConfig, }, function(err, iden) { if (err) { - // if (err && !iden) { $scope.loading = false; - + if ((err.toString() || '').match('BADSTR')) { - $scope.error ='Bad password or corrupt profile file'; + $scope.error = 'Bad password or corrupt profile file'; } else if ((err.toString() || '').match('EEXISTS')) { $scope.error = 'Profile already exists'; } else { $scope.error = 'Unknown error'; } - + $scope.$digest(); + } else { var firstWallet = iden.getLastFocusedWallet(); controllerUtils.bindProfile($scope, iden, firstWallet); diff --git a/js/models/Identity.js b/js/models/Identity.js index 891006670..2503a9c62 100644 --- a/js/models/Identity.js +++ b/js/models/Identity.js @@ -300,7 +300,9 @@ Identity.prototype.importWalletFromObj = function(obj, opts, cb) { self.addWallet(w); self.bindWallet(w); - var writeOpts = _.extend({ noWallets: true }, opts); + var writeOpts = _.extend({ + noWallets: true + }, opts); self.storeWallet(w, function(err) { if (err) return cb(err); @@ -365,26 +367,30 @@ Identity.importFromFullJson = function(str, password, opts, cb) { json.wallets = json.wallets || {}; iden.store(opts, function(err) { - console.log('Error importing existing profile',err); - return cb(err, iden); + if (err) return cb(err); //profile already exists + + opts.failIfExists = false; + async.map(json.wallets, function(walletData, callback) { + + if (!walletData) + return callback(); + + iden.importWalletFromObj(walletData, opts, function(err, w) { + if (err) return callback(err); + log.debug('Wallet ' + w.getId() + ' imported'); + callback(); + }); + }, function(err, results) { + if (err) return cb(err); + + iden.store(opts, function(err) { + return cb(err, iden); + }); + }); + }); - async.map(json.wallets, function(walletData, callback) { - if (!walletData) - return callback(); - iden.importWalletFromObj(walletData, opts, function(err, w) { - if (err) return callback(err); - log.debug('Wallet ' + w.getId() + ' imported'); - callback(); - }); - }, function(err, results) { - if (err) return cb(err); - - iden.store(opts, function(err) { - return cb(err, iden); - }); - }); }; Identity.prototype.bindWallet = function(w) {