@btc-vision/bitcoin 7.0.0-alpha.2 → 7.0.0-alpha.3
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 +334 -161
- package/browser/ecc/context.d.ts +22 -21
- 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/index.d.ts +2 -2
- package/browser/index.d.ts.map +1 -1
- package/browser/index.js +5790 -4062
- package/browser/payments/p2tr.d.ts.map +1 -1
- package/browser/psbt/types.d.ts +2 -68
- package/browser/psbt/types.d.ts.map +1 -1
- package/browser/psbt.d.ts +9 -11
- package/browser/psbt.d.ts.map +1 -1
- package/browser/types.d.ts +1 -1
- package/browser/types.d.ts.map +1 -1
- package/build/ecc/context.d.ts +22 -21
- package/build/ecc/context.d.ts.map +1 -1
- package/build/ecc/context.js +19 -114
- 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/index.d.ts +2 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js.map +1 -1
- package/build/payments/p2tr.d.ts.map +1 -1
- package/build/payments/p2tr.js +2 -3
- package/build/payments/p2tr.js.map +1 -1
- package/build/psbt/types.d.ts +2 -68
- package/build/psbt/types.d.ts.map +1 -1
- package/build/psbt.d.ts +9 -11
- package/build/psbt.d.ts.map +1 -1
- package/build/psbt.js +38 -53
- package/build/psbt.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/build/types.d.ts +1 -1
- package/build/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/ecc/context.ts +26 -147
- package/src/ecc/index.ts +2 -2
- package/src/ecc/types.ts +7 -138
- package/src/index.ts +1 -2
- package/src/payments/p2tr.ts +2 -2
- package/src/psbt/types.ts +2 -84
- package/src/psbt.ts +63 -121
- package/src/types.ts +1 -1
package/README.md
CHANGED
|
@@ -1,201 +1,374 @@
|
|
|
1
|
-
#
|
|
2
|
-
[](https://github.com/bitcoinjs/bitcoinjs-lib/actions/workflows/main_ci.yml) [](https://www.npmjs.org/package/bitcoinjs-lib) [](https://github.com/prettier/prettier)
|
|
1
|
+
# @btc-vision/bitcoin
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
[](https://github.com/prettier/prettier)
|
|
7
9
|
|
|
8
|
-
##
|
|
9
|
-
If you are thinking of using the *master* branch of this library in production, **stop**.
|
|
10
|
-
Master is not stable; it is our development branch, and [only tagged releases may be classified as stable](https://github.com/bitcoinjs/bitcoinjs-lib/tags).
|
|
10
|
+
## Overview
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
> Don't trust. Verify.
|
|
12
|
+
A client-side Bitcoin library for Node.js and browsers, written in TypeScript. Provides low-level transaction handling, PSBT (Partially Signed Bitcoin Transactions), address encoding/decoding, payment script creation, and cryptographic operations across multiple networks.
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
This is a modernized fork of [bitcoinjs-lib](https://github.com/bitcoinjs/bitcoinjs-lib) with significant API changes:
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
- **Branded types** (`Bytes32`, `PrivateKey`, `PublicKey`, `Satoshi`, etc.) for compile-time safety
|
|
17
|
+
- **Modular PSBT architecture** split into composable classes (`PsbtCache`, `PsbtSigner`, `PsbtFinalizer`, `PsbtTransaction`)
|
|
18
|
+
- **Worker-based parallel signing** for both Node.js (`worker_threads`) and browsers (Web Workers)
|
|
19
|
+
- **Native `Uint8Array`** throughout (no Node.js `Buffer` dependency)
|
|
20
|
+
- **`bigint` for satoshi values** instead of `number` to prevent precision loss
|
|
21
|
+
- **Structured error hierarchy** with typed error classes
|
|
22
|
+
- **Granular sub-path exports** for tree-shaking
|
|
23
|
+
- **Multi-chain support** including Bitcoin, Litecoin, Dogecoin, Bitcoin Cash, and Dash
|
|
18
24
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
-
|
|
22
|
-
- Standardized, using [prettier](https://github.com/prettier/prettier) and Node `Buffer`'s throughout, and
|
|
23
|
-
- Friendly, with a strong and helpful community, ready to answer questions.
|
|
25
|
+
> **Breaking Changes from bitcoinjs-lib**
|
|
26
|
+
>
|
|
27
|
+
> This library has undergone massive API-breaking changes. Transaction values use `bigint` (as `Satoshi`), all byte buffers are `Uint8Array` with branded type wrappers, the ECC library must be explicitly initialized, and key management has been moved to [`@btc-vision/ecpair`](https://github.com/btc-vision/ecpair) and [`@btc-vision/bip32`](https://github.com/btc-vision/bip32).
|
|
24
28
|
|
|
25
|
-
##
|
|
26
|
-
Visit our [documentation](https://bitcoinjs.github.io/bitcoinjs-lib/) to explore the available resources. We're continually enhancing our documentation with additional features for an enriched experience. If you need further guidance beyond what our [examples](#examples) offer, don't hesitate to [ask for help](https://github.com/bitcoinjs/bitcoinjs-lib/issues/new). We're here to assist you.
|
|
29
|
+
## Installation
|
|
27
30
|
|
|
28
|
-
|
|
31
|
+
```bash
|
|
32
|
+
npm install @btc-vision/bitcoin
|
|
33
|
+
# Key management libraries (separate packages)
|
|
34
|
+
npm install @btc-vision/ecpair @btc-vision/bip32
|
|
35
|
+
# ECC backend
|
|
36
|
+
npm install tiny-secp256k1
|
|
37
|
+
```
|
|
29
38
|
|
|
30
|
-
|
|
31
|
-
**Most of the time, this is not appropriate. Creating issues and pull requests in the open will help others with similar issues, so please try to use public issues and pull requests for communication.**
|
|
39
|
+
Requires Node.js >= 24.0.0.
|
|
32
40
|
|
|
33
|
-
|
|
41
|
+
## Quick Start
|
|
34
42
|
|
|
35
|
-
|
|
43
|
+
### Initialize the ECC Library
|
|
36
44
|
|
|
37
|
-
|
|
45
|
+
The ECC library must be initialized before using Taproot, signing, or any elliptic curve operations.
|
|
38
46
|
|
|
39
|
-
|
|
47
|
+
```typescript
|
|
48
|
+
import { initEccLib } from '@btc-vision/bitcoin';
|
|
49
|
+
import type { EccLib } from '@btc-vision/bitcoin';
|
|
50
|
+
import * as ecc from 'tiny-secp256k1';
|
|
40
51
|
|
|
41
|
-
|
|
52
|
+
initEccLib(ecc as unknown as EccLib);
|
|
53
|
+
```
|
|
42
54
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
55
|
+
### Create a Key Pair
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { ECPairSigner, createNobleBackend } from '@btc-vision/ecpair';
|
|
59
|
+
import { networks } from '@btc-vision/bitcoin';
|
|
60
|
+
|
|
61
|
+
const backend = createNobleBackend();
|
|
62
|
+
|
|
63
|
+
// Random key pair
|
|
64
|
+
const keyPair = ECPairSigner.makeRandom(backend, networks.bitcoin);
|
|
65
|
+
|
|
66
|
+
// From WIF
|
|
67
|
+
const imported = ECPairSigner.fromWIF(
|
|
68
|
+
backend,
|
|
69
|
+
'L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr',
|
|
70
|
+
networks.bitcoin,
|
|
71
|
+
);
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Generate Addresses
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { payments, networks } from '@btc-vision/bitcoin';
|
|
78
|
+
|
|
79
|
+
// P2PKH (Legacy)
|
|
80
|
+
const { address: legacy } = payments.p2pkh({ pubkey: keyPair.publicKey });
|
|
81
|
+
|
|
82
|
+
// P2WPKH (Native SegWit)
|
|
83
|
+
const { address: segwit } = payments.p2wpkh({ pubkey: keyPair.publicKey });
|
|
84
|
+
|
|
85
|
+
// P2TR (Taproot) - requires ECC initialization
|
|
86
|
+
import { toXOnly } from '@btc-vision/bitcoin';
|
|
87
|
+
const { address: taproot } = payments.p2tr({
|
|
88
|
+
internalPubkey: toXOnly(keyPair.publicKey),
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// P2SH-P2WPKH (Wrapped SegWit)
|
|
92
|
+
const { address: wrapped } = payments.p2sh({
|
|
93
|
+
redeem: payments.p2wpkh({ pubkey: keyPair.publicKey }),
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// P2SH Multisig (2-of-3)
|
|
97
|
+
const { address: multisig } = payments.p2sh({
|
|
98
|
+
redeem: payments.p2ms({ m: 2, pubkeys: [pubkey1, pubkey2, pubkey3] }),
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Create and Sign a Transaction (PSBT)
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import { Psbt, networks } from '@btc-vision/bitcoin';
|
|
106
|
+
import { fromHex } from '@btc-vision/bitcoin';
|
|
107
|
+
import type { Satoshi } from '@btc-vision/bitcoin';
|
|
108
|
+
|
|
109
|
+
const psbt = new Psbt({ network: networks.bitcoin });
|
|
110
|
+
|
|
111
|
+
// Add input
|
|
112
|
+
psbt.addInput({
|
|
113
|
+
hash: '7d067b4a697a09d2c3cff7d4d9506c9955e93bff41bf82d439da7d030382bc3e',
|
|
114
|
+
index: 0,
|
|
115
|
+
nonWitnessUtxo: fromHex('0200000001...'),
|
|
116
|
+
sighashType: 1,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Add output (values are bigint)
|
|
120
|
+
psbt.addOutput({
|
|
121
|
+
address: '1KRMKfeZcmosxALVYESdPNez1AP1mEtywp',
|
|
122
|
+
value: 80_000n as Satoshi,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// Sign, finalize, and extract
|
|
126
|
+
psbt.signInput(0, keyPair);
|
|
127
|
+
psbt.finalizeAllInputs();
|
|
128
|
+
const txHex = psbt.extractTransaction().toHex();
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Async Signing
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
await psbt.signInputAsync(0, keyPair);
|
|
135
|
+
await psbt.signAllInputsAsync(keyPair);
|
|
50
136
|
```
|
|
51
137
|
|
|
52
|
-
|
|
138
|
+
### Parse a Transaction
|
|
53
139
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
However, depending on adoption among other environments (browsers etc.) we may keep the target back a year or two.
|
|
57
|
-
If in doubt, see the [main_ci.yml](.github/workflows/main_ci.yml) for what versions are used by our continuous integration tests.
|
|
140
|
+
```typescript
|
|
141
|
+
import { Transaction } from '@btc-vision/bitcoin';
|
|
58
142
|
|
|
59
|
-
|
|
143
|
+
const tx = Transaction.fromHex('0200000001...');
|
|
144
|
+
console.log(tx.version); // 2
|
|
145
|
+
console.log(tx.ins.length); // number of inputs
|
|
146
|
+
console.log(tx.outs.length); // number of outputs
|
|
147
|
+
console.log(tx.toHex()); // round-trip back to hex
|
|
148
|
+
```
|
|
60
149
|
|
|
150
|
+
### Decode and Encode Addresses
|
|
61
151
|
|
|
62
|
-
|
|
63
|
-
|
|
152
|
+
```typescript
|
|
153
|
+
import { address, networks } from '@btc-vision/bitcoin';
|
|
64
154
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
155
|
+
// Decode any address to its output script
|
|
156
|
+
const outputScript = address.toOutputScript(
|
|
157
|
+
'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4',
|
|
158
|
+
networks.bitcoin,
|
|
159
|
+
);
|
|
69
160
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
Often, Javascript itself is working against us by bypassing these counter-measures.
|
|
161
|
+
// Convert output script back to address
|
|
162
|
+
const addr = address.fromOutputScript(outputScript, networks.bitcoin);
|
|
73
163
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
164
|
+
// Low-level Base58Check
|
|
165
|
+
const { hash, version } = address.fromBase58Check('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH');
|
|
166
|
+
const encoded = address.toBase58Check(hash, version);
|
|
77
167
|
|
|
78
|
-
|
|
79
|
-
|
|
168
|
+
// Low-level Bech32
|
|
169
|
+
const decoded = address.fromBech32('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4');
|
|
170
|
+
const bech32Addr = address.toBech32(decoded.data, decoded.version, 'bc');
|
|
171
|
+
```
|
|
80
172
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
173
|
+
### Parallel Signing with Workers
|
|
174
|
+
|
|
175
|
+
For high-throughput signing across many inputs, use the worker pool:
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
import { createSigningPool, SignatureType } from '@btc-vision/bitcoin/workers';
|
|
179
|
+
|
|
180
|
+
// Create a platform-appropriate pool (Node.js worker_threads or Web Workers)
|
|
181
|
+
const pool = await createSigningPool({ workerCount: 4 });
|
|
182
|
+
pool.preserveWorkers();
|
|
183
|
+
|
|
184
|
+
const tasks = [
|
|
185
|
+
{
|
|
186
|
+
taskId: 'input-0',
|
|
187
|
+
inputIndex: 0,
|
|
188
|
+
hash: sighash0,
|
|
189
|
+
signatureType: SignatureType.ECDSA,
|
|
190
|
+
sighashType: 0x01,
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
taskId: 'input-1',
|
|
194
|
+
inputIndex: 1,
|
|
195
|
+
hash: sighash1,
|
|
196
|
+
signatureType: SignatureType.Schnorr,
|
|
197
|
+
sighashType: 0x00,
|
|
198
|
+
},
|
|
199
|
+
];
|
|
200
|
+
|
|
201
|
+
const result = await pool.signBatch(tasks, keyPair);
|
|
202
|
+
if (result.success) {
|
|
203
|
+
console.log(`Signed ${result.signatures.size} inputs in ${result.durationMs}ms`);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Or sign a PSBT directly in parallel
|
|
207
|
+
import { signPsbtParallel } from '@btc-vision/bitcoin/workers';
|
|
208
|
+
await signPsbtParallel(psbt, keyPair, pool);
|
|
209
|
+
|
|
210
|
+
await pool.shutdown();
|
|
211
|
+
```
|
|
87
212
|
|
|
213
|
+
## API Reference
|
|
214
|
+
|
|
215
|
+
### Exports
|
|
216
|
+
|
|
217
|
+
The library provides granular sub-path exports for tree-shaking:
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import { ... } from '@btc-vision/bitcoin'; // Full API
|
|
221
|
+
import { ... } from '@btc-vision/bitcoin/address'; // Address encoding/decoding
|
|
222
|
+
import { ... } from '@btc-vision/bitcoin/script'; // Script compile/decompile
|
|
223
|
+
import { ... } from '@btc-vision/bitcoin/crypto'; // Hash functions
|
|
224
|
+
import { ... } from '@btc-vision/bitcoin/transaction';// Transaction class
|
|
225
|
+
import { ... } from '@btc-vision/bitcoin/psbt'; // PSBT classes
|
|
226
|
+
import { ... } from '@btc-vision/bitcoin/networks'; // Network definitions
|
|
227
|
+
import { ... } from '@btc-vision/bitcoin/payments'; // Payment creators
|
|
228
|
+
import { ... } from '@btc-vision/bitcoin/io'; // Binary I/O utilities
|
|
229
|
+
import { ... } from '@btc-vision/bitcoin/ecc'; // ECC context
|
|
230
|
+
import { ... } from '@btc-vision/bitcoin/types'; // Type definitions & guards
|
|
231
|
+
import { ... } from '@btc-vision/bitcoin/errors'; // Error classes
|
|
232
|
+
import { ... } from '@btc-vision/bitcoin/workers'; // Parallel signing
|
|
233
|
+
```
|
|
88
234
|
|
|
89
|
-
###
|
|
90
|
-
|
|
235
|
+
### Branded Types
|
|
236
|
+
|
|
237
|
+
Values use branded types to prevent accidental misuse:
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
import type {
|
|
241
|
+
Bytes32, // 32-byte Uint8Array (tx hashes, witness programs)
|
|
242
|
+
Bytes20, // 20-byte Uint8Array (pubkey hashes)
|
|
243
|
+
PublicKey, // Compressed/uncompressed public key
|
|
244
|
+
XOnlyPublicKey, // 32-byte x-only pubkey (Taproot)
|
|
245
|
+
PrivateKey, // 32-byte private key
|
|
246
|
+
Satoshi, // bigint value (0 to 21e14)
|
|
247
|
+
Signature, // DER-encoded ECDSA signature
|
|
248
|
+
SchnorrSignature, // 64-byte Schnorr signature
|
|
249
|
+
Script, // Compiled script bytes
|
|
250
|
+
} from '@btc-vision/bitcoin';
|
|
251
|
+
|
|
252
|
+
// Type guards
|
|
253
|
+
import {
|
|
254
|
+
isBytes32, isBytes20, isPoint, isSatoshi,
|
|
255
|
+
isPrivateKey, isSignature, isSchnorrSignature,
|
|
256
|
+
isXOnlyPublicKey, isScript,
|
|
257
|
+
} from '@btc-vision/bitcoin';
|
|
258
|
+
|
|
259
|
+
// Conversion helpers (throw on invalid input)
|
|
260
|
+
import { toBytes32, toBytes20, toSatoshi } from '@btc-vision/bitcoin';
|
|
261
|
+
```
|
|
91
262
|
|
|
92
|
-
|
|
263
|
+
### Payment Types
|
|
264
|
+
|
|
265
|
+
| Type | Function | Class | Description |
|
|
266
|
+
|------|----------|-------|-------------|
|
|
267
|
+
| P2PK | `p2pk()` | `P2PK` | Pay-to-Public-Key |
|
|
268
|
+
| P2PKH | `p2pkh()` | `P2PKH` | Pay-to-Public-Key-Hash (Legacy) |
|
|
269
|
+
| P2SH | `p2sh()` | `P2SH` | Pay-to-Script-Hash |
|
|
270
|
+
| P2MS | `p2ms()` | `P2MS` | Pay-to-Multisig |
|
|
271
|
+
| P2WPKH | `p2wpkh()` | `P2WPKH` | SegWit v0 Public Key Hash |
|
|
272
|
+
| P2WSH | `p2wsh()` | `P2WSH` | SegWit v0 Script Hash |
|
|
273
|
+
| P2TR | `p2tr()` | `P2TR` | Taproot (SegWit v1) |
|
|
274
|
+
| P2OP | `p2op()` | `P2OP` | OPNet (SegWit v16) |
|
|
275
|
+
| Embed | `p2data()` | `Embed` | OP_RETURN data |
|
|
276
|
+
|
|
277
|
+
### Network Support
|
|
278
|
+
|
|
279
|
+
| Network | Constant | Bech32 Prefix |
|
|
280
|
+
|---------|----------|---------------|
|
|
281
|
+
| Bitcoin Mainnet | `networks.bitcoin` | `bc` |
|
|
282
|
+
| Bitcoin Testnet | `networks.testnet` | `tb` |
|
|
283
|
+
| Bitcoin Regtest | `networks.regtest` | `bcrt` |
|
|
284
|
+
| Dogecoin | `networks.dogecoin` | - |
|
|
285
|
+
| Litecoin | `networks.litecoin` | `ltc` |
|
|
286
|
+
| Bitcoin Cash | `networks.bitcoinCash` | `bitcoincash` |
|
|
287
|
+
| Dash | `networks.dash` | - |
|
|
288
|
+
|
|
289
|
+
### Error Handling
|
|
290
|
+
|
|
291
|
+
All errors extend `BitcoinError`:
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
import {
|
|
295
|
+
BitcoinError, // Base class
|
|
296
|
+
ValidationError, // Input validation failures
|
|
297
|
+
InvalidInputError, // Invalid transaction input
|
|
298
|
+
InvalidOutputError,// Invalid transaction output
|
|
299
|
+
ScriptError, // Script operation failures
|
|
300
|
+
PsbtError, // PSBT operation failures
|
|
301
|
+
EccError, // ECC library not initialized
|
|
302
|
+
AddressError, // Address encoding/decoding failures
|
|
303
|
+
SignatureError, // Signature operation failures
|
|
304
|
+
} from '@btc-vision/bitcoin';
|
|
305
|
+
|
|
306
|
+
try {
|
|
307
|
+
psbt.signInput(0, signer);
|
|
308
|
+
} catch (err) {
|
|
309
|
+
if (err instanceof PsbtError) {
|
|
310
|
+
// Handle PSBT-specific error
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
```
|
|
93
314
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
315
|
+
### Utility Functions
|
|
316
|
+
|
|
317
|
+
```typescript
|
|
318
|
+
import {
|
|
319
|
+
toHex, fromHex, isHex, // Hex encoding
|
|
320
|
+
concat, equals, compare, // Buffer operations
|
|
321
|
+
clone, reverse, reverseCopy, // Buffer manipulation
|
|
322
|
+
alloc, xor, isZero, // Buffer utilities
|
|
323
|
+
fromUtf8, toUtf8, // UTF-8 conversion
|
|
324
|
+
toXOnly, // Compress pubkey to x-only (32 bytes)
|
|
325
|
+
decompressPublicKey, // Decompress compressed pubkey
|
|
326
|
+
} from '@btc-vision/bitcoin';
|
|
97
327
|
```
|
|
98
328
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
```javascript
|
|
102
|
-
<script type="module">import "/scripts/bitcoinjs-lib.js"</script>
|
|
103
|
-
````
|
|
104
|
-
|
|
105
|
-
#### Using Taproot:
|
|
106
|
-
When utilizing Taproot features with bitcoinjs-lib, you may need to include an additional ECC (Elliptic Curve Cryptography) library. The commonly used `tiny-secp256k1` library, however, might lead to compatibility issues due to its reliance on WASM (WebAssembly). The following alternatives may be used instead, though they may be significantly slower for high volume of signing and pubkey deriving operations.
|
|
107
|
-
|
|
108
|
-
#### Alternatives for ECC Library:
|
|
109
|
-
1. `@bitcoinjs-lib/tiny-secp256k1-asmjs`
|
|
110
|
-
A version of `tiny-secp256k1` compiled to ASM.js directly from the WASM version, potentially better supported in browsers. This is the slowest option.
|
|
111
|
-
2. `@bitcoinerlab/secp256k1`
|
|
112
|
-
Another alternative library for ECC functionality. This requires access to the global `BigInt` primitive.
|
|
113
|
-
For advantages and detailed comparison of these libraries, visit: [tiny-secp256k1 GitHub page](https://github.com/bitcoinjs/tiny-secp256k1).
|
|
114
|
-
|
|
115
|
-
**NOTE**: We use Node Maintenance LTS features, if you need strict ES5, use [`--transform babelify`](https://github.com/babel/babelify) in conjunction with your `browserify` step (using an [`es2015`](https://babeljs.io/docs/plugins/preset-es2015/) preset).
|
|
116
|
-
|
|
117
|
-
**WARNING**: iOS devices have [problems](https://github.com/feross/buffer/issues/136), use at least [buffer@5.0.5](https://github.com/feross/buffer/pull/155) or greater, and enforce the test suites (for `Buffer`, and any other dependency) pass before use.
|
|
118
|
-
|
|
119
|
-
### Typescript or VSCode users
|
|
120
|
-
Type declarations for Typescript are included in this library. Normal installation should include all the needed type information.
|
|
121
|
-
|
|
122
|
-
## Examples
|
|
123
|
-
The below examples are implemented as integration tests, they should be very easy to understand.
|
|
124
|
-
Otherwise, pull requests are appreciated.
|
|
125
|
-
Some examples interact (via HTTPS) with a 3rd Party Blockchain Provider (3PBP).
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
- [Taproot Key Spend](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/taproot.spec.ts)
|
|
129
|
-
- [Generate a random address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
130
|
-
- [Import an address via WIF](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
131
|
-
- [Generate a 2-of-3 P2SH multisig address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
132
|
-
- [Generate a SegWit address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
133
|
-
- [Generate a SegWit P2SH address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
134
|
-
- [Generate a SegWit 3-of-4 multisig address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
135
|
-
- [Generate a SegWit 2-of-2 P2SH multisig address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
136
|
-
- [Support the retrieval of transactions for an address (3rd party blockchain)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
137
|
-
- [Generate a Testnet address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
138
|
-
- [Generate a Litecoin address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
139
|
-
- [Create a 1-to-1 Transaction](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
140
|
-
- [Create (and broadcast via 3PBP) a typical Transaction](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
141
|
-
- [Create (and broadcast via 3PBP) a Transaction with an OP\_RETURN output](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
142
|
-
- [Create (and broadcast via 3PBP) a Transaction with a 2-of-4 P2SH(multisig) input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
143
|
-
- [Create (and broadcast via 3PBP) a Transaction with a SegWit P2SH(P2WPKH) input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
144
|
-
- [Create (and broadcast via 3PBP) a Transaction with a SegWit P2WPKH input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
145
|
-
- [Create (and broadcast via 3PBP) a Transaction with a SegWit P2PK input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
146
|
-
- [Create (and broadcast via 3PBP) a Transaction with a SegWit 3-of-4 P2SH(P2WSH(multisig)) input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
147
|
-
- [Create (and broadcast via 3PBP) a Transaction and sign with an HDSigner interface (bip32)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
148
|
-
- [Import a BIP32 testnet xpriv and export to WIF](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
149
|
-
- [Export a BIP32 xpriv, then import it](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
150
|
-
- [Export a BIP32 xpub](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
151
|
-
- [Create a BIP32, bitcoin, account 0, external address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
152
|
-
- [Create a BIP44, bitcoin, account 0, external address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
153
|
-
- [Create a BIP49, bitcoin testnet, account 0, external address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
154
|
-
- [Use BIP39 to generate BIP32 addresses](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
155
|
-
- [Create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the past)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.spec.ts)
|
|
156
|
-
- [Create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the future)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.spec.ts)
|
|
157
|
-
- [Create (and broadcast via 3PBP) a Transaction where Alice and Bob can redeem the output at any time](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.spec.ts)
|
|
158
|
-
- [Create (but fail to broadcast via 3PBP) a Transaction where Alice attempts to redeem before the expiry](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.spec.ts)
|
|
159
|
-
- [Create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the future) (simple CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
|
|
160
|
-
- [Create (but fail to broadcast via 3PBP) a Transaction where Alice attempts to redeem before the expiry (simple CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
|
|
161
|
-
- [Create (and broadcast via 3PBP) a Transaction where Bob and Charles can send (complex CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
|
|
162
|
-
- [Create (and broadcast via 3PBP) a Transaction where Alice (mediator) and Bob can send after 2 blocks (complex CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
|
|
163
|
-
- [Create (and broadcast via 3PBP) a Transaction where Alice (mediator) can send after 5 blocks (complex CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
|
|
164
|
-
|
|
165
|
-
If you have a use case that you feel could be listed here, please [ask for it](https://github.com/bitcoinjs/bitcoinjs-lib/issues/new)!
|
|
329
|
+
### Crypto Functions
|
|
166
330
|
|
|
331
|
+
```typescript
|
|
332
|
+
import { sha256, sha1, ripemd160, hash160, hash256, taggedHash } from '@btc-vision/bitcoin';
|
|
167
333
|
|
|
168
|
-
|
|
169
|
-
|
|
334
|
+
const h = hash160(publicKey); // RIPEMD160(SHA256(data))
|
|
335
|
+
const d = hash256(data); // SHA256(SHA256(data))
|
|
336
|
+
const t = taggedHash('TapLeaf', data); // BIP340 tagged hash
|
|
337
|
+
```
|
|
170
338
|
|
|
339
|
+
## Browser Usage
|
|
171
340
|
|
|
172
|
-
|
|
341
|
+
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.
|
|
173
342
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
343
|
+
For Taproot operations in the browser, `tiny-secp256k1` relies on WebAssembly. If WASM is unavailable, consider these alternatives:
|
|
344
|
+
|
|
345
|
+
- **`@bitcoinjs-lib/tiny-secp256k1-asmjs`** -- ASM.js fallback (slower)
|
|
346
|
+
- **`@bitcoinerlab/secp256k1`** -- Pure JS with `BigInt` (requires `BigInt` support)
|
|
347
|
+
|
|
348
|
+
## Running Tests
|
|
349
|
+
|
|
350
|
+
```bash
|
|
351
|
+
npm test # Full suite (lint + build + test)
|
|
352
|
+
npm run unit # Unit tests only
|
|
353
|
+
npm run integration # Integration tests
|
|
354
|
+
npm run test:browser # Browser tests (Playwright)
|
|
355
|
+
npm run bench # Benchmarks
|
|
177
356
|
```
|
|
178
357
|
|
|
179
|
-
##
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
- [BCoin](https://github.com/indutny/bcoin)
|
|
197
|
-
- [Bitcore](https://github.com/bitpay/bitcore)
|
|
198
|
-
- [Cryptocoin](https://github.com/cryptocoinjs/cryptocoin)
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
## LICENSE [MIT](LICENSE)
|
|
358
|
+
## Contributing
|
|
359
|
+
|
|
360
|
+
1. Fork the repository
|
|
361
|
+
2. Create a feature branch
|
|
362
|
+
3. Make your changes
|
|
363
|
+
4. Run tests: `npm test`
|
|
364
|
+
5. Submit a pull request
|
|
365
|
+
|
|
366
|
+
## License
|
|
367
|
+
|
|
368
|
+
[MIT](LICENSE)
|
|
369
|
+
|
|
370
|
+
## Links
|
|
371
|
+
|
|
372
|
+
- [GitHub](https://github.com/btc-vision/bitcoin)
|
|
373
|
+
- [npm](https://www.npmjs.com/package/@btc-vision/bitcoin)
|
|
374
|
+
- [API Documentation](https://bitcoinjs.github.io/bitcoinjs-lib/)
|