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

Commit

Permalink
Merge pull request #24 from Parity-JS/am-auto-sign
Browse files Browse the repository at this point in the history
Auto sign token when necessary
  • Loading branch information
amaury1093 authored Mar 21, 2018
2 parents 235d690 + 55a7ede commit 24ec91f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 23 deletions.
59 changes: 39 additions & 20 deletions src/Connection/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,45 @@ class Connection extends Component {
versionInfoStore = stores.parity.versionInfo().get(this.context.api)

componentDidMount () {
if (isElectron()) {
const { ipcRenderer, remote } = electron;
const parityInstallLocation = remote.getGlobal('parityInstallLocation');

this.setState({ parityInstallLocation });

// Run parity if parityInstallLocation !== null and not connected yet
if (!parityInstallLocation) { return; }

// After 3s, check if ui is still isConnecting
// If yes, then try to run `parity`
// The reason why we do this after 3s, is that even when parity is
// running, isConnecting is true on componentWillMount (lag to ping the
// node). -Amaury 13.03.2018
// TODO Find a more reliable way to know if parity is running or not
setTimeout(() => {
if (!this.props.isConnecting) { return; }
console.log('Launching parity.');
ipcRenderer.send('asynchronous-message', 'run-parity');
}, 3000);
if (!isElectron()) { return; }
const { ipcRenderer, remote } = electron;
const parityInstallLocation = remote.getGlobal('parityInstallLocation');

this.setState({ parityInstallLocation });

// Run parity if parityInstallLocation !== null and not connected yet
if (!parityInstallLocation) { return; }

// After 3s, check if ui is still isConnecting
// If yes, then try to run `parity`
// The reason why we do this after 3s, is that even when parity is
// running, isConnecting is true on componentWillMount (lag to ping the
// node). -Amaury 13.03.2018
// TODO Find a more reliable way to know if parity is running or not
setTimeout(() => {
if (!this.props.isConnecting) { return; }
console.log('Launching parity.');
ipcRenderer.send('asynchronous-message', 'run-parity');
}, 3000);
}

componentWillReceiveProps ({ needsToken }) {
if (!isElectron()) { return; }
// needsToken is set to false by default at the beginning. If the UI needs
// a token, then it will be set to true. At that point, we send an IPC
// message to the main process to attempt to run `parity signer new-token`
// automatically.
if (!this.props.needsToken && needsToken) {
const { ipcRenderer } = electron;

ipcRenderer.send('asynchronous-message', 'signer-new-token');

ipcRenderer.once('asynchronous-reply', (_, arg) => {
if (!arg) { return; }
// If `parity signer new-token` has successfully given us a token back,
// then we submit it
this.onChangeToken(null, arg);
});
}
}

Expand Down
30 changes: 27 additions & 3 deletions src/index.electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,33 @@ function createWindow () {

// Listen to messages from renderer process
ipcMain.on('asynchronous-message', (event, arg) => {
// Run an instance of parity if we receive the `run-parity` message
if (arg === 'run-parity') {
parity = spawn(global.parityInstallLocation, ['--ws-origins', 'parity://*.ui.parity']); // Argument for retro-compatibility with <1.10 versions
switch (arg) {
case 'run-parity': {
// Run an instance of parity if we receive the `run-parity` message
parity = spawn(global.parityInstallLocation, ['--ws-origins', 'parity://*.ui.parity']); // Argument for retro-compatibility with <1.10 versions
break;
}
case 'signer-new-token': {
// Generate a new token if we can find the parity binary
if (!global.parityInstallLocation) { return; }
const paritySigner = spawn(global.parityInstallLocation, ['signer', 'new-token']);

// Listen to the output of the previous command
paritySigner.stdout.on('data', (data) => {
// If the output line is xxxx-xxxx-xxxx-xxxx, then it's our token
const match = data.toString().match(/[a-zA-Z0-9]{4}(-)?[a-zA-Z0-9]{4}(-)?[a-zA-Z0-9]{4}(-)?[a-zA-Z0-9]{4}/);

if (match) {
const token = match[0];

// Send back the token to the renderer process
event.sender.send('asynchronous-reply', token);
paritySigner.kill(); // We don't need the signer anymore
}
});
break;
}
default:
}
});

Expand Down

0 comments on commit 24ec91f

Please sign in to comment.