]> zoso.dev Git - libnemo.git/commitdiff
Implement once-called function inline, and move validation to top of calculate function.
authorChris Duncan <chris@zoso.dev>
Wed, 11 Dec 2024 22:07:10 +0000 (14:07 -0800)
committerChris Duncan <chris@zoso.dev>
Wed, 11 Dec 2024 22:07:10 +0000 (14:07 -0800)
src/lib/workers/pow.ts

index 78ba3b2a94267322a4807db52de7a310aebbcf0c..4cdf7b07c388672a098e24112fcfc78a27782237 100644 (file)
@@ -40,16 +40,13 @@ export class Pow {
                return out
        }
 
-       static hex_reverse (hex: string): string {
-               let out = ''
-               for (let i = hex.length; i > 0; i -= 2) {
-                       out += hex.slice(i - 2, i)
-               }
-               return out
-       }
-
        static calculate (hashHex: string, threshold: number | string = '0xFFFFFFF8', callback: (nonce: string | PromiseLike<string>) => any): void {
                if (typeof threshold === 'number') threshold = '0x' + threshold.toString(16)
+               if (!/^[A-F-a-f0-9]{64}$/.test(hashHex)) throw new Error(`invalid_hash ${hashHex}`)
+               let reverseHex = ''
+               for (let i = hashHex.length; i > 0; i -= 2) {
+                       reverseHex += hashHex.slice(i - 2, i)
+               }
 
                const canvas = new OffscreenCanvas(this.webglWidth, this.webglHeight)
                const gl = canvas.getContext('webgl2')
@@ -57,12 +54,8 @@ export class Pow {
                if (!gl)
                        throw new Error('webgl2_required')
 
-               if (!/^[A-F-a-f0-9]{64}$/.test(hashHex))
-                       throw new Error(`invalid_hash ${hashHex}`)
-
                gl.clearColor(0, 0, 0, 1)
 
-               const reverseHex = this.hex_reverse(hashHex)
 
                // Vertext Shader
                const vsSource = `#version 300 es