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) { modalInstance.result.then(function(ok) {
if (ok) { if (ok) {
_deleteWallet(); doDeleteWallet();
} }
}); });
}; };
var _deleteWallet = function() { var doDeleteWallet = function() {
var fc = profileService.focusedClient; var fc = profileService.focusedClient;
var walletName = fc.credentials.walletName; var walletName = fc.credentials.walletName;
profileService.deleteWalletFC({}, function(err) { profileService.deleteWalletClient(fc, function(err) {
if (err) { if (err) {
self.error = err.message || err; self.error = err.message || err;
$timeout(function() { $timeout(function() {

View file

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

View file

@ -10,8 +10,6 @@ angular.module('copayApp.controllers').controller('disclaimerController',
opts = opts || {}; opts = opts || {};
$log.debug('Creating profile'); $log.debug('Creating profile');
profileService.create(opts, function(err) { profileService.create(opts, function(err) {
console.log('[disclaimer.js.13]', err); //TODO
if (err) { if (err) {
$log.warn(err); $log.warn(err);
$scope.error = err; $scope.error = err;
@ -33,8 +31,6 @@ angular.module('copayApp.controllers').controller('disclaimerController',
$scope.error = ""; $scope.error = "";
self.creatingProfile = false; self.creatingProfile = false;
console.log('[disclaimer.js.33]'); //TODO
}); });
}; };
@ -44,10 +40,7 @@ angular.module('copayApp.controllers').controller('disclaimerController',
profileService.getProfile(function(err, profile) { profileService.getProfile(function(err, profile) {
if (!profile) { if (!profile) {
console.log('[disclaimer.js.43]'); //TODO
create(opts); create(opts);
console.log('[disclaimer.js.46]'); //TODO
} else { } else {
$log.info('There is already a profile'); $log.info('There is already a profile');
self.creatingProfile = false; 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) { self.updateRemotePreferences = function(opts, cb) {
var clients;
var prefs = opts.preferences || {}; var prefs = opts.preferences || {};
var fc = profileService.focusedClient;
// Update this JIC. // Update this JIC.
var config = configService.getSync().wallet.settings; var config = configService.getSync().wallet.settings;
@ -295,24 +296,20 @@ angular.module('copayApp.controllers').controller('indexController', function($r
prefs.language = self.defaultLanguageIsoCode; prefs.language = self.defaultLanguageIsoCode;
prefs.unit = config.unitCode; prefs.unit = config.unitCode;
var clients = [];
if (opts.saveAll) { if (opts.client) {
clients = lodash.values(profileService.walletClients); clients = [opts.client];
} else { } else {
clients = [fc]; clients = lodash.values(profileService.walletClients);
}; };
self._updateRemotePreferencesFor(clients, prefs, function(err) { self._updateRemotePreferencesFor(clients, prefs, function(err) {
if (err) return cb(err); if (err) return cb(err);
if (!fc) return cb();
fc.getPreferences(function(err, preferences) { lodash.each(clients, function(c) {
if (err) { c.preferences = lodash.assign(prefs, c.preferences);
return cb(err);
}
self.preferences = preferences;
return cb();
}); });
return cb();
}); });
}; };
@ -1055,7 +1052,9 @@ angular.module('copayApp.controllers').controller('indexController', function($r
return; return;
} }
profileService.setWalletClients(); profileService.addWalletClient(fc, {
force: true
});
self.startScan(self.walletId); self.startScan(self.walletId);
}); });
}; };
@ -1513,9 +1512,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
$rootScope.$on('Local/ProfileBound', function() { $rootScope.$on('Local/ProfileBound', function() {
storageService.getRemotePrefsStoredFlag(function(err, val) { storageService.getRemotePrefsStoredFlag(function(err, val) {
if (err || val) return; if (err || val) return;
self.updateRemotePreferences({ self.updateRemotePreferences({}, function() {
saveAll: true
}, function() {
$log.debug('Remote preferences saved'); $log.debug('Remote preferences saved');
storageService.setRemotePrefsStoredFlag(function() {}); storageService.setRemotePrefsStoredFlag(function() {});
}); });
@ -1524,9 +1521,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
$rootScope.$on('Local/LanguageSettingUpdated', function() { $rootScope.$on('Local/LanguageSettingUpdated', function() {
self.setUxLanguage(function() { self.setUxLanguage(function() {
self.updateRemotePreferences({ self.updateRemotePreferences({}, function() {
saveAll: true
}, function() {
$log.debug('Remote preferences saved') $log.debug('Remote preferences saved')
}); });
}); });
@ -1558,15 +1553,14 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.updateAll({ self.updateAll({
triggerTxUpdate: true, triggerTxUpdate: true,
}); });
self.updateRemotePreferences({ self.updateRemotePreferences({}, function() {
saveAll: true
}, function() {
$log.debug('Remote preferences saved') $log.debug('Remote preferences saved')
}); });
}); });
$rootScope.$on('Local/EmailSettingUpdated', function(event, email, cb) { $rootScope.$on('Local/EmailSettingUpdated', function(event, client, email, cb) {
self.updateRemotePreferences({ self.updateRemotePreferences({
client: client,
preferences: { preferences: {
email: email || null email: email || null
}, },
@ -1582,11 +1576,14 @@ angular.module('copayApp.controllers').controller('indexController', function($r
} }
}); });
$rootScope.$on('Local/ProfileCreated', function(event) {
self.updateRemotePreferences({ lodash.each(['Local/ProfileCreated', 'Local/WalletListUpdated'], function(eventName) {
saveAll: true $rootScope.$on(eventName, function(event, client) {
}, function() { self.updateRemotePreferences({
$log.debug('Remote preferences saved'); 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) { $rootScope.$on('Local/NoWallets', function(event) {
$log.debug('Event: NoWallets');
$timeout(function() { $timeout(function() {
self.hasProfile = true; self.hasProfile = true;

View file

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

View file

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

View file

@ -27,6 +27,7 @@ Profile.fromObj = function(obj) {
x.credentials = obj.credentials; x.credentials = obj.credentials;
x.disclaimerAccepted = obj.disclaimerAccepted; x.disclaimerAccepted = obj.disclaimerAccepted;
x.checked = obj.checked || {}; x.checked = obj.checked || {};
x.checkedUA = null;
if (x.credentials[0] && typeof x.credentials[0] != 'object') if (x.credentials[0] && typeof x.credentials[0] != 'object')
throw ("credentials should be an object"); throw ("credentials should be an object");
@ -41,3 +42,44 @@ Profile.fromString = function(str) {
Profile.prototype.toObj = function() { Profile.prototype.toObj = function() {
return JSON.stringify(this); 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) { // Adds a wallet client to profileService
if (root.walletClients[credentials.walletId] && root.bindWalletClient = function(client, opts) {
root.walletClients[credentials.walletId].started) { var opts = opts || {};
return; var walletId = client.credentials.walletId;
if ((root.walletClients[walletId] && root.walletClients[walletId].started)
|| opts.force) {
return false;
} }
root.walletClients[walletId] = client;
var getBaseURL = function(walletId) { root.walletClients[walletId].started = true;
var config = configService.getSync(); root.walletClients[walletId].doNotVerifyPayPro = isChromeApp;
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;
if (client.incorrectDerivation) { if (client.incorrectDerivation) {
$log.warn('Key Derivation failed for wallet:' + credentials.walletId); $log.warn('Key Derivation failed for wallet:' + walletId);
storageService.clearLastAddress(credentials.walletId, function() {}); storageService.clearLastAddress(walletId, function() {});
} else if (!skipKeyValidation) {
root.profile.checked[credentials.walletId] = platformInfo.ua;
storageService.storeProfileThrottled(root.profile, function() {});
} }
client.removeAllListeners(); client.removeAllListeners();
client.on('report', function(n) { client.on('report', function(n) {
$log.info('BWC Report:' + n); $log.info('BWC Report:' + n);
@ -100,9 +90,9 @@ angular.module('copayApp.services')
client.on('notification', function(n) { client.on('notification', function(n) {
$log.debug('BWC Notification:', n); $log.debug('BWC Notification:', n);
notificationService.newBWCNotification(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); $rootScope.$emit(n.type, n);
} else { } else {
$rootScope.$apply(); $rootScope.$apply();
@ -113,16 +103,12 @@ angular.module('copayApp.services')
$log.debug('Wallet completed'); $log.debug('Wallet completed');
root.updateCredentialsFC(function() { 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()) { if (client.hasPrivKeyEncrypted() && !client.isPrivKeyEncrypted()) {
$log.warn('Auto locking unlocked wallet:' + credentials.walletId); $log.warn('Auto locking unlocked wallet:' + walletId);
client.lock(); client.lock();
} }
@ -133,15 +119,35 @@ angular.module('copayApp.services')
} }
client.setNotificationsInterval(BACKGROUND_UPDATE_PERIOD); client.setNotificationsInterval(BACKGROUND_UPDATE_PERIOD);
}); });
}
root.setWalletClients = function() { return true;
var credentials = root.profile.credentials; };
lodash.each(credentials, function(credential) {
//$log.info("Credentials:", credentials);
root.setWalletClient(credential); // 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) { root.bindProfile = function(profile, cb) {
@ -150,7 +156,12 @@ angular.module('copayApp.services')
configService.get(function(err) { configService.get(function(err) {
$log.debug('Preferences read'); $log.debug('Preferences read');
if (err) return cb(err); 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) { storageService.getFocusedWalletId(function(err, focusedWalletId) {
if (err) return cb(err); if (err) return cb(err);
root._setFocus(focusedWalletId, function() { root._setFocus(focusedWalletId, function() {
@ -228,7 +239,6 @@ angular.module('copayApp.services')
var walletClient = bwcService.getClient(null, opts); var walletClient = bwcService.getClient(null, opts);
var network = opts.networkName || 'livenet'; var network = opts.networkName || 'livenet';
if (opts.mnemonic) { if (opts.mnemonic) {
try { try {
opts.mnemonic = root._normalizeMnemonic(opts.mnemonic); opts.mnemonic = root._normalizeMnemonic(opts.mnemonic);
@ -286,47 +296,54 @@ angular.module('copayApp.services')
return cb(null, walletClient); return cb(null, walletClient);
}; };
root._createNewProfile = function(opts, cb) { // Creates a wallet on BWC/BWS
root.doCreateWallet = 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) {
$log.debug('Creating Wallet:', opts); $log.debug('Creating Wallet:', opts);
root._seedWallet(opts, function(err, walletClient) { root._seedWallet(opts, function(err, walletClient) {
if (err) return cb(err); 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, network: opts.networkName,
walletPrivKey: opts.walletPrivKey, walletPrivKey: opts.walletPrivKey,
}, function(err, secret) { }, function(err, secret) {
if (err) return bwsError.cb(err, gettext('Error creating wallet'), cb); if (err) return bwsError.cb(err, gettext('Error creating wallet'), cb);
return cb(null, walletClient, secret);
root._addWalletClient(walletClient, opts, cb); });
})
}); });
}; };
// 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) { root.joinWallet = function(opts, cb) {
var walletClient = bwcService.getClient(); var walletClient = bwcService.getClient();
$log.debug('Joining Wallet:', opts); $log.debug('Joining Wallet:', opts);
@ -352,7 +369,9 @@ angular.module('copayApp.services')
walletClient.joinWallet(opts.secret, opts.myName || 'me', {}, function(err) { walletClient.joinWallet(opts.secret, opts.myName || 'me', {}, function(err) {
if (err) return bwsError.cb(err, gettext('Could not join wallet'), cb); 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]; return root.walletClients[walletId];
}; };
root.deleteWalletFC = function(opts, cb) { root.deleteWalletClient = function(client, cb) {
var fc = root.focusedClient; var walletId = client.credentials.walletId;
var walletId = fc.credentials.walletId;
pushNotificationsService.unsubscribe(root.getClient(walletId), function(err) { pushNotificationsService.unsubscribe(root.getClient(walletId), function(err) {
if (err) $log.warn('Unsubscription error: ' + err.message); if (err) $log.warn('Unsubscription error: ' + err.message);
@ -371,29 +389,22 @@ angular.module('copayApp.services')
}); });
$log.debug('Deleting Wallet:', fc.credentials.walletName); $log.debug('Deleting Wallet:', fc.credentials.walletName);
fc.removeAllListeners(); fc.removeAllListeners();
root.profile.credentials = lodash.reject(root.profile.credentials, {
walletId: walletId root.profile.deleteWallet(walletId);
});
delete root.walletClients[walletId]; delete root.walletClients[walletId];
root.focusedClient = null; root.focusedClient = null;
storageService.clearLastAddress(walletId, function(err) {
storageService.removeAllWalletData(walletId, function(err) {
if (err) $log.warn(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() { $timeout(function() {
root.setWalletClients(); $rootScope.$emit('Local/WalletListUpdated');
root.setAndStoreFocus(null, function() { root.setAndStoreFocus(null, function() {
storageService.storeProfile(root.profile, function(err) { storageService.storeProfile(root.profile, function(err) {
if (err) return cb(err); if (err) return cb(err);
@ -422,54 +433,50 @@ angular.module('copayApp.services')
}); });
} }
root._addWalletClient = function(walletClient, opts, cb) { root.addAndBindWalletClient = function(client, opts, cb) {
var walletId = walletClient.credentials.walletId; var walletId = client.credentials.walletId
// check if exist if (!root.profile.addWallet(JSON.parse(client.export())))
var w = lodash.find(root.profile.credentials, { return cb(gettext('Wallet already in Copay'));
'walletId': walletId
});
if (w) {
return cb(gettext('Wallet already in Copay' + ": ") + w.walletName);
}
var config = configService.getSync(); root.bindWalletClient(client);
var defaults = configService.getDefaults(); $rootScope.$emit('Local/WalletListUpdated', client);
var bwsFor = {};
bwsFor[walletId] = opts.bwsurl || defaults.bws.url;
configService.set({ var saveBwsUrl = function(cb) {
bwsFor: bwsFor, if (!opts.bwsurl) return cb();
}, function(err) { var config = configService.getSync();
if (err) console.log(err); var defaults = configService.getDefaults();
var bwsFor = {};
bwsFor[walletId] = opts.bwsurl || defaults.bws.url;
root.profile.credentials.push(JSON.parse(walletClient.export())); configService.set({
root.setWalletClients(); 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) { if (!walletClient.isComplete())
var isImport = opts.mnemonic || opts.externalSource || opts.extendedPrivateKey; return cb();
if (!isImport) storageService.setCleanAndScanAddresses(walletId, cb);
return cb(); };
$rootScope.$emit('Local/BackupDone', walletId); saveBwsUrl(function() {
handleImportedClient(function() {
if (!walletClient.isComplete())
return cb();
storageService.setCleanAndScanAddresses(walletId, cb);
};
handleImport(function() {
root.setAndStoreFocus(walletId, function() { root.setAndStoreFocus(walletId, function() {
storageService.storeProfile(root.profile, function(err) { storageService.storeProfile(root.profile, function(err) {
$rootScope.$emit('Local/ProfileCreated');
if (config.pushNotifications.enabled) if (config.pushNotifications.enabled)
pushNotificationsService.enableNotifications(root.walletClients); pushNotificationsService.enableNotifications(root.walletClients);
return cb(err, walletId); return cb(err, walletId);
}); });
}); });
}); });
}); });
@ -494,7 +501,13 @@ angular.module('copayApp.services')
var addressBook = str.addressBook || {}; var addressBook = str.addressBook || {};
var historyCache = str.historyCache ||  []; 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); if (err) return cb(err);
root.setMetaData(walletClient, addressBook, historyCache, function(error) { root.setMetaData(walletClient, addressBook, historyCache, function(error) {
if (error) $log.warn(error); if (error) $log.warn(error);
@ -511,7 +524,10 @@ angular.module('copayApp.services')
if (err) if (err)
return bwsError.cb(err, gettext('Could not import'), cb); 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) if (err)
return bwsError.cb(err, gettext('Could not import'), cb); 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); 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); $log.info('Creating profile', opts);
var defaults = configService.getDefaults(); var defaults = configService.getDefaults();
console.log('[profileService.js.567]'); //TODO
configService.get(function(err) { configService.get(function(err) {
console.log('[profileService.js.570]'); //TODO root.createDefaultProfile(opts, function(err, p) {
root._createNewProfile(opts, function(err, p) {
if (err) return cb(err); if (err) return cb(err);
console.log('[profileService.js.574]'); //TODO
root.bindProfile(p, function(err) { root.bindProfile(p, function(err) {
console.log('[profileService.js.577]'); //TODO
// ignore NONAGREEDDISCLAIMER // ignore NONAGREEDDISCLAIMER
storageService.storeNewProfile(p, function(err) { storageService.storeNewProfile(p, function(err) {
console.log('[profileService.js.581]'); //TODO
return cb(err); return cb(err);
}); });
}); });
@ -616,22 +631,18 @@ console.log('[profileService.js.581]'); //TODO
walletClient.createWalletFromOldCopay(username, password, blob, function(err, existed) { walletClient.createWalletFromOldCopay(username, password, blob, function(err, existed) {
if (err) return cb(gettext('Error importing wallet: ') + err); 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'); $log.debug('Wallet:' + walletClient.credentials.walletName + ' already imported');
return cb(gettext('Wallet Already Imported: ') + walletClient.credentials.walletName); return cb(gettext('Wallet Already Imported: ') + walletClient.credentials.walletName);
}; };
$log.debug('Creating Wallet:', walletClient.credentials.walletName); root.addAndBindWalletClient(walletClient, {
root.profile.credentials.push(JSON.parse(walletClient.export())); isImport: true
root.setWalletClients(); }, cb);
root.setAndStoreFocus(walletClient.credentials.walletId, function() {
storageService.storeProfile(root.profile, function(err) {
return cb(null, walletClient.credentials.walletId, walletClient.credentials.walletName, existed);
});
});
}); });
}; };
// TODO
root.updateCredentialsFC = function(cb) { root.updateCredentialsFC = function(cb) {
var fc = root.focusedClient; var fc = root.focusedClient;

View file

@ -24,21 +24,6 @@ angular.module('copayApp.services')
}, cb); }, 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 decryptOnMobile = function(text, cb) {
var json; var json;
try { try {
@ -107,18 +92,14 @@ angular.module('copayApp.services')
}; };
root.storeNewProfile = function(profile, cb) { root.storeNewProfile = function(profile, cb) {
encryptOnMobile(profile.toObj(), function(err, x) { storage.create('profile', profile.toObj(), cb);
storage.create('profile', x, cb);
});
}; };
root.storeProfile = function(profile, cb) { root.storeProfile = function(profile, cb) {
encryptOnMobile(profile.toObj(), function(err, x) { storage.set('profile', profile.toObj(), cb);
storage.set('profile', x, cb);
});
}; };
root.storeProfileThrottled = lodash.throttle(root.storeProfile, 5000); root.storeProfileThrottled = lodash.throttle(root.storeProfile, 5000);
root.getProfile = function(cb) { root.getProfile = function(cb) {
storage.get('profile', function(err, str) { storage.get('profile', function(err, str) {
@ -293,5 +274,17 @@ angular.module('copayApp.services')
storage.remove('coinbaseTxs-' + network, cb); 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; return root;
}); });