@cap.js/widget 0.1.35 → 0.1.37
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-floating.js +2 -2
- package/src/cap.css +244 -0
- package/src/cap.js +861 -604
- package/src/worker.js +196 -113
- package/wasm-hashes.min.js +1 -26
package/src/worker.js
CHANGED
|
@@ -1,115 +1,198 @@
|
|
|
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
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
2
|
+
const solveFallback = async ({ salt, target }) => {
|
|
3
|
+
let nonce = 0;
|
|
4
|
+
const batchSize = 50000;
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
|
|
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);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const partialMask = remainingBits > 0 ? (0xff << (8 - remainingBits)) & 0xff : 0;
|
|
19
|
+
|
|
20
|
+
while (true) {
|
|
21
|
+
try {
|
|
22
|
+
for (let i = 0; i < batchSize; i++) {
|
|
23
|
+
const inputString = salt + nonce;
|
|
24
|
+
const inputBytes = encoder.encode(inputString);
|
|
25
|
+
|
|
26
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", inputBytes);
|
|
27
|
+
|
|
28
|
+
const hashBytes = new Uint8Array(hashBuffer);
|
|
29
|
+
|
|
30
|
+
let matches = true;
|
|
31
|
+
|
|
32
|
+
for (let k = 0; k < fullBytes; k++) {
|
|
33
|
+
if (hashBytes[k] !== targetBytes[k]) {
|
|
34
|
+
matches = false;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (matches && remainingBits > 0) {
|
|
40
|
+
if ((hashBytes[fullBytes] & partialMask) !== (targetBytes[fullBytes] & partialMask)) {
|
|
41
|
+
matches = false;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (matches) {
|
|
46
|
+
self.postMessage({ nonce, found: true });
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
nonce++;
|
|
51
|
+
}
|
|
52
|
+
} catch (error) {
|
|
53
|
+
console.error("[cap worker]", error);
|
|
54
|
+
self.postMessage({
|
|
55
|
+
found: false,
|
|
56
|
+
error: error.message,
|
|
57
|
+
});
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
if (typeof WebAssembly !== "object" || typeof WebAssembly?.instantiate !== "function") {
|
|
64
|
+
console.warn(
|
|
65
|
+
"[cap worker] wasm not supported, falling back to alternative solver. this will be significantly slower.",
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
self.onmessage = async ({ data: { salt, target } }) => {
|
|
69
|
+
return solveFallback({ salt, target });
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let solve_pow_function = null;
|
|
76
|
+
|
|
77
|
+
const initFromModule = (wasmModule) => {
|
|
78
|
+
try {
|
|
79
|
+
let wasm;
|
|
80
|
+
let WASM_VECTOR_LEN = 0;
|
|
81
|
+
let cachedUint8ArrayMemory = null;
|
|
82
|
+
|
|
83
|
+
const getMemory = () => {
|
|
84
|
+
if (cachedUint8ArrayMemory === null || cachedUint8ArrayMemory.byteLength === 0) {
|
|
85
|
+
cachedUint8ArrayMemory = new Uint8Array(wasm.memory.buffer);
|
|
86
|
+
}
|
|
87
|
+
return cachedUint8ArrayMemory;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const encoder = new TextEncoder();
|
|
91
|
+
|
|
92
|
+
const passStringToWasm = (str, malloc, realloc) => {
|
|
93
|
+
if (realloc === undefined) {
|
|
94
|
+
const encoded = encoder.encode(str);
|
|
95
|
+
const ptr = malloc(encoded.length, 1) >>> 0;
|
|
96
|
+
getMemory()
|
|
97
|
+
.subarray(ptr, ptr + encoded.length)
|
|
98
|
+
.set(encoded);
|
|
99
|
+
WASM_VECTOR_LEN = encoded.length;
|
|
100
|
+
return ptr;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
let len = str.length;
|
|
104
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
105
|
+
const mem = getMemory();
|
|
106
|
+
let offset = 0;
|
|
107
|
+
|
|
108
|
+
for (; offset < len; offset++) {
|
|
109
|
+
const code = str.charCodeAt(offset);
|
|
110
|
+
if (code > 127) break;
|
|
111
|
+
mem[ptr + offset] = code;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (offset !== len) {
|
|
115
|
+
if (offset !== 0) str = str.slice(offset);
|
|
116
|
+
ptr = realloc(ptr, len, (len = offset + str.length * 3), 1) >>> 0;
|
|
117
|
+
const subarray = getMemory().subarray(ptr + offset, ptr + len);
|
|
118
|
+
const { written } = encoder.encodeInto(str, subarray);
|
|
119
|
+
offset += written;
|
|
120
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
WASM_VECTOR_LEN = offset;
|
|
124
|
+
return ptr;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const imports = { wbg: {} };
|
|
128
|
+
imports.wbg.__wbindgen_init_externref_table = () => {
|
|
129
|
+
const table = wasm.__wbindgen_export_0;
|
|
130
|
+
const offset = table.grow(4);
|
|
131
|
+
table.set(0, undefined);
|
|
132
|
+
table.set(offset + 0, undefined);
|
|
133
|
+
table.set(offset + 1, null);
|
|
134
|
+
table.set(offset + 2, true);
|
|
135
|
+
table.set(offset + 3, false);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const instance = new WebAssembly.Instance(wasmModule, imports);
|
|
139
|
+
wasm = instance.exports;
|
|
140
|
+
|
|
141
|
+
if (wasm.__wbindgen_start) wasm.__wbindgen_start();
|
|
142
|
+
|
|
143
|
+
solve_pow_function = (salt, target) => {
|
|
144
|
+
const saltPtr = passStringToWasm(salt, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
145
|
+
const saltLen = WASM_VECTOR_LEN;
|
|
146
|
+
const targetPtr = passStringToWasm(target, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
147
|
+
const targetLen = WASM_VECTOR_LEN;
|
|
148
|
+
return BigInt.asUintN(64, wasm.solve_pow(saltPtr, saltLen, targetPtr, targetLen));
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
return true;
|
|
152
|
+
} catch (e) {
|
|
153
|
+
console.error("[cap worker] failed to init wasm from module:", e);
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
self.onmessage = async ({ data: { salt, target, wasmModule } }) => {
|
|
159
|
+
if (wasmModule instanceof WebAssembly.Module && solve_pow_function === null) {
|
|
160
|
+
const ok = initFromModule(wasmModule);
|
|
161
|
+
if (!ok) {
|
|
162
|
+
console.warn("[cap worker] wasm init failed, falling back to JS solver.");
|
|
163
|
+
return solveFallback({ salt, target });
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (solve_pow_function === null) {
|
|
168
|
+
console.warn("[cap worker] no wasm module provided, falling back to JS solver.");
|
|
169
|
+
return solveFallback({ salt, target });
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
const startTime = performance.now();
|
|
174
|
+
const nonce = solve_pow_function(salt, target);
|
|
175
|
+
const endTime = performance.now();
|
|
176
|
+
|
|
177
|
+
self.postMessage({
|
|
178
|
+
nonce: Number(nonce),
|
|
179
|
+
found: true,
|
|
180
|
+
durationMs: (endTime - startTime).toFixed(2),
|
|
181
|
+
});
|
|
182
|
+
} catch (error) {
|
|
183
|
+
console.error("[cap worker]", error);
|
|
184
|
+
|
|
185
|
+
self.postMessage({
|
|
186
|
+
found: false,
|
|
187
|
+
error: error.message || String(error),
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
self.onerror = (error) => {
|
|
193
|
+
self.postMessage({
|
|
194
|
+
found: false,
|
|
195
|
+
error,
|
|
196
|
+
});
|
|
197
|
+
};
|
|
115
198
|
})();
|
package/wasm-hashes.min.js
CHANGED
|
@@ -359,26 +359,6 @@
|
|
|
359
359
|
});
|
|
360
360
|
});
|
|
361
361
|
|
|
362
|
-
// ====================================================
|
|
363
|
-
|
|
364
|
-
// this is a now unused library which an old version of
|
|
365
|
-
// cap depended on. to avoid breaking things as much as
|
|
366
|
-
// possible, we've kept it here.
|
|
367
|
-
|
|
368
|
-
try {
|
|
369
|
-
// lets us know how many people are actually using this
|
|
370
|
-
// so we can know when it's safe to deprecate.
|
|
371
|
-
|
|
372
|
-
// server source code:
|
|
373
|
-
// https://gist.github.com/tiagozip/ffb39489f5af91e6e45f94f1bfa14ab2
|
|
374
|
-
fetch(
|
|
375
|
-
`https://api.tiagorangel.com/cap-legacy-widget?h=${self.location?.hostname || "unknown"}`,
|
|
376
|
-
{
|
|
377
|
-
mode: "no-cors",
|
|
378
|
-
},
|
|
379
|
-
).catch(() => {});
|
|
380
|
-
} catch {}
|
|
381
|
-
|
|
382
362
|
console.warn(
|
|
383
363
|
`[cap]
|
|
384
364
|
%cYou're using a deprecated version of Cap's widget that still relies on this file.
|
|
@@ -387,9 +367,4 @@ It may continue to work for now, but could break at any time since this dependen
|
|
|
387
367
|
|
|
388
368
|
Please update Cap to fix this.`,
|
|
389
369
|
"font-size:15px;background-image:url('https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fpreview.colorkit.co%2Fcolor%2FEEEEEE.png%3Ftype%3Darticle-preview-logo%26size%3Dsocial%26colorname%3DSuper%2520Silver&f=1&nofb=1&ipt=49845e9195461b7c779182793c2ebf7834102eaf5561c15fa2cbb55494b77a9b');background-size:10px",
|
|
390
|
-
);
|
|
391
|
-
|
|
392
|
-
console.log(
|
|
393
|
-
`%cTo help us understand how many people are using this legacy version and when it's safe to deprecate it, we've sent a small, privacy-friendly beacon to our servers. All related code, including the telemetry server, is open source.\n\nYou might have gotten a CSP error before this, it should be safe to ignore. Server source code: https://gist.github.com/tiagozip/ffb39489f5af91e6e45f94f1bfa14ab2`,
|
|
394
|
-
"font-size:11px;color:#b7b7b7",
|
|
395
|
-
);
|
|
370
|
+
);
|