From 707acfaf6403d6afd83f9e9b291f1f7e37e11405 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 19 Jun 2015 21:33:27 -0300 Subject: [PATCH 1/7] refactor getWallet in profile Signed-off-by: Matias Alejo Garcia --- public/views/paymentUri.html | 39 ++++++++++++++++++++----------- src/js/controllers/paymentUri.js | 19 +-------------- src/js/services/profileService.js | 24 +++++++++++++++++++ 3 files changed, 50 insertions(+), 32 deletions(-) diff --git a/public/views/paymentUri.html b/public/views/paymentUri.html index 0c5ece585..390a84d77 100644 --- a/public/views/paymentUri.html +++ b/public/views/paymentUri.html @@ -9,7 +9,7 @@

Bitcoin URI is NOT valid!

-
+

Make a payment to

Address: {{payment.uri.address.toString()}}
@@ -17,19 +17,30 @@
Message: {{payment.uri.message}}
Network: {{payment.uri.network}}
-

Select a wallet

- +
+
+ + There are not wallets to make this payment + [testnet] + +
+
+ +
+

Select a wallet

+ +
diff --git a/src/js/controllers/paymentUri.js b/src/js/controllers/paymentUri.js index 831c5c791..9d88f35eb 100644 --- a/src/js/controllers/paymentUri.js +++ b/src/js/controllers/paymentUri.js @@ -38,24 +38,7 @@ angular.module('copayApp.controllers').controller('paymentUriController', }; this.getWallets = function(network) { - if (!profileService.profile) return; - var config = configService.getSync(); - config.colorFor = config.colorFor || {}; - config.aliasFor = config.aliasFor || {}; - var ret = lodash.map(profileService.profile.credentials, function(c) { - return { - m: c.m, - n: c.n, - name: config.aliasFor[c.walletId] || c.walletName, - id: c.walletId, - network: c.network, - color: config.colorFor[c.walletId] || '#2C3E50' - }; - }); - ret = lodash.filter(ret, function(w) { - return (w.network == network); - }); - return lodash.sortBy(ret, 'name'); + return profileService.getWallets(network); }; this.selectWallet = function(wid) { diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index d237bf47a..28c5477e7 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -396,5 +396,29 @@ angular.module('copayApp.services') }); }; + root.getWallets = function(network) { + if (!root.profile) return []; + + var config = configService.getSync(); + config.colorFor = config.colorFor || {}; + config.aliasFor = config.aliasFor || {}; + var ret = lodash.map(root.profile.credentials, function(c) { + return { + m: c.m, + n: c.n, + name: config.aliasFor[c.walletId] || c.walletName, + id: c.walletId, + network: c.network, + color: config.colorFor[c.walletId] || '#2C3E50' + }; + }); + ret = lodash.filter(ret, function(w) { + return (w.network == network); + }); + return lodash.sortBy(ret, 'name'); + }; + + + return root; }); From 7433b1b863d71f1610bd71553e1348a76d5ac02e Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 19 Jun 2015 22:01:38 -0300 Subject: [PATCH 2/7] wallet selection popup, link to open it --- public/views/modals/wallets.html | 31 +++++++++++++++++++++++++++++++ public/views/walletHome.html | 5 +++++ src/css/mobile.css | 2 +- src/js/controllers/index.js | 1 + src/js/controllers/walletHome.js | 22 ++++++++++++++++++++++ 5 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 public/views/modals/wallets.html diff --git a/public/views/modals/wallets.html b/public/views/modals/wallets.html new file mode 100644 index 000000000..163d5ec70 --- /dev/null +++ b/public/views/modals/wallets.html @@ -0,0 +1,31 @@ + + + + diff --git a/public/views/walletHome.html b/public/views/walletHome.html index 674153eaa..11f66f4a9 100644 --- a/public/views/walletHome.html +++ b/public/views/walletHome.html @@ -327,6 +327,11 @@
+
+ send to my wallets +
+ + diff --git a/src/css/mobile.css b/src/css/mobile.css index 352972395..165843b02 100644 --- a/src/css/mobile.css +++ b/src/css/mobile.css @@ -285,7 +285,7 @@ a.missing-copayers { background-color: #122232; } -.sidebar .avatar-wallet, .payment-uri .avatar-wallet { +.sidebar .avatar-wallet, .payment-uri .avatar-wallet, .modal-content .avatar-wallet { background-color: #2C3E50; color: #fff; font-size: 20px; diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index f6ec8aea5..6e25753b6 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -237,6 +237,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r self.copayers = walletStatus.wallet.copayers; self.preferences = walletStatus.preferences; self.setBalance(walletStatus.balance); + self.otherWallets = profileService.getWallets(self.network); $rootScope.$apply(); }); }); diff --git a/src/js/controllers/walletHome.js b/src/js/controllers/walletHome.js index 3c7e0206e..8c9347733 100644 --- a/src/js/controllers/walletHome.js +++ b/src/js/controllers/walletHome.js @@ -125,6 +125,28 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi }); }; + + $scope.openWalletsModal = function(wallets) { + var fc = profileService.focusedClient; + + var ModalInstanceCtrl = function($scope, $modalInstance) { + $scope.wallets = wallets; + $scope.cancel = function() { + $modalInstance.dismiss('cancel'); + }; + }; + var modalInstance = $modal.open({ + templateUrl: 'views/modals/wallets.html', + windowClass: 'full animated slideInUp', + controller: ModalInstanceCtrl, + }); + + modalInstance.result.finally(function() { + var m = angular.element(document.getElementsByClassName('reveal-modal')); + m.addClass('slideOutDown'); + }); + }; + this.openTxpModal = function(tx, copayers) { var fc = profileService.focusedClient; var refreshUntilItChanges = false; From 38b8240341ea206e0720a10a30e35b34772ec9eb Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Fri, 26 Jun 2015 05:42:39 -0300 Subject: [PATCH 3/7] also try to register handlers in non-cordova environments --- src/js/init.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/js/init.js b/src/js/init.js index a0070e3f7..75e113534 100644 --- a/src/js/init.js +++ b/src/js/init.js @@ -6,6 +6,15 @@ angular.element(document).ready(function() { var startAngular = function() { angular.bootstrap(document, ['copayApp']); }; + + var handleBitcoinURI = function(url) { + if (!url) return; + setTimeout(function() { + window.location = '#/uri-payment/' + url; + }, 1000); + }; + + /* Cordova specific Init */ if (window.cordova !== undefined) { @@ -37,8 +46,7 @@ angular.element(document).ready(function() { var loc = window.location; if (loc.toString().match(/index\.html#\/$/)) { navigator.app.exitApp(); - } - else { + } else { window.location = '#/cordova/walletHome'; } }, false); @@ -53,13 +61,6 @@ angular.element(document).ready(function() { navigator.splashscreen.hide(); }, 2000); - function handleBitcoinURI(url) { - if (!url) return; - setTimeout(function() { - window.location = '#/uri-payment/' + url; - }, 1000); - } - window.plugins.webintent.getUri(handleBitcoinURI); window.plugins.webintent.onNewIntent(handleBitcoinURI); window.handleOpenURL = handleBitcoinURI; @@ -67,6 +68,13 @@ angular.element(document).ready(function() { startAngular(); }, false); } else { + + try { + window.handleOpenURL = handleBitcoinURI; + window.plugins.webintent.getUri(handleBitcoinURI); + window.plugins.webintent.onNewIntent(handleBitcoinURI); + } catch (e) {} + startAngular(); } From 1323ad48db12a98509c2f7d7e8f2664fb71086e2 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sat, 27 Jun 2015 13:22:56 -0300 Subject: [PATCH 4/7] refactor address management --- public/views/walletHome.html | 2 +- src/js/controllers/index.js | 27 ++++------- src/js/controllers/walletHome.js | 62 +++++++++---------------- src/js/services/addressService.js | 75 +++++++++++++++++++++++++++++++ src/js/services/profileService.js | 5 +++ 5 files changed, 110 insertions(+), 61 deletions(-) create mode 100644 src/js/services/addressService.js diff --git a/public/views/walletHome.html b/public/views/walletHome.html index 11f66f4a9..037f68fca 100644 --- a/public/views/walletHome.html +++ b/public/views/walletHome.html @@ -258,7 +258,7 @@
- diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index 6e25753b6..635b4f0fa 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, lodash, go, profileService, configService, isCordova, rateService, storageService, gettextCatalog, gettext, amMoment) { +angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettextCatalog, gettext, amMoment) { var self = this; self.isCordova = isCordova; @@ -464,7 +464,12 @@ angular.module('copayApp.controllers').controller('indexController', function($r self.alternativeIsoCode = config.alternativeIsoCode; // Check address - self.checkLastAddress(balance.byAddress); + addressService.isUsed(self.walletId, balance.byAddress, function(err, used){ + if (used) { + $log.debug('Address used. Creating new'); + $rootScope.$emit('Local/NeedNewAddress'); + } + }); rateService.whenAvailable(function() { @@ -488,20 +493,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r } }; - self.checkLastAddress = function(byAddress, cb) { - storageService.getLastAddress(self.walletId, function(err, addr) { - var used = lodash.find(byAddress, { - address: addr - }); - if (used) { - $log.debug('Address ' + addr + ' was used. Cleaning Cache.') - storageService.clearLastAddress(self.walletId, function(err) { - $rootScope.$emit('Local/NeedNewAddress', err); - if (cb) return cb(); - }); - }; - }); - }; self.clientError = function(err) { if (isCordova) { @@ -729,7 +720,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r $rootScope.$on('Local/WalletImported', function(event, walletId) { self.needsBackup = false; storageService.setBackupFlag(walletId, function() { - storageService.clearLastAddress(walletId, function(err) { + addressService.expireAddress(walletId, function(err) { self.startScan(walletId); }); }); @@ -792,7 +783,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r if (val) { $log.debug('Clear last address cache and Scan'); lodash.each(lodash.keys(profileService.walletClients), function(walletId) { - storageService.clearLastAddress(walletId, function(err) { + addressService.expireAddress(walletId, function(err) { self.startScan(walletId); }); }); diff --git a/src/js/controllers/walletHome.js b/src/js/controllers/walletHome.js index 8c9347733..f2e1e29a9 100644 --- a/src/js/controllers/walletHome.js +++ b/src/js/controllers/walletHome.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.controllers').controller('walletHomeController', function($scope, $rootScope, $timeout, $filter, $modal, $log, notification, txStatus, isCordova, profileService, lodash, configService, rateService, storageService, bitcore, isChromeApp, gettext, gettextCatalog, nodeWebkit) { +angular.module('copayApp.controllers').controller('walletHomeController', function($scope, $rootScope, $timeout, $filter, $modal, $log, notification, txStatus, isCordova, profileService, lodash, configService, rateService, storageService, bitcore, isChromeApp, gettext, gettextCatalog, nodeWebkit, addressService) { var self = this; $rootScope.hideMenuBar = false; @@ -38,7 +38,7 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi }); var disableAddrListener = $rootScope.$on('Local/NeedNewAddress', function() { - self.setNewAddress(); + self.setAddress(true); }); var disableFocusListener = $rootScope.$on('Local/NewFocusedWallet', function() { @@ -90,6 +90,8 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi var parseError = function(err) { + if (!err) return; + if (err.message) { // TODO : this is not used anymore? if (err.message.indexOf('CORS') >= 0) { @@ -361,56 +363,31 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi }; - // Receive - this.setNewAddress = function() { - var fc = profileService.focusedClient; - self.generatingAddress = true; - self.addrError = null; - fc.createAddress(function(err, addr) { - self.generatingAddress = false; - if (err) { - if (err.error && err.error.match(/locked/gi)) { - $log.debug(err.error); - $timeout(function() { - self.setNewAddress(); - }, 5000); - } else { - $log.debug('Creating address ERROR:', err); - parseError(err); - self.addrError = err.message || gettext('Could not create address. Check you connection and try again'); - $scope.$digest(); - } - return; - } - self.addrError = null; - self.addr[fc.credentials.walletId] = addr.address; - storageService.storeLastAddress(fc.credentials.walletId, addr.address, function() { - - self.generatingAddress = false; - $scope.$digest(); - }); - }); - }; - - this.setAddress = function() { + this.setAddress = function(forceNew) { self.addrError = null; var fc = profileService.focusedClient; if (!fc) return; - if (self.addr[fc.credentials.walletId]) { + // Address already set? + if (!forceNew && self.addr[fc.credentials.walletId]) { return; } - + self.generatingAddress = true; $timeout(function() { - storageService.getLastAddress(fc.credentials.walletId, function(err, addr) { - if (addr) { - self.addr[fc.credentials.walletId] = addr; - $scope.$digest(); - } else { - self.setNewAddress(); + addressService.getAddress(fc.credentials.walletId, forceNew, function(err,addr){ + self.generatingAddress = false; + + if (err) { + parseError(err); + self.addrError = err.message || gettext('Could not create address. Check you connection and try again'); } + + if (addr) + self.addr[fc.credentials.walletId] = addr; + + $scope.$digest(); }); }); }; @@ -1050,6 +1027,7 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi } } + /* Start setup */ this.bindTouchDown(); this.setAddress(); diff --git a/src/js/services/addressService.js b/src/js/services/addressService.js new file mode 100644 index 000000000..975961214 --- /dev/null +++ b/src/js/services/addressService.js @@ -0,0 +1,75 @@ +'use strict'; +'use strict'; +angular.module('copayApp.services') + .factory('addressService', function(storageService, profileService, $log, $timeout, lodash) { + var root = {}; + + + root.expireAddress = function(walletId,cb) { + $log.debug('Cleaning Address ' + addr ); + storageService.clearLastAddress(walletId, function(err) { + return cb(err); + }); + }; + + root.isUsed = function(walletId, byAddress, cb) { + storageService.getLastAddress(walletId, function(err, addr) { + var used = lodash.find(byAddress, { + address: addr + }); + return cb(null, used); + }); + }; + + root._createAddress = function(walletId, cb) { + var client = profileService.getClient(walletId); + + $log.debug('Creating address for wallet:', walletId); + + client.createAddress(function(err, addr) { + if (err) { + if (err.error && err.error.match(/locked/gi)) { + $log.debug(err.error); + return $timeout(function() { + root._createAddress(walletId, cb); + }, 5000); + } + $log.debug('Creating address ERROR:', err); + return cb(err); + } + return cb(null, addr.address); + }); + }; + + root.getAddress = function(walletId, forceNew, cb) { + + var firstStep; + if (forceNew) { + firstStep = storageService.clearLastAddress; + } else { + firstStep = function(walletId, cb) { + return cb(); + }; + } + + firstStep(walletId, function(err) { + if (err) return cb(err); + + storageService.getLastAddress(walletId, function(err, addr) { + if (err) return cb(err); + + if (addr) return cb(null, addr); + + root._createAddress(walletId, function(err, addr) { + if (err) return cb(err); + storageService.storeLastAddress(walletId, addr, function() { + if (err) return cb(err); + return cb(null, addr); + }); + }); + }); + }); + }; + + return root; + }); diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index 28c5477e7..956dc76ee 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -233,6 +233,11 @@ angular.module('copayApp.services') }) }; + + root.getClient = function(walletId) { + return root.walletClients[walletId]; + }; + root.deleteWalletFC = function(opts, cb) { var fc = root.focusedClient; $log.debug('Deleting Wallet:', fc.credentials.walletName); From 063b520db0d9b66c38d30906c6e31a2d352ea24a Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sat, 27 Jun 2015 13:48:25 -0300 Subject: [PATCH 5/7] create new address on send --- public/views/modals/wallets.html | 38 ++++++++++++++++++++++---------- src/js/controllers/index.js | 4 +++- src/js/controllers/walletHome.js | 26 +++++++++++++++++++++- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/public/views/modals/wallets.html b/public/views/modals/wallets.html index 163d5ec70..8975e5567 100644 --- a/public/views/modals/wallets.html +++ b/public/views/modals/wallets.html @@ -13,18 +13,32 @@ diff --git a/src/js/controllers/index.js b/src/js/controllers/index.js index 635b4f0fa..39e9ce28d 100644 --- a/src/js/controllers/index.js +++ b/src/js/controllers/index.js @@ -237,7 +237,9 @@ angular.module('copayApp.controllers').controller('indexController', function($r self.copayers = walletStatus.wallet.copayers; self.preferences = walletStatus.preferences; self.setBalance(walletStatus.balance); - self.otherWallets = profileService.getWallets(self.network); + self.otherWallets = lodash.filter(profileService.getWallets(self.network), function(w) { + return w.id != self.walletId; + });; $rootScope.$apply(); }); }); diff --git a/src/js/controllers/walletHome.js b/src/js/controllers/walletHome.js index f2e1e29a9..cc6f3e342 100644 --- a/src/js/controllers/walletHome.js +++ b/src/js/controllers/walletHome.js @@ -129,14 +129,32 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi $scope.openWalletsModal = function(wallets) { - var fc = profileService.focusedClient; var ModalInstanceCtrl = function($scope, $modalInstance) { $scope.wallets = wallets; $scope.cancel = function() { $modalInstance.dismiss('cancel'); }; + + $scope.selectWallet = function(walletId, walletName) { + $scope.gettingAddress=true; + $scope.selectedWalletName=walletName; + $timeout(function(){ + $scope.$apply(); + }); + addressService.getAddress(walletId,true, function(err,addr) { + $scope.gettingAddress=false; + if (!err || addr) + $modalInstance.close(addr); + else { + parseError(err); + self.error = err; + $modalInstance.dismiss('cancel'); + } + }); + }; }; + var modalInstance = $modal.open({ templateUrl: 'views/modals/wallets.html', windowClass: 'full animated slideInUp', @@ -147,6 +165,12 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi var m = angular.element(document.getElementsByClassName('reveal-modal')); m.addClass('slideOutDown'); }); + + modalInstance.result.then(function(addr) { + if (addr) { + self.setForm(addr); + } + }); }; this.openTxpModal = function(tx, copayers) { From d8deaea8c1e579241c82aa41684fb74eea1a6626 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sat, 27 Jun 2015 13:51:46 -0300 Subject: [PATCH 6/7] update receive string --- public/views/walletHome.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/public/views/walletHome.html b/public/views/walletHome.html index 037f68fca..e298293d8 100644 --- a/public/views/walletHome.html +++ b/public/views/walletHome.html @@ -266,7 +266,7 @@
From 7efb48ff063960f93080a4d99460ef41fa6a3a04 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Sat, 27 Jun 2015 14:16:02 -0300 Subject: [PATCH 7/7] not always a new addr --- src/js/controllers/walletHome.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/controllers/walletHome.js b/src/js/controllers/walletHome.js index cc6f3e342..86aa52c4f 100644 --- a/src/js/controllers/walletHome.js +++ b/src/js/controllers/walletHome.js @@ -142,7 +142,7 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi $timeout(function(){ $scope.$apply(); }); - addressService.getAddress(walletId,true, function(err,addr) { + addressService.getAddress(walletId, false, function(err,addr) { $scope.gettingAddress=false; if (!err || addr) $modalInstance.close(addr);