Wallet/src/js/services/storageService.js

763 lines
24 KiB
JavaScript
Raw Normal View History

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 = {};
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
var shouldUseFileStorage = platformInfo.isCordova && !platformInfo.isWP;
2015-04-26 11:41:25 -03:00
$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 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
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)
return cb('Could not decrypt storage: could not get device ID');
2015-03-06 12:00:10 -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);
return cb('Could not decrypt storage: device ID mismatch');
};
2015-03-06 12:00:10 -03:00
return cb(null, text);
});
};
2016-11-11 18:11:52 -05:00
////////////////////////////////////////////////////////////////////////////
//
// UPGRADING STORAGE
//
// TODO: prior to upgrading storage a backup of all storage should be captured and made available
// for export for recovery/debugging.
//
// Upgraders are executed in numerical order per the '##_' object key prefix. Each upgrader will run.
// Each upgrader should detect storage configuraton and fail-safe; no upgrader should damage the ability
// of another to function properly (in order). Each upgrader should upgrade storage incrementally; storage
// upgrade is not complete until all upgraders have run.
//
2016-11-11 18:11:52 -05:00
// 1. Write a function to upgrade the desired storage key(s). The function should have the protocol:
//
2016-11-11 18:11:52 -05:00
// _upgrade_x(key, network, cb), where:
//
2016-11-11 18:11:52 -05:00
// `x` is the name of the storage key
// `key` is the name of the storage key being upgraded
2016-11-11 18:11:52 -05:00
// `network` is one of 'livenet', 'testnet'
//
// 2. Add the storage key to `_upgraders` object using the name of the key as the `_upgrader` object key
// with the value being the name of the upgrade function (e.g., _upgrade_x). In order to avoid conflicts
// when a storage key is involved in multiple upgraders as well as predicte the order in which upgrades
// occur the `_upgrader` object key should be prefixed with '##_' (e.g., '01_') to create a unique and
2016-11-11 18:11:52 -05:00
// sortable name. This format is interpreted by the _upgrade() function.
//
// 3. Any dependency functions called by upgraders should be copied/factored out and remain unchanged as
// long as the upgrader remains in effect. By convention the dependency function is prefixed by '##_' to
// match the upgrader key.
//
2016-11-11 18:11:52 -05:00
var _upgraders = {
'00_bitpayDebitCards' : _upgrade_bitpayDebitCards, // 2016-11: Upgrade bitpayDebitCards-x to bitpayAccounts-x
'01_bitpayCardCredentials' : _upgrade_bitpayCardCredentials, // 2016-11: Upgrade bitpayCardCredentials-x to appIdentity-x
'02_bitpayAccounts' : _upgrade_bitpayAccounts // 2016-12: Reorganize the object
2016-11-11 18:11:52 -05:00
};
function _upgrade_bitpayDebitCards(key, network, cb) {
key += '-' + network;
storage.get(key, function(err, data) {
if (err) return cb(err);
if (data != null) {
// Needs upgrade
if (lodash.isString(data)) {
data = JSON.parse(data);
}
data = data || {};
_00_setBitpayDebitCards(network, data, function(err) {
2016-11-11 18:11:52 -05:00
if (err) return cb(err);
storage.remove(key, function() {
cb(null, 'replaced with \'bitpayAccounts\'');
});
});
} else {
cb();
}
});
};
function _upgrade_bitpayCardCredentials(key, network, cb) {
key += '-' + network;
storage.get(key, function(err, data) {
if (err) return cb(err);
if (data != null) {
// Needs upgrade
_01_setAppIdentity(network, data, function(err) {
if (err) return cb(err);
storage.remove(key, function() {
cb(null, 'replaced with \'appIdentity\'');
});
});
} else {
cb();
}
});
};
function _upgrade_bitpayAccounts(key, network, cb) {
key += '-' + network;
storage.get(key, function(err, data) {
if (err) return cb(err);
if (lodash.isString(data)) {
data = JSON.parse(data);
}
data = data || {};
var upgraded = '';
Object.keys(data).forEach(function(key) {
// Keys are account emails
if (!data[key]['bitpayApi-' + network].token) {
// Needs upgrade
upgraded += ' ' + key;
var acctData = {
token: data[key]['bitpayDebitCards-' + network].token,
email: key
};
_02_setBitpayAccount(network, data, function(err) {
if (err) return cb(err);
_02_setBitpayDebitCards(network, data[key]['bitpayDebitCards-' + network], function(err) {
if (err) return cb(err);
});
});
}
});
if (upgraded.length > 0) {
cb(null, 'upgraded \'bitpayAccounts\':' + upgraded);
} else {
cb();
}
});
};
//
////////////////////////////////////////////////////////////////////////////
//
// UPGRADER DEPENDENCIES
// These functions remain as long as the upgrader remains in effect.
//
var _00_setBitpayDebitCards = function(network, data, cb) {
if (lodash.isString(data)) {
data = JSON.parse(data);
}
data = data || {};
if (lodash.isEmpty(data) || !data.email) return cb('No card(s) to set');
storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) {
if (err) return cb(err);
if (lodash.isString(bitpayAccounts)) {
bitpayAccounts = JSON.parse(bitpayAccounts);
}
bitpayAccounts = bitpayAccounts || {};
bitpayAccounts[data.email] = bitpayAccounts[data.email] || {};
bitpayAccounts[data.email]['bitpayDebitCards-' + network] = data;
storage.set('bitpayAccounts-' + network, JSON.stringify(bitpayAccounts), cb);
});
};
var _01_setAppIdentity = function(network, data, cb) {
storage.set('appIdentity-' + network, data, cb);
};
var _02_setBitpayAccount = function(network, data, cb) {
if (lodash.isString(data)) {
data = JSON.parse(data);
}
data = data || {};
if (lodash.isEmpty(data) || !data.email) return cb('No account to set');
storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) {
if (err) return cb(err);
if (lodash.isString(bitpayAccounts)) {
bitpayAccounts = JSON.parse(bitpayAccounts);
}
bitpayAccounts = bitpayAccounts || {};
bitpayAccounts[data.email] = bitpayAccounts[data.email] || {};
bitpayAccounts[data.email]['bitpayApi-' + network] = bitpayAccounts[data.email]['bitpayApi-' + network] || {};
bitpayAccounts[data.email]['bitpayApi-' + network].token = data.token;
storage.set('bitpayAccounts-' + network, JSON.stringify(bitpayAccounts), cb);
});
};
var _02_setBitpayDebitCards = function(network, data, cb) {
if (lodash.isString(data)) {
data = JSON.parse(data);
}
data = data || {};
if (lodash.isEmpty(data) || !data.email) return cb('No card(s) to set');
storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) {
if (err) return cb(err);
if (lodash.isString(bitpayAccounts)) {
bitpayAccounts = JSON.parse(bitpayAccounts);
}
bitpayAccounts = bitpayAccounts || {};
bitpayAccounts[data.email] = bitpayAccounts[data.email] || {};
bitpayAccounts[data.email]['bitpayDebitCards-' + network] = data.cards;
storage.set('bitpayAccounts-' + network, JSON.stringify(bitpayAccounts), cb);
});
};
2016-11-11 18:11:52 -05:00
//
////////////////////////////////////////////////////////////////////////////
// IMPORTANT: This function is designed to block execution until it completes.
// Ideally storage should not be used until it has been verified.
root.verify = function(cb) {
_upgrade(function(err) {
cb(err);
});
};
function _handleUpgradeError(key, err) {
$log.error('Failed to upgrade storage for \'' + key + '\': ' + err);
};
2016-11-11 18:11:52 -05:00
function _handleUpgradeSuccess(key, msg) {
$log.info('Storage upgraded for \'' + key + '\': ' + msg);
};
function _upgrade(cb) {
var errorCount = 0;
var errorMessage = undefined;
var keys = Object.keys(_upgraders).sort();
var networks = ['livenet', 'testnet'];
keys.forEach(function(key) {
networks.forEach(function(network) {
var storagekey = key.split('_')[1];
_upgraders[key](storagekey, network, function(err, msg) {
if (err) {
_handleUpgradeError(storagekey, err);
errorCount++;
errorMessage = errorCount + ' storage upgrade failures';
}
if (msg) _handleUpgradeSuccess(storagekey, msg);
});
});
});
cb(errorMessage);
};
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) {
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) {
storage.get('profile', function(err, str) {
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) {
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) {
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
};
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);
};
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);
};
root.getHomeTipAccepted = function(cb) {
storage.get('homeTip', cb);
};
root.setHomeTipAccepted = function(val, cb) {
storage.set('homeTip', val, cb);
};
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);
};
//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);
};
Feat/coinbase integration (#4012) * Oauth2 and first view * Connect with Coinbase using mobile * Buy and Sell through Coinbase * Fix buy * Receive and send bitcoin to Coinbase account * Receive bitcoin from Coinbase to Copay * Complete user and account information. Connection errors * Improves error handler * Removes console.log * Coinbase background color. Send to Coinbase form validation * Fix send from different wallet * Send and receive using Coinbase * Pagination activity * Fix Buy and Sell * One option in the sidebar to Buy and Sell * Native balance on Coinbase homepage * Rename receive and send * Auto-close window after authenticate * Reorder * Get payment methods * Fix when token expired * Fix token expired * Integration: sell and send to Coinbase * Store pending transaction before sell * Sell flow completed * Removing files * Fix sell * Fix sell * Fix sell * Sell completed * Buy bitcoin through coinbase * Buy auto * Currency set to USD * Select payment methods. Limits * Removes payment methods from preferences * Fix signs. Tx ordered by updated. Minor fixes * Removes console.log * Improving ux-language things * Fix selectedpaymentmethod if not verified * Set error if tx not found * Price sensitivity. Minor fixes * Adds coinbase api key to gitignore * Coinbase production ready * Fix sell in usd * Bug fixes * New Sensitivity step * Refresh token with a simple click * Refresh token * Refactor * Fix auto reconnect if token expired Signed-off-by: Gustavo Maximiliano Cortez <cmgustavo83@gmail.com> * Fix calls if token expired
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);
};
root.removeCoinbaseRefreshToken = function(network, cb) {
storage.remove('coinbaseRefreshToken-' + network, cb);
};
Feat/coinbase integration (#4012) * Oauth2 and first view * Connect with Coinbase using mobile * Buy and Sell through Coinbase * Fix buy * Receive and send bitcoin to Coinbase account * Receive bitcoin from Coinbase to Copay * Complete user and account information. Connection errors * Improves error handler * Removes console.log * Coinbase background color. Send to Coinbase form validation * Fix send from different wallet * Send and receive using Coinbase * Pagination activity * Fix Buy and Sell * One option in the sidebar to Buy and Sell * Native balance on Coinbase homepage * Rename receive and send * Auto-close window after authenticate * Reorder * Get payment methods * Fix when token expired * Fix token expired * Integration: sell and send to Coinbase * Store pending transaction before sell * Sell flow completed * Removing files * Fix sell * Fix sell * Fix sell * Sell completed * Buy bitcoin through coinbase * Buy auto * Currency set to USD * Select payment methods. Limits * Removes payment methods from preferences * Fix signs. Tx ordered by updated. Minor fixes * Removes console.log * Improving ux-language things * Fix selectedpaymentmethod if not verified * Set error if tx not found * Price sensitivity. Minor fixes * Adds coinbase api key to gitignore * Coinbase production ready * Fix sell in usd * Bug fixes * New Sensitivity step * Refresh token with a simple click * Refresh token * Refactor * Fix auto reconnect if token expired Signed-off-by: Gustavo Maximiliano Cortez <cmgustavo83@gmail.com> * Fix calls if token expired
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);
};
root.setLastCurrencyUsed = function(lastCurrencyUsed, cb) {
storage.set('lastCurrencyUsed', lastCurrencyUsed, cb)
};
root.getLastCurrencyUsed = function(cb) {
storage.get('lastCurrencyUsed', cb)
};
root.checkQuota = function() {
var block = '';
// 50MB
2016-07-29 12:46:41 -03:00
for (var i = 0; i < 1024 * 1024; ++i) {
block += '12345678901234567890123456789012345678901234567890';
}
storage.set('test', block, function(err) {
2016-07-29 12:46:41 -03:00
$log.error('CheckQuota Return:' + err);
});
};
2015-10-08 14:29:35 -03:00
root.setTxHistory = function(txs, walletId, cb) {
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);
}
root.removeTxHistory = function(walletId, cb) {
storage.remove('txsHistory-' + walletId, cb);
2015-10-08 14:29:35 -03:00
}
Feat/coinbase integration (#4012) * Oauth2 and first view * Connect with Coinbase using mobile * Buy and Sell through Coinbase * Fix buy * Receive and send bitcoin to Coinbase account * Receive bitcoin from Coinbase to Copay * Complete user and account information. Connection errors * Improves error handler * Removes console.log * Coinbase background color. Send to Coinbase form validation * Fix send from different wallet * Send and receive using Coinbase * Pagination activity * Fix Buy and Sell * One option in the sidebar to Buy and Sell * Native balance on Coinbase homepage * Rename receive and send * Auto-close window after authenticate * Reorder * Get payment methods * Fix when token expired * Fix token expired * Integration: sell and send to Coinbase * Store pending transaction before sell * Sell flow completed * Removing files * Fix sell * Fix sell * Fix sell * Sell completed * Buy bitcoin through coinbase * Buy auto * Currency set to USD * Select payment methods. Limits * Removes payment methods from preferences * Fix signs. Tx ordered by updated. Minor fixes * Removes console.log * Improving ux-language things * Fix selectedpaymentmethod if not verified * Set error if tx not found * Price sensitivity. Minor fixes * Adds coinbase api key to gitignore * Coinbase production ready * Fix sell in usd * Bug fixes * New Sensitivity step * Refresh token with a simple click * Refresh token * Refactor * Fix auto reconnect if token expired Signed-off-by: Gustavo Maximiliano Cortez <cmgustavo83@gmail.com> * Fix calls if token expired
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);
};
2016-10-07 13:51:55 -03:00
root.setBitpayDebitCardsHistory = function(network, data, cb) {
storage.set('bitpayDebitCardsHistory-' + network, data, cb);
2016-08-10 15:29:31 -03:00
};
2016-10-07 13:51:55 -03:00
root.getBitpayDebitCardsHistory = function(network, cb) {
storage.get('bitpayDebitCardsHistory-' + network, cb);
2016-08-10 15:29:31 -03:00
};
2016-11-11 18:11:52 -05:00
root.removeBitpayDebitCardHistory = function(network, card, cb) {
root.getBitpayDebitCardsHistory(network, function(err, data) {
if (err) return cb(err);
if (lodash.isString(data)) {
data = JSON.parse(data);
}
data = data || {};
delete data[card.eid];
root.setBitpayDebitCardsHistory(network, JSON.stringify(data), cb);
});
2016-08-10 15:29:31 -03:00
};
// data: {
// cards: [
// eid: card id
// id: card id
// lastFourDigits: card number
// token: card token
// ]
// email: account email
// token: account token
// }
2016-10-06 19:23:39 -03:00
root.setBitpayDebitCards = function(network, data, cb) {
2016-11-11 18:11:52 -05:00
if (lodash.isString(data)) {
data = JSON.parse(data);
}
data = data || {};
if (lodash.isEmpty(data) || !data.email) return cb('No card(s) to set');
storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) {
if (err) return cb(err);
if (lodash.isString(bitpayAccounts)) {
bitpayAccounts = JSON.parse(bitpayAccounts);
}
bitpayAccounts = bitpayAccounts || {};
2016-11-11 18:11:52 -05:00
bitpayAccounts[data.email] = bitpayAccounts[data.email] || {};
bitpayAccounts[data.email]['bitpayDebitCards-' + network] = data.cards;
2016-11-11 18:11:52 -05:00
storage.set('bitpayAccounts-' + network, JSON.stringify(bitpayAccounts), cb);
});
2016-09-28 21:09:41 -03: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) {
2016-11-11 18:11:52 -05:00
storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) {
if (lodash.isString(bitpayAccounts)) {
bitpayAccounts = JSON.parse(bitpayAccounts);
}
bitpayAccounts = bitpayAccounts || {};
2016-11-11 18:11:52 -05:00
var cards = [];
Object.keys(bitpayAccounts).forEach(function(email) {
// For the UI, add the account email to the card object.
var acctCards = bitpayAccounts[email]['bitpayDebitCards-' + network] || [];
2016-11-11 18:11:52 -05:00
for (var i = 0; i < acctCards.length; i++) {
acctCards[i].email = email;
}
cards = cards.concat(acctCards);
});
cb(err, cards);
});
2016-09-28 21:09:41 -03:00
};
// card: {
// eid: card id
// id: card id
// lastFourDigits: card number
// token: card token
// }
2016-11-11 18:11:52 -05:00
root.removeBitpayDebitCard = function(network, card, cb) {
if (lodash.isString(card)) {
card = JSON.parse(card);
}
card = card || {};
if (lodash.isEmpty(card) || !card.eid) return cb('No card to remove');
storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) {
if (err) cb(err);
if (lodash.isString(bitpayAccounts)) {
bitpayAccounts = JSON.parse(bitpayAccounts);
}
bitpayAccounts = bitpayAccounts || {};
Object.keys(bitpayAccounts).forEach(function(email) {
var data = bitpayAccounts[email]['bitpayDebitCards-' + network];
var newCards = lodash.reject(data, {
'eid': card.eid
});
data = {};
2016-11-11 18:11:52 -05:00
data.cards = newCards;
data.email = email;
2016-11-11 18:11:52 -05:00
root.setBitpayDebitCards(network, data, function(err) {
if (err) cb(err);
// If there are no more cards in storage then re-enable the next step entry.
root.getBitpayDebitCards(network, function(err, cards) {
2016-11-11 18:11:52 -05:00
if (err) cb(err);
if (cards.length == 0) {
root.removeNextStep('BitpayCard', cb);
} else {
cb();
}
});
});
});
});
2016-10-10 18:25:07 -03:00
};
// data: {
// email: account email
// token: account token
// }
root.setBitpayAccount = function(network, data, cb) {
if (lodash.isString(data)) {
data = JSON.parse(data);
}
data = data || {};
if (lodash.isEmpty(data) || !data.email) return cb('No account to set');
storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) {
if (err) return cb(err);
if (lodash.isString(bitpayAccounts)) {
bitpayAccounts = JSON.parse(bitpayAccounts);
}
bitpayAccounts = bitpayAccounts || {};
bitpayAccounts[data.email] = bitpayAccounts[data.email] || {};
bitpayAccounts[data.email]['bitpayApi-' + network] = bitpayAccounts[data.email]['bitpayApi-' + network] || {};
bitpayAccounts[data.email]['bitpayApi-' + network].token = data.token;
storage.set('bitpayAccounts-' + network, JSON.stringify(bitpayAccounts), cb);
});
};
// cb(err, accounts)
// accounts: {
// email_1: {
// bitpayApi-<network>: {
// token: account token
// }
// bitpayDebitCards-<network>: {
// <card-data>
// }
// }
// ...
// email_n: {
// bitpayApi-<network>: {
// token: account token
// }
// bitpayDebitCards-<network>: {
// <card-data>
// }
// }
// }
root.getBitpayAccounts = function(network, cb) {
storage.get('bitpayAccounts-' + network, function(err, bitpayAccounts) {
if (err) return cb(err);
if (lodash.isString(bitpayAccounts)) {
bitpayAccounts = JSON.parse(bitpayAccounts);
}
cb(err, bitpayAccounts);
});
};
root.setAppIdentity = function(network, data, cb) {
storage.set('appIdentity-' + network, data, cb);
2016-10-10 18:25:07 -03:00
};
root.getAppIdentity = function(network, cb) {
storage.get('appIdentity-' + network, cb);
2016-10-10 18:25:07 -03: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
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;
});