-
Notifications
You must be signed in to change notification settings - Fork 24
/
wallet.js
78 lines (68 loc) · 2.62 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
const {ipcMain} = require('electron');
const request = require('request');
const fs = require('fs');
const os = require('os');
const config = require('./config.js').options;
// get config options from wallet daemon file
let array = [];
if (os.platform() === 'win32') {
array = fs.readFileSync(process.env.APPDATA + '/Zclassic/' + 'zclassic.conf', 'UTF-8').toString().split('\r');
} else if (os.platform() === 'darwin') {
array = fs.readFileSync(process.env.HOME + '/Library/Application Support/Zclassic/' + 'zclassic.conf', 'UTF-8').toString().split('\n');
}
for (let i = 0; i < array.length; i++) {
let tmpString = array[i].replace(' ', '').toLowerCase().trim();
if (tmpString.search('rpcuser') > -1) {
var rpcUser = array[i].replace(' ', '').trim().substr(array[i].replace(' ', '').trim().indexOf('=')+1);
}
if (tmpString.search('rpcpassword') > -1) {
var rpcPassword = array[i].replace(' ', '').trim().substr(array[i].replace(' ', '').trim().indexOf('=')+1);
}
if (tmpString.search('rpcport') > -1) {
var rpcPort = array[i].replace(' ', '').trim().substr(array[i].replace(' ', '').trim().indexOf('=')+1);
if (rpcPort == undefined) {
rpcPort = '8232';
}
}
}
if (rpcPort === '' || rpcPort === undefined) {
rpcPort = '8232';
}
ipcMain.on('jsonQuery-request', (event, query) => {
let response = jsonQuery(query, function(response) {
event.sender.send('jsonQuery-reply', response);
});
});
ipcMain.on('jsonQuery-request-sync', (event, query) => {
let response = jsonQuery(query, function(response) {
event.returnValue = response;
});
});
function jsonQuery(query, callback) {
var options = {
method: 'POST',
url: 'http://' + rpcUser + ':' + rpcPassword + '@' + config.rpcIP + ':' + rpcPort,
headers: {
'Content-type': 'text/plain'
},
json: query
};
request(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
callback(response.body);
}
else if (response.statusCode === 401) { // we have an error
console.log('Cannot authenticate with wallet RPC service. Check username and password.');
callback(response.body);
}
else {
console.log('An unknown error occurred sending a JSON query to the wallet daemon. Check configuration settings.');
console.log(response.body);
callback(response.body);
}
});
}
ipcMain.on('coin-request', (event) => {
event.sender.send('coin-reply', config.coin);
});
module.exports = { jsonQuery };