From 3d7132861dd9d25091654df5e3d380b96e46b516 Mon Sep 17 00:00:00 2001 From: Javier Date: Mon, 19 Oct 2015 17:26:15 -0300 Subject: [PATCH] fix create and join bws errors --- src/js/controllers/create.js | 10 +--- src/js/controllers/import.js | 12 +--- src/js/controllers/index.js | 13 ++--- src/js/controllers/join.js | 22 ++------ src/js/controllers/preferencesBwsUrl.js | 18 +++--- src/js/services/profileService.js | 74 ++++++++++++++++--------- src/js/services/storageService.js | 6 +- 7 files changed, 73 insertions(+), 82 deletions(-) diff --git a/src/js/controllers/create.js b/src/js/controllers/create.js index 6be707689..d7f84738f 100644 --- a/src/js/controllers/create.js +++ b/src/js/controllers/create.js @@ -56,6 +56,7 @@ angular.module('copayApp.controllers').controller('createController', name: form.walletName.$modelValue, myName: $scope.totalCopayers > 1 ? form.myName.$modelValue : null, networkName: form.isTestnet.$modelValue ? 'testnet' : 'livenet', + bwsurl: $scope.bwsurl }; var setSeed = form.setSeed.$modelValue; if (setSeed) { @@ -111,15 +112,6 @@ angular.module('copayApp.controllers').controller('createController', return; } - var opts_ = { - bws: {} - }; - opts_.bws[walletId] = $scope.bwsurl; - configService.set(opts_, function(err) { - if (err) console.log(err); - go.walletHome(); - }); - if (opts.mnemonic || opts.externalSource || opts.extendedPrivateKey) { if (opts.n == 1) { $rootScope.$emit('Local/WalletImported', walletId); diff --git a/src/js/controllers/import.js b/src/js/controllers/import.js index 385b6252f..be7dcba92 100644 --- a/src/js/controllers/import.js +++ b/src/js/controllers/import.js @@ -28,10 +28,7 @@ angular.module('copayApp.controllers').controller('importController', bws: {} }; opts.bws[walletId] = $scope.bwsurl; - configService.set(opts, function(err) { - if (err) return cb(err); - return cb(null); - }); + configService.set(opts, cb); } var _importBlob = function(str, opts) { @@ -56,7 +53,8 @@ angular.module('copayApp.controllers').controller('importController', $timeout(function() { profileService.importWallet(str2, { compressed: null, - password: null + password: null, + bwsurl: $scope.bwsurl }, function(err, walletId) { self.loading = false; if (err) { @@ -64,7 +62,6 @@ angular.module('copayApp.controllers').controller('importController', } else { setBwsURL(walletId, function() { $rootScope.$emit('Local/WalletImported', walletId); - go.walletHome(); notification.success(gettext('Success'), gettext('Your wallet has been imported correctly')); }); } @@ -88,7 +85,6 @@ angular.module('copayApp.controllers').controller('importController', setBwsURL(walletId, function() { $rootScope.$emit('Local/WalletImported', walletId); notification.success(gettext('Success'), gettext('Your wallet has been imported correctly')); - go.walletHome(); }); }); }, 100); @@ -109,7 +105,6 @@ angular.module('copayApp.controllers').controller('importController', setBwsURL(walletId, function() { $rootScope.$emit('Local/WalletImported', walletId); notification.success(gettext('Success'), gettext('Your wallet has been imported correctly')); - go.walletHome(); }); }); }, 100); @@ -264,7 +259,6 @@ angular.module('copayApp.controllers').controller('importController', setBwsURL(walletId, function() { $rootScope.$emit('Local/WalletImported', walletId); notification.success(gettext('Success'), gettext('Your wallet has been imported correctly')); - go.walletHome(); }); }); }, 100); diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index 3bb7e2a9e..79e44238a 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -1129,18 +1129,15 @@ angular.module('copayApp.controllers').controller('indexController', function($r self.setFocusedWallet(); self.updateTxHistory(); go.walletHome(); - storageService.getCleanAndScanAddresses(function(err, val) { - if (val) { - $log.debug('Clear last address cache and Scan'); - lodash.each(lodash.keys(profileService.walletClients), function(walletId) { - addressService.expireAddress(walletId, function(err) { - self.startScan(walletId); - }); + storageService.getCleanAndScanAddresses(function(err, walletId) { + if (walletId && profileService.walletClients[walletId]) { + $log.debug('Clear last address cache and Scan ', walletId); + addressService.expireAddress(walletId, function(err) { + self.startScan(walletId); }); storageService.removeCleanAndScanAddresses(function() {}); } }); - }); $rootScope.$on('Local/SetTab', function(event, tab, reset) { diff --git a/src/js/controllers/join.js b/src/js/controllers/join.js index 9bbc8f102..9b5c044ed 100644 --- a/src/js/controllers/join.js +++ b/src/js/controllers/join.js @@ -1,11 +1,10 @@ 'use strict'; angular.module('copayApp.controllers').controller('joinController', - function($scope, $rootScope, $timeout, go, notification, profileService, configService, isCordova, $modal, gettext, lodash, ledger, trezor) { + function($scope, $rootScope, $timeout, go, notification, profileService, configService, isCordova, storageService, applicationService, $modal, gettext, lodash, ledger, trezor) { var self = this; var defaults = configService.getDefaults(); - var defaults = configService.getDefaults(); $scope.bwsurl = defaults.bws.url; this.onQrCodeScanned = function(data) { @@ -24,6 +23,7 @@ angular.module('copayApp.controllers').controller('joinController', var opts = { secret: form.secret.$modelValue, myName: form.myName.$modelValue, + bwsurl: $scope.bwsurl } var setSeed = form.setSeed.$modelValue; @@ -71,25 +71,13 @@ angular.module('copayApp.controllers').controller('joinController', self.loading = false; self.error = err; $rootScope.$apply(); - return + return; } + $timeout(function() { var fc = profileService.focusedClient; - - var opts_ = { - bws: {} - }; - - opts_.bws[fc.credentials.walletId] = $scope.bwsurl; - configService.set(opts_, function(err) { - if (err) console.log(err); - }); - - if (fc.isComplete() && (opts.mnemonic || opts.externalSource || opts.extendedPrivateKey)) { + if (fc.isComplete() && (opts.mnemonic || opts.externalSource || opts.extendedPrivateKey)) $rootScope.$emit('Local/WalletImported', fc.credentials.walletId); - } else { - go.walletHome(); - } }, 2000); }); }, 100); diff --git a/src/js/controllers/preferencesBwsUrl.js b/src/js/controllers/preferencesBwsUrl.js index 5e993b0cb..7d5d5465c 100644 --- a/src/js/controllers/preferencesBwsUrl.js +++ b/src/js/controllers/preferencesBwsUrl.js @@ -35,16 +35,16 @@ angular.module('copayApp.controllers').controller('preferencesBwsUrlController', this.bwsurl = bws; } - var opts = { - bws: {} - }; - opts.bws[walletId] = this.bwsurl; + var opts = { + bws: {} + }; + opts.bws[walletId] = this.bwsurl; - configService.set(opts, function(err) { - if (err) console.log(err); - storageService.setCleanAndScanAddresses(function() { - applicationService.restart(); + configService.set(opts, function(err) { + if (err) console.log(err); + storageService.setCleanAndScanAddresses(walletId, function() { + applicationService.restart(); + }); }); - }); }; }); diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index 1abf74e2b..c03bea497 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -48,17 +48,21 @@ angular.module('copayApp.services') }); }; + root.setBaseURL = function(walletId) { + var config = configService.getSync(); + var defaults = configService.getDefaults(); + + bwcService.setBaseUrl(config.bws[walletId] || defaults.bws.url); + bwcService.setTransports(['polling']); + } + root.setWalletClient = function(credentials) { if (root.walletClients[credentials.walletId] && root.walletClients[credentials.walletId].started) { return; } - var config = configService.getSync(); - var defaults = configService.getDefaults(); - - bwcService.setBaseUrl(config.bws[credentials.walletId] || defaults.bws.url); - bwcService.setTransports(['polling']); + root.setBaseURL(credentials.walletId); var client = bwcService.getClient(JSON.stringify(credentials)); root.walletClients[credentials.walletId] = client; @@ -108,7 +112,7 @@ angular.module('copayApp.services') }); } - root.setWalletClients = function(walletId) { + root.setWalletClients = function() { var credentials = root.profile.credentials; lodash.each(credentials, function(credentials) { root.setWalletClient(credentials); @@ -165,6 +169,9 @@ angular.module('copayApp.services') root._seedWallet = function(opts, cb) { opts = opts || {}; + if (opts.bwsurl) + bwcService.setBaseUrl(opts.bwsurl); + var walletClient = bwcService.getClient(); var network = opts.networkName || 'livenet'; @@ -240,19 +247,36 @@ angular.module('copayApp.services') }, function(err, secret) { if (err) return bwsError.cb(err, gettext('Error creating wallet'), cb); - root.profile.credentials.push(JSON.parse(walletClient.export())); - root.setWalletClients(); - - root.setAndStoreFocus(walletClient.credentials.walletId, function() { - storageService.storeProfile(root.profile, function(err) { - return cb(null, secret, walletClient.credentials.walletId); - }); + root.storeData(walletClient, function(err) { + if (err) return cb(err); + return cb(null); }); }) }); }; - root.joinWallet = function(opts, cb) { + root.storeData = function(walletClient, cb) { + var walletId = walletClient.credentials.walletId; + var opts_ = { + bws: {} + }; + opts_.bws[walletId] = bwsurl; + configService.set(opts_, function(err) { + if (err) console.log(err); + + root.profile.credentials.push(JSON.parse(walletClient.export())); + root.setWalletClients(); + + root.setAndStoreFocus(walletId, function() { + storageService.storeProfile(root.profile, function(err) { + if (err) return cb(err); + return cb(null); + }); + }); + }); + } + + root.joinWallet = function(opts, bwsurl, cb) { var walletClient = bwcService.getClient(); $log.debug('Joining Wallet:', opts); @@ -278,19 +302,14 @@ angular.module('copayApp.services') walletClient.joinWallet(opts.secret, opts.myName || 'me', {}, function(err) { if (err) return bwsError.cb(err, gettext('Could not join wallet'), cb); - root.profile.credentials.push(JSON.parse(walletClient.export())); - root.setWalletClients(); - - root.setAndStoreFocus(walletClient.credentials.walletId, function() { - storageService.storeProfile(root.profile, function(err) { - return cb(); - }); + root.storeData(walletClient, function(err) { + if (err) return cb(err); + return cb(null); }); - }) - }) + }); + }); }; - root.getClient = function(walletId) { return root.walletClients[walletId]; }; @@ -341,7 +360,11 @@ angular.module('copayApp.services') }; root.importWallet = function(str, opts, cb) { + if (opts.bwsurl) + bwcService.setBaseUrl(opts.bwsurl); + var walletClient = bwcService.getClient(); + $log.debug('Importing Wallet:', opts); try { walletClient.import(str, { @@ -366,7 +389,6 @@ angular.module('copayApp.services') }); }; - root._normalizeMnemonic = function(words) { var isJA = words.indexOf('\u3000') > -1; var wordList = words.split(/[\u3000\s]+/); @@ -409,8 +431,6 @@ angular.module('copayApp.services') }); }; - - root.create = function(opts, cb) { $log.info('Creating profile'); configService.get(function(err) { diff --git a/src/js/services/storageService.js b/src/js/services/storageService.js index b7208ea7a..bff7c9ec6 100644 --- a/src/js/services/storageService.js +++ b/src/js/services/storageService.js @@ -124,7 +124,7 @@ angular.module('copayApp.services') }; root.storeFocusedWalletId = function(id, cb) { - storage.set('focusedWalletId', id||'', cb); + storage.set('focusedWalletId', id || '', cb); }; root.getFocusedWalletId = function(cb) { @@ -151,8 +151,8 @@ angular.module('copayApp.services') storage.get('backup-' + walletId, cb); }; - root.setCleanAndScanAddresses = function(cb) { - storage.set('CleanAndScanAddresses', Date.now(), cb); + root.setCleanAndScanAddresses = function(walletId, cb) { + storage.set('CleanAndScanAddresses', walletId, cb); }; root.getCleanAndScanAddresses = function(cb) {