-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.mjs
More file actions
96 lines (74 loc) · 1.89 KB
/
utils.mjs
File metadata and controls
96 lines (74 loc) · 1.89 KB
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
import { networkInterfaces } from "node:os";
import { Client, PrivateKey, cryptoUtils, Signature } from "@hiveio/dhive";
import axios from "axios";
import {
ACCOUNT,
CORE_MODULE_API,
PORT,
SIGNING_PRIVATE_KEY,
NODES,
CHAIN_ID,
TEST_MODE,
} from "./config.mjs";
export const hiveClient = new Client(NODES, { chainId: CHAIN_ID });
export const isSignedWithActiveKey = async (
signingPublicKey,
rawData,
signature
) => {
let validSignature = false;
try {
const json = JSON.stringify(rawData);
const pubActiveKey = Signature.fromString(signature)
.recover(cryptoUtils.sha256(json), TEST_MODE ? "TST" : "STM")
.toString();
validSignature = pubActiveKey === signingPublicKey;
} catch {
//
}
return validSignature;
};
export const fetchCoreConfig = async () => {
let config = {};
try {
({ data: config } = await axios.get(`${CORE_MODULE_API}/config`));
} catch {
//
}
return config;
};
const getMyIp = () => {
const interfaces = Object.values(networkInterfaces()).flat();
const netInterface = interfaces.find(
(item) => !item.internal && item.family === "IPv4"
);
if (netInterface) {
return netInterface.address;
}
return null;
};
export const pingCoreModule = async () => {
let success = false;
try {
const ip = getMyIp();
if (!ip) throw new Error("IP address can not be determined!");
const postData = {
account: ACCOUNT,
ip,
port: PORT,
};
const sig = PrivateKey.fromString(SIGNING_PRIVATE_KEY)
.sign(cryptoUtils.sha256(JSON.stringify(postData)))
.toString();
const { data } = await axios.post(`${CORE_MODULE_API}/ping`, postData, {
headers: {
"X-Signature": sig,
},
});
success = data.success;
} catch (e) {
const error = e?.response?.data?.message || e.message;
console.error("Ping Error:", error);
}
return success;
};