Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pbca26 committed Mar 29, 2018
1 parent 2ef6b45 commit 684afdc
Show file tree
Hide file tree
Showing 27 changed files with 247 additions and 133 deletions.
15 changes: 9 additions & 6 deletions routes/shepherd/addCoinShortcuts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const electrumServers = require('../electrumjs/electrumServers');
const request = require('request');

// TODO: refactor

module.exports = (shepherd) => {
shepherd.startSPV = (coin) => {
Expand Down Expand Up @@ -44,7 +47,7 @@ module.exports = (shepherd) => {
}),
};

shepherd.request(options, (error, response, body) => {
request(options, (error, response, body) => {
if (response &&
response.statusCode &&
response.statusCode === 200) {
Expand Down Expand Up @@ -78,7 +81,7 @@ module.exports = (shepherd) => {
}),
};

shepherd.request(options, (error, response, body) => {
request(options, (error, response, body) => {
if (response &&
response.statusCode &&
response.statusCode === 200) {
Expand Down Expand Up @@ -112,7 +115,7 @@ module.exports = (shepherd) => {
}),
};

shepherd.request(options, (error, response, body) => {
request(options, (error, response, body) => {
if (response &&
response.statusCode &&
response.statusCode === 200) {
Expand Down Expand Up @@ -146,7 +149,7 @@ module.exports = (shepherd) => {
}),
};

shepherd.request(options, (error, response, body) => {
request(options, (error, response, body) => {
if (response &&
response.statusCode &&
response.statusCode === 200) {
Expand Down Expand Up @@ -180,7 +183,7 @@ module.exports = (shepherd) => {
}),
};

shepherd.request(options, (error, response, body) => {
request(options, (error, response, body) => {
if (response &&
response.statusCode &&
response.statusCode === 200) {
Expand Down Expand Up @@ -231,7 +234,7 @@ module.exports = (shepherd) => {
}),
};

shepherd.request(options, (error, response, body) => {
request(options, (error, response, body) => {
if (response &&
response.statusCode &&
response.statusCode === 200) {
Expand Down
12 changes: 8 additions & 4 deletions routes/shepherd/binsUtils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const os = require('os');
const fsnode = require('fs');
const _fs = require('graceful-fs');

module.exports = (shepherd) => {
// osx and linux
shepherd.binFixRights = () => {
const osPlatform = shepherd.os.platform();
const osPlatform = os.platform();
const _bins = [
shepherd.komododBin,
shepherd.komodocliBin
Expand All @@ -10,11 +14,11 @@ module.exports = (shepherd) => {
if (osPlatform === 'darwin' ||
osPlatform === 'linux') {
for (let i = 0; i < _bins.length; i++) {
shepherd._fs.stat(_bins[i], (err, stat) => {
_fs.stat(_bins[i], (err, stat) => {
if (!err) {
if (parseInt(stat.mode.toString(8), 10) !== 100775) {
shepherd.log(`${_bins[i]} fix permissions`);
shepherd.fsnode.chmodSync(_bins[i], '0775');
fsnode.chmodSync(_bins[i], '0775');
}
} else {
shepherd.log(`error: ${_bins[i]} not found`);
Expand All @@ -26,8 +30,8 @@ module.exports = (shepherd) => {

shepherd.killRogueProcess = (processName) => {
// kill rogue process copies on start
const osPlatform = os.platform();
let processGrep;
const osPlatform = shepherd.os.platform();

switch (osPlatform) {
case 'darwin':
Expand Down
11 changes: 7 additions & 4 deletions routes/shepherd/coindWalletKeys.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
const fs = require('fs-extra');
const _fs = require('graceful-fs');
const wif = require('wif');
const bitcoinJS = require('bitcoinjs-lib');

module.exports = (shepherd) => {
/*
* type: GET
*
*/
shepherd.get('/coindwalletkeys', (req, res, next) => {
if (shepherd.checkToken(req.query.token)) {
const wif = require('wif');
const fs = require('fs');
const chain = req.query.chain;

// ref: https://gist.github.com/kendricktan/1e62495150ad236b38616d733aac4eb9
let _walletDatLocation = chain === 'komodo' || chain === 'null' ? `${shepherd.komodoDir}/wallet.dat` : `${shepherd.komodoDir}/${chain}/wallet.dat`;
_walletDatLocation = chain === 'CHIPS' ? `${shepherd.chipsDir}/wallet.dat` : _walletDatLocation;

try {
shepherd._fs.access(_walletDatLocation, shepherd.fs.constants.R_OK, (err) => {
_fs.access(_walletDatLocation, shepherd.fs.constants.R_OK, (err) => {
if (err) {
shepherd.log(`error reading ${_walletDatLocation}`);
successObj = {
Expand Down Expand Up @@ -60,7 +63,7 @@ module.exports = (shepherd) => {
const keyObj = wif.decode(key);
const wifKey = wif.encode(keyObj);

const keyPair = shepherd.bitcoinJS.ECPair.fromWIF(wifKey, shepherd.electrumJSNetworks.komodo);
const keyPair = bitcoinJS.ECPair.fromWIF(wifKey, shepherd.electrumJSNetworks.komodo);
const _keyPair = {
priv: keyPair.toWIF(),
pub: keyPair.getAddress(),
Expand Down
8 changes: 5 additions & 3 deletions routes/shepherd/coinsList.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const fs = require('fs-extra');

module.exports = (shepherd) => {
/*
* type: GET
*
*/
shepherd.get('/coinslist', (req, res, next) => {
if (shepherd.checkToken(req.query.token)) {
if (shepherd.fs.existsSync(`${shepherd.agamaDir}/shepherd/coinslist.json`)) {
shepherd.fs.readFile(`${shepherd.agamaDir}/shepherd/coinslist.json`, 'utf8', (err, data) => {
if (fs.existsSync(`${shepherd.agamaDir}/shepherd/coinslist.json`)) {
fs.readFile(`${shepherd.agamaDir}/shepherd/coinslist.json`, 'utf8', (err, data) => {
if (err) {
const errorObj = {
msg: 'error',
Expand Down Expand Up @@ -57,7 +59,7 @@ module.exports = (shepherd) => {

res.end(JSON.stringify(errorObj));
} else {
shepherd.fs.writeFile(`${shepherd.agamaDir}/shepherd/coinslist.json`, JSON.stringify(_payload), (err) => {
fs.writeFile(`${shepherd.agamaDir}/shepherd/coinslist.json`, JSON.stringify(_payload), (err) => {
if (err) {
const errorObj = {
msg: 'error',
Expand Down
5 changes: 3 additions & 2 deletions routes/shepherd/confMaxconnections.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const fs = require('fs-extra');
const Promise = require('bluebird');

module.exports = (shepherd) => {
shepherd.getMaxconKMDConf = () => {
return new shepherd.Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
fs.readFile(`${shepherd.komodoDir}/komodo.conf`, 'utf8', (err, data) => {
if (err) {
shepherd.log('kmd conf maxconnections param read failed');
Expand All @@ -23,7 +24,7 @@ module.exports = (shepherd) => {
}

shepherd.setMaxconKMDConf = (limit) => {
return new shepherd.Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
fs.readFile(`${shepherd.komodoDir}/komodo.conf`, 'utf8', (err, data) => {
const _maxconVal = limit ? 1 : 10;

Expand Down
21 changes: 13 additions & 8 deletions routes/shepherd/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const fs = require('fs-extra');
const _fs = require('graceful-fs');
const fsnode = require('fs');
const Promise = require('bluebird');

module.exports = (shepherd) => {
shepherd.loadLocalConfig = () => {
if (shepherd.fs.existsSync(`${shepherd.agamaDir}/config.json`)) {
let localAppConfig = shepherd.fs.readFileSync(`${shepherd.agamaDir}/config.json`, 'utf8');
if (fs.existsSync(`${shepherd.agamaDir}/config.json`)) {
let localAppConfig = fs.readFileSync(`${shepherd.agamaDir}/config.json`, 'utf8');

shepherd.log('app config set from local file');
shepherd.writeLog('app config set from local file');
Expand Down Expand Up @@ -53,14 +58,14 @@ module.exports = (shepherd) => {
shepherd.saveLocalAppConf = (appSettings) => {
let appConfFileName = `${shepherd.agamaDir}/config.json`;

shepherd._fs.access(shepherd.agamaDir, shepherd.fs.constants.R_OK, (err) => {
_fs.access(shepherd.agamaDir, shepherd.fs.constants.R_OK, (err) => {
if (!err) {

const FixFilePermissions = () => {
return new shepherd.Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
const result = 'config.json file permissions updated to Read/Write';

shepherd.fsnode.chmodSync(appConfFileName, '0666');
fsnode.chmodSync(appConfFileName, '0666');

setTimeout(() => {
shepherd.log(result);
Expand All @@ -71,10 +76,10 @@ module.exports = (shepherd) => {
}

const FsWrite = () => {
return new shepherd.Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
const result = 'config.json write file is done';

shepherd.fs.writeFile(appConfFileName,
fs.writeFile(appConfFileName,
JSON.stringify(appSettings)
.replace(/,/g, ',\n') // format json in human readable form
.replace(/":/g, '": ')
Expand All @@ -84,7 +89,7 @@ module.exports = (shepherd) => {
return shepherd.log(err);
});

shepherd.fsnode.chmodSync(appConfFileName, '0666');
fsnode.chmodSync(appConfFileName, '0666');
setTimeout(() => {
shepherd.log(result);
shepherd.log(`app conf.json file is created successfully at: ${shepherd.agamaDir}`);
Expand Down
16 changes: 10 additions & 6 deletions routes/shepherd/daemonControl.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ module.exports = (shepherd) => {
}
break;
case 'coind':
DaemonConfPath = _platform === 'win32' ? shepherd.path.normalize(`${shepherd.coindRootDir}/${coind.toLowerCase()}`) : `${shepherd.coindRootDir}/${coind.toLowerCase()}`;
DaemonConfPath = _platform === 'win32' ? path.normalize(`${shepherd.coindRootDir}/${coind.toLowerCase()}`) : `${shepherd.coindRootDir}/${coind.toLowerCase()}`;
break;
default:
DaemonConfPath = `${shepherd.komodoDir}/${flock}`;
if (_platform === 'win32') {
DaemonConfPath = shepherd.path.normalize(DaemonConfPath);
DaemonConfPath = path.normalize(DaemonConfPath);
}
}

Expand Down Expand Up @@ -230,7 +230,11 @@ module.exports = (shepherd) => {
let spawnErr = fs.openSync(_daemonLogName, 'a');

spawn(shepherd.komododBin, _arg, {
stdio: ['ignore', spawnOut, spawnErr],
stdio: [
'ignore',
spawnOut,
spawnErr
],
detached: true,
}).unref();
} else {
Expand Down Expand Up @@ -439,7 +443,7 @@ module.exports = (shepherd) => {
fs.unlink(coindDebugLogLocation);
}
});
} catch(e) {
} catch (e) {
shepherd.log(`coind ${coind} debug.log access err: ${e}`);
shepherd.writeLog(`coind ${coind} debug.log access err: ${e}`);
}
Expand Down Expand Up @@ -475,7 +479,7 @@ module.exports = (shepherd) => {
shepherd.writeLog(`port ${_port} (${coind}) is already in use`);
}
});
} catch(e) {
} catch (e) {
shepherd.log(`failed to start ${coind} err: ${e}`);
shepherd.writeLog(`failed to start ${coind} err: ${e}`);
}
Expand Down Expand Up @@ -609,7 +613,7 @@ module.exports = (shepherd) => {
shepherd.log('rpcuser: OK');
shepherd.writeLog('rpcuser: OK');
} else {
const randomstring = shepherd.md5((Math.random() * Math.random() * 999).toString());
const randomstring = md5((Math.random() * Math.random() * 999).toString());

shepherd.log('rpcuser: NOT FOUND');
shepherd.writeLog('rpcuser: NOT FOUND');
Expand Down
5 changes: 3 additions & 2 deletions routes/shepherd/dashboardUpdate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Promise = require('bluebird');
const request = require('request');

module.exports = (shepherd) => {
/*
Expand All @@ -24,7 +25,7 @@ module.exports = (shepherd) => {
_promiseStack = [
'getinfo',
'listtransactions',
'getbalance',
'getbalance'
];
} else {
_returnObj = {
Expand Down Expand Up @@ -272,7 +273,7 @@ module.exports = (shepherd) => {
timeout: 120000,
};

shepherd.request(options, (error, response, body) => {
request(options, (error, response, body) => {
if (response &&
response.statusCode &&
response.statusCode === 200) {
Expand Down
12 changes: 8 additions & 4 deletions routes/shepherd/debugLog.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const path = require('path');
const _fs = require('graceful-fs');
const Promise = require('bluebird');

module.exports = (shepherd) => {
/*
* type: POST
Expand All @@ -20,7 +24,7 @@ module.exports = (shepherd) => {

if (shepherd.os.platform() === 'win32') {
shepherd.komodoDir = shepherd.appConfig.dataDir.length ? shepherd.appConfig.dataDir : `${process.env.APPDATA}/Komodo`;
shepherd.komodoDir = shepherd.path.normalize(shepherd.komodoDir);
shepherd.komodoDir = path.normalize(shepherd.komodoDir);
}

if (_herd === 'komodo') {
Expand Down Expand Up @@ -93,18 +97,18 @@ module.exports = (shepherd) => {
});

shepherd.readDebugLog = (fileLocation, lastNLines) => {
return new shepherd.Promise(
return new Promise(
(resolve, reject) => {
if (lastNLines) {
try {
shepherd._fs.access(fileLocation, shepherd.fs.constants.R_OK, (err) => {
_fs.access(fileLocation, shepherd.fs.constants.R_OK, (err) => {
if (err) {
shepherd.log(`error reading ${fileLocation}`);
shepherd.writeLog(`error reading ${fileLocation}`);
reject(`readDebugLog error: ${err}`);
} else {
shepherd.log(`reading ${fileLocation}`);
shepherd._fs.readFile(fileLocation, 'utf-8', (err, data) => {
_fs.readFile(fileLocation, 'utf-8', (err, data) => {
if (err) {
shepherd.writeLog(`readDebugLog err: ${err}`);
shepherd.log(`readDebugLog err: ${err}`);
Expand Down
Loading

0 comments on commit 684afdc

Please sign in to comment.