2015-03-06 12:00:10 -03:00
|
|
|
'use strict';
|
|
|
|
|
angular.module('copayApp.services')
|
2016-11-11 18:11:52 -05:00
|
|
|
.factory('storageService', function(logHeader, fileStorageService, localStorageService, sjcl, $log, lodash, platformInfo, $timeout) {
|
2015-03-06 12:00:10 -03:00
|
|
|
|
|
|
|
|
var root = {};
|
2017-02-21 16:27:51 -05:00
|
|
|
var storage;
|
2015-04-26 11:41:25 -03:00
|
|
|
|
2016-02-24 19:04:29 -03:00
|
|
|
// File storage is not supported for writing according to
|
2015-04-26 11:41:25 -03:00
|
|
|
// https://github.com/apache/cordova-plugin-file/#supported-platforms
|
2016-05-31 14:55:08 -03:00
|
|
|
var shouldUseFileStorage = platformInfo.isCordova && !platformInfo.isWP;
|
2015-04-26 11:41:25 -03:00
|
|
|
|
2017-02-21 16:27:51 -05:00
|
|
|
if (shouldUseFileStorage) {
|
|
|
|
|
$log.debug('Using: FileStorage');
|
|
|
|
|
storage = fileStorageService;
|
|
|
|
|
} else {
|
|
|
|
|
$log.debug('Using: LocalStorage');
|
|
|
|
|
storage = 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);
|
|
|
|
|
};
|
|
|
|
|
|
2017-01-27 17:54:41 -03:00
|
|
|
// This is only used in Copay, we used to encrypt profile
|
|
|
|
|
// using device's UUID.
|
|
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
var decryptOnMobile = function(text, cb) {
|
|
|
|
|
var json;
|
|
|
|
|
try {
|
|
|
|
|
json = JSON.parse(text);
|
2016-06-21 11:47:42 -03:00
|
|
|
} catch (e) {
|
|
|
|
|
$log.warn('Could not open profile:' + text);
|
|
|
|
|
|
|
|
|
|
var i = text.lastIndexOf('}{');
|
|
|
|
|
if (i > 0) {
|
|
|
|
|
text = text.substr(i + 1);
|
|
|
|
|
$log.warn('trying last part only:' + text);
|
|
|
|
|
try {
|
|
|
|
|
json = JSON.parse(text);
|
|
|
|
|
$log.warn('Worked... saving.');
|
|
|
|
|
storage.set('profile', text, function() {});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
$log.warn('Could not open profile (2nd try):' + e);
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
};
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2015-10-28 16:19:16 -03:00
|
|
|
if (!json) return cb('Could not access storage')
|
|
|
|
|
|
2015-11-07 11:43:19 -03:00
|
|
|
if (!json.iter || !json.ct) {
|
|
|
|
|
$log.debug('Profile is not encrypted');
|
2015-03-06 12:00:10 -03:00
|
|
|
return cb(null, text);
|
2015-11-07 11:43:19 -03:00
|
|
|
}
|
2015-03-06 12:00:10 -03:00
|
|
|
|
|
|
|
|
$log.debug('Profile is encrypted');
|
|
|
|
|
getUUID(function(uuid) {
|
2015-10-29 12:09:04 -03:00
|
|
|
$log.debug('Device UUID:' + uuid);
|
2015-03-06 12:00:10 -03:00
|
|
|
if (!uuid)
|
2015-10-28 16:19:16 -03:00
|
|
|
return cb('Could not decrypt storage: could not get device ID');
|
2015-03-06 12:00:10 -03:00
|
|
|
|
2015-10-28 16:19:16 -03:00
|
|
|
try {
|
|
|
|
|
text = sjcl.decrypt(uuid, text);
|
2015-11-07 12:13:06 -03:00
|
|
|
|
|
|
|
|
$log.info('Migrating to unencrypted profile');
|
|
|
|
|
return storage.set('profile', text, function(err) {
|
|
|
|
|
return cb(err, text);
|
|
|
|
|
});
|
2015-11-23 12:17:04 -03:00
|
|
|
} catch (e) {
|
2015-10-29 12:09:04 -03:00
|
|
|
$log.warn('Decrypt error: ', e);
|
2015-10-28 16:19:16 -03:00
|
|
|
return cb('Could not decrypt storage: device ID mismatch');
|
|
|
|
|
};
|
2015-03-06 12:00:10 -03:00
|
|
|
return cb(null, text);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2017-01-27 10:51:51 -03:00
|
|
|
// This is only use in Copay, for very old instalations
|
|
|
|
|
// in which we use to use localStorage instead of fileStorage
|
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) {
|
2016-06-06 12:21:15 -03:00
|
|
|
storage.create('profile', profile.toObj(), cb);
|
2015-03-06 12:00:10 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.storeProfile = function(profile, cb) {
|
2016-06-06 12:21:15 -03:00
|
|
|
storage.set('profile', profile.toObj(), 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-25 14:42:17 -03:00
|
|
|
if (err || !str)
|
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) {
|
2015-11-07 11:43:19 -03:00
|
|
|
$log.debug('Could not read profile:', e);
|
2015-03-06 12:00:10 -03:00
|
|
|
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
|
|
|
};
|
|
|
|
|
|
2016-11-11 17:04:53 -03:00
|
|
|
root.setFeedbackInfo = function(feedbackValues, cb) {
|
|
|
|
|
storage.set('feedback', feedbackValues, cb);
|
2016-11-08 12:37:05 -03:00
|
|
|
};
|
|
|
|
|
|
2016-11-11 17:04:53 -03:00
|
|
|
root.getFeedbackInfo = function(cb) {
|
|
|
|
|
storage.get('feedback', cb);
|
2016-11-08 12:37:05 -03:00
|
|
|
};
|
|
|
|
|
|
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-11-06 16:54:25 -03:00
|
|
|
root.clearBackupFlag = function(walletId, cb) {
|
|
|
|
|
storage.remove('backup-' + walletId, cb);
|
|
|
|
|
};
|
|
|
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-10 13:06:29 -04:00
|
|
|
root.getHomeTipAccepted = function(cb) {
|
|
|
|
|
storage.get('homeTip', cb);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-11 12:22:58 -03:00
|
|
|
root.setHomeTipAccepted = function(val, cb) {
|
|
|
|
|
storage.set('homeTip', val, cb);
|
|
|
|
|
};
|
2016-10-10 13:06:29 -04:00
|
|
|
|
2016-05-13 15:05:43 -03:00
|
|
|
root.setHideBalanceFlag = function(walletId, val, cb) {
|
|
|
|
|
storage.set('hideBalance-' + walletId, val, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getHideBalanceFlag = function(walletId, cb) {
|
|
|
|
|
storage.get('hideBalance-' + walletId, cb);
|
|
|
|
|
};
|
|
|
|
|
|
2015-11-30 16:34:24 -03:00
|
|
|
//for compatibility
|
|
|
|
|
root.getCopayDisclaimerFlag = function(cb) {
|
|
|
|
|
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);
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-14 12:36:28 -03:00
|
|
|
root.setGlideraPermissions = function(network, p, cb) {
|
|
|
|
|
storage.set('glideraPermissions-' + network, p, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getGlideraPermissions = function(network, cb) {
|
|
|
|
|
storage.get('glideraPermissions-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.removeGlideraPermissions = function(network, cb) {
|
|
|
|
|
storage.remove('glideraPermissions-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.setGlideraStatus = function(network, status, cb) {
|
|
|
|
|
storage.set('glideraStatus-' + network, status, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getGlideraStatus = function(network, cb) {
|
|
|
|
|
storage.get('glideraStatus-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.removeGlideraStatus = function(network, cb) {
|
|
|
|
|
storage.remove('glideraStatus-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
2017-02-15 12:20:36 -03:00
|
|
|
root.setGlideraTxs = function(network, txs, cb) {
|
|
|
|
|
storage.set('glideraTxs-' + network, txs, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getGlideraTxs = function(network, cb) {
|
|
|
|
|
storage.get('glideraTxs-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.removeGlideraTxs = function(network, cb) {
|
|
|
|
|
storage.remove('glideraTxs-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-13 14:08:03 -03:00
|
|
|
root.setCoinbaseRefreshToken = function(network, token, cb) {
|
|
|
|
|
storage.set('coinbaseRefreshToken-' + network, token, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getCoinbaseRefreshToken = function(network, cb) {
|
|
|
|
|
storage.get('coinbaseRefreshToken-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-29 10:27:46 -03:00
|
|
|
root.removeCoinbaseRefreshToken = function(network, cb) {
|
|
|
|
|
storage.remove('coinbaseRefreshToken-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-13 14:08:03 -03:00
|
|
|
root.setCoinbaseToken = function(network, token, cb) {
|
|
|
|
|
storage.set('coinbaseToken-' + network, token, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getCoinbaseToken = function(network, cb) {
|
|
|
|
|
storage.get('coinbaseToken-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.removeCoinbaseToken = function(network, cb) {
|
|
|
|
|
storage.remove('coinbaseToken-' + 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);
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-26 11:11:14 -03:00
|
|
|
root.setNextStep = function(service, status, cb) {
|
|
|
|
|
storage.set('nextStep-' + service, status, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getNextStep = function(service, cb) {
|
|
|
|
|
storage.get('nextStep-' + service, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.removeNextStep = function(service, cb) {
|
|
|
|
|
storage.remove('nextStep-' + service, cb);
|
|
|
|
|
};
|
|
|
|
|
|
2016-12-13 16:32:22 -03:00
|
|
|
root.setLastCurrencyUsed = function(lastCurrencyUsed, cb) {
|
|
|
|
|
storage.set('lastCurrencyUsed', lastCurrencyUsed, cb)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getLastCurrencyUsed = function(cb) {
|
|
|
|
|
storage.get('lastCurrencyUsed', cb)
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-27 22:31:22 -03:00
|
|
|
root.checkQuota = function() {
|
|
|
|
|
var block = '';
|
|
|
|
|
// 50MB
|
2016-07-29 12:46:41 -03:00
|
|
|
for (var i = 0; i < 1024 * 1024; ++i) {
|
2016-07-27 22:31:22 -03:00
|
|
|
block += '12345678901234567890123456789012345678901234567890';
|
|
|
|
|
}
|
|
|
|
|
storage.set('test', block, function(err) {
|
2016-07-29 12:46:41 -03:00
|
|
|
$log.error('CheckQuota Return:' + err);
|
2016-07-27 22:31:22 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-10-08 14:29:35 -03:00
|
|
|
root.setTxHistory = function(txs, walletId, cb) {
|
2016-07-27 22:31:22 -03:00
|
|
|
try {
|
|
|
|
|
storage.set('txsHistory-' + walletId, txs, cb);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
$log.error('Error saving tx History. Size:' + txs.length);
|
|
|
|
|
$log.error(e);
|
|
|
|
|
return cb(e);
|
|
|
|
|
}
|
2015-10-08 14:29:35 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.getTxHistory = function(walletId, cb) {
|
|
|
|
|
storage.get('txsHistory-' + walletId, cb);
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-13 16:11:07 -03:00
|
|
|
root.removeTxHistory = function(walletId, cb) {
|
|
|
|
|
storage.remove('txsHistory-' + walletId, cb);
|
2015-10-08 14:29:35 -03:00
|
|
|
}
|
|
|
|
|
|
2016-04-13 14:08:03 -03:00
|
|
|
root.setCoinbaseTxs = function(network, ctx, cb) {
|
|
|
|
|
storage.set('coinbaseTxs-' + network, ctx, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getCoinbaseTxs = function(network, cb) {
|
|
|
|
|
storage.get('coinbaseTxs-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.removeCoinbaseTxs = function(network, cb) {
|
|
|
|
|
storage.remove('coinbaseTxs-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
2017-01-31 14:24:13 -03:00
|
|
|
root.setBalanceCache = function(cardId, data, cb) {
|
2017-01-31 16:59:27 -03:00
|
|
|
storage.set('balanceCache-' + cardId, data, cb);
|
2016-08-10 15:29:31 -03:00
|
|
|
};
|
|
|
|
|
|
2017-01-31 14:24:13 -03:00
|
|
|
root.getBalanceCache = function(cardId, cb) {
|
2017-01-31 16:59:27 -03:00
|
|
|
storage.get('balanceCache-' + cardId, cb);
|
2016-08-10 15:29:31 -03:00
|
|
|
};
|
|
|
|
|
|
2017-01-31 16:59:27 -03:00
|
|
|
root.removeBalanceCache = function(cardId, cb) {
|
|
|
|
|
storage.remove('balanceCache-' + cardId, cb);
|
2016-08-10 15:29:31 -03:00
|
|
|
};
|
|
|
|
|
|
2016-12-23 11:37:19 -05:00
|
|
|
// cards: [
|
|
|
|
|
// eid: card id
|
|
|
|
|
// id: card id
|
|
|
|
|
// lastFourDigits: card number
|
|
|
|
|
// token: card token
|
|
|
|
|
// ]
|
2017-01-27 17:54:41 -03:00
|
|
|
root.setBitpayDebitCards = function(network, email, cards, cb) {
|
|
|
|
|
|
|
|
|
|
root.getBitpayAccounts(network, function(err, allAccounts) {
|
2016-11-11 18:11:52 -05:00
|
|
|
if (err) return cb(err);
|
2017-01-27 17:54:41 -03:00
|
|
|
|
|
|
|
|
if (!allAccounts[email]) {
|
|
|
|
|
return cb('Cannot set cards for unknown account ' + email);
|
2016-11-16 10:11:23 -05:00
|
|
|
}
|
2017-01-27 17:54:41 -03:00
|
|
|
|
|
|
|
|
allAccounts[email].cards = cards;
|
|
|
|
|
storage.set('bitpayAccounts-v2-' + network, allAccounts, cb);
|
2016-11-11 18:11:52 -05:00
|
|
|
});
|
2016-09-28 21:09:41 -03:00
|
|
|
};
|
|
|
|
|
|
2016-12-23 11:37:19 -05:00
|
|
|
// cb(err, cards)
|
|
|
|
|
// cards: [
|
|
|
|
|
// eid: card id
|
|
|
|
|
// id: card id
|
|
|
|
|
// lastFourDigits: card number
|
|
|
|
|
// token: card token
|
|
|
|
|
// email: account email
|
|
|
|
|
// ]
|
2016-10-06 19:23:39 -03:00
|
|
|
root.getBitpayDebitCards = function(network, cb) {
|
2017-01-27 17:54:41 -03:00
|
|
|
|
|
|
|
|
root.getBitpayAccounts(network, function(err, allAccounts) {
|
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
|
|
var allCards = [];
|
|
|
|
|
|
2017-02-01 11:36:05 -03:00
|
|
|
lodash.each(allAccounts, function(account, email) {
|
2017-01-27 17:54:41 -03:00
|
|
|
|
2017-02-01 11:36:05 -03:00
|
|
|
// Add account's email to card list, for convenience
|
2017-01-27 17:54:41 -03:00
|
|
|
var cards = lodash.clone(account.cards);
|
2017-02-01 11:36:05 -03:00
|
|
|
lodash.each(cards, function(x) {
|
|
|
|
|
x.email = email;
|
2017-01-27 17:54:41 -03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
allCards = allCards.concat(cards);
|
2016-11-11 18:11:52 -05:00
|
|
|
});
|
2017-01-27 17:54:41 -03:00
|
|
|
|
|
|
|
|
return cb(null, allCards);
|
2016-11-11 18:11:52 -05:00
|
|
|
});
|
2016-09-28 21:09:41 -03:00
|
|
|
};
|
|
|
|
|
|
2016-12-23 11:37:19 -05:00
|
|
|
// card: {
|
|
|
|
|
// eid: card id
|
|
|
|
|
// id: card id
|
|
|
|
|
// lastFourDigits: card number
|
|
|
|
|
// token: card token
|
|
|
|
|
// }
|
2017-01-27 17:54:41 -03:00
|
|
|
root.removeBitpayDebitCard = function(network, cardEid, cb) {
|
|
|
|
|
|
2017-02-07 16:13:28 -05:00
|
|
|
root.getBitpayAccounts(network, function(err, allAccounts) {
|
2017-01-27 17:54:41 -03:00
|
|
|
|
2017-02-07 16:13:28 -05:00
|
|
|
lodash.each(allAccounts, function(account) {
|
2017-01-27 17:54:41 -03:00
|
|
|
account.cards = lodash.reject(account.cards, {
|
|
|
|
|
'eid': cardEid
|
2016-11-11 18:11:52 -05:00
|
|
|
});
|
|
|
|
|
});
|
2016-10-10 18:25:07 -03:00
|
|
|
|
2017-01-27 17:54:41 -03:00
|
|
|
storage.set('bitpayAccounts-v2-' + network, allAccounts, cb);
|
2016-12-23 11:37:19 -05:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// cb(err, accounts)
|
|
|
|
|
// accounts: {
|
|
|
|
|
// email_1: {
|
2017-01-27 17:54:41 -03:00
|
|
|
// token: account token
|
|
|
|
|
// cards: {
|
2016-12-23 11:37:19 -05:00
|
|
|
// <card-data>
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// ...
|
|
|
|
|
// email_n: {
|
2017-01-27 17:54:41 -03:00
|
|
|
// token: account token
|
|
|
|
|
// cards: {
|
2016-12-23 11:37:19 -05:00
|
|
|
// <card-data>
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2017-01-27 17:54:41 -03:00
|
|
|
//
|
2016-12-23 11:37:19 -05:00
|
|
|
root.getBitpayAccounts = function(network, cb) {
|
2017-01-27 17:54:41 -03:00
|
|
|
storage.get('bitpayAccounts-v2-' + network, function(err, allAccountsStr) {
|
2016-12-23 11:37:19 -05:00
|
|
|
if (err) return cb(err);
|
2017-01-27 17:54:41 -03:00
|
|
|
|
2017-02-21 16:27:51 -05:00
|
|
|
if (!allAccountsStr)
|
2017-02-07 16:16:21 -05:00
|
|
|
return cb(null, {});
|
|
|
|
|
|
2017-01-27 17:54:41 -03:00
|
|
|
var allAccounts = {};
|
|
|
|
|
try {
|
|
|
|
|
allAccounts = JSON.parse(allAccountsStr);
|
2017-02-07 16:13:28 -05:00
|
|
|
} catch (e) {
|
|
|
|
|
$log.error('Bad storage value for bitpayAccount-v2' + allAccountsStr)
|
|
|
|
|
return cb(null, {});
|
|
|
|
|
};
|
2017-01-27 17:54:41 -03:00
|
|
|
|
2017-01-31 16:59:27 -03:00
|
|
|
var anyMigration;
|
|
|
|
|
|
2017-01-27 17:54:41 -03:00
|
|
|
lodash.each(allAccounts, function(account, email) {
|
|
|
|
|
|
|
|
|
|
// Migrate old `'bitpayApi-' + network` key, if exists
|
|
|
|
|
if (!account.token && account['bitpayApi-' + network].token) {
|
2017-01-31 16:59:27 -03:00
|
|
|
|
2017-01-27 17:54:41 -03:00
|
|
|
$log.info('Migrating all bitpayApi-network branch');
|
2017-01-31 16:59:27 -03:00
|
|
|
account.token = account['bitpayApi-' + network].token;
|
|
|
|
|
account.cards = lodash.clone(account['bitpayApi-' + network].cards);
|
|
|
|
|
if (!account.cards) {
|
|
|
|
|
account.cards = lodash.clone(account['bitpayDebitCards-' + network]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete account['bitpayDebitCards-' + network];
|
2017-01-27 17:54:41 -03:00
|
|
|
delete account['bitpayApi-' + network];
|
2017-01-31 16:59:27 -03:00
|
|
|
anyMigration = true;
|
|
|
|
|
|
2017-01-27 17:54:41 -03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2017-01-31 16:59:27 -03:00
|
|
|
if (anyMigration) {
|
2017-02-07 16:13:28 -05:00
|
|
|
storage.set('bitpayAccounts-v2-' + network, allAccounts, function() {
|
2017-01-31 16:59:27 -03:00
|
|
|
return cb(err, allAccounts);
|
|
|
|
|
});
|
2017-02-07 16:13:28 -05:00
|
|
|
} else
|
2017-01-31 16:59:27 -03:00
|
|
|
return cb(err, allAccounts);
|
|
|
|
|
|
2017-01-27 17:54:41 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// data: {
|
|
|
|
|
// email: account email
|
|
|
|
|
// token: account token
|
|
|
|
|
// }
|
|
|
|
|
root.setBitpayAccount = function(network, data, cb) {
|
|
|
|
|
|
|
|
|
|
if (!lodash.isObject(data) || !data.email || !data.token)
|
|
|
|
|
return cb('No account to set');
|
|
|
|
|
|
|
|
|
|
var email = data.email;
|
|
|
|
|
var token = data.token;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
root.getBitpayAccounts(network, function(err, allAccounts) {
|
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
|
|
var account = allAccounts[email] || {};
|
|
|
|
|
account.token = token;
|
|
|
|
|
|
|
|
|
|
allAccounts[email] = account;
|
|
|
|
|
|
|
|
|
|
$log.info('Storing BitPay accounts with new account:' + email);
|
|
|
|
|
storage.set('bitpayAccounts-v2-' + network, allAccounts, cb);
|
2016-12-23 11:37:19 -05:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-14 16:24:52 -05:00
|
|
|
root.setAppIdentity = function(network, data, cb) {
|
|
|
|
|
storage.set('appIdentity-' + network, data, cb);
|
2016-10-10 18:25:07 -03:00
|
|
|
};
|
|
|
|
|
|
2016-11-14 16:24:52 -05:00
|
|
|
root.getAppIdentity = function(network, cb) {
|
2017-01-05 16:29:17 -05:00
|
|
|
storage.get('appIdentity-' + network, function(err, data) {
|
|
|
|
|
if (err) return cb(err);
|
2017-01-27 17:54:41 -03:00
|
|
|
cb(err, JSON.parse(data || '{}'));
|
2017-01-05 16:29:17 -05:00
|
|
|
});
|
2016-10-10 18:25:07 -03:00
|
|
|
};
|
|
|
|
|
|
2016-11-14 16:24:52 -05:00
|
|
|
root.removeAppIdentity = function(network, cb) {
|
|
|
|
|
storage.remove('appIdentity-' + network, cb);
|
2016-09-28 21:09:41 -03:00
|
|
|
};
|
|
|
|
|
|
2016-06-06 12:21:15 -03:00
|
|
|
root.removeAllWalletData = function(walletId, cb) {
|
2016-06-06 19:09:57 -03:00
|
|
|
root.clearLastAddress(walletId, function(err) {
|
2016-06-06 12:21:15 -03:00
|
|
|
if (err) return cb(err);
|
2016-06-06 19:09:57 -03:00
|
|
|
root.removeTxHistory(walletId, function(err) {
|
2016-06-06 12:21:15 -03:00
|
|
|
if (err) return cb(err);
|
2016-06-06 19:09:57 -03:00
|
|
|
root.clearBackupFlag(walletId, function(err) {
|
2016-06-06 12:21:15 -03:00
|
|
|
return cb(err);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
2016-07-21 11:18:48 -03:00
|
|
|
|
2016-05-13 11:48:48 -03:00
|
|
|
root.setAmazonGiftCards = function(network, gcs, cb) {
|
|
|
|
|
storage.set('amazonGiftCards-' + network, gcs, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getAmazonGiftCards = function(network, cb) {
|
|
|
|
|
storage.get('amazonGiftCards-' + network, cb);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.removeAmazonGiftCards = function(network, cb) {
|
|
|
|
|
storage.remove('amazonGiftCards-' + network, cb);
|
|
|
|
|
};
|
2016-06-06 12:21:15 -03:00
|
|
|
|
2015-03-06 12:00:10 -03:00
|
|
|
return root;
|
|
|
|
|
});
|