From: Chris Duncan Date: Wed, 11 Dec 2024 22:07:10 +0000 (-0800) Subject: Implement once-called function inline, and move validation to top of calculate function. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=4afc7994d94f41634d0435930f0e9ef9da46e6b3;p=libnemo.git Implement once-called function inline, and move validation to top of calculate function. --- diff --git a/src/lib/workers/pow.ts b/src/lib/workers/pow.ts index 78ba3b2..4cdf7b0 100644 --- a/src/lib/workers/pow.ts +++ b/src/lib/workers/pow.ts @@ -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) => 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