@btc-vision/bitcoin 7.0.0-alpha.3 → 7.0.0-alpha.5
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 +139 -12
- package/browser/chunks/index.browser-CUaPaKyS.js +10742 -0
- package/browser/env.d.ts +13 -0
- package/browser/env.d.ts.map +1 -0
- package/browser/index.d.ts +1 -1
- package/browser/index.d.ts.map +1 -1
- package/browser/index.js +1894 -12851
- package/browser/io/index.d.ts +0 -1
- package/browser/io/index.d.ts.map +1 -1
- package/browser/types.d.ts.map +1 -1
- package/browser/workers/WorkerSigningPool.d.ts +6 -0
- package/browser/workers/WorkerSigningPool.d.ts.map +1 -1
- package/browser/workers/WorkerSigningPool.node.d.ts +6 -0
- package/browser/workers/WorkerSigningPool.node.d.ts.map +1 -1
- package/browser/workers/WorkerSigningPool.sequential.d.ts +69 -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 -17
- package/browser/workers/index.d.ts.map +1 -1
- package/browser/workers/index.js +21 -0
- package/browser/workers/index.node.d.ts +12 -19
- package/browser/workers/index.node.d.ts.map +1 -1
- 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/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 +2 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2 -1
- package/build/index.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/tsconfig.build.tsbuildinfo +1 -1
- package/build/types.d.ts.map +1 -1
- package/build/types.js +2 -16
- package/build/types.js.map +1 -1
- package/build/workers/WorkerSigningPool.d.ts +6 -0
- package/build/workers/WorkerSigningPool.d.ts.map +1 -1
- package/build/workers/WorkerSigningPool.js +8 -0
- package/build/workers/WorkerSigningPool.js.map +1 -1
- package/build/workers/WorkerSigningPool.node.d.ts +6 -0
- package/build/workers/WorkerSigningPool.node.d.ts.map +1 -1
- package/build/workers/WorkerSigningPool.node.js +9 -2
- package/build/workers/WorkerSigningPool.node.js.map +1 -1
- package/build/workers/WorkerSigningPool.sequential.d.ts +78 -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 +388 -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 -11
- package/build/workers/index.js.map +1 -1
- package/build/workers/index.node.d.ts +17 -3
- package/build/workers/index.node.d.ts.map +1 -1
- package/build/workers/index.node.js +23 -4
- package/build/workers/index.node.js.map +1 -1
- 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/package.json +19 -1
- package/src/env.ts +237 -0
- package/src/index.ts +1 -2
- package/src/io/index.ts +0 -3
- package/src/types.ts +4 -27
- package/src/workers/WorkerSigningPool.node.ts +10 -2
- package/src/workers/WorkerSigningPool.sequential.ts +190 -0
- package/src/workers/WorkerSigningPool.ts +9 -0
- package/src/workers/WorkerSigningPool.worklet.ts +519 -0
- package/src/workers/index.browser.ts +34 -0
- package/src/workers/index.node.ts +28 -5
- package/src/workers/index.react-native.ts +110 -0
- package/src/workers/index.shared.ts +58 -0
- package/src/workers/index.ts +15 -64
- package/src/workers/psbt-parallel.ts +8 -8
- package/src/workers/types.ts +21 -0
- package/test/env.spec.ts +418 -0
- package/test/workers-pool.spec.ts +43 -0
- package/test/workers-sequential.spec.ts +669 -0
- package/test/workers-worklet.spec.ts +500 -0
- package/vite.config.browser.ts +31 -6
- 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/io/MemoryPool.ts +0 -343
package/README.md
CHANGED
|
@@ -43,13 +43,25 @@ Requires Node.js >= 24.0.0.
|
|
|
43
43
|
### Initialize the ECC Library
|
|
44
44
|
|
|
45
45
|
The ECC library must be initialized before using Taproot, signing, or any elliptic curve operations.
|
|
46
|
+
Two backends are available:
|
|
47
|
+
|
|
48
|
+
**Noble (recommended for browsers)** -- pure JS, no WASM dependency:
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
import { initEccLib } from '@btc-vision/bitcoin';
|
|
52
|
+
import { createNobleBackend } from '@btc-vision/ecpair';
|
|
53
|
+
|
|
54
|
+
initEccLib(createNobleBackend());
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**tiny-secp256k1** -- WASM-based, faster in Node.js:
|
|
46
58
|
|
|
47
59
|
```typescript
|
|
48
60
|
import { initEccLib } from '@btc-vision/bitcoin';
|
|
49
|
-
import
|
|
50
|
-
import * as
|
|
61
|
+
import { createLegacyBackend } from '@btc-vision/ecpair';
|
|
62
|
+
import * as tinysecp from 'tiny-secp256k1';
|
|
51
63
|
|
|
52
|
-
initEccLib(
|
|
64
|
+
initEccLib(createLegacyBackend(tinysecp));
|
|
53
65
|
```
|
|
54
66
|
|
|
55
67
|
### Create a Key Pair
|
|
@@ -172,15 +184,56 @@ const bech32Addr = address.toBech32(decoded.data, decoded.version, 'bc');
|
|
|
172
184
|
|
|
173
185
|
### Parallel Signing with Workers
|
|
174
186
|
|
|
175
|
-
For high-throughput signing across many inputs, use the worker pool
|
|
187
|
+
For high-throughput signing across many inputs, use the worker pool.
|
|
188
|
+
|
|
189
|
+
#### Sign a PSBT in parallel
|
|
190
|
+
|
|
191
|
+
`signPsbtParallel` analyzes inputs, distributes signing across workers, and applies signatures back to the PSBT:
|
|
176
192
|
|
|
177
193
|
```typescript
|
|
178
|
-
import {
|
|
194
|
+
import { Psbt, networks } from '@btc-vision/bitcoin';
|
|
195
|
+
import { signPsbtParallel, createSigningPool } from '@btc-vision/bitcoin/workers';
|
|
179
196
|
|
|
180
197
|
// Create a platform-appropriate pool (Node.js worker_threads or Web Workers)
|
|
181
198
|
const pool = await createSigningPool({ workerCount: 4 });
|
|
182
199
|
pool.preserveWorkers();
|
|
183
200
|
|
|
201
|
+
// Build your PSBT as usual
|
|
202
|
+
const psbt = new Psbt({ network: networks.bitcoin });
|
|
203
|
+
psbt.addInput({ /* ... */ });
|
|
204
|
+
psbt.addInput({ /* ... */ });
|
|
205
|
+
psbt.addOutput({ /* ... */ });
|
|
206
|
+
|
|
207
|
+
// Sign all signable inputs in parallel — replaces signAllInputs / signAllInputsAsync
|
|
208
|
+
const result = await signPsbtParallel(psbt, keyPair, pool);
|
|
209
|
+
|
|
210
|
+
if (result.success) {
|
|
211
|
+
console.log(`Signed ${result.signatures.size} inputs in ${result.durationMs}ms`);
|
|
212
|
+
psbt.finalizeAllInputs();
|
|
213
|
+
const tx = psbt.extractTransaction();
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Shut down when done (or keep the pool alive for future PSBTs)
|
|
217
|
+
await pool.shutdown();
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
You can also pass a config object instead of a pool instance. A temporary pool will be created and
|
|
221
|
+
destroyed automatically:
|
|
222
|
+
|
|
223
|
+
```typescript
|
|
224
|
+
const result = await signPsbtParallel(psbt, keyPair, { workerCount: 4 });
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
#### Low-level batch signing
|
|
228
|
+
|
|
229
|
+
For manual control over sighash computation and task construction:
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
import { createSigningPool, SignatureType } from '@btc-vision/bitcoin/workers';
|
|
233
|
+
|
|
234
|
+
const pool = await createSigningPool({ workerCount: 4 });
|
|
235
|
+
pool.preserveWorkers();
|
|
236
|
+
|
|
184
237
|
const tasks = [
|
|
185
238
|
{
|
|
186
239
|
taskId: 'input-0',
|
|
@@ -203,10 +256,6 @@ if (result.success) {
|
|
|
203
256
|
console.log(`Signed ${result.signatures.size} inputs in ${result.durationMs}ms`);
|
|
204
257
|
}
|
|
205
258
|
|
|
206
|
-
// Or sign a PSBT directly in parallel
|
|
207
|
-
import { signPsbtParallel } from '@btc-vision/bitcoin/workers';
|
|
208
|
-
await signPsbtParallel(psbt, keyPair, pool);
|
|
209
|
-
|
|
210
259
|
await pool.shutdown();
|
|
211
260
|
```
|
|
212
261
|
|
|
@@ -340,10 +389,88 @@ const t = taggedHash('TapLeaf', data); // BIP340 tagged hash
|
|
|
340
389
|
|
|
341
390
|
The library ships with a browser-optimized build via the `browser` conditional export. Bundlers that support the `exports` field in `package.json` (Vite, Webpack 5+, esbuild) will automatically resolve to the browser build.
|
|
342
391
|
|
|
343
|
-
For
|
|
392
|
+
For browser environments, use `createNobleBackend()` -- it is pure JavaScript with no WASM dependency. The `tiny-secp256k1` backend requires WebAssembly support and is better suited for Node.js.
|
|
393
|
+
|
|
394
|
+
## React Native
|
|
395
|
+
|
|
396
|
+
The library works in React Native with Hermes 0.76+ (BigInt and `crypto.getRandomValues()` required). The core library (PSBT, transactions, addresses, crypto) is pure JS and runs on the main thread. Parallel signing uses `react-native-worklets` when available, otherwise falls back to sequential execution automatically.
|
|
397
|
+
|
|
398
|
+
### Metro Configuration
|
|
399
|
+
|
|
400
|
+
Add `react-native` to the condition names in your `metro.config.js`:
|
|
401
|
+
|
|
402
|
+
```javascript
|
|
403
|
+
const config = {
|
|
404
|
+
resolver: {
|
|
405
|
+
unstable_conditionNames: ['react-native', 'import', 'default'],
|
|
406
|
+
},
|
|
407
|
+
};
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
### Usage
|
|
411
|
+
|
|
412
|
+
Usage is identical to Node.js and browsers:
|
|
413
|
+
|
|
414
|
+
```typescript
|
|
415
|
+
import { initEccLib, Psbt, networks } from '@btc-vision/bitcoin';
|
|
416
|
+
import { createNobleBackend, ECPairSigner } from '@btc-vision/ecpair';
|
|
417
|
+
|
|
418
|
+
// Initialize ECC (pure JS Noble backend — no WASM needed)
|
|
419
|
+
const backend = createNobleBackend();
|
|
420
|
+
initEccLib(backend);
|
|
421
|
+
|
|
422
|
+
// Create key pair, build PSBT, sign — same API as Node.js/browser
|
|
423
|
+
const keyPair = ECPairSigner.fromWIF(backend, wif, networks.bitcoin);
|
|
424
|
+
const psbt = new Psbt({ network: networks.bitcoin });
|
|
425
|
+
// ... add inputs/outputs ...
|
|
426
|
+
psbt.signAllInputs(keyPair);
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
### Parallel Signing
|
|
430
|
+
|
|
431
|
+
`signPsbtParallel()` and `createSigningPool()` support true parallel signing in React Native via [`react-native-worklets`](https://github.com/margelo/react-native-worklets) (Software Mansion, v0.7+). Each worklet runtime gets its own ECC module instance and signing tasks are distributed round-robin across runtimes.
|
|
432
|
+
|
|
433
|
+
Install the optional peer dependency to enable parallel signing:
|
|
434
|
+
|
|
435
|
+
```bash
|
|
436
|
+
npm install react-native-worklets
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
Usage is identical to Node.js/browser — `createSigningPool()` detects worklets automatically:
|
|
440
|
+
|
|
441
|
+
```typescript
|
|
442
|
+
import { signPsbtParallel, createSigningPool } from '@btc-vision/bitcoin/workers';
|
|
443
|
+
|
|
444
|
+
// Automatically uses WorkletSigningPool if react-native-worklets is installed,
|
|
445
|
+
// otherwise falls back to SequentialSigningPool (main-thread, one-by-one)
|
|
446
|
+
const pool = await createSigningPool({ workerCount: 4 });
|
|
447
|
+
pool.preserveWorkers();
|
|
448
|
+
|
|
449
|
+
const result = await signPsbtParallel(psbt, keyPair, pool);
|
|
450
|
+
await pool.shutdown();
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
You can also import `WorkletSigningPool` directly for explicit control:
|
|
454
|
+
|
|
455
|
+
```typescript
|
|
456
|
+
import { WorkletSigningPool } from '@btc-vision/bitcoin/workers';
|
|
457
|
+
|
|
458
|
+
const pool = WorkletSigningPool.getInstance({ workerCount: 4 });
|
|
459
|
+
pool.preserveWorkers();
|
|
460
|
+
await pool.initialize();
|
|
461
|
+
|
|
462
|
+
const result = await pool.signBatch(tasks, keyPair);
|
|
463
|
+
await pool.shutdown();
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
If `react-native-worklets` is not installed, `createSigningPool()` returns a `SequentialSigningPool` that signs inputs one-by-one on the main thread using the same API.
|
|
467
|
+
|
|
468
|
+
### Requirements
|
|
344
469
|
|
|
345
|
-
-
|
|
346
|
-
-
|
|
470
|
+
- **Hermes 0.76+** (React Native 0.76+) for BigInt and `crypto.getRandomValues()` support
|
|
471
|
+
- **`react-native-worklets` >= 0.7.0** (optional) for parallel signing across worklet runtimes
|
|
472
|
+
- `react-native-quick-crypto` is **not** required — the core library uses `@noble/hashes` and `@noble/curves` (pure JS)
|
|
473
|
+
- A future `@btc-vision/react-native-secp256k1` Nitro Module would provide native C++ performance via `initEccLib(createNativeBackend())`
|
|
347
474
|
|
|
348
475
|
## Running Tests
|
|
349
476
|
|