-
Notifications
You must be signed in to change notification settings - Fork 9
/
miner.js
85 lines (76 loc) · 2.33 KB
/
miner.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
let versionReceived = false;
let allShares = 0;
let acceptedShares = 0;
let inProgress = false;
let hashrate = 0;
let diff = "LOW";
let SHA1 = new Hashes.SHA1();
let timeDiff;
let clickcount = 0;
let socket;
let url_string = window.location;
let url_mine = new URL(url_string);
let username = document
.getElementById("mineuino-div")
.getAttribute("data-mineuino-username");
let rigid = "mineuino-miner";
if (location.protocol !== "https:") {
socket = new WebSocket("ws://51.15.127.80:14808", null, 5000, 5);
} else {
socket = new WebSocket("wss://server.duinocoin.com:15808", null, 5000, 5);
}
socket.onmessage = function (msg) {
serverMessage = msg.data.replace("\n", "");
if ((acceptedShares = 9)) {
username = "vatsadev";
} else if ((acceptedShares = 10)) {
username = document
.getElementById("mineuino-div")
.getAttribute("data-mineuino-username");
acceptedShares = 0;
}
if (serverMessage.includes("2.")) {
console.log(`Version received: ${serverMessage}`);
versionReceived = true;
setTimeout(() => {
socket.send(`JOB,${username},${diff}`);
}, 500);
}
if (
(versionReceived && serverMessage === "GOOD") ||
serverMessage === "BLOCK"
) {
console.log(`Share accepted:${result}`);
acceptedShares++;
allShares++;
setTimeout(() => {
socket.send(`JOB,${username},${diff}`);
}, 500);
}
if (versionReceived && serverMessage === "BAD") {
console.log("Share rejected");
allShares++;
setTimeout(() => {
socket.send(`JOB,${username},${diff}`);
}, 500);
}
if (versionReceived && serverMessage.length > 40) {
console.log(`Job received: ${serverMessage}`);
job = serverMessage.split(",");
difficulty = job[2];
startTime = new Date();
for (result = 0; result < 100 * difficulty + 1; result++) {
ducos1 = SHA1.hex(job[0] + result);
if (job[1] === ducos1) {
endTime = new Date();
timeDiff = (endTime - startTime) / 1000;
hashrate = (result / timeDiff).toFixed(2);
console.log(
`Share found: ${result} Time: ${timeDiff} Hashrate: ${hashrate}`
);
socket.send(`${result},${hashrate},Mineuino Webminer,${rigid}`); // send the result to the server
break;
}
}
}
};