@cap.js/widget 0.1.36 → 0.1.38

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
@@ -4,11 +4,18 @@
4
4
  const batchSize = 50000;
5
5
  const encoder = new TextEncoder();
6
6
 
7
- const targetBytes = new Uint8Array(target.length / 2);
8
- for (let k = 0; k < targetBytes.length; k++) {
9
- targetBytes[k] = parseInt(target.substring(k * 2, k * 2 + 2), 16);
7
+ const targetBits = target.length * 4;
8
+ const fullBytes = Math.floor(targetBits / 8);
9
+ const remainingBits = targetBits % 8;
10
+
11
+ const paddedTarget = target.length % 2 === 0 ? target : target + "0";
12
+ const targetBytesLength = paddedTarget.length / 2;
13
+ const targetBytes = new Uint8Array(targetBytesLength);
14
+ for (let k = 0; k < targetBytesLength; k++) {
15
+ targetBytes[k] = parseInt(paddedTarget.substring(k * 2, k * 2 + 2), 16);
10
16
  }
11
- const targetBytesLength = targetBytes.length;
17
+
18
+ const partialMask = remainingBits > 0 ? (0xff << (8 - remainingBits)) & 0xff : 0;
12
19
 
13
20
  while (true) {
14
21
  try {
@@ -18,16 +25,23 @@
18
25
 
19
26
  const hashBuffer = await crypto.subtle.digest("SHA-256", inputBytes);
20
27
 
21
- const hashBytes = new Uint8Array(hashBuffer, 0, targetBytesLength);
28
+ const hashBytes = new Uint8Array(hashBuffer);
22
29
 
23
30
  let matches = true;
24
- for (let k = 0; k < targetBytesLength; k++) {
31
+
32
+ for (let k = 0; k < fullBytes; k++) {
25
33
  if (hashBytes[k] !== targetBytes[k]) {
26
34
  matches = false;
27
35
  break;
28
36
  }
29
37
  }
30
38
 
39
+ if (matches && remainingBits > 0) {
40
+ if ((hashBytes[fullBytes] & partialMask) !== (targetBytes[fullBytes] & partialMask)) {
41
+ matches = false;
42
+ }
43
+ }
44
+
31
45
  if (matches) {
32
46
  self.postMessage({ nonce, found: true });
33
47
  return;
@@ -123,7 +137,7 @@
123
137
 
124
138
  const instance = new WebAssembly.Instance(wasmModule, imports);
125
139
  wasm = instance.exports;
126
-
140
+
127
141
  if (wasm.__wbindgen_start) wasm.__wbindgen_start();
128
142
 
129
143
  solve_pow_function = (salt, target) => {