// SPDX-License-Identifier: GPL-3.0-or-later
export class Pow {
- static SEND_THRESHOLD = '0xfffffff8'
- /** Used to set canvas size. Must be a multiple of 256. */
- static WORKLOAD: number = 256 * 4
-
- static async find (hash: string, threshold: string = this.SEND_THRESHOLD): Promise<string> {
+ static async find (hash: string, threshold: number = this.SEND_THRESHOLD): Promise<string> {
return new Promise<string>(resolve => {
this.calculate(hash, threshold, resolve)
})
* Nano Currency Proof of Work Value generation using WebGL2
* Author: numtel <ben@latenightsketches.com>
* License: MIT
-
- * self.NanoWebglPow(hashHex, callback, progressCallback, threshold);
- * @param hashHex String Previous Block Hash as Hex String
- * @param callback Function Called when work value found
- * Receives single string argument, work value as hex
- * @param progressCallback Function Optional
- * Receives single argument: n, number of frames so far
- * Return true to abort
- * @param threshold Number|String Optional difficulty threshold (default=0xFFFFFFF8 since v21)
*/
+ static SEND_THRESHOLD = 0xFFFFFFF8
+ /** Used to set canvas size. Must be a multiple of 256. */
+ static WORKLOAD: number = 256 * 4
static gl = new OffscreenCanvas(this.WORKLOAD, this.WORKLOAD).getContext('webgl2')
static work0 = new Uint8Array(4)
static work1 = new Uint8Array(4)
}
}
- /*
- for (i = 0; i<12; i++) {
- B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1]);
- ->
- B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1]);
- */
-
static hexify (arr: number[] | Uint8Array): string {
let out = ''
for (let i = arr.length - 1; i >= 0; i--) {
}
return out
}
+ /*
+ for (i = 0; i<12; i++) {
+ B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1]);
+ ->
+ B2B_G(0, 8, 16, 24, SIGMA82[i * 16 + 0], SIGMA82[i * 16 + 1]);
+ */
- static calculate (hashHex: string, threshold: number | string = '0xFFFFFFF8', callback: (nonce: string | PromiseLike<string>) => any): void {
- if (typeof threshold === 'number') threshold = '0x' + threshold.toString(16)
+ /**
+ * self.NanoWebglPow (hashHex, callback, progressCallback, threshold)
+ * @param {string} hashHex - Previous Block Hash as Hex String
+ * @param {string} threshold - Optional difficulty threshold(default=0xFFFFFFF8 since v21)
+ * @param {function} callback - Called when work value found
+ * Receives single string argument, work value as hex
+ */
+ static calculate (hashHex: string, threshold: number = this.SEND_THRESHOLD, callback: (nonce: string | PromiseLike<string>) => any): void {
+ if (typeof threshold !== 'number') throw new TypeError(`invalid_threshold ${threshold}`)
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)
// Threshold test, first 4 bytes not significant,
// only calculate digest of the second 4 bytes
- if((BLAKE2B_IV32_1 ^ v[1] ^ v[17]) > ` + threshold + `u) {
+ if((BLAKE2B_IV32_1 ^ v[1] ^ v[17]) > ${threshold}u) {
// Success found, return pixel data so work value can be constructed
fragColor = vec4(
float(x_index + 1u)/255., // +1 to distinguish from 0 (unsuccessful) pixels
this.gl.uniform4uiv(work0Location, this.work0)
this.gl.uniform4uiv(work1Location, this.work1)
- this.gl.uniform1uiv(blockHashLocation, new Uint32Array(blockHashComponents))
+ this.gl.uniform1uiv(blockHashLocation, blockHashComponents)
this.gl.clear(this.gl.COLOR_BUFFER_BIT)
this.gl.drawArrays(this.gl.TRIANGLES, 0, 6)