Skip to content

Commit

Permalink
Merge pull request #152 from particl/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
rynomster committed Oct 9, 2017
2 parents 91a4a09 + 5401d64 commit 9122604
Show file tree
Hide file tree
Showing 19 changed files with 556 additions and 431 deletions.
111 changes: 16 additions & 95 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,25 @@ log.transports.file.file = log.transports.file
.findLogPath(log.transports.file.appName)
.replace('log.log', 'partgui.log');

const daemonManager = require('./modules/clientBinaries/clientBinaries');
const rpc = require('./modules/rpc/rpc');
const daemon = require('./modules/rpc/daemon');

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let tray;
let daemon;
let options;

let openDevTools = false;
let options;

function createWindow () {

options = parseArguments();
options.port = options.rpcport
? options.rpcport // custom rpc port
: options.testnet
? 51935 // default testnet port
: 51735; // default mainnet port

rpc.init(options);

// Daemon already running... Start window
rpc.checkDaemon(options).then(() =>initMainWindow(makeTray()))
.catch(_ => log.debug('Daemon not running. It will be started bt the daemon manager'));

// check for daemon version, maybe update, and keep the daemon's process for exit
daemonManager.init(false, options).then(child => {
if (child) {
daemon = child;
}
const _initWindow = () => {
if (!mainWindow) {
const maxRetries = 10; // Some slow computers...
let retries = 0;
const daemonStartup = () => {
rpc.checkDaemon(options)
.then(() => initMainWindow(makeTray()))
.catch(() => retries < maxRetries && setTimeout(daemonStartup, 1000));
retries++;
if (daemon.exitCode || retries >= maxRetries) {
app.exit(991);
}
}
if (daemon && !daemon.exitCode) {
setTimeout(daemonStartup, 1000);
}
initMainWindow(makeTray());
}
}).catch(error => log.error(error));
};

daemon.init(_initWindow);
options = daemon.getOptions();
}

/*
Expand Down Expand Up @@ -202,55 +172,18 @@ function makeTray() {
// Set the tray icon
tray.setToolTip('Particl ' + app.getVersion());
tray.setContextMenu(contextMenu)

return trayImage;
}

/*
** compose options from arguments
**
** exemple:
** --dev -testnet -reindex -rpcuser=user -rpcpassword=pass
** strips --dev out of argv (double dash is not a particld argument) and returns
** {
** dev: true,
** testnet: true,
** reindex: true,
** rpcuser: user,
** rpcpassword: pass
** }
*/
function parseArguments() {

let options = {};
if (path.basename(process.argv[0]).includes('electron')) {

// striping 'electron .' from argv
process.argv = process.argv.splice(2);
} else {
// striping /path/to/particl from argv
process.argv = process.argv.splice(1);
}

process.argv.forEach((arg, index) => {
if (arg.includes('=')) {
arg = arg.split('=');
options[arg[0].substr(1)] = arg[1];
} else if (arg[1] === '-'){
// double dash command
options[arg.substr(2)] = true;
} else if (arg[0] === '-') {
// simple dash command
options[arg.substr(1)] = true;
}

// Always show window when tray icon clicked
tray.on('click',function() {
mainWindow.show()
});
return options;
return trayImage;
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function () {
Expand All @@ -259,27 +192,15 @@ app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})

app.on('quit', function (event, exitCode) {
electron.ipcMain.removeAllListeners(['backend-rpccall']); // Remove all ipc listeners
// kill the particl daemon if initiated on launch
if (daemon && !daemon.exitCode) {
rpc.stopDaemon()
.catch(() => daemon.kill('SIGINT'));
}
if (exitCode === 991) {
throw Error('Could not connect to daemon.');
}
})
});

app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
Expand Down
115 changes: 115 additions & 0 deletions modules/rpc/cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
const fs = require('fs');
const os = require('os');
const path = require('path');
const log = require('electron-log');

/*
** returns Particl config folder
*/
function findCookiePath() {

var homeDir = os.homedir ? os.homedir() : process.env['HOME'];

var dir,
appName = 'Particl';
switch (process.platform) {
case 'linux': {
dir = prepareDir(homeDir, '.' + appName.toLowerCase())
.result;
break;
}

case 'darwin': {
dir = prepareDir(homeDir, 'Library', 'Application Support', appName)
.result;
break;
}

case 'win32': {
dir = prepareDir(process.env['APPDATA'], appName)
.or(homeDir, 'AppData', 'Roaming', appName)
.result;
break;
}
}

if (dir) {
return dir;
} else {
return false;
}
}

/*
** directory resolver
*/
function prepareDir(dirPath) {
// jshint -W040
if (!this || this.or !== prepareDir || !this.result) {
// if dirPath couldn't be resolved
if (!dirPath) {
// return this function to be chained with .or()
return { or: prepareDir };
}

//noinspection JSCheckFunctionSignatures
dirPath = path.join.apply(path, arguments);
mkDir(dirPath);

try {
fs.accessSync(dirPath, fs.W_OK);
} catch (e) {
// return this function to be chained with .or()
return { or: prepareDir };
}
}

return {
or: prepareDir,
result: (this ? this.result : false) || dirPath
};
}

/*
** create a directory
*/
function mkDir(dirPath, root) {
var dirs = dirPath.split(path.sep);
var dir = dirs.shift();
root = (root || '') + dir + path.sep;

try {
fs.mkdirSync(root);
} catch (e) {
if (!fs.statSync(root).isDirectory()) {
throw new Error(e);
}
}

return !dirs.length || mkDir(dirs.join(path.sep), root);
}

/*
** returns the current RPC cookie
** RPC cookie is regenerated at every particld startup
*/
function getAuth(options) {
if (options.rpcuser && options.rpcpassword) {
return options.rpcuser + ':' + options.rpcpassword;
}

// const COOKIE_FILE = findCookiePath() + `${options.testnet ? '/testnet' : ''}/.cookie`;
const COOKIE_FILE = findCookiePath() + (options.testnet ? '/testnet' : '') + '/.cookie';
let auth;

if (fs.existsSync(COOKIE_FILE)) {
auth = fs.readFileSync(COOKIE_FILE, 'utf8').trim();
} else {
auth = undefined;
log.debug('could not find cookie file! path:', COOKIE_FILE);
}

return (auth)
}

exports.getAuth = getAuth;
Loading

0 comments on commit 9122604

Please sign in to comment.