This commit is contained in:
Josh Harvey 2016-10-09 18:49:13 +01:00
parent f62e65fe93
commit bd3441adfb
4 changed files with 43 additions and 21 deletions

View file

@ -46,7 +46,7 @@
] ]
}, },
{ {
"code": "crypto-services", "code": "cryptoServices",
"display": "Crypto services", "display": "Crypto services",
"cryptoScope": "specific", "cryptoScope": "specific",
"machineScope": "both", "machineScope": "both",

View file

@ -9,19 +9,31 @@ exports.connect = connect
function load () { function load () {
var db = connect() var db = connect()
return db.one('select data from user_config where type=$1', 'exchanges') return db.one('select data from user_config where type=$1', 'config')
.then(function (data) { .then(function (data) {
pgp.end() pgp.end()
return data.data return data.data
}) })
} }
function loadAccounts () {
const toFields = fieldArr => R.fromPairs(R.map(r => [r.code, r.value], fieldArr))
const toPairs = r => [r.code, toFields(r.fields)]
var db = connect()
return db.one('select data from user_config where type=$1', 'accounts')
.then(function (data) {
pgp.end()
return R.fromPairs(R.map(toPairs, data.data.accounts))
})
}
module.exports = { module.exports = {
load, load,
unscoped, unscoped,
cryptoScoped, cryptoScoped,
machineScoped, machineScoped,
scoped scoped,
loadAccounts
} }
function matchesValue (crypto, machine, instance) { function matchesValue (crypto, machine, instance) {

View file

@ -38,7 +38,6 @@ const tickerPlugins = {}
const traderPlugins = {} const traderPlugins = {}
const walletPlugins = {} const walletPlugins = {}
let idVerifierPlugin = null let idVerifierPlugin = null
let infoPlugin = null
let emailPlugin = null let emailPlugin = null
let smsPlugin = null let smsPlugin = null
let hkdf = null let hkdf = null
@ -133,11 +132,9 @@ function loadPlugin (name, config) {
return plugin return plugin
} }
function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, options, function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, cfg, accounts, options,
onChangeCallback) { onChangeCallback) {
const currentName = cryptoCode const currentName = cfg.cryptoServices[pluginType]
? cachedConfig.exchanges.plugins.current[cryptoCode][pluginType]
: cachedConfig.exchanges.plugins.current[pluginType]
currentlyUsedPlugins[cryptoCode] = currentlyUsedPlugins[cryptoCode] || {} currentlyUsedPlugins[cryptoCode] = currentlyUsedPlugins[cryptoCode] || {}
@ -145,7 +142,7 @@ function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, options,
if (!currentName) pluginHandle = null if (!currentName) pluginHandle = null
else { // some plugins may be disabled else { // some plugins may be disabled
const pluginConfig = cachedConfig.exchanges.plugins.settings[currentName] || {} const pluginConfig = accounts[currentName]
const mergedConfig = R.merge(pluginConfig, options) const mergedConfig = R.merge(pluginConfig, options)
@ -169,23 +166,26 @@ function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, options,
exports.loadOrConfigPlugin = loadOrConfigPlugin exports.loadOrConfigPlugin = loadOrConfigPlugin
// Note: this whole function gets called every time there's a config update // Note: this whole function gets called every time there's a config update
exports.configure = function configure (config) { exports.configure = function configure (cfg) {
if (config.exchanges.settings.lowBalanceMargin < 1) { if (config.exchanges.settings.lowBalanceMargin < 1) {
throw new Error('\'settings.lowBalanceMargin\' has to be >= 1') throw new Error('\'settings.lowBalanceMargin\' has to be >= 1')
} }
cachedConfig = config cachedConfig = cfg
const accounts = config.loadAccounts()
deviceCurrency = config.fiat.fiatCurrency deviceCurrency = config.fiat.fiatCurrency
cryptoCodes = config.crypto.cryptoCurrencies cryptoCodes = config.crypto.cryptoCurrencies
cryptoCodes.forEach(cryptoCode => { cryptoCodes.forEach(cryptoCode => {
cryptoScopedConfig = config.cryptoScoped(cryptoCode, cachedConfig) const cryptoScopedConfig = config.cryptoScoped(cryptoCode, cachedConfig)
// TICKER [required] configure (or load) // TICKER [required] configure (or load)
loadOrConfigPlugin( loadOrConfigPlugin(
tickerPlugins[cryptoCode], tickerPlugins[cryptoCode],
'ticker', 'ticker',
cryptoCode, cryptoCode,
cryptoScopedConfig,
accounts,
{currency: deviceCurrency}, {currency: deviceCurrency},
function onTickerChange (newTicker) { function onTickerChange (newTicker) {
tickerPlugins[cryptoCode] = newTicker tickerPlugins[cryptoCode] = newTicker
@ -201,6 +201,8 @@ exports.configure = function configure (config) {
walletPlugins[cryptoCode], walletPlugins[cryptoCode],
'transfer', 'transfer',
cryptoCode, cryptoCode,
cryptoScopedConfig,
accounts,
{masterSeed: cryptoSeed}, {masterSeed: cryptoSeed},
function onWalletChange (newWallet) { function onWalletChange (newWallet) {
walletPlugins[cryptoCode] = newWallet walletPlugins[cryptoCode] = newWallet
@ -214,6 +216,8 @@ exports.configure = function configure (config) {
traderPlugins[cryptoCode], traderPlugins[cryptoCode],
'trader', 'trader',
cryptoCode, cryptoCode,
cryptoScopedConfig,
accounts,
null, null,
function onTraderChange (newTrader) { function onTraderChange (newTrader) {
traderPlugins[cryptoCode] = newTrader traderPlugins[cryptoCode] = newTrader
@ -223,25 +227,31 @@ exports.configure = function configure (config) {
) )
}) })
const unscopedCfg = config.unscoped(cachedConfig)
// ID VERIFIER [optional] configure (or load) // ID VERIFIER [optional] configure (or load)
idVerifierPlugin = loadOrConfigPlugin( idVerifierPlugin = loadOrConfigPlugin(
idVerifierPlugin, idVerifierPlugin,
'idVerifier' 'idVerifier',
) null,
unscopedCfg,
infoPlugin = loadOrConfigPlugin( accounts
infoPlugin,
'info'
) )
emailPlugin = loadOrConfigPlugin( emailPlugin = loadOrConfigPlugin(
emailPlugin, emailPlugin,
'email' 'email',
null,
unscopedCfg,
accounts
) )
smsPlugin = loadOrConfigPlugin( smsPlugin = loadOrConfigPlugin(
smsPlugin, smsPlugin,
'sms' 'sms',
null,
unscopedCfg,
accounts
) )
} }

View file

@ -38,7 +38,7 @@ v update migrate-config to match lamassu.json schema
- look into how each plugin is used - look into how each plugin is used
- info plugin not used - info plugin not used
- need either transitive closure of all cryptos accross machines, - need either transitive closure of all cryptos across machines,
or add new plugin when needed or add new plugin when needed
- currently we're looking at all cryptos, so this is probably easier - currently we're looking at all cryptos, so this is probably easier