From 48609bd1b703d757de89e14fba9830c589286750 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Tue, 25 Oct 2022 03:16:59 +0530 Subject: [PATCH] Changing env-var to arguments - Changes console.log and console.debug to relevant types - PASSWORD and I environment variables are now passed as arguments to npm commands - Added optional argument `--debug`. when passed, console.debug is also logged, else console.debug are turned off --- .gitignore | 2 ++ README.md | 23 ++++++++--------------- docs/scripts/floGlobals.js | 4 +--- docs/scripts/floTokenAPI.js | 4 ++-- setup/configure-settings.js | 15 +++++++++++---- setup/create-schema.js | 9 ++++++++- setup/help.js | 8 ++------ setup/reset-password.js | 9 ++++++++- src/backup/head.js | 6 +++--- src/backup/slave.js | 2 +- src/coupling.js | 4 ++-- src/main.js | 14 ++++++++++---- src/market.js | 2 +- src/services/conversion.js | 8 ++++---- src/set_globals.js | 3 +++ 15 files changed, 66 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index e067b5b..bc0c19d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,7 @@ /args/config*.json /args/param.json /args/keys*.json +/log.txt +/bash_start* *test* *.tmp* diff --git a/README.md b/README.md index 5bf29b2..5e55a46 100644 --- a/README.md +++ b/README.md @@ -15,24 +15,17 @@ npm run create-schema - Create schema in MySQL database. npm start - Start the application (main). ``` **NOTE:** -env variable `PASSWORD` required for `npm start`. - -Windows: +Argument `PASSWORD` required for `npm start`. ``` -$env:PASSWORD=""; npm start -``` -Linux: -``` -PASSWORD=" npm start +npm start -- -PASSWORD= ``` *(Optional)* -Multiple instance can be run/setup on the same dir with different config files by using env variable 'I'. - -Windows: +Multiple instance can be run/setup on the same dir with different config files by using argument 'I'. ``` -$env:I=""; + -- -I= ``` -Linux: -``` -I="" +*(Optional)* +`console.debug` is now turned off by default. pass argument `--debug` to turn it on ``` +npm start -- -PASSWORD= --debug +``` \ No newline at end of file diff --git a/docs/scripts/floGlobals.js b/docs/scripts/floGlobals.js index 5501626..3ca2f62 100644 --- a/docs/scripts/floGlobals.js +++ b/docs/scripts/floGlobals.js @@ -1,10 +1,8 @@ const floGlobals = { - - //Required for all blockchain: "FLO", application: "exchange", adminID: "FMxYC7gYZhouzqtHZukGnPiQ8nvG4CMzXM", currency: "rupee" }; -('object' === typeof module) ? module.exports = floGlobals: null; \ No newline at end of file +('object' === typeof module) ? module.exports = floGlobals : null; \ No newline at end of file diff --git a/docs/scripts/floTokenAPI.js b/docs/scripts/floTokenAPI.js index 7378f43..3f4cb5f 100644 --- a/docs/scripts/floTokenAPI.js +++ b/docs/scripts/floTokenAPI.js @@ -1,4 +1,4 @@ -(function(EXPORTS) { //floTokenAPI v1.0.3b +(function(EXPORTS) { //floTokenAPI v1.0.3c /* Token Operator to send/receive tokens via blockchain using API calls*/ 'use strict'; const tokenAPI = EXPORTS; @@ -29,7 +29,7 @@ const fetch_api = tokenAPI.fetch = function(apicall) { return new Promise((resolve, reject) => { - console.log(DEFAULT.apiURL + apicall); + console.debug(DEFAULT.apiURL + apicall); fetch(DEFAULT.apiURL + apicall).then(response => { if (response.ok) response.json().then(data => resolve(data)); diff --git a/setup/configure-settings.js b/setup/configure-settings.js index abc1673..9a3b431 100644 --- a/setup/configure-settings.js +++ b/setup/configure-settings.js @@ -1,9 +1,16 @@ const fs = require('fs'); const getInput = require('./getInput'); +let _I = ""; +for (let arg of process.argv) + if (/^-I=/.test(arg)) { + _I = arg.split(/=(.*)/s)[1]; + break; + } + var config, flag_new; try { - config = require(`../args/config${process.env.I || ""}.json`); + config = require(`../args/config${_I}.json`); flag_new = false; } catch (error) { config = { @@ -24,8 +31,8 @@ function flaggedYesOrNo(text) { resolve(true); else getInput.YesOrNo(text) - .then(result => resolve(result)) - .catch(error => reject(error)) + .then(result => resolve(result)) + .catch(error => reject(error)) }) } @@ -84,7 +91,7 @@ function configure() { configurePort().then(port_result => { randomizeSessionSecret().then(secret_result => { configureSQL().then(sql_result => { - fs.writeFile(__dirname + `/../args/config${process.env.I || ""}.json`, JSON.stringify(config), 'utf8', (err) => { + fs.writeFile(__dirname + `/../args/config${_I}.json`, JSON.stringify(config), 'utf8', (err) => { if (err) { console.error(err); return reject(false); diff --git a/setup/create-schema.js b/setup/create-schema.js index 3ed6c19..0b0f6fc 100644 --- a/setup/create-schema.js +++ b/setup/create-schema.js @@ -1,8 +1,15 @@ const fs = require('fs'); let Database = require('../src/database'); +let _I = ""; +for (let arg of process.argv) + if (/^-I=/.test(arg)) { + _I = arg.split(/=(.*)/s)[1]; + break; + } + function createSchema() { - const config = require(`../args/config${process.env.I || ""}.json`); + const config = require(`../args/config${_I}.json`); return new Promise((resolve, reject) => { fs.readFile(__dirname + '/../args/schema.sql', 'utf8', (err, data) => { if (err) { diff --git a/setup/help.js b/setup/help.js index 9092343..c431269 100644 --- a/setup/help.js +++ b/setup/help.js @@ -14,13 +14,9 @@ npm run backup - Run the backup-node. npm start - Start the application (main). -NOTE: env variable 'PASSWORD' required for 'npm start'. +NOTE: argument 'PASSWORD' required for 'npm start' +npm start -- -PASSWORD= -WINDOWS: -$env:PASSWORD=""; npm start - -LINUX: -PASSWORD=" npm start `; console.log(message); \ No newline at end of file diff --git a/setup/reset-password.js b/setup/reset-password.js index b0d0847..9fd6c3d 100644 --- a/setup/reset-password.js +++ b/setup/reset-password.js @@ -8,6 +8,13 @@ const floCrypto = require('../docs/scripts/floCrypto'); console.log(__dirname); +let _I = ""; +for (let arg of process.argv) + if (/^-I=/.test(arg)) { + _I = arg.split(/=(.*)/s)[1]; + break; + } + function validateKey(privKey) { return new Promise((resolve, reject) => { try { @@ -62,7 +69,7 @@ function resetPassword() { let encrypted = Crypto.AES.encrypt(privKey, password); let randNum = floCrypto.randInt(10, 15); let splitShares = floCrypto.createShamirsSecretShares(encrypted, randNum, randNum); - fs.writeFile(__dirname + `/../args/keys${process.env.I || ""}.json`, JSON.stringify(splitShares), 'utf8', (err) => { + fs.writeFile(__dirname + `/../args/keys${_I}.json`, JSON.stringify(splitShares), 'utf8', (err) => { if (err) { console.error(err); return reject(false); diff --git a/src/backup/head.js b/src/backup/head.js index da9b884..7447f63 100644 --- a/src/backup/head.js +++ b/src/backup/head.js @@ -161,7 +161,7 @@ function collectShares(floID, sinkID, share) { try { let sinkKey = floCrypto.retrieveShamirSecret([].concat(...Object.values(shares_collected[sinkID].shares))); if (floCrypto.verifyPrivKey(sinkKey, sinkID)) { - console.log("Shares collected successfully for", sinkID); + console.debug("Shares collected successfully for", sinkID); shares_collected[sinkID].callbacks.forEach(fn => fn instanceof Function ? fn(sinkKey) : null); delete shares_collected[sinkID]; } @@ -200,7 +200,7 @@ function connectToMaster(i = 0, init = false) { //Node becomes master function serveAsMaster(init) { - console.debug('Starting master process'); + console.info('Starting master process'); slave.stop(); _mode = MASTER_MODE; informLiveNodes(init); @@ -208,7 +208,7 @@ function serveAsMaster(init) { } function serveAsSlave(ws, init) { - console.debug('Starting slave process'); + console.info('Starting slave process'); app.pause(); slave.start(ws, init); _mode = SLAVE_MODE; diff --git a/src/backup/slave.js b/src/backup/slave.js index 7b33ec4..d02e99e 100644 --- a/src/backup/slave.js +++ b/src/backup/slave.js @@ -153,7 +153,7 @@ function storeSinkShare(sinkID, keyShare, decrypt = true) { if (decrypt) keyShare = floCrypto.decryptData(keyShare, global.myPrivKey) let encryptedShare = Crypto.AES.encrypt(keyShare, global.myPrivKey); - console.log(Date.now(), '|sinkID:', sinkID, '|EnShare:', encryptedShare); + console.debug(Date.now(), '|sinkID:', sinkID, '|EnShare:', encryptedShare); DB.query("INSERT INTO sinkShares (floID, share) VALUE (?) ON DUPLICATE KEY UPDATE share=?", [[sinkID, encryptedShare], encryptedShare]) .then(_ => null).catch(error => console.error(error)); } diff --git a/src/coupling.js b/src/coupling.js index 7113784..d250af9 100644 --- a/src/coupling.js +++ b/src/coupling.js @@ -98,7 +98,7 @@ function recursiveCoupling(asset, cur_rate, flag = false) { }).catch(error => { //noBuy = error[0], noSell = error[1], reason = error[2] price.noOrder(asset, error[0], error[1]); - console.error(error[2]); + error[3] ? console.debug(error[2]) : console.error(error[2]); //set timeout for next coupling (if not order placement occurs) if (flag) { price.updateLastTime(asset); @@ -145,7 +145,7 @@ function processCoupling(asset, cur_rate) { console.error(error.sell); noSell = null; } - reject([noBuy, noSell, `No valid ${noSell? 'sellOrders': ''} | ${noBuy? 'buyOrders': ''} for Asset: ${asset}`]); + reject([noBuy, noSell, `No valid ${noSell ? 'sellOrders' : ''} | ${noBuy ? 'buyOrders' : ''} for Asset: ${asset}`, true]); }); }) } diff --git a/src/main.js b/src/main.js index 2b8b7bd..54adb1c 100644 --- a/src/main.js +++ b/src/main.js @@ -5,7 +5,7 @@ require('../docs/scripts/lib'); global.floCrypto = require('../docs/scripts/floCrypto'); global.floBlockchainAPI = require('../docs/scripts/floBlockchainAPI'); global.floTokenAPI = require('../docs/scripts/floTokenAPI'); -global.btcOperator = require('../docs/scripts/btcOperator'); +global.btcOperator = require('../docs/scripts/btcOperator'); const Database = require("./database"); const App = require('./app'); @@ -162,11 +162,17 @@ function setDB(db) { } module.exports = function startServer(public_dir) { - const config = require(`../args/config${process.env.I || ""}.json`); + let _pass, _I = ""; + for (let arg of process.argv) { + if (/^-I=/.test(arg)) + _I = arg.split(/=(.*)/s)[1]; + else if (/^-password=/i.test(arg)) + _pass = arg.split(/=(.*)/s)[1]; + } + const config = require(`../args/config${_I}.json`); try { - var _tmp = require(`../args/keys${process.env.I || ""}.json`); + var _tmp = require(`../args/keys${_I}.json`); _tmp = floCrypto.retrieveShamirSecret(_tmp); - var _pass = process.env.PASSWORD; if (!_pass) { console.error('Password not entered!'); process.exit(1); diff --git a/src/market.js b/src/market.js index a95d52b..8dae1cb 100644 --- a/src/market.js +++ b/src/market.js @@ -501,7 +501,7 @@ function periodicProcess() { if (lastSyncBlockHeight < result.blocks[0].height) { lastSyncBlockHeight = result.blocks[0].height; background.process(); - console.debug("Last Block :", lastSyncBlockHeight); + console.log("Last Block :", lastSyncBlockHeight); } }).catch(error => console.error(error)); } diff --git a/src/services/conversion.js b/src/services/conversion.js index 6c9506f..44c7987 100644 --- a/src/services/conversion.js +++ b/src/services/conversion.js @@ -144,7 +144,7 @@ function depositCurrencyFund(floID, txid, coin) { if (result.length) return reject(INVALID(eCode.DUPLICATE_ENTRY, "Transaction already in process")); DB.query("INSERT INTO ConvertFund(txid, mode, coin, r_status) VALUES (?)", [[txid, pCode.CONVERT_MODE_GET, coin, pCode.STATUS_PROCESSING]]) - .then(result => resolve("Add currency fund in process")) + .then(result => resolve("Deposit currency fund in process")) .catch(error => reject(error)) }).catch(error => reject(error)) }) @@ -160,7 +160,7 @@ function depositCoinFund(floID, txid, coin) { if (result.length) return reject(INVALID(eCode.DUPLICATE_ENTRY, "Transaction already in process")); DB.query("INSERT INTO ConvertFund(txid, mode, coin, r_status) VALUES (?)", [[txid, pCode.CONVERT_MODE_PUT, coin, pCode.STATUS_PROCESSING]]) - .then(result => resolve("Add coin fund in process")) + .then(result => resolve("Deposit coin fund in process")) .catch(error => reject(error)) }).catch(error => reject(error)) }) @@ -178,7 +178,7 @@ function withdrawCurrencyFund(floID, coin, amount) { if (available_amount < amount) return reject(INVALID(eCode.INSUFFICIENT_BALANCE, "Insufficient convert-fund deposits to withdraw")); DB.query("INSERT INTO ConvertFund(mode, coin, amount, r_status) VALUES (?)", [[pCode.CONVERT_MODE_PUT, coin, amount, pCode.STATUS_PENDING]]) - .then(result => resolve("Add currency fund in process")) + .then(result => resolve("Withdraw currency fund in process")) .catch(error => reject(error)) }).catch(error => reject(error)) }).catch(error => reject(error)) @@ -197,7 +197,7 @@ function withdrawCoinFund(floID, coin, quantity) { if (available_quantity < quantity) return reject(INVALID(eCode.INSUFFICIENT_BALANCE, "Insufficient convert-fund deposits to withdraw")); DB.query("INSERT INTO ConvertFund(mode, coin, quantity, r_status) VALUES (?)", [[pCode.CONVERT_MODE_GET, coin, quantity, pCode.STATUS_PENDING]]) - .then(result => resolve("Add currency fund in process")) + .then(result => resolve("Withdraw currency fund in process")) .catch(error => reject(error)) }).catch(error => reject(error)) }).catch(error => reject(error)) diff --git a/src/set_globals.js b/src/set_globals.js index f402c37..20cae60 100644 --- a/src/set_globals.js +++ b/src/set_globals.js @@ -13,6 +13,9 @@ try { global[p] = param[p]; } +if (!process.argv.includes("--debug")) + global.console.debug = () => null; + /* //Trace the debug logs in node js var debug = console.debug;