forked from VerusCoin/Verusbridgekeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
confFile.js
215 lines (170 loc) · 5.98 KB
/
confFile.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const settings = require('./setup');
const fs = require('fs');
const os = require('os');
const path = require('path');
var ini = require('ini');
const CONSTANTS = require('./constants');
const rootPath = function (chainName, currency) {
let chaintc = chainName.toUpperCase();
const pbaasFolder = settings.pbaas[currency]; //TODO: Make modular
const pbaasRoot = settings.pbaasRoot[chaintc];
let confPath;
let homeDir = os.homedir();
switch (os.platform()) {
case 'darwin':
confPath = homeDir + "/Library/Application Support" + pbaasRoot.darwin + pbaasFolder.darwin;
break;
case 'win32':
confPath = global.HOME + pbaasRoot.win32 + pbaasFolder.win32;
confPath = path.normalize(confPath);
break;
case 'linux':
confPath = homeDir + pbaasRoot.linux + pbaasFolder.linux;
break;
}
return confPath;
}
const getVerusConf = (chainName) => {
let chaintc = chainName.toUpperCase();
const vrscFolder = settings.coin[chaintc];
const vrscFile = settings.verusConfFile[chaintc];
let confPath;
let homeDir = os.homedir();
switch (os.platform()) {
case 'darwin':
confPath = homeDir + "/Library/Application Support" + vrscFolder.darwin + vrscFile;
break;
case 'win32':
confPath = global.HOME + vrscFolder.win32 + vrscFile;
confPath = path.normalize(confPath);
break;
case 'linux':
confPath = homeDir + vrscFolder.linux + vrscFile;
break;
}
let _data;
try {
_data = fs.readFileSync(confPath, 'utf8');
} catch (error) {
throw (error);
}
if (!_data.length) {
throw (new Error("No data in veth.conf file"));
}
var config = ini.parse(_data);
return config;
}
const checkConfFileExists = function (chainName) {
let chaintc = chainName.toUpperCase();
const ID = CONSTANTS.VETHIDHEXREVERSED[chaintc]
let confPath = rootPath(chainName, ID);
return fs.existsSync(confPath);
}
const loadConfFile = (chainName) => {
let chaintc = chainName.toUpperCase();
const ID = CONSTANTS.VETHIDHEXREVERSED[chaintc]
let Config = settings.INIKeys;
let rpcconf = {};
let confPath = rootPath(chainName, ID);
if (!fs.existsSync(confPath)) {
fs.mkdirSync(confPath, { recursive: true });
}
let _data = {};
try {
_data = fs.readFileSync(confPath + '/' + ID + '.conf', 'utf8');
} catch (error) {
if (error.code != 'ENOENT') {
console.log("Error reading file at: ", confPath + "\nError: " + error.message);
}
}
if (_data.length && fs.existsSync(confPath + '/' + ID + '.conf')) {
let _match;
console.log("(veth.conf) file found at: ", confPath);
for (const [key, value] of Object.entries(Config)) {
if (_match = _data.match(`${key}` + '=\\n*(.*)')) {
if (_match[1] != "empty") {
Config[key] = _match[1];
} else {
console.log("Empty veth.conf file value: ", `${key}:"empty" `);
}
}
}
rpcconf = Config;
} else {
let err = fs.writeFileSync(confPath + '/' + ID + '.conf', "", 'utf8');
if (err) {
console.log(err, 'Errror writing veth.conf', err.message);
}
for (const [key, value] of Object.entries(settings.RPCDefault[chaintc])) {
fs.appendFileSync(confPath + '/' + ID + '.conf', `${key}=${value}` + "\n");
}
let tempvalues = fs.readFileSync(confPath + '/' + ID + '.conf', 'utf8');
console.log("Quitting....\n\nPlease check veth.conf file located at: ", path.normalize(confPath + '/' + ID + '.conf'));
console.log("Default Values:\n", ini.parse(tempvalues, 'utf-8'))
}
return rpcconf;
}
const set_conf = (key, infuraLink, ethContract, chainName)=> {
if (!infuraLink && !ethContract)
{
return new Error("No data set, please fill in a form");
}
if (!checkConfFileExists(chainName))
{
loadConfFile(chainName);
}
let chaintc = chainName.toUpperCase();
const ID = CONSTANTS.VETHIDHEXREVERSED[chaintc]
confPath = rootPath(chainName, ID);
let confKeys = settings.RPCDefault[chaintc];
let _data = {};
if (key) {
confKeys.privatekey = key;
}
if (infuraLink) {
confKeys.ethnode = infuraLink;
}
if (ethContract) {
confKeys.delegatorcontractaddress = ethContract;
}
try {
_data = fs.readFileSync(confPath + '/' + ID + '.conf', 'utf8');
} catch (error) {
if (error.code === 'ENOENT') {
let err = fs.writeFileSync(confPath + '/' + ID + '.conf', "", 'utf8');
} else {
throw (error);
}
}
if (_data.length && fs.existsSync(confPath + '/' + ID + '.conf')) {
var config = ini.parse(_data)
if (key) {
config.privatekey = key;
}
if (infuraLink) {
config.ethnode = infuraLink;
}
if (ethContract) {
config.delegatorcontractaddress = ethContract;
}
fs.truncateSync(confPath + '/' + ID + '.conf', 0);
for (const [key, value] of Object.entries(config)) {
fs.appendFileSync(confPath + '/' + ID + '.conf', `${key}=${value}` + "\n");
}
return "Conf file updated";
}
else {
if (!(infuraLink && ethContract)) {
throw new Error("Please fill in all fields");
} else if (infuraLink.slice(0,4) === "http") {
throw new Error("Please use the wws:// protocol for the Eth node");
}
for (const [key, value] of Object.entries(confKeys)) {
fs.appendFileSync(confPath + '/' + ID + '.conf', `${key}=${value}` + "\n");
}
return "Conf file created";
}
};
exports.set_conf = set_conf;
exports.loadConfFile = loadConfFile;
exports.getVerusConf = getVerusConf;