@cap.js/widget 0.1.25 → 0.1.26
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/cap.min.js +1 -1
- package/package.json +1 -1
- package/src/cap.js +601 -597
- package/src/worker.js +113 -108
package/src/worker.js
CHANGED
|
@@ -1,110 +1,115 @@
|
|
|
1
1
|
(() => {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
2
|
+
const solveFallback = async ({ salt, target }) => {
|
|
3
|
+
let nonce = 0;
|
|
4
|
+
const batchSize = 50000;
|
|
5
|
+
const encoder = new TextEncoder();
|
|
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);
|
|
10
|
+
}
|
|
11
|
+
const targetBytesLength = targetBytes.length;
|
|
12
|
+
|
|
13
|
+
while (true) {
|
|
14
|
+
try {
|
|
15
|
+
for (let i = 0; i < batchSize; i++) {
|
|
16
|
+
const inputString = salt + nonce;
|
|
17
|
+
const inputBytes = encoder.encode(inputString);
|
|
18
|
+
|
|
19
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", inputBytes);
|
|
20
|
+
|
|
21
|
+
const hashBytes = new Uint8Array(hashBuffer, 0, targetBytesLength);
|
|
22
|
+
|
|
23
|
+
let matches = true;
|
|
24
|
+
for (let k = 0; k < targetBytesLength; k++) {
|
|
25
|
+
if (hashBytes[k] !== targetBytes[k]) {
|
|
26
|
+
matches = false;
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (matches) {
|
|
32
|
+
self.postMessage({ nonce, found: true });
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
nonce++;
|
|
37
|
+
}
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.error("[cap worker]", error);
|
|
40
|
+
self.postMessage({
|
|
41
|
+
found: false,
|
|
42
|
+
error: error.message,
|
|
43
|
+
});
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
if (
|
|
50
|
+
typeof WebAssembly !== "object" ||
|
|
51
|
+
typeof WebAssembly?.instantiate !== "function"
|
|
52
|
+
) {
|
|
53
|
+
console.warn(
|
|
54
|
+
"[cap worker] wasm not supported, falling back to alternative solver. this will be significantly slower.",
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
self.onmessage = async ({ data: { salt, target } }) => {
|
|
58
|
+
return solveFallback({ salt, target });
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
let wasmCacheUrl, solve_pow_function;
|
|
65
|
+
|
|
66
|
+
self.onmessage = async ({ data: { salt, target, wasmUrl } }) => {
|
|
67
|
+
let fb;
|
|
68
|
+
|
|
69
|
+
if (wasmCacheUrl !== wasmUrl) {
|
|
70
|
+
wasmCacheUrl = wasmUrl;
|
|
71
|
+
await import(wasmUrl)
|
|
72
|
+
.then((wasmModule) => {
|
|
73
|
+
return wasmModule.default().then((instance) => {
|
|
74
|
+
solve_pow_function = (
|
|
75
|
+
instance?.exports ? instance.exports : wasmModule
|
|
76
|
+
).solve_pow;
|
|
77
|
+
});
|
|
78
|
+
})
|
|
79
|
+
.catch((e) => {
|
|
80
|
+
console.error("[cap worker] using fallback solver due to error:", e);
|
|
81
|
+
fb = true;
|
|
82
|
+
|
|
83
|
+
return solveFallback({ salt, target });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
if (fb) return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const startTime = performance.now();
|
|
91
|
+
const nonce = solve_pow_function(salt, target);
|
|
92
|
+
const endTime = performance.now();
|
|
93
|
+
|
|
94
|
+
self.postMessage({
|
|
95
|
+
nonce: Number(nonce),
|
|
96
|
+
found: true,
|
|
97
|
+
durationMs: (endTime - startTime).toFixed(2),
|
|
98
|
+
});
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.error("[cap worker]", error);
|
|
101
|
+
|
|
102
|
+
self.postMessage({
|
|
103
|
+
found: false,
|
|
104
|
+
error: error.message || String(error),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
self.onerror = (error) => {
|
|
110
|
+
self.postMessage({
|
|
111
|
+
found: false,
|
|
112
|
+
error,
|
|
113
|
+
});
|
|
114
|
+
};
|
|
110
115
|
})();
|