Merge pull request #89 from andrerfneves/bugfix/improve-zcash-conf-parse
Bugfix/improve zcash conf parse
This commit is contained in:
commit
33bf15c8c1
|
@ -4,7 +4,6 @@ import fs from 'fs';
|
||||||
|
|
||||||
import { locateZcashConf } from './locate-zcash-conf';
|
import { locateZcashConf } from './locate-zcash-conf';
|
||||||
import { filterObjectNullKeys } from '../../app/utils/filter-object-null-keys';
|
import { filterObjectNullKeys } from '../../app/utils/filter-object-null-keys';
|
||||||
import store from '../electron-store';
|
|
||||||
|
|
||||||
type ZcashConfFile = {
|
type ZcashConfFile = {
|
||||||
testnet: ?string,
|
testnet: ?string,
|
||||||
|
@ -32,14 +31,15 @@ type ZcashConfFile = {
|
||||||
paytxfee: ?string,
|
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) => {
|
fs.readFile(locateZcashConf(), (err, file) => {
|
||||||
if (err) return reject(err);
|
if (err) return reject(err);
|
||||||
|
|
||||||
const fileString = file.toString();
|
const fileString = file.toString();
|
||||||
|
|
||||||
|
/* eslint-disable no-unused-vars */
|
||||||
// $FlowFixMe
|
// $FlowFixMe
|
||||||
const { rpcuser, rpcpassword, ...payload }: ZcashConfFile = filterObjectNullKeys(
|
const payload: ZcashConfFile = filterObjectNullKeys(
|
||||||
fileString.split('\n').reduce((acc, cur) => {
|
fileString.split('\n').reduce((acc, cur) => {
|
||||||
if (!cur) return acc;
|
if (!cur) return acc;
|
||||||
|
|
||||||
|
@ -52,14 +52,18 @@ export const parseZcashConf = (): Promise<Array<string>> => new Promise((resolve
|
||||||
}, {}),
|
}, {}),
|
||||||
);
|
);
|
||||||
|
|
||||||
store.set('rpcuser', rpcuser || '');
|
resolve(payload);
|
||||||
store.set('rpcpassword', rpcpassword || '');
|
|
||||||
|
|
||||||
// $FlowFixMe
|
|
||||||
resolve(Object.keys(payload).reduce((acc, key) => acc.concat(`-${key}=${payload[key]}`), []));
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/* 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 } => {
|
export const parseCmdArgs = (cmd: string): { user: string, password: string } => {
|
||||||
const rpcUserInArgs = cmd.split(' ').find(x => x.startsWith('-rpcuser'));
|
const rpcUserInArgs = cmd.split(' ').find(x => x.startsWith('-rpcuser'));
|
||||||
const rpcPasswordInArgs = cmd.split(' ').find(x => x.startsWith('-rpcpassword'));
|
const rpcPasswordInArgs = cmd.split(' ').find(x => x.startsWith('-rpcpassword'));
|
||||||
|
|
|
@ -19,7 +19,7 @@ import getDaemonName from './get-daemon-name';
|
||||||
import fetchParams from './run-fetch-params';
|
import fetchParams from './run-fetch-params';
|
||||||
import log from './logger';
|
import log from './logger';
|
||||||
import store from '../electron-store';
|
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 }) => {
|
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));
|
const [, isRunning] = await eres(processExists(ZCASHD_PROCESS_NAME));
|
||||||
|
|
||||||
// This will parse and save rpcuser and rpcpassword in the store
|
// 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) {
|
if (isRunning) {
|
||||||
log('Already is running!');
|
log('Already is running!');
|
||||||
|
@ -85,28 +88,22 @@ const runDaemon: () => Promise<?ChildProcess> = () => new Promise(async (resolve
|
||||||
return 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
|
const rpcCredentials = {
|
||||||
? {
|
|
||||||
username: store.get('rpcuser'),
|
username: store.get('rpcuser'),
|
||||||
password: store.get('rpcpassword'),
|
password: store.get('rpcpassword'),
|
||||||
}
|
|
||||||
: {
|
|
||||||
username: uuid(),
|
|
||||||
password: uuid(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (isDev) log('Rpc Credentials', rpcCredentials);
|
if (isDev) log('Rpc Credentials', rpcCredentials);
|
||||||
|
|
||||||
if (!hasCredentials) {
|
|
||||||
store.set('rpcuser', rpcCredentials.username);
|
|
||||||
store.set('rpcpassword', rpcCredentials.password);
|
|
||||||
}
|
|
||||||
|
|
||||||
const childProcess = cp.spawn(
|
const childProcess = cp.spawn(
|
||||||
processName,
|
processName,
|
||||||
await getDaemonOptions({ ...rpcCredentials, optionsFromZcashConf }),
|
await getDaemonOptions({
|
||||||
|
...rpcCredentials,
|
||||||
|
optionsFromZcashConf: generateArgsFromConf(optionsFromZcashConf),
|
||||||
|
}),
|
||||||
{
|
{
|
||||||
stdio: ['ignore', 'pipe', 'pipe'],
|
stdio: ['ignore', 'pipe', 'pipe'],
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "zec-react-wallet",
|
"name": "zec-react-wallet",
|
||||||
"version": "0.4.4",
|
"version": "0.4.5",
|
||||||
"description": "Zcash Reference Wallet",
|
"description": "Zcash Reference Wallet",
|
||||||
"main": "config/main.js",
|
"main": "config/main.js",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|
|
@ -11,8 +11,8 @@ const RPC = {
|
||||||
host: '127.0.0.1',
|
host: '127.0.0.1',
|
||||||
// port: isDev ? 18232 : 8232,
|
// port: isDev ? 18232 : 8232,
|
||||||
port: 18232, // TODO: Test purposes only
|
port: 18232, // TODO: Test purposes only
|
||||||
user: store.get('rpcuser'),
|
user: store.get('rpcuser') || '',
|
||||||
password: store.get('rpcpassword'),
|
password: store.get('rpcpassword') || '',
|
||||||
};
|
};
|
||||||
|
|
||||||
const client = got.extend({
|
const client = got.extend({
|
||||||
|
|
Loading…
Reference in New Issue