@btc-vision/bitcoin 7.0.0-beta.0 → 7.0.0-beta.1
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 +112 -13
- package/benchmark-compare/BENCHMARK.md +74 -59
- package/benchmark-compare/compare.bench.ts +249 -96
- package/benchmark-compare/harness.ts +23 -25
- package/benchmark-compare/package.json +1 -0
- package/browser/address.d.ts +4 -4
- package/browser/address.d.ts.map +1 -1
- package/browser/chunks/{psbt-parallel-B-dfm5GZ.js → psbt-parallel-jZ6QcCnM.js} +3128 -2731
- package/browser/index.d.ts +1 -1
- package/browser/index.d.ts.map +1 -1
- package/browser/index.js +603 -585
- package/browser/io/base58check.d.ts +1 -25
- package/browser/io/base58check.d.ts.map +1 -1
- package/browser/io/base64.d.ts.map +1 -1
- package/browser/networks.d.ts +1 -0
- package/browser/networks.d.ts.map +1 -1
- package/browser/payments/bip341.d.ts +17 -0
- package/browser/payments/bip341.d.ts.map +1 -1
- package/browser/payments/index.d.ts +3 -2
- package/browser/payments/index.d.ts.map +1 -1
- package/browser/payments/p2mr.d.ts +169 -0
- package/browser/payments/p2mr.d.ts.map +1 -0
- package/browser/payments/types.d.ts +11 -1
- package/browser/payments/types.d.ts.map +1 -1
- package/browser/psbt/bip371.d.ts +30 -0
- package/browser/psbt/bip371.d.ts.map +1 -1
- package/browser/psbt/psbtutils.d.ts +1 -0
- package/browser/psbt/psbtutils.d.ts.map +1 -1
- package/browser/psbt.d.ts.map +1 -1
- package/browser/workers/index.js +9 -9
- package/build/address.d.ts +4 -4
- package/build/address.d.ts.map +1 -1
- package/build/address.js +11 -1
- package/build/address.js.map +1 -1
- package/build/index.d.ts +1 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js.map +1 -1
- package/build/io/base58check.d.ts +1 -25
- package/build/io/base58check.d.ts.map +1 -1
- package/build/io/base58check.js +1 -31
- package/build/io/base58check.js.map +1 -1
- package/build/io/base64.d.ts.map +1 -1
- package/build/io/base64.js +3 -0
- package/build/io/base64.js.map +1 -1
- package/build/networks.d.ts +1 -0
- package/build/networks.d.ts.map +1 -1
- package/build/networks.js +12 -0
- package/build/networks.js.map +1 -1
- package/build/payments/bip341.d.ts +17 -0
- package/build/payments/bip341.d.ts.map +1 -1
- package/build/payments/bip341.js +32 -1
- package/build/payments/bip341.js.map +1 -1
- package/build/payments/index.d.ts +3 -2
- package/build/payments/index.d.ts.map +1 -1
- package/build/payments/index.js +2 -1
- package/build/payments/index.js.map +1 -1
- package/build/payments/p2mr.d.ts +178 -0
- package/build/payments/p2mr.d.ts.map +1 -0
- package/build/payments/p2mr.js +555 -0
- package/build/payments/p2mr.js.map +1 -0
- package/build/payments/types.d.ts +11 -1
- package/build/payments/types.d.ts.map +1 -1
- package/build/payments/types.js +1 -0
- package/build/payments/types.js.map +1 -1
- package/build/psbt/bip371.d.ts +30 -0
- package/build/psbt/bip371.d.ts.map +1 -1
- package/build/psbt/bip371.js +80 -15
- package/build/psbt/bip371.js.map +1 -1
- package/build/psbt/psbtutils.d.ts +1 -0
- package/build/psbt/psbtutils.d.ts.map +1 -1
- package/build/psbt/psbtutils.js +2 -0
- package/build/psbt/psbtutils.js.map +1 -1
- package/build/psbt.d.ts.map +1 -1
- package/build/psbt.js +3 -2
- package/build/psbt.js.map +1 -1
- package/build/pubkey.js +1 -1
- package/build/pubkey.js.map +1 -1
- package/build/tsconfig.build.tsbuildinfo +1 -1
- package/documentation/README.md +122 -0
- package/documentation/address.md +820 -0
- package/documentation/block.md +679 -0
- package/documentation/crypto.md +461 -0
- package/documentation/ecc.md +584 -0
- package/documentation/errors.md +656 -0
- package/documentation/io.md +942 -0
- package/documentation/networks.md +625 -0
- package/documentation/p2mr.md +380 -0
- package/documentation/payments.md +1485 -0
- package/documentation/psbt.md +1400 -0
- package/documentation/script.md +730 -0
- package/documentation/taproot.md +670 -0
- package/documentation/transaction.md +943 -0
- package/documentation/types.md +587 -0
- package/documentation/workers.md +1007 -0
- package/eslint.config.js +3 -0
- package/package.json +17 -14
- package/src/address.ts +22 -10
- package/src/index.ts +1 -0
- package/src/io/base58check.ts +1 -35
- package/src/io/base64.ts +5 -0
- package/src/networks.ts +13 -0
- package/src/payments/bip341.ts +36 -1
- package/src/payments/index.ts +4 -0
- package/src/payments/p2mr.ts +660 -0
- package/src/payments/types.ts +12 -0
- package/src/psbt/bip371.ts +84 -13
- package/src/psbt/psbtutils.ts +2 -0
- package/src/psbt.ts +4 -2
- package/src/pubkey.ts +1 -1
- package/test/bitcoin.core.spec.ts +1 -1
- package/test/fixtures/p2mr.json +270 -0
- package/test/integration/taproot.spec.ts +7 -3
- package/test/opnetTestnet.spec.ts +302 -0
- package/test/payments.spec.ts +3 -1
- package/test/psbt.spec.ts +297 -2
- package/test/tsconfig.json +2 -2
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
# Errors
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
The library defines and exports a hierarchy of custom error classes rooted at `BitcoinError`. However, the library itself does **not** throw these custom classes internally. All errors thrown by the library are standard built-in types: `Error`, `TypeError`, and `RangeError`.
|
|
6
|
+
|
|
7
|
+
The custom error classes are exported as a convenience for **consumers** of the library. You can use them in your own application code to throw domain-specific, categorized errors that integrate with the `BitcoinError` hierarchy.
|
|
8
|
+
|
|
9
|
+
### What the Library Actually Throws
|
|
10
|
+
|
|
11
|
+
| Error Type | Typical Situations |
|
|
12
|
+
|---|---|
|
|
13
|
+
| `Error` | General failures: malformed data, missing prerequisites, invalid state |
|
|
14
|
+
| `TypeError` | Wrong argument types, invalid lengths, format mismatches, payment validation failures |
|
|
15
|
+
| `RangeError` | Numeric values out of bounds (e.g. negative satoshi, exceeding max supply, exceeding curve order) |
|
|
16
|
+
|
|
17
|
+
### Custom Error Classes (for Consumer Use)
|
|
18
|
+
|
|
19
|
+
| Error class | Intended Domain |
|
|
20
|
+
|-------------|-----------------|
|
|
21
|
+
| `BitcoinError` | Base class for all custom Bitcoin errors |
|
|
22
|
+
| `ValidationError` | Data validation failures (key lengths, value ranges, format checks) |
|
|
23
|
+
| `InvalidInputError` | Invalid transaction inputs |
|
|
24
|
+
| `InvalidOutputError` | Invalid transaction outputs |
|
|
25
|
+
| `ScriptError` | Script compilation or decompilation failures |
|
|
26
|
+
| `PsbtError` | PSBT construction, signing, or finalization failures |
|
|
27
|
+
| `EccError` | ECC library not initialized or cryptographic operation failures |
|
|
28
|
+
| `AddressError` | Address encoding or decoding failures |
|
|
29
|
+
| `SignatureError` | Signature creation or validation failures |
|
|
30
|
+
|
|
31
|
+
### Inheritance Hierarchy
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
Error (built-in)
|
|
35
|
+
└── BitcoinError
|
|
36
|
+
├── ValidationError
|
|
37
|
+
├── InvalidInputError
|
|
38
|
+
├── InvalidOutputError
|
|
39
|
+
├── ScriptError
|
|
40
|
+
├── PsbtError
|
|
41
|
+
├── EccError
|
|
42
|
+
├── AddressError
|
|
43
|
+
└── SignatureError
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Imports
|
|
49
|
+
|
|
50
|
+
All error classes are exported individually and also grouped under the `errors` namespace object.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
// Individual named imports
|
|
54
|
+
import {
|
|
55
|
+
BitcoinError,
|
|
56
|
+
ValidationError,
|
|
57
|
+
InvalidInputError,
|
|
58
|
+
InvalidOutputError,
|
|
59
|
+
ScriptError,
|
|
60
|
+
PsbtError,
|
|
61
|
+
EccError,
|
|
62
|
+
AddressError,
|
|
63
|
+
SignatureError,
|
|
64
|
+
} from '@btc-vision/bitcoin';
|
|
65
|
+
|
|
66
|
+
// Namespace import (all error classes in one object)
|
|
67
|
+
import { errors } from '@btc-vision/bitcoin';
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Errors Thrown by the Library
|
|
73
|
+
|
|
74
|
+
The library throws standard `Error`, `TypeError`, and `RangeError` instances with descriptive messages. The sections below list common message patterns organized by area. These are not exhaustive but cover the most frequently encountered errors.
|
|
75
|
+
|
|
76
|
+
### Address Errors
|
|
77
|
+
|
|
78
|
+
Thrown as `TypeError` or `Error` from address encoding/decoding operations.
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
TypeError: Invalid program length for segwit address
|
|
82
|
+
TypeError: Empty output
|
|
83
|
+
TypeError: Invalid version for segwit address
|
|
84
|
+
TypeError: Unsupported push opcode in script
|
|
85
|
+
TypeError: Invalid segwit version <version>
|
|
86
|
+
TypeError: <address> is too short
|
|
87
|
+
TypeError: <address> is too long
|
|
88
|
+
TypeError: <address> has no matching Script
|
|
89
|
+
Error: <address> has an invalid prefix
|
|
90
|
+
Error: <script> has no matching Address
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Transaction Errors
|
|
94
|
+
|
|
95
|
+
Thrown as `TypeError` or `Error` from transaction construction and hashing.
|
|
96
|
+
|
|
97
|
+
```
|
|
98
|
+
TypeError: Expected 32-byte hash
|
|
99
|
+
TypeError: Expected unsigned 32-bit integer for index
|
|
100
|
+
TypeError: Expected unsigned 32-bit integer for sequence
|
|
101
|
+
TypeError: Expected Uint8Array for scriptPubKey
|
|
102
|
+
TypeError: Expected bigint satoshi value (0 to 2^63-1)
|
|
103
|
+
TypeError: Expected non-negative integer for inIndex
|
|
104
|
+
TypeError: Expected Uint8Array for prevOutScript
|
|
105
|
+
TypeError: Expected integer for hashType
|
|
106
|
+
Error: Could not decompile prevOutScript
|
|
107
|
+
Error: Transaction has superfluous witness data
|
|
108
|
+
Error: Transaction has unexpected data
|
|
109
|
+
Error: Must supply prevout script and value for all inputs
|
|
110
|
+
Error: Add outputs to the transaction before signing.
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### PSBT Errors
|
|
114
|
+
|
|
115
|
+
Thrown as `Error` from PSBT construction, signing, and finalization.
|
|
116
|
+
|
|
117
|
+
```
|
|
118
|
+
Error: Input index too high
|
|
119
|
+
Error: Not finalized
|
|
120
|
+
Error: Need Signer to sign input
|
|
121
|
+
Error: Need HDSigner to sign input
|
|
122
|
+
Error: Need Schnorr Signer to sign taproot input #<index>.
|
|
123
|
+
Error: No inputs were signed
|
|
124
|
+
Error: Can not sign for input #<index> with the key <pubkey>
|
|
125
|
+
Error: No script found for input #<index>
|
|
126
|
+
Error: Cannot finalize input #<index>. Not Taproot.
|
|
127
|
+
Error: Cannot finalize input #<index>. Missing witness utxo.
|
|
128
|
+
Error: Cannot finalize taproot key spend
|
|
129
|
+
Error: No signatures to validate
|
|
130
|
+
Error: Need validator function to validate signatures
|
|
131
|
+
Error: No signatures for this pubkey
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Script Errors
|
|
135
|
+
|
|
136
|
+
Thrown as `TypeError` or `Error` from script compilation and decompilation.
|
|
137
|
+
|
|
138
|
+
```
|
|
139
|
+
TypeError: Expected an array
|
|
140
|
+
TypeError: Expected a Uint8Array
|
|
141
|
+
TypeError: Expected a string
|
|
142
|
+
TypeError: Expected hex string
|
|
143
|
+
TypeError: Expected push-only script
|
|
144
|
+
Error: Could not decode chunks
|
|
145
|
+
Error: Could not convert invalid chunks to ASM
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Type Validation Errors
|
|
149
|
+
|
|
150
|
+
Thrown as `TypeError` or `RangeError` from the type-checking utilities.
|
|
151
|
+
|
|
152
|
+
```
|
|
153
|
+
TypeError: Expected 32-byte Uint8Array, got <n> bytes
|
|
154
|
+
TypeError: Expected 20-byte Uint8Array, got <n> bytes
|
|
155
|
+
TypeError: <name> must be Uint8Array, got <type>
|
|
156
|
+
TypeError: <name> must be 32 bytes, got <n>
|
|
157
|
+
RangeError: <name> cannot be zero
|
|
158
|
+
RangeError: <name> exceeds curve order
|
|
159
|
+
RangeError: Satoshi cannot be negative, got <value>
|
|
160
|
+
RangeError: Satoshi exceeds maximum supply (<max>), got <value>
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Payment Validation Errors
|
|
164
|
+
|
|
165
|
+
Thrown as `TypeError` from payment type constructors (p2pkh, p2wpkh, p2wsh, p2tr, p2ms, p2mr, p2op, embed).
|
|
166
|
+
|
|
167
|
+
```
|
|
168
|
+
TypeError: Invalid address
|
|
169
|
+
TypeError: Invalid prefix or Network mismatch
|
|
170
|
+
TypeError: Invalid address version
|
|
171
|
+
TypeError: Invalid address data
|
|
172
|
+
TypeError: Hash mismatch
|
|
173
|
+
TypeError: Output is invalid
|
|
174
|
+
TypeError: Pubkey mismatch
|
|
175
|
+
TypeError: Signature mismatch
|
|
176
|
+
TypeError: Input is invalid
|
|
177
|
+
TypeError: Input has invalid signature
|
|
178
|
+
TypeError: Witness is invalid
|
|
179
|
+
TypeError: Not enough data
|
|
180
|
+
TypeError: Redeem.output is invalid
|
|
181
|
+
TypeError: Non push-only scriptSig
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### ECC Errors
|
|
185
|
+
|
|
186
|
+
Thrown as `Error` when the ECC library context is missing or misconfigured.
|
|
187
|
+
|
|
188
|
+
```
|
|
189
|
+
Error: ECC Library is required (from initEccLib or context)
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### Script Number Errors
|
|
193
|
+
|
|
194
|
+
```
|
|
195
|
+
TypeError: Script number overflow
|
|
196
|
+
Error: Non-minimally encoded script number
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### Signature Encoding Errors
|
|
200
|
+
|
|
201
|
+
```
|
|
202
|
+
Error: Invalid hashType <hashType>
|
|
203
|
+
TypeError: Expected signature to be a 64-byte Uint8Array
|
|
204
|
+
TypeError: Expected hashType to be a UInt8
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
### Bech32 Errors
|
|
208
|
+
|
|
209
|
+
```
|
|
210
|
+
TypeError: <address> uses wrong encoding
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## Custom Error Class Reference
|
|
216
|
+
|
|
217
|
+
The following classes are defined in `src/errors.ts` and exported for use in consumer code. The library itself does not throw them.
|
|
218
|
+
|
|
219
|
+
### BitcoinError
|
|
220
|
+
|
|
221
|
+
Base class for all custom Bitcoin errors. All other custom error classes extend this one, so catching `BitcoinError` will catch every custom subtype.
|
|
222
|
+
|
|
223
|
+
| Property | Value |
|
|
224
|
+
|----------|-------|
|
|
225
|
+
| `name` | `'BitcoinError'` |
|
|
226
|
+
| Extends | `Error` |
|
|
227
|
+
|
|
228
|
+
#### Constructor
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
new BitcoinError(message: string)
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
| Parameter | Type | Description |
|
|
235
|
+
|-----------|------|-------------|
|
|
236
|
+
| `message` | `string` | Human-readable description of the error |
|
|
237
|
+
|
|
238
|
+
The constructor calls `Error.captureStackTrace` (when available on V8 engines) to maintain a clean stack trace pointing to the call site rather than the error constructor itself.
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
### ValidationError
|
|
243
|
+
|
|
244
|
+
Intended for data validation failures such as incorrect key lengths, values outside allowed ranges, or malformed data formats.
|
|
245
|
+
|
|
246
|
+
| Property | Value |
|
|
247
|
+
|----------|-------|
|
|
248
|
+
| `name` | `'ValidationError'` |
|
|
249
|
+
| Extends | `BitcoinError` |
|
|
250
|
+
|
|
251
|
+
#### Constructor
|
|
252
|
+
|
|
253
|
+
```typescript
|
|
254
|
+
new ValidationError(message: string)
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
| Parameter | Type | Description |
|
|
258
|
+
|-----------|------|-------------|
|
|
259
|
+
| `message` | `string` | Description of the validation failure |
|
|
260
|
+
|
|
261
|
+
#### Recommended Use
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
import { ValidationError } from '@btc-vision/bitcoin';
|
|
265
|
+
|
|
266
|
+
function validatePublicKey(key: Uint8Array): void {
|
|
267
|
+
if (key.length !== 33 && key.length !== 65) {
|
|
268
|
+
throw new ValidationError(`Invalid public key length: ${key.length}`);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
---
|
|
274
|
+
|
|
275
|
+
### InvalidInputError
|
|
276
|
+
|
|
277
|
+
Intended for signaling that a transaction input is malformed or references invalid data.
|
|
278
|
+
|
|
279
|
+
| Property | Value |
|
|
280
|
+
|----------|-------|
|
|
281
|
+
| `name` | `'InvalidInputError'` |
|
|
282
|
+
| Extends | `BitcoinError` |
|
|
283
|
+
|
|
284
|
+
#### Constructor
|
|
285
|
+
|
|
286
|
+
```typescript
|
|
287
|
+
new InvalidInputError(message: string)
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
| Parameter | Type | Description |
|
|
291
|
+
|-----------|------|-------------|
|
|
292
|
+
| `message` | `string` | Description of the invalid input |
|
|
293
|
+
|
|
294
|
+
#### Recommended Use
|
|
295
|
+
|
|
296
|
+
```typescript
|
|
297
|
+
import { InvalidInputError } from '@btc-vision/bitcoin';
|
|
298
|
+
|
|
299
|
+
function checkInput(index: number, totalInputs: number): void {
|
|
300
|
+
if (index >= totalInputs) {
|
|
301
|
+
throw new InvalidInputError(`Input index ${index} is out of range (max ${totalInputs - 1})`);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
---
|
|
307
|
+
|
|
308
|
+
### InvalidOutputError
|
|
309
|
+
|
|
310
|
+
Intended for signaling that a transaction output is malformed or contains invalid values.
|
|
311
|
+
|
|
312
|
+
| Property | Value |
|
|
313
|
+
|----------|-------|
|
|
314
|
+
| `name` | `'InvalidOutputError'` |
|
|
315
|
+
| Extends | `BitcoinError` |
|
|
316
|
+
|
|
317
|
+
#### Constructor
|
|
318
|
+
|
|
319
|
+
```typescript
|
|
320
|
+
new InvalidOutputError(message: string)
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
| Parameter | Type | Description |
|
|
324
|
+
|-----------|------|-------------|
|
|
325
|
+
| `message` | `string` | Description of the invalid output |
|
|
326
|
+
|
|
327
|
+
#### Recommended Use
|
|
328
|
+
|
|
329
|
+
```typescript
|
|
330
|
+
import { InvalidOutputError } from '@btc-vision/bitcoin';
|
|
331
|
+
|
|
332
|
+
function checkOutputValue(value: bigint): void {
|
|
333
|
+
if (value < 0n) {
|
|
334
|
+
throw new InvalidOutputError('Output value cannot be negative');
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
---
|
|
340
|
+
|
|
341
|
+
### ScriptError
|
|
342
|
+
|
|
343
|
+
Intended for signaling script compilation or decompilation failures.
|
|
344
|
+
|
|
345
|
+
| Property | Value |
|
|
346
|
+
|----------|-------|
|
|
347
|
+
| `name` | `'ScriptError'` |
|
|
348
|
+
| Extends | `BitcoinError` |
|
|
349
|
+
|
|
350
|
+
#### Constructor
|
|
351
|
+
|
|
352
|
+
```typescript
|
|
353
|
+
new ScriptError(message: string)
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
| Parameter | Type | Description |
|
|
357
|
+
|-----------|------|-------------|
|
|
358
|
+
| `message` | `string` | Description of the script failure |
|
|
359
|
+
|
|
360
|
+
#### Recommended Use
|
|
361
|
+
|
|
362
|
+
```typescript
|
|
363
|
+
import { ScriptError } from '@btc-vision/bitcoin';
|
|
364
|
+
|
|
365
|
+
function processScript(raw: Uint8Array): void {
|
|
366
|
+
if (raw.length === 0) {
|
|
367
|
+
throw new ScriptError('Cannot process empty script');
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
---
|
|
373
|
+
|
|
374
|
+
### PsbtError
|
|
375
|
+
|
|
376
|
+
Intended for signaling PSBT construction, signing, or finalization failures.
|
|
377
|
+
|
|
378
|
+
| Property | Value |
|
|
379
|
+
|----------|-------|
|
|
380
|
+
| `name` | `'PsbtError'` |
|
|
381
|
+
| Extends | `BitcoinError` |
|
|
382
|
+
|
|
383
|
+
#### Constructor
|
|
384
|
+
|
|
385
|
+
```typescript
|
|
386
|
+
new PsbtError(message: string)
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
| Parameter | Type | Description |
|
|
390
|
+
|-----------|------|-------------|
|
|
391
|
+
| `message` | `string` | Description of the PSBT failure |
|
|
392
|
+
|
|
393
|
+
#### Recommended Use
|
|
394
|
+
|
|
395
|
+
```typescript
|
|
396
|
+
import { PsbtError } from '@btc-vision/bitcoin';
|
|
397
|
+
|
|
398
|
+
function requireSigned(inputIndex: number, isSigned: boolean): void {
|
|
399
|
+
if (!isSigned) {
|
|
400
|
+
throw new PsbtError(`Cannot finalize unsigned input #${inputIndex}`);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
---
|
|
406
|
+
|
|
407
|
+
### EccError
|
|
408
|
+
|
|
409
|
+
Intended for signaling ECC initialization or cryptographic operation failures.
|
|
410
|
+
|
|
411
|
+
| Property | Value |
|
|
412
|
+
|----------|-------|
|
|
413
|
+
| `name` | `'EccError'` |
|
|
414
|
+
| Extends | `BitcoinError` |
|
|
415
|
+
|
|
416
|
+
#### Constructor
|
|
417
|
+
|
|
418
|
+
```typescript
|
|
419
|
+
new EccError(message: string)
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
| Parameter | Type | Description |
|
|
423
|
+
|-----------|------|-------------|
|
|
424
|
+
| `message` | `string` | Description of the ECC failure |
|
|
425
|
+
|
|
426
|
+
#### Recommended Use
|
|
427
|
+
|
|
428
|
+
```typescript
|
|
429
|
+
import { EccError } from '@btc-vision/bitcoin';
|
|
430
|
+
|
|
431
|
+
function ensureEccReady(eccLib: unknown): void {
|
|
432
|
+
if (!eccLib) {
|
|
433
|
+
throw new EccError('ECC library must be initialized before use');
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
---
|
|
439
|
+
|
|
440
|
+
### AddressError
|
|
441
|
+
|
|
442
|
+
Intended for signaling address encoding or decoding failures.
|
|
443
|
+
|
|
444
|
+
| Property | Value |
|
|
445
|
+
|----------|-------|
|
|
446
|
+
| `name` | `'AddressError'` |
|
|
447
|
+
| Extends | `BitcoinError` |
|
|
448
|
+
|
|
449
|
+
#### Constructor
|
|
450
|
+
|
|
451
|
+
```typescript
|
|
452
|
+
new AddressError(message: string)
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
| Parameter | Type | Description |
|
|
456
|
+
|-----------|------|-------------|
|
|
457
|
+
| `message` | `string` | Description of the address failure |
|
|
458
|
+
|
|
459
|
+
#### Recommended Use
|
|
460
|
+
|
|
461
|
+
```typescript
|
|
462
|
+
import { AddressError } from '@btc-vision/bitcoin';
|
|
463
|
+
|
|
464
|
+
function validateAddress(addr: string): void {
|
|
465
|
+
if (!addr || addr.length < 14) {
|
|
466
|
+
throw new AddressError(`Address is too short: ${addr}`);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
---
|
|
472
|
+
|
|
473
|
+
### SignatureError
|
|
474
|
+
|
|
475
|
+
Intended for signaling signature creation or validation failures.
|
|
476
|
+
|
|
477
|
+
| Property | Value |
|
|
478
|
+
|----------|-------|
|
|
479
|
+
| `name` | `'SignatureError'` |
|
|
480
|
+
| Extends | `BitcoinError` |
|
|
481
|
+
|
|
482
|
+
#### Constructor
|
|
483
|
+
|
|
484
|
+
```typescript
|
|
485
|
+
new SignatureError(message: string)
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
| Parameter | Type | Description |
|
|
489
|
+
|-----------|------|-------------|
|
|
490
|
+
| `message` | `string` | Description of the signature failure |
|
|
491
|
+
|
|
492
|
+
#### Recommended Use
|
|
493
|
+
|
|
494
|
+
```typescript
|
|
495
|
+
import { SignatureError } from '@btc-vision/bitcoin';
|
|
496
|
+
|
|
497
|
+
function checkSignature(sig: Uint8Array): void {
|
|
498
|
+
if (sig.length !== 64 && sig.length !== 65) {
|
|
499
|
+
throw new SignatureError(`Unexpected signature length: ${sig.length}`);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
---
|
|
505
|
+
|
|
506
|
+
## The `errors` Namespace
|
|
507
|
+
|
|
508
|
+
All error classes are also available as a single namespace object for convenience.
|
|
509
|
+
|
|
510
|
+
```typescript
|
|
511
|
+
import { errors } from '@btc-vision/bitcoin';
|
|
512
|
+
|
|
513
|
+
// Access any error class through the namespace
|
|
514
|
+
const err = new errors.ValidationError('bad key');
|
|
515
|
+
console.log(err instanceof errors.BitcoinError); // true
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
The namespace contains the following properties:
|
|
519
|
+
|
|
520
|
+
| Property | Class |
|
|
521
|
+
|----------|-------|
|
|
522
|
+
| `errors.BitcoinError` | `BitcoinError` |
|
|
523
|
+
| `errors.ValidationError` | `ValidationError` |
|
|
524
|
+
| `errors.InvalidInputError` | `InvalidInputError` |
|
|
525
|
+
| `errors.InvalidOutputError` | `InvalidOutputError` |
|
|
526
|
+
| `errors.ScriptError` | `ScriptError` |
|
|
527
|
+
| `errors.PsbtError` | `PsbtError` |
|
|
528
|
+
| `errors.EccError` | `EccError` |
|
|
529
|
+
| `errors.AddressError` | `AddressError` |
|
|
530
|
+
| `errors.SignatureError` | `SignatureError` |
|
|
531
|
+
|
|
532
|
+
---
|
|
533
|
+
|
|
534
|
+
## Error Handling Patterns
|
|
535
|
+
|
|
536
|
+
### Catching Library Errors
|
|
537
|
+
|
|
538
|
+
Since the library throws standard `Error`, `TypeError`, and `RangeError`, catch those types directly.
|
|
539
|
+
|
|
540
|
+
```typescript
|
|
541
|
+
try {
|
|
542
|
+
// ... any library operation
|
|
543
|
+
} catch (error) {
|
|
544
|
+
if (error instanceof TypeError) {
|
|
545
|
+
console.error('Type/validation error:', (error as Error).message);
|
|
546
|
+
} else if (error instanceof RangeError) {
|
|
547
|
+
console.error('Value out of range:', (error as Error).message);
|
|
548
|
+
} else if (error instanceof Error) {
|
|
549
|
+
console.error('Error:', error.message);
|
|
550
|
+
} else {
|
|
551
|
+
throw error;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
### Catching by Error Message
|
|
557
|
+
|
|
558
|
+
When you need to distinguish between different failure modes from the library, check the error message.
|
|
559
|
+
|
|
560
|
+
```typescript
|
|
561
|
+
try {
|
|
562
|
+
// ... address decoding
|
|
563
|
+
} catch (error) {
|
|
564
|
+
if (error instanceof Error) {
|
|
565
|
+
if (error.message.includes('has no matching Script')) {
|
|
566
|
+
console.error('Unrecognized address format');
|
|
567
|
+
} else if (error.message.includes('too short') || error.message.includes('too long')) {
|
|
568
|
+
console.error('Address has wrong length');
|
|
569
|
+
} else {
|
|
570
|
+
console.error('Address error:', error.message);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
```
|
|
575
|
+
|
|
576
|
+
### Wrapping Library Errors with Custom Classes
|
|
577
|
+
|
|
578
|
+
A common pattern is to catch the standard errors thrown by the library and wrap them in the exported custom error classes. This gives your application typed, categorized errors while preserving the original cause.
|
|
579
|
+
|
|
580
|
+
```typescript
|
|
581
|
+
import { address, AddressError, PsbtError, Psbt } from '@btc-vision/bitcoin';
|
|
582
|
+
import * as networks from '@btc-vision/bitcoin';
|
|
583
|
+
|
|
584
|
+
function decodeAddress(addr: string): object {
|
|
585
|
+
try {
|
|
586
|
+
return address.fromBase58Check(addr);
|
|
587
|
+
} catch (error) {
|
|
588
|
+
throw new AddressError(`Failed to decode address "${addr}": ${(error as Error).message}`);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function finalizePsbt(psbt: Psbt): void {
|
|
593
|
+
try {
|
|
594
|
+
psbt.finalizeAllInputs();
|
|
595
|
+
} catch (error) {
|
|
596
|
+
throw new PsbtError(`PSBT finalization failed: ${(error as Error).message}`);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
```
|
|
600
|
+
|
|
601
|
+
### Using the `name` Property
|
|
602
|
+
|
|
603
|
+
Every custom error class sets a `name` property matching its class name. This is useful for logging and serialization contexts where `instanceof` is not available (for example, across worker boundaries).
|
|
604
|
+
|
|
605
|
+
```typescript
|
|
606
|
+
import { BitcoinError } from '@btc-vision/bitcoin';
|
|
607
|
+
|
|
608
|
+
function handleError(error: unknown): void {
|
|
609
|
+
if (error instanceof BitcoinError) {
|
|
610
|
+
switch (error.name) {
|
|
611
|
+
case 'ValidationError':
|
|
612
|
+
// handle validation
|
|
613
|
+
break;
|
|
614
|
+
case 'PsbtError':
|
|
615
|
+
// handle PSBT
|
|
616
|
+
break;
|
|
617
|
+
default:
|
|
618
|
+
// handle other BitcoinError subtypes
|
|
619
|
+
break;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
### Building an Application Error Layer
|
|
626
|
+
|
|
627
|
+
You can combine the library's custom error classes with your own error hierarchy.
|
|
628
|
+
|
|
629
|
+
```typescript
|
|
630
|
+
import { AddressError, ValidationError } from '@btc-vision/bitcoin';
|
|
631
|
+
|
|
632
|
+
class WalletError extends Error {
|
|
633
|
+
constructor(message: string, public readonly cause?: Error) {
|
|
634
|
+
super(message);
|
|
635
|
+
this.name = 'WalletError';
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
function validateUserAddress(addr: string): void {
|
|
640
|
+
try {
|
|
641
|
+
// ... decode and validate the address using the library
|
|
642
|
+
} catch (error) {
|
|
643
|
+
if (error instanceof TypeError || error instanceof Error) {
|
|
644
|
+
// Wrap the standard library error in a domain-specific custom error
|
|
645
|
+
throw new WalletError(`Invalid address: ${addr}`, error);
|
|
646
|
+
}
|
|
647
|
+
throw error;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
```
|
|
651
|
+
|
|
652
|
+
---
|
|
653
|
+
|
|
654
|
+
## Source
|
|
655
|
+
|
|
656
|
+
All error classes are defined in `src/errors.ts` and re-exported from the package entry point.
|