Merge pull request #89 from andrerfneves/bugfix/improve-zcash-conf-parse

Bugfix/improve zcash conf parse
This commit is contained in:
André Neves 2019-03-12 22:00:47 -04:00 committed by GitHub
commit 33bf15c8c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 29 deletions

View File

@ -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<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;
@ -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'));

View File

@ -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<?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!');
@ -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'],
},

View File

@ -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",

View File

@ -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({