diff --git a/lamassu-schema.json b/lamassu-schema.json index b1739821..c2064d60 100644 --- a/lamassu-schema.json +++ b/lamassu-schema.json @@ -46,7 +46,7 @@ ] }, { - "code": "crypto-services", + "code": "cryptoServices", "display": "Crypto services", "cryptoScope": "specific", "machineScope": "both", diff --git a/lib/config.js b/lib/config.js index 69ca6071..c6dc7d2a 100644 --- a/lib/config.js +++ b/lib/config.js @@ -9,19 +9,31 @@ exports.connect = connect function load () { 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) { pgp.end() 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 = { load, unscoped, cryptoScoped, machineScoped, - scoped + scoped, + loadAccounts } function matchesValue (crypto, machine, instance) { diff --git a/lib/plugins.js b/lib/plugins.js index a315a039..113bb631 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -38,7 +38,6 @@ const tickerPlugins = {} const traderPlugins = {} const walletPlugins = {} let idVerifierPlugin = null -let infoPlugin = null let emailPlugin = null let smsPlugin = null let hkdf = null @@ -133,11 +132,9 @@ function loadPlugin (name, config) { return plugin } -function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, options, +function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, cfg, accounts, options, onChangeCallback) { - const currentName = cryptoCode - ? cachedConfig.exchanges.plugins.current[cryptoCode][pluginType] - : cachedConfig.exchanges.plugins.current[pluginType] + const currentName = cfg.cryptoServices[pluginType] currentlyUsedPlugins[cryptoCode] = currentlyUsedPlugins[cryptoCode] || {} @@ -145,7 +142,7 @@ function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, options, if (!currentName) pluginHandle = null else { // some plugins may be disabled - const pluginConfig = cachedConfig.exchanges.plugins.settings[currentName] || {} + const pluginConfig = accounts[currentName] const mergedConfig = R.merge(pluginConfig, options) @@ -169,23 +166,26 @@ function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, options, exports.loadOrConfigPlugin = loadOrConfigPlugin // 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) { throw new Error('\'settings.lowBalanceMargin\' has to be >= 1') } - cachedConfig = config + cachedConfig = cfg + const accounts = config.loadAccounts() deviceCurrency = config.fiat.fiatCurrency cryptoCodes = config.crypto.cryptoCurrencies cryptoCodes.forEach(cryptoCode => { - cryptoScopedConfig = config.cryptoScoped(cryptoCode, cachedConfig) + const cryptoScopedConfig = config.cryptoScoped(cryptoCode, cachedConfig) // TICKER [required] configure (or load) loadOrConfigPlugin( tickerPlugins[cryptoCode], 'ticker', cryptoCode, + cryptoScopedConfig, + accounts, {currency: deviceCurrency}, function onTickerChange (newTicker) { tickerPlugins[cryptoCode] = newTicker @@ -201,6 +201,8 @@ exports.configure = function configure (config) { walletPlugins[cryptoCode], 'transfer', cryptoCode, + cryptoScopedConfig, + accounts, {masterSeed: cryptoSeed}, function onWalletChange (newWallet) { walletPlugins[cryptoCode] = newWallet @@ -214,6 +216,8 @@ exports.configure = function configure (config) { traderPlugins[cryptoCode], 'trader', cryptoCode, + cryptoScopedConfig, + accounts, null, function onTraderChange (newTrader) { traderPlugins[cryptoCode] = newTrader @@ -223,25 +227,31 @@ exports.configure = function configure (config) { ) }) + const unscopedCfg = config.unscoped(cachedConfig) + // ID VERIFIER [optional] configure (or load) idVerifierPlugin = loadOrConfigPlugin( idVerifierPlugin, - 'idVerifier' - ) - - infoPlugin = loadOrConfigPlugin( - infoPlugin, - 'info' + 'idVerifier', + null, + unscopedCfg, + accounts ) emailPlugin = loadOrConfigPlugin( emailPlugin, - 'email' + 'email', + null, + unscopedCfg, + accounts ) smsPlugin = loadOrConfigPlugin( smsPlugin, - 'sms' + 'sms', + null, + unscopedCfg, + accounts ) } diff --git a/todo.txt b/todo.txt index 02efd89a..38177483 100644 --- a/todo.txt +++ b/todo.txt @@ -38,7 +38,7 @@ v update migrate-config to match lamassu.json schema - look into how each plugin is 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 - currently we're looking at all cryptos, so this is probably easier