-
Notifications
You must be signed in to change notification settings - Fork 11
/
proofOfWorkerStub.js
146 lines (126 loc) · 3.57 KB
/
proofOfWorkerStub.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
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
// IN ORDER FOR CHANGES TO THIS FILE TO "TAKE" AND BE USED IN THE APP, THE BUILD IN wasm_build HAS TO BE RE-RUN
// scrypt and scryptPromise will be filled out by js code that gets appended below this script by the wasm_build process
// --- snip ---
let scrypt;
let scryptPromise;
let working = false;
const batchSize = 8;
onmessage = function(e) {
if(e.data.stop) {
working = false;
return;
}
const challengeBase64 = e.data.challenge;
const workerId = e.data.workerId;
if(!challengeBase64) {
postMessage({
type: "error",
challenge: challengeBase64,
message: `challenge was not provided`
});
}
working = true;
let challengeJSON = null;
let challenge = null;
try {
challengeJSON = atob(challengeBase64);
} catch (err) {
postMessage({
type: "error",
challenge: challengeBase64,
message: `couldn't decode challenge '${challengeBase64}' as base64: ${err}`
});
}
try {
challenge = JSON.parse(challengeJSON);
} catch (err) {
postMessage({
type: "error",
challenge: challengeBase64,
message: `couldn't parse challenge '${challengeJSON}' as json: ${err}`
});
}
challenge = {
cpuAndMemoryCost: challenge.N,
blockSize: challenge.r,
paralellization: challenge.p,
keyLength: challenge.klen,
preimage: challenge.i,
difficulty: challenge.d,
difficultyLevel: challenge.dl
}
const probabilityOfFailurePerAttempt = 1-(1/Math.pow(2, challenge.difficultyLevel));
let i = workerId * Math.pow(2, challenge.difficultyLevel) * 1000;
const hexPreimage = base64ToHex(challenge.preimage);
let smallestHash = challenge.difficulty.split("").map(x => "f").join("");
postMessage({
type: "progress",
challenge: challengeBase64,
attempts: 0,
smallestHash: smallestHash,
difficulty: challenge.difficulty,
probabilityOfFailurePerAttempt: probabilityOfFailurePerAttempt
});
const doWork = () => {
var j = 0;
while(j < batchSize) {
j++;
i++;
let nonceHex = i.toString(16);
if((nonceHex.length % 2) == 1) {
nonceHex = `0${nonceHex}`;
}
const hashHex = scrypt(
nonceHex,
hexPreimage,
challenge.cpuAndMemoryCost,
challenge.blockSize,
challenge.paralellization,
challenge.keyLength
);
//console.log(i.toString(16), hashHex);
const endOfHash = hashHex.substring(hashHex.length-challenge.difficulty.length);
if(endOfHash < smallestHash) {
smallestHash = endOfHash
}
if(endOfHash <= challenge.difficulty) {
postMessage({
type: "success",
challenge: challengeBase64,
nonce: nonceHex,
smallestHash: endOfHash,
difficulty: challenge.difficulty
});
break
}
}
postMessage({
type: "progress",
challenge: challengeBase64,
attempts: batchSize,
smallestHash: smallestHash,
difficulty: challenge.difficulty,
probabilityOfFailurePerAttempt: probabilityOfFailurePerAttempt
});
if(working) {
this.setTimeout(doWork, 1);
}
};
if(scrypt) {
doWork();
} else {
scryptPromise.then(() => {
doWork();
});
}
}
// https://stackoverflow.com/questions/39460182/decode-base64-to-hexadecimal-string-with-javascript
function base64ToHex(str) {
const raw = atob(str);
let result = '';
for (let i = 0; i < raw.length; i++) {
const hex = raw.charCodeAt(i).toString(16);
result += (hex.length === 2 ? hex : '0' + hex);
}
return result;
}