Wallet/src/js/services/configService.js

118 lines
2.6 KiB
JavaScript

'use strict';
angular.module('copayApp.services').factory('configService', function(storageService, lodash, $log) {
var root = {};
var defaultConfig = {
// wallet limits
limits: {
totalCopayers: 6,
mPlusN: 100,
},
// Bitcore wallet service URL
bws: {
url: 'https://bws.bitpay.com/bws/api',
},
// wallet default config
wallet: {
requiredCopayers: 2,
totalCopayers: 3,
spendUnconfirmed: true,
reconnectDelay: 5000,
idleDurationMin: 4,
settings: {
unitName: 'bits',
unitToSatoshi: 100,
unitDecimals: 2,
unitCode: 'bit',
alternativeName: 'US Dollar',
alternativeIsoCode: 'USD',
}
},
// External services
glidera: {
enabled: true,
testnet: false
},
rates: {
url: 'https://insight.bitpay.com:443/api/rates',
},
bwsFor: {
},
};
var configCache = null;
root.getSync = function() {
if (!configCache)
throw new Error('configService#getSync called when cache is not initialized');
return configCache;
};
root.get = function(cb) {
storageService.getConfig(function(err, localConfig) {
if (localConfig) {
configCache = JSON.parse(localConfig);
//these ifs are to avoid migration problems
if (!configCache.bws) {
configCache.bws = defaultConfig.bws;
}
if (!configCache.wallet) {
configCache.wallet = defaultConfig.wallet;
}
if (!configCache.wallet.settings.unitCode) {
configCache.wallet.settings.unitCode = defaultConfig.wallet.settings.unitCode;
}
if (!configCache.glidera) {
configCache.glidera = defaultConfig.glidera;
}
} else {
configCache = lodash.clone(defaultConfig);
};
$log.debug('Preferences read:', configCache)
return cb(err, configCache);
});
};
root.set = function(newOpts, cb) {
var config = defaultConfig;
storageService.getConfig(function(err, oldOpts) {
if (lodash.isString(oldOpts)) {
oldOpts = JSON.parse(oldOpts);
}
if (lodash.isString(config)) {
config = JSON.parse(config);
}
if (lodash.isString(newOpts)) {
newOpts = JSON.parse(newOpts);
}
lodash.merge(config, oldOpts, newOpts);
configCache = config;
storageService.storeConfig(JSON.stringify(config), cb);
});
};
root.reset = function(cb) {
configCache = lodash.clone(defaultConfig);
storageService.removeConfig(cb);
};
root.getDefaults = function() {
return lodash.clone(defaultConfig);
};
return root;
});