Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: BSD-3-Clause
import debug from 'debug';
import { spawn } from 'child_process';
import { getParityPath } from './getParityPath';
import logCommand from './utils/logCommand';
import { name } from '../package.json';
const logger = debug(`${name}:main`);
/**
* Launch a parity instance to get a secure token.
*/
export const signerNewToken = () =>
new Promise(async (resolve, reject) => {
logger('Requesting new token.');
const parityPath = await getParityPath();
// Generate a new token
const paritySigner = spawn(parityPath, ['signer', 'new-token']);
logger(logCommand(parityPath, ['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];
paritySigner.kill(); // We don't need the signer anymore
logger('Successfully extracted token.');
resolve(token);
}
});
// If after 2s we still didn't find the token, consider it failed.
setTimeout(() => {
reject(new Error('Error extracting token.'));
}, 2000);
});