* byte, converts the subsequent 3 pixels with the nonce byte values to a hex
* string, and returns the result.
*
- * @param work - Buffer with the original random nonce value
+ * @param workBytes - Buffer with the original random nonce value
+ * @param workHex - Original nonce if provided for a validation call
* @returns Nonce as an 8-byte (16-char) hexadecimal string
*/
- static #readResult (work: Uint8Array): string {
+ static #readResult (workBytes: Uint8Array, workHex?: string): string {
if (this.#gl == null) throw new Error('WebGL 2 is required to read pixels')
this.#gl.readPixels(0, 0, this.#gl.drawingBufferWidth, this.#gl.drawingBufferHeight, this.#gl.RGBA, this.#gl.UNSIGNED_BYTE, this.#pixels)
for (let i = 0; i < this.#pixels.length; i += 4) {
if (this.#pixels[i] !== 0) {
/** Return the work value with the custom bits */
- const hex = this.#hexify(work.subarray(4, 8)) + this.#hexify([
+ const hex = this.#hexify(workBytes.subarray(4, 8)) + this.#hexify([
this.#pixels[i + 2],
this.#pixels[i + 3],
- work[2] ^ (this.#pixels[i] - 1),
- work[3] ^ (this.#pixels[i + 1] - 1)
+ workBytes[2] ^ (this.#pixels[i] - 1),
+ workBytes[3] ^ (this.#pixels[i + 1] - 1)
])
- return hex
+ if (workHex == null || workHex == hex) return hex
}
}
throw new Error('Query reported result but nonce value not found')
data.setBigUint64(0, BigInt(`0x${work}`), true)
const seed = new Uint8Array(data.buffer)
this.#draw(seed)
- const found = await this.#checkQueryResult()
+ let found = await this.#checkQueryResult()
if (found) {
- nonce = this.#readResult(seed)
+ try {
+ nonce = this.#readResult(seed, work)
+ } catch (err) {
+ found = false
+ }
}
if (found && nonce !== work) throw new Error(`Nonce found but does not match work`)
return found