2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
|
|
|
|
angular.module('copayApp.services')
|
2015-04-26 11:41:25 -03:00
|
|
|
.factory('storageService', function(logHeader, fileStorageService, localStorageService, sjcl, $log, lodash, isCordova) {
|
2015-03-06 12:00:10 -03:00
|
|
|
|
|
|
|
|
var root = {};
|
2015-04-26 11:41:25 -03:00
|
|
|
|
|
|
|
|
// File storage is not supported for writting according to
|
|
|
|
|
// https://github.com/apache/cordova-plugin-file/#supported-platforms
|
|
|
|
|
var shouldUseFileStorage = isCordova && !isMobile.Windows();
|
|
|
|
|
$log.debug('Using file storage:', shouldUseFileStorage);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var storage = shouldUseFileStorage ? fileStorageService : localStorageService;
|
2015-03-06 12:00:10 -03:00
|
|
|
|
|
|
|
|
var getUUID = function(cb) {
|
|
|
|
|
// TO SIMULATE MOBILE
|
|
|
|
|
//return cb('hola');
|
|
|
|
|
if (!window || !window.plugins || !window.plugins.uniqueDeviceID)
|
|
|
|
|
return cb(null);
|
|
|
|
|
|
|
|
|
|
window.plugins.uniqueDeviceID.get(
|
|
|
|
|
function(uuid) {
|
|
|
|
|
return cb(uuid);
|
|
|
|
|
}, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var encryptOnMobile = function(text, cb) {
|
|
|
|
|
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 {
|
|
|
|
|
json = JSON.parse(text);
|
|
|
|
|
} catch (e) {};
|
|
|
|
|
|
|
|
|
|
if (!json.iter || !json.ct)
|
|
|
|
|
return cb(null, text);
|
|
|
|
|
|
|
|
|
|
$log.debug('Profile is encrypted');
|
|
|
|
|
getUUID(function(uuid) {
|
|
|
|
|
if (!uuid)
|
|
|
|
|
return cb(new Error('Could not decrypt localstorage profile'));
|
|
|
|
|
|
|
|
|
|
text = sjcl.decrypt(uuid, text);
|
|
|
|
|
return cb(null, text);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-04-25 14:42:17 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
root.tryToMigrate = function(cb) {
|
2015-04-26 11:41:25 -03:00
|
|
|
if (!shouldUseFileStorage) return cb();
|
2015-04-25 14:42:17 -03:00
|
|
|
|
2015-04-25 15:09:13 -03:00
|
|
|
localStorageService.get('profile', function(err, str) {
|
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
if (!str) return cb();
|
|
|
|
|
|
2015-04-30 13:03:30 -03:00
|
|
|
$log.info('Starting Migration profile to File storage...');
|
2015-04-25 15:09:13 -03:00
|
|
|
|
|
|
|
|
fileStorageService.create('profile', str, function(err) {
|
2015-04-25 14:42:17 -03:00
|
|
|
if (err) cb(err);
|
|
|
|
|
$log.info('Profile Migrated successfully');
|
|
|
|
|
|
|
|
|
|
localStorageService.get('config', function(err, c) {
|
2015-04-25 15:09:13 -03:00
|
|
|
if (err) return cb(err);
|
|
|
|
|
if (!c) return root.getProfile(cb);
|
|
|
|
|
|
2015-04-25 14:42:17 -03:00
|
|
|
fileStorageService.create('config', c, function(err) {
|
2015-04-25 15:09:13 -03:00
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
$log.info('Error migrating config: ignoring', err);
|
|
|
|
|
return root.getProfile(cb);
|
|
|
|
|
}
|
2015-04-25 14:42:17 -03:00
|
|
|
$log.info('Config Migrated successfully');
|
2015-04-25 15:09:13 -03:00
|
|
|
return root.getProfile(cb);
|
2015-04-25 14:42:17 -03:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
root.storeNewProfile = function(profile, cb) {
|
|
|
|
|
encryptOnMobile(profile.toObj(), function(err, x) {
|
2015-04-24 16:39:12 -03:00
|
|
|
storage.create('profile', x, cb);
|
2015-03-06 12:00:10 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.storeProfile = function(profile, cb) {
|
|
|
|
|
encryptOnMobile(profile.toObj(), function(err, x) {
|
2015-04-24 16:39:12 -03:00
|
|
|
storage.set('profile', x, cb);
|
2015-03-06 12:00:10 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getProfile = function(cb) {
|
2015-04-24 16:39:12 -03:00
|
|
|
storage.get('profile', function(err, str) {
|
2015-04-24 18:06:04 -03:00
|
|
|
|
2015-04-25 14:42:17 -03:00
|
|
|
if (err || !str)
|
|
|
|
|
// Migrate ?
|
2015-04-24 18:06:04 -03:00
|
|
|
return cb(err);
|
2015-03-06 12:00:10 -03:00
|
|
|
|
|
|
|
|
decryptOnMobile(str, function(err, str) {
|
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
var p, err;
|
|
|
|
|
try {
|
|
|
|
|
p = Profile.fromString(str);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
err = new Error('Could not read profile:' + p);
|
|
|
|
|
}
|
|
|
|
|
return cb(err, p);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.deleteProfile = function(cb) {
|
2015-04-24 16:39:12 -03:00
|
|
|
storage.remove('profile', cb);
|
2015-03-06 12:00:10 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.storeFocusedWalletId = function(id, cb) {
|
2015-04-24 16:39:12 -03:00
|
|
|
storage.set('focusedWalletId', id, cb);
|
2015-03-06 12:00:10 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getFocusedWalletId = function(cb) {
|
2015-04-24 16:39:12 -03:00
|
|
|
storage.get('focusedWalletId', cb);
|
2015-03-06 12:00:10 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getLastAddress = function(walletId, cb) {
|
2015-04-24 16:39:12 -03:00
|
|
|
storage.get('lastAddress-' + walletId, cb);
|
2015-03-06 12:00:10 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.storeLastAddress = function(walletId, address, cb) {
|
2015-04-24 16:39:12 -03:00
|
|
|
storage.set('lastAddress-' + walletId, address, cb);
|
2015-03-06 12:00:10 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.clearLastAddress = function(walletId, cb) {
|
2015-04-24 16:39:12 -03:00
|
|
|
storage.remove('lastAddress-' + walletId, cb);
|
2015-03-06 12:00:10 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.setBackupFlag = function(walletId, cb) {
|
2015-04-24 16:39:12 -03:00
|
|
|
storage.set('backup-' + walletId, Date.now(), cb);
|
2015-03-06 12:00:10 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getBackupFlag = function(walletId, cb) {
|
2015-04-24 16:39:12 -03:00
|
|
|
storage.get('backup-' + walletId, cb);
|
2015-03-06 12:00:10 -03:00
|
|
|
};
|
|
|
|
|
|
2015-04-25 12:37:04 -03:00
|
|
|
root.getConfig = function(cb) {
|
|
|
|
|
storage.get('config', cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.storeConfig = function(val, cb) {
|
2015-04-26 20:13:02 -03:00
|
|
|
$log.debug('Storing Preferences', val);
|
2015-04-25 12:37:04 -03:00
|
|
|
storage.set('config', val, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.clearConfig = function(cb) {
|
|
|
|
|
storage.remove('config', cb);
|
|
|
|
|
};
|
|
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
return root;
|
|
|
|
|
});
|