refactor preferences

This commit is contained in:
Matias Alejo Garcia 2016-06-06 18:26:45 -03:00
commit 1e2555dad9
No known key found for this signature in database
GPG key ID: 02470DB551277AB3
15 changed files with 222 additions and 200 deletions

View file

@ -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();
});
};

View file

@ -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);
}
};

View file

@ -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;
});