-
Notifications
You must be signed in to change notification settings - Fork 0
/
noderpc.mjs
55 lines (54 loc) · 1.67 KB
/
noderpc.mjs
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
import { homedir } from 'node:os';
import { readFileSync } from 'node:fs';
import { Agent } from 'node:https';
import { request } from 'node:https';
async function nodeRequest(path,data){
const keypath = homedir() + '/.chia/mainnet/config/ssl/full_node/';
let privatekey, privatecert;
try{
privatekey = readFileSync(keypath + "private_full_node.key",{encoding:'utf8',flag:'r'});
}catch(e){
console.log("Could not open full node private key: "+e);
return {success: false};
}
try{
privatecert = readFileSync(keypath + "private_full_node.crt",{encoding:'utf8',flag:'r'});
}catch(e){
console.log("Could not open full node private certificate: "+e);
return {success: false};
}
const clientoptions = {
hostname: '127.0.0.1',
port: 8555,
path: path,
method: 'POST',
key: privatekey,
cert: privatecert
};
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
clientoptions.agent = new Agent(clientoptions);
return new Promise((resolve, reject) => {
const noderequest = request(clientoptions, (noderesponse) => {
let responsedata = '';
noderesponse.on('data', (chunk) => {
responsedata += chunk;
});
noderesponse.on('end', () => {
const responseobject = JSON.parse(responsedata.toString());
resolve(responseobject);
});
noderequest.on('error', (e) => {
console.log("Requesterror:\n"+e);
reject(e);
});
noderequest.on('timeout', () => {
nodereq.destroy();
console.log("RPC Error:\n"+e);
reject(new Error('Connection timeout'));
});
});
noderequest.write(data);
noderequest.end();
});
}
export { nodeRequest };