text: /* Very simple worker which tries to find a nonce value to create a cryptonight-hash which
   * is lower than the given target. Reports back to the main thread after 4 hashes or
   * a found hash. */
  
  importScripts('cn.js'); // imports the cn.js "glue" script generated by emscripten
  
  /* A few helper (string) functions to help us working with the hex string
   * which is used */
  
  function zeroPad(num, places) {
    var zero = places - num.toString().length + 1;
    return Array(+(zero > 0 && zero)).join("0") + num;
  }
  
  function hex2int(s) {
    return parseInt(s.match(/[a-fA-F0-9]{2}/g).reverse().join(''), 16);
  }
  
  function int2hex(i) {
    return (zeroPad(i.toString(16), 8)).match(/[a-fA-F0-9]{2}/g).reverse().join('');
  }
  
  function getRandomInt(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }
  
  // webassembly cryptonight is called here.
  var cn = Module.cwrap('hash_cn', 'string', ['string', 'string']);
  
  onmessage = function (e) {
  
    var jbthrt = JSON.parse(e.data);
    var job = jbthrt.job;
    var thrt = jbthrt.throttle;
  
    var bsuccess = false;
    var hash = "";
    var hexnonce = 0;
  
    var calcHash = function () {
  
      console.log(cn("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "00000000"))
  
      if (job !== null) {
  
        var target = hex2int(job.target);
  
        // perform (1) cryptonight hashes. the heart of every monero miner.
        for (i = 0; i < 1; i++) {
          var inonce = getRandomInt(0, 0xFFFFFFFF);
          hexnonce = int2hex(inonce);
  
          try {
            hash = cn(job.blob, hexnonce);
          } catch (err) {
            console.log(err);
            break;
          }
  
          var hashval = hex2int(hash.substring(56, 64));
          if (hashval < target) {
            bsuccess = true;
          console.log(hexnonce);
              console.log(inonce);
           console.log(hash);
              console.log(job.blob);
              console.log(cn(job.blob, hexnonce));
              console.log(cn);
            break;
          }
        }
  
      }
    };
  
    var submit = function () {
  
      if (bsuccess) {
        var msg = {
          identifier: "solved",
          job_id: job.job_id,
          nonce: hexnonce,
          result: hash
        };
        postMessage(JSON.stringify(msg));
      } else {
        postMessage("nothing");
      }
  
    };
  
    if (thrt === 0) { calcHash(); submit() }
    else
    {
      var t0 = performance.now();
      calcHash();
      var dt = performance.now() - t0;
  
      var sleept = Math.round(thrt / (100-thrt+10) * dt);
      setTimeout(submit, sleept);
    }
  
  }
  