-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminer-worker.js
More file actions
326 lines (293 loc) · 7.92 KB
/
miner-worker.js
File metadata and controls
326 lines (293 loc) · 7.92 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"use strict";
function rawToHex(raw) {
var hex = "";
var hexChars = "0123456789abcdef";
for (var i = 0; i < raw.length; i++) {
var c = raw.charCodeAt(i);
hex += hexChars.charAt((c >>> 4) & 0x0f) + hexChars.charAt(c & 0x0f);
}
return hex;
}
function sha1Raw(raw) {
return binaryToRaw(sha1Binary(rawToBinary(raw), raw.length * 8));
}
function binaryToRaw(bin) {
var raw = "";
for (var i = 0, il = bin.length * 32; i < il; i += 8) {
raw += String.fromCharCode((bin[i >> 5] >>> (24 - (i % 32))) & 0xff);
}
return raw;
}
function sha1Binary(bin, len) {
bin[len >> 5] |= 0x80 << (24 - (len % 32));
bin[(((len + 64) >> 9) << 4) + 15] = len;
var w = new Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for (var i = 0, il = bin.length; i < il; i += 16) {
var _a = a;
var _b = b;
var _c = c;
var _d = d;
var _e = e;
for (var j = 0; j < 80; j++) {
if (j < 16) {
w[j] = bin[i + j];
} else {
w[j] = _rotateLeft(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
}
var t = _add(
_add(_rotateLeft(a, 5), _ft(j, b, c, d)),
_add(_add(e, w[j]), _kt(j))
);
e = d;
d = c;
c = _rotateLeft(b, 30);
b = a;
a = t;
}
a = _add(a, _a);
b = _add(b, _b);
c = _add(c, _c);
d = _add(d, _d);
e = _add(e, _e);
}
return [a, b, c, d, e];
}
function _add(x, y) {
var lsw = (x & 0xffff) + (y & 0xffff);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xffff);
}
function _rotateLeft(n, count) {
return (n << count) | (n >>> (32 - count));
}
function _ft(t, b, c, d) {
if (t < 20) {
return (b & c) | (~b & d);
} else if (t < 40) {
return b ^ c ^ d;
} else if (t < 60) {
return (b & c) | (b & d) | (c & d);
} else {
return b ^ c ^ d;
}
}
function _kt(t) {
if (t < 20) {
return 1518500249;
} else if (t < 40) {
return 1859775393;
} else if (t < 60) {
return -1894007588;
} else {
return -899497514;
}
}
function rawToBinary(raw) {
var binary = new Array(raw.length >> 2);
for (var i = 0, il = binary.length; i < il; i++) {
binary[i] = 0;
}
for (i = 0, il = raw.length * 8; i < il; i += 8) {
binary[i >> 5] |= (raw.charCodeAt(i / 8) & 0xff) << (24 - (i % 32));
}
return binary;
}
function stringToRaw(string) {
var raw = "",
x,
y;
var i = -1;
var il = string.length;
while (++i < il) {
x = string.charCodeAt(i);
y = i + 1 < il ? string.charCodeAt(i + 1) : 0;
if (0xd800 <= x && x <= 0xdbff && 0xdc00 <= y && y <= 0xdfff) {
x = 0x10000 + ((x & 0x03ff) << 10) + (y & 0x03ff);
++i;
}
if (x <= 0x7f) {
raw += String.fromCharCode(x);
} else if (x <= 0x7ff) {
raw += String.fromCharCode(0xc0 | ((x >>> 6) & 0x1f), 0x80 | (x & 0x3f));
} else if (x <= 0xffff) {
raw += String.fromCharCode(
0xe0 | ((x >>> 12) & 0x0f),
0x80 | ((x >>> 6) & 0x3f),
0x80 | (x & 0x3f)
);
} else if (x <= 0x1fffff) {
raw += String.fromCharCode(
0xf0 | ((x >>> 18) & 0x07),
0x80 | ((x >>> 12) & 0x3f),
0x80 | ((x >>> 6) & 0x3f),
0x80 | (x & 0x3f)
);
}
}
return raw;
}
function hmacRaw(key, data) {
var binaryKey = rawToBinary(key);
if (binaryKey.length > 16) {
binaryKey = sha1Binary(binaryKey, key.length * 8);
}
var ipad = new Array(16);
var opad = new Array(16);
for (var i = 0; i < 16; i++) {
ipad[i] = binaryKey[i] ^ 0x36363636;
opad[i] = binaryKey[i] ^ 0x5c5c5c5c;
}
var hash = sha1Binary(ipad.concat(rawToBinary(data)), 512 + data.length * 8);
return binaryToRaw(sha1Binary(opad.concat(hash), 512 + 160));
}
function sha1(toHash) {
return rawToHex(sha1Raw(stringToRaw(toHash)));
}
function roundTo(precision, value) {
var power_of_ten = 10 * (precision * precision);
return Math.round(value * power_of_ten) / power_of_ten;
}
let XMLHttpFactories = [
function () {
return new XMLHttpRequest();
},
function () {
return new ActiveXObject("Msxml3.XMLHTTP");
},
function () {
return new ActiveXObject("Msxml2.XMLHTTP.6.0");
},
function () {
return new ActiveXObject("Msxml2.XMLHTTP.3.0");
},
function () {
return new ActiveXObject("Msxml2.XMLHTTP");
},
function () {
return new ActiveXObject("Microsoft.XMLHTTP");
},
];
let requestApi = false;
for (let i = 0; i < XMLHttpFactories.length; i++) {
try {
requestApi = XMLHttpFactories[i]();
} catch (e) {
continue;
}
break;
}
function httpGet(theUrl) {
requestApi.open("GET", theUrl, false);
requestApi.send(null);
return requestApi.responseText;
}
function httpPost(theUrl) {
requestApi.open("POST", theUrl, false);
requestApi.send(null);
return requestApi.responseText;
}
let shouldRun = false;
let username;
let key;
let useragent;
const base_url = "http://51.15.127.80";
onmessage = function (event) {
username = event.data.username;
key = event.data.key;
useragent = event.data.userAgent;
if (event.data.action === "mine") {
console.log("mine");
shouldRun = true;
mineLoop();
} else if (event.data.action === "stop") {
console.log("stop");
shouldRun = false;
}
};
function mineLoop() {
if (shouldRun) {
console.log("running");
try {
const job = httpGet(
base_url +
"/legacy_job?u=" +
username +
"&i=" +
useragent +
"&nocache=" +
new Date().getTime()
);
console.log("Start Mining");
const last_block_hash = job.split(",")[0];
const expected_hash = job.split(",")[1];
const difficulty = job.split(",")[2];
postMessage({
type: "progress",
data: {
status: "Trying ...",
lastHash: last_block_hash,
expected: expected_hash,
diff: difficulty,
},
});
const timeStart = new Date().getTime();
let result = 0;
let current_hash;
for (; result < difficulty * 100; result++) {
current_hash = sha1(last_block_hash + result);
if (current_hash == expected_hash) {
console.log("Found Hash");
const timeEnd = new Date().getTime();
const timeDiff = timeEnd - timeStart;
const hashRate = roundTo(2, (result / timeDiff) * 1000);
const feedback = `Hash found: ${current_hash}\nResult: ${result}\nTime taken: ${timeDiff} ms\nHash rate: ${hashRate} hashes/s`;
console.log(feedback);
postMessage({
type: "progress",
data: {
status: "FOUND !",
lastHash: last_block_hash,
expected: expected_hash,
diff: difficulty,
},
});
const response = httpPost(
base_url +
"/legacy_job?u=" +
username +
"&r=" +
result +
"&k=" +
key +
"&s=Duino Miner JS" +
"&j=" +
expected_hash +
"&i=" +
useragent +
"&h=" +
hashRate +
"&b=" +
timeDiff +
"&nocache=" +
new Date().getTime()
);
postMessage({
type: "result",
data: { resp: feedback, validated: response },
});
break;
} else {
}
}
} catch (error) {
// Handle any errors and send the error message to the main thread
postMessage({ type: "error", data: { message: error.message } });
}
postMessage({ type: "done" });
}
}