Wallet/src/js/services/storageService.js

196 lines
5.1 KiB
JavaScript
Raw Normal View History

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);
});
};
root.tryToMigrate = function(cb) {
2015-04-26 11:41:25 -03:00
if (!shouldUseFileStorage) return cb();
2015-04-25 15:09:13 -03:00
localStorageService.get('profile', function(err, str) {
if (err) return cb(err);
if (!str) return cb();
$log.info('Starting Migration profile to File storage...');
2015-04-25 15:09:13 -03:00
fileStorageService.create('profile', str, function(err) {
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);
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);
}
$log.info('Config Migrated successfully');
2015-04-25 15:09:13 -03:00
return root.getProfile(cb);
});
});
});
});
};
2015-03-06 12:00:10 -03:00
root.storeNewProfile = function(profile, cb) {
encryptOnMobile(profile.toObj(), function(err, x) {
storage.create('profile', x, cb);
2015-03-06 12:00:10 -03:00
});
};
root.storeProfile = function(profile, cb) {
encryptOnMobile(profile.toObj(), function(err, x) {
storage.set('profile', x, cb);
2015-03-06 12:00:10 -03:00
});
};
root.getProfile = function(cb) {
storage.get('profile', function(err, str) {
2015-04-24 18:06:04 -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) {
storage.remove('profile', cb);
2015-03-06 12:00:10 -03:00
};
root.storeFocusedWalletId = function(id, cb) {
2015-05-18 10:30:09 -03:00
storage.set('focusedWalletId', id||'', cb);
2015-03-06 12:00:10 -03:00
};
root.getFocusedWalletId = function(cb) {
storage.get('focusedWalletId', cb);
2015-03-06 12:00:10 -03:00
};
root.getLastAddress = function(walletId, cb) {
storage.get('lastAddress-' + walletId, cb);
2015-03-06 12:00:10 -03:00
};
root.storeLastAddress = function(walletId, address, cb) {
storage.set('lastAddress-' + walletId, address, cb);
2015-03-06 12:00:10 -03:00
};
root.clearLastAddress = function(walletId, cb) {
storage.remove('lastAddress-' + walletId, cb);
2015-03-06 12:00:10 -03:00
};
root.setBackupFlag = function(walletId, cb) {
storage.set('backup-' + walletId, Date.now(), cb);
2015-03-06 12:00:10 -03:00
};
root.getBackupFlag = function(walletId, cb) {
storage.get('backup-' + walletId, cb);
2015-03-06 12:00:10 -03:00
};
root.setCleanAndScanAddresses = function(cb) {
storage.set('CleanAndScanAddresses', Date.now(), cb);
};
root.getCleanAndScanAddresses = function(cb) {
storage.get('CleanAndScanAddresses', cb);
};
root.removeCleanAndScanAddresses = function(cb) {
storage.remove('CleanAndScanAddresses', cb);
};
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-06-11 11:59:48 -03:00
root.setCopayDisclaimer = function(cb) {
storage.set('agreeDisclaimer', true, cb);
};
root.getCopayDisclaimer = function(cb) {
storage.get('agreeDisclaimer', cb);
};
2015-06-29 21:46:34 -03:00
root.setRemotePreferencesStored = function(cb) {
storage.set('remotePrefStored', true, cb);
};
root.getRemovePreferencesStored = function(cb) {
storage.get('remotePrefStored', cb);
};
2015-03-06 12:00:10 -03:00
return root;
});