##// END OF EJS Templates
Added tag 3.5.0 for changeset 757b4ada4ca1
Added tag 3.5.0 for changeset 757b4ada4ca1

File last commit:

r1468:14229bdf default
r1731:cef12850 default
Show More
proof_of_work.js
66 lines | 1.7 KiB | application/javascript | JavascriptLexer
neko259
Added PoW instead of 30-second captcha
r1428 var POW_COMPUTING_TIMEOUT = 2;
var POW_HASH_LENGTH = 16;
neko259
Don't include pow and hasher scripts to both document and worker. Import...
r1468 var hasher = null;
neko259
Added PoW instead of 30-second captcha
r1428
neko259
Make pow script be a shared worker
r1462
neko259
Don't include pow and hasher scripts to both document and worker. Import...
r1468 function computeHash(iteration, guess, target, payload, timestamp, port) {
neko259
Added PoW instead of 30-second captcha
r1428 iteration += 1;
var hash = hasher(payload + iteration).toString();
guess = hash.substring(0, POW_HASH_LENGTH);
if (guess <= target) {
neko259
Fixed bug in PoW in Chrome. Recompiled translations after merging 2 different features (BB-83)
r1431 //console.log("Iteration: ", iteration);
//console.log("Guess: ", guess);
//console.log("Target: ", target);
neko259
Added PoW instead of 30-second captcha
r1428
var data = {
iteration: iteration,
timestamp: timestamp,
guess: guess
};
neko259
Make pow script be a shared worker
r1462 port.postMessage(data);
neko259
Added PoW instead of 30-second captcha
r1428 } else {
//console.log("Iteration: ", iteration);
//console.log("Guess: ", guess);
//console.log("Target: ", target);
setTimeout(function() {
neko259
Don't include pow and hasher scripts to both document and worker. Import...
r1468 computeHash(iteration, guess, target, payload, timestamp, port);
neko259
Added PoW instead of 30-second captcha
r1428 }, POW_COMPUTING_TIMEOUT);
}
}
neko259
Don't include pow and hasher scripts to both document and worker. Import...
r1468 function doWork(message, difficulty, port) {
neko259
Added PoW instead of 30-second captcha
r1428 var timestamp = Date.now();
var iteration = 0;
var payload = timestamp + message;
var target = parseInt(Math.pow(2, POW_HASH_LENGTH * 3) / difficulty).toString();
while (target.length < POW_HASH_LENGTH) {
target = '0' + target;
}
var guess = target + '0';
setTimeout(function() {
neko259
Don't include pow and hasher scripts to both document and worker. Import...
r1468 computeHash(iteration, guess, target, payload, timestamp, port);
neko259
Added PoW instead of 30-second captcha
r1428 }, POW_COMPUTING_TIMEOUT);
}
neko259
Make pow script be a shared worker
r1462 onconnect = function(e) {
var port = e.ports[0];
port.start();
port.onmessage = function(e) {
var difficulty = e.data.difficulty;
neko259
Don't include pow and hasher scripts to both document and worker. Import...
r1468 if (hasher == null) {
importScripts(e.data.hasher);
hasher = CryptoJS.SHA256;
}
self.doWork(e.data.msg, difficulty, port);
neko259
Make pow script be a shared worker
r1462 };
}