From 24609c6b15f12e4d28f67c8db45f6dafec7ec011 Mon Sep 17 00:00:00 2001 From: Javier Date: Mon, 19 Oct 2015 11:19:28 -0300 Subject: [PATCH] set bws url for each wallet --- src/js/controllers/create.js | 1 - src/js/controllers/import.js | 1 - src/js/controllers/index.js | 11 +- src/js/controllers/join.js | 1 - src/js/controllers/preferencesBwsUrl.js | 17 +-- src/js/services/profileService.js | 132 ++++++++++++------------ 6 files changed, 77 insertions(+), 86 deletions(-) diff --git a/src/js/controllers/create.js b/src/js/controllers/create.js index b4cb64337..d985cad8d 100644 --- a/src/js/controllers/create.js +++ b/src/js/controllers/create.js @@ -118,7 +118,6 @@ angular.module('copayApp.controllers').controller('createController', configService.set(opts_, function(err) { if (err) console.log(err); $scope.$emit('Local/BWSUpdated'); - applicationService.restart(); go.walletHome(); }); diff --git a/src/js/controllers/import.js b/src/js/controllers/import.js index ddf1c5fb2..b1719ac5e 100644 --- a/src/js/controllers/import.js +++ b/src/js/controllers/import.js @@ -31,7 +31,6 @@ angular.module('copayApp.controllers').controller('importController', configService.set(opts, function(err) { if (err) return cb(err); $scope.$emit('Local/BWSUpdated'); - applicationService.restart(); return cb(null); }); } diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index 3ef895e8a..3bb7e2a9e 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -840,9 +840,9 @@ angular.module('copayApp.controllers').controller('indexController', function($r self.initGlidera = function(accessToken) { self.glideraEnabled = configService.getSync().glidera.enabled; -// self.glideraTestnet = configService.getSync().glidera.testnet; -// var network = self.glideraTestnet ? 'testnet' : 'livenet'; -// Disabled for testnet + // self.glideraTestnet = configService.getSync().glidera.testnet; + // var network = self.glideraTestnet ? 'testnet' : 'livenet'; + // Disabled for testnet self.glideraTestnet = false; var network = 'livenet'; @@ -1005,11 +1005,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r }, cb); }); - $rootScope.$on('Local/BWSUpdated', function(event) { - profileService.applyConfig(); - storageService.setCleanAndScanAddresses(function() {}); - }); - $rootScope.$on('Local/WalletCompleted', function(event) { self.setFocusedWallet(); go.walletHome(); diff --git a/src/js/controllers/join.js b/src/js/controllers/join.js index 7bcb80ef2..4edc065d7 100644 --- a/src/js/controllers/join.js +++ b/src/js/controllers/join.js @@ -84,7 +84,6 @@ angular.module('copayApp.controllers').controller('joinController', configService.set(opts_, function(err) { if (err) console.log(err); $scope.$emit('Local/BWSUpdated'); - applicationService.restart(); }); if (fc.isComplete() && (opts.mnemonic || opts.externalSource || opts.extendedPrivateKey)) { diff --git a/src/js/controllers/preferencesBwsUrl.js b/src/js/controllers/preferencesBwsUrl.js index 807749e63..5e993b0cb 100644 --- a/src/js/controllers/preferencesBwsUrl.js +++ b/src/js/controllers/preferencesBwsUrl.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('copayApp.controllers').controller('preferencesBwsUrlController', - function($scope, $log, configService, go, applicationService, profileService) { + function($scope, $log, configService, go, applicationService, profileService, storageService) { this.error = null; this.success = null; @@ -35,15 +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); - $scope.$emit('Local/BWSUpdated'); + configService.set(opts, function(err) { + if (err) console.log(err); + storageService.setCleanAndScanAddresses(function() { applicationService.restart(); }); + }); }; }); diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index ba1197a52..1abf74e2b 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -48,81 +48,80 @@ angular.module('copayApp.services') }); }; - root.setWalletClients = function() { - lodash.each(root.profile.credentials, function(credentials) { + root.setWalletClient = function(credentials) { + if (root.walletClients[credentials.walletId] && + root.walletClients[credentials.walletId].started) { + return; + } - if (root.walletClients[credentials.walletId] && - root.walletClients[credentials.walletId].started) { + var config = configService.getSync(); + var defaults = configService.getDefaults(); + + bwcService.setBaseUrl(config.bws[credentials.walletId] || defaults.bws.url); + bwcService.setTransports(['polling']); + + var client = bwcService.getClient(JSON.stringify(credentials)); + root.walletClients[credentials.walletId] = client; + client.removeAllListeners(); + + client.on('reconnect', function() { + if (root.focusedClient.credentials.walletId == client.credentials.walletId) { + $log.debug('### Online'); + } + }); + + client.on('reconnecting', function() { + if (root.focusedClient.credentials.walletId == client.credentials.walletId) { + $log.debug('### Offline'); + } + }); + + client.on('notification', function(n) { + $log.debug('BWC Notification:', n); + notificationService.newBWCNotification(n, + client.credentials.walletId, client.credentials.walletName); + + if (root.focusedClient.credentials.walletId == client.credentials.walletId) { + $rootScope.$emit(n.type); + } else { + $rootScope.$apply(); + } + }); + + client.on('walletCompleted', function() { + $log.debug('Wallet completed'); + + root.updateCredentialsFC(function() { + $rootScope.$emit('Local/WalletCompleted') + }); + + }); + + root.walletClients[credentials.walletId].started = true; + root.walletClients[credentials.walletId].doNotVerifyPayPro = isChromeApp; + + client.initNotifications(function(err) { + if (err) { + $log.error('Could not init notifications err:', err); return; } + }); + } - var client = bwcService.getClient(JSON.stringify(credentials)); - root.walletClients[credentials.walletId] = client; - client.removeAllListeners(); - - - client.on('reconnect', function() { - if (root.focusedClient.credentials.walletId == client.credentials.walletId) { - $log.debug('### Online'); - } - }); - - - client.on('reconnecting', function() { - if (root.focusedClient.credentials.walletId == client.credentials.walletId) { - $log.debug('### Offline'); - } - }); - - client.on('notification', function(n) { - $log.debug('BWC Notification:', n); - notificationService.newBWCNotification(n, - client.credentials.walletId, client.credentials.walletName); - - if (root.focusedClient.credentials.walletId == client.credentials.walletId) { - $rootScope.$emit(n.type); - } else { - $rootScope.$apply(); - } - }); - - client.on('walletCompleted', function() { - $log.debug('Wallet completed'); - - root.updateCredentialsFC(function() { - $rootScope.$emit('Local/WalletCompleted') - }); - - }); - - root.walletClients[credentials.walletId].started = true; - root.walletClients[credentials.walletId].doNotVerifyPayPro = isChromeApp; - - client.initNotifications(function(err) { - if (err) { - $log.error('Could not init notifications err:', err); - return; - } - }); + root.setWalletClients = function(walletId) { + var credentials = root.profile.credentials; + lodash.each(credentials, function(credentials) { + root.setWalletClient(credentials); }); $rootScope.$emit('Local/WalletListUpdated'); }; - - root.applyConfig = function() { - var config = configService.getSync(); - $log.debug('Applying preferences'); - bwcService.setBaseUrl(config.bws.url); - bwcService.setTransports(['polling']); - }; - root.bindProfile = function(profile, cb) { root.profile = profile; configService.get(function(err) { $log.debug('Preferences read'); if (err) return cb(err); - root.applyConfig(); root.setWalletClients(); storageService.getFocusedWalletId(function(err, focusedWalletId) { if (err) return cb(err); @@ -327,7 +326,7 @@ angular.module('copayApp.services') 'walletId': walletId }); if (w) { - return cb(gettext('Wallet already in Copay' + ": ") + w.walletName ); + return cb(gettext('Wallet already in Copay' + ": ") + w.walletName); } root.profile.credentials.push(JSON.parse(walletClient.export())); @@ -369,7 +368,7 @@ angular.module('copayApp.services') root._normalizeMnemonic = function(words) { - var isJA = words.indexOf('\u3000') > -1; + var isJA = words.indexOf('\u3000') > -1; var wordList = words.split(/[\u3000\s]+/); return wordList.join(isJA ? '\u3000' : ' '); @@ -415,7 +414,6 @@ angular.module('copayApp.services') root.create = function(opts, cb) { $log.info('Creating profile'); configService.get(function(err) { - root.applyConfig(); root._createNewProfile(opts, function(err, p) { if (err) return cb(err); @@ -552,7 +550,7 @@ angular.module('copayApp.services') $log.info('Requesting Ledger Chrome app to sign the transaction'); ledger.signTx(txp, 0, function(result) { - $log.debug('Ledger response',result); + $log.debug('Ledger response', result); if (!result.success) return cb(result.message || result.error); @@ -568,11 +566,11 @@ angular.module('copayApp.services') var fc = root.focusedClient; $log.info('Requesting Trezor to sign the transaction'); - var xPubKeys = lodash.pluck(fc.credentials.publicKeyRing,'xPubKey'); + var xPubKeys = lodash.pluck(fc.credentials.publicKeyRing, 'xPubKey'); trezor.signTx(xPubKeys, txp, 0, function(err, result) { if (err) return cb(err); - $log.debug('Trezor response',result); + $log.debug('Trezor response', result); txp.signatures = result.signatures; return fc.signTxProposal(txp, cb); });