diff --git a/src/js/controllers/bitpayCard.js b/src/js/controllers/bitpayCard.js index dd84deaf9..48ec83506 100644 --- a/src/js/controllers/bitpayCard.js +++ b/src/js/controllers/bitpayCard.js @@ -145,6 +145,12 @@ angular.module('copayApp.controllers').controller('bitpayCardController', functi updateHistoryFromCache(function() { self.update(); }); + bitpayCardService.getBitpayDebitCards(function(err, cards) { + if (err) return; + $scope.card = lodash.find(cards, function(card) { + return card.eid == $scope.cardId; + }); + }); } }); diff --git a/src/js/controllers/preferencesBitpayCard.js b/src/js/controllers/preferencesBitpayCard.js index 0fb74d228..33060296d 100644 --- a/src/js/controllers/preferencesBitpayCard.js +++ b/src/js/controllers/preferencesBitpayCard.js @@ -3,16 +3,16 @@ angular.module('copayApp.controllers').controller('preferencesBitpayCardController', function($scope, $state, $timeout, $ionicHistory, bitpayCardService, popupService, gettextCatalog) { - $scope.remove = function() { + $scope.remove = function(card) { var msg = gettextCatalog.getString('Are you sure you would like to remove your BitPay Card account from this device?'); popupService.showConfirm(null, msg, null, null, function(res) { - if (res) remove(); + if (res) remove(card); }); }; - var remove = function() { - bitpayCardService.remove(function() { - $ionicHistory.removeBackView(); + var remove = function(card) { + bitpayCardService.remove(card, function() { + $ionicHistory.clearHistory(); $timeout(function() { $state.go('tabs.home'); }, 100); @@ -22,7 +22,7 @@ angular.module('copayApp.controllers').controller('preferencesBitpayCardControll $scope.$on("$ionicView.beforeEnter", function(event, data) { bitpayCardService.getBitpayDebitCards(function(err, data) { if (err) return; - $scope.bitpayCards = data.cards; + $scope.bitpayCards = data; }); }); diff --git a/src/js/controllers/tab-home.js b/src/js/controllers/tab-home.js index d30628d7c..93503cd77 100644 --- a/src/js/controllers/tab-home.js +++ b/src/js/controllers/tab-home.js @@ -246,7 +246,7 @@ angular.module('copayApp.controllers').controller('tabHomeController', $scope.bitpayCards = null; return; } - $scope.bitpayCards = data.cards; + $scope.bitpayCards = data; }); bitpayCardService.getBitpayDebitCardsHistory(null, function(err, data) { if (err) return; diff --git a/src/js/routes.js b/src/js/routes.js index ee65083d3..97cd6e6b6 100644 --- a/src/js/routes.js +++ b/src/js/routes.js @@ -1001,41 +1001,50 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr }); } - - $log.info('Init profile...'); - // Try to open local profile - profileService.loadAndBindProfile(function(err) { - $ionicHistory.nextViewOptions({ - disableAnimate: true - }); + $log.info('Verifying storage...'); + storageService.verify(function(err) { if (err) { - if (err.message && err.message.match('NOPROFILE')) { - $log.debug('No profile... redirecting'); - $state.go('onboarding.welcome'); - } else if (err.message && err.message.match('NONAGREEDDISCLAIMER')) { - if (lodash.isEmpty(profileService.getWallets())) { - $log.debug('No wallets and no disclaimer... redirecting'); - $state.go('onboarding.welcome'); - } else { - $log.debug('Display disclaimer... redirecting'); - $state.go('onboarding.disclaimer', { - resume: true - }); - } - } else { - throw new Error(err); // TODO - } + $log.error('Storage failed to verify: ' + err); + // TODO - what next? } else { - profileService.storeProfileIfDirty(); - $log.debug('Profile loaded ... Starting UX.'); - scannerService.gentleInitialize(); - $state.go('tabs.home'); + $log.info('Storage OK'); } - // After everything have been loaded, initialize handler URL - $timeout(function() { - openURLService.init(); - }, 1000); + $log.info('Init profile...'); + // Try to open local profile + profileService.loadAndBindProfile(function(err) { + $ionicHistory.nextViewOptions({ + disableAnimate: true + }); + if (err) { + if (err.message && err.message.match('NOPROFILE')) { + $log.debug('No profile... redirecting'); + $state.go('onboarding.welcome'); + } else if (err.message && err.message.match('NONAGREEDDISCLAIMER')) { + if (lodash.isEmpty(profileService.getWallets())) { + $log.debug('No wallets and no disclaimer... redirecting'); + $state.go('onboarding.welcome'); + } else { + $log.debug('Display disclaimer... redirecting'); + $state.go('onboarding.disclaimer', { + resume: true + }); + } + } else { + throw new Error(err); // TODO + } + } else { + profileService.storeProfileIfDirty(); + $log.debug('Profile loaded ... Starting UX.'); + scannerService.gentleInitialize(); + $state.go('tabs.home'); + } + + // After everything have been loaded, initialize handler URL + $timeout(function() { + openURLService.init(); + }, 1000); + }); }); }); diff --git a/src/js/services/bitpayCardService.js b/src/js/services/bitpayCardService.js index 84bf93a47..bd671c02d 100644 --- a/src/js/services/bitpayCardService.js +++ b/src/js/services/bitpayCardService.js @@ -174,7 +174,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http, if (err) return cb(err); root.getBitpayDebitCards(function(err, data) { if (err) return cb(err); - var card = lodash.find(data.cards, {id : cardId}); + var card = lodash.find(data, {id : cardId}); if (!card) return cb(_setError('Not card found')); // Get invoices $http(_post('/api/v2/' + card.token, json, credentials)).then(function(data) { @@ -211,7 +211,7 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http, if (err) return cb(err); root.getBitpayDebitCards(function(err, data) { if (err) return cb(err); - var card = lodash.find(data.cards, {id : cardId}); + var card = lodash.find(data, {id : cardId}); if (!card) return cb(_setError('Not card found')); $http(_post('/api/v2/' + card.token, json, credentials)).then(function(data) { $log.info('BitPay TopUp: SUCCESS'); @@ -288,13 +288,11 @@ angular.module('copayApp.services').factory('bitpayCardService', function($http, }); }; - root.remove = function(cb) { - storageService.removeBitpayCardCredentials(BITPAY_CARD_NETWORK, function(err) { - storageService.removeBitpayDebitCards(BITPAY_CARD_NETWORK, function(err) { - storageService.removeBitpayDebitCardsHistory(BITPAY_CARD_NETWORK, function(err) { - $log.info('BitPay Debit Cards Removed: SUCCESS'); - return cb(); - }); + root.remove = function(card, cb) { + storageService.removeBitpayDebitCard(BITPAY_CARD_NETWORK, card, function(err) { + storageService.removeBitpayDebitCardHistory(BITPAY_CARD_NETWORK, card, function(err) { + $log.info('BitPay Debit Card(s) Removed: SUCCESS'); + return cb(); }); }); }; diff --git a/src/js/services/storageService.js b/src/js/services/storageService.js index 60545747a..8c956dc5f 100644 --- a/src/js/services/storageService.js +++ b/src/js/services/storageService.js @@ -1,6 +1,6 @@ 'use strict'; angular.module('copayApp.services') - .factory('storageService', function(logHeader, fileStorageService, localStorageService, sjcl, $log, lodash, platformInfo) { + .factory('storageService', function(logHeader, fileStorageService, localStorageService, sjcl, $log, lodash, platformInfo, $timeout) { var root = {}; @@ -74,7 +74,90 @@ angular.module('copayApp.services') }); }; + //////////////////////////////////////////////////////////////////////////// + // + // UPGRADING STORAGE + // + // 1. Write a function to upgrade the desired storage key(s). The function should have the protocol: + // + // _upgrade_x(key, network, cb), where: + // + // `x` is the name of the storage key + // `key` is the name of the storage key being upgraded + // `network` is one of 'livenet', 'testnet' + // + // 2. Add the storage key to `_upgraders` object using the name of the key as the `_upgrader` object key + // with the value being the name of the upgrade function (e.g., _upgrade_x). In order to avoid conflicts + // when a storage key is involved in multiple upgraders as well as predicte the order in which upgrades + // occur the `_upgrader` object key should be prefixed with '##_' (e.g., '01_') to create a unique and + // sortable name. This format is interpreted by the _upgrade() function. + // + // Upgraders are executed in numerical order per the '##_' object key prefix. + // + var _upgraders = { + '00_bitpayDebitCards' : _upgrade_bitpayDebitCards // 2016-11: Upgrade bitpayDebitCards-x to bitpayAccounts-x + }; + function _upgrade_bitpayDebitCards(key, network, cb) { + key += '-' + network; + storage.get(key, function(err, data) { + if (err) return cb(err); + if (data != null) { + // Needs upgrade + if (lodash.isString(data)) { + data = JSON.parse(data); + } + data = data || {}; + root.setBitpayDebitCards(network, data, function(err) { + if (err) return cb(err); + storage.remove(key, function() { + cb(null, 'replaced with \'bitpayAccounts\''); + }); + }); + } else { + cb(); + } + }); + }; + // + //////////////////////////////////////////////////////////////////////////// + + // IMPORTANT: This function is designed to block execution until it completes. + // Ideally storage should not be used until it has been verified. + root.verify = function(cb) { + _upgrade(function(err) { + cb(err); + }); + }; + + function _handleUpgradeError(key, err) { + $log.error('Failed to upgrade storage for \'' + key + '\': ' + err); + }; + + function _handleUpgradeSuccess(key, msg) { + $log.info('Storage upgraded for \'' + key + '\': ' + msg); + }; + + function _upgrade(cb) { + var errorCount = 0; + var errorMessage = undefined; + var keys = Object.keys(_upgraders).sort(); + var networks = ['livenet', 'testnet']; + keys.forEach(function(key) { + networks.forEach(function(network) { + var storagekey = key.split('_')[1]; + _upgraders[key](storagekey, network, function(err, msg) { + if (err) { + _handleUpgradeError(storagekey, err); + errorCount++; + errorMessage = errorCount + ' storage upgrade failures'; + } + if (msg) _handleUpgradeSuccess(storagekey, msg); + }); + }); + }); + cb(errorMessage); + }; root.tryToMigrate = function(cb) { if (!shouldUseFileStorage) return cb(); @@ -349,20 +432,76 @@ angular.module('copayApp.services') storage.get('bitpayDebitCardsHistory-' + network, cb); }; - root.removeBitpayDebitCardsHistory = function(network, cb) { - storage.remove('bitpayDebitCardsHistory-' + network, cb); + root.removeBitpayDebitCardHistory = function(network, card, cb) { + root.getBitpayDebitCardsHistory(network, function(err, data) { + if (err) return cb(err); + if (lodash.isString(data)) { + data = JSON.parse(data); + } + data = data || {}; + delete data[card.eid]; + root.setBitpayDebitCardsHistory(network, JSON.stringify(data), cb); + }); }; root.setBitpayDebitCards = function(network, data, cb) { - storage.set('bitpayDebitCards-' + network, data, cb); + if (lodash.isString(data)) { + data = JSON.parse(data); + } + data = data || {}; + if (lodash.isEmpty(data) || !data.email) return cb('No card(s) to set'); + storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) { + if (err) return cb(err); + bitpayAccounts = JSON.parse(bitpayAccounts) || {}; + bitpayAccounts[data.email] = bitpayAccounts[data.email] || {}; + bitpayAccounts[data.email]['bitpayDebitCards-' + network] = data; + storage.set('bitpayAccounts-' + network, JSON.stringify(bitpayAccounts), cb); + }); }; root.getBitpayDebitCards = function(network, cb) { - storage.get('bitpayDebitCards-' + network, cb); + storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) { + bitpayAccounts = JSON.parse(bitpayAccounts) || {}; + var cards = []; + Object.keys(bitpayAccounts).forEach(function(email) { + // For the UI, add the account email to the card object. + var acctCards = bitpayAccounts[email]['bitpayDebitCards-' + network].cards; + for (var i = 0; i < acctCards.length; i++) { + acctCards[i].email = email; + } + cards = cards.concat(acctCards); + }); + cb(err, cards); + }); }; - root.removeBitpayDebitCards = function(network, cb) { - storage.remove('bitpayDebitCards-' + network, cb); + root.removeBitpayDebitCard = function(network, card, cb) { + if (lodash.isString(card)) { + card = JSON.parse(card); + } + card = card || {}; + if (lodash.isEmpty(card) || !card.eid) return cb('No card to remove'); + storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) { + if (err) cb(err); + bitpayAccounts = JSON.parse(bitpayAccounts) || {}; + Object.keys(bitpayAccounts).forEach(function(userId) { + var data = bitpayAccounts[userId]['bitpayDebitCards-' + network]; + var newCards = lodash.reject(data.cards, {'eid': card.eid}); + data.cards = newCards; + root.setBitpayDebitCards(network, data, function(err) { + if (err) cb(err); + // If there are no more cards in storage then re-enable the next step entry. + root.getBitpayDebitCards(network, function(err, cards){ + if (err) cb(err); + if (cards.length == 0) { + root.removeNextStep('BitpayCard', cb); + } else { + cb(); + } + }); + }); + }); + }); }; root.setBitpayCardCredentials = function(network, data, cb) { diff --git a/src/sass/views/bitpayCardPreferences.scss b/src/sass/views/bitpayCardPreferences.scss new file mode 100644 index 000000000..670e8752e --- /dev/null +++ b/src/sass/views/bitpayCardPreferences.scss @@ -0,0 +1,15 @@ +#bitpayCardPreferences { + .item { + .item-title { + display: block; + } + .item-subtitle { + display: block; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + color: $light-gray; + font-size: 14px; + } + } +} diff --git a/src/sass/views/views.scss b/src/sass/views/views.scss index 5b3e8290d..5db678130 100644 --- a/src/sass/views/views.scss +++ b/src/sass/views/views.scss @@ -13,6 +13,7 @@ @import "advancedSettings"; @import "bitpayCard"; @import "bitpayCardIntro"; +@import "bitpayCardPreferences"; @import "address-book"; @import "wallet-backup-phrase"; @import "zero-state"; diff --git a/www/views/bitpayCard.html b/www/views/bitpayCard.html index 8f1464bc5..bf36da1fd 100644 --- a/www/views/bitpayCard.html +++ b/www/views/bitpayCard.html @@ -2,7 +2,7 @@ - BitPay Visa® Card + BitPay Visa® Card ({{card.lastFourDigits}})