forked from billaue2/cryptonote-sumokoin-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiInterfaces.js
93 lines (83 loc) · 2.46 KB
/
apiInterfaces.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
var http = require('http');
var https = require('https');
function jsonHttpRequest(host, port, data, callback, path){
path = path || '/json_rpc';
var options = {
hostname: host,
port: port,
path: path,
method: data ? 'POST' : 'GET',
headers: {
'Content-Length': data.length,
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
var req = (port == 443 ? https : http).request(options, function(res){
var replyData = '';
res.setEncoding('utf8');
res.on('data', function(chunk){
replyData += chunk;
});
res.on('end', function(){
var replyJson;
try{
replyJson = JSON.parse(replyData);
}
catch(e){
callback(e);
return;
}
callback(null, replyJson);
});
});
req.on('error', function(e){
callback(e);
});
req.end(data);
}
function rpc(host, port, method, params, callback){
var data = JSON.stringify({
id: "0",
jsonrpc: "2.0",
method: method,
params: params
});
jsonHttpRequest(host, port, data, function(error, replyJson){
if (error){
callback(error);
return;
}
callback(replyJson.error, replyJson.result)
});
}
function batchRpc(host, port, array, callback){
var rpcArray = [];
for (var i = 0; i < array.length; i++){
rpcArray.push({
id: i.toString(),
jsonrpc: "2.0",
method: array[i][0],
params: array[i][1]
});
}
var data = JSON.stringify(rpcArray);
jsonHttpRequest(host, port, data, callback);
}
module.exports = function(daemonConfig, walletConfig, poolApiConfig){
return {
batchRpcDaemon: function(batchArray, callback){
batchRpc(daemonConfig.host, daemonConfig.port, batchArray, callback);
},
rpcDaemon: function(method, params, callback){
rpc(daemonConfig.host, daemonConfig.port, method, params, callback);
},
rpcWallet: function(method, params, callback){
rpc(walletConfig.host, walletConfig.port, method, params, callback);
},
pool: function(method, callback){
jsonHttpRequest('127.0.0.1', poolApiConfig.port, '', callback, method);
},
jsonHttpRequest: jsonHttpRequest
}
};