@btc-vision/bitcoin 7.0.0-alpha.0 → 7.0.0-alpha.10
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/README.md +455 -155
- package/browser/address.d.ts +6 -2
- package/browser/address.d.ts.map +1 -1
- package/browser/block.d.ts.map +1 -1
- package/browser/branded.d.ts +3 -14
- package/browser/branded.d.ts.map +1 -1
- package/browser/chunks/psbt-parallel-BBFlkmiv.js +10717 -0
- package/browser/crypto.d.ts +1 -1
- package/browser/ecc/context.d.ts +25 -24
- package/browser/ecc/context.d.ts.map +1 -1
- package/browser/ecc/index.d.ts +1 -1
- package/browser/ecc/index.d.ts.map +1 -1
- package/browser/ecc/types.d.ts +10 -123
- package/browser/ecc/types.d.ts.map +1 -1
- package/browser/env.d.ts +13 -0
- package/browser/env.d.ts.map +1 -0
- package/browser/index.d.ts +7 -7
- package/browser/index.d.ts.map +1 -1
- package/browser/index.js +2497 -11686
- package/browser/io/BinaryReader.d.ts +15 -15
- package/browser/io/BinaryReader.d.ts.map +1 -1
- package/browser/io/BinaryWriter.d.ts +17 -17
- package/browser/io/BinaryWriter.d.ts.map +1 -1
- package/browser/io/hex.d.ts.map +1 -1
- package/browser/io/index.d.ts +0 -1
- package/browser/io/index.d.ts.map +1 -1
- package/browser/opcodes.d.ts +11 -0
- package/browser/opcodes.d.ts.map +1 -1
- package/browser/payments/bip341.d.ts +1 -1
- package/browser/payments/bip341.d.ts.map +1 -1
- package/browser/payments/embed.d.ts +1 -1
- package/browser/payments/embed.d.ts.map +1 -1
- package/browser/payments/p2ms.d.ts.map +1 -1
- package/browser/payments/p2op.d.ts +1 -1
- package/browser/payments/p2op.d.ts.map +1 -1
- package/browser/payments/p2pk.d.ts +1 -1
- package/browser/payments/p2pk.d.ts.map +1 -1
- package/browser/payments/p2pkh.d.ts +1 -1
- package/browser/payments/p2pkh.d.ts.map +1 -1
- package/browser/payments/p2sh.d.ts.map +1 -1
- package/browser/payments/p2tr.d.ts +2 -2
- package/browser/payments/p2tr.d.ts.map +1 -1
- package/browser/payments/p2wpkh.d.ts +1 -1
- package/browser/payments/p2wpkh.d.ts.map +1 -1
- package/browser/payments/p2wsh.d.ts.map +1 -1
- package/browser/payments/types.d.ts +1 -1
- package/browser/payments/types.d.ts.map +1 -1
- package/browser/psbt/PsbtCache.d.ts +54 -0
- package/browser/psbt/PsbtCache.d.ts.map +1 -0
- package/browser/psbt/PsbtFinalizer.d.ts +21 -0
- package/browser/psbt/PsbtFinalizer.d.ts.map +1 -0
- package/browser/psbt/PsbtSigner.d.ts +32 -0
- package/browser/psbt/PsbtSigner.d.ts.map +1 -0
- package/browser/psbt/PsbtTransaction.d.ts +25 -0
- package/browser/psbt/PsbtTransaction.d.ts.map +1 -0
- package/browser/psbt/bip371.d.ts.map +1 -1
- package/browser/psbt/types.d.ts +5 -71
- package/browser/psbt/types.d.ts.map +1 -1
- package/browser/psbt/validation.d.ts +1 -1
- package/browser/psbt/validation.d.ts.map +1 -1
- package/browser/psbt.d.ts +26 -40
- package/browser/psbt.d.ts.map +1 -1
- package/browser/script.d.ts.map +1 -1
- package/browser/transaction.d.ts +4 -4
- package/browser/transaction.d.ts.map +1 -1
- package/browser/types.d.ts +5 -3
- package/browser/types.d.ts.map +1 -1
- package/browser/workers/WorkerSigningPool.d.ts +24 -17
- package/browser/workers/WorkerSigningPool.d.ts.map +1 -1
- package/browser/workers/WorkerSigningPool.node.d.ts +19 -12
- package/browser/workers/WorkerSigningPool.node.d.ts.map +1 -1
- package/browser/workers/WorkerSigningPool.sequential.d.ts +67 -0
- package/browser/workers/WorkerSigningPool.sequential.d.ts.map +1 -0
- package/browser/workers/WorkerSigningPool.worklet.d.ts +64 -0
- package/browser/workers/WorkerSigningPool.worklet.d.ts.map +1 -0
- package/browser/workers/index.browser.d.ts +16 -0
- package/browser/workers/index.browser.d.ts.map +1 -0
- package/browser/workers/index.d.ts +4 -64
- package/browser/workers/index.d.ts.map +1 -1
- package/browser/workers/index.js +28 -0
- package/browser/workers/index.node.d.ts +17 -0
- package/browser/workers/index.node.d.ts.map +1 -0
- package/browser/workers/index.react-native.d.ts +28 -0
- package/browser/workers/index.react-native.d.ts.map +1 -0
- package/browser/workers/index.shared.d.ts +15 -0
- package/browser/workers/index.shared.d.ts.map +1 -0
- package/browser/workers/psbt-parallel.d.ts +2 -3
- package/browser/workers/psbt-parallel.d.ts.map +1 -1
- package/browser/workers/types.d.ts +17 -0
- package/browser/workers/types.d.ts.map +1 -1
- package/build/address.d.ts +6 -2
- package/build/address.d.ts.map +1 -1
- package/build/address.js +32 -19
- package/build/address.js.map +1 -1
- package/build/bech32utils.js.map +1 -1
- package/build/block.d.ts.map +1 -1
- package/build/block.js +2 -4
- package/build/block.js.map +1 -1
- package/build/branded.d.ts +3 -14
- package/build/branded.d.ts.map +1 -1
- package/build/branded.js +0 -5
- package/build/branded.js.map +1 -1
- package/build/crypto.d.ts +1 -1
- package/build/ecc/context.d.ts +25 -24
- package/build/ecc/context.d.ts.map +1 -1
- package/build/ecc/context.js +29 -101
- package/build/ecc/context.js.map +1 -1
- package/build/ecc/index.d.ts +1 -1
- package/build/ecc/index.d.ts.map +1 -1
- package/build/ecc/types.d.ts +7 -126
- package/build/ecc/types.d.ts.map +1 -1
- package/build/ecc/types.js +4 -1
- package/build/ecc/types.js.map +1 -1
- package/build/env.d.ts +13 -0
- package/build/env.d.ts.map +1 -0
- package/build/env.js +198 -0
- package/build/env.js.map +1 -0
- package/build/index.d.ts +8 -7
- package/build/index.d.ts.map +1 -1
- package/build/index.js +9 -7
- package/build/index.js.map +1 -1
- package/build/io/BinaryReader.d.ts +15 -15
- package/build/io/BinaryReader.d.ts.map +1 -1
- package/build/io/BinaryReader.js +17 -17
- package/build/io/BinaryReader.js.map +1 -1
- package/build/io/BinaryWriter.d.ts +17 -17
- package/build/io/BinaryWriter.d.ts.map +1 -1
- package/build/io/BinaryWriter.js +39 -39
- package/build/io/BinaryWriter.js.map +1 -1
- package/build/io/hex.d.ts.map +1 -1
- package/build/io/hex.js +2 -1
- package/build/io/hex.js.map +1 -1
- package/build/io/index.d.ts +0 -1
- package/build/io/index.d.ts.map +1 -1
- package/build/io/index.js +0 -2
- package/build/io/index.js.map +1 -1
- package/build/opcodes.d.ts +11 -0
- package/build/opcodes.d.ts.map +1 -1
- package/build/opcodes.js +19 -4
- package/build/opcodes.js.map +1 -1
- package/build/payments/bip341.d.ts +1 -2
- package/build/payments/bip341.d.ts.map +1 -1
- package/build/payments/bip341.js +1 -2
- package/build/payments/bip341.js.map +1 -1
- package/build/payments/embed.d.ts +1 -1
- package/build/payments/embed.d.ts.map +1 -1
- package/build/payments/embed.js +14 -14
- package/build/payments/embed.js.map +1 -1
- package/build/payments/p2ms.d.ts.map +1 -1
- package/build/payments/p2ms.js +21 -21
- package/build/payments/p2ms.js.map +1 -1
- package/build/payments/p2op.d.ts +1 -1
- package/build/payments/p2op.d.ts.map +1 -1
- package/build/payments/p2op.js +18 -18
- package/build/payments/p2op.js.map +1 -1
- package/build/payments/p2pk.d.ts +1 -1
- package/build/payments/p2pk.d.ts.map +1 -1
- package/build/payments/p2pk.js +17 -17
- package/build/payments/p2pk.js.map +1 -1
- package/build/payments/p2pkh.d.ts +1 -1
- package/build/payments/p2pkh.d.ts.map +1 -1
- package/build/payments/p2pkh.js +20 -20
- package/build/payments/p2pkh.js.map +1 -1
- package/build/payments/p2sh.d.ts.map +1 -1
- package/build/payments/p2sh.js +22 -20
- package/build/payments/p2sh.js.map +1 -1
- package/build/payments/p2tr.d.ts +2 -2
- package/build/payments/p2tr.d.ts.map +1 -1
- package/build/payments/p2tr.js +25 -26
- package/build/payments/p2tr.js.map +1 -1
- package/build/payments/p2wpkh.d.ts +1 -1
- package/build/payments/p2wpkh.d.ts.map +1 -1
- package/build/payments/p2wpkh.js +20 -20
- package/build/payments/p2wpkh.js.map +1 -1
- package/build/payments/p2wsh.d.ts.map +1 -1
- package/build/payments/p2wsh.js +22 -22
- package/build/payments/p2wsh.js.map +1 -1
- package/build/payments/types.d.ts +1 -1
- package/build/payments/types.d.ts.map +1 -1
- package/build/psbt/PsbtCache.d.ts +54 -0
- package/build/psbt/PsbtCache.d.ts.map +1 -0
- package/build/psbt/PsbtCache.js +249 -0
- package/build/psbt/PsbtCache.js.map +1 -0
- package/build/psbt/PsbtFinalizer.d.ts +21 -0
- package/build/psbt/PsbtFinalizer.d.ts.map +1 -0
- package/build/psbt/PsbtFinalizer.js +157 -0
- package/build/psbt/PsbtFinalizer.js.map +1 -0
- package/build/psbt/PsbtSigner.d.ts +32 -0
- package/build/psbt/PsbtSigner.d.ts.map +1 -0
- package/build/psbt/PsbtSigner.js +192 -0
- package/build/psbt/PsbtSigner.js.map +1 -0
- package/build/psbt/PsbtTransaction.d.ts +25 -0
- package/build/psbt/PsbtTransaction.d.ts.map +1 -0
- package/build/psbt/PsbtTransaction.js +61 -0
- package/build/psbt/PsbtTransaction.js.map +1 -0
- package/build/psbt/bip371.d.ts.map +1 -1
- package/build/psbt/bip371.js +6 -2
- package/build/psbt/bip371.js.map +1 -1
- package/build/psbt/psbtutils.js +1 -1
- package/build/psbt/psbtutils.js.map +1 -1
- package/build/psbt/types.d.ts +5 -71
- package/build/psbt/types.d.ts.map +1 -1
- package/build/psbt/validation.d.ts +1 -1
- package/build/psbt/validation.d.ts.map +1 -1
- package/build/psbt/validation.js +1 -1
- package/build/psbt/validation.js.map +1 -1
- package/build/psbt.d.ts +26 -40
- package/build/psbt.d.ts.map +1 -1
- package/build/psbt.js +180 -808
- package/build/psbt.js.map +1 -1
- package/build/script.d.ts.map +1 -1
- package/build/script.js +4 -4
- package/build/script.js.map +1 -1
- package/build/transaction.d.ts +4 -4
- package/build/transaction.d.ts.map +1 -1
- package/build/transaction.js +6 -5
- package/build/transaction.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/build/types.d.ts +5 -3
- package/build/types.d.ts.map +1 -1
- package/build/types.js +14 -25
- package/build/types.js.map +1 -1
- package/build/workers/WorkerSigningPool.d.ts +24 -17
- package/build/workers/WorkerSigningPool.d.ts.map +1 -1
- package/build/workers/WorkerSigningPool.js +36 -25
- package/build/workers/WorkerSigningPool.js.map +1 -1
- package/build/workers/WorkerSigningPool.node.d.ts +19 -12
- package/build/workers/WorkerSigningPool.node.d.ts.map +1 -1
- package/build/workers/WorkerSigningPool.node.js +60 -28
- package/build/workers/WorkerSigningPool.node.js.map +1 -1
- package/build/workers/WorkerSigningPool.sequential.d.ts +76 -0
- package/build/workers/WorkerSigningPool.sequential.d.ts.map +1 -0
- package/build/workers/WorkerSigningPool.sequential.js +160 -0
- package/build/workers/WorkerSigningPool.sequential.js.map +1 -0
- package/build/workers/WorkerSigningPool.worklet.d.ts +79 -0
- package/build/workers/WorkerSigningPool.worklet.d.ts.map +1 -0
- package/build/workers/WorkerSigningPool.worklet.js +390 -0
- package/build/workers/WorkerSigningPool.worklet.js.map +1 -0
- package/build/workers/index.browser.d.ts +24 -0
- package/build/workers/index.browser.d.ts.map +1 -0
- package/build/workers/index.browser.js +30 -0
- package/build/workers/index.browser.js.map +1 -0
- package/build/workers/index.d.ts +6 -18
- package/build/workers/index.d.ts.map +1 -1
- package/build/workers/index.js +12 -14
- package/build/workers/index.js.map +1 -1
- package/build/workers/index.node.d.ts +38 -0
- package/build/workers/index.node.d.ts.map +1 -0
- package/build/workers/index.node.js +45 -0
- package/build/workers/index.node.js.map +1 -0
- package/build/workers/index.react-native.d.ts +28 -0
- package/build/workers/index.react-native.d.ts.map +1 -0
- package/build/workers/index.react-native.js +67 -0
- package/build/workers/index.react-native.js.map +1 -0
- package/build/workers/index.shared.d.ts +15 -0
- package/build/workers/index.shared.d.ts.map +1 -0
- package/build/workers/index.shared.js +20 -0
- package/build/workers/index.shared.js.map +1 -0
- package/build/workers/psbt-parallel.d.ts +2 -3
- package/build/workers/psbt-parallel.d.ts.map +1 -1
- package/build/workers/psbt-parallel.js +4 -4
- package/build/workers/psbt-parallel.js.map +1 -1
- package/build/workers/types.d.ts +17 -0
- package/build/workers/types.d.ts.map +1 -1
- package/build/workers/types.js.map +1 -1
- package/package.json +48 -9
- package/src/address.ts +53 -21
- package/src/bech32utils.ts +3 -3
- package/src/block.ts +17 -10
- package/src/branded.ts +15 -13
- package/src/crypto.ts +1 -1
- package/src/ecc/context.ts +36 -136
- package/src/ecc/index.ts +2 -2
- package/src/ecc/types.ts +7 -145
- package/src/env.ts +239 -0
- package/src/index.ts +57 -22
- package/src/io/BinaryReader.ts +18 -18
- package/src/io/BinaryWriter.ts +43 -43
- package/src/io/hex.ts +2 -1
- package/src/io/index.ts +0 -3
- package/src/opcodes.ts +21 -4
- package/src/payments/bip341.ts +5 -7
- package/src/payments/embed.ts +19 -19
- package/src/payments/p2ms.ts +34 -27
- package/src/payments/p2op.ts +22 -22
- package/src/payments/p2pk.ts +22 -22
- package/src/payments/p2pkh.ts +28 -28
- package/src/payments/p2sh.ts +33 -30
- package/src/payments/p2tr.ts +40 -40
- package/src/payments/p2wpkh.ts +30 -30
- package/src/payments/p2wsh.ts +29 -29
- package/src/payments/types.ts +1 -1
- package/src/psbt/PsbtCache.ts +325 -0
- package/src/psbt/PsbtFinalizer.ts +213 -0
- package/src/psbt/PsbtSigner.ts +302 -0
- package/src/psbt/PsbtTransaction.ts +82 -0
- package/src/psbt/bip371.ts +7 -3
- package/src/psbt/psbtutils.ts +1 -1
- package/src/psbt/types.ts +5 -94
- package/src/psbt/validation.ts +5 -12
- package/src/psbt.ts +376 -1201
- package/src/script.ts +6 -9
- package/src/transaction.ts +19 -15
- package/src/types.ts +33 -45
- package/src/workers/WorkerSigningPool.node.ts +72 -36
- package/src/workers/WorkerSigningPool.sequential.ts +191 -0
- package/src/workers/WorkerSigningPool.ts +48 -39
- package/src/workers/WorkerSigningPool.worklet.ts +522 -0
- package/src/workers/index.browser.ts +34 -0
- package/src/workers/index.node.ts +50 -0
- package/src/workers/index.react-native.ts +110 -0
- package/src/workers/index.shared.ts +58 -0
- package/src/workers/index.ts +14 -65
- package/src/workers/psbt-parallel.ts +8 -13
- package/src/workers/types.ts +26 -1
- package/test/address.spec.ts +2 -2
- package/test/bitcoin.core.spec.ts +5 -2
- package/test/browser/payments.spec.ts +151 -0
- package/test/browser/psbt.spec.ts +1510 -0
- package/test/browser/script.spec.ts +223 -0
- package/test/browser/setup.ts +13 -0
- package/test/browser/workers-signing.spec.ts +537 -0
- package/test/crypto.spec.ts +2 -2
- package/test/env.spec.ts +418 -0
- package/test/fixtures/core/base58_encode_decode.json +12 -48
- package/test/fixtures/core/base58_keys_invalid.json +50 -150
- package/test/fixtures/core/sighash.json +1 -3
- package/test/fixtures/core/tx_valid.json +133 -501
- package/test/fixtures/embed.json +3 -11
- package/test/fixtures/p2ms.json +21 -91
- package/test/fixtures/p2pk.json +5 -24
- package/test/fixtures/p2pkh.json +7 -36
- package/test/fixtures/p2sh.json +8 -54
- package/test/fixtures/p2tr.json +2 -6
- package/test/fixtures/p2wpkh.json +7 -36
- package/test/fixtures/p2wsh.json +14 -59
- package/test/fixtures/psbt.json +2 -6
- package/test/fixtures/script.json +12 -48
- package/test/integration/addresses.spec.ts +11 -5
- package/test/integration/bip32.spec.ts +1 -1
- package/test/integration/cltv.spec.ts +10 -6
- package/test/integration/csv.spec.ts +10 -9
- package/test/integration/payments.spec.ts +8 -4
- package/test/integration/taproot.spec.ts +26 -6
- package/test/integration/transactions.spec.ts +22 -8
- package/test/payments.spec.ts +1 -1
- package/test/payments.utils.ts +1 -1
- package/test/psbt.spec.ts +250 -64
- package/test/script_signature.spec.ts +1 -1
- package/test/transaction.spec.ts +18 -5
- package/test/tsconfig.json +6 -20
- package/test/workers-pool.spec.ts +65 -23
- package/test/workers-sequential.spec.ts +669 -0
- package/test/workers-signing.spec.ts +7 -3
- package/test/workers-worklet.spec.ts +500 -0
- package/test/workers.spec.ts +6 -7
- package/typedoc.json +39 -0
- package/vite.config.browser.ts +31 -6
- package/vitest.config.browser.ts +68 -0
- package/browser/ecpair.d.ts +0 -99
- package/browser/io/MemoryPool.d.ts +0 -220
- package/browser/io/MemoryPool.d.ts.map +0 -1
- package/build/io/MemoryPool.d.ts +0 -220
- package/build/io/MemoryPool.d.ts.map +0 -1
- package/build/io/MemoryPool.js +0 -309
- package/build/io/MemoryPool.js.map +0 -1
- package/src/ecpair.d.ts +0 -99
- package/src/io/MemoryPool.ts +0 -343
- package/test/taproot-cache.spec.ts +0 -694
|
@@ -31,22 +31,18 @@
|
|
|
31
31
|
*/
|
|
32
32
|
|
|
33
33
|
import type {
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
BatchSigningMessage,
|
|
35
|
+
BatchSigningResultMessage,
|
|
36
|
+
BatchSigningTask,
|
|
36
37
|
ParallelSignerKeyPair,
|
|
37
38
|
ParallelSigningResult,
|
|
39
|
+
PooledWorker,
|
|
38
40
|
SigningResultMessage,
|
|
41
|
+
SigningTask,
|
|
42
|
+
WorkerPoolConfig,
|
|
39
43
|
WorkerResponse,
|
|
40
|
-
BatchSigningMessage,
|
|
41
|
-
BatchSigningTask,
|
|
42
|
-
BatchSigningResultMessage,
|
|
43
|
-
PooledWorker,
|
|
44
|
-
} from './types.js';
|
|
45
|
-
import {
|
|
46
|
-
WorkerState,
|
|
47
|
-
isBatchResult,
|
|
48
|
-
isWorkerReady,
|
|
49
44
|
} from './types.js';
|
|
45
|
+
import { isBatchResult, isWorkerReady, WorkerState } from './types.js';
|
|
50
46
|
import { createWorkerBlobUrl, revokeWorkerBlobUrl } from './signing-worker.js';
|
|
51
47
|
|
|
52
48
|
/**
|
|
@@ -151,34 +147,6 @@ export class WorkerSigningPool {
|
|
|
151
147
|
this.#preserveWorkers = this.#config.preserveWorkers;
|
|
152
148
|
}
|
|
153
149
|
|
|
154
|
-
/**
|
|
155
|
-
* Gets the singleton pool instance.
|
|
156
|
-
*
|
|
157
|
-
* @param config - Optional configuration (only used on first call)
|
|
158
|
-
* @returns The singleton pool instance
|
|
159
|
-
*
|
|
160
|
-
* @example
|
|
161
|
-
* ```typescript
|
|
162
|
-
* const pool = WorkerSigningPool.getInstance({ workerCount: 8 });
|
|
163
|
-
* ```
|
|
164
|
-
*/
|
|
165
|
-
public static getInstance(config?: WorkerPoolConfig): WorkerSigningPool {
|
|
166
|
-
if (!WorkerSigningPool.#instance) {
|
|
167
|
-
WorkerSigningPool.#instance = new WorkerSigningPool(config);
|
|
168
|
-
}
|
|
169
|
-
return WorkerSigningPool.#instance;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* Resets the singleton instance (for testing).
|
|
174
|
-
*/
|
|
175
|
-
public static resetInstance(): void {
|
|
176
|
-
if (WorkerSigningPool.#instance) {
|
|
177
|
-
WorkerSigningPool.#instance.shutdown().catch(() => {});
|
|
178
|
-
WorkerSigningPool.#instance = null;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
150
|
/**
|
|
183
151
|
* Number of workers in the pool.
|
|
184
152
|
*/
|
|
@@ -207,6 +175,34 @@ export class WorkerSigningPool {
|
|
|
207
175
|
return this.#preserveWorkers;
|
|
208
176
|
}
|
|
209
177
|
|
|
178
|
+
/**
|
|
179
|
+
* Gets the singleton pool instance.
|
|
180
|
+
*
|
|
181
|
+
* @param config - Optional configuration (only used on first call)
|
|
182
|
+
* @returns The singleton pool instance
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* const pool = WorkerSigningPool.getInstance({ workerCount: 8 });
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
public static getInstance(config?: WorkerPoolConfig): WorkerSigningPool {
|
|
190
|
+
if (!WorkerSigningPool.#instance) {
|
|
191
|
+
WorkerSigningPool.#instance = new WorkerSigningPool(config);
|
|
192
|
+
}
|
|
193
|
+
return WorkerSigningPool.#instance;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Resets the singleton instance (for testing).
|
|
198
|
+
*/
|
|
199
|
+
public static resetInstance(): void {
|
|
200
|
+
if (WorkerSigningPool.#instance) {
|
|
201
|
+
WorkerSigningPool.#instance.shutdown().catch(() => {});
|
|
202
|
+
WorkerSigningPool.#instance = null;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
210
206
|
/**
|
|
211
207
|
* Enables worker preservation between signing batches.
|
|
212
208
|
*
|
|
@@ -393,6 +389,15 @@ export class WorkerSigningPool {
|
|
|
393
389
|
}
|
|
394
390
|
}
|
|
395
391
|
|
|
392
|
+
/**
|
|
393
|
+
* Disposes of the pool by shutting down all workers.
|
|
394
|
+
*
|
|
395
|
+
* Enables `await using pool = ...` syntax for automatic cleanup.
|
|
396
|
+
*/
|
|
397
|
+
public async [Symbol.asyncDispose](): Promise<void> {
|
|
398
|
+
await this.shutdown();
|
|
399
|
+
}
|
|
400
|
+
|
|
396
401
|
/**
|
|
397
402
|
* Shuts down the pool and terminates all workers.
|
|
398
403
|
*
|
|
@@ -426,6 +431,10 @@ export class WorkerSigningPool {
|
|
|
426
431
|
this.#shuttingDown = false;
|
|
427
432
|
}
|
|
428
433
|
|
|
434
|
+
public [Symbol.dispose](): void {
|
|
435
|
+
void this.shutdown();
|
|
436
|
+
}
|
|
437
|
+
|
|
429
438
|
/**
|
|
430
439
|
* Creates a new worker and adds it to the pool.
|
|
431
440
|
*/
|
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worklet-based parallel signing pool for React Native.
|
|
3
|
+
*
|
|
4
|
+
* Uses `react-native-worklets` (Software Mansion v0.7+) to run signing
|
|
5
|
+
* operations in parallel across multiple worklet runtimes.
|
|
6
|
+
* Each runtime gets its own ECC module instance via eval of the bundled
|
|
7
|
+
* @noble/secp256k1 IIFE string.
|
|
8
|
+
*
|
|
9
|
+
* SECURITY ARCHITECTURE:
|
|
10
|
+
* - Private keys are cloned per-runtime (structuredClone semantics)
|
|
11
|
+
* - Keys are zeroed inside worklet AND in main thread finally block
|
|
12
|
+
* - Tainted runtimes (timeout) are replaced, not reused
|
|
13
|
+
*
|
|
14
|
+
* @packageDocumentation
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type {
|
|
18
|
+
ParallelSignerKeyPair,
|
|
19
|
+
ParallelSigningResult,
|
|
20
|
+
SigningResultMessage,
|
|
21
|
+
SigningTask,
|
|
22
|
+
WorkerPoolConfig,
|
|
23
|
+
} from './types.js';
|
|
24
|
+
import { SignatureType } from './types.js';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Minimal interface for react-native-worklets runtime.
|
|
28
|
+
* Keeps us decoupled from the actual module types.
|
|
29
|
+
*/
|
|
30
|
+
interface WorkletRuntime {
|
|
31
|
+
readonly name: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Minimal interface for the react-native-worklets module.
|
|
36
|
+
*/
|
|
37
|
+
interface WorkletsModule {
|
|
38
|
+
createWorkletRuntime(name: string): WorkletRuntime;
|
|
39
|
+
runOnRuntime<T>(runtime: WorkletRuntime, fn: () => T): Promise<T>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Default configuration for the worklet pool.
|
|
44
|
+
*/
|
|
45
|
+
const DEFAULT_CONFIG: Required<WorkerPoolConfig> = {
|
|
46
|
+
workerCount: 4,
|
|
47
|
+
taskTimeoutMs: 30000,
|
|
48
|
+
maxKeyHoldTimeMs: 5000,
|
|
49
|
+
verifySignatures: true,
|
|
50
|
+
preserveWorkers: false,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Internal runtime wrapper for pool management.
|
|
55
|
+
*/
|
|
56
|
+
interface PooledRuntime {
|
|
57
|
+
readonly id: number;
|
|
58
|
+
runtime: WorkletRuntime;
|
|
59
|
+
tainted: boolean;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Worklet-based parallel signing pool for React Native.
|
|
64
|
+
*
|
|
65
|
+
* Mirrors the API of WorkerSigningPool (browser) but uses
|
|
66
|
+
* `react-native-worklets` runtimes instead of Web Workers.
|
|
67
|
+
* `runOnRuntime()` returns a Promise directly — no postMessage protocol.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* import { WorkletSigningPool } from '@btc-vision/bitcoin/workers';
|
|
72
|
+
*
|
|
73
|
+
* const pool = WorkletSigningPool.getInstance();
|
|
74
|
+
* pool.preserveWorkers();
|
|
75
|
+
*
|
|
76
|
+
* const result = await pool.signBatch(tasks, keyPair);
|
|
77
|
+
* await pool.shutdown();
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export class WorkletSigningPool {
|
|
81
|
+
static #instance: WorkletSigningPool | null = null;
|
|
82
|
+
|
|
83
|
+
readonly #config: Required<WorkerPoolConfig>;
|
|
84
|
+
readonly #runtimes: PooledRuntime[] = [];
|
|
85
|
+
|
|
86
|
+
#workletsModule: WorkletsModule | null = null;
|
|
87
|
+
#eccBundleCode: string | null = null;
|
|
88
|
+
#preserveWorkers: boolean = false;
|
|
89
|
+
#nextRuntimeId: number = 0;
|
|
90
|
+
#initialized: boolean = false;
|
|
91
|
+
#shuttingDown: boolean = false;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Whether Uint8Array survives worklet boundary.
|
|
95
|
+
* Detected during initialize(); if false, we encode as number[].
|
|
96
|
+
*/
|
|
97
|
+
#uint8ArraySupported: boolean = true;
|
|
98
|
+
|
|
99
|
+
private constructor(config: WorkerPoolConfig = {}) {
|
|
100
|
+
this.#config = { ...DEFAULT_CONFIG, ...config };
|
|
101
|
+
this.#preserveWorkers = this.#config.preserveWorkers;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Number of active runtimes. */
|
|
105
|
+
public get workerCount(): number {
|
|
106
|
+
return this.#runtimes.length;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Idle runtimes (all non-tainted). */
|
|
110
|
+
public get idleWorkerCount(): number {
|
|
111
|
+
return this.#runtimes.filter((r) => !r.tainted).length;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Busy runtimes — always 0 outside of signBatch. */
|
|
115
|
+
public get busyWorkerCount(): number {
|
|
116
|
+
return 0;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Whether runtimes are preserved between batches. */
|
|
120
|
+
public get isPreservingWorkers(): boolean {
|
|
121
|
+
return this.#preserveWorkers;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Gets the singleton pool instance.
|
|
126
|
+
*
|
|
127
|
+
* @param config - Optional configuration (only used on first call)
|
|
128
|
+
*/
|
|
129
|
+
public static getInstance(config?: WorkerPoolConfig): WorkletSigningPool {
|
|
130
|
+
if (!WorkletSigningPool.#instance) {
|
|
131
|
+
WorkletSigningPool.#instance = new WorkletSigningPool(config);
|
|
132
|
+
}
|
|
133
|
+
return WorkletSigningPool.#instance;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Resets the singleton instance (for testing). */
|
|
137
|
+
public static resetInstance(): void {
|
|
138
|
+
if (WorkletSigningPool.#instance) {
|
|
139
|
+
WorkletSigningPool.#instance.shutdown().catch(() => {});
|
|
140
|
+
WorkletSigningPool.#instance = null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Enable runtime preservation between signing batches. */
|
|
145
|
+
public preserveWorkers(): void {
|
|
146
|
+
this.#preserveWorkers = true;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Disable runtime preservation. */
|
|
150
|
+
public releaseWorkers(): void {
|
|
151
|
+
this.#preserveWorkers = false;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Initializes the worklet pool.
|
|
156
|
+
*
|
|
157
|
+
* Dynamically imports `react-native-worklets`, creates N runtimes,
|
|
158
|
+
* and injects the ECC bundle into each via eval.
|
|
159
|
+
*
|
|
160
|
+
* @throws If `react-native-worklets` is not installed or eval fails
|
|
161
|
+
*/
|
|
162
|
+
public async initialize(): Promise<void> {
|
|
163
|
+
if (this.#initialized) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (this.#shuttingDown) {
|
|
168
|
+
throw new Error('Cannot initialize pool while shutting down');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// Lazy dynamic import — module loads even without the dep
|
|
172
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
173
|
+
const worklets: WorkletsModule = await import('react-native-worklets' as string);
|
|
174
|
+
this.#workletsModule = worklets;
|
|
175
|
+
|
|
176
|
+
// Load ECC bundle
|
|
177
|
+
const { ECC_BUNDLE } = await import('./ecc-bundle.js');
|
|
178
|
+
this.#eccBundleCode = ECC_BUNDLE;
|
|
179
|
+
|
|
180
|
+
// Create runtimes and inject ECC
|
|
181
|
+
for (let i = 0; i < this.#config.workerCount; i++) {
|
|
182
|
+
await this.#createRuntime();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Feature-detect Uint8Array support across worklet boundary
|
|
186
|
+
const firstRuntime = this.#runtimes[0];
|
|
187
|
+
if (firstRuntime) {
|
|
188
|
+
try {
|
|
189
|
+
const result = await this.#workletsModule.runOnRuntime(
|
|
190
|
+
firstRuntime.runtime,
|
|
191
|
+
() => {
|
|
192
|
+
const arr = new Uint8Array([1, 2, 3]);
|
|
193
|
+
return arr instanceof Uint8Array;
|
|
194
|
+
},
|
|
195
|
+
);
|
|
196
|
+
this.#uint8ArraySupported = result;
|
|
197
|
+
} catch {
|
|
198
|
+
this.#uint8ArraySupported = false;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
this.#initialized = true;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Signs a batch of tasks in parallel across worklet runtimes.
|
|
207
|
+
*
|
|
208
|
+
* SECURITY: Private keys are cloned per-runtime and zeroed both
|
|
209
|
+
* inside the worklet and in the main thread finally block.
|
|
210
|
+
*/
|
|
211
|
+
public async signBatch(
|
|
212
|
+
tasks: readonly SigningTask[],
|
|
213
|
+
keyPair: ParallelSignerKeyPair,
|
|
214
|
+
): Promise<ParallelSigningResult> {
|
|
215
|
+
const startTime = performance.now();
|
|
216
|
+
|
|
217
|
+
if (!this.#initialized) {
|
|
218
|
+
await this.initialize();
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (tasks.length === 0) {
|
|
222
|
+
return {
|
|
223
|
+
success: true,
|
|
224
|
+
signatures: new Map(),
|
|
225
|
+
errors: new Map(),
|
|
226
|
+
durationMs: performance.now() - startTime,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Distribute tasks round-robin
|
|
231
|
+
const runtimeCount = Math.min(this.#runtimes.length, tasks.length);
|
|
232
|
+
const taskBatches: SigningTask[][] = Array.from({ length: runtimeCount }, () => []);
|
|
233
|
+
|
|
234
|
+
for (let i = 0; i < tasks.length; i++) {
|
|
235
|
+
const batch = taskBatches[i % runtimeCount];
|
|
236
|
+
const task = tasks[i];
|
|
237
|
+
if (batch && task) {
|
|
238
|
+
batch.push(task);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const privateKey = keyPair.getPrivateKey();
|
|
243
|
+
|
|
244
|
+
try {
|
|
245
|
+
const batchResults = await Promise.allSettled(
|
|
246
|
+
taskBatches.map((batch, index) =>
|
|
247
|
+
this.#signBatchOnRuntime(
|
|
248
|
+
batch,
|
|
249
|
+
privateKey,
|
|
250
|
+
keyPair.publicKey,
|
|
251
|
+
index,
|
|
252
|
+
),
|
|
253
|
+
),
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
const signatures = new Map<number, SigningResultMessage>();
|
|
257
|
+
const errors = new Map<number, string>();
|
|
258
|
+
|
|
259
|
+
for (let i = 0; i < batchResults.length; i++) {
|
|
260
|
+
const result = batchResults[i];
|
|
261
|
+
if (!result) continue;
|
|
262
|
+
|
|
263
|
+
if (result.status === 'fulfilled') {
|
|
264
|
+
for (const sig of result.value.signatures) {
|
|
265
|
+
signatures.set(sig.inputIndex, sig);
|
|
266
|
+
}
|
|
267
|
+
for (const [idx, errMsg] of result.value.errors) {
|
|
268
|
+
errors.set(idx, errMsg);
|
|
269
|
+
}
|
|
270
|
+
} else {
|
|
271
|
+
const reason = result.reason as { message?: string } | undefined;
|
|
272
|
+
const errorMsg = reason?.message ?? 'Worklet batch signing failed';
|
|
273
|
+
const failedBatch = taskBatches[i];
|
|
274
|
+
if (failedBatch) {
|
|
275
|
+
for (const task of failedBatch) {
|
|
276
|
+
errors.set(task.inputIndex, errorMsg);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Cleanup runtimes if not preserving
|
|
283
|
+
if (!this.#preserveWorkers) {
|
|
284
|
+
await this.shutdown();
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return {
|
|
288
|
+
success: errors.size === 0,
|
|
289
|
+
signatures,
|
|
290
|
+
errors,
|
|
291
|
+
durationMs: performance.now() - startTime,
|
|
292
|
+
};
|
|
293
|
+
} finally {
|
|
294
|
+
// SECURITY: Zero the key in main thread
|
|
295
|
+
privateKey.fill(0);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Shuts down all runtimes. */
|
|
300
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
301
|
+
public async shutdown(): Promise<void> {
|
|
302
|
+
if (this.#shuttingDown) {
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
this.#shuttingDown = true;
|
|
307
|
+
|
|
308
|
+
// Worklet runtimes don't have a destroy() — clear references for GC
|
|
309
|
+
this.#runtimes.length = 0;
|
|
310
|
+
this.#workletsModule = null;
|
|
311
|
+
this.#eccBundleCode = null;
|
|
312
|
+
this.#initialized = false;
|
|
313
|
+
this.#shuttingDown = false;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
public [Symbol.dispose](): void {
|
|
317
|
+
void this.shutdown();
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
public async [Symbol.asyncDispose](): Promise<void> {
|
|
321
|
+
await this.shutdown();
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Creates a new worklet runtime and injects the ECC bundle.
|
|
326
|
+
*/
|
|
327
|
+
async #createRuntime(): Promise<PooledRuntime> {
|
|
328
|
+
if (!this.#workletsModule || !this.#eccBundleCode) {
|
|
329
|
+
throw new Error('Worklets module or ECC bundle not loaded');
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
const id = this.#nextRuntimeId++;
|
|
333
|
+
const runtime = this.#workletsModule.createWorkletRuntime(`signing-runtime-${id}`);
|
|
334
|
+
|
|
335
|
+
// Inject ECC bundle into the worklet runtime.
|
|
336
|
+
// The bundle declares `var nobleBundle = (()=>{...})()` with "use strict",
|
|
337
|
+
// so a plain eval won't leak it to globalThis. We use `new Function` to
|
|
338
|
+
// execute the bundle and then explicitly assign the result.
|
|
339
|
+
const bundleCode = this.#eccBundleCode;
|
|
340
|
+
await this.#workletsModule.runOnRuntime(runtime, () => {
|
|
341
|
+
'worklet';
|
|
342
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
343
|
+
const fn = new Function(bundleCode + '; return nobleBundle;') as () => unknown;
|
|
344
|
+
(globalThis as Record<string, unknown>)['nobleBundle'] = fn();
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
const pooled: PooledRuntime = { id, runtime, tainted: false };
|
|
348
|
+
this.#runtimes.push(pooled);
|
|
349
|
+
return pooled;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Replaces a tainted runtime with a fresh one.
|
|
354
|
+
*/
|
|
355
|
+
async #replaceRuntime(pooled: PooledRuntime): Promise<void> {
|
|
356
|
+
const idx = this.#runtimes.indexOf(pooled);
|
|
357
|
+
if (idx >= 0) {
|
|
358
|
+
this.#runtimes.splice(idx, 1);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
try {
|
|
362
|
+
await this.#createRuntime();
|
|
363
|
+
} catch {
|
|
364
|
+
// If replacement fails, pool continues with fewer runtimes
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Signs a batch of tasks on a specific runtime.
|
|
370
|
+
*/
|
|
371
|
+
async #signBatchOnRuntime(
|
|
372
|
+
tasks: readonly SigningTask[],
|
|
373
|
+
privateKey: Uint8Array,
|
|
374
|
+
publicKey: Uint8Array,
|
|
375
|
+
runtimeIndex: number,
|
|
376
|
+
): Promise<{
|
|
377
|
+
signatures: SigningResultMessage[];
|
|
378
|
+
errors: Map<number, string>;
|
|
379
|
+
}> {
|
|
380
|
+
if (tasks.length === 0) {
|
|
381
|
+
return { signatures: [], errors: new Map() };
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const pooled = this.#runtimes[runtimeIndex];
|
|
385
|
+
if (!pooled || pooled.tainted) {
|
|
386
|
+
throw new Error(`Runtime ${runtimeIndex} unavailable or tainted`);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (!this.#workletsModule) {
|
|
390
|
+
throw new Error('Worklets module not loaded');
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// Prepare data for worklet transfer
|
|
394
|
+
const useArrayEncoding = !this.#uint8ArraySupported;
|
|
395
|
+
const keyData: number[] | Uint8Array = useArrayEncoding
|
|
396
|
+
? Array.from(privateKey)
|
|
397
|
+
: new Uint8Array(privateKey);
|
|
398
|
+
|
|
399
|
+
const taskData = tasks.map((t) => ({
|
|
400
|
+
taskId: t.taskId,
|
|
401
|
+
inputIndex: t.inputIndex,
|
|
402
|
+
hash: useArrayEncoding ? Array.from(t.hash) : new Uint8Array(t.hash),
|
|
403
|
+
signatureType: t.signatureType,
|
|
404
|
+
lowR: t.lowR,
|
|
405
|
+
sighashType: t.sighashType,
|
|
406
|
+
leafHash: t.leafHash
|
|
407
|
+
? (useArrayEncoding ? Array.from(t.leafHash) : new Uint8Array(t.leafHash))
|
|
408
|
+
: undefined,
|
|
409
|
+
}));
|
|
410
|
+
|
|
411
|
+
const pubKeyData: number[] | Uint8Array = useArrayEncoding
|
|
412
|
+
? Array.from(publicKey)
|
|
413
|
+
: new Uint8Array(publicKey);
|
|
414
|
+
|
|
415
|
+
// Dispatch to worklet runtime with timeout
|
|
416
|
+
const signingPromise = this.#workletsModule.runOnRuntime(
|
|
417
|
+
pooled.runtime,
|
|
418
|
+
() => {
|
|
419
|
+
'worklet';
|
|
420
|
+
// Inside the worklet runtime, nobleBundle is available from init eval
|
|
421
|
+
// The bundled secp.sign() returns a compact 64-byte Uint8Array directly
|
|
422
|
+
// (not a Signature object). secp.schnorr.sign() also returns Uint8Array.
|
|
423
|
+
const eccModule = (globalThis as Record<string, unknown>)['nobleBundle'] as {
|
|
424
|
+
secp: {
|
|
425
|
+
sign(
|
|
426
|
+
hash: Uint8Array,
|
|
427
|
+
key: Uint8Array,
|
|
428
|
+
opts?: { lowS: boolean; prehash: boolean },
|
|
429
|
+
): Uint8Array;
|
|
430
|
+
schnorr: {
|
|
431
|
+
sign(hash: Uint8Array, key: Uint8Array): Uint8Array;
|
|
432
|
+
};
|
|
433
|
+
};
|
|
434
|
+
};
|
|
435
|
+
|
|
436
|
+
const toU8 = (data: number[] | Uint8Array): Uint8Array =>
|
|
437
|
+
data instanceof Uint8Array ? data : new Uint8Array(data);
|
|
438
|
+
|
|
439
|
+
const privKey = toU8(keyData);
|
|
440
|
+
const results: Array<{
|
|
441
|
+
type: 'result';
|
|
442
|
+
taskId: string;
|
|
443
|
+
signature: number[];
|
|
444
|
+
inputIndex: number;
|
|
445
|
+
publicKey: number[];
|
|
446
|
+
signatureType: number;
|
|
447
|
+
leafHash?: number[];
|
|
448
|
+
}> = [];
|
|
449
|
+
const errors: Array<{ inputIndex: number; error: string }> = [];
|
|
450
|
+
|
|
451
|
+
try {
|
|
452
|
+
for (const task of taskData) {
|
|
453
|
+
try {
|
|
454
|
+
const hash = toU8(task.hash);
|
|
455
|
+
let signature: Uint8Array;
|
|
456
|
+
|
|
457
|
+
if (task.signatureType === 1) {
|
|
458
|
+
signature = eccModule.secp.schnorr.sign(hash, privKey);
|
|
459
|
+
} else {
|
|
460
|
+
// prehash: false — input is already a 32-byte hash
|
|
461
|
+
signature = eccModule.secp.sign(hash, privKey, {
|
|
462
|
+
lowS: true,
|
|
463
|
+
prehash: false,
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
const entry: (typeof results)[number] = {
|
|
468
|
+
type: 'result' as const,
|
|
469
|
+
taskId: task.taskId,
|
|
470
|
+
signature: Array.from(signature),
|
|
471
|
+
inputIndex: task.inputIndex,
|
|
472
|
+
publicKey: Array.from(toU8(pubKeyData)),
|
|
473
|
+
signatureType: task.signatureType,
|
|
474
|
+
};
|
|
475
|
+
if (task.leafHash) {
|
|
476
|
+
entry.leafHash = Array.from(toU8(task.leafHash));
|
|
477
|
+
}
|
|
478
|
+
results.push(entry);
|
|
479
|
+
} catch (err: unknown) {
|
|
480
|
+
const msg = err instanceof Error ? err.message : 'Signing failed';
|
|
481
|
+
errors.push({ inputIndex: task.inputIndex, error: msg });
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
} finally {
|
|
485
|
+
// SECURITY: Zero key inside worklet
|
|
486
|
+
privKey.fill(0);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
return { results, errors };
|
|
490
|
+
},
|
|
491
|
+
);
|
|
492
|
+
|
|
493
|
+
// Timeout guard
|
|
494
|
+
const timeoutPromise = new Promise<never>((_resolve, reject) => {
|
|
495
|
+
setTimeout(() => {
|
|
496
|
+
pooled.tainted = true;
|
|
497
|
+
this.#replaceRuntime(pooled).catch(() => {});
|
|
498
|
+
reject(new Error(`Worklet signing timeout for ${tasks.length} tasks`));
|
|
499
|
+
}, this.#config.maxKeyHoldTimeMs);
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
const raw = await Promise.race([signingPromise, timeoutPromise]);
|
|
503
|
+
|
|
504
|
+
// Convert results back to proper types
|
|
505
|
+
const signatures: SigningResultMessage[] = raw.results.map((r) => ({
|
|
506
|
+
type: 'result' as const,
|
|
507
|
+
taskId: r.taskId,
|
|
508
|
+
signature: new Uint8Array(r.signature),
|
|
509
|
+
inputIndex: r.inputIndex,
|
|
510
|
+
publicKey: new Uint8Array(r.publicKey),
|
|
511
|
+
signatureType: r.signatureType as SignatureType,
|
|
512
|
+
leafHash: r.leafHash ? new Uint8Array(r.leafHash) : undefined,
|
|
513
|
+
}));
|
|
514
|
+
|
|
515
|
+
const errors = new Map<number, string>();
|
|
516
|
+
for (const e of raw.errors) {
|
|
517
|
+
errors.set(e.inputIndex, e.error);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
return { signatures, errors };
|
|
521
|
+
}
|
|
522
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser worker pool entry point.
|
|
3
|
+
*
|
|
4
|
+
* Uses Web Workers for parallel signing. Never references Node.js modules
|
|
5
|
+
* (worker_threads, os, etc.), so it is safe for browser bundlers.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { WorkerPoolConfig, SigningPoolLike } from './types.js';
|
|
11
|
+
|
|
12
|
+
export * from './index.shared.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Detects the runtime environment.
|
|
16
|
+
*
|
|
17
|
+
* @returns Always 'browser' in this entry point.
|
|
18
|
+
*/
|
|
19
|
+
export function detectRuntime(): 'node' | 'browser' | 'react-native' | 'unknown' {
|
|
20
|
+
return 'browser';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Creates a signing pool for the browser runtime using Web Workers.
|
|
25
|
+
*
|
|
26
|
+
* @param config - Optional pool configuration
|
|
27
|
+
* @returns A promise resolving to the initialized signing pool
|
|
28
|
+
*/
|
|
29
|
+
export async function createSigningPool(config?: WorkerPoolConfig): Promise<SigningPoolLike> {
|
|
30
|
+
const { WorkerSigningPool } = await import('./WorkerSigningPool.js');
|
|
31
|
+
const pool = WorkerSigningPool.getInstance(config);
|
|
32
|
+
await pool.initialize();
|
|
33
|
+
return pool;
|
|
34
|
+
}
|