diff --git a/config/daemon/parse-zcash-conf.js b/config/daemon/parse-zcash-conf.js index 2625c20..de8036e 100644 --- a/config/daemon/parse-zcash-conf.js +++ b/config/daemon/parse-zcash-conf.js @@ -4,7 +4,6 @@ import fs from 'fs'; import { locateZcashConf } from './locate-zcash-conf'; import { filterObjectNullKeys } from '../../app/utils/filter-object-null-keys'; -import store from '../electron-store'; type ZcashConfFile = { testnet: ?string, @@ -32,14 +31,15 @@ type ZcashConfFile = { paytxfee: ?string, }; -export const parseZcashConf = (): Promise> => new Promise((resolve, reject) => { +export const parseZcashConf = (): Promise => new Promise((resolve, reject) => { fs.readFile(locateZcashConf(), (err, file) => { if (err) return reject(err); const fileString = file.toString(); + /* eslint-disable no-unused-vars */ // $FlowFixMe - const { rpcuser, rpcpassword, ...payload }: ZcashConfFile = filterObjectNullKeys( + const payload: ZcashConfFile = filterObjectNullKeys( fileString.split('\n').reduce((acc, cur) => { if (!cur) return acc; @@ -52,14 +52,18 @@ export const parseZcashConf = (): Promise> => new Promise((resolve }, {}), ); - store.set('rpcuser', rpcuser || ''); - store.set('rpcpassword', rpcpassword || ''); - - // $FlowFixMe - resolve(Object.keys(payload).reduce((acc, key) => acc.concat(`-${key}=${payload[key]}`), [])); + resolve(payload); }); }); +/* eslint-disable-next-line max-len */ +export const generateArgsFromConf = (obj: ZcashConfFile): Array => Object.keys(obj).reduce((acc, key) => { + // We can omit the credentials for the command line + if (key === 'rpcuser' || key === 'rpcpassword') return acc; + + return acc.concat(`-${key}=${String(obj[key])}`); +}, []); + export const parseCmdArgs = (cmd: string): { user: string, password: string } => { const rpcUserInArgs = cmd.split(' ').find(x => x.startsWith('-rpcuser')); const rpcPasswordInArgs = cmd.split(' ').find(x => x.startsWith('-rpcpassword')); diff --git a/config/daemon/zcashd-child-process.js b/config/daemon/zcashd-child-process.js index 952c553..6677059 100644 --- a/config/daemon/zcashd-child-process.js +++ b/config/daemon/zcashd-child-process.js @@ -19,7 +19,7 @@ import getDaemonName from './get-daemon-name'; import fetchParams from './run-fetch-params'; import log from './logger'; import store from '../electron-store'; -import { parseZcashConf, parseCmdArgs } from './parse-zcash-conf'; +import { parseZcashConf, parseCmdArgs, generateArgsFromConf } from './parse-zcash-conf'; const getDaemonOptions = ({ username, password, optionsFromZcashConf }) => { /* @@ -70,7 +70,10 @@ const runDaemon: () => Promise = () => new Promise(async (resolve const [, isRunning] = await eres(processExists(ZCASHD_PROCESS_NAME)); // This will parse and save rpcuser and rpcpassword in the store - const [, optionsFromZcashConf = []] = await eres(parseZcashConf()); + const [, optionsFromZcashConf] = await eres(parseZcashConf()); + + if (optionsFromZcashConf.rpcuser) store.set('rpcuser', optionsFromZcashConf.rpcuser); + if (optionsFromZcashConf.rpcpassword) store.set('rpcpassword', optionsFromZcashConf.rpcpassword); if (isRunning) { log('Already is running!'); @@ -85,28 +88,22 @@ const runDaemon: () => Promise = () => new Promise(async (resolve return resolve(); } - const hasCredentials = store.has('rpcuser') && store.has('rpcpassword'); + if (!optionsFromZcashConf.rpcuser) store.set('rpcuser', uuid()); + if (!optionsFromZcashConf.rpcpassword) store.set('rpcpassword', uuid()); - const rpcCredentials = hasCredentials - ? { - username: store.get('rpcuser'), - password: store.get('rpcpassword'), - } - : { - username: uuid(), - password: uuid(), - }; + const rpcCredentials = { + username: store.get('rpcuser'), + password: store.get('rpcpassword'), + }; if (isDev) log('Rpc Credentials', rpcCredentials); - if (!hasCredentials) { - store.set('rpcuser', rpcCredentials.username); - store.set('rpcpassword', rpcCredentials.password); - } - const childProcess = cp.spawn( processName, - await getDaemonOptions({ ...rpcCredentials, optionsFromZcashConf }), + await getDaemonOptions({ + ...rpcCredentials, + optionsFromZcashConf: generateArgsFromConf(optionsFromZcashConf), + }), { stdio: ['ignore', 'pipe', 'pipe'], }, diff --git a/package.json b/package.json index b0ca03d..1d9c1c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zec-react-wallet", - "version": "0.4.4", + "version": "0.4.5", "description": "Zcash Reference Wallet", "main": "config/main.js", "license": "MIT", diff --git a/services/api.js b/services/api.js index dc603a7..8b80cb3 100644 --- a/services/api.js +++ b/services/api.js @@ -11,8 +11,8 @@ const RPC = { host: '127.0.0.1', // port: isDev ? 18232 : 8232, port: 18232, // TODO: Test purposes only - user: store.get('rpcuser'), - password: store.get('rpcpassword'), + user: store.get('rpcuser') || '', + password: store.get('rpcpassword') || '', }; const client = got.extend({