@cap.js/widget 0.1.58 → 0.1.59

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/src/worker.js CHANGED
@@ -7,7 +7,8 @@
7
7
  const HASHWX_YIELD_MS = 16;
8
8
  const HASHWX_PROGRESS_MS = 150;
9
9
  let stopRequested = false;
10
- const yieldChannel = typeof MessageChannel === "function" ? new MessageChannel() : null;
10
+ const yieldChannel =
11
+ typeof MessageChannel === "function" ? new MessageChannel() : null;
11
12
  const macrotask = () =>
12
13
  new Promise((resolve) => {
13
14
  if (!yieldChannel) return setTimeout(resolve, 0);
@@ -31,7 +32,8 @@
31
32
  targetBytes[k] = parseInt(paddedTarget.substring(k * 2, k * 2 + 2), 16);
32
33
  }
33
34
 
34
- const partialMask = remainingBits > 0 ? (0xff << (8 - remainingBits)) & 0xff : 0;
35
+ const partialMask =
36
+ remainingBits > 0 ? (0xff << (8 - remainingBits)) & 0xff : 0;
35
37
 
36
38
  while (true) {
37
39
  try {
@@ -53,7 +55,10 @@
53
55
  }
54
56
 
55
57
  if (matches && remainingBits > 0) {
56
- if ((hashBytes[fullBytes] & partialMask) !== (targetBytes[fullBytes] & partialMask)) {
58
+ if (
59
+ (hashBytes[fullBytes] & partialMask) !==
60
+ (targetBytes[fullBytes] & partialMask)
61
+ ) {
57
62
  matches = false;
58
63
  }
59
64
  }
@@ -66,7 +71,7 @@
66
71
  nonce++;
67
72
  }
68
73
  } catch (error) {
69
- wlog("error","fallback solver crashed:", error.message || error);
74
+ wlog("error", "fallback solver crashed:", error.message || error);
70
75
  self.postMessage({
71
76
  found: false,
72
77
  error: error.message,
@@ -95,14 +100,17 @@
95
100
  };
96
101
 
97
102
  const SHA_K = new Uint32Array([
98
- 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
99
- 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
100
- 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
101
- 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
102
- 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
103
- 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
104
- 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
105
- 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
103
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
104
+ 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
105
+ 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
106
+ 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
107
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
108
+ 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
109
+ 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
110
+ 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
111
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
112
+ 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
113
+ 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
106
114
  ]);
107
115
 
108
116
  const sha256Into = (bytes, out) => {
@@ -116,39 +124,77 @@
116
124
  dv.setUint32(padded.length - 4, bitLen >>> 0, false);
117
125
  dv.setUint32(padded.length - 8, Math.floor(bitLen / 0x100000000), false);
118
126
 
119
- let h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372, h3 = 0xa54ff53a;
120
- let h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19;
127
+ let h0 = 0x6a09e667,
128
+ h1 = 0xbb67ae85,
129
+ h2 = 0x3c6ef372,
130
+ h3 = 0xa54ff53a;
131
+ let h4 = 0x510e527f,
132
+ h5 = 0x9b05688c,
133
+ h6 = 0x1f83d9ab,
134
+ h7 = 0x5be0cd19;
121
135
  const w = new Uint32Array(64);
122
136
 
123
137
  for (let b = 0; b < blocks; b++) {
124
138
  const off = b << 6;
125
139
  for (let i = 0; i < 16; i++) w[i] = dv.getUint32(off + (i << 2), false);
126
140
  for (let i = 16; i < 64; i++) {
127
- const x = w[i - 15], y = w[i - 2];
128
- const s0 = ((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14)) ^ (x >>> 3);
129
- const s1 = ((y >>> 17) | (y << 15)) ^ ((y >>> 19) | (y << 13)) ^ (y >>> 10);
141
+ const x = w[i - 15],
142
+ y = w[i - 2];
143
+ const s0 =
144
+ ((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14)) ^ (x >>> 3);
145
+ const s1 =
146
+ ((y >>> 17) | (y << 15)) ^ ((y >>> 19) | (y << 13)) ^ (y >>> 10);
130
147
  w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
131
148
  }
132
- let a = h0, bb = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7;
149
+ let a = h0,
150
+ bb = h1,
151
+ c = h2,
152
+ d = h3,
153
+ e = h4,
154
+ f = h5,
155
+ g = h6,
156
+ h = h7;
133
157
  for (let i = 0; i < 64; i++) {
134
- const S1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
158
+ const S1 =
159
+ ((e >>> 6) | (e << 26)) ^
160
+ ((e >>> 11) | (e << 21)) ^
161
+ ((e >>> 25) | (e << 7));
135
162
  const ch = (e & f) ^ (~e & g);
136
163
  const t1 = (h + S1 + ch + SHA_K[i] + w[i]) >>> 0;
137
- const S0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
164
+ const S0 =
165
+ ((a >>> 2) | (a << 30)) ^
166
+ ((a >>> 13) | (a << 19)) ^
167
+ ((a >>> 22) | (a << 10));
138
168
  const maj = (a & bb) ^ (a & c) ^ (bb & c);
139
169
  const t2 = (S0 + maj) >>> 0;
140
- h = g; g = f; f = e; e = (d + t1) >>> 0;
141
- d = c; c = bb; bb = a; a = (t1 + t2) >>> 0;
170
+ h = g;
171
+ g = f;
172
+ f = e;
173
+ e = (d + t1) >>> 0;
174
+ d = c;
175
+ c = bb;
176
+ bb = a;
177
+ a = (t1 + t2) >>> 0;
142
178
  }
143
- h0 = (h0 + a) >>> 0; h1 = (h1 + bb) >>> 0; h2 = (h2 + c) >>> 0; h3 = (h3 + d) >>> 0;
144
- h4 = (h4 + e) >>> 0; h5 = (h5 + f) >>> 0; h6 = (h6 + g) >>> 0; h7 = (h7 + h) >>> 0;
179
+ h0 = (h0 + a) >>> 0;
180
+ h1 = (h1 + bb) >>> 0;
181
+ h2 = (h2 + c) >>> 0;
182
+ h3 = (h3 + d) >>> 0;
183
+ h4 = (h4 + e) >>> 0;
184
+ h5 = (h5 + f) >>> 0;
185
+ h6 = (h6 + g) >>> 0;
186
+ h7 = (h7 + h) >>> 0;
145
187
  }
146
188
 
147
189
  const odv = new DataView(out.buffer, out.byteOffset, 32);
148
- odv.setUint32(0, h0, false); odv.setUint32(4, h1, false);
149
- odv.setUint32(8, h2, false); odv.setUint32(12, h3, false);
150
- odv.setUint32(16, h4, false); odv.setUint32(20, h5, false);
151
- odv.setUint32(24, h6, false); odv.setUint32(28, h7, false);
190
+ odv.setUint32(0, h0, false);
191
+ odv.setUint32(4, h1, false);
192
+ odv.setUint32(8, h2, false);
193
+ odv.setUint32(12, h3, false);
194
+ odv.setUint32(16, h4, false);
195
+ odv.setUint32(20, h5, false);
196
+ odv.setUint32(24, h6, false);
197
+ odv.setUint32(28, h7, false);
152
198
  };
153
199
 
154
200
  let hashwxRuntime = null;
@@ -192,7 +238,8 @@
192
238
  getHashwxRuntime(hashwxModule);
193
239
 
194
240
  const challenge = new Uint8Array(32);
195
- for (let i = 0; i < 32; i++) challenge[i] = parseInt(c.slice(i * 2, i * 2 + 2), 16);
241
+ for (let i = 0; i < 32; i++)
242
+ challenge[i] = parseInt(c.slice(i * 2, i * 2 + 2), 16);
196
243
  const seedInput = new Uint8Array(40);
197
244
  seedInput.set(challenge, 0);
198
245
  const seedOut = new Uint8Array(32);
@@ -205,7 +252,10 @@
205
252
  try {
206
253
  for (let block = workerIndex; ; block += workerCount) {
207
254
  let rest = block;
208
- for (let i = 0; i < 8; i++) { seedInput[32 + i] = rest & 0xff; rest = Math.floor(rest / 256); }
255
+ for (let i = 0; i < 8; i++) {
256
+ seedInput[32 + i] = rest & 0xff;
257
+ rest = Math.floor(rest / 256);
258
+ }
209
259
  sha256Into(seedInput, seedOut);
210
260
  new Uint8Array(w.memory.buffer, seedPtr, 32).set(seedOut);
211
261
  w.hashwx_make(ctx, seedPtr);
@@ -214,7 +264,11 @@
214
264
  if (compiled) {
215
265
  const codePtr = w.hashwx_module(ctx);
216
266
  const codeSize = w.hashwx_module_size(ctx);
217
- const bytes = new Uint8Array(w.memory.buffer, codePtr, codeSize).slice();
267
+ const bytes = new Uint8Array(
268
+ w.memory.buffer,
269
+ codePtr,
270
+ codeSize,
271
+ ).slice();
218
272
  side = new WebAssembly.Instance(new WebAssembly.Module(bytes), {
219
273
  env: { memory: w.memory },
220
274
  }).exports.exec;
@@ -264,8 +318,14 @@
264
318
  }
265
319
  };
266
320
 
267
- if (typeof WebAssembly !== "object" || typeof WebAssembly?.instantiate !== "function") {
268
- wlog("warn","WebAssembly unavailable, using JS fallback solver (significantly slower)");
321
+ if (
322
+ typeof WebAssembly !== "object" ||
323
+ typeof WebAssembly?.instantiate !== "function"
324
+ ) {
325
+ wlog(
326
+ "warn",
327
+ "WebAssembly unavailable, using JS fallback solver (significantly slower)",
328
+ );
269
329
 
270
330
  self.onmessage = async ({ data }) => {
271
331
  if (data?.kind === "stop") {
@@ -294,7 +354,10 @@
294
354
  let cachedUint8ArrayMemory = null;
295
355
 
296
356
  const getMemory = () => {
297
- if (cachedUint8ArrayMemory === null || cachedUint8ArrayMemory.byteLength === 0) {
357
+ if (
358
+ cachedUint8ArrayMemory === null ||
359
+ cachedUint8ArrayMemory.byteLength === 0
360
+ ) {
298
361
  cachedUint8ArrayMemory = new Uint8Array(wasm.memory.buffer);
299
362
  }
300
363
  return cachedUint8ArrayMemory;
@@ -356,16 +419,27 @@
356
419
  if (wasm.__wbindgen_start) wasm.__wbindgen_start();
357
420
 
358
421
  solve_pow_function = (salt, target) => {
359
- const saltPtr = passStringToWasm(salt, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
422
+ const saltPtr = passStringToWasm(
423
+ salt,
424
+ wasm.__wbindgen_malloc,
425
+ wasm.__wbindgen_realloc,
426
+ );
360
427
  const saltLen = WASM_VECTOR_LEN;
361
- const targetPtr = passStringToWasm(target, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
428
+ const targetPtr = passStringToWasm(
429
+ target,
430
+ wasm.__wbindgen_malloc,
431
+ wasm.__wbindgen_realloc,
432
+ );
362
433
  const targetLen = WASM_VECTOR_LEN;
363
- return BigInt.asUintN(64, wasm.solve_pow(saltPtr, saltLen, targetPtr, targetLen));
434
+ return BigInt.asUintN(
435
+ 64,
436
+ wasm.solve_pow(saltPtr, saltLen, targetPtr, targetLen),
437
+ );
364
438
  };
365
439
 
366
440
  return true;
367
441
  } catch (e) {
368
- wlog("error","wasm init failed:", e.message || e);
442
+ wlog("error", "wasm init failed:", e.message || e);
369
443
  return false;
370
444
  }
371
445
  };
@@ -381,20 +455,26 @@
381
455
  return await solveHashwx(data);
382
456
  } catch (error) {
383
457
  wlog("error", "hashwx solver threw:", error.message || error);
384
- return self.postMessage({ found: false, error: error.message || String(error) });
458
+ return self.postMessage({
459
+ found: false,
460
+ error: error.message || String(error),
461
+ });
385
462
  }
386
463
  }
387
464
  const { salt, target, wasmModule } = data;
388
- if (wasmModule instanceof WebAssembly.Module && solve_pow_function === null) {
465
+ if (
466
+ wasmModule instanceof WebAssembly.Module &&
467
+ solve_pow_function === null
468
+ ) {
389
469
  const ok = initFromModule(wasmModule);
390
470
  if (!ok) {
391
- wlog("warn","wasm init failed, falling back to JS solver");
471
+ wlog("warn", "wasm init failed, falling back to JS solver");
392
472
  return solveFallback({ salt, target });
393
473
  }
394
474
  }
395
475
 
396
476
  if (solve_pow_function === null) {
397
- wlog("warn","no wasm module provided, falling back to JS solver");
477
+ wlog("warn", "no wasm module provided, falling back to JS solver");
398
478
  return solveFallback({ salt, target });
399
479
  }
400
480
 
@@ -409,7 +489,7 @@
409
489
  durationMs: (endTime - startTime).toFixed(2),
410
490
  });
411
491
  } catch (error) {
412
- wlog("error","solve_pow threw:", error.message || error);
492
+ wlog("error", "solve_pow threw:", error.message || error);
413
493
 
414
494
  self.postMessage({
415
495
  found: false,