-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
104 lines (93 loc) · 2.62 KB
/
utils.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
const crypto = require('crypto');
const config = require('./config');
const { RPC } = require('node-kaspa-rpc');
const kaspaRPC = new RPC(config.KASPA_RPC_HOST, config.KASPA_RPC_PORT, config.KASPA_RPC_USER, config.KASPA_RPC_PASS);
const jwt = require('jsonwebtoken');
async function generateBlockHeader() {
try {
const response = await kaspaRPC.getBlockTemplate();
const { headerData, target, isSynch } = response.result;
if (!isSynch){
console.log("WARNING: Kaspa node not synched");
}
return {
headerData,
target
};
} catch (error) {
console.error('Error fetching block template:', error);
return null;
}
}
function difficultyToTarget(difficulty) {
if (difficulty === 1) {
return "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
} else {
console.error("ERROR: Solo pool can only use difficulty 1");
return null;
}
}
function bitsToTarget(bits){
const shift = (bits >> 24) & 0xff;
const value = bits & 0x00ffffff;
const target = value * (2 ** (8 * (shift - 3)));
let target_hex = target.toString(16).padStart(64,'0');
if(target_hex.length>64){
target_hex = 'f'.repeat(64);
}
return target_hex;
}
async function calculateBlockReward(){
try {
const response = await kaspaRPC.getBlockTemplate();
const blockReward = response.result.blockreward;
return blockReward;
}
catch (error){
console.error("Error getting block reward from kaspa RPC", error);
return 0;
}
}
async function getBlockHash(headerBytes) {
try {
const response = await kaspaRPC.submitBlock({
header: headerBytes
});
return response.result;
} catch (error) {
console.error("Error getting block hash from kaspa RPC", error);
return null;
}
}
function generateJWT(payload) {
try {
return jwt.sign(payload, config.WEB_UI_SECRET, {expiresIn: '1h'});
} catch (e) {
console.error("Error creating JWT", e);
return null;
}
}
function generateJobId() {
return crypto.randomBytes(8).toString('hex');
}
function calculateHashrate(lastShare, now){
const timeDiff = (now - lastShare ) / 1000; //time difference in seconds
if (timeDiff > 0){
return (1/timeDiff) * (2**25);
}
return 0;
}
function generateSubscriptionId() {
return crypto.randomBytes(8).toString('hex');
}
module.exports = {
generateBlockHeader,
difficultyToTarget,
generateSubscriptionId,
bitsToTarget,
calculateBlockReward,
generateJWT,
generateJobId,
calculateHashrate,
getBlockHash
};