refactor profileService

This commit is contained in:
Matias Alejo Garcia 2016-06-06 12:21:15 -03:00
commit 4865ea8ad8
No known key found for this signature in database
GPG key ID: 02470DB551277AB3
9 changed files with 241 additions and 204 deletions

View file

@ -54,15 +54,15 @@ angular.module('copayApp.controllers').controller('copayersController',
modalInstance.result.then(function(ok) {
if (ok) {
_deleteWallet();
doDeleteWallet();
}
});
};
var _deleteWallet = function() {
var doDeleteWallet = function() {
var fc = profileService.focusedClient;
var walletName = fc.credentials.walletName;
profileService.deleteWalletFC({}, function(err) {
profileService.deleteWalletClient(fc, function(err) {
if (err) {
self.error = err.message || err;
$timeout(function() {

View file

@ -169,7 +169,7 @@ angular.module('copayApp.controllers').controller('createController',
self.loading = true;
$timeout(function() {
profileService.createWallet(opts, function(err, walletId) {
profileService.createWallet(opts, function(err) {
self.loading = false;
if (err) {
$log.warn(err);

View file

@ -10,8 +10,6 @@ angular.module('copayApp.controllers').controller('disclaimerController',
opts = opts || {};
$log.debug('Creating profile');
profileService.create(opts, function(err) {
console.log('[disclaimer.js.13]', err); //TODO
if (err) {
$log.warn(err);
$scope.error = err;
@ -33,8 +31,6 @@ angular.module('copayApp.controllers').controller('disclaimerController',
$scope.error = "";
self.creatingProfile = false;
console.log('[disclaimer.js.33]'); //TODO
});
};
@ -44,10 +40,7 @@ angular.module('copayApp.controllers').controller('disclaimerController',
profileService.getProfile(function(err, profile) {
if (!profile) {
console.log('[disclaimer.js.43]'); //TODO
create(opts);
console.log('[disclaimer.js.46]'); //TODO
} else {
$log.info('There is already a profile');
self.creatingProfile = false;

View file

@ -284,9 +284,10 @@ angular.module('copayApp.controllers').controller('indexController', function($r
};
// TODO: should not be in index.js
self.updateRemotePreferences = function(opts, cb) {
var clients;
var prefs = opts.preferences || {};
var fc = profileService.focusedClient;
// Update this JIC.
var config = configService.getSync().wallet.settings;
@ -295,24 +296,20 @@ angular.module('copayApp.controllers').controller('indexController', function($r
prefs.language = self.defaultLanguageIsoCode;
prefs.unit = config.unitCode;
var clients = [];
if (opts.saveAll) {
clients = lodash.values(profileService.walletClients);
if (opts.client) {
clients = [opts.client];
} else {
clients = [fc];
clients = lodash.values(profileService.walletClients);
};
self._updateRemotePreferencesFor(clients, prefs, function(err) {
if (err) return cb(err);
if (!fc) return cb();
fc.getPreferences(function(err, preferences) {
if (err) {
return cb(err);
}
self.preferences = preferences;
return cb();
lodash.each(clients, function(c) {
c.preferences = lodash.assign(prefs, c.preferences);
});
return cb();
});
};
@ -1055,7 +1052,9 @@ angular.module('copayApp.controllers').controller('indexController', function($r
return;
}
profileService.setWalletClients();
profileService.addWalletClient(fc, {
force: true
});
self.startScan(self.walletId);
});
};
@ -1513,9 +1512,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
$rootScope.$on('Local/ProfileBound', function() {
storageService.getRemotePrefsStoredFlag(function(err, val) {
if (err || val) return;
self.updateRemotePreferences({
saveAll: true
}, function() {
self.updateRemotePreferences({}, function() {
$log.debug('Remote preferences saved');
storageService.setRemotePrefsStoredFlag(function() {});
});
@ -1524,9 +1521,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
$rootScope.$on('Local/LanguageSettingUpdated', function() {
self.setUxLanguage(function() {
self.updateRemotePreferences({
saveAll: true
}, function() {
self.updateRemotePreferences({}, function() {
$log.debug('Remote preferences saved')
});
});
@ -1558,15 +1553,14 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.updateAll({
triggerTxUpdate: true,
});
self.updateRemotePreferences({
saveAll: true
}, function() {
self.updateRemotePreferences({}, function() {
$log.debug('Remote preferences saved')
});
});
$rootScope.$on('Local/EmailSettingUpdated', function(event, email, cb) {
$rootScope.$on('Local/EmailSettingUpdated', function(event, client, email, cb) {
self.updateRemotePreferences({
client: client,
preferences: {
email: email || null
},
@ -1582,11 +1576,14 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}
});
$rootScope.$on('Local/ProfileCreated', function(event) {
self.updateRemotePreferences({
saveAll: true
}, function() {
$log.debug('Remote preferences saved');
lodash.each(['Local/ProfileCreated', 'Local/WalletListUpdated'], function(eventName) {
$rootScope.$on(eventName, function(event, client) {
self.updateRemotePreferences({
client: client
}, function() {
$log.debug('Remote preferences saved');
});
});
});
@ -1743,6 +1740,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
});
$rootScope.$on('Local/NoWallets', function(event) {
$log.debug('Event: NoWallets');
$timeout(function() {
self.hasProfile = true;

View file

@ -40,19 +40,19 @@ angular.module('copayApp.controllers').controller('preferencesDeleteWalletContro
modalInstance.result.then(function(ok) {
if (ok) {
_deleteWallet();
doDeleteWallet();
}
});
};
var _deleteWallet = function() {
var doDeleteWallet = function() {
$scope.isDeletingWallet = true;
var fc = profileService.focusedClient;
var name = fc.credentials.walletName;
var walletName = (fc.alias || '') + ' [' + name + ']';
var self = this;
profileService.deleteWalletFC({}, function(err) {
profileService.deleteWalletClient(fc, function(err) {
$scope.isDeletingWallet = false;
if (err) {
self.error = err.message || err;
@ -72,7 +72,7 @@ angular.module('copayApp.controllers').controller('preferencesDeleteWalletContro
delete_msg,
function(buttonIndex) {
if (buttonIndex == 1) {
_deleteWallet();
doDeleteWallet();
}
},
confirm_msg, [accept_msg, cancel_msg]

View file

@ -8,7 +8,7 @@ angular.module('copayApp.controllers').controller('preferencesEmailController',
var fc = profileService.focusedClient;
this.saving = true;
$scope.$emit('Local/EmailSettingUpdated', self.email, function() {
$scope.$emit('Local/EmailSettingUpdated', fc,self.email, function() {
self.saving = false;
go.path('preferences');
});

View file

@ -27,6 +27,7 @@ Profile.fromObj = function(obj) {
x.credentials = obj.credentials;
x.disclaimerAccepted = obj.disclaimerAccepted;
x.checked = obj.checked || {};
x.checkedUA = null;
if (x.credentials[0] && typeof x.credentials[0] != 'object')
throw ("credentials should be an object");
@ -41,3 +42,44 @@ Profile.fromString = function(str) {
Profile.prototype.toObj = function() {
return JSON.stringify(this);
};
Profile.prototype.hasWallet = function(walletId) {
for (var i in this.credentials) {
var c = this.credentials[i];
if (c.walletId == walletId) return true;
};
return false;
};
Profile.prototype.isChecked = function(ua, walletId) {
return this.checkedUA == ua && this.checked[walletId];
};
Profile.prototype.setChecked = function(ua, walletId) {
if (this.checkedUA != ua) {
this.checkedUA = ua;
this.checked = {};
}
this.checked[walletId] = true;
};
Profile.prototype.addWallet = function(credentials) {
if (this.hasWallet(credentials.walletId))
return false;
this.credentials.push(credentials);
return true;
};
Profile.prototype.deleteWallet = function(walletId) {
if (!this.hasWallet(walletId))
return false;
this.credentials = this.credentials.filter(function(c) {
return c.walletId != walletId;
});
return true;
};

View file

@ -62,36 +62,26 @@ angular.module('copayApp.services')
});
};
root.setWalletClient = function(credentials) {
if (root.walletClients[credentials.walletId] &&
root.walletClients[credentials.walletId].started) {
return;
// Adds a wallet client to profileService
root.bindWalletClient = function(client, opts) {
var opts = opts || {};
var walletId = client.credentials.walletId;
if ((root.walletClients[walletId] && root.walletClients[walletId].started)
|| opts.force) {
return false;
}
var getBaseURL = function(walletId) {
var config = configService.getSync();
var defaults = configService.getDefaults();
return ((config.bwsFor && config.bwsFor[walletId]) || defaults.bws.url);
};
$log.debug('Importing wallet:' + credentials.walletId);
var skipKeyValidation = root.profile.checked[credentials.walletId] == platformInfo.ua;
var client = bwcService.getClient(JSON.stringify(credentials), {
baseurl: getBaseURL(credentials.walletId),
skipKeyValidation: skipKeyValidation,
});
root.walletClients[credentials.walletId] = client;
root.walletClients[walletId] = client;
root.walletClients[walletId].started = true;
root.walletClients[walletId].doNotVerifyPayPro = isChromeApp;
if (client.incorrectDerivation) {
$log.warn('Key Derivation failed for wallet:' + credentials.walletId);
storageService.clearLastAddress(credentials.walletId, function() {});
} else if (!skipKeyValidation) {
root.profile.checked[credentials.walletId] = platformInfo.ua;
storageService.storeProfileThrottled(root.profile, function() {});
$log.warn('Key Derivation failed for wallet:' + walletId);
storageService.clearLastAddress(walletId, function() {});
}
client.removeAllListeners();
client.on('report', function(n) {
$log.info('BWC Report:' + n);
@ -100,9 +90,9 @@ angular.module('copayApp.services')
client.on('notification', function(n) {
$log.debug('BWC Notification:', n);
notificationService.newBWCNotification(n,
client.credentials.walletId, client.credentials.walletName);
walletId, client.credentials.walletName);
if (root.focusedClient.credentials.walletId == client.credentials.walletId) {
if (root.focusedClient.credentials.walletId == walletId) {
$rootScope.$emit(n.type, n);
} else {
$rootScope.$apply();
@ -113,16 +103,12 @@ angular.module('copayApp.services')
$log.debug('Wallet completed');
root.updateCredentialsFC(function() {
$rootScope.$emit('Local/WalletCompleted', client.credentials.walletId);
$rootScope.$emit('Local/WalletCompleted', walletId);
});
});
root.walletClients[credentials.walletId].started = true;
root.walletClients[credentials.walletId].doNotVerifyPayPro = isChromeApp;
if (client.hasPrivKeyEncrypted() && !client.isPrivKeyEncrypted()) {
$log.warn('Auto locking unlocked wallet:' + credentials.walletId);
$log.warn('Auto locking unlocked wallet:' + walletId);
client.lock();
}
@ -133,15 +119,35 @@ angular.module('copayApp.services')
}
client.setNotificationsInterval(BACKGROUND_UPDATE_PERIOD);
});
}
root.setWalletClients = function() {
var credentials = root.profile.credentials;
lodash.each(credentials, function(credential) {
//$log.info("Credentials:", credentials);
root.setWalletClient(credential);
return true;
};
// Used when reading wallets from the profile
root.bindWallet = function(credentials) {
if (!credentials.walletId)
throw 'bindWallet should receive credentials JSON';
$log.debug('Bind wallet:' + credentials.walletId);
// Create the client
var getBaseURL = function(walletId) {
var config = configService.getSync();
var defaults = configService.getDefaults();
return ((config.bwsFor && config.bwsFor[walletId]) || defaults.bws.url);
};
var skipKeyValidation = root.profile.isChecked(platformInfo.ua, credentials.walletId);
var client = bwcService.getClient(JSON.stringify(credentials), {
baseurl: getBaseURL(credentials.walletId),
skipKeyValidation: skipKeyValidation,
});
$rootScope.$emit('Local/WalletListUpdated');
if (!skipKeyValidation && !client.incorrectDerivation)
root.profile.setChecked(platformInfo.ua, credentials.walletId);
return root.bindWalletClient(client);
};
root.bindProfile = function(profile, cb) {
@ -150,7 +156,12 @@ angular.module('copayApp.services')
configService.get(function(err) {
$log.debug('Preferences read');
if (err) return cb(err);
root.setWalletClients();
lodash.each(root.profile.credentials, function(credentials) {
root.bindWallet(credentials);
});
$rootScope.$emit('Local/WalletListUpdated');
storageService.getFocusedWalletId(function(err, focusedWalletId) {
if (err) return cb(err);
root._setFocus(focusedWalletId, function() {
@ -228,7 +239,6 @@ angular.module('copayApp.services')
var walletClient = bwcService.getClient(null, opts);
var network = opts.networkName || 'livenet';
if (opts.mnemonic) {
try {
opts.mnemonic = root._normalizeMnemonic(opts.mnemonic);
@ -286,47 +296,54 @@ angular.module('copayApp.services')
return cb(null, walletClient);
};
root._createNewProfile = function(opts, cb) {
if (opts.noWallet) {
return cb(null, Profile.create());
}
root._seedWallet(opts, function(err, walletClient) {
if (err) return cb(err);
var walletName = gettextCatalog.getString('Personal Wallet');
var me = gettextCatalog.getString('me');
walletClient.createWallet(walletName, me, 1, 1, {
network: 'livenet',
walletPrivKey: opts.walletPrivKey,
}, function(err) {
if (err) return bwsError.cb(err, gettext('Error creating wallet'), cb);
var p = Profile.create({
credentials: [JSON.parse(walletClient.export())],
});
return cb(null, p);
});
})
};
root.createWallet = function(opts, cb) {
// Creates a wallet on BWC/BWS
root.doCreateWallet = function(opts, cb) {
$log.debug('Creating Wallet:', opts);
root._seedWallet(opts, function(err, walletClient) {
if (err) return cb(err);
walletClient.createWallet(opts.name, opts.myName || 'me', opts.m, opts.n, {
var name = opts.name || gettextCatalog.getString('Personal Wallet');
var myName = opts.myName || gettextCatalog.getString('me');
walletClient.createWallet(name, myName, opts.m, opts.n, {
network: opts.networkName,
walletPrivKey: opts.walletPrivKey,
}, function(err, secret) {
if (err) return bwsError.cb(err, gettext('Error creating wallet'), cb);
root._addWalletClient(walletClient, opts, cb);
})
return cb(null, walletClient, secret);
});
});
};
// Creates the default Copay profile and its wallet
root.createDefaultProfile = function(opts, cb) {
var p = Profile.create();
if (opts.noWallet) {
return cb(null, p);
}
opts.m = 1;
opts.n = 1;
opts.network = 'livenet';
root.doCreateWallet(opts, function(err, walletClient) {
if (err) return bwsError.cb(err, gettext('Error creating wallet'), cb);
p.addWallet(JSON.parse(walletClient.export()));
return cb(null, p);
});
};
// create and store a wallet
root.createWallet = function(opts, cb) {
root.doCreateWallet(opts, function(err, walletClient, secret) {
root.addAndBindWalletClient(walletClient, {
bwsurl: opts.bwsurl
}, cb);
});
};
// joins and stores a wallet
root.joinWallet = function(opts, cb) {
var walletClient = bwcService.getClient();
$log.debug('Joining Wallet:', opts);
@ -352,7 +369,9 @@ angular.module('copayApp.services')
walletClient.joinWallet(opts.secret, opts.myName || 'me', {}, function(err) {
if (err) return bwsError.cb(err, gettext('Could not join wallet'), cb);
root._addWalletClient(walletClient, opts, cb);
root.addAndBindWalletClient(walletClient, {
bwsurl: opts.bwsurl
}, cb);
});
});
};
@ -361,9 +380,8 @@ angular.module('copayApp.services')
return root.walletClients[walletId];
};
root.deleteWalletFC = function(opts, cb) {
var fc = root.focusedClient;
var walletId = fc.credentials.walletId;
root.deleteWalletClient = function(client, cb) {
var walletId = client.credentials.walletId;
pushNotificationsService.unsubscribe(root.getClient(walletId), function(err) {
if (err) $log.warn('Unsubscription error: ' + err.message);
@ -371,29 +389,22 @@ angular.module('copayApp.services')
});
$log.debug('Deleting Wallet:', fc.credentials.walletName);
fc.removeAllListeners();
root.profile.credentials = lodash.reject(root.profile.credentials, {
walletId: walletId
});
root.profile.deleteWallet(walletId);
delete root.walletClients[walletId];
root.focusedClient = null;
storageService.clearLastAddress(walletId, function(err) {
storageService.removeAllWalletData(walletId, function(err) {
if (err) $log.warn(err);
});
storageService.removeTxHistory(walletId, function(err) {
if (err) $log.warn(err);
});
storageService.clearBackupFlag(walletId, function(err) {
if (err) $log.warn(err);
});
$timeout(function() {
root.setWalletClients();
$rootScope.$emit('Local/WalletListUpdated');
root.setAndStoreFocus(null, function() {
storageService.storeProfile(root.profile, function(err) {
if (err) return cb(err);
@ -422,54 +433,50 @@ angular.module('copayApp.services')
});
}
root._addWalletClient = function(walletClient, opts, cb) {
var walletId = walletClient.credentials.walletId;
root.addAndBindWalletClient = function(client, opts, cb) {
var walletId = client.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);
}
if (!root.profile.addWallet(JSON.parse(client.export())))
return cb(gettext('Wallet already in Copay'));
var config = configService.getSync();
var defaults = configService.getDefaults();
var bwsFor = {};
bwsFor[walletId] = opts.bwsurl || defaults.bws.url;
root.bindWalletClient(client);
$rootScope.$emit('Local/WalletListUpdated', client);
configService.set({
bwsFor: bwsFor,
}, function(err) {
if (err) console.log(err);
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;
root.profile.credentials.push(JSON.parse(walletClient.export()));
root.setWalletClients();
configService.set({
bwsFor: bwsFor,
}, function(err) {
if (err) $log.warn(err);
return cb();
});
};
var handleImportedClient = function(cb) {
if (!opts.isImport) return cb();
$rootScope.$emit('Local/BackupDone', walletId);
var handleImport = function(cb) {
var isImport = opts.mnemonic || opts.externalSource || opts.extendedPrivateKey;
if (!walletClient.isComplete())
return cb();
if (!isImport)
return cb();
storageService.setCleanAndScanAddresses(walletId, cb);
};
$rootScope.$emit('Local/BackupDone', walletId);
if (!walletClient.isComplete())
return cb();
storageService.setCleanAndScanAddresses(walletId, cb);
};
handleImport(function() {
saveBwsUrl(function() {
handleImportedClient(function() {
root.setAndStoreFocus(walletId, function() {
storageService.storeProfile(root.profile, function(err) {
$rootScope.$emit('Local/ProfileCreated');
if (config.pushNotifications.enabled)
pushNotificationsService.enableNotifications(root.walletClients);
return cb(err, walletId);
});
});
});
});
@ -494,7 +501,13 @@ angular.module('copayApp.services')
var addressBook = str.addressBook || {};
var historyCache = str.historyCache ||  [];
root._addWalletClient(walletClient, opts, function(err, walletId) {
if (!walletClient.incorrectDerivation)
root.profile.setChecked(platformInfo.ua, walletClient.credentials.walletId);
root.addAndBindWalletClient(walletClient, {
bwsurl: opts.bwsurl,
isImport: true
}, function(err, walletId) {
if (err) return cb(err);
root.setMetaData(walletClient, addressBook, historyCache, function(error) {
if (error) $log.warn(error);
@ -511,7 +524,10 @@ angular.module('copayApp.services')
if (err)
return bwsError.cb(err, gettext('Could not import'), cb);
root._addWalletClient(walletClient, opts, cb);
root.addAndBindWalletClient(walletClient, {
bwsurl: opts.bwsurl,
isImport: true
}, cb);
});
};
@ -536,7 +552,10 @@ angular.module('copayApp.services')
if (err)
return bwsError.cb(err, gettext('Could not import'), cb);
root._addWalletClient(walletClient, opts, cb);
root.addAndBindWalletClient(walletClient, {
bwsurl: opts.bwsurl,
isImport: true
}, cb);
});
};
@ -557,7 +576,10 @@ angular.module('copayApp.services')
return bwsError.cb(err, gettext('Could not import'), cb);
}
root._addWalletClient(walletClient, opts, cb);
root.addAndBindWalletClient(walletClient, {
bwsurl: opts.bwsurl,
isImport: true
}, cb);
});
};
@ -565,21 +587,14 @@ angular.module('copayApp.services')
$log.info('Creating profile', opts);
var defaults = configService.getDefaults();
console.log('[profileService.js.567]'); //TODO
configService.get(function(err) {
console.log('[profileService.js.570]'); //TODO
root._createNewProfile(opts, function(err, p) {
root.createDefaultProfile(opts, function(err, p) {
if (err) return cb(err);
console.log('[profileService.js.574]'); //TODO
root.bindProfile(p, function(err) {
console.log('[profileService.js.577]'); //TODO
// ignore NONAGREEDDISCLAIMER
storageService.storeNewProfile(p, function(err) {
console.log('[profileService.js.581]'); //TODO
return cb(err);
});
});
@ -616,22 +631,18 @@ console.log('[profileService.js.581]'); //TODO
walletClient.createWalletFromOldCopay(username, password, blob, function(err, existed) {
if (err) return cb(gettext('Error importing wallet: ') + err);
if (root.walletClients[walletClient.credentials.walletId]) {
if (root.profile.hasWallet(walletClient.credentials.walletId)) {
$log.debug('Wallet:' + walletClient.credentials.walletName + ' already imported');
return cb(gettext('Wallet Already Imported: ') + walletClient.credentials.walletName);
};
$log.debug('Creating Wallet:', walletClient.credentials.walletName);
root.profile.credentials.push(JSON.parse(walletClient.export()));
root.setWalletClients();
root.setAndStoreFocus(walletClient.credentials.walletId, function() {
storageService.storeProfile(root.profile, function(err) {
return cb(null, walletClient.credentials.walletId, walletClient.credentials.walletName, existed);
});
});
root.addAndBindWalletClient(walletClient, {
isImport: true
}, cb);
});
};
// TODO
root.updateCredentialsFC = function(cb) {
var fc = root.focusedClient;

View file

@ -24,21 +24,6 @@ angular.module('copayApp.services')
}, cb);
};
var encryptOnMobile = function(text, cb) {
// UUID encryption is disabled.
return cb(null, text);
//
// getUUID(function(uuid) {
// if (uuid) {
// $log.debug('Encrypting profile');
// text = sjcl.encrypt(uuid, text);
// }
// return cb(null, text);
// });
};
var decryptOnMobile = function(text, cb) {
var json;
try {
@ -107,18 +92,14 @@ angular.module('copayApp.services')
};
root.storeNewProfile = function(profile, cb) {
encryptOnMobile(profile.toObj(), function(err, x) {
storage.create('profile', x, cb);
});
storage.create('profile', profile.toObj(), cb);
};
root.storeProfile = function(profile, cb) {
encryptOnMobile(profile.toObj(), function(err, x) {
storage.set('profile', x, cb);
});
storage.set('profile', profile.toObj(), cb);
};
root.storeProfileThrottled = lodash.throttle(root.storeProfile, 5000);
root.storeProfileThrottled = lodash.throttle(root.storeProfile, 5000);
root.getProfile = function(cb) {
storage.get('profile', function(err, str) {
@ -293,5 +274,17 @@ angular.module('copayApp.services')
storage.remove('coinbaseTxs-' + network, cb);
};
root.removeAllWalletData = function(walletId, cb) {
storageService.clearLastAddress(walletId, function(err) {
if (err) return cb(err);
storageService.removeTxHistory(walletId, function(err) {
if (err) return cb(err);
storageService.clearBackupFlag(walletId, function(err) {
return cb(err);
});
});
});
};
return root;
});