@vbyte/btc-dev 1.1.8 → 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 (181) hide show
  1. package/CHANGELOG.md +127 -0
  2. package/LICENSE +21 -121
  3. package/README.md +69 -3
  4. package/dist/const.d.ts +3 -0
  5. package/dist/const.js +23 -22
  6. package/dist/error.d.ts +11 -0
  7. package/dist/error.js +20 -0
  8. package/dist/index.d.ts +12 -11
  9. package/dist/index.js +11 -10
  10. package/dist/lib/address/api.d.ts +2 -2
  11. package/dist/lib/address/api.js +13 -12
  12. package/dist/lib/address/encode.d.ts +1 -1
  13. package/dist/lib/address/encode.js +26 -24
  14. package/dist/lib/address/index.d.ts +6 -6
  15. package/dist/lib/address/index.js +6 -6
  16. package/dist/lib/address/p2pkh.d.ts +2 -2
  17. package/dist/lib/address/p2pkh.js +15 -15
  18. package/dist/lib/address/p2sh.d.ts +2 -2
  19. package/dist/lib/address/p2sh.js +14 -14
  20. package/dist/lib/address/p2tr.d.ts +2 -2
  21. package/dist/lib/address/p2tr.js +14 -14
  22. package/dist/lib/address/p2wpkh.d.ts +2 -2
  23. package/dist/lib/address/p2wpkh.js +15 -15
  24. package/dist/lib/address/p2wsh.d.ts +2 -2
  25. package/dist/lib/address/p2wsh.js +14 -14
  26. package/dist/lib/address/script.d.ts +1 -1
  27. package/dist/lib/address/script.js +16 -16
  28. package/dist/lib/address/util.d.ts +1 -1
  29. package/dist/lib/address/util.js +24 -22
  30. package/dist/lib/meta/index.d.ts +4 -4
  31. package/dist/lib/meta/index.js +4 -4
  32. package/dist/lib/meta/locktime.d.ts +1 -1
  33. package/dist/lib/meta/locktime.js +13 -12
  34. package/dist/lib/meta/ref.js +13 -9
  35. package/dist/lib/meta/scribe.d.ts +2 -2
  36. package/dist/lib/meta/scribe.js +71 -56
  37. package/dist/lib/meta/sequence.d.ts +1 -1
  38. package/dist/lib/meta/sequence.js +21 -19
  39. package/dist/lib/script/decode.d.ts +2 -2
  40. package/dist/lib/script/decode.js +53 -17
  41. package/dist/lib/script/encode.d.ts +1 -1
  42. package/dist/lib/script/encode.js +21 -16
  43. package/dist/lib/script/index.d.ts +5 -13
  44. package/dist/lib/script/index.js +5 -14
  45. package/dist/lib/script/lock.d.ts +2 -2
  46. package/dist/lib/script/lock.js +15 -12
  47. package/dist/lib/script/util.js +4 -4
  48. package/dist/lib/script/words.js +131 -130
  49. package/dist/lib/sighash/index.d.ts +3 -3
  50. package/dist/lib/sighash/index.js +3 -3
  51. package/dist/lib/sighash/segwit.d.ts +2 -2
  52. package/dist/lib/sighash/segwit.js +18 -14
  53. package/dist/lib/sighash/taproot.d.ts +2 -2
  54. package/dist/lib/sighash/taproot.js +27 -23
  55. package/dist/lib/sighash/util.d.ts +2 -2
  56. package/dist/lib/sighash/util.js +8 -7
  57. package/dist/lib/signer/index.d.ts +2 -2
  58. package/dist/lib/signer/index.js +2 -2
  59. package/dist/lib/signer/sign.d.ts +1 -1
  60. package/dist/lib/signer/sign.js +43 -7
  61. package/dist/lib/signer/verify.d.ts +17 -3
  62. package/dist/lib/signer/verify.js +232 -3
  63. package/dist/lib/taproot/cblock.d.ts +1 -1
  64. package/dist/lib/taproot/cblock.js +16 -17
  65. package/dist/lib/taproot/encode.d.ts +1 -1
  66. package/dist/lib/taproot/encode.js +9 -8
  67. package/dist/lib/taproot/index.d.ts +4 -4
  68. package/dist/lib/taproot/index.js +4 -4
  69. package/dist/lib/taproot/parse.d.ts +1 -1
  70. package/dist/lib/taproot/parse.js +15 -15
  71. package/dist/lib/taproot/tree.d.ts +2 -2
  72. package/dist/lib/taproot/tree.js +12 -7
  73. package/dist/lib/tx/create.d.ts +1 -1
  74. package/dist/lib/tx/create.js +28 -12
  75. package/dist/lib/tx/decode.d.ts +2 -2
  76. package/dist/lib/tx/decode.js +52 -17
  77. package/dist/lib/tx/encode.d.ts +2 -2
  78. package/dist/lib/tx/encode.js +13 -16
  79. package/dist/lib/tx/index.d.ts +7 -7
  80. package/dist/lib/tx/index.js +7 -7
  81. package/dist/lib/tx/parse.d.ts +1 -1
  82. package/dist/lib/tx/parse.js +9 -9
  83. package/dist/lib/tx/size.d.ts +2 -2
  84. package/dist/lib/tx/size.js +9 -11
  85. package/dist/lib/tx/util.d.ts +2 -2
  86. package/dist/lib/tx/util.js +22 -20
  87. package/dist/lib/tx/validate.d.ts +1 -1
  88. package/dist/lib/tx/validate.js +37 -9
  89. package/dist/lib/witness/index.d.ts +2 -2
  90. package/dist/lib/witness/index.js +2 -2
  91. package/dist/lib/witness/parse.d.ts +2 -2
  92. package/dist/lib/witness/parse.js +24 -23
  93. package/dist/lib/witness/util.d.ts +2 -2
  94. package/dist/lib/witness/util.js +5 -5
  95. package/dist/main.cjs +3305 -2035
  96. package/dist/main.cjs.map +1 -1
  97. package/dist/module.mjs +3303 -2036
  98. package/dist/module.mjs.map +1 -1
  99. package/dist/package.json +24 -19
  100. package/dist/schema/base.d.ts +1 -1
  101. package/dist/schema/base.js +17 -13
  102. package/dist/schema/index.d.ts +2 -2
  103. package/dist/schema/index.js +2 -2
  104. package/dist/schema/taproot.d.ts +1 -1
  105. package/dist/schema/taproot.js +2 -2
  106. package/dist/schema/tx.d.ts +1 -1
  107. package/dist/schema/tx.js +4 -4
  108. package/dist/script.js +10 -12
  109. package/dist/script.js.map +1 -1
  110. package/dist/types/address.d.ts +4 -4
  111. package/dist/types/index.d.ts +8 -8
  112. package/dist/types/index.js +8 -8
  113. package/dist/types/meta.d.ts +4 -4
  114. package/dist/types/psbt.d.ts +2 -2
  115. package/dist/types/script.d.ts +2 -2
  116. package/dist/types/sighash.d.ts +2 -2
  117. package/dist/types/witness.d.ts +5 -5
  118. package/docs/API.md +1145 -0
  119. package/docs/CONVENTIONS.md +316 -0
  120. package/docs/FAQ.md +396 -0
  121. package/docs/GUIDE.md +1102 -0
  122. package/package.json +24 -19
  123. package/src/const.ts +0 -61
  124. package/src/index.ts +0 -13
  125. package/src/lib/address/api.ts +0 -50
  126. package/src/lib/address/encode.ts +0 -183
  127. package/src/lib/address/index.ts +0 -7
  128. package/src/lib/address/p2pkh.ts +0 -94
  129. package/src/lib/address/p2sh.ts +0 -96
  130. package/src/lib/address/p2tr.ts +0 -91
  131. package/src/lib/address/p2wpkh.ts +0 -94
  132. package/src/lib/address/p2wsh.ts +0 -92
  133. package/src/lib/address/script.ts +0 -63
  134. package/src/lib/address/util.ts +0 -87
  135. package/src/lib/meta/index.ts +0 -4
  136. package/src/lib/meta/locktime.ts +0 -57
  137. package/src/lib/meta/ref.ts +0 -107
  138. package/src/lib/meta/scribe.ts +0 -256
  139. package/src/lib/meta/sequence.ts +0 -146
  140. package/src/lib/script/decode.ts +0 -85
  141. package/src/lib/script/encode.ts +0 -129
  142. package/src/lib/script/index.ts +0 -20
  143. package/src/lib/script/lock.ts +0 -73
  144. package/src/lib/script/util.ts +0 -78
  145. package/src/lib/script/words.ts +0 -182
  146. package/src/lib/sighash/index.ts +0 -3
  147. package/src/lib/sighash/segwit.ts +0 -152
  148. package/src/lib/sighash/taproot.ts +0 -206
  149. package/src/lib/sighash/util.ts +0 -51
  150. package/src/lib/signer/index.ts +0 -2
  151. package/src/lib/signer/sign.ts +0 -39
  152. package/src/lib/signer/verify.ts +0 -88
  153. package/src/lib/taproot/cblock.ts +0 -96
  154. package/src/lib/taproot/encode.ts +0 -49
  155. package/src/lib/taproot/index.ts +0 -4
  156. package/src/lib/taproot/parse.ts +0 -65
  157. package/src/lib/taproot/tree.ts +0 -94
  158. package/src/lib/tx/create.ts +0 -90
  159. package/src/lib/tx/decode.ts +0 -123
  160. package/src/lib/tx/encode.ts +0 -155
  161. package/src/lib/tx/index.ts +0 -7
  162. package/src/lib/tx/parse.ts +0 -69
  163. package/src/lib/tx/size.ts +0 -68
  164. package/src/lib/tx/util.ts +0 -111
  165. package/src/lib/tx/validate.ts +0 -49
  166. package/src/lib/witness/index.ts +0 -2
  167. package/src/lib/witness/parse.ts +0 -127
  168. package/src/lib/witness/util.ts +0 -18
  169. package/src/schema/base.ts +0 -57
  170. package/src/schema/index.ts +0 -2
  171. package/src/schema/taproot.ts +0 -12
  172. package/src/schema/tx.ts +0 -48
  173. package/src/types/address.ts +0 -35
  174. package/src/types/index.ts +0 -8
  175. package/src/types/meta.ts +0 -48
  176. package/src/types/psbt.ts +0 -15
  177. package/src/types/script.ts +0 -18
  178. package/src/types/sighash.ts +0 -16
  179. package/src/types/taproot.ts +0 -41
  180. package/src/types/txdata.ts +0 -85
  181. package/src/types/witness.ts +0 -42
package/CHANGELOG.md ADDED
@@ -0,0 +1,127 @@
1
+ # CHANGELOG
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
+
36
+ ## [2.0.0] - 2026-01-23
37
+
38
+ ### Security Fixes
39
+
40
+ - **Fixed witness parsing bug**: Added missing `break` statements in switch block in `src/lib/witness/parse.ts` that could cause incorrect script extraction for p2ts and p2wsh witness types.
41
+ - **Implemented `verify_tx()` function**: Full signature verification for segwit (ECDSA) and taproot (Schnorr) transactions, supporting all sighash types.
42
+ - **Added input validation to sign functions**: Secret key format validation in `sign_segwit_tx()` and `sign_taproot_tx()` to prevent invalid key errors.
43
+ - **Added transaction size limits**: Maximum transaction size (4MB), varint size (10MB), and element count (100k) limits in decoder to prevent memory exhaustion attacks.
44
+ - **Added taproot tree depth limits**: Maximum depth of 128 levels in merkle tree construction to prevent stack overflow from deeply nested trees.
45
+ - **Sanitized error messages**: Removed sensitive data from error messages in address and transaction modules.
46
+ - **Fixed taproot verification null check**: Added proper null check for `prevout` in taproot input verification to prevent runtime errors.
47
+
48
+ ### New Features
49
+
50
+ - **Full signature verification**: `verify_tx()` now returns detailed verification results including per-input status and error messages.
51
+ - **Enhanced witness parsing**: Improved annex detection and control block parsing for taproot witnesses.
52
+
53
+ ### Code Quality
54
+
55
+ - **Biome linting compliance**: Applied comprehensive linting fixes across all 59 source files in `src/`
56
+ - Converted string concatenation to template literals
57
+ - Added explicit radix parameter to all `parseInt()` calls
58
+ - Converted `import` statements to `import type` where appropriate
59
+ - Replaced `isNaN()` with `Number.isNaN()` for type safety
60
+ - Simplified computed property access to literal keys
61
+ - Standardized code formatting (quotes, semicolons, indentation)
62
+ - **Improved control block construction**: Replaced `forEach` with spread operator in taproot control block building
63
+
64
+ ### Testing
65
+
66
+ - Added 843+ new tests (total: 1063 tests passing)
67
+ - **SIGNER module tests**: Essential signing function tests, transaction scenarios, and sighash coverage
68
+ - **WITNESS module tests**: Full witness parsing coverage (p2wpkh, p2wsh, p2tr, p2ts, annex) plus edge cases
69
+ - **SCRIPT module tests**: Lock script detection, decode/encode roundtrips, malformed script handling, size limits
70
+ - **SIGHASH module tests**: Bounds checking and edge cases
71
+ - **META module tests**: BIP-65 locktime, BIP-68 sequence, and reference pointer encoding/decoding
72
+ - **TX module tests**: Size calculation, essential operations, create functions, encoding functions, error handling
73
+ - **TAPROOT module tests**: Parse operations coverage
74
+ - **Integration tests**: End-to-end workflows for address creation, transaction building, and signing
75
+
76
+ ### Documentation
77
+
78
+ - **README.md**: Expanded with installation instructions, quick start guide, module overview, and API examples
79
+ - **SECURITY.md**: New security guidelines document covering private key handling, input validation, and best practices
80
+ - **EXAMPLES.md**: New examples document with practical code for common Bitcoin development tasks
81
+ - **CONVENTIONS.md**: Added project coding conventions documentation
82
+ - **JSDoc**: Added comprehensive documentation to exported functions with @param, @returns, @throws, and @example
83
+ - **Type definitions**: Documented all interfaces in `src/types/`
84
+ - **Schemas**: Added inline documentation for validation schemas
85
+
86
+ ### Breaking Changes
87
+
88
+ - `verify_tx()` now returns a `VerifyResult` object instead of a boolean. Access the `.valid` property for boolean result.
89
+
90
+ ## [1.1.8]
91
+
92
+ - Removed excess logging.
93
+
94
+ ## [1.1.7]
95
+
96
+ - Removed schema dependency.
97
+ - Fixed an issue with the transaction encoder.
98
+
99
+ ## [1.1.6]
100
+
101
+ - Changed `create_address` to `get_address`, fixed issues with interface.
102
+
103
+ ## [1.1.5]
104
+
105
+ - Updated exports for Script module.
106
+
107
+ ## [1.1.4]
108
+
109
+ - Updates to API for Address module.
110
+ - Updates to API for Script module.
111
+ - Updates to API for TX module.
112
+
113
+ ## [1.1.3]
114
+
115
+ - Updated transaction utils to have a better input interface.
116
+
117
+ ## [1.1.2]
118
+
119
+ - Methods `get_tx_output_type` and `get_tx_output_version` have been updated and replaced with `get_script_pk_type` and `get_script_pk_version`.
120
+
121
+ ## [1.1.1]
122
+
123
+ - Updated `encode_script`
124
+
125
+ ## [1.1.0]
126
+
127
+ - Public release.
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,5 +1,71 @@
1
- # btc-dev
1
+ # @vbyte/btc-dev
2
2
 
3
- Experimental revision of the bitcoin [tapscript](https://www.npmjs.com/package/@cmdcode/tapscript) library.
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/)
4
6
 
5
- Documentation coming soon!
7
+ A batteries-included TypeScript toolset for Bitcoin development. Create, sign, and verify Bitcoin transactions with full support for segwit and taproot.
8
+
9
+ ## Installation
10
+
11
+ ```bash
12
+ npm install @vbyte/btc-dev
13
+ ```
14
+
15
+ ## Quick Example
16
+
17
+ ```typescript
18
+ import { ADDRESS, TX, SIGNER } from '@vbyte/btc-dev'
19
+
20
+ // Create a P2WPKH address
21
+ const address = ADDRESS.p2wpkh(pubkey, 'main')
22
+ console.log(address.data) // bc1q...
23
+
24
+ // Parse a transaction
25
+ const tx = TX.parse(rawTxHex)
26
+
27
+ // Sign and verify
28
+ const signature = SIGNER.sign_segwit_tx(secretKey, tx, {
29
+ txindex: 0,
30
+ pubkey,
31
+ sigflag: 0x01
32
+ })
33
+ const result = SIGNER.verify_tx(tx)
34
+ ```
35
+
36
+ ## Modules
37
+
38
+ | Module | Description |
39
+ |--------|-------------|
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 |
48
+
49
+ ## Documentation
50
+
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
56
+
57
+ ## Contributing
58
+
59
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup and guidelines.
60
+
61
+ ## Dependencies
62
+
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
68
+
69
+ ## License
70
+
71
+ [CC-BY-1.0](LICENSE)
package/dist/const.d.ts CHANGED
@@ -36,3 +36,6 @@ export declare const TX_SIZE: {
36
36
  export declare const SIGHASH_DEFAULT = 1;
37
37
  export declare const SIGHASH_SEGWIT: number[];
38
38
  export declare const SIGHASH_TAPROOT: number[];
39
+ export declare const MAX_SCRIPT_SIZE = 10000;
40
+ export declare const MAX_VARINT_SIZE = 520000;
41
+ export declare const OP_1_OFFSET = 80;
package/dist/const.js CHANGED
@@ -1,36 +1,34 @@
1
1
  export const COINBASE = {
2
- TXID: '00'.repeat(32),
3
- VOUT: 0xFFFFFFFF,
2
+ TXID: "00".repeat(32),
3
+ VOUT: 0xffffffff,
4
4
  };
5
5
  export const DEFAULT = {
6
6
  LOCKTIME: 0,
7
- SEQUENCE: 0xFFFFFFFF,
7
+ SEQUENCE: 0xffffffff,
8
8
  VERSION: 2,
9
9
  };
10
10
  export const TAPLEAF_VERSIONS = [
11
- 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce,
12
- 0xd0, 0xd2, 0xd4, 0xd6, 0xd8, 0xda, 0xdc, 0xde,
13
- 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee,
14
- 0xf0, 0xf2, 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe,
15
- 0x66, 0x7e, 0x80, 0x84, 0x96, 0x98, 0xba, 0xbc,
16
- 0xbe
11
+ 0xc0, 0xc2, 0xc4, 0xc6, 0xc8, 0xca, 0xcc, 0xce, 0xd0, 0xd2, 0xd4, 0xd6, 0xd8,
12
+ 0xda, 0xdc, 0xde, 0xe0, 0xe2, 0xe4, 0xe6, 0xe8, 0xea, 0xec, 0xee, 0xf0, 0xf2,
13
+ 0xf4, 0xf6, 0xf8, 0xfa, 0xfc, 0xfe, 0x66, 0x7e, 0x80, 0x84, 0x96, 0x98, 0xba,
14
+ 0xbc, 0xbe,
17
15
  ];
18
16
  export const TAPLEAF_DEFAULT_VERSION = 0xc0;
19
17
  export const LOCK_SCRIPT_TYPE = {
20
- P2PKH: 'p2pkh',
21
- P2SH: 'p2sh',
22
- P2WPKH: 'p2wpkh',
23
- P2WSH: 'p2wsh',
24
- P2TR: 'p2tr',
25
- OPRETURN: 'opreturn',
18
+ P2PKH: "p2pkh",
19
+ P2SH: "p2sh",
20
+ P2WPKH: "p2wpkh",
21
+ P2WSH: "p2wsh",
22
+ P2TR: "p2tr",
23
+ OPRETURN: "opreturn",
26
24
  };
27
25
  export const SPEND_SCRIPT_TYPE = {
28
- P2PKH: 'p2pkh',
29
- P2SH: 'p2sh',
30
- P2WPKH: 'p2wpkh',
31
- P2WSH: 'p2wsh',
32
- P2TR: 'p2tr',
33
- P2TS: 'p2ts',
26
+ P2PKH: "p2pkh",
27
+ P2SH: "p2sh",
28
+ P2WPKH: "p2wpkh",
29
+ P2WSH: "p2wsh",
30
+ P2TR: "p2tr",
31
+ P2TS: "p2ts",
34
32
  };
35
33
  export const LOCK_SCRIPT_REGEX = {
36
34
  [LOCK_SCRIPT_TYPE.P2PKH]: /^76a914[0-9a-f]{40}88ac$/i,
@@ -40,7 +38,7 @@ export const LOCK_SCRIPT_REGEX = {
40
38
  [LOCK_SCRIPT_TYPE.P2TR]: /^5120[0-9a-f]{64}$/i,
41
39
  [LOCK_SCRIPT_TYPE.OPRETURN]: /^6a[0-9a-f]{2,}$/i,
42
40
  };
43
- export const SCRIPT_INT_KEY = '';
41
+ export const SCRIPT_INT_KEY = "";
44
42
  export const TX_SIZE = {
45
43
  GLOBAL_BASE: 8,
46
44
  GLOBAL_WIT: 10,
@@ -50,3 +48,6 @@ export const TX_SIZE = {
50
48
  export const SIGHASH_DEFAULT = 0x01;
51
49
  export const SIGHASH_SEGWIT = [0x01, 0x02, 0x03, 0x81, 0x82, 0x83];
52
50
  export const SIGHASH_TAPROOT = [0x00, ...SIGHASH_SEGWIT];
51
+ export const MAX_SCRIPT_SIZE = 10_000;
52
+ export const MAX_VARINT_SIZE = 520_000;
53
+ export const OP_1_OFFSET = 0x50;
@@ -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
@@ -1,11 +1,12 @@
1
- export * as ADDRESS from './lib/address/index.js';
2
- export * as META from './lib/meta/index.js';
3
- export * as SCRIPT from './lib/script/index.js';
4
- export * as SIGHASH from './lib/sighash/index.js';
5
- export * as SIGNER from './lib/signer/index.js';
6
- export * as TAPROOT from './lib/taproot/index.js';
7
- export * as TX from './lib/tx/index.js';
8
- export * as WITNESS from './lib/witness/index.js';
9
- export * as CONST from './const.js';
10
- export * as SCHEMA from './schema/index.js';
11
- export type * from './types/index.js';
1
+ export * as CONST from "./const.js";
2
+ export * as ADDRESS from "./lib/address/index.js";
3
+ export * as META from "./lib/meta/index.js";
4
+ export * as SCRIPT from "./lib/script/index.js";
5
+ export * as SIGHASH from "./lib/sighash/index.js";
6
+ export * as SIGNER from "./lib/signer/index.js";
7
+ export * as TAPROOT from "./lib/taproot/index.js";
8
+ export * as TX from "./lib/tx/index.js";
9
+ export * as WITNESS from "./lib/witness/index.js";
10
+ export * as SCHEMA from "./schema/index.js";
11
+ export { ValidationError, DecodingError, ConfigError } from "./error.js";
12
+ export type * from "./types/index.js";
package/dist/index.js CHANGED
@@ -1,10 +1,11 @@
1
- export * as ADDRESS from './lib/address/index.js';
2
- export * as META from './lib/meta/index.js';
3
- export * as SCRIPT from './lib/script/index.js';
4
- export * as SIGHASH from './lib/sighash/index.js';
5
- export * as SIGNER from './lib/signer/index.js';
6
- export * as TAPROOT from './lib/taproot/index.js';
7
- export * as TX from './lib/tx/index.js';
8
- export * as WITNESS from './lib/witness/index.js';
9
- export * as CONST from './const.js';
10
- export * as SCHEMA from './schema/index.js';
1
+ export * as CONST from "./const.js";
2
+ export * as ADDRESS from "./lib/address/index.js";
3
+ export * as META from "./lib/meta/index.js";
4
+ export * as SCRIPT from "./lib/script/index.js";
5
+ export * as SIGHASH from "./lib/sighash/index.js";
6
+ export * as SIGNER from "./lib/signer/index.js";
7
+ export * as TAPROOT from "./lib/taproot/index.js";
8
+ export * as TX from "./lib/tx/index.js";
9
+ export * as WITNESS from "./lib/witness/index.js";
10
+ export * as SCHEMA from "./schema/index.js";
11
+ export { ValidationError, DecodingError, ConfigError } from "./error.js";
@@ -1,4 +1,4 @@
1
- import { Bytes } from '@vbyte/buff';
2
- import type { AddressInfo, ChainNetwork } from '../../types/index.js';
1
+ import { type Bytes } from "@vbyte/buff";
2
+ import type { AddressInfo, ChainNetwork } from "../../types/index.js";
3
3
  export declare function get_address(script: Bytes, network?: ChainNetwork): string;
4
4
  export declare function parse_address(address: string): AddressInfo;
@@ -1,17 +1,18 @@
1
- import { Buff } from '@vbyte/buff';
2
- import { get_lock_script_type } from '../../lib/script/lock.js';
3
- import { get_address_info } from './util.js';
4
- import { LOCK_SCRIPT_TYPE } from '../../const.js';
5
- import { P2PKH } from './p2pkh.js';
6
- import { P2SH } from './p2sh.js';
7
- import { P2TR } from './p2tr.js';
8
- import { P2WPKH } from './p2wpkh.js';
9
- import { P2WSH } from './p2wsh.js';
10
- export function get_address(script, network = 'main') {
1
+ import { Buff } from "@vbyte/buff";
2
+ import { LOCK_SCRIPT_TYPE } from "../../const.js";
3
+ import { ConfigError } from "../../error.js";
4
+ import { get_lock_script_type } from "../../lib/script/lock.js";
5
+ import { P2PKH } from "./p2pkh.js";
6
+ import { P2SH } from "./p2sh.js";
7
+ import { P2TR } from "./p2tr.js";
8
+ import { P2WPKH } from "./p2wpkh.js";
9
+ import { P2WSH } from "./p2wsh.js";
10
+ import { get_address_info } from "./util.js";
11
+ 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 locking script: ' + bytes.hex);
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,3 +1,3 @@
1
- import type { EncoderConfig } from '../../types/address.js';
1
+ import type { EncoderConfig } from "../../types/address.js";
2
2
  export declare function decode_address(address: string): EncoderConfig;
3
3
  export declare function encode_address(config: EncoderConfig): string;