Merge remote-tracking branch 'origin/wallet/task/351' into wallet/task/366

This commit is contained in:
Sebastiaan Pasma 2018-06-06 14:23:59 +02:00
commit 80299cd99d
4 changed files with 137 additions and 20 deletions

View file

@ -9,12 +9,12 @@ function Profile() {
this.version = '1.0.0';
};
Profile.create = function(opts) {
opts = opts || {};
Profile.create = function(appVersion) {
var x = new Profile();
x.appVersion = appVersion;
x.createdOn = Date.now();
x.credentials = opts.credentials || [];
x.credentials = [];
x.disclaimerAccepted = true;
x.checked = {};
return x;
@ -23,6 +23,7 @@ Profile.create = function(opts) {
Profile.fromObj = function(obj) {
var x = new Profile();
x.appVersion = obj.appVersion;
x.createdOn = obj.createdOn;
x.credentials = obj.credentials;
x.disclaimerAccepted = obj.disclaimerAccepted;
@ -62,6 +63,39 @@ Profile.prototype.isDeviceChecked = function(ua) {
return this.checkedUA == ua;
};
/**
*
* @param {Profile} other
*/
Profile.prototype.merge = function(other) {
var newCredentials = [];
var otherCredentialsLength = other.credentials.length;
var thisProfile = this;
other.credentials.forEach(function(otherCredential) {
var credentialExists = false;
thisProfile.credentials.forEach(function(thisCredential) {
if (otherCredential.walletId === thisCredential.walletId) {
credentialExists = true;
}
});
if (!credentialExists) {
newCredentials.push(otherCredential);
}
});
Array.prototype.push.apply(this.credentials, newCredentials);
};
/**
* It's a simple operation, but it means that all the profile logic stays
* in this file.
* @param {string} appVersion - ie "4.11.0"
*/
Profile.prototype.setAppVersion = function(appVersion) {
this.appVersion = appVersion;
}
Profile.prototype.setChecked = function(ua, walletId) {
if (this.checkedUA != ua) {

View file

@ -20,8 +20,7 @@ angular.module('copayApp.services')
if (isChromeApp || isNW) {
chrome.storage.local.get(k,
function(data) {
//TODO check for errors
return cb(null, data[k]);
return cb(chrome.runtime.lastError, data[k]);
});
} else {
return cb(null, ls.getItem(k));
@ -56,16 +55,24 @@ angular.module('copayApp.services')
obj[k] = v;
chrome.storage.local.set(obj, cb);
chrome.storage.local.set(obj, function(){
cb(chrome.runtime.lastError);
});
} else {
ls.setItem(k, v);
try {
ls.setItem(k, v);
} catch (e) {
return cb(e);
}
return cb();
}
};
root.remove = function(k, cb) {
if (isChromeApp || isNW) {
chrome.storage.local.remove(k, cb);
chrome.storage.local.remove(k, function(){
cb(chrome.runtime.lastError);
});
} else {
ls.removeItem(k);
return cb();

View file

@ -706,7 +706,7 @@ angular.module('copayApp.services')
configService.get(function(err) {
if (err) $log.debug(err);
var p = Profile.create();
var p = Profile.create(appConfigService.version);
storageService.storeNewProfile(p, function(err) {
if (err) return cb(err);
root.bindProfile(p, function(err) {

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.services')
.factory('storageService', function(logHeader, fileStorageService, localStorageService, sjcl, $log, lodash, platformInfo, secureStorageService, $timeout) {
.factory('storageService', function(appConfigService, logHeader, fileStorageService, localStorageService, sjcl, $log, lodash, platformInfo, secureStorageService, $timeout) {
var root = {};
var storage;
@ -151,22 +151,98 @@ angular.module('copayApp.services')
secureStorageService.set('profile', profile.toObj(), cb);
};
root.getProfile = function(cb) {
secureStorageService.get('profile', function(err, str) {
/**
* @callback getProfileCallback
* @param {Error} error - falsy if profile not found.
* @param {Profile} profile - falsy if error or profile not found.
*/
if (err || !str)
return cb(err);
/**
*
* @param {Profile} oldProfile
* @param {Profile} secureProfile - may be falsy if no secure profile found.
* @param {getProfileCallback} cb
*/
function _migrateProfiles(oldProfile, secureProfile, cb) {
var newProfile;
decryptOnMobile(str, function(err, str) {
if (err) return cb(err);
var p, err;
try {
p = Profile.fromString(str);
} catch (e) {
$log.debug('Could not read profile:', e);
err = new Error('Could not read profile:' + p);
if (secureProfile) {
secureProfile.merge(oldProfile);
newProfile = secureProfile;
} else {
newProfile = oldProfile;
newProfile.setAppVersion(appConfigService.version);
}
root.storeNewProfile(newProfile, function(storeErr) {
if (storeErr) {
cb(storeErr, null);
return;
}
storage.remove('profile', function(removeErr){
if (removeErr) {
cb(removeErr, null);
return;
}
return cb(err, p);
cb(null, newProfile);
});
});
};
/**
*
* @param {getProfileCallback} cb
*/
root.getProfile = function(cb) {
secureStorageService.get('profile', function(secureErr, secureStr) {
var secureProfile;
var oldProfile;
if (secureErr) {
return cb(secureErr, null);
}
if (secureStr) {
try {
secureProfile = Profile.fromString(secureStr);
$log.debug('profile: ' + JSON.stringify(secureProfile));
} catch (e) {
$log.error(e);
return cb(e, null);
}
}
storage.get('profile', function(getErr, getStr) {
if (getErr) {
return cb(getErr);
}
if (!getStr) {
if (secureProfile) {
return cb(null, secureProfile);
} else {
// No profiles found. No errors either.
return cb(null, null);
}
}
decryptOnMobile(getStr, function(err, str) {
if (err) return cb(err);
var p, err;
try {
oldProfile = Profile.fromString(str);
} catch (e) {
$log.debug('Could not read profile:', e);
err = new Error('Could not read profile.');
return(err, null);
}
_migrateProfiles(oldProfile, secureProfile, cb);
});
});
});
};