-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (66 loc) · 2.62 KB
/
index.js
File metadata and controls
80 lines (66 loc) · 2.62 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const fs = require('fs');
let DAPP = require("./node/index");
//some static modules for future maybe
DAPP.initConfig = (path) => {
let def_cnf = {
"network": "",
"test": {
},
"main": {
}
};
let firstRun = false;
let crypto = require('./node/modules/crypto/index')
//if config is not exist or no have "node" key in selected network - add "node" value
try {
if (fs.existsSync(path)) {
let json = JSON.parse(fs.readFileSync(path).toString('utf8'));
if (!json['main']['node'] || !json['main']['node']['privateKey']) {
let keystore = crypto.createKeyPair();
json['main']['node'] = {
privateKey: keystore.private,
publicKey: keystore.publicKey
};
}
if (!json['test']['node'] || !json['test']['node']['privateKey']) {
firstRun = true;
let keystore = crypto.createKeyPair();
json['test']['node'] = {
privateKey: keystore.private,
publicKey: keystore.publicKey
};
}
//if have only privateKey?
//generate public!
if (!json['main']['node']['publicKey'] && json['main']['node']['privateKey']) {
firstRun = true;//i think its first run too.
json['main']['node']['publicKey'] = crypto.getPublicByPrivate(json['main']['node']['privateKey']);
}
if (!json['test']['node']['publicKey'] && json['test']['node']['privateKey']) {
firstRun = true;//i think its first run too.
json['test']['node']['publicKey'] = crypto.getPublicByPrivate(json['test']['node']['privateKey']);
}
fs.writeFileSync(path, JSON.stringify(json, null, ' '));
} else
throw new Error('not found');
} catch (err) {
let keystore = crypto.createKeyPair();
def_cnf['test']['node'] = {
privateKey: keystore.private,
publicKey: keystore.public
};
let keystore2 = crypto.createKeyPair();
def_cnf['main']['node'] = {
privateKey: keystore2.private,
publicKey: keystore2.public
};
fs.writeFileSync(path, JSON.stringify(def_cnf, null, ' '));
firstRun = true;
}
return firstRun;
}
DAPP.create = (configFile, network) => {
let firstRun = DAPP.initConfig(configFile);//preinstall config before first run
return new DAPP(require(configFile), network, firstRun);
}
module.exports = DAPP;