@prosopo/account 2.8.102 → 2.8.104

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.
@@ -1,192 +0,0 @@
1
- // Copyright 2021-2026 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- import CryptoWorkerConstructor from "./cryptoWorker.js?worker&inline";
16
-
17
- interface CryptoWorkerMessage {
18
- taskId: string;
19
- task: string;
20
- data: Record<string, string | Uint8Array>;
21
- }
22
-
23
- export interface KeypairBytes {
24
- publicKey: Uint8Array;
25
- secretKey: Uint8Array;
26
- }
27
-
28
- export interface EntropyToKeypairResult extends KeypairBytes {
29
- mnemonic: string;
30
- }
31
-
32
- type CryptoWorkerResult = string | KeypairBytes | EntropyToKeypairResult;
33
-
34
- interface CryptoWorkerResponse {
35
- taskId: string;
36
- result?: CryptoWorkerResult;
37
- error?: string;
38
- }
39
-
40
- /**
41
- * Manages Web Worker for cryptographic operations to prevent blocking the main thread
42
- */
43
- export class CryptoWorkerManager {
44
- private worker: Worker | null = null;
45
- private isInitializing = false;
46
-
47
- /**
48
- * Initialize the worker. No test round-trip — construction failure throws
49
- * synchronously (caught below); post-construction failures surface via
50
- * `worker.onerror` (cleans up so the next call retries); anything that
51
- * gets past both surfaces via `runTask`'s timeout + reject path, which
52
- * already falls back to main-thread crypto. The prior `testWorker()` ping
53
- * was a Blob-URL-era defensive check that no longer earns its ~30–80ms
54
- * critical-path cost under Vite's `?worker&inline` constructor.
55
- */
56
- private initWorker(): void {
57
- if (this.worker || this.isInitializing) {
58
- return;
59
- }
60
-
61
- this.isInitializing = true;
62
-
63
- try {
64
- this.worker = new CryptoWorkerConstructor({ type: "module" });
65
-
66
- this.worker.onerror = () => {
67
- // Terminate the failed worker; the next runTask will re-init.
68
- this.cleanup();
69
- };
70
- } catch {
71
- this.cleanup();
72
- } finally {
73
- this.isInitializing = false;
74
- }
75
- }
76
-
77
- /**
78
- * Eagerly spawn the worker so the first runTask() doesn't pay the
79
- * worker-spawn + module-parse cost. Synchronous now — construction is
80
- * fire-and-forget with cleanup via `worker.onerror`.
81
- */
82
- prewarm(): void {
83
- this.initWorker();
84
- }
85
-
86
- /**
87
- * Run a task in the Web Worker
88
- */
89
- async runTask<T>(
90
- task: string,
91
- data: Record<string, string | Uint8Array>,
92
- ): Promise<T> {
93
- this.initWorker();
94
-
95
- if (!this.worker) {
96
- throw new Error("Failed to initialize worker");
97
- }
98
-
99
- const worker = this.worker;
100
-
101
- return new Promise((resolve, reject) => {
102
- const taskId = Math.random().toString(36).substr(2, 9);
103
- const timeoutId = setTimeout(() => {
104
- // Clean up listeners on timeout
105
- worker.removeEventListener("message", handleMessage);
106
- worker.removeEventListener("error", handleError);
107
- reject(new Error(`Worker task ${task} timeout (Task ID: ${taskId})`));
108
- }, 10000);
109
-
110
- const handleMessage = (event: MessageEvent<CryptoWorkerResponse>) => {
111
- if (event.data.taskId === taskId) {
112
- clearTimeout(timeoutId);
113
- worker.removeEventListener("message", handleMessage);
114
- worker.removeEventListener("error", handleError);
115
-
116
- if (event.data.error) {
117
- // Reject with the error message sent back by the worker
118
- reject(new Error(`Worker task failed: ${event.data.error}`));
119
- } else {
120
- resolve(event.data.result as T); // Type assertion for clean resolution
121
- }
122
- }
123
- };
124
-
125
- const handleError = (error: ErrorEvent) => {
126
- clearTimeout(timeoutId);
127
- worker.removeEventListener("message", handleMessage);
128
- worker.removeEventListener("error", handleError);
129
- // Reject on global worker error while task is pending
130
- reject(new Error(`Worker error during task: ${error.message}`));
131
- };
132
-
133
- worker.addEventListener("message", handleMessage);
134
- worker.addEventListener("error", handleError);
135
-
136
- const message: CryptoWorkerMessage = { taskId, task, data };
137
- worker.postMessage(message);
138
- });
139
- }
140
-
141
- /**
142
- * Convert entropy to mnemonic using Web Worker
143
- */
144
- async entropyToMnemonic(entropy: Uint8Array): Promise<string> {
145
- return this.runTask<string>("entropyToMnemonic", { entropy });
146
- }
147
-
148
- /**
149
- * Fused: entropy → mnemonic → sr25519 keypair in a single worker
150
- * round-trip. Callers that don't need the mnemonic separately should
151
- * prefer this over two `entropyToMnemonic + keypairFromMnemonic` hops —
152
- * saves one postMessage transit (~30–80ms on constrained hardware / under
153
- * throttle) on the frictionless critical path. sr25519 derivation is a
154
- * ~500ms main-thread cost otherwise.
155
- */
156
- async entropyToKeypair(entropy: Uint8Array): Promise<EntropyToKeypairResult> {
157
- return this.runTask<EntropyToKeypairResult>("entropyToKeypair", {
158
- entropy,
159
- });
160
- }
161
-
162
- /**
163
- * Clean up worker resources
164
- */
165
- private cleanup(): void {
166
- if (this.worker) {
167
- this.worker.terminate();
168
- }
169
- this.worker = null;
170
- this.isInitializing = false;
171
- }
172
-
173
- /**
174
- * Public dispose method
175
- */
176
- public dispose(): void {
177
- this.cleanup();
178
- }
179
- }
180
-
181
- // Create a singleton instance for reuse
182
- let workerManagerInstance: CryptoWorkerManager | null = null;
183
-
184
- /**
185
- * Get the singleton CryptoWorkerManager instance
186
- */
187
- export function getCryptoWorkerManager(): CryptoWorkerManager {
188
- if (!workerManagerInstance) {
189
- workerManagerInstance = new CryptoWorkerManager();
190
- }
191
- return workerManagerInstance;
192
- }
@@ -1,98 +0,0 @@
1
- // Copyright 2021-2026 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- // cryptoWorker.ts
16
-
17
- import {
18
- entropyToMnemonic,
19
- mnemonicToMiniSecret,
20
- sr25519FromSeed,
21
- } from "@prosopo/util-crypto";
22
-
23
- interface CryptoWorkerMessage {
24
- taskId: string;
25
- task: "entropyToMnemonic" | "entropyToKeypair" | "test";
26
- data: {
27
- entropy?: Uint8Array;
28
- };
29
- }
30
-
31
- interface EntropyToKeypairResult {
32
- mnemonic: string;
33
- publicKey: Uint8Array;
34
- secretKey: Uint8Array;
35
- }
36
-
37
- interface CryptoWorkerResponse {
38
- taskId: string;
39
- result?: string | EntropyToKeypairResult;
40
- error?: string;
41
- }
42
-
43
- // The main message listener runs in the worker's global scope
44
- self.addEventListener(
45
- "message",
46
- async (event: MessageEvent<CryptoWorkerMessage>) => {
47
- const { taskId, task, data } = event.data;
48
-
49
- try {
50
- let result: string | EntropyToKeypairResult;
51
-
52
- switch (task) {
53
- case "test":
54
- // Respond to test message to confirm worker is ready
55
- result = "ready";
56
- break;
57
- case "entropyToMnemonic":
58
- if (!data.entropy) {
59
- throw new Error("Entropy data is required");
60
- }
61
- result = entropyToMnemonic(data.entropy);
62
- break;
63
- case "entropyToKeypair":
64
- // Fused: entropy → mnemonic → sr25519 keypair in one worker
65
- // round-trip. Saves the postMessage transit on the frictionless
66
- // critical path (~30–80ms per hop on constrained hardware).
67
- // Called by ExtensionWeb2.createAccount when the caller doesn't
68
- // need the mnemonic string for any other purpose than deriving
69
- // the pair. sr25519 derivation is a scalar multiplication on
70
- // Ristretto25519 — ~500ms mid-tier, ~800ms mobile — and was the
71
- // single biggest main-thread cost pre-worker.
72
- if (!data.entropy) {
73
- throw new Error("Entropy data is required");
74
- }
75
- result = processEntropyToKeypair(data.entropy);
76
- break;
77
- default:
78
- throw new Error(`Unknown task: ${task}`);
79
- }
80
-
81
- const response: CryptoWorkerResponse = { taskId, result };
82
- self.postMessage(response);
83
- } catch (error) {
84
- const response: CryptoWorkerResponse = {
85
- taskId,
86
- error: error instanceof Error ? error.message : "Unknown error",
87
- };
88
- self.postMessage(response);
89
- }
90
- },
91
- );
92
-
93
- function processEntropyToKeypair(entropy: Uint8Array): EntropyToKeypairResult {
94
- const mnemonic = entropyToMnemonic(entropy);
95
- const seed = mnemonicToMiniSecret(mnemonic);
96
- const { publicKey, secretKey } = sr25519FromSeed(seed);
97
- return { mnemonic, publicKey, secretKey };
98
- }
@@ -1,18 +0,0 @@
1
- // Copyright 2021-2026 Prosopo (UK) Ltd.
2
- //
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
6
- //
7
- // http://www.apache.org/licenses/LICENSE-2.0
8
- //
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
14
-
15
- export {
16
- CryptoWorkerManager,
17
- getCryptoWorkerManager,
18
- } from "./CryptoWorkerManager.js";
package/tsconfig.cjs.json DELETED
@@ -1,37 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.cjs.json",
3
- "compilerOptions": {
4
- "rootDir": "./src",
5
- "outDir": "./dist/cjs"
6
- },
7
- "include": [
8
- "./src/**/*.ts",
9
- "./src/**/*.json",
10
- "./src/**/*.d.ts",
11
- "./src/**/*.tsx",
12
- "custom.d.ts"
13
- ],
14
- "references": [
15
- {
16
- "path": "../common/tsconfig.cjs.json"
17
- },
18
- {
19
- "path": "../util/tsconfig.cjs.json"
20
- },
21
- {
22
- "path": "../fingerprint/tsconfig.cjs.json"
23
- },
24
- {
25
- "path": "../keyring/tsconfig.cjs.json"
26
- },
27
- {
28
- "path": "../types/tsconfig.cjs.json"
29
- },
30
- {
31
- "path": "../util-crypto/tsconfig.cjs.json"
32
- },
33
- {
34
- "path": "../../dev/config/tsconfig.cjs.json"
35
- }
36
- ]
37
- }
package/tsconfig.json DELETED
@@ -1,40 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.esm.json",
3
- "compilerOptions": {
4
- "rootDir": "./src",
5
- "outDir": "./dist",
6
- "lib": ["es6", "dom"],
7
- "allowJs": true
8
- },
9
- "include": [
10
- "src",
11
- "src/**/*.json",
12
- "src/**/*.ts",
13
- "src/**/*.tsx",
14
- "src/**/*.d.ts",
15
- "custom.d.ts"
16
- ],
17
- "references": [
18
- {
19
- "path": "../common"
20
- },
21
- {
22
- "path": "../util"
23
- },
24
- {
25
- "path": "../fingerprint"
26
- },
27
- {
28
- "path": "../keyring"
29
- },
30
- {
31
- "path": "../types"
32
- },
33
- {
34
- "path": "../util-crypto"
35
- },
36
- {
37
- "path": "../../dev/config/tsconfig.json"
38
- }
39
- ]
40
- }