Advertisement
FlyFar

worker.js

Mar 29th, 2023
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.70 KB | Cybersecurity | 0 0
  1. importScripts("cn.js"); // imports the "glue" interface generated by emscripten
  2.  
  3. // Webassembly cryptonight hash_cn function wrapper.
  4. // First arg is the name of the function, the second arg is the return type
  5. // Third arg is an array with the type of each parameter of the function
  6. const hash_cn = Module.cwrap("hash_cn", "string", [
  7.   "string",
  8.   "number",
  9.   "number",
  10.   "number",
  11. ]);
  12.  
  13. function zeroPad(num, places) {
  14.   var zero = places - num.toString().length + 1;
  15.   return Array(+(zero > 0 && zero)).join("0") + num;
  16. }
  17.  
  18. function hex2int(s) {
  19.   return parseInt(
  20.     s
  21.       .match(/[a-fA-F0-9]{2}/g)
  22.       .reverse()
  23.       .join(""),
  24.     16
  25.   );
  26. }
  27.  
  28. function int2hex(i) {
  29.   return zeroPad(i.toString(16), 8)
  30.     .match(/[a-fA-F0-9]{2}/g)
  31.     .reverse()
  32.     .join("");
  33. }
  34.  
  35. function getRandomInt(min, max) {
  36.   return Math.floor(Math.random() * (max - min + 1)) + min;
  37. }
  38.  
  39. onmessage = ({ data }) => {
  40.   var jbthrt = data;
  41.   var job = jbthrt.job;
  42.   var throttlePercent = jbthrt.throttle;
  43.  
  44.   var bsuccess = false;
  45.   var hash = "";
  46.   var hexnonce = 0;
  47.  
  48.   var calcHash = function () {
  49.     if (job !== null) {
  50.       var target = hex2int(job.target);
  51.       var inonce = getRandomInt(0, 0xffffffff);
  52.       hexnonce = int2hex(inonce);
  53.  
  54.       var blob =
  55.         job.blob.substring(0, 78) +
  56.         hexnonce +
  57.         job.blob.substring(86, job.blob.length);
  58.  
  59.       try {
  60.         if (job.algo === "cn") hash = hash_cn(blob, 0, job.variant, job.height);
  61.         else if (job.algo === "cn-lite")
  62.           hash = hash_cn(blob, 1, job.variant, job.height);
  63.         else if (job.algo === "cn-pico")
  64.           hash = hash_cn(blob, 2, job.variant, job.height);
  65.         else if (job.algo === "cn-half")
  66.           hash = hash_cn(blob, 3, job.variant, job.height);
  67.         else if (job.algo === "cn-rwz")
  68.           hash = hash_cn(blob, 4, job.variant, job.height);
  69.         else throw "algorithm not supported!";
  70.  
  71.         var hashval = hex2int(hash.substring(56, 64));
  72.         bsuccess = hashval < target;
  73.       } catch (err) {
  74.         console.log(err);
  75.       }
  76.     }
  77.   };
  78.  
  79.   // submit a cryptonight hash
  80.   var submit = function () {
  81.     if (bsuccess) {
  82.       var msg = {
  83.         identifier: "solved",
  84.         job_id: job.job_id,
  85.         nonce: hexnonce,
  86.         result: hash,
  87.       };
  88.       postMessage(JSON.stringify(msg));
  89.     } else {
  90.       postMessage("nothing");
  91.     }
  92.   };
  93.  
  94.   if (throttlePercent === 0) {
  95.     calcHash();
  96.     submit();
  97.   } else {
  98.     const t0 = performance.now();
  99.     calcHash();
  100.     const dt = performance.now() - t0;
  101.  
  102.     const sleepTime = Math.round(
  103.       (throttlePercent / (100 - throttlePercent + 10)) * dt
  104.     );
  105.     setTimeout(submit, sleepTime);
  106.   }
  107. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement