forked from WaveringAna/eleos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.js
153 lines (137 loc) · 4.97 KB
/
wallet.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
// @flow
/*jshint esversion: 6 */
/*jslint node: true */
"use strict";
const {app, dialog, ipcMain} = require("electron");
const fs = require("fs");
const os = require("os");
const readline = require("readline");
const request = require("request");
let config = require("./main.js").getConfig();
// set default coin config location
let coinConf;
// function existsSync(filename) {
// try {
// fs.accessSync(filename);
// return true;
// } catch(ex) {
// return false;
// }
// }
if ((config.confPathWin.length > 0 && fs.existsSync(config.confPathWin)) ||
(config.confPathMacOS.length > 0 && fs.existsSync(config.confPathWin)) ||
(config.confPathLinux.length > 0 && fs.existsSync(config.confPathWin))) {
if (os.platform() === "win32") {
coinConf = config.confPathWin;
}
if (os.platform() === "darwin") {
coinConf = config.confPathMacOS;
}
if (os.platform() === "linux") {
coinConf = config.confPathLinux;
}
} else {
if ((config.coin.toLowerCase() === "zcl") && ((os.platform() === "win32") || (os.platform() === "darwin"))) {
coinConf = app.getPath("appData") + "/Zclassic/zclassic.conf";
} else if (config.coin.toLowerCase() === "zcl") {
coinConf = app.getPath("home") + "/.zclassic/zclassic.conf";
}
if ((config.coin.toLowerCase() === "zec") && ((os.platform() === "win32") || (os.platform() === "darwin"))) {
coinConf = app.getPath("appData") + "/Zcash/zcash.conf";
} else if (config.coin.toLowerCase() === "zec") {
coinConf = app.getPath("home") + "/.zcash/zcash.conf";
}
if ((config.coin.toLowerCase() === "zen") && ((os.platform() === "win32") || (os.platform() === "darwin"))) {
coinConf = app.getPath("appData") + "/Zen/zen.conf";
} else if (config.coin.toLowerCase() === "zen") {
coinConf = app.getPath("home") + "/.zen/zen.conf";
}
}
// get config options from wallet daemon file
let rl;
let rpcOpts = {};
let rpcUser;
let rpcPassword;
let rpcIP;
let rpcPort;
if (!fs.existsSync(coinConf)) {
console.log("Invalid path " + coinConf + " for wallet config file. Check config.json for accuracy.");
//return;
} else {
rl = readline.createInterface({input: fs.createReadStream(coinConf)});
}
rl.on("line", function (line) {
line.trim();
rpcOpts[line.split("=", 2)[0].toLowerCase()] = line.split("=", 2)[1];
});
rl.on("close", function () {
// set RPC communication options
rpcUser = rpcOpts.rpcuser ? rpcOpts.rpcuser : config.rpcUser;
rpcPassword = rpcOpts.rpcpassword ? rpcOpts.rpcpassword : config.rpcPassword;
rpcIP = config.rpcIP.length > 0 ? config.rpcIP : "127.0.0.1";
//rpcPort = rpcOpts.rpcport ? rpcOpts.rpcport : (config.rpcPort.length > 0 ? config.rpcPort : (config.coin == "zcl" ? 8232 : 8233));
if (rpcOpts.rpcport) {
rpcPort = rpcOpts.rpcport;
} else {
if (config.rpcPort.length > 0) {
rpcPort = config.rpcPort;
} else {
if (config.coin === "zcl") {
rpcPort = 8232;
} else if (config.coin === "zec") {
rpcPort = 8233;
} else if (config.coin === "zen") {
rpcPort = 8231;
}
}
}
// authentication for terminal
let HtAuth = require("ht-auth");
let htAuth = HtAuth.create({file: (app.getPath("userData") + "/eleos.htpasswd")});
htAuth.add({username: rpcUser, password: rpcPassword, force: true}, function (err) {
// initialize xtermjs
const term = require("./xterm.js");
});
});
function jsonQuery(query, callback) {
if (rpcUser.length === 0 || rpcPassword.length === 0 || rpcIP.length === 0 || rpcPort.length === 0) {
return;
}
let options = {
method: "POST",
url: encodeURI("http://" + rpcUser + ":" + rpcPassword + "@" + rpcIP + ":" + rpcPort),
headers: {
"Content-type": "text/plain"
},
json: query
};
request(options, function (error, response, body) {
if (!error && response.statusCode === 401) { // we have an error
console.log("Cannot authenticate with wallet RPC service. Check username and password.");
callback(response.body);
} else if (!error) {
callback(response.body);
}
});
}
ipcMain.on("jsonQuery-request", function (event, query) {
jsonQuery(query, function (response) {
event.sender.send("jsonQuery-reply", response);
});
});
ipcMain.on("jsonQuery-request-sync", function (event, query) {
jsonQuery(query, function (response) {
event.returnValue = response;
});
});
ipcMain.on("coin-request", function (event) {
if (config.coin.length === 0) {
event.sender.send("coin-reply", "zcl");
} else {
event.sender.send("coin-reply", config.coin.toLowerCase());
}
});
function getCredentials() {
return {rpcUser: rpcUser, rpcPassword: rpcPassword, rpcIP: rpcIP, rpcPort: rpcPort};
}
module.exports = {getCredentials, jsonQuery};