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-10-19 17:26:15 -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-10-19 17:26:15 -03:00
|
|
|
root.setCleanAndScanAddresses = function(walletId, cb) {
|
|
|
|
|
storage.set('CleanAndScanAddresses', walletId, cb);
|
2015-06-10 11:12:41 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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-29 22:40:39 -03:00
|
|
|
root.setCopayDisclaimerFlag = function(cb) {
|
2015-06-11 11:59:48 -03:00
|
|
|
storage.set('agreeDisclaimer', true, cb);
|
|
|
|
|
};
|
|
|
|
|
|
2015-06-29 22:40:39 -03:00
|
|
|
root.getCopayDisclaimerFlag = function(cb) {
|
2015-06-11 11:59:48 -03:00
|
|
|
storage.get('agreeDisclaimer', cb);
|
|
|
|
|
};
|
|
|
|
|
|
2015-06-29 22:40:39 -03:00
|
|
|
root.setRemotePrefsStoredFlag = function(cb) {
|
2015-06-29 21:46:34 -03:00
|
|
|
storage.set('remotePrefStored', true, cb);
|
|
|
|
|
};
|
|
|
|
|
|
2015-06-29 22:40:39 -03:00
|
|
|
root.getRemotePrefsStoredFlag = function(cb) {
|
2015-06-29 21:46:34 -03:00
|
|
|
storage.get('remotePrefStored', cb);
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-28 18:23:24 -03:00
|
|
|
root.setGlideraToken = function(network, token, cb) {
|
|
|
|
|
storage.set('glideraToken-' + network, token, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getGlideraToken = function(network, cb) {
|
|
|
|
|
storage.get('glideraToken-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.removeGlideraToken = function(network, cb) {
|
|
|
|
|
storage.remove('glideraToken-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-22 18:43:32 -03:00
|
|
|
root.setAddressbook = function(network, addressbook, cb) {
|
|
|
|
|
storage.set('addressbook-' + network, addressbook, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getAddressbook = function(network, cb) {
|
|
|
|
|
storage.get('addressbook-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.removeAddressbook = function(network, cb) {
|
|
|
|
|
storage.remove('addressbook-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-08 14:29:35 -03:00
|
|
|
root.setTxHistory = function(txs, walletId, cb) {
|
|
|
|
|
storage.set('txsHistory-' + walletId, txs, cb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.getTxHistory = function(walletId, cb) {
|
|
|
|
|
storage.get('txsHistory-' + walletId, cb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.setTxHistoryFlag = function(value, walletId, cb) {
|
|
|
|
|
storage.set('txsHistoryFlag-' + walletId, value, cb);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.getTxHistoryFlag = function(walletId, cb) {
|
|
|
|
|
storage.get('txsHistoryFlag-' + walletId, cb);
|
|
|
|
|
}
|
2015-03-06 12:00:10 -03:00
|
|
|
return root;
|
|
|
|
|
});
|