Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
Merge pull request #89 from andrerfneves/bugfix/improve-zcash-conf-parse
Browse files Browse the repository at this point in the history
Bugfix/improve zcash conf parse
  • Loading branch information
andrerfneves authored Mar 13, 2019
2 parents e1b40a7 + b57a7e8 commit 33bf15c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
20 changes: 12 additions & 8 deletions config/daemon/parse-zcash-conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -32,14 +31,15 @@ type ZcashConfFile = {
paytxfee: ?string,
};

export const parseZcashConf = (): Promise<Array<string>> => new Promise((resolve, reject) => {
export const parseZcashConf = (): Promise<ZcashConfFile> => 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;

Expand All @@ -52,14 +52,18 @@ export const parseZcashConf = (): Promise<Array<string>> => 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<string> => 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'));
Expand Down
33 changes: 15 additions & 18 deletions config/daemon/zcashd-child-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => {
/*
Expand Down Expand Up @@ -70,7 +70,10 @@ const runDaemon: () => Promise<?ChildProcess> = () => 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!');
Expand All @@ -85,28 +88,22 @@ const runDaemon: () => Promise<?ChildProcess> = () => 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'],
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions services/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 33bf15c

Please sign in to comment.