<commit_before>'use strict'

const fs = require('fs')
const uuid = require('node-uuid')
const R = require('ramda')
const async = require('async')
const HKDF = require('node-hkdf-sync')
const BigNumber = require('bignumber.js')
BigNumber.config({CRYPTO: true})
const db = require('./postgresql_interface')
const logger = require('./logger')
const notifier = require('./notifier')
const T = require('./time')

const tradeIntervals = {}

const CHECK_NOTIFICATION_INTERVAL = T.minute
const ALERT_SEND_INTERVAL = T.hour
const POLLING_RATE = T.minute
const INCOMING_TX_INTERVAL = 30 * T.seconds
const LIVE_INCOMING_TX_INTERVAL = 5 * T.seconds
const STALE_INCOMING_TX_AGE = T.week
const STALE_LIVE_INCOMING_TX_AGE = 10 * T.minutes
const UNNOTIFIED_INTERVAL = T.minute
const MAX_NOTIFY_AGE = 2 * T.days
const MIN_NOTIFY_AGE = 5 * T.minutes
const TRANSACTION_EXPIRATION = 2 * T.days
const SWEEP_LIVE_HD_INTERVAL = T.minute
const SWEEP_OLD_HD_INTERVAL = 2 * T.minutes
const TRADE_INTERVAL = T.minute
const TRADE_TTL = 5 * T.minutes
const LOW_BALANCE_MARGIN_DEFAULT = 1.05

let cryptoCodes = null

const tickerPlugins = {}
const traderPlugins = {}
const walletPlugins = {}
let idVerifierPlugin = null
let infoPlugin = null
let emailPlugin = null
let smsPlugin = null
let hkdf = null

const currentlyUsedPlugins = {}

let cachedConfig = null
let deviceCurrency = 'USD'

const lastBalances = {}
const lastRates = {}

const tradesQueues = {}

const coins = {
  BTC: {unitScale: 8},
  ETH: {unitScale: 18}
}

let alertFingerprint = null
let lastAlertTime = null

exports.init = function init (connectionString, seedPath) {
  const masterSeed = new Buffer(fs.readFileSync(seedPath, 'utf8').trim(), 'hex')
  hkdf = new HKDF('sha256', 'lamassu-server-salt', masterSeed)

  db.init(connectionString)
}

function loadPlugin (name, config) {
  // plugins definitions
  const moduleMethods = {
    ticker: ['ticker'],
    trader: ['purchase', 'sell'],
    wallet: ['balance', 'sendBitcoins', 'newAddress'],
    idVerifier: ['verifyUser', 'verifyTransaction'],
    info: ['checkAddress'],
    email: ['sendMessage']
  }

  let plugin = null

  // each used plugin MUST be installed
  try {
    plugin = require('lamassu-' + name)
  } catch (_) {
    throw new Error(name + ' module is not installed. ' +
        'Try running \'npm install --save lamassu-' + name + '\' first')
  }

  // each plugin MUST implement those
  if (typeof plugin.SUPPORTED_MODULES !== 'undefined') {
    if (plugin.SUPPORTED_MODULES === 'string') {
      plugin.SUPPORTED_MODULES = [plugin.SUPPORTED_MODULES]
    }
  }

  if (!(plugin.SUPPORTED_MODULES instanceof Array)) {
    throw new Error('\'' + name + '\' fails to implement *required* ' +
      '\'SUPPORTED_MODULES\' constant')
  }

  plugin.SUPPORTED_MODULES.forEach(moduleName => {<commit_msg>correct error logging for plugin loading issues<commit_after>'use strict'

const fs = require('fs')
const uuid = require('node-uuid')
const R = require('ramda')
const async = require('async')
const HKDF = require('node-hkdf-sync')
const BigNumber = require('bignumber.js')
BigNumber.config({CRYPTO: true})
const db = require('./postgresql_interface')
const logger = require('./logger')
const notifier = require('./notifier')
const T = require('./time')

const tradeIntervals = {}

const CHECK_NOTIFICATION_INTERVAL = T.minute
const ALERT_SEND_INTERVAL = T.hour
const POLLING_RATE = T.minute
const INCOMING_TX_INTERVAL = 30 * T.seconds
const LIVE_INCOMING_TX_INTERVAL = 5 * T.seconds
const STALE_INCOMING_TX_AGE = T.week
const STALE_LIVE_INCOMING_TX_AGE = 10 * T.minutes
const UNNOTIFIED_INTERVAL = T.minute
const MAX_NOTIFY_AGE = 2 * T.days
const MIN_NOTIFY_AGE = 5 * T.minutes
const TRANSACTION_EXPIRATION = 2 * T.days
const SWEEP_LIVE_HD_INTERVAL = T.minute
const SWEEP_OLD_HD_INTERVAL = 2 * T.minutes
const TRADE_INTERVAL = T.minute
const TRADE_TTL = 5 * T.minutes
const LOW_BALANCE_MARGIN_DEFAULT = 1.05

let cryptoCodes = null

const tickerPlugins = {}
const traderPlugins = {}
const walletPlugins = {}
let idVerifierPlugin = null
let infoPlugin = null
let emailPlugin = null
let smsPlugin = null
let hkdf = null

const currentlyUsedPlugins = {}

let cachedConfig = null
let deviceCurrency = 'USD'

const lastBalances = {}
const lastRates = {}

const tradesQueues = {}

const coins = {
  BTC: {unitScale: 8},
  ETH: {unitScale: 18}
}

let alertFingerprint = null
let lastAlertTime = null

exports.init = function init (connectionString, seedPath) {
  const masterSeed = new Buffer(fs.readFileSync(seedPath, 'utf8').trim(), 'hex')
  hkdf = new HKDF('sha256', 'lamassu-server-salt', masterSeed)

  db.init(connectionString)
}

function loadPlugin (name, config) {
  // plugins definitions
  const moduleMethods = {
    ticker: ['ticker'],
    trader: ['purchase', 'sell'],
    wallet: ['balance', 'sendBitcoins', 'newAddress'],
    idVerifier: ['verifyUser', 'verifyTransaction'],
    info: ['checkAddress'],
    email: ['sendMessage']
  }

  let plugin = null

  // each used plugin MUST be installed
  try {
    plugin = require('lamassu-' + name)
  } catch (err) {
    if (err.code === 'MODULE_NOT_FOUND') {
      throw new Error(name + ' module is not installed. ' +
          'Try running \'npm install --save lamassu-' + name + '\' first')
    }
    logger.error('Error in %s plugin', name)
    logger.error(err)
    throw err
  }

  // each plugin MUST implement those
  if (typeof plugin.SUPPORTED_MODULES !== 'undefined') {
    if (plugin.SUPPORTED_MODULES === 'string') {
      plugin.SUPPORTED_MODULES = [plugin.SUPPORTED_MODULES]
    }
  }

  if (!(plugin.SUPPORTED_MODULES instanceof Array)) {
    throw new Error('\'' + name + '\' fails to implement *required* ' +
      '\'SUPPORTED_MODULES\' constant')
  }

  plugin.SUPPORTED_MODULES.forEach(moduleName => {
{'fraction_non_alphanumeric': 0.12482710926694329, 'fraction_numerical': 0.006915629322268326, 'mean_word_length': 4.240036231884058, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5472756', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1901, 'n_words': 504}