2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
|
|
|
|
angular.module('copayApp.services')
|
2015-04-24 16:39:12 -03:00
|
|
|
.factory('storageService', function(fileStorageService, localStorageService, sjcl, $log, lodash, isCordova) {
|
2015-03-06 12:00:10 -03:00
|
|
|
|
|
|
|
|
var root = {};
|
2015-04-24 16:39:12 -03:00
|
|
|
var storage = isCordova ? 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.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-03-06 12:00:10 -03:00
|
|
|
if (err || !str) return cb(err);
|
|
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
});
|