@vbyte/btc-dev 2.0.0 → 2.1.0

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/LICENSE +21 -121
  3. package/README.md +36 -227
  4. package/dist/error.d.ts +11 -0
  5. package/dist/error.js +20 -0
  6. package/dist/index.d.ts +1 -0
  7. package/dist/index.js +1 -0
  8. package/dist/lib/address/api.js +3 -2
  9. package/dist/lib/address/encode.js +6 -4
  10. package/dist/lib/address/p2pkh.js +4 -4
  11. package/dist/lib/address/p2sh.js +3 -3
  12. package/dist/lib/address/p2tr.js +3 -3
  13. package/dist/lib/address/p2wpkh.js +4 -4
  14. package/dist/lib/address/p2wsh.js +3 -3
  15. package/dist/lib/address/util.js +3 -1
  16. package/dist/lib/meta/locktime.js +3 -2
  17. package/dist/lib/meta/ref.js +4 -3
  18. package/dist/lib/meta/scribe.js +26 -6
  19. package/dist/lib/meta/sequence.js +8 -7
  20. package/dist/lib/script/decode.js +10 -9
  21. package/dist/lib/script/encode.js +4 -3
  22. package/dist/lib/script/words.js +4 -3
  23. package/dist/lib/sighash/segwit.js +9 -6
  24. package/dist/lib/sighash/taproot.js +7 -4
  25. package/dist/lib/sighash/util.js +4 -3
  26. package/dist/lib/signer/sign.js +7 -6
  27. package/dist/lib/signer/verify.js +8 -9
  28. package/dist/lib/taproot/cblock.js +3 -2
  29. package/dist/lib/taproot/encode.d.ts +1 -1
  30. package/dist/lib/taproot/encode.js +4 -3
  31. package/dist/lib/taproot/parse.js +9 -7
  32. package/dist/lib/taproot/tree.js +3 -2
  33. package/dist/lib/tx/create.js +1 -1
  34. package/dist/lib/tx/decode.js +13 -11
  35. package/dist/lib/tx/encode.js +1 -1
  36. package/dist/lib/tx/parse.js +3 -3
  37. package/dist/lib/tx/size.js +2 -4
  38. package/dist/lib/tx/util.js +2 -3
  39. package/dist/lib/tx/validate.js +36 -8
  40. package/dist/lib/witness/util.js +1 -1
  41. package/dist/main.cjs +1127 -1160
  42. package/dist/main.cjs.map +1 -1
  43. package/dist/module.mjs +1125 -1161
  44. package/dist/module.mjs.map +1 -1
  45. package/dist/package.json +13 -11
  46. package/dist/script.js +10 -12
  47. package/dist/script.js.map +1 -1
  48. package/docs/API.md +1145 -0
  49. package/docs/CONVENTIONS.md +316 -0
  50. package/docs/FAQ.md +396 -0
  51. package/docs/GUIDE.md +1102 -0
  52. package/package.json +13 -11
package/CHANGELOG.md CHANGED
@@ -1,5 +1,38 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## [2.1.0] - 2026-01-26
4
+
5
+ ### License
6
+
7
+ - **Changed license to MIT**: Updated from CC-BY-1.0/CC0 to MIT license for broader compatibility.
8
+
9
+ ### New Features
10
+
11
+ - **Custom error classes**: Added `ValidationError`, `DecodingError`, and `ConfigError` for better error handling.
12
+ - `ValidationError`: Invalid input format, out of range values, type mismatches
13
+ - `DecodingError`: Malformed data during decode/parse operations
14
+ - `ConfigError`: Invalid configuration (unknown network, invalid sighash, etc.)
15
+ - **Standardized error types**: All modules now throw appropriate custom error classes instead of generic `Error`.
16
+
17
+ ### Documentation
18
+
19
+ - **Restructured security documentation**: Moved security best practices into GUIDE.md, expanded FAQ.md security section.
20
+ - **New SECURITY.md**: Added vulnerability reporting policy at project root.
21
+ - **Fixed documentation errors**: Corrected `tweak_seckey` references in GUIDE.md and FAQ.md to use `@vbyte/crypto/ecc`.
22
+
23
+ ### Packaging
24
+
25
+ - **Docs included in package**: Added `docs/` to package files for npm distribution.
26
+ - **Fixed package script**: Changed from pipe to `&&` so test failures prevent builds.
27
+
28
+ ### Testing
29
+
30
+ - Added 190 new tests (total: 1253 tests passing)
31
+ - **Custom error tests**: Full coverage for ValidationError, DecodingError, and ConfigError
32
+ - **Scribe tests**: Inscription encoding/decoding, rune labels
33
+ - **Verify-tx tests**: Multi-input transaction verification scenarios
34
+ - **Taproot tests**: Control block creation/verification, tree depth limits
35
+
3
36
  ## [2.0.0] - 2026-01-23
4
37
 
5
38
  ### Security Fixes
package/LICENSE CHANGED
@@ -1,121 +1,21 @@
1
- Creative Commons Legal Code
2
-
3
- CC0 1.0 Universal
4
-
5
- CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
6
- LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
7
- ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
8
- INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
9
- REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
10
- PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
11
- THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
12
- HEREUNDER.
13
-
14
- Statement of Purpose
15
-
16
- The laws of most jurisdictions throughout the world automatically confer
17
- exclusive Copyright and Related Rights (defined below) upon the creator
18
- and subsequent owner(s) (each and all, an "owner") of an original work of
19
- authorship and/or a database (each, a "Work").
20
-
21
- Certain owners wish to permanently relinquish those rights to a Work for
22
- the purpose of contributing to a commons of creative, cultural and
23
- scientific works ("Commons") that the public can reliably and without fear
24
- of later claims of infringement build upon, modify, incorporate in other
25
- works, reuse and redistribute as freely as possible in any form whatsoever
26
- and for any purposes, including without limitation commercial purposes.
27
- These owners may contribute to the Commons to promote the ideal of a free
28
- culture and the further production of creative, cultural and scientific
29
- works, or to gain reputation or greater distribution for their Work in
30
- part through the use and efforts of others.
31
-
32
- For these and/or other purposes and motivations, and without any
33
- expectation of additional consideration or compensation, the person
34
- associating CC0 with a Work (the "Affirmer"), to the extent that he or she
35
- is an owner of Copyright and Related Rights in the Work, voluntarily
36
- elects to apply CC0 to the Work and publicly distribute the Work under its
37
- terms, with knowledge of his or her Copyright and Related Rights in the
38
- Work and the meaning and intended legal effect of CC0 on those rights.
39
-
40
- 1. Copyright and Related Rights. A Work made available under CC0 may be
41
- protected by copyright and related or neighboring rights ("Copyright and
42
- Related Rights"). Copyright and Related Rights include, but are not
43
- limited to, the following:
44
-
45
- i. the right to reproduce, adapt, distribute, perform, display,
46
- communicate, and translate a Work;
47
- ii. moral rights retained by the original author(s) and/or performer(s);
48
- iii. publicity and privacy rights pertaining to a person's image or
49
- likeness depicted in a Work;
50
- iv. rights protecting against unfair competition in regards to a Work,
51
- subject to the limitations in paragraph 4(a), below;
52
- v. rights protecting the extraction, dissemination, use and reuse of data
53
- in a Work;
54
- vi. database rights (such as those arising under Directive 96/9/EC of the
55
- European Parliament and of the Council of 11 March 1996 on the legal
56
- protection of databases, and under any national implementation
57
- thereof, including any amended or successor version of such
58
- directive); and
59
- vii. other similar, equivalent or corresponding rights throughout the
60
- world based on applicable law or treaty, and any national
61
- implementations thereof.
62
-
63
- 2. Waiver. To the greatest extent permitted by, but not in contravention
64
- of, applicable law, Affirmer hereby overtly, fully, permanently,
65
- irrevocably and unconditionally waives, abandons, and surrenders all of
66
- Affirmer's Copyright and Related Rights and associated claims and causes
67
- of action, whether now known or unknown (including existing as well as
68
- future claims and causes of action), in the Work (i) in all territories
69
- worldwide, (ii) for the maximum duration provided by applicable law or
70
- treaty (including future time extensions), (iii) in any current or future
71
- medium and for any number of copies, and (iv) for any purpose whatsoever,
72
- including without limitation commercial, advertising or promotional
73
- purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
74
- member of the public at large and to the detriment of Affirmer's heirs and
75
- successors, fully intending that such Waiver shall not be subject to
76
- revocation, rescission, cancellation, termination, or any other legal or
77
- equitable action to disrupt the quiet enjoyment of the Work by the public
78
- as contemplated by Affirmer's express Statement of Purpose.
79
-
80
- 3. Public License Fallback. Should any part of the Waiver for any reason
81
- be judged legally invalid or ineffective under applicable law, then the
82
- Waiver shall be preserved to the maximum extent permitted taking into
83
- account Affirmer's express Statement of Purpose. In addition, to the
84
- extent the Waiver is so judged Affirmer hereby grants to each affected
85
- person a royalty-free, non transferable, non sublicensable, non exclusive,
86
- irrevocable and unconditional license to exercise Affirmer's Copyright and
87
- Related Rights in the Work (i) in all territories worldwide, (ii) for the
88
- maximum duration provided by applicable law or treaty (including future
89
- time extensions), (iii) in any current or future medium and for any number
90
- of copies, and (iv) for any purpose whatsoever, including without
91
- limitation commercial, advertising or promotional purposes (the
92
- "License"). The License shall be deemed effective as of the date CC0 was
93
- applied by Affirmer to the Work. Should any part of the License for any
94
- reason be judged legally invalid or ineffective under applicable law, such
95
- partial invalidity or ineffectiveness shall not invalidate the remainder
96
- of the License, and in such case Affirmer hereby affirms that he or she
97
- will not (i) exercise any of his or her remaining Copyright and Related
98
- Rights in the Work or (ii) assert any associated claims and causes of
99
- action with respect to the Work, in either case contrary to Affirmer's
100
- express Statement of Purpose.
101
-
102
- 4. Limitations and Disclaimers.
103
-
104
- a. No trademark or patent rights held by Affirmer are waived, abandoned,
105
- surrendered, licensed or otherwise affected by this document.
106
- b. Affirmer offers the Work as-is and makes no representations or
107
- warranties of any kind concerning the Work, express, implied,
108
- statutory or otherwise, including without limitation warranties of
109
- title, merchantability, fitness for a particular purpose, non
110
- infringement, or the absence of latent or other defects, accuracy, or
111
- the present or absence of errors, whether or not discoverable, all to
112
- the greatest extent permissible under applicable law.
113
- c. Affirmer disclaims responsibility for clearing rights of other persons
114
- that may apply to the Work or any use thereof, including without
115
- limitation any person's Copyright and Related Rights in the Work.
116
- Further, Affirmer disclaims responsibility for obtaining any necessary
117
- consents, permissions or other rights required for any use of the
118
- Work.
119
- d. Affirmer understands and acknowledges that Creative Commons is not a
120
- party to this document and has no duty or obligation with respect to
121
- this CC0 or use of the Work.
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Christopher Scott
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,18 +1,10 @@
1
1
  # @vbyte/btc-dev
2
2
 
3
- A batteries-included TypeScript toolset for Bitcoin development. Create, sign, and verify Bitcoin transactions with full support for segwit and taproot.
4
-
5
- ## Features
3
+ [![npm version](https://img.shields.io/npm/v/@vbyte/btc-dev.svg)](https://www.npmjs.com/package/@vbyte/btc-dev)
4
+ [![License: CC-BY-1.0](https://img.shields.io/badge/License-CC--BY--1.0-blue.svg)](LICENSE)
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.0+-blue.svg)](https://www.typescriptlang.org/)
6
6
 
7
- - **Full Address Support**: P2PKH, P2SH, P2WPKH, P2WSH, P2TR (taproot)
8
- - **Transaction Building**: Create, encode, decode, and parse Bitcoin transactions
9
- - **Signature Hashing**: BIP-143 (segwit) and BIP-341 (taproot) sighash calculation
10
- - **Signing & Verification**: ECDSA (segwit) and Schnorr (taproot) signatures
11
- - **Taproot Scripts**: Merkle tree construction, control blocks, and script-path spends
12
- - **Script Handling**: Encode/decode Bitcoin scripts, lock script detection
13
- - **Metadata Utilities**: Locktime, sequence (BIP-68), outpoints, rune IDs, inscription IDs
14
- - **Type Safety**: Full TypeScript support with strict types
15
- - **Tree-shakeable**: Import only what you need
7
+ A batteries-included TypeScript toolset for Bitcoin development. Create, sign, and verify Bitcoin transactions with full support for segwit and taproot.
16
8
 
17
9
  ## Installation
18
10
 
@@ -20,243 +12,60 @@ A batteries-included TypeScript toolset for Bitcoin development. Create, sign, a
20
12
  npm install @vbyte/btc-dev
21
13
  ```
22
14
 
23
- ## Quick Start
15
+ ## Quick Example
24
16
 
25
17
  ```typescript
26
18
  import { ADDRESS, TX, SIGNER } from '@vbyte/btc-dev'
27
19
 
28
- // Create a P2WPKH address from a public key
20
+ // Create a P2WPKH address
29
21
  const address = ADDRESS.p2wpkh(pubkey, 'main')
30
22
  console.log(address.data) // bc1q...
31
23
 
32
- // Parse a raw transaction
33
- const tx = TX.parse(txhex)
34
- console.log(tx.vin.length, 'inputs')
35
- console.log(tx.vout.length, 'outputs')
24
+ // Parse a transaction
25
+ const tx = TX.parse(rawTxHex)
36
26
 
37
- // Sign a segwit transaction
38
- const signature = SIGNER.sign_segwit_tx(secretKey, txdata, {
27
+ // Sign and verify
28
+ const signature = SIGNER.sign_segwit_tx(secretKey, tx, {
39
29
  txindex: 0,
40
- pubkey: publicKey,
41
- sigflag: 0x01 // SIGHASH_ALL
30
+ pubkey,
31
+ sigflag: 0x01
42
32
  })
33
+ const result = SIGNER.verify_tx(tx)
43
34
  ```
44
35
 
45
- ## Module Overview
36
+ ## Modules
46
37
 
47
38
  | Module | Description |
48
39
  |--------|-------------|
49
- | `ADDRESS` | Create and parse Bitcoin addresses (P2PKH, P2SH, P2WPKH, P2WSH, P2TR) |
50
- | `TX` | Transaction creation, encoding, decoding, parsing, and validation |
51
- | `SCRIPT` | Bitcoin script encoding, decoding, and lock script detection |
52
- | `SIGHASH` | Signature hash calculation for segwit (BIP-143) and taproot (BIP-341) |
53
- | `SIGNER` | Sign and verify transactions using ECDSA and Schnorr |
54
- | `TAPROOT` | Taproot tree construction, control blocks, and tweaking |
55
- | `WITNESS` | Parse and analyze witness data |
56
- | `META` | Locktime, sequence, outpoints, and reference IDs |
57
-
58
- ## Import Patterns
59
-
60
- ```typescript
61
- // Import all namespaces
62
- import { ADDRESS, TX, SCRIPT, SIGHASH, SIGNER, TAPROOT, WITNESS, META } from '@vbyte/btc-dev'
63
-
64
- // Tree-shaking: import specific modules
65
- import { p2wpkh, p2tr } from '@vbyte/btc-dev/address'
66
- import { parse_tx, encode_tx } from '@vbyte/btc-dev/tx'
67
- import { sign_segwit_tx, sign_taproot_tx, verify_tx } from '@vbyte/btc-dev/signer'
68
- ```
69
-
70
- ## API Highlights
71
-
72
- ### Address Creation
73
-
74
- ```typescript
75
- import { ADDRESS } from '@vbyte/btc-dev'
76
-
77
- // P2PKH (legacy)
78
- const p2pkh = ADDRESS.p2pkh(pubkey, 'main')
79
-
80
- // P2WPKH (native segwit)
81
- const p2wpkh = ADDRESS.p2wpkh(pubkey, 'main')
82
-
83
- // P2WSH (segwit script hash)
84
- const p2wsh = ADDRESS.p2wsh(redeemScript, 'main')
85
-
86
- // P2TR (taproot)
87
- const p2tr = ADDRESS.p2tr(xOnlyPubkey, 'main')
88
- ```
89
-
90
- ### Transaction Parsing
40
+ | `ADDRESS` | Create and parse addresses (P2PKH, P2SH, P2WPKH, P2WSH, P2TR) |
41
+ | `TX` | Transaction creation, encoding, decoding, parsing |
42
+ | `SCRIPT` | Script encoding, decoding, type detection |
43
+ | `SIGHASH` | Signature hash calculation (BIP-143, BIP-341) |
44
+ | `SIGNER` | Sign and verify transactions (ECDSA, Schnorr) |
45
+ | `TAPROOT` | Taproot trees, control blocks, tweaking |
46
+ | `WITNESS` | Witness data parsing and analysis |
47
+ | `META` | Locktime, sequence, reference IDs |
91
48
 
92
- ```typescript
93
- import { TX } from '@vbyte/btc-dev'
94
-
95
- // Parse raw transaction hex
96
- const tx = TX.parse(rawTxHex)
97
-
98
- // Access transaction data
99
- console.log(tx.version) // Transaction version
100
- console.log(tx.vin) // Inputs array
101
- console.log(tx.vout) // Outputs array
102
- console.log(tx.locktime) // Locktime
103
-
104
- // Encode transaction back to hex
105
- const encoded = TX.encode(tx)
106
- ```
107
-
108
- ### Signing Transactions
49
+ ## Documentation
109
50
 
110
- ```typescript
111
- import { SIGNER } from '@vbyte/btc-dev'
112
-
113
- // Sign segwit (v0) transaction input
114
- const segwitSig = SIGNER.sign_segwit_tx(secretKey, txdata, {
115
- txindex: 0, // Input index to sign
116
- pubkey: compressedPubkey, // For P2WPKH
117
- sigflag: 0x01 // SIGHASH_ALL
118
- })
51
+ - **[Guide](docs/GUIDE.md)** - Complete tutorial covering addresses, transactions, signing, and taproot
52
+ - **[API Reference](docs/API.md)** - Full function reference for all modules
53
+ - **[Security](docs/SECURITY.md)** - Best practices for production use
54
+ - **[FAQ](docs/FAQ.md)** - Common questions and troubleshooting
55
+ - **[Conventions](docs/CONVENTIONS.md)** - Coding conventions for contributors
119
56
 
120
- // Sign taproot (v1) transaction input
121
- const taprootSig = SIGNER.sign_taproot_tx(secretKey, txdata, {
122
- txindex: 0,
123
- sigflag: 0x00 // SIGHASH_DEFAULT (taproot)
124
- })
125
-
126
- // Verify all signatures in a transaction
127
- const result = SIGNER.verify_tx(txdata)
128
- if (result.valid) {
129
- console.log('All signatures valid')
130
- } else {
131
- console.log('Verification failed:', result.error)
132
- result.inputs.forEach(input => {
133
- if (!input.valid) console.log(`Input ${input.index}: ${input.error}`)
134
- })
135
- }
136
- ```
137
-
138
- ### Taproot Scripts
139
-
140
- ```typescript
141
- import { TAPROOT } from '@vbyte/btc-dev'
142
-
143
- // Create a taproot output with scripts
144
- const taprootCtx = TAPROOT.create({
145
- pubkey: internalPubkey,
146
- leaves: [tapleaf1, tapleaf2] // Script leaves
147
- })
57
+ ## Contributing
148
58
 
149
- console.log(taprootCtx.tapkey) // Tweaked public key
150
- console.log(taprootCtx.cblock) // Control block for script-path
151
- console.log(taprootCtx.taptweak) // Tweak value
152
-
153
- // Verify control block
154
- const isValid = TAPROOT.verify(tapkey, target, cblock)
155
- ```
156
-
157
- ### Script Detection
158
-
159
- ```typescript
160
- import { SCRIPT } from '@vbyte/btc-dev'
161
-
162
- // Detect lock script type
163
- const type = SCRIPT.get_lock_script_type(scriptPubKey)
164
- // Returns: 'p2pkh' | 'p2sh' | 'p2wpkh' | 'p2wsh' | 'p2tr' | 'opreturn' | null
165
-
166
- // Check specific types
167
- SCRIPT.is_p2wpkh_script(script) // true/false
168
- SCRIPT.is_p2tr_script(script) // true/false
169
-
170
- // Get witness version
171
- const version = SCRIPT.get_lock_script_version(script)
172
- // Returns: 0 (segwit v0), 1 (taproot), or null (legacy)
173
- ```
174
-
175
- ### Witness Parsing
176
-
177
- ```typescript
178
- import { WITNESS } from '@vbyte/btc-dev'
179
-
180
- // Parse witness data from transaction input
181
- const witnessData = WITNESS.parse(witness)
182
-
183
- console.log(witnessData.type) // 'p2wpkh' | 'p2wsh' | 'p2tr' | 'p2ts' | null
184
- console.log(witnessData.version) // 0 | 1 | null
185
- console.log(witnessData.params) // Signature and other params
186
- console.log(witnessData.script) // Witness script (if p2wsh/p2ts)
187
- console.log(witnessData.cblock) // Control block (if p2ts)
188
- console.log(witnessData.annex) // Annex data (if present)
189
- ```
190
-
191
- ### Metadata Utilities
192
-
193
- ```typescript
194
- import { META } from '@vbyte/btc-dev'
195
-
196
- // Encode/decode locktime (BIP-65)
197
- const locktime = META.encode_locktime({ type: 'heightlock', height: 800000 })
198
- const decoded = META.decode_locktime(locktime)
199
-
200
- // Encode/decode sequence (BIP-68)
201
- const sequence = META.encode_sequence({ mode: 'height', height: 144 }) // ~24 hours
202
- const decodedSeq = META.decode_sequence(sequence)
203
-
204
- // Reference pointers
205
- const outpoint = META.RefPointer.outpoint.encode(txid, vout)
206
- const inscriptionId = META.RefPointer.record_id.encode(txid, 0)
207
- const runeId = META.RefPointer.rune_id.encode(840000, 15)
208
- ```
209
-
210
- ## Security Considerations
211
-
212
- - Always validate inputs before signing transactions
213
- - Never log or expose secret keys
214
- - Use secure random number generation for key generation
215
- - Verify signatures after signing to ensure correctness
216
- - Be aware of sighash flag implications (ANYONECANPAY, SINGLE, NONE)
217
-
218
- See [SECURITY.md](./docs/SECURITY.md) for detailed security guidelines.
219
-
220
- ## Type Definitions
221
-
222
- All types are exported from the main module:
223
-
224
- ```typescript
225
- import type {
226
- TxData,
227
- TxInput,
228
- TxOutput,
229
- SigHashOptions,
230
- AddressData,
231
- WitnessData,
232
- TaprootContext
233
- } from '@vbyte/btc-dev'
234
- ```
59
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup and guidelines.
235
60
 
236
61
  ## Dependencies
237
62
 
238
- - `@noble/curves` - Elliptic curve cryptography
239
- - `@noble/hashes` - Cryptographic hash functions
240
- - `@scure/btc-signer` - Bitcoin signing utilities
241
- - `@vbyte/buff` - Buffer manipulation utilities
242
- - `zod` - Runtime validation schemas
243
-
244
- ## Development
245
-
246
- ```bash
247
- # Run tests
248
- npm test
249
-
250
- # Run specific test file
251
- npm run script test/path/to/file.ts
252
-
253
- # Build the package
254
- npm run build
255
-
256
- # Build and test
257
- npm run package
258
- ```
63
+ - [@noble/curves](https://github.com/paulmillr/noble-curves) - Elliptic curve cryptography
64
+ - [@noble/hashes](https://github.com/paulmillr/noble-hashes) - Cryptographic hash functions
65
+ - [@scure/btc-signer](https://github.com/paulmillr/scure-btc-signer) - Bitcoin signing utilities
66
+ - [@vbyte/buff](https://www.npmjs.com/package/@vbyte/buff) - Buffer manipulation
67
+ - [zod](https://zod.dev) - Runtime validation
259
68
 
260
69
  ## License
261
70
 
262
- CC-BY-1.0
71
+ [CC-BY-1.0](LICENSE)
@@ -0,0 +1,11 @@
1
+ export declare class ValidationError extends Error {
2
+ field?: string;
3
+ constructor(message: string, field?: string);
4
+ }
5
+ export declare class DecodingError extends Error {
6
+ position?: number;
7
+ constructor(message: string, position?: number);
8
+ }
9
+ export declare class ConfigError extends Error {
10
+ constructor(message: string);
11
+ }
package/dist/error.js ADDED
@@ -0,0 +1,20 @@
1
+ export class ValidationError extends Error {
2
+ constructor(message, field) {
3
+ super(message);
4
+ this.name = "ValidationError";
5
+ this.field = field;
6
+ }
7
+ }
8
+ export class DecodingError extends Error {
9
+ constructor(message, position) {
10
+ super(message);
11
+ this.name = "DecodingError";
12
+ this.position = position;
13
+ }
14
+ }
15
+ export class ConfigError extends Error {
16
+ constructor(message) {
17
+ super(message);
18
+ this.name = "ConfigError";
19
+ }
20
+ }
package/dist/index.d.ts CHANGED
@@ -8,4 +8,5 @@ export * as TAPROOT from "./lib/taproot/index.js";
8
8
  export * as TX from "./lib/tx/index.js";
9
9
  export * as WITNESS from "./lib/witness/index.js";
10
10
  export * as SCHEMA from "./schema/index.js";
11
+ export { ValidationError, DecodingError, ConfigError } from "./error.js";
11
12
  export type * from "./types/index.js";
package/dist/index.js CHANGED
@@ -8,3 +8,4 @@ export * as TAPROOT from "./lib/taproot/index.js";
8
8
  export * as TX from "./lib/tx/index.js";
9
9
  export * as WITNESS from "./lib/witness/index.js";
10
10
  export * as SCHEMA from "./schema/index.js";
11
+ export { ValidationError, DecodingError, ConfigError } from "./error.js";
@@ -1,5 +1,6 @@
1
1
  import { Buff } from "@vbyte/buff";
2
2
  import { LOCK_SCRIPT_TYPE } from "../../const.js";
3
+ import { ConfigError } from "../../error.js";
3
4
  import { get_lock_script_type } from "../../lib/script/lock.js";
4
5
  import { P2PKH } from "./p2pkh.js";
5
6
  import { P2SH } from "./p2sh.js";
@@ -11,7 +12,7 @@ export function get_address(script, network = "main") {
11
12
  const bytes = Buff.bytes(script);
12
13
  const type = get_lock_script_type(bytes);
13
14
  if (type === null)
14
- throw new Error("Unknown or unsupported locking script type");
15
+ throw new ConfigError("Unknown or unsupported locking script type");
15
16
  switch (type) {
16
17
  case LOCK_SCRIPT_TYPE.P2PKH:
17
18
  return P2PKH.encode_address(script, network);
@@ -24,7 +25,7 @@ export function get_address(script, network = "main") {
24
25
  case LOCK_SCRIPT_TYPE.P2TR:
25
26
  return P2TR.encode_address(script, network);
26
27
  default:
27
- throw new Error(`unknown script type: ${type}`);
28
+ throw new ConfigError(`unknown script type: ${type}`);
28
29
  }
29
30
  }
30
31
  export function parse_address(address) {
@@ -1,5 +1,7 @@
1
1
  import { Buff } from "@vbyte/buff";
2
- import { Assert, B58chk, Bech32, Bech32m } from "@vbyte/micro-lib";
2
+ import { Assert } from "@vbyte/util";
3
+ import { B58chk, Bech32, Bech32m } from "@vbyte/crypto";
4
+ import { ValidationError } from "../../error.js";
3
5
  const ENCODING_REGEX = {
4
6
  base58: /^[13mn2][a-km-zA-HJ-NP-Z1-9]{25,34}$/,
5
7
  bech32: /^(bc|tb|bcrt)1q[ac-hj-np-z02-9]{6,87}$/,
@@ -12,14 +14,14 @@ const VERSION = {
12
14
  export function decode_address(address) {
13
15
  const format = get_address_format(address);
14
16
  if (format === null)
15
- throw new Error(`unrecognized address format: ${address}`);
17
+ throw new ValidationError(`unrecognized address format: ${address}`, "address");
16
18
  if (format === "base58")
17
19
  return base58_decode(address);
18
20
  if (format === "bech32")
19
21
  return bech32_decode(address);
20
22
  if (format === "bech32m")
21
23
  return bech32m_decode(address);
22
- throw new Error("unable to find a matching address configuration");
24
+ throw new ValidationError("unable to find a matching address configuration", "address");
23
25
  }
24
26
  export function encode_address(config) {
25
27
  if (config.format === "base58")
@@ -28,7 +30,7 @@ export function encode_address(config) {
28
30
  return bech32_encode(config);
29
31
  if (config.format === "bech32m")
30
32
  return bech32m_encode(config);
31
- throw new Error(`unrecognized encoding format: ${config.format}`);
33
+ throw new ValidationError(`unrecognized encoding format: ${config.format}`, "format");
32
34
  }
33
35
  function get_address_format(address) {
34
36
  for (const [format, regex] of Object.entries(ENCODING_REGEX)) {
@@ -1,6 +1,6 @@
1
1
  import { Buff } from "@vbyte/buff";
2
- import { Assert } from "@vbyte/micro-lib";
3
- import { hash160 } from "@vbyte/micro-lib/hash";
2
+ import { Assert } from "@vbyte/util";
3
+ import { hash160 } from "@vbyte/crypto/hash";
4
4
  import { LOCK_SCRIPT_TYPE } from "../../const.js";
5
5
  import { is_p2pkh_script } from "../../lib/script/lock.js";
6
6
  import { encode_address } from "./encode.js";
@@ -21,7 +21,7 @@ function create_p2pkh_address(pubkey, network = "main") {
21
21
  }
22
22
  function create_p2pkh_script(pubkey) {
23
23
  const bytes = Buff.bytes(pubkey);
24
- Assert.size(bytes, 33, "invalid pubkey size");
24
+ Assert.ok(bytes.length === 33, "invalid pubkey size");
25
25
  const hash = hash160(bytes);
26
26
  return encode_p2pkh_script(hash);
27
27
  }
@@ -32,7 +32,7 @@ function encode_p2pkh_address(script, network = "main") {
32
32
  const pk_hash = decode_p2pkh_script(script);
33
33
  const config = get_address_config(network, ADDRESS_TYPE);
34
34
  Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}`);
35
- Assert.size(pk_hash, config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}`);
35
+ Assert.ok(pk_hash.length === config.size, `invalid payload size: ${pk_hash.length} !== ${config.size}`);
36
36
  return encode_address({
37
37
  data: pk_hash,
38
38
  format: "base58",
@@ -1,6 +1,6 @@
1
1
  import { Buff } from "@vbyte/buff";
2
- import { Assert } from "@vbyte/micro-lib";
3
- import { hash160 } from "@vbyte/micro-lib/hash";
2
+ import { Assert } from "@vbyte/util";
3
+ import { hash160 } from "@vbyte/crypto/hash";
4
4
  import { LOCK_SCRIPT_TYPE } from "../../const.js";
5
5
  import { is_p2sh_script } from "../../lib/script/lock.js";
6
6
  import { encode_address } from "./encode.js";
@@ -33,7 +33,7 @@ function encode_p2sh_address(script_pk, network = "main") {
33
33
  const script_hash = decode_p2sh_script(script_pk);
34
34
  const config = get_address_config(network, ADDRESS_TYPE);
35
35
  Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}`);
36
- Assert.size(script_hash, config.size, `invalid payload size: ${script_hash.length} !== ${config.size}`);
36
+ Assert.ok(script_hash.length === config.size, `invalid payload size: ${script_hash.length} !== ${config.size}`);
37
37
  return encode_address({
38
38
  data: script_hash,
39
39
  format: "base58",
@@ -1,5 +1,5 @@
1
1
  import { Buff } from "@vbyte/buff";
2
- import { Assert } from "@vbyte/micro-lib";
2
+ import { Assert } from "@vbyte/util";
3
3
  import { LOCK_SCRIPT_TYPE } from "../../const.js";
4
4
  import { is_p2tr_script } from "../../lib/script/lock.js";
5
5
  import { encode_address } from "./encode.js";
@@ -20,7 +20,7 @@ function create_p2tr_address(pubkey, network = "main") {
20
20
  }
21
21
  function create_p2tr_script(pubkey) {
22
22
  const bytes = Buff.bytes(pubkey);
23
- Assert.size(bytes, 32, "invalid pubkey size");
23
+ Assert.ok(bytes.length === 32, "invalid pubkey size");
24
24
  return encode_p2tr_script(bytes);
25
25
  }
26
26
  function encode_p2tr_script(pubkey) {
@@ -30,7 +30,7 @@ function encode_p2tr_address(script_pk, network = "main") {
30
30
  const pubkey = decode_p2tr_script(script_pk);
31
31
  const config = get_address_config(network, ADDRESS_TYPE);
32
32
  Assert.exists(config, `unrecognized address config: ${ADDRESS_TYPE} on ${network}`);
33
- Assert.size(pubkey, config.size, `invalid payload size: ${pubkey.length} !== ${config.size}`);
33
+ Assert.ok(pubkey.length === config.size, `invalid payload size: ${pubkey.length} !== ${config.size}`);
34
34
  return encode_address({
35
35
  data: pubkey,
36
36
  format: "bech32m",