feat(zcashd): load and save rpcuser and rpcpassword from zcash.conf

This commit is contained in:
George Lima 2019-02-04 13:34:58 -03:00
parent 888e06235b
commit 070c7c6047
2 changed files with 48 additions and 12 deletions

View File

@ -3,6 +3,33 @@ 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,
regtest: ?string,
proxy: ?string,
bind: ?string,
whitebind: ?string,
addnode: ?string,
connect: ?string,
listen: ?string,
maxconnections: ?string,
server: ?string,
rpcbind: ?string,
rpcuser: ?string,
rpcpassword: ?string,
rpcclienttimeout: ?string,
rpcallowip: ?string,
rpcport: ?string,
rpcconnect: ?string,
sendfreetransactions: ?string,
txconfirmtarget: ?string,
gen: ?string,
genproclimit: ?string,
keypool: ?string,
paytxfee: ?string,
};
export const parseZcashConf = (): Promise<Array<string>> => new Promise((resolve, reject) => {
fs.readFile(locateZcashConf(), (err, file) => {
@ -11,18 +38,20 @@ export const parseZcashConf = (): Promise<Array<string>> => new Promise((resolve
const fileString = file.toString();
const optionsFromZcashConf = filterObjectNullKeys(
// $FlowFixMe
const { rpcuser, rpcpassword, ...payload }: ZcashConfFile = filterObjectNullKeys(
fileString.split('\n').reduce((acc, cur) => {
const [key, value] = cur.split('=');
return { ...acc, [key]: value };
}, {}),
);
resolve(
Object.keys(optionsFromZcashConf).reduce(
(acc, key) => acc.concat(`-${key}=${optionsFromZcashConf[key]}`),
[],
),
);
if (rpcuser && rpcpassword) {
store.set('rpcuser', rpcuser);
store.set('rpcpassword', rpcpassword);
}
// $FlowFixMe
resolve(Object.keys(payload).reduce((acc, key) => acc.concat(`-${key}=${payload[key]}`), []));
});
});

View File

@ -19,8 +19,7 @@ import log from './logger';
import store from '../electron-store';
import { parseZcashConf } from './parse-zcash-conf';
const getDaemonOptions = async ({ username, password }) => {
const [, optionsFromZcashConf = []] = await eres(parseZcashConf());
const getDaemonOptions = ({ username, password, optionsFromZcashConf }) => {
/*
-showmetrics
Show metrics on stdout
@ -43,6 +42,8 @@ const getDaemonOptions = async ({ username, password }) => {
...optionsFromZcashConf,
];
log(defaultOptions);
return isDev ? defaultOptions.concat(['-testnet', '-addnode=testnet.z.cash']) : defaultOptions;
};
@ -68,6 +69,8 @@ const runDaemon: () => Promise<?ChildProcess> = () => new Promise(async (resolve
return resolve();
}
const [, optionsFromZcashConf = []] = await eres(parseZcashConf());
const hasCredentials = store.has('rpcuser') && store.has('rpcpassword');
const rpcCredentials = hasCredentials
@ -87,9 +90,13 @@ const runDaemon: () => Promise<?ChildProcess> = () => new Promise(async (resolve
store.set('rpcpassword', rpcCredentials.password);
}
const childProcess = cp.spawn(processName, await getDaemonOptions(rpcCredentials), {
stdio: ['ignore', 'pipe', 'pipe'],
});
const childProcess = cp.spawn(
processName,
await getDaemonOptions({ ...rpcCredentials, optionsFromZcashConf }),
{
stdio: ['ignore', 'pipe', 'pipe'],
},
);
childProcess.stdout.on('data', (data) => {
if (!mainWindow.isDestroyed()) mainWindow.webContents.send('zcashd-log', data.toString());