refactor preferences
This commit is contained in:
parent
4865ea8ad8
commit
1e2555dad9
15 changed files with 222 additions and 200 deletions
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('buyCoinbaseController',
|
||||
function($scope, $modal, $log, $timeout, lodash, profileService, coinbaseService, animationService, bwsError, addressService, walletService) {
|
||||
function($scope, $modal, $log, $timeout, lodash, profileService, coinbaseService, animationService, bwsError, addressService) {
|
||||
|
||||
window.ignoreMobilePause = true;
|
||||
var self = this;
|
||||
|
|
@ -72,7 +72,7 @@ angular.module('copayApp.controllers').controller('buyCoinbaseController',
|
|||
|
||||
$scope.selectWallet = function(walletId, walletName) {
|
||||
var client = profileService.getClient(walletId);
|
||||
walletService.isReady(client, function(err) {
|
||||
profileService.isReady(client, function(err) {
|
||||
if (err) {
|
||||
self.error = {errors: [{ message: err }]};
|
||||
$modalInstance.dismiss('cancel');
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('buyGlideraController',
|
||||
function($scope, $timeout, $modal, profileService, addressService, glideraService, bwsError, lodash, animationService, walletService) {
|
||||
function($scope, $timeout, $modal, profileService, addressService, glideraService, bwsError, lodash, animationService) {
|
||||
|
||||
var self = this;
|
||||
this.show2faCodeInput = null;
|
||||
|
|
@ -51,7 +51,7 @@ angular.module('copayApp.controllers').controller('buyGlideraController',
|
|||
|
||||
$scope.selectWallet = function(walletId, walletName) {
|
||||
var client = profileService.getClient(walletId);
|
||||
walletService.isReady(client, function(err) {
|
||||
profileService.isReady(client, function(err) {
|
||||
if (err) {
|
||||
self.error = err;
|
||||
$modalInstance.dismiss('cancel');
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, $ionicSideMenuDelegate, $ionicScrollDelegate, $ionicPopup, latestReleaseService, feeService, bwcService, pushNotificationsService, lodash, go, profileService, configService, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, addonManager, bwsError, txFormatService, uxLanguage, glideraService, coinbaseService, platformInfo, addressbookService, walletService) {
|
||||
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, $ionicSideMenuDelegate, $ionicScrollDelegate, $ionicPopup, latestReleaseService, feeService, bwcService, pushNotificationsService, lodash, go, profileService, configService, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, addonManager, bwsError, txFormatService, uxLanguage, glideraService, coinbaseService, platformInfo, addressbookService) {
|
||||
var self = this;
|
||||
var SOFT_CONFIRMATION_LIMIT = 12;
|
||||
var errors = bwcService.getErrors();
|
||||
|
|
@ -184,7 +184,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
}
|
||||
}
|
||||
|
||||
walletService.isBackupNeeded(fc, function(needsBackup) {
|
||||
profileService.needsBackup(fc, function(needsBackup) {
|
||||
self.needsBackup = needsBackup;
|
||||
self.openWallet(function() {
|
||||
if (!self.isComplete) {
|
||||
|
|
@ -268,50 +268,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
};
|
||||
|
||||
|
||||
self._updateRemotePreferencesFor = function(clients, prefs, cb) {
|
||||
var client = clients.shift();
|
||||
|
||||
if (!client)
|
||||
return cb();
|
||||
|
||||
$log.debug('Saving remote preferences', client.credentials.walletName, prefs);
|
||||
client.savePreferences(prefs, function(err) {
|
||||
// we ignore errors here
|
||||
if (err) $log.warn(err);
|
||||
|
||||
self._updateRemotePreferencesFor(clients, prefs, cb);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
// TODO: should not be in index.js
|
||||
self.updateRemotePreferences = function(opts, cb) {
|
||||
var clients;
|
||||
var prefs = opts.preferences || {};
|
||||
|
||||
// Update this JIC.
|
||||
var config = configService.getSync().wallet.settings;
|
||||
|
||||
//prefs.email (may come from arguments)
|
||||
prefs.language = self.defaultLanguageIsoCode;
|
||||
prefs.unit = config.unitCode;
|
||||
|
||||
|
||||
if (opts.client) {
|
||||
clients = [opts.client];
|
||||
} else {
|
||||
clients = lodash.values(profileService.walletClients);
|
||||
};
|
||||
|
||||
self._updateRemotePreferencesFor(clients, prefs, function(err) {
|
||||
if (err) return cb(err);
|
||||
|
||||
lodash.each(clients, function(c) {
|
||||
c.preferences = lodash.assign(prefs, c.preferences);
|
||||
});
|
||||
return cb();
|
||||
});
|
||||
};
|
||||
|
||||
var _walletStatusHash = function(walletStatus) {
|
||||
var bal;
|
||||
|
|
@ -323,6 +279,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
return bal;
|
||||
};
|
||||
|
||||
// TODO move this to wallet service
|
||||
self.updateAll = function(opts, initStatusHash, tries) {
|
||||
$scope.$broadcast('scroll.refreshComplete');
|
||||
tries = tries || 0;
|
||||
|
|
@ -1091,14 +1048,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
});
|
||||
};
|
||||
|
||||
self.setUxLanguage = function(cb) {
|
||||
uxLanguage.update(function(lang) {
|
||||
var userLang = lang;
|
||||
self.defaultLanguageIsoCode = userLang;
|
||||
self.defaultLanguageName = uxLanguage.getName(userLang);
|
||||
if (cb) return cb();
|
||||
});
|
||||
};
|
||||
|
||||
self.initGlidera = function(accessToken) {
|
||||
self.glideraEnabled = configService.getSync().glidera.enabled;
|
||||
|
|
@ -1509,24 +1458,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
self.updateAll();
|
||||
});
|
||||
|
||||
$rootScope.$on('Local/ProfileBound', function() {
|
||||
storageService.getRemotePrefsStoredFlag(function(err, val) {
|
||||
if (err || val) return;
|
||||
self.updateRemotePreferences({}, function() {
|
||||
$log.debug('Remote preferences saved');
|
||||
storageService.setRemotePrefsStoredFlag(function() {});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
$rootScope.$on('Local/LanguageSettingUpdated', function() {
|
||||
self.setUxLanguage(function() {
|
||||
self.updateRemotePreferences({}, function() {
|
||||
$log.debug('Remote preferences saved')
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
$rootScope.$on('Local/GlideraUpdated', function(event, accessToken) {
|
||||
self.initGlidera(accessToken);
|
||||
});
|
||||
|
|
@ -1553,18 +1484,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
self.updateAll({
|
||||
triggerTxUpdate: true,
|
||||
});
|
||||
self.updateRemotePreferences({}, function() {
|
||||
$log.debug('Remote preferences saved')
|
||||
});
|
||||
});
|
||||
|
||||
$rootScope.$on('Local/EmailSettingUpdated', function(event, client, email, cb) {
|
||||
self.updateRemotePreferences({
|
||||
client: client,
|
||||
preferences: {
|
||||
email: email || null
|
||||
},
|
||||
}, cb);
|
||||
});
|
||||
|
||||
$rootScope.$on('Local/WalletCompleted', function(event, walletId) {
|
||||
|
|
@ -1576,17 +1495,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
}
|
||||
});
|
||||
|
||||
|
||||
lodash.each(['Local/ProfileCreated', 'Local/WalletListUpdated'], function(eventName) {
|
||||
$rootScope.$on(eventName, function(event, client) {
|
||||
self.updateRemotePreferences({
|
||||
client: client
|
||||
}, function() {
|
||||
$log.debug('Remote preferences saved');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
self.debouncedUpdate = lodash.throttle(function() {
|
||||
self.updateAll({
|
||||
quiet: true
|
||||
|
|
@ -1740,14 +1648,13 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
});
|
||||
|
||||
$rootScope.$on('Local/NoWallets', function(event) {
|
||||
$log.debug('Event: NoWallets');
|
||||
|
||||
$timeout(function() {
|
||||
self.hasProfile = true;
|
||||
self.noFocusedWallet = true;
|
||||
self.isComplete = null;
|
||||
self.walletName = null;
|
||||
self.setUxLanguage();
|
||||
uxLanguage.update();
|
||||
|
||||
profileService.isDisclaimerAccepted(function(v) {
|
||||
if (v) {
|
||||
go.path('import');
|
||||
|
|
@ -1757,7 +1664,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
});
|
||||
|
||||
$rootScope.$on('Local/NewFocusedWallet', function() {
|
||||
self.setUxLanguage();
|
||||
uxLanguage.update();
|
||||
self.setFocusedWallet();
|
||||
self.updateHistory();
|
||||
storageService.getCleanAndScanAddresses(function(err, walletId) {
|
||||
|
|
@ -1866,6 +1773,10 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
|
||||
});
|
||||
|
||||
$rootScope.$on('Local/EmailUpdated', function(event, email) {
|
||||
self.preferences.email = email
|
||||
});
|
||||
|
||||
lodash.each(['NewCopayer', 'CopayerUpdated'], function(eventName) {
|
||||
$rootScope.$on(eventName, function() {
|
||||
// Re try to open wallet (will triggers)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('addressbookController', function($rootScope, $scope, $timeout, profileService, walletService, addressService, addressbookService) {
|
||||
angular.module('copayApp.controllers').controller('addressbookController', function($rootScope, $scope, $timeout, profileService, addressService, addressbookService) {
|
||||
var self = $scope.self;
|
||||
|
||||
var fc = profileService.focusedClient;
|
||||
|
|
@ -118,7 +118,7 @@ angular.module('copayApp.controllers').controller('addressbookController', funct
|
|||
var client = profileService.getClient(walletId);
|
||||
$scope.errorSelectedWallet = {};
|
||||
|
||||
walletService.isReady(client, function(err) {
|
||||
profileService.isReady(client, function(err) {
|
||||
if (err) $scope.errorSelectedWallet[walletId] = err;
|
||||
else {
|
||||
$scope.gettingAddress = true;
|
||||
|
|
|
|||
|
|
@ -3,15 +3,20 @@
|
|||
angular.module('copayApp.controllers').controller('preferencesController',
|
||||
function($scope, $rootScope, $timeout, $log, configService, profileService, fingerprintService, walletService) {
|
||||
|
||||
var fc = profileService.focusedClient;
|
||||
var self = this;
|
||||
var fc;
|
||||
var config = configService.getSync();
|
||||
$scope.deleted = false;
|
||||
|
||||
if (fc.credentials && !fc.credentials.mnemonicEncrypted && !fc.credentials.mnemonic) {
|
||||
$scope.deleted = true;
|
||||
}
|
||||
var disableFocusListener = $rootScope.$on('Local/NewFocusedWalletReady', function() {
|
||||
self.init();
|
||||
});
|
||||
|
||||
$scope.$on('$destroy', function() {
|
||||
disableFocusListener();
|
||||
});
|
||||
|
||||
this.init = function() {
|
||||
fc = profileService.focusedClient;
|
||||
if (fc) {
|
||||
$scope.encryptEnabled = walletService.isEncrypted(fc);
|
||||
this.externalSource = fc.getPrivKeyExternalSourceName() == 'ledger' ? "Ledger" : null;
|
||||
|
|
@ -21,38 +26,69 @@ angular.module('copayApp.controllers').controller('preferencesController',
|
|||
|
||||
this.touchidAvailable = fingerprintService.isAvailable();
|
||||
$scope.touchidEnabled = config.touchIdFor ? config.touchIdFor[fc.credentials.walletId] : null;
|
||||
|
||||
$scope.deleted = false;
|
||||
if (fc.credentials && !fc.credentials.mnemonicEncrypted && !fc.credentials.mnemonic) {
|
||||
$scope.deleted = true;
|
||||
}
|
||||
};
|
||||
|
||||
var handleEncryptedWallet = function(client, cb) {
|
||||
var handleEncryptedWallet = function(cb) {
|
||||
$rootScope.$emit('Local/NeedsPassword', false, function(err, password) {
|
||||
if (err) return cb(err);
|
||||
return cb(walletService.unlock(client, password));
|
||||
return cb(walletService.unlock(fc, password));
|
||||
});
|
||||
};
|
||||
|
||||
$scope.encryptChange = function() {
|
||||
var self = this;
|
||||
if (!fc) return;
|
||||
var val = $scope.encryptEnabled;
|
||||
|
||||
var setPrivateKeyEncryption = function(password, cb) {
|
||||
$log.debug('Encrypting private key for', fc.credentials.walletName);
|
||||
|
||||
fc.setPrivateKeyEncryption(password);
|
||||
fc.lock();
|
||||
profileService.updateCredentials(fc.export(), function() {
|
||||
$log.debug('Wallet encrypted');
|
||||
return cb();
|
||||
});
|
||||
};
|
||||
|
||||
var disablePrivateKeyEncryption = function(cb) {
|
||||
$log.debug('Disabling private key encryption for', fc.credentials.walletName);
|
||||
|
||||
try {
|
||||
fc.disablePrivateKeyEncryption();
|
||||
} catch (e) {
|
||||
return cb(e);
|
||||
}
|
||||
profileService.updateCredentials(fc.export(), function() {
|
||||
$log.debug('Wallet encryption disabled');
|
||||
return cb();
|
||||
});
|
||||
};
|
||||
|
||||
if (val && !walletService.isEncrypted(fc)) {
|
||||
$rootScope.$emit('Local/NeedsPassword', true, function(err, password) {
|
||||
if (err || !password) {
|
||||
$scope.encryptEnabled = false;
|
||||
return;
|
||||
}
|
||||
profileService.setPrivateKeyEncryptionFC(password, function() {
|
||||
setPrivateKeyEncryption(password, function() {
|
||||
$rootScope.$emit('Local/NewEncryptionSetting');
|
||||
$scope.encryptEnabled = true;
|
||||
});
|
||||
});
|
||||
} else {
|
||||
if (!val && walletService.isEncrypted(fc)) {
|
||||
handleEncryptedWallet(fc, function(err) {
|
||||
handleEncryptedWallet(function(err) {
|
||||
if (err) {
|
||||
$scope.encryptEnabled = true;
|
||||
return;
|
||||
}
|
||||
profileService.disablePrivateKeyEncryptionFC(function(err) {
|
||||
disablePrivateKeyEncryption(function(err) {
|
||||
$rootScope.$emit('Local/NewEncryptionSetting');
|
||||
if (err) {
|
||||
$scope.encryptEnabled = true;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('preferencesAltCurrencyController',
|
||||
function($scope, $timeout, $log, configService, rateService, lodash, go) {
|
||||
function($scope, $timeout, $log, configService, rateService, lodash, go, profileService, walletService) {
|
||||
this.hideAdv = true;
|
||||
this.hidePriv = true;
|
||||
this.hideSecret = true;
|
||||
|
|
@ -51,6 +51,9 @@ angular.module('copayApp.controllers').controller('preferencesAltCurrencyControl
|
|||
if (err) $log.warn(err);
|
||||
go.preferencesGlobal();
|
||||
$scope.$emit('Local/UnitSettingUpdated');
|
||||
walletService.updateRemotePreferences(profileService.walletClients, {}, function() {
|
||||
$log.debug('Remote preferences saved')
|
||||
});
|
||||
$timeout(function() {
|
||||
$scope.$apply();
|
||||
}, 100);
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ angular.module('copayApp.controllers').controller('preferencesDeleteWordsControl
|
|||
confirmDialog.show(msg, function(ok) {
|
||||
if (ok) {
|
||||
fc.clearMnemonic();
|
||||
profileService.updateCredentialsFC(function() {
|
||||
profileService.updateCredentials(fc.export(), function() {
|
||||
notification.success(successMsg);
|
||||
go.walletHome();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,14 +1,22 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('preferencesEmailController',
|
||||
function($scope, go, profileService, gettext, $log) {
|
||||
function($rootScope, go, profileService, gettext, $log, walletService) {
|
||||
this.save = function(form) {
|
||||
var self = this;
|
||||
this.error = null;
|
||||
|
||||
var fc = profileService.focusedClient;
|
||||
this.saving = true;
|
||||
$scope.$emit('Local/EmailSettingUpdated', fc,self.email, function() {
|
||||
var email = self.email || '';
|
||||
|
||||
walletService.updateRemotePreferences(fc, {
|
||||
email: email,
|
||||
}, function(err) {
|
||||
|
||||
if (!err)
|
||||
$rootScope.$emit('Local/EmailUpdated', email);
|
||||
|
||||
self.saving = false;
|
||||
go.path('preferences');
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('preferencesLanguageController',
|
||||
function($scope, $log, $timeout, configService, uxLanguage, go) {
|
||||
function($scope, $log, $timeout, configService, profileService, uxLanguage, walletService, go) {
|
||||
|
||||
this.availableLanguages = uxLanguage.getLanguages();
|
||||
this.currentLanguage = uxLanguage.getCurrentLanguage();
|
||||
|
|
@ -19,10 +19,18 @@ angular.module('copayApp.controllers').controller('preferencesLanguageController
|
|||
configService.set(opts, function(err) {
|
||||
if (err) $log.warn(err);
|
||||
go.preferencesGlobal();
|
||||
$scope.$emit('Local/LanguageSettingUpdated');
|
||||
$timeout(function() {
|
||||
$scope.$apply();
|
||||
}, 100);
|
||||
|
||||
|
||||
uxLanguage.update(function() {
|
||||
$timeout(function() {
|
||||
$scope.$apply();
|
||||
}, 100);
|
||||
|
||||
walletService.updateRemotePreferences(profileService.getClients(), {},
|
||||
function() {
|
||||
$log.debug('Remote preferences saved')
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('preferencesUnitController',
|
||||
function($scope, $timeout, $log, configService, go) {
|
||||
function($scope, $timeout, $log, configService, go, walletService, profileService) {
|
||||
var config = configService.getSync();
|
||||
this.unitName = config.wallet.settings.unitName;
|
||||
this.unitOpts = [
|
||||
|
|
@ -54,6 +54,9 @@ angular.module('copayApp.controllers').controller('preferencesUnitController',
|
|||
if (err) $log.warn(err);
|
||||
go.preferencesGlobal();
|
||||
$scope.$emit('Local/UnitSettingUpdated');
|
||||
walletService.updateRemotePreferences(profileService.getClients(), {}, function() {
|
||||
$log.debug('Remote preferences saved')
|
||||
});
|
||||
$timeout(function() {
|
||||
$scope.$apply();
|
||||
}, 100);
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ Profile.fromObj = function(obj) {
|
|||
x.credentials = obj.credentials;
|
||||
x.disclaimerAccepted = obj.disclaimerAccepted;
|
||||
x.checked = obj.checked || {};
|
||||
x.checkedUA = null;
|
||||
x.checkedUA = obj.checkedUA || {};
|
||||
|
||||
if (x.credentials[0] && typeof x.credentials[0] != 'object')
|
||||
throw ("credentials should be an object");
|
||||
|
|
@ -40,6 +40,7 @@ Profile.fromString = function(str) {
|
|||
};
|
||||
|
||||
Profile.prototype.toObj = function() {
|
||||
delete this.dirty;
|
||||
return JSON.stringify(this);
|
||||
};
|
||||
|
||||
|
|
@ -53,7 +54,7 @@ Profile.prototype.hasWallet = function(walletId) {
|
|||
};
|
||||
|
||||
Profile.prototype.isChecked = function(ua, walletId) {
|
||||
return this.checkedUA == ua && this.checked[walletId];
|
||||
return !!(this.checkedUA == ua && this.checked[walletId]);
|
||||
};
|
||||
|
||||
Profile.prototype.setChecked = function(ua, walletId) {
|
||||
|
|
@ -62,6 +63,7 @@ Profile.prototype.setChecked = function(ua, walletId) {
|
|||
this.checked = {};
|
||||
}
|
||||
this.checked[walletId] = true;
|
||||
this.dirty = true;
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -70,6 +72,20 @@ Profile.prototype.addWallet = function(credentials) {
|
|||
return false;
|
||||
|
||||
this.credentials.push(credentials);
|
||||
this.dirty = true;
|
||||
return true;
|
||||
};
|
||||
|
||||
Profile.prototype.updateWallet = function(credentials) {
|
||||
if (!this.hasWallet(credentials.walletId))
|
||||
return false;
|
||||
|
||||
this.credentials = this.credentials.filter(function(c) {
|
||||
return c.walletId != walletId;
|
||||
});
|
||||
|
||||
this.addWallet(credentials);
|
||||
this.dirty = true;
|
||||
return true;
|
||||
};
|
||||
|
||||
|
|
@ -81,5 +97,6 @@ Profile.prototype.deleteWallet = function(walletId) {
|
|||
return c.walletId != walletId;
|
||||
});
|
||||
|
||||
this.dirty = true;
|
||||
return true;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -632,6 +632,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
|
|||
throw new Error(err); // TODO
|
||||
}
|
||||
} else {
|
||||
profileService.storeProfileIfDirty();
|
||||
$log.debug('Profile loaded ... Starting UX.');
|
||||
$state.transitionTo(toState.name || toState, toParams);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
'use strict';
|
||||
angular.module('copayApp.services')
|
||||
.factory('profileService', function profileServiceFactory($rootScope, $timeout, $filter, $log, sjcl, lodash, storageService, bwcService, configService, notificationService, pushNotificationsService, gettext, gettextCatalog, bwsError, uxLanguage, bitcore, platformInfo) {
|
||||
.factory('profileService', function profileServiceFactory($rootScope, $timeout, $filter, $log, sjcl, lodash, storageService, bwcService, configService, notificationService, pushNotificationsService, gettext, gettextCatalog, bwsError, uxLanguage, bitcore, platformInfo, walletService) {
|
||||
|
||||
|
||||
var isChromeApp = platformInfo.isChromeApp;
|
||||
|
|
@ -67,8 +67,7 @@ angular.module('copayApp.services')
|
|||
var opts = opts || {};
|
||||
var walletId = client.credentials.walletId;
|
||||
|
||||
if ((root.walletClients[walletId] && root.walletClients[walletId].started)
|
||||
|| opts.force) {
|
||||
if ((root.walletClients[walletId] && root.walletClients[walletId].started) || opts.force) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -81,7 +80,6 @@ angular.module('copayApp.services')
|
|||
storageService.clearLastAddress(walletId, function() {});
|
||||
}
|
||||
|
||||
|
||||
client.removeAllListeners();
|
||||
client.on('report', function(n) {
|
||||
$log.info('BWC Report:' + n);
|
||||
|
|
@ -102,7 +100,7 @@ angular.module('copayApp.services')
|
|||
client.on('walletCompleted', function() {
|
||||
$log.debug('Wallet completed');
|
||||
|
||||
root.updateCredentialsFC(function() {
|
||||
root.updateCredentials(client.export(), function() {
|
||||
$rootScope.$emit('Local/WalletCompleted', walletId);
|
||||
});
|
||||
});
|
||||
|
|
@ -165,7 +163,6 @@ angular.module('copayApp.services')
|
|||
storageService.getFocusedWalletId(function(err, focusedWalletId) {
|
||||
if (err) return cb(err);
|
||||
root._setFocus(focusedWalletId, function() {
|
||||
$rootScope.$emit('Local/ProfileBound');
|
||||
if (usePushNotifications)
|
||||
root.pushNotificationsInit();
|
||||
root.isDisclaimerAccepted(function(val) {
|
||||
|
|
@ -433,6 +430,7 @@ angular.module('copayApp.services')
|
|||
});
|
||||
}
|
||||
|
||||
// Adds and bind a new client to the profile
|
||||
root.addAndBindWalletClient = function(client, opts, cb) {
|
||||
var walletId = client.credentials.walletId
|
||||
|
||||
|
|
@ -443,12 +441,14 @@ angular.module('copayApp.services')
|
|||
$rootScope.$emit('Local/WalletListUpdated', client);
|
||||
|
||||
var saveBwsUrl = function(cb) {
|
||||
if (!opts.bwsurl) return cb();
|
||||
var config = configService.getSync();
|
||||
var defaults = configService.getDefaults();
|
||||
var bwsFor = {};
|
||||
bwsFor[walletId] = opts.bwsurl || defaults.bws.url;
|
||||
|
||||
// Dont save the default
|
||||
if (bwsFor[walletId] == defaults.bws.url)
|
||||
return cb();
|
||||
|
||||
configService.set({
|
||||
bwsFor: bwsFor,
|
||||
}, function(err) {
|
||||
|
|
@ -467,11 +467,16 @@ angular.module('copayApp.services')
|
|||
storageService.setCleanAndScanAddresses(walletId, cb);
|
||||
};
|
||||
|
||||
walletService.updateRemotePreferences(client, {}, function() {
|
||||
$log.debug('Remote preferences saved for:' + walletId)
|
||||
});
|
||||
|
||||
saveBwsUrl(function() {
|
||||
handleImportedClient(function() {
|
||||
root.setAndStoreFocus(walletId, function() {
|
||||
storageService.storeProfile(root.profile, function(err) {
|
||||
|
||||
var config = configService.getSync();
|
||||
if (config.pushNotifications.enabled)
|
||||
pushNotificationsService.enableNotifications(root.walletClients);
|
||||
return cb(err, walletId);
|
||||
|
|
@ -482,6 +487,17 @@ angular.module('copayApp.services')
|
|||
});
|
||||
};
|
||||
|
||||
root.storeProfileIfDirty = function(cb) {
|
||||
if (root.profile.dirty) {
|
||||
storageService.storeProfile(root.profile, function(err) {
|
||||
$log.debug('Saved modified Profile');
|
||||
if (cb) return cb(err);
|
||||
});
|
||||
} else {
|
||||
if (cb) return cb();
|
||||
};
|
||||
};
|
||||
|
||||
root.importWallet = function(str, opts, cb) {
|
||||
|
||||
var walletClient = bwcService.getClient(null, opts);
|
||||
|
|
@ -642,44 +658,34 @@ angular.module('copayApp.services')
|
|||
});
|
||||
};
|
||||
|
||||
// TODO
|
||||
root.updateCredentialsFC = function(cb) {
|
||||
var fc = root.focusedClient;
|
||||
|
||||
var newCredentials = lodash.reject(root.profile.credentials, {
|
||||
walletId: fc.credentials.walletId
|
||||
});
|
||||
newCredentials.push(JSON.parse(fc.export()));
|
||||
root.profile.credentials = newCredentials;
|
||||
|
||||
root.updateCredentials = function(credentials, cb) {
|
||||
root.profile.updateWallet(credentials);
|
||||
storageService.storeProfileThrottled(root.profile, cb);
|
||||
};
|
||||
|
||||
root.getClients = function() {
|
||||
return lodash.values(root.walletClients);
|
||||
};
|
||||
|
||||
root.setPrivateKeyEncryptionFC = function(password, cb) {
|
||||
var fc = root.focusedClient;
|
||||
$log.debug('Encrypting private key for', fc.credentials.walletName);
|
||||
root.needsBackup = function(client, cb) {
|
||||
|
||||
fc.setPrivateKeyEncryption(password);
|
||||
fc.lock();
|
||||
root.updateCredentialsFC(function() {
|
||||
$log.debug('Wallet encrypted');
|
||||
return cb();
|
||||
if (!walletService.needsBackup(client))
|
||||
return cb(false);
|
||||
|
||||
storageService.getBackupFlag(client.credentials.walletId, function(err, val) {
|
||||
if (err) $log.error(err);
|
||||
if (val) return cb(false);
|
||||
return cb(true);
|
||||
});
|
||||
};
|
||||
|
||||
root.isReady = function(client, cb) {
|
||||
if (!client.isComplete())
|
||||
return cb('WALLET_NOT_COMPLETE');
|
||||
|
||||
root.disablePrivateKeyEncryptionFC = function(cb) {
|
||||
var fc = root.focusedClient;
|
||||
$log.debug('Disabling private key encryption for', fc.credentials.walletName);
|
||||
|
||||
try {
|
||||
fc.disablePrivateKeyEncryption();
|
||||
} catch (e) {
|
||||
return cb(e);
|
||||
}
|
||||
root.updateCredentialsFC(function() {
|
||||
$log.debug('Wallet encryption disabled');
|
||||
root.needsBackup(client, function(needsBackup) {
|
||||
if (needsBackup)
|
||||
return cb('WALLET_NEEDS_BACKUP');
|
||||
return cb();
|
||||
});
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ angular.module('copayApp.services')
|
|||
.factory('uxLanguage', function languageService($log, lodash, gettextCatalog, amMoment, configService) {
|
||||
var root = {};
|
||||
|
||||
root.currentLanguage = null;
|
||||
|
||||
root.availableLanguages = [{
|
||||
name: 'English',
|
||||
isoCode: 'en',
|
||||
|
|
@ -33,7 +35,6 @@ angular.module('copayApp.services')
|
|||
isoCode: 'ru',
|
||||
}];
|
||||
|
||||
root.currentLanguage = null;
|
||||
|
||||
root._detect = function(cb) {
|
||||
|
||||
|
|
@ -99,20 +100,20 @@ angular.module('copayApp.services')
|
|||
var userLang = configService.getSync().wallet.settings.defaultLanguage;
|
||||
|
||||
if (!userLang) {
|
||||
|
||||
root._detect(function(lang) {
|
||||
userLang = lang;
|
||||
|
||||
if (userLang != root.currentLanguage) {
|
||||
root._set(lang);
|
||||
}
|
||||
return cb(userLang);
|
||||
if (cb) return cb(userLang);
|
||||
});
|
||||
} else {
|
||||
if (userLang != root.currentLanguage) {
|
||||
root._set(userLang);
|
||||
}
|
||||
return cb(userLang);
|
||||
|
||||
if (cb) return cb(userLang);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.services').factory('walletService', function($log, lodash, trezor, ledger, storageService) {
|
||||
// DO NOT INCLUDE STORAGE HERE \/ \/
|
||||
angular.module('copayApp.services').factory('walletService', function($log, lodash, trezor, ledger, storageService, configService, uxLanguage) {
|
||||
// DO NOT INCLUDE STORAGE HERE ^^
|
||||
|
||||
var root = {};
|
||||
|
||||
var _signWithLedger = function(client, txp, cb) {
|
||||
|
|
@ -31,27 +34,14 @@ angular.module('copayApp.services').factory('walletService', function($log, loda
|
|||
});
|
||||
};
|
||||
|
||||
root.isBackupNeeded = function(client, cb) {
|
||||
if (client.isPrivKeyExternal()) return cb(false);
|
||||
if (!client.credentials.mnemonic) return cb(false);
|
||||
if (client.credentials.network == 'testnet') return cb(false);
|
||||
root.needsBackup = function(client) {
|
||||
if (client.isPrivKeyExternal()) return false;
|
||||
if (!client.credentials.mnemonic) return false;
|
||||
if (client.credentials.network == 'testnet') return false;
|
||||
|
||||
storageService.getBackupFlag(client.credentials.walletId, function(err, val) {
|
||||
if (err) $log.error(err);
|
||||
if (val) return cb(false);
|
||||
return cb(true);
|
||||
});
|
||||
return true;
|
||||
};
|
||||
|
||||
root.isReady = function(client, cb) {
|
||||
if(!client.isComplete())
|
||||
return cb('WALLET_NOT_COMPLETE');
|
||||
root.isBackupNeeded(client, function(needsBackup) {
|
||||
if (needsBackup)
|
||||
return cb('WALLET_NEEDS_BACKUP');
|
||||
return cb();
|
||||
});
|
||||
};
|
||||
|
||||
root.isEncrypted = function(client) {
|
||||
if (lodash.isEmpty(client)) return;
|
||||
|
|
@ -82,7 +72,7 @@ angular.module('copayApp.services').factory('walletService', function($log, loda
|
|||
};
|
||||
|
||||
root.createTx = function(client, txp, cb) {
|
||||
if (lodash.isEmpty(txp) || lodash.isEmpty(client))
|
||||
if (lodash.isEmpty(txp) || lodash.isEmpty(client))
|
||||
return cb('MISSING_PARAMETER');
|
||||
|
||||
if (txp.sendMax) {
|
||||
|
|
@ -97,7 +87,7 @@ angular.module('copayApp.services').factory('walletService', function($log, loda
|
|||
var feeLevelValue = lodash.find(levels, {
|
||||
level: txp.feeLevel
|
||||
});
|
||||
|
||||
|
||||
if (!feeLevelValue || !feeLevelValue.feePerKB)
|
||||
return cb({
|
||||
message: 'Could not get dynamic fee for level: ' + feeLevel
|
||||
|
|
@ -118,10 +108,12 @@ angular.module('copayApp.services').factory('walletService', function($log, loda
|
|||
};
|
||||
|
||||
root.publishTx = function(client, txp, cb) {
|
||||
if (lodash.isEmpty(txp) || lodash.isEmpty(client))
|
||||
if (lodash.isEmpty(txp) || lodash.isEmpty(client))
|
||||
return cb('MISSING_PARAMETER');
|
||||
|
||||
client.publishTxProposal({txp: txp}, function(err, publishedTx) {
|
||||
client.publishTxProposal({
|
||||
txp: txp
|
||||
}, function(err, publishedTx) {
|
||||
if (err) return cb(err);
|
||||
else {
|
||||
$log.debug('Transaction published');
|
||||
|
|
@ -131,9 +123,9 @@ angular.module('copayApp.services').factory('walletService', function($log, loda
|
|||
};
|
||||
|
||||
root.signTx = function(client, txp, cb) {
|
||||
if (lodash.isEmpty(txp) || lodash.isEmpty(client))
|
||||
if (lodash.isEmpty(txp) || lodash.isEmpty(client))
|
||||
return cb('MISSING_PARAMETER');
|
||||
|
||||
|
||||
if (client.isPrivKeyExternal()) {
|
||||
switch (client.getPrivKeyExternalSourceName()) {
|
||||
case 'ledger':
|
||||
|
|
@ -160,14 +152,14 @@ angular.module('copayApp.services').factory('walletService', function($log, loda
|
|||
};
|
||||
|
||||
root.broadcastTx = function(client, txp, cb) {
|
||||
if (lodash.isEmpty(txp) || lodash.isEmpty(client))
|
||||
if (lodash.isEmpty(txp) || lodash.isEmpty(client))
|
||||
return cb('MISSING_PARAMETER');
|
||||
|
||||
|
||||
if (txp.status != 'accepted')
|
||||
return cb('TX_NOT_ACCEPTED');
|
||||
|
||||
client.broadcastTxProposal(txp, function(err, broadcastedTxp, memo) {
|
||||
if (err)
|
||||
if (err)
|
||||
return cb(err);
|
||||
|
||||
$log.debug('Transaction broadcasted');
|
||||
|
|
@ -178,9 +170,9 @@ angular.module('copayApp.services').factory('walletService', function($log, loda
|
|||
};
|
||||
|
||||
root.rejectTx = function(client, txp, cb) {
|
||||
if (lodash.isEmpty(txp) || lodash.isEmpty(client))
|
||||
if (lodash.isEmpty(txp) || lodash.isEmpty(client))
|
||||
return cb('MISSING_PARAMETER');
|
||||
|
||||
|
||||
client.rejectTxProposal(txp, null, function(err, rejectedTxp) {
|
||||
$log.debug('Transaction rejected');
|
||||
return cb(err, rejectedTxp);
|
||||
|
|
@ -188,14 +180,50 @@ angular.module('copayApp.services').factory('walletService', function($log, loda
|
|||
};
|
||||
|
||||
root.removeTx = function(client, txp, cb) {
|
||||
if (lodash.isEmpty(txp) || lodash.isEmpty(client))
|
||||
if (lodash.isEmpty(txp) || lodash.isEmpty(client))
|
||||
return cb('MISSING_PARAMETER');
|
||||
|
||||
|
||||
client.removeTxProposal(txp, function(err) {
|
||||
$log.debug('Transaction removed');
|
||||
return cb(err);
|
||||
});
|
||||
};
|
||||
|
||||
root.updateRemotePreferences = function(clients, prefs, cb) {
|
||||
prefs = prefs || {};
|
||||
|
||||
if (!lodash.isArray(clients))
|
||||
clients = [clients];
|
||||
|
||||
function updateRemotePreferencesFor(clients, prefs, cb) {
|
||||
var client = clients.shift();
|
||||
if (!client) return cb();
|
||||
$log.debug('Saving remote preferences', client.credentials.walletName, prefs);
|
||||
|
||||
client.savePreferences(prefs, function(err) {
|
||||
// we ignore errors here
|
||||
if (err) $log.warn(err);
|
||||
|
||||
updateRemotePreferencesFor(clients, prefs, cb);
|
||||
});
|
||||
};
|
||||
|
||||
// Update this JIC.
|
||||
var config = configService.getSync().wallet.settings;
|
||||
|
||||
//prefs.email (may come from arguments)
|
||||
prefs.language = uxLanguage.getCurrentLanguage();;
|
||||
prefs.unit = config.unitCode;
|
||||
|
||||
updateRemotePreferencesFor(clients, prefs, function(err) {
|
||||
if (err) return cb(err);
|
||||
|
||||
lodash.each(clients, function(c) {
|
||||
c.preferences = lodash.assign(prefs, c.preferences);
|
||||
});
|
||||
return cb();
|
||||
});
|
||||
};
|
||||
|
||||
return root;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue