diff --git a/src/js/controllers/export.js b/src/js/controllers/export.js index 185ee2505..f42ca34c7 100644 --- a/src/js/controllers/export.js +++ b/src/js/controllers/export.js @@ -1,44 +1,106 @@ 'use strict'; angular.module('copayApp.controllers').controller('backupController', - function($rootScope, $scope, $timeout, backupService, profileService, isMobile, notification, go, gettext, gettextCatalog) { - this.error = null; - this.success = null; + function($rootScope, $scope, $timeout, $log, backupService, storageService, profileService, isMobile, notification, go, gettext, gettextCatalog) { + var self = this; + + self.error = null; + self.success = null; var fc = profileService.focusedClient; - this.isEncrypted = fc.isPrivKeyEncrypted(); + self.isEncrypted = fc.isPrivKeyEncrypted(); - this.downloadWalletBackup = function() { - var self = this; - var opts = { - noSign: $scope.noSign, - }; - backupService.walletDownload(this.password, opts, function(err) { + self.downloadWalletBackup = function() { + self.getMetaData(function(err, txsFromLocal, localAddressBook) { if (err) { self.error = true; - return ; + return; } - $rootScope.$emit('Local/BackupDone'); - notification.success(gettext('Success'), gettext('Encrypted export file saved')); - go.walletHome(); + var opts = { + noSign: $scope.noSign, + historyCache: txsFromLocal, + addressBook: localAddressBook + }; + + backupService.walletDownload(self.password, opts, function(err) { + if (err) { + self.error = true; + return; + } + + $rootScope.$emit('Local/BackupDone'); + notification.success(gettext('Success'), gettext('Encrypted export file saved')); + go.walletHome(); + }); }); }; - this.getBackup = function() { - var opts = { - noSign: $scope.noSign, - }; + self.getMetaData = function() { + self.getHistoryCache(function(err, txsFromLocal) { + if (err) { + return cb(err); + } + self.getAddressbook(function(err, localAddressBook) { + if (err) { + return cb(err); + } + return cb(null, txsFromLocal, localAddressBook) + }); + }); + } - var ew = backupService.walletExport(this.password, opts); - if (!ew) { - this.error = true; - } else { - this.error = false; - } - return ew; - }; + self.getHistoryCache = function(cb) { + storageService.getTxHistory(fc.credentials.walletId, function(err, txs) { + if (err) return cb(err); - this.viewWalletBackup = function() { + var localTxs = []; + + try { + localTxs = JSON.parse(txs); + } catch (ex) { + $log.warn(ex); + } + return cb(null, localTxs); + }); + } + + self.getAddressbook = function(cb) { + storageService.getAddressbook(fc.credentials.network, function(err, addressBook) { + if (err) return cb(err); + + var localAddressBook = []; + try { + localAddressBook = JSON.parse(addressBook); + } catch (ex) { + $log.warn(ex); + } + return cb(null, localAddressBook); + }); + } + + self.getBackup = function() { + self.getMetaData(function(err, txsFromLocal, localAddressBook) { + if (err) { + self.error = true; + return; + } + var opts = { + noSign: $scope.noSign, + historyCache: txsFromLocal, + addressBook: localAddressBook + }; + + var ew = backupService.walletExport(self.password, opts); + if (!ew) { + self.error = true; + } else { + self.error = false; + } + return ew; + }); + } + + self.viewWalletBackup = function() { var self = this; $timeout(function() { var ew = self.getBackup(); @@ -48,15 +110,15 @@ angular.module('copayApp.controllers').controller('backupController', }, 100); }; - this.copyWalletBackup = function() { - var ew = this.getBackup(); + self.copyWalletBackup = function() { + var ew = self.getBackup(); if (!ew) return; window.cordova.plugins.clipboard.copy(ew); window.plugins.toast.showShortCenter(gettextCatalog.getString('Copied to clipboard')); $rootScope.$emit('Local/BackupDone'); }; - this.sendWalletBackup = function() { + self.sendWalletBackup = function() { var fc = profileService.focusedClient; if (isMobile.Android() || isMobile.Windows()) { window.ignoreMobilePause = true; @@ -66,10 +128,10 @@ angular.module('copayApp.controllers').controller('backupController', if (fc.alias) { name = fc.alias + ' [' + name + ']'; } - var ew = this.getBackup(); + var ew = self.getBackup(); if (!ew) return; - if( $scope.noSign) + if ($scope.noSign) name = name + '(No Private Key)'; var properties = { @@ -81,4 +143,4 @@ angular.module('copayApp.controllers').controller('backupController', window.plugin.email.open(properties); }; - }); + }); \ No newline at end of file diff --git a/src/js/services/profileService.js b/src/js/services/profileService.js index e81b3e633..172dcfccc 100644 --- a/src/js/services/profileService.js +++ b/src/js/services/profileService.js @@ -11,7 +11,7 @@ angular.module('copayApp.services') root.focusedClient = null; root.walletClients = {}; - root.Utils = bwcService.getUtils(); + root.Utils = bwcService.getUtils(); root.formatAmount = function(amount) { var config = configService.getSync().wallet.settings; if (config.unitCode == 'sat') return amount; @@ -276,8 +276,8 @@ angular.module('copayApp.services') // check if exist if (lodash.find(root.profile.credentials, { - 'walletId': walletData.walletId - })) { + 'walletId': walletData.walletId + })) { return cb(gettext('Cannot join the same wallet more that once')); } } catch (ex) { @@ -337,32 +337,45 @@ angular.module('copayApp.services') }); }; + root.setMetaData = function(walletClient, cb) { + storageService.setAddressbook(walletClient.credentials.network, walletClient.credentials.historyCache, function(err) { + if (err) return cb(err); + storageService.setTxHistory(walletClient.credentials.historyCache, walletClient.credentials.walletId, function(err) { + if (err) return cb(err); + return cb(null); + }); + }); + } + root._addWalletClient = function(walletClient, opts, cb) { var walletId = walletClient.credentials.walletId; - - // check if exist - var w = lodash.find(root.profile.credentials, { - 'walletId': walletId - }); - if (w) { - return cb(gettext('Wallet already in Copay' + ": ") + w.walletName); - } - - var defaults = configService.getDefaults(); - var bwsFor = {}; - bwsFor[walletId] = opts.bwsurl || defaults.bws.url; - - configService.set({ - bwsFor: bwsFor, - }, function(err) { + root.setMetaData(walletClient, function(err) { if (err) console.log(err); - root.profile.credentials.push(JSON.parse(walletClient.export())); - root.setWalletClients(); + // check if exist + var w = lodash.find(root.profile.credentials, { + 'walletId': walletId + }); + if (w) { + return cb(gettext('Wallet already in Copay' + ": ") + w.walletName); + } - root.setAndStoreFocus(walletId, function() { - storageService.storeProfile(root.profile, function(err) { - return cb(err, walletId); + var defaults = configService.getDefaults(); + var bwsFor = {}; + bwsFor[walletId] = opts.bwsurl || defaults.bws.url; + + configService.set({ + bwsFor: bwsFor, + }, 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) { + return cb(err, walletId); + }); }); }); });