From c5ff3622eaeba0cc675434fa10e83f55b73bedce Mon Sep 17 00:00:00 2001 From: Michele Marcucci Date: Fri, 12 Apr 2024 11:31:46 +0200 Subject: [PATCH] Removed redundant code --- src/init.js | 112 ++++---------------------------- src/store/api/auth/authSetup.js | 1 - src/utils.js | 13 ++-- 3 files changed, 18 insertions(+), 108 deletions(-) diff --git a/src/init.js b/src/init.js index fcd751b..1b2e17f 100644 --- a/src/init.js +++ b/src/init.js @@ -30,23 +30,22 @@ const initEnvFile = async () => { const runMigrations = async () => { try { console.log('Run migrations'); - const resp = await knex.migrate.latest(); - await createCkpoolConfigFile(); - await createBitcoinConfigFile(); - await runGenerateBitcoinPassword(); - } catch (err) { - console.log(err); - } -}; - -const runGenerateBitcoinPassword = async () => { - try { - console.log('Checking bitcoin password existence'); + await knex.migrate.latest(); const [settings] = await knex('settings') .select(['node_rpc_password as nodeRpcPassword']) .orderBy('created_at', 'desc') .orderBy('id', 'desc') .limit(1); + await utils.auth.manageBitcoinConf(settings); + await runGenerateBitcoinPassword(settings); + } catch (err) { + console.log(err); + } +}; + +const runGenerateBitcoinPassword = async (settings) => { + try { + console.log('Checking bitcoin password existence'); if (settings && settings.nodeRpcPassword) return console.log('Bitcoin password found'); @@ -56,95 +55,6 @@ const runGenerateBitcoinPassword = async () => { } }; -const createCkpoolConfigFile = async () => { - const configFilePath = path.resolve( - __dirname, - '../backend/ckpool/ckpool.conf' - ); - const configContent = `{ - "btcd": [ - { - "url": "127.0.0.1:8332", - "auth": "futurebit", - "pass": "", - "notify": true - } - ], - "logdir": "/opt/apolloapi/backend/ckpool/logs" -}`; - - try { - // Check if the file exists - await fs.access(configFilePath); - console.log('File ckpool.conf already exists.'); - } catch (error) { - try { - // Create the file - await fs.writeFile(configFilePath, configContent, 'utf-8'); - console.log('File ckpool.conf created.'); - } catch (error) { - console.error( - `Error during the creation of the file ckpool.conf: ${error.message}` - ); - } - } finally { - const [settings] = await knex('settings') - .select(['node_rpc_password as nodeRpcPassword']) - .orderBy('created_at', 'desc') - .orderBy('id', 'desc') - .limit(1); - - if (settings && settings.nodeRpcPassword) { - exec( - `sudo sed -i 's#"pass":.*#"pass": "${settings.nodeRpcPassword}",#g' ${configFilePath}` - ); - } - } -}; - -const createBitcoinConfigFile = async () => { - const configFilePath = path.resolve( - __dirname, - '../backend/node/bitcoin.conf' - ); - const configContent = `server=1 -rpcuser=futurebit -rpcpassword= -daemon=0 -maxconnections=32 -upnp=1 -uacomment=FutureBit-Apollo-Node`; - - try { - // Check if the file exists - await fs.access(configFilePath); - console.log('File bitcoin.conf already exists.'); - } catch (error) { - try { - // Create the file - await fs.writeFile(configFilePath, configContent, 'utf-8'); - console.log('File bitcoin.conf created.'); - await utils.auth.changeNodeRpcPassword(); - } catch (error) { - console.error( - `Error during the creation of the file bitcoin.conf: ${error.message}` - ); - } - } finally { - const [settings] = await knex('settings') - .select(['node_rpc_password as nodeRpcPassword']) - .orderBy('created_at', 'desc') - .orderBy('id', 'desc') - .limit(1); - - if (settings && settings.nodeRpcPassword) { - exec( - `sudo sed -i 's/rpcpassword.*/rpcpassword=${settings.nodeRpcPassword}/g' ${configFilePath}` - ); - } - } -}; - initEnvFile(); runMigrations().then(startServer); diff --git a/src/store/api/auth/authSetup.js b/src/store/api/auth/authSetup.js index 6bf54a6..549678f 100644 --- a/src/store/api/auth/authSetup.js +++ b/src/store/api/auth/authSetup.js @@ -11,7 +11,6 @@ module.exports = ({ define }) => { }) utils.auth.changeSystemPassword(password) - await utils.auth.changeNodeRpcPassword() } catch (err) { console.log('ERROR', err); } diff --git a/src/utils.js b/src/utils.js index 77749ff..af2f146 100644 --- a/src/utils.js +++ b/src/utils.js @@ -55,11 +55,11 @@ module.exports.auth = { await fsPromises.access(configCkpoolFilePath); exec( - `sudo sed -i s/rpcpassword.*/rpcpassword=${password}/g ${configBitcoinFilePath}` + `sudo sed -i 's/rpcpassword.*/rpcpassword=${password}/g' ${configBitcoinFilePath}` ); exec( - `sudo sed -i 's#"pass": ""#"pass": "${password}"#g' ${configCkpoolFilePath}` + `sudo sed -i 's#"pass":.*#"pass": "${password}",#g' ${configCkpoolFilePath}` ); exec('sudo systemctl restart node'); @@ -109,11 +109,11 @@ module.exports.auth = { const defaultConf = `server=1\nrpcuser=futurebit\nrpcpassword=${settings.nodeRpcPassword}\ndaemon=0\nmaxconnections=32\nupnp=1\nuacomment=FutureBit-Apollo-Node`; let conf = defaultConf; + this.manageCkpoolConf(settings); + if (settings.nodeEnableSoloMining) { conf += `\n#SOLO_START\nzmqpubhashblock=tcp://127.0.0.1:28332\n#SOLO_END`; - this.manageCkpoolConf(settings); - exec( `sudo cp ${configCkpoolServiceFilePath} /etc/systemd/system/ckpool.service` ); @@ -139,8 +139,9 @@ module.exports.auth = { console.log('Writing Bitcoin conf file', conf); - exec( - `echo "${conf}" | sudo tee ${configBitcoinFilePath}` + await fsPromises.writeFile( + configBitcoinFilePath, + conf ); exec('sudo systemctl restart node');