Wallet/src/js/services/configService.js

266 lines
7.1 KiB
JavaScript
Raw Normal View History

2015-03-06 12:00:10 -03:00
'use strict';
angular.module('copayApp.services').factory('configService', function(storageService, lodash, $log, $timeout, $rootScope, $injector, platformInfo) {
2015-03-06 12:00:10 -03:00
var root = {};
var isWindowsPhoneApp = platformInfo.isCordova && platformInfo.isWP;
2015-03-06 12:00:10 -03:00
var defaultConfig = {
// wallet limits
limits: {
totalCopayers: 6,
mPlusN: 100,
},
// Bitcore wallet service URL
bws: {
url: 'https://bws.bitcoin.com/bws/api'
},
bwscash: {
url: 'https://bwscash.bitcoin.com/bws/api'
2015-03-06 12:00:10 -03:00
},
2016-11-02 10:18:10 -03:00
download: {
2017-03-09 16:48:38 -03:00
bitpay: {
2017-06-18 18:52:13 +09:00
url: 'https://wallet.bitcoin.com'
2017-03-09 16:48:38 -03:00
},
copay: {
2017-06-18 18:52:13 +09:00
url: 'https://wallet.bitcoin.com'
2017-06-12 19:21:47 +09:00
},
bitcoincom: {
url: 'https://wallet.bitcoin.com'
2017-03-09 16:48:38 -03:00
}
2016-11-02 10:18:10 -03:00
},
2017-11-20 16:02:54 +09:00
blockExplorer: {
btc: 'explorer.bitcoin.com/btc',
bch: 'explorer.bitcoin.com/bch'
},
2017-03-09 14:46:07 -03:00
rateApp: {
bitpay: {
ios: 'http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=1149581638&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8',
android: 'https://play.google.com/store/apps/details?id=com.bitpay.wallet',
wp: ''
},
copay: {
ios: 'http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=951330296&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8',
android: 'https://play.google.com/store/apps/details?id=com.bitpay.copay',
wp: ''
2017-06-12 19:21:47 +09:00
},
bitcoincom: {
ios: 'http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=1252903728&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8',
2017-06-18 18:52:13 +09:00
android: 'https://play.google.com/store/apps/details?id=com.bitcoin.mwallet',
2017-06-12 19:21:47 +09:00
wp: ''
2017-03-09 14:46:07 -03:00
}
2017-03-09 10:20:28 -03:00
},
2015-03-06 12:00:10 -03:00
// wallet default config
wallet: {
requiredCopayers: 2,
totalCopayers: 3,
spendUnconfirmed: true,
2015-03-06 12:00:10 -03:00
reconnectDelay: 5000,
idleDurationMin: 4,
settings: {
unitName: 'BTC',
2016-10-21 11:47:32 -03:00
unitToSatoshi: 100000000,
unitDecimals: 8,
unitCode: 'btc',
2015-03-06 12:00:10 -03:00
alternativeName: 'US Dollar',
alternativeIsoCode: 'USD',
}
},
2017-03-29 11:30:16 -03:00
lock: {
2017-04-18 13:19:16 -03:00
method: null,
value: null,
2017-03-30 11:31:23 -03:00
bannedUntil: null,
2017-02-24 14:46:54 -05:00
},
cashSupport: true,
2017-08-29 10:52:26 -03:00
2017-01-31 16:32:45 -03:00
recentTransactions: {
2015-09-11 13:11:41 -03:00
enabled: true,
},
displayBitcoinCore: {
enabled: false,
},
2017-01-31 16:32:45 -03:00
hideNextSteps: {
enabled: isWindowsPhoneApp ? true : false,
2016-09-27 15:57:39 -03:00
},
2015-03-06 12:00:10 -03:00
rates: {
url: 'https://insight.bitpay.com:443/api/rates',
},
2016-04-11 12:56:18 -03:00
release: {
2017-06-18 18:52:13 +09:00
url: 'https://api.github.com/repos/Bitcoin-com/Wallet/releases/latest'
2016-04-11 12:56:18 -03:00
},
2017-02-03 18:03:29 -03:00
pushNotificationsEnabled: true,
2016-10-31 14:08:52 -03:00
2017-07-14 15:45:18 -03:00
confirmedTxsNotifications: {
enabled: true,
},
2016-10-31 14:08:52 -03:00
emailNotifications: {
enabled: false,
},
soundsEnabled: false,
log: {
filter: 'debug',
},
bitcoinAlias: 'btc',
bitcoinCashAlias: 'bch',
bitcoinWalletColor: '#fab915', // Observatory
bitcoinCashWalletColor: '#26B03C' // Dollar Green
2015-03-06 12:00:10 -03:00
};
var configCache = null;
root.getSync = function() {
if (!configCache)
throw new Error('configService#getSync called when cache is not initialized');
return configCache;
};
2016-08-19 23:40:28 -03:00
root._queue = [];
root.whenAvailable = function(cb) {
if (!configCache) {
root._queue.push(cb);
return;
}
return cb(configCache);
};
2015-03-06 12:00:10 -03:00
root.get = function(cb) {
2015-04-25 12:37:04 -03:00
storageService.getConfig(function(err, localConfig) {
2015-03-06 12:00:10 -03:00
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;
}
2015-03-06 12:00:10 -03:00
if (!configCache.wallet.settings.unitCode) {
configCache.wallet.settings.unitCode = defaultConfig.wallet.settings.unitCode;
}
2017-01-31 17:32:11 -03:00
if (!configCache.hideNextSteps) {
configCache.hideNextSteps = defaultConfig.hideNextSteps;
2016-09-27 15:57:39 -03:00
}
2017-01-31 17:32:11 -03:00
// Always support Bitcoin Cash
configCache.cashSupport = true;
2017-08-29 10:52:26 -03:00
2016-09-27 15:57:39 -03:00
if (!configCache.recentTransactions) {
configCache.recentTransactions = defaultConfig.recentTransactions;
}
2016-01-22 00:35:11 -03:00
if (!configCache.pushNotifications) {
configCache.pushNotifications = defaultConfig.pushNotifications;
}
if (!configCache.bitpayAccount) {
configCache.bitpayAccount = defaultConfig.bitpayAccount;
}
2015-03-06 12:00:10 -03:00
if (configCache.wallet.settings.unitCode == 'bit') {
// Convert to BTC. Bits will be disabled
configCache.wallet.settings.unitName = defaultConfig.wallet.settings.unitName;
configCache.wallet.settings.unitToSatoshi = defaultConfig.wallet.settings.unitToSatoshi;
configCache.wallet.settings.unitDecimals = defaultConfig.wallet.settings.unitDecimals;
configCache.wallet.settings.unitCode = defaultConfig.wallet.settings.unitCode;
}
// Convert tarascash wallet to new style cash wallet
if (configCache.bwsbcc && configCache.bwsbcc.url && configCache.bwsbcc.url.indexOf('bwsbcc') >= 0) {
configCache.bwsbcc = defaultConfig.bwscash.url;
}
if (configCache.bwsFor) {
for (var key in configCache.bwsFor) {
if (configCache.bwsFor[key].indexOf('bwsbcc') >= 0) {
configCache.bwsFor[key] = defaultConfig.bwscash.url;
}
}
}
2015-03-06 12:00:10 -03:00
} else {
configCache = lodash.clone(defaultConfig);
2015-03-06 12:00:10 -03:00
};
2016-08-22 19:17:31 -03:00
configCache.bwsFor = configCache.bwsFor || {};
configCache.colorFor = configCache.colorFor || {};
configCache.aliasFor = configCache.aliasFor || {};
2016-09-01 17:46:56 -03:00
configCache.emailFor = configCache.emailFor || {};
2016-08-22 19:17:31 -03:00
$log.debug('Preferences read:', configCache)
2016-08-19 23:40:28 -03:00
lodash.each(root._queue, function(x) {
$timeout(function() {
return x(configCache);
}, 1);
});
root._queue = [];
2015-03-06 12:00:10 -03:00
return cb(err, configCache);
});
};
root.set = function(newOpts, cb) {
var config = lodash.cloneDeep(defaultConfig);
2015-04-25 12:37:04 -03:00
storageService.getConfig(function(err, oldOpts) {
2016-06-09 19:00:09 -03:00
oldOpts = oldOpts || {};
2015-03-06 12:00:10 -03:00
if (lodash.isString(oldOpts)) {
oldOpts = JSON.parse(oldOpts);
}
if (lodash.isString(config)) {
config = JSON.parse(config);
}
if (lodash.isString(newOpts)) {
newOpts = JSON.parse(newOpts);
}
2016-06-09 19:00:09 -03:00
2015-03-06 12:00:10 -03:00
lodash.merge(config, oldOpts, newOpts);
configCache = config;
2016-08-22 14:42:43 -03:00
$rootScope.$emit('Local/SettingsUpdated');
2015-04-26 20:13:02 -03:00
storageService.storeConfig(JSON.stringify(config), cb);
2015-03-06 12:00:10 -03:00
});
};
root.reset = function(cb) {
2015-04-25 12:37:04 -03:00
configCache = lodash.clone(defaultConfig);
storageService.removeConfig(cb);
2015-03-06 12:00:10 -03:00
};
root.getDefaults = function() {
2015-04-25 12:37:04 -03:00
return lodash.clone(defaultConfig);
2015-03-06 12:00:10 -03:00
};
root.checkIfConfigIsSet = function(key) {
return new Promise(function(resolve, reject) {
storageService.getConfig(function(err, localConfig) {
if (localConfig) {
configCache = JSON.parse(localConfig);
resolve(configCache.hasOwnProperty(key));
} else {
reject(false);
}
});
});
}
2015-03-06 12:00:10 -03:00
return root;
});