WIP
This commit is contained in:
parent
896bec93cb
commit
35cbb46688
6 changed files with 62 additions and 23 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -30,3 +30,4 @@ options.mine.js
|
||||||
raqia.json
|
raqia.json
|
||||||
|
|
||||||
scratch/
|
scratch/
|
||||||
|
seeds/
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
|
var fs = require('fs')
|
||||||
var R = require('ramda')
|
var R = require('ramda')
|
||||||
var async = require('async')
|
var async = require('async')
|
||||||
|
|
||||||
var BigNumber = require('bignumber.js')
|
var BigNumber = require('bignumber.js')
|
||||||
BigNumber.config({CRYPTO: true})
|
BigNumber.config({CRYPTO: true})
|
||||||
|
|
||||||
|
|
@ -34,6 +34,7 @@ var idVerifierPlugin = null
|
||||||
var infoPlugin = null
|
var infoPlugin = null
|
||||||
var emailPlugin = null
|
var emailPlugin = null
|
||||||
var smsPlugin = null
|
var smsPlugin = null
|
||||||
|
var masterSeed = null
|
||||||
|
|
||||||
var currentlyUsedPlugins = {}
|
var currentlyUsedPlugins = {}
|
||||||
|
|
||||||
|
|
@ -54,12 +55,8 @@ var alertFingerprint = null
|
||||||
var lastAlertTime = null
|
var lastAlertTime = null
|
||||||
|
|
||||||
// that's basically a constructor
|
// that's basically a constructor
|
||||||
exports.init = function init (databaseHandle) {
|
exports.init = function init () {
|
||||||
if (!databaseHandle) {
|
fs.readFileSync('seeds/seed.txt')
|
||||||
throw new Error('\'db\' is required')
|
|
||||||
}
|
|
||||||
|
|
||||||
db = databaseHandle
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadPlugin (name, config) {
|
function loadPlugin (name, config) {
|
||||||
|
|
@ -121,7 +118,7 @@ function loadPlugin (name, config) {
|
||||||
return plugin
|
return plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, currency,
|
function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, options,
|
||||||
onChangeCallback) {
|
onChangeCallback) {
|
||||||
cryptoCode = cryptoCode || 'BTC'
|
cryptoCode = cryptoCode || 'BTC'
|
||||||
|
|
||||||
|
|
@ -137,11 +134,11 @@ function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, currency,
|
||||||
else { // some plugins may be disabled
|
else { // some plugins may be disabled
|
||||||
var pluginConfig = cachedConfig.exchanges.plugins.settings[currentName] || {}
|
var pluginConfig = cachedConfig.exchanges.plugins.settings[currentName] || {}
|
||||||
|
|
||||||
if (currency) pluginConfig.currency = currency
|
const mergedConfig = R.merge(pluginConfig, options)
|
||||||
|
|
||||||
if (pluginHandle && !pluginChanged) pluginHandle.config(pluginConfig)
|
if (pluginHandle && !pluginChanged) pluginHandle.config(mergedConfig)
|
||||||
else {
|
else {
|
||||||
pluginHandle = loadPlugin(currentName, pluginConfig)
|
pluginHandle = loadPlugin(currentName, mergedConfig)
|
||||||
currentlyUsedPlugins[cryptoCode] = currentlyUsedPlugins[cryptoCode] || {}
|
currentlyUsedPlugins[cryptoCode] = currentlyUsedPlugins[cryptoCode] || {}
|
||||||
currentlyUsedPlugins[cryptoCode][pluginType] = currentName
|
currentlyUsedPlugins[cryptoCode][pluginType] = currentName
|
||||||
logger.debug('[%s] plugin(%s) loaded: %s', cryptoCode, pluginType, pluginHandle.NAME ||
|
logger.debug('[%s] plugin(%s) loaded: %s', cryptoCode, pluginType, pluginHandle.NAME ||
|
||||||
|
|
@ -149,7 +146,7 @@ function loadOrConfigPlugin (pluginHandle, pluginType, cryptoCode, currency,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof onChangeCallback === 'function') onChangeCallback(pluginHandle, currency)
|
if (typeof onChangeCallback === 'function') onChangeCallback(pluginHandle)
|
||||||
|
|
||||||
return pluginHandle
|
return pluginHandle
|
||||||
}
|
}
|
||||||
|
|
@ -171,7 +168,7 @@ exports.configure = function configure (config) {
|
||||||
tickerPlugins[cryptoCode],
|
tickerPlugins[cryptoCode],
|
||||||
'ticker',
|
'ticker',
|
||||||
cryptoCode,
|
cryptoCode,
|
||||||
deviceCurrency, // device currency
|
{currency: deviceCurrency},
|
||||||
function onTickerChange (newTicker) {
|
function onTickerChange (newTicker) {
|
||||||
tickerPlugins[cryptoCode] = newTicker
|
tickerPlugins[cryptoCode] = newTicker
|
||||||
pollRate(cryptoCode)
|
pollRate(cryptoCode)
|
||||||
|
|
@ -183,7 +180,10 @@ exports.configure = function configure (config) {
|
||||||
walletPlugins[cryptoCode],
|
walletPlugins[cryptoCode],
|
||||||
'transfer',
|
'transfer',
|
||||||
cryptoCode,
|
cryptoCode,
|
||||||
null,
|
{
|
||||||
|
nextCashOutSerialHD: db.nextCashOutSerialHD(cryptoCode),
|
||||||
|
masterSeed: masterSeed
|
||||||
|
},
|
||||||
function onWalletChange (newWallet) {
|
function onWalletChange (newWallet) {
|
||||||
walletPlugins[cryptoCode] = newWallet
|
walletPlugins[cryptoCode] = newWallet
|
||||||
pollBalance(cryptoCode)
|
pollBalance(cryptoCode)
|
||||||
|
|
@ -360,8 +360,12 @@ exports.cashOut = function cashOut (session, tx) {
|
||||||
walletPlugin.newAddress(tmpInfo, function (err, address) {
|
walletPlugin.newAddress(tmpInfo, function (err, address) {
|
||||||
if (err) return reject(err)
|
if (err) return reject(err)
|
||||||
|
|
||||||
const newTx = R.assoc('toAddress', address, tx)
|
const addressRec = R.is(String, address)
|
||||||
return db.addInitialIncoming(session, newTx)
|
? {address: address}
|
||||||
|
: address
|
||||||
|
|
||||||
|
const newTx = R.assoc('toAddress', addressRec.address, tx)
|
||||||
|
return db.addInitialIncoming(session, newTx, addressRec)
|
||||||
.then(() => resolve(address))
|
.then(() => resolve(address))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -113,7 +113,7 @@ exports.sentCoins = function sentCoins (session, tx, toSend, fee, error, txHash)
|
||||||
return db.none(sql, [txHash, error, session.id])
|
return db.none(sql, [txHash, error, session.id])
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.addInitialIncoming = function addInitialIncoming (session, tx) {
|
exports.addInitialIncoming = function addInitialIncoming (session, tx, addressRec) {
|
||||||
const fields = ['session_id', 'device_fingerprint', 'to_address',
|
const fields = ['session_id', 'device_fingerprint', 'to_address',
|
||||||
'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash',
|
'crypto_atoms', 'crypto_code', 'currency_code', 'fiat', 'tx_hash',
|
||||||
'phone', 'error'
|
'phone', 'error'
|
||||||
|
|
@ -133,6 +133,15 @@ exports.addInitialIncoming = function addInitialIncoming (session, tx) {
|
||||||
]
|
]
|
||||||
|
|
||||||
return db.none(getInsertQuery('cash_out_txs', fields), values)
|
return db.none(getInsertQuery('cash_out_txs', fields), values)
|
||||||
|
.then(() => {
|
||||||
|
const hd = addressRec.hd
|
||||||
|
if (hd) {
|
||||||
|
const fields2 = ['session_id', 'to_address', 'crypto_code',
|
||||||
|
'hd_path_prefix', 'hd_serial']
|
||||||
|
const values2 = [tx.sessionId, tx.toAddress, tx.cryptoCode, hd.pathPrefix, hd.serial]
|
||||||
|
return db.none(getInsertQuery('cash_out_hds', fields2), values2)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function insertDispense (session, tx, cartridges) {
|
function insertDispense (session, tx, cartridges) {
|
||||||
|
|
@ -424,3 +433,10 @@ exports.cacheResponse = function (session, path, method, body) {
|
||||||
|
|
||||||
return db.none(sql, values)
|
return db.none(sql, values)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.nextCashOutSerialHD = function nextCashOutSerialHD (cryptoCode) {
|
||||||
|
const sql = `select hd_serial from cash_out_hds
|
||||||
|
where crypto_code=$1 order by hd_serial desc limit 1`
|
||||||
|
return db.oneOrNone(sql, [cryptoCode])
|
||||||
|
.then(row => row ? row.hd_serial + 1 : 0)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,7 @@ exports.up = function (next) {
|
||||||
tx_hash text,
|
tx_hash text,
|
||||||
phone text,
|
phone text,
|
||||||
error text,
|
error text,
|
||||||
created timestamptz NOT NULL default now(),
|
created timestamptz NOT NULL default now()
|
||||||
unique (session_id)
|
|
||||||
)`,
|
)`,
|
||||||
`create table cash_out_txs (
|
`create table cash_out_txs (
|
||||||
session_id uuid PRIMARY KEY,
|
session_id uuid PRIMARY KEY,
|
||||||
|
|
@ -40,8 +39,7 @@ exports.up = function (next) {
|
||||||
phone text,
|
phone text,
|
||||||
error text,
|
error text,
|
||||||
created timestamptz NOT NULL default now(),
|
created timestamptz NOT NULL default now(),
|
||||||
confirmation_time timestamptz,
|
confirmation_time timestamptz
|
||||||
unique (session_id)
|
|
||||||
)`,
|
)`,
|
||||||
`create type cash_out_action_types AS ENUM (${actions})`,
|
`create type cash_out_action_types AS ENUM (${actions})`,
|
||||||
`create table cash_out_actions (
|
`create table cash_out_actions (
|
||||||
|
|
|
||||||
20
migrations/012-add-hd-path-serial.js
Normal file
20
migrations/012-add-hd-path-serial.js
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
var db = require('./db')
|
||||||
|
|
||||||
|
exports.up = function (next) {
|
||||||
|
var sql = [
|
||||||
|
`create table cash_out_hds (
|
||||||
|
session_id uuid PRIMARY KEY,
|
||||||
|
to_address text NOT NULL,
|
||||||
|
crypto_code text NOT NULL,
|
||||||
|
hd_path_prefix text NOT NULL,
|
||||||
|
hd_serial integer NOT NULL,
|
||||||
|
created timestamptz NOT NULL default now(),
|
||||||
|
unique (crypto_code, hd_serial)
|
||||||
|
)`
|
||||||
|
]
|
||||||
|
db.multi(sql, next)
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.down = function (next) {
|
||||||
|
next()
|
||||||
|
}
|
||||||
4
todo.txt
4
todo.txt
|
|
@ -1,4 +1,4 @@
|
||||||
- l-m shouldn't keep polling l-s when not on pending screen (low priority)
|
- l-m shouldn't keep polling l-s when not on pending screen (low priority)
|
||||||
|
|
||||||
- don't record multiple tx authorizations to db
|
- need more bulletproof way to ensure only unique serials for each coin
|
||||||
- processTxStatus isn't atomic
|
- use tx to read last, then insert new row
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue