This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc-client.js
131 lines (111 loc) · 3.82 KB
/
rpc-client.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
const fs = require('fs')
const path = require('path')
const os = require('os')
const electron = require('electron')
const {ipcRenderer} = electron
const dialog = require('dialog')
const smartcashNodeClient = require('node-smartcash')
let config, client
init()
function init() {
// check for default config file, create an empty one if it doesn't exist
if (!fs.existsSync('config')) {
try {
fs.mkdirSync('config')
}
catch(err) {
dialog.err('You must run SmartSweeper as an Administrator.', 'Error', function() { process.exit(1) })
}
}
if (fs.existsSync('config')) {
var defaultConfigPath = path.join('config', 'development.json')
try {
fs.openSync(defaultConfigPath, 'r')
}
catch(err) {
if (err.code === "ENOENT")
fs.writeFileSync(defaultConfigPath, '{}')
}
config = require("exp-config")
// check for the config file, create it if it doesn't exist
try {
fs.openSync('.env', 'r')
client = new smartcashNodeClient.Client({
host: config.rpc.host,
port: config.rpc.port,
user: config.rpc.username,
pass: config.rpc.password,
timeout: 20000
})
}
catch(err) {
if (err.code === "ENOENT") {
var defaultSmartcashPath
var content = "rpc.host=127.0.0.1\n"
content += "rpc.port=9678\n"
content += "rpc.username=rpcusername\n"
content += "rpc.password=rpcpassword\n"
if (os.platform() === "win32")
defaultSmartcashPath = "C:\\Program Files\\SmartCash\\"
else if (os.platform() === "darwin")
defaultSmartcashPath = "//Applications//"
else if (os.platform() === "linux")
defaultSmartcashPath = ""
content += "smartcashPath=" + defaultSmartcashPath
fs.writeFileSync('.env', content)
config = { rpc: {} }
config.rpc.host = "127.0.0.1"
config.rpc.port = "9678"
config.rpc.username = "rpcusername"
config.rpc.password = "rpcpassword"
config.smartcashPath = defaultSmartcashPath
client = new smartcashNodeClient.Client({
host: config.rpc.host,
port: config.rpc.port,
user: config.rpc.username,
pass: config.rpc.password,
timeout: 20000
})
}
else {
var content = {
text: {
title: 'Error',
body: 'The configuration file cannot be loaded. SmartSweeper will now exit.'
},
fatal: true
}
ipcRenderer.send('showErrorDialog', content.text)
}
console.log(client)
}
}
}
// send a command to the RPC server
function sendCmd(cmd, callback) {
client.cmd(cmd.method, cmd.params, function(err, result, resHeaders) {
if (err) {
/*console.log('sendCmd')
console.log(err)
console.log(result)
console.log(resHeaders)*/
callback(true, err.stack)
}
else
callback(false, result)
})
}
// checks to see whether or not the client can communicate with the core
function rpcCheck(callback) {
var cmd = {
method: 'getblockcount',
params: []
}
sendCmd(cmd, function(err, resp) {
callback({msg: resp, err: err})
})
}
module.exports = {
sendCmd: sendCmd,
rpcCheck: rpcCheck
}