nano-pow 5.1.5 → 5.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bin/cli.js CHANGED
@@ -132,10 +132,70 @@ Object.defineProperty(webgpu, "isSupported", {
132
132
  // src/utils/bigint.ts
133
133
  //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
134
134
  //! SPDX-License-Identifier: GPL-3.0-or-later
135
+ function bigintFrom(value, type) {
136
+ if (typeof value === "bigint") {
137
+ return value;
138
+ } else if (typeof value === "boolean" || typeof value === "number") {
139
+ return BigInt(value);
140
+ } else if (typeof value === "string") {
141
+ const v = value.trim().replace(/n$/, "");
142
+ if (/^0[Bb][01]+$/.test(v) || /^0[Oo][0-7]+$/.test(v) || /^0[Xx][A-Fa-f\d]+$/.test(v) || /^\d+$/.test(v)) {
143
+ return BigInt(v);
144
+ }
145
+ if (type === "bin" && /^[01]+$/.test(v)) {
146
+ return BigInt(`0b${v}`);
147
+ }
148
+ if (type === "oct" && /^[0-7]+$/.test(v)) {
149
+ return BigInt(`0o${v}`);
150
+ }
151
+ if (type === "hex" || /^\d*[A-Fa-f]+\d*$/.test(v)) {
152
+ return BigInt(`0x${v}`);
153
+ }
154
+ }
155
+ throw new TypeError(`can't convert string to BigInt`);
156
+ }
135
157
 
136
158
  // src/utils/cache.ts
137
159
  //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
138
160
  //! SPDX-License-Identifier: GPL-3.0-or-later
161
+ var Cache = class {
162
+ static clear() {
163
+ this.#removeItem("NanoPowCache");
164
+ }
165
+ static search(hash, difficulty) {
166
+ const bigintHash = bigintFrom(hash, "hex");
167
+ const item = this.#getItem("NanoPowCache");
168
+ if (item) {
169
+ const cache = JSON.parse(item);
170
+ for (const c of cache) {
171
+ if (bigintFrom(c.hash, "hex") === bigintHash && bigintFrom(c.difficulty, "hex") >= difficulty) {
172
+ return c;
173
+ }
174
+ }
175
+ }
176
+ return null;
177
+ }
178
+ static store(result) {
179
+ const item = this.#getItem("NanoPowCache");
180
+ const cache = JSON.parse(item ?? "[]");
181
+ if (cache.push(result) > 1e3) cache.shift();
182
+ this.#setItem("NanoPowCache", JSON.stringify(cache));
183
+ return result;
184
+ }
185
+ static #storage = {};
186
+ static #getItem(key) {
187
+ if (globalThis?.localStorage) return globalThis.localStorage.getItem(key);
188
+ return this.#storage[key];
189
+ }
190
+ static #removeItem(key) {
191
+ if (globalThis?.localStorage) return globalThis.localStorage.removeItem(key);
192
+ this.#storage = {};
193
+ }
194
+ static #setItem(key, item) {
195
+ if (globalThis?.localStorage) return globalThis.localStorage.setItem(key, item);
196
+ this.#storage[key] = item;
197
+ }
198
+ };
139
199
 
140
200
  // src/utils/logger.ts
141
201
  //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
@@ -184,6 +244,7 @@ var Logger = class {
184
244
  // src/utils/index.ts
185
245
  //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
186
246
  //! SPDX-License-Identifier: GPL-3.0-or-later
247
+ var clearCache = Cache.clear;
187
248
  function isHexN(value, byteLength) {
188
249
  if (typeof value !== "string") return false;
189
250
  const v = value.replace(/^0[Xx]/, "");
@@ -135,10 +135,70 @@ Object.defineProperty(webgpu, "isSupported", {
135
135
  // src/utils/bigint.ts
136
136
  //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
137
137
  //! SPDX-License-Identifier: GPL-3.0-or-later
138
+ function bigintFrom(value, type) {
139
+ if (typeof value === "bigint") {
140
+ return value;
141
+ } else if (typeof value === "boolean" || typeof value === "number") {
142
+ return BigInt(value);
143
+ } else if (typeof value === "string") {
144
+ const v = value.trim().replace(/n$/, "");
145
+ if (/^0[Bb][01]+$/.test(v) || /^0[Oo][0-7]+$/.test(v) || /^0[Xx][A-Fa-f\d]+$/.test(v) || /^\d+$/.test(v)) {
146
+ return BigInt(v);
147
+ }
148
+ if (type === "bin" && /^[01]+$/.test(v)) {
149
+ return BigInt(`0b${v}`);
150
+ }
151
+ if (type === "oct" && /^[0-7]+$/.test(v)) {
152
+ return BigInt(`0o${v}`);
153
+ }
154
+ if (type === "hex" || /^\d*[A-Fa-f]+\d*$/.test(v)) {
155
+ return BigInt(`0x${v}`);
156
+ }
157
+ }
158
+ throw new TypeError(`can't convert string to BigInt`);
159
+ }
138
160
 
139
161
  // src/utils/cache.ts
140
162
  //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
141
163
  //! SPDX-License-Identifier: GPL-3.0-or-later
164
+ var Cache = class {
165
+ static clear() {
166
+ this.#removeItem("NanoPowCache");
167
+ }
168
+ static search(hash2, difficulty) {
169
+ const bigintHash = bigintFrom(hash2, "hex");
170
+ const item = this.#getItem("NanoPowCache");
171
+ if (item) {
172
+ const cache = JSON.parse(item);
173
+ for (const c of cache) {
174
+ if (bigintFrom(c.hash, "hex") === bigintHash && bigintFrom(c.difficulty, "hex") >= difficulty) {
175
+ return c;
176
+ }
177
+ }
178
+ }
179
+ return null;
180
+ }
181
+ static store(result) {
182
+ const item = this.#getItem("NanoPowCache");
183
+ const cache = JSON.parse(item ?? "[]");
184
+ if (cache.push(result) > 1e3) cache.shift();
185
+ this.#setItem("NanoPowCache", JSON.stringify(cache));
186
+ return result;
187
+ }
188
+ static #storage = {};
189
+ static #getItem(key) {
190
+ if (globalThis?.localStorage) return globalThis.localStorage.getItem(key);
191
+ return this.#storage[key];
192
+ }
193
+ static #removeItem(key) {
194
+ if (globalThis?.localStorage) return globalThis.localStorage.removeItem(key);
195
+ this.#storage = {};
196
+ }
197
+ static #setItem(key, item) {
198
+ if (globalThis?.localStorage) return globalThis.localStorage.setItem(key, item);
199
+ this.#storage[key] = item;
200
+ }
201
+ };
142
202
 
143
203
  // src/utils/logger.ts
144
204
  //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
@@ -187,6 +247,7 @@ var Logger = class {
187
247
  // src/utils/index.ts
188
248
  //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
189
249
  //! SPDX-License-Identifier: GPL-3.0-or-later
250
+ var clearCache = Cache.clear;
190
251
  function isHexN(value, byteLength) {
191
252
  if (typeof value !== "string") return false;
192
253
  const v = value.replace(/^0[Xx]/, "");
package/dist/main.min.js CHANGED
@@ -228,14 +228,8 @@ function bigintToHex(int, length = 0) {
228
228
  //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
229
229
  //! SPDX-License-Identifier: GPL-3.0-or-later
230
230
  var Cache = class {
231
- static #storage = {};
232
- static #getItem(key) {
233
- if (globalThis?.localStorage) return globalThis.localStorage.getItem(key);
234
- return this.#storage[key];
235
- }
236
- static #setItem(key, item) {
237
- if (globalThis?.localStorage) return globalThis.localStorage.setItem(key, item);
238
- this.#storage[key] = item;
231
+ static clear() {
232
+ this.#removeItem("NanoPowCache");
239
233
  }
240
234
  static search(hash, difficulty) {
241
235
  const bigintHash = bigintFrom(hash, "hex");
@@ -257,6 +251,19 @@ var Cache = class {
257
251
  this.#setItem("NanoPowCache", JSON.stringify(cache));
258
252
  return result2;
259
253
  }
254
+ static #storage = {};
255
+ static #getItem(key) {
256
+ if (globalThis?.localStorage) return globalThis.localStorage.getItem(key);
257
+ return this.#storage[key];
258
+ }
259
+ static #removeItem(key) {
260
+ if (globalThis?.localStorage) return globalThis.localStorage.removeItem(key);
261
+ this.#storage = {};
262
+ }
263
+ static #setItem(key, item) {
264
+ if (globalThis?.localStorage) return globalThis.localStorage.setItem(key, item);
265
+ this.#storage[key] = item;
266
+ }
260
267
  };
261
268
 
262
269
  // src/utils/logger.ts
@@ -334,6 +341,7 @@ var Queue = class {
334
341
  // src/utils/index.ts
335
342
  //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
336
343
  //! SPDX-License-Identifier: GPL-3.0-or-later
344
+ var clearCache = Cache.clear;
337
345
  var SEND = 0xfffffff800000000n;
338
346
  var RECEIVE = 0xfffffe0000000000n;
339
347
  function stats(times) {
@@ -1379,8 +1387,11 @@ async function generate4(hash, difficulty, effort, debug) {
1379
1387
  // src/lib/config/index.ts
1380
1388
  //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@zoso.dev>
1381
1389
  //! SPDX-License-Identifier: GPL-3.0-or-later
1382
- var NanoPowConfigConstructor = class _NanoPowConfigConstructor {
1390
+ var NanoPowConfigConstructor = class {
1383
1391
  static #isInternal = false;
1392
+ static get isInternal() {
1393
+ return this.#isInternal;
1394
+ }
1384
1395
  api;
1385
1396
  debug;
1386
1397
  difficulty;
@@ -1394,14 +1405,13 @@ var NanoPowConfigConstructor = class _NanoPowConfigConstructor {
1394
1405
  };
1395
1406
  }
1396
1407
  constructor(api, debug, difficulty, effort) {
1397
- if (!_NanoPowConfigConstructor.#isInternal) {
1408
+ if (!this.constructor.isInternal) {
1398
1409
  throw new TypeError(`NanoPowConfig cannot be constructed with 'new'.`);
1399
1410
  }
1400
1411
  this.api = api;
1401
1412
  this.debug = debug;
1402
1413
  this.difficulty = difficulty;
1403
1414
  this.effort = effort;
1404
- _NanoPowConfigConstructor.#isInternal = false;
1405
1415
  }
1406
1416
  static async create(options) {
1407
1417
  const input = options;
@@ -1411,6 +1421,7 @@ var NanoPowConfigConstructor = class _NanoPowConfigConstructor {
1411
1421
  const effort = this.#getValidEffort(input);
1412
1422
  this.#isInternal = true;
1413
1423
  const config = new this(api, debug, difficulty, effort);
1424
+ this.#isInternal = false;
1414
1425
  return config;
1415
1426
  }
1416
1427
  // Check platform support for default API setting
@@ -1573,6 +1584,7 @@ var NanoPow = class {
1573
1584
  };
1574
1585
  export {
1575
1586
  NanoPow,
1587
+ clearCache,
1576
1588
  NanoPow as default,
1577
1589
  stats
1578
1590
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nano-pow",
3
- "version": "5.1.5",
3
+ "version": "5.1.7",
4
4
  "description": "Proof-of-work generation and validation with WebGPU/WebGL/WASM for Nano cryptocurrency.",
5
5
  "keywords": [
6
6
  "nemo",
@@ -43,8 +43,9 @@
43
43
  },
44
44
  "scripts": {
45
45
  "benchmark": "npm run build && ./dist/bin/nano-pow.sh --benchmark 10 --debug",
46
- "build": "rm -rf {dist,types} && tsc && npm run generate && node esbuild.mjs && cp -p src/bin/nano-pow.sh dist/bin",
46
+ "build": "npm run clean && npm run generate && node esbuild.mjs && cp -p src/bin/nano-pow.sh dist/bin",
47
47
  "build:dev": "NODE_ENV=development npm run build",
48
+ "clean": "rm -rf dist types && tsc",
48
49
  "generate": "node ./scripts/blake2b-gen.js && asc src/lib/generate/wasm/asm/index.ts",
49
50
  "prepare": "npm run build",
50
51
  "score": "npm run build && ./dist/bin/nano-pow.sh --effort 4 --benchmark 10 --score 10",
@@ -60,7 +61,7 @@
60
61
  "typescript": "^5.9.2"
61
62
  },
62
63
  "optionalDependencies": {
63
- "puppeteer": "^24.16.2"
64
+ "puppeteer": "^24.17.0"
64
65
  },
65
66
  "type": "module",
66
67
  "exports": {