+(()=>{function b(u,e=5,t=null,o=null,d=navigator.hardwareConcurrency||1){return console.debug("fast algo"),new Promise((m,n)=>{let r=URL.createObjectURL(new Blob(["(",k(),")()"],{type:"application/javascript"})),c=[],l=()=>{c.forEach(a=>a.terminate()),t!=null&&(t.removeEventListener("abort",l),t.aborted&&(console.log("PoW aborted"),n(!1)))};t?.addEventListener("abort",l,{once:!0});for(let a=0;a<d;a++){let i=new Worker(r);i.onmessage=s=>{typeof s.data=="number"?o?.(s.data):(l(),m(s.data))},i.onerror=s=>{l(),n(s)},i.postMessage({data:u,difficulty:e,nonce:a,threads:d}),c.push(i)}URL.revokeObjectURL(r)})}function k(){return function(){let u=t=>{let o=new TextEncoder().encode(t);return crypto.subtle.digest("SHA-256",o.buffer)};function e(t){return Array.from(t).map(o=>o.toString(16).padStart(2,"0")).join("")}addEventListener("message",async t=>{let o=t.data.data,d=t.data.difficulty,m,n=t.data.nonce,r=t.data.threads,c=n;for(;;){let l=await u(o+n),a=new Uint8Array(l),i=!0;for(let p=0;p<d;p++){let y=Math.floor(p/2),f=p%2;if((a[y]>>(f===0?4:0)&15)!==0){i=!1;break}}if(i){m=e(a),console.log(m);break}let s=n;n+=r,n>s|1023&&(n>>10)%r===c&&postMessage(n)}postMessage({hash:m,data:o,difficulty:d,nonce:n})})}.toString()}var h=class{static state={baseURL:"",version:"unknown"};static elements={title:null,mascot:null,status:null,metrics:null,message:null,progressContainer:null,progressBar:null};static initialize(e){this.state={...this.state,...e},this.elements={title:document.getElementById("title"),mascot:document.getElementById("mascot"),status:document.getElementById("status"),metrics:document.getElementById("metrics"),message:document.getElementById("message"),progressContainer:document.getElementById("progress-container"),progressBar:document.getElementById("progress-bar")}}static setState(e,t={}){switch(e){case"checking":this.setChecking(t);break;case"success":this.setSuccess(t);break}}static setChecking(e){this.elements.title.textContent=e.title||"Making sure you're not a bot!",this.elements.mascot.src=`${this.state.baseURL}/static/img/mascot-puzzle.png?v=${this.state.version}`,this.elements.status.textContent=e.status||"Calculating...",this.elements.progressContainer.classList.remove("hidden"),this.setCheckingProgress(e.progress,e.metrics,e.message)}static setCheckingProgress(e,t,o){e!==void 0&&(this.elements.progressBar.style.width=`${e}%`),t!==void 0&&(t===""?this.elements.metrics.classList.add("hidden"):(this.elements.metrics.classList.remove("hidden"),this.elements.metrics.textContent=t)),o!==void 0&&(this.elements.message.textContent=o)}static setSuccess(e){this.elements.title.textContent="Success!",this.elements.mascot.src=`${this.state.baseURL}/static/img/mascot-pass.png?v=${this.state.version}`,this.elements.status.textContent=e.status||"Done!",this.elements.metrics.textContent=e.metrics||"Took ?, ? iterations",this.elements.message.textContent=e.message||"",this.elements.progressContainer.classList.add("hidden")}};function x(u,e,t,o,d,m){let n=document.createElement("form");n.method="POST",n.action=`${t}/answer`;let r=document.createElement("input");r.type="hidden",r.name="response",r.value=u;let c=document.createElement("input");c.type="hidden",c.name="solution",c.value=e;let l=document.createElement("input");l.type="hidden",l.name="nonce",l.value=o;let a=document.createElement("input");a.type="hidden",a.name="ts",a.value=d;let i=document.createElement("input");i.type="hidden",i.name="signature",i.value=m;let s=document.createElement("input");return s.type="hidden",s.name="redir",s.value=window.location.href,n.appendChild(r),n.appendChild(c),n.appendChild(l),n.appendChild(a),n.appendChild(i),n.appendChild(s),document.body.appendChild(n),n}(async()=>{let u=JSON.parse(document.getElementById("challenge").textContent),e=JSON.parse(document.getElementById("difficulty").textContent),t=JSON.parse(document.getElementById("baseURL").textContent),o=JSON.parse(document.getElementById("version").textContent),d=JSON.parse(document.getElementById("nonce").textContent),m=JSON.parse(document.getElementById("ts").textContent),n=JSON.parse(document.getElementById("signature").textContent);h.initialize({baseURL:t,version:o}),h.setState("checking",{metrics:`Difficulty: ${e}, Speed: calculating...`,message:""});let r=Date.now(),c=0,l=Math.pow(16,-e),a=`${u}|${d}|${m}|${n}`,{hash:i,nonce:s}=await b(a,e,null,f=>{let C=Math.pow(1-l,f),w=(1-Math.pow(C,2))*100,g=Date.now()-r;if(console.log("delta",g,"lastUpdate",c,"delta - lastUpdate",g-c),g-c>100){let E=f/g;h.setCheckingProgress(w,`Difficulty: ${e}, Speed: ${E.toFixed(3)}kH/s`,C<.01?"This is taking longer than expected. Please do not refresh the page.":void 0),c=g}}),p=Date.now();console.log({hash:i,solution:s}),h.setState("success",{status:"Verification Complete!",metrics:`Took ${p-r}ms, ${s} iterations`});let y=x(i,s,t,d,m,n);setTimeout(()=>{y.submit()},250)})();})();
0 commit comments