@btc-vision/bitcoin 6.3.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.
- package/LICENSE +21 -0
- package/README.md +201 -0
- package/package.json +95 -0
- package/src/address.d.ts +42 -0
- package/src/address.js +191 -0
- package/src/bip66.d.ts +7 -0
- package/src/bip66.js +103 -0
- package/src/block.d.ts +30 -0
- package/src/block.js +224 -0
- package/src/bufferutils.d.ts +54 -0
- package/src/bufferutils.js +182 -0
- package/src/crypto.d.ts +18 -0
- package/src/crypto.js +128 -0
- package/src/ecc_lib.d.ts +17 -0
- package/src/ecc_lib.js +122 -0
- package/src/hooks/AdvancedSignatureManager.d.ts +44 -0
- package/src/hooks/AdvancedSignatureManager.js +88 -0
- package/src/hooks/HookedSigner.d.ts +4 -0
- package/src/hooks/HookedSigner.js +90 -0
- package/src/hooks/SignatureManager.d.ts +35 -0
- package/src/hooks/SignatureManager.js +72 -0
- package/src/index.d.ts +42 -0
- package/src/index.js +87 -0
- package/src/merkle.d.ts +10 -0
- package/src/merkle.js +30 -0
- package/src/networks.d.ts +29 -0
- package/src/networks.js +71 -0
- package/src/ops.d.ts +126 -0
- package/src/ops.js +131 -0
- package/src/payments/bip341.d.ts +49 -0
- package/src/payments/bip341.js +124 -0
- package/src/payments/embed.d.ts +9 -0
- package/src/payments/embed.js +54 -0
- package/src/payments/index.d.ts +48 -0
- package/src/payments/index.js +69 -0
- package/src/payments/lazy.d.ts +2 -0
- package/src/payments/lazy.js +32 -0
- package/src/payments/p2ms.d.ts +9 -0
- package/src/payments/p2ms.js +158 -0
- package/src/payments/p2pk.d.ts +10 -0
- package/src/payments/p2pk.js +82 -0
- package/src/payments/p2pkh.d.ts +10 -0
- package/src/payments/p2pkh.js +143 -0
- package/src/payments/p2sh.d.ts +10 -0
- package/src/payments/p2sh.js +204 -0
- package/src/payments/p2tr.d.ts +10 -0
- package/src/payments/p2tr.js +315 -0
- package/src/payments/p2wpkh.d.ts +10 -0
- package/src/payments/p2wpkh.js +146 -0
- package/src/payments/p2wsh.d.ts +10 -0
- package/src/payments/p2wsh.js +226 -0
- package/src/psbt/bip371.d.ts +42 -0
- package/src/psbt/bip371.js +424 -0
- package/src/psbt/psbtutils.d.ts +64 -0
- package/src/psbt/psbtutils.js +191 -0
- package/src/psbt.d.ts +235 -0
- package/src/psbt.js +1825 -0
- package/src/push_data.d.ts +29 -0
- package/src/push_data.js +83 -0
- package/src/script.d.ts +42 -0
- package/src/script.js +231 -0
- package/src/script_number.d.ts +19 -0
- package/src/script_number.js +78 -0
- package/src/script_signature.d.ts +21 -0
- package/src/script_signature.js +79 -0
- package/src/transaction.d.ts +60 -0
- package/src/transaction.js +571 -0
- package/src/types.d.ts +54 -0
- package/src/types.js +106 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2011-2020 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
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# BitcoinJS (bitcoinjs-lib)
|
|
2
|
+
[](https://github.com/bitcoinjs/bitcoinjs-lib/actions/workflows/main_ci.yml) [](https://www.npmjs.org/package/bitcoinjs-lib) [](https://github.com/prettier/prettier)
|
|
3
|
+
|
|
4
|
+
A javascript Bitcoin library for node.js and browsers. Written in TypeScript, but committing the JS files to verify.
|
|
5
|
+
|
|
6
|
+
Released under the terms of the [MIT LICENSE](LICENSE).
|
|
7
|
+
|
|
8
|
+
## Should I use this in production?
|
|
9
|
+
If you are thinking of using the *master* branch of this library in production, **stop**.
|
|
10
|
+
Master is not stable; it is our development branch, and [only tagged releases may be classified as stable](https://github.com/bitcoinjs/bitcoinjs-lib/tags).
|
|
11
|
+
|
|
12
|
+
## Can I trust this code?
|
|
13
|
+
> Don't trust. Verify.
|
|
14
|
+
|
|
15
|
+
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.
|
|
16
|
+
|
|
17
|
+
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:
|
|
18
|
+
|
|
19
|
+
- Easy to audit and verify,
|
|
20
|
+
- Tested, with test coverage >95%,
|
|
21
|
+
- Advanced and feature rich,
|
|
22
|
+
- Standardized, using [prettier](https://github.com/prettier/prettier) and Node `Buffer`'s throughout, and
|
|
23
|
+
- Friendly, with a strong and helpful community, ready to answer questions.
|
|
24
|
+
|
|
25
|
+
## Documentation
|
|
26
|
+
Visit our [documentation](https://bitcoinjs.github.io/bitcoinjs-lib/) to explore the available resources. We're continually enhancing our documentation with additional features for an enriched experience. If you need further guidance beyond what our [examples](#examples) offer, don't hesitate to [ask for help](https://github.com/bitcoinjs/bitcoinjs-lib/issues/new). We're here to assist you.
|
|
27
|
+
|
|
28
|
+
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).
|
|
29
|
+
|
|
30
|
+
## How can I contact the developers outside of Github?
|
|
31
|
+
**Most of the time, this is not appropriate. Creating issues and pull requests in the open will help others with similar issues, so please try to use public issues and pull requests for communication.**
|
|
32
|
+
|
|
33
|
+
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.
|
|
34
|
+
|
|
35
|
+
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`.
|
|
36
|
+
|
|
37
|
+
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.)
|
|
38
|
+
|
|
39
|
+
We used to have a Slack. It is dead. If you find it, no one will answer you most likely.
|
|
40
|
+
|
|
41
|
+
No we will not make a Discord.
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
``` bash
|
|
45
|
+
npm install bitcoinjs-lib
|
|
46
|
+
# optionally, install a key derivation library as well
|
|
47
|
+
npm install ecpair bip32
|
|
48
|
+
# ecpair is the ECPair class for single keys
|
|
49
|
+
# bip32 is for generating HD keys
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
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).
|
|
53
|
+
|
|
54
|
+
Typically we support the [Node Maintenance LTS version](https://github.com/nodejs/Release). TypeScript target will be set
|
|
55
|
+
to the ECMAScript version in which all features are fully supported by current Active Node LTS.
|
|
56
|
+
However, depending on adoption among other environments (browsers etc.) we may keep the target back a year or two.
|
|
57
|
+
If in doubt, see the [main_ci.yml](.github/workflows/main_ci.yml) for what versions are used by our continuous integration tests.
|
|
58
|
+
|
|
59
|
+
**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.
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
## Usage
|
|
63
|
+
Crypto is hard.
|
|
64
|
+
|
|
65
|
+
When working with private keys, the random number generator is fundamentally one of the most important parts of any software you write.
|
|
66
|
+
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.
|
|
67
|
+
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**.
|
|
68
|
+
You should always verify this yourself to your own standards.
|
|
69
|
+
|
|
70
|
+
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.
|
|
71
|
+
Unfortunately, this isn't a silver bullet.
|
|
72
|
+
Often, Javascript itself is working against us by bypassing these counter-measures.
|
|
73
|
+
|
|
74
|
+
Problems in [`Buffer (UInt8Array)`](https://github.com/feross/buffer), for example, can trivially result in **catastrophic fund loss** without any warning.
|
|
75
|
+
It can do this through undermining your random number generation, accidentally producing a [duplicate `k` value](https://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.
|
|
76
|
+
Running tests in your target environment is important and a recommended step to verify continuously.
|
|
77
|
+
|
|
78
|
+
Finally, **adhere to best practice**.
|
|
79
|
+
We are not an authoritative source of best practice, but, at the very least:
|
|
80
|
+
|
|
81
|
+
* [Don't reuse addresses](https://en.bitcoin.it/wiki/Address_reuse).
|
|
82
|
+
* 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**.
|
|
83
|
+
* [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.
|
|
84
|
+
* Enforce that users always verify (manually) a freshly-decoded human-readable version of their intended transaction before broadcast.
|
|
85
|
+
* [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.
|
|
86
|
+
* Lastly, if you can, use [Typescript](https://www.typescriptlang.org/) or similar.
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
### Browser
|
|
90
|
+
The recommended method of using `bitcoinjs-lib` in your browser is through [browserify](http://browserify.org/).
|
|
91
|
+
|
|
92
|
+
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:
|
|
93
|
+
|
|
94
|
+
```sh
|
|
95
|
+
$ npm install bitcoinjs-lib browserify
|
|
96
|
+
$ npx browserify --standalone bitcoin - -o bitcoinjs-lib.js <<<"module.exports = require('bitcoinjs-lib');"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Which you can then import as an ESM module:
|
|
100
|
+
|
|
101
|
+
```javascript
|
|
102
|
+
<script type="module">import "/scripts/bitcoinjs-lib.js"</script>
|
|
103
|
+
````
|
|
104
|
+
|
|
105
|
+
#### Using Taproot:
|
|
106
|
+
When utilizing Taproot features with bitcoinjs-lib, you may need to include an additional ECC (Elliptic Curve Cryptography) library. The commonly used `tiny-secp256k1` library, however, might lead to compatibility issues due to its reliance on WASM (WebAssembly). The following alternatives may be used instead, though they may be significantly slower for high volume of signing and pubkey deriving operations.
|
|
107
|
+
|
|
108
|
+
#### Alternatives for ECC Library:
|
|
109
|
+
1. `@bitcoinjs-lib/tiny-secp256k1-asmjs`
|
|
110
|
+
A version of `tiny-secp256k1` compiled to ASM.js directly from the WASM version, potentially better supported in browsers. This is the slowest option.
|
|
111
|
+
2. `@bitcoinerlab/secp256k1`
|
|
112
|
+
Another alternative library for ECC functionality. This requires access to the global `BigInt` primitive.
|
|
113
|
+
For advantages and detailed comparison of these libraries, visit: [tiny-secp256k1 GitHub page](https://github.com/bitcoinjs/tiny-secp256k1).
|
|
114
|
+
|
|
115
|
+
**NOTE**: We use Node Maintenance LTS features, if you need strict ES5, use [`--transform babelify`](https://github.com/babel/babelify) in conjunction with your `browserify` step (using an [`es2015`](https://babeljs.io/docs/plugins/preset-es2015/) preset).
|
|
116
|
+
|
|
117
|
+
**WARNING**: iOS devices have [problems](https://github.com/feross/buffer/issues/136), use at least [buffer@5.0.5](https://github.com/feross/buffer/pull/155) or greater, and enforce the test suites (for `Buffer`, and any other dependency) pass before use.
|
|
118
|
+
|
|
119
|
+
### Typescript or VSCode users
|
|
120
|
+
Type declarations for Typescript are included in this library. Normal installation should include all the needed type information.
|
|
121
|
+
|
|
122
|
+
## Examples
|
|
123
|
+
The below examples are implemented as integration tests, they should be very easy to understand.
|
|
124
|
+
Otherwise, pull requests are appreciated.
|
|
125
|
+
Some examples interact (via HTTPS) with a 3rd Party Blockchain Provider (3PBP).
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
- [Taproot Key Spend](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/taproot.spec.ts)
|
|
129
|
+
- [Generate a random address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
130
|
+
- [Import an address via WIF](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
131
|
+
- [Generate a 2-of-3 P2SH multisig address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
132
|
+
- [Generate a SegWit address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
133
|
+
- [Generate a SegWit P2SH address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
134
|
+
- [Generate a SegWit 3-of-4 multisig address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
135
|
+
- [Generate a SegWit 2-of-2 P2SH multisig address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
136
|
+
- [Support the retrieval of transactions for an address (3rd party blockchain)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
137
|
+
- [Generate a Testnet address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
138
|
+
- [Generate a Litecoin address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/addresses.spec.ts)
|
|
139
|
+
- [Create a 1-to-1 Transaction](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
140
|
+
- [Create (and broadcast via 3PBP) a typical Transaction](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
141
|
+
- [Create (and broadcast via 3PBP) a Transaction with an OP\_RETURN output](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
142
|
+
- [Create (and broadcast via 3PBP) a Transaction with a 2-of-4 P2SH(multisig) input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
143
|
+
- [Create (and broadcast via 3PBP) a Transaction with a SegWit P2SH(P2WPKH) input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
144
|
+
- [Create (and broadcast via 3PBP) a Transaction with a SegWit P2WPKH input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
145
|
+
- [Create (and broadcast via 3PBP) a Transaction with a SegWit P2PK input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
146
|
+
- [Create (and broadcast via 3PBP) a Transaction with a SegWit 3-of-4 P2SH(P2WSH(multisig)) input](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
147
|
+
- [Create (and broadcast via 3PBP) a Transaction and sign with an HDSigner interface (bip32)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.spec.ts)
|
|
148
|
+
- [Import a BIP32 testnet xpriv and export to WIF](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
149
|
+
- [Export a BIP32 xpriv, then import it](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
150
|
+
- [Export a BIP32 xpub](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
151
|
+
- [Create a BIP32, bitcoin, account 0, external address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
152
|
+
- [Create a BIP44, bitcoin, account 0, external address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
153
|
+
- [Create a BIP49, bitcoin testnet, account 0, external address](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
154
|
+
- [Use BIP39 to generate BIP32 addresses](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/bip32.spec.ts)
|
|
155
|
+
- [Create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the past)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.spec.ts)
|
|
156
|
+
- [Create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the future)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.spec.ts)
|
|
157
|
+
- [Create (and broadcast via 3PBP) a Transaction where Alice and Bob can redeem the output at any time](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.spec.ts)
|
|
158
|
+
- [Create (but fail to broadcast via 3PBP) a Transaction where Alice attempts to redeem before the expiry](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/cltv.spec.ts)
|
|
159
|
+
- [Create (and broadcast via 3PBP) a Transaction where Alice can redeem the output after the expiry (in the future) (simple CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
|
|
160
|
+
- [Create (but fail to broadcast via 3PBP) a Transaction where Alice attempts to redeem before the expiry (simple CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
|
|
161
|
+
- [Create (and broadcast via 3PBP) a Transaction where Bob and Charles can send (complex CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
|
|
162
|
+
- [Create (and broadcast via 3PBP) a Transaction where Alice (mediator) and Bob can send after 2 blocks (complex CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
|
|
163
|
+
- [Create (and broadcast via 3PBP) a Transaction where Alice (mediator) can send after 5 blocks (complex CHECKSEQUENCEVERIFY)](https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/csv.spec.ts)
|
|
164
|
+
|
|
165
|
+
If you have a use case that you feel could be listed here, please [ask for it](https://github.com/bitcoinjs/bitcoinjs-lib/issues/new)!
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
## Contributing
|
|
169
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
### Running the test suite
|
|
173
|
+
|
|
174
|
+
``` bash
|
|
175
|
+
npm test
|
|
176
|
+
npm run-script coverage
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Complementing Libraries
|
|
180
|
+
- [BIP21](https://github.com/bitcoinjs/bip21) - A BIP21 compatible URL encoding library
|
|
181
|
+
- [BIP38](https://github.com/bitcoinjs/bip38) - Passphrase-protected private keys
|
|
182
|
+
- [BIP39](https://github.com/bitcoinjs/bip39) - Mnemonic generation for deterministic keys
|
|
183
|
+
- [BIP32-Utils](https://github.com/bitcoinjs/bip32-utils) - A set of utilities for working with BIP32
|
|
184
|
+
- [BIP66](https://github.com/bitcoinjs/bip66) - Strict DER signature decoding
|
|
185
|
+
- [BIP68](https://github.com/bitcoinjs/bip68) - Relative lock-time encoding library
|
|
186
|
+
- [BIP69](https://github.com/bitcoinjs/bip69) - Lexicographical Indexing of Transaction Inputs and Outputs
|
|
187
|
+
- [Base58](https://github.com/cryptocoinjs/bs58) - Base58 encoding/decoding
|
|
188
|
+
- [Base58 Check](https://github.com/bitcoinjs/bs58check) - Base58 check encoding/decoding
|
|
189
|
+
- [Bech32](https://github.com/bitcoinjs/bech32) - A BIP173/BIP350 compliant Bech32/Bech32m encoding library
|
|
190
|
+
- [coinselect](https://github.com/bitcoinjs/coinselect) - A fee-optimizing, transaction input selection module for bitcoinjs-lib.
|
|
191
|
+
- [merkle-lib](https://github.com/bitcoinjs/merkle-lib) - A performance conscious library for merkle root and tree calculations.
|
|
192
|
+
- [minimaldata](https://github.com/bitcoinjs/minimaldata) - A module to check bitcoin policy: SCRIPT_VERIFY_MINIMALDATA
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
## Alternatives
|
|
196
|
+
- [BCoin](https://github.com/indutny/bcoin)
|
|
197
|
+
- [Bitcore](https://github.com/bitpay/bitcore)
|
|
198
|
+
- [Cryptocoin](https://github.com/cryptocoinjs/cryptocoin)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
## LICENSE [MIT](LICENSE)
|
package/package.json
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@btc-vision/bitcoin",
|
|
3
|
+
"version": "6.3.0",
|
|
4
|
+
"description": "Client-side Bitcoin JavaScript library",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"types": "./src/index.d.ts",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=8.0.0"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"bitcoinjs",
|
|
12
|
+
"bitcoin",
|
|
13
|
+
"browserify",
|
|
14
|
+
"javascript",
|
|
15
|
+
"bitcoinjs"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"audit": "better-npm-audit audit -l high",
|
|
19
|
+
"build": "npm run clean && tsc -p ./tsconfig.json && npm run formatjs",
|
|
20
|
+
"build:tests": "npm run clean:jstests && tsc -p ./test/tsconfig.json",
|
|
21
|
+
"clean": "rimraf src",
|
|
22
|
+
"clean:jstests": "rimraf 'test/**/!(ts-node-register)*.js'",
|
|
23
|
+
"coverage-report": "npm run build && npm run nobuild:coverage-report",
|
|
24
|
+
"coverage-html": "npm run build && npm run nobuild:coverage-html",
|
|
25
|
+
"coverage": "npm run build && npm run nobuild:coverage",
|
|
26
|
+
"doc": "typedoc",
|
|
27
|
+
"format": "npm run prettier -- --write",
|
|
28
|
+
"formatjs": "npm run prettierjs -- --write",
|
|
29
|
+
"format:ci": "npm run prettier -- --check && npm run prettierjs -- --check",
|
|
30
|
+
"gitdiff:ci": "npm run build && git diff --exit-code",
|
|
31
|
+
"integration": "npm run build && npm run nobuild:integration",
|
|
32
|
+
"lint": "eslint ts_src/** src/**/*.js",
|
|
33
|
+
"lint:tests": "eslint test/**/*.spec.ts",
|
|
34
|
+
"mocha:ts": "mocha --recursive --require test/ts-node-register",
|
|
35
|
+
"nobuild:coverage-report": "nyc report --reporter=lcov",
|
|
36
|
+
"nobuild:coverage-html": "nyc report --reporter=html",
|
|
37
|
+
"nobuild:coverage": "npm run build:tests && nyc --check-coverage --branches 85 --functions 90 --lines 90 mocha && npm run clean:jstests",
|
|
38
|
+
"nobuild:integration": "npm run mocha:ts -- --timeout 50000 'test/integration/*.ts'",
|
|
39
|
+
"nobuild:unit": "npm run mocha:ts -- 'test/*.ts'",
|
|
40
|
+
"prettier": "prettier \"ts_src/**/*.ts\" \"test/**/*.ts\" --ignore-path ./.prettierignore",
|
|
41
|
+
"prettierjs": "prettier \"src/**/*.js\" --ignore-path ./.prettierignore",
|
|
42
|
+
"test": "npm run build && npm run format:ci && npm run lint && npm run nobuild:coverage",
|
|
43
|
+
"unit": "npm run build && npm run nobuild:unit"
|
|
44
|
+
},
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "https://github.com/btc-vision/bitcoin.git"
|
|
48
|
+
},
|
|
49
|
+
"files": [
|
|
50
|
+
"src"
|
|
51
|
+
],
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@noble/hashes": "^1.2.0",
|
|
54
|
+
"bech32": "^2.0.0",
|
|
55
|
+
"bip174": "^2.1.1",
|
|
56
|
+
"bs58check": "^3.0.1",
|
|
57
|
+
"typeforce": "^1.11.3",
|
|
58
|
+
"varuint-bitcoin": "^1.1.2"
|
|
59
|
+
},
|
|
60
|
+
"devDependencies": {
|
|
61
|
+
"@types/bs58": "^4.0.0",
|
|
62
|
+
"@types/bs58check": "^2.1.0",
|
|
63
|
+
"@types/mocha": "^5.2.7",
|
|
64
|
+
"@types/node": "^16.11.7",
|
|
65
|
+
"@types/proxyquire": "^1.3.28",
|
|
66
|
+
"@types/randombytes": "^2.0.0",
|
|
67
|
+
"@typescript-eslint/eslint-plugin": "^8.7.0",
|
|
68
|
+
"@typescript-eslint/parser": "^8.7.0",
|
|
69
|
+
"better-npm-audit": "^3.7.3",
|
|
70
|
+
"bip32": "^4.0.0",
|
|
71
|
+
"bip39": "^3.1.0",
|
|
72
|
+
"bip65": "^1.0.1",
|
|
73
|
+
"bip68": "^1.0.3",
|
|
74
|
+
"bs58": "^4.0.0",
|
|
75
|
+
"dhttp": "^3.0.0",
|
|
76
|
+
"ecpair": "^2.0.1",
|
|
77
|
+
"eslint": "^8.29.0",
|
|
78
|
+
"eslint-config-prettier": "^8.5.0",
|
|
79
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
80
|
+
"hoodwink": "^2.0.0",
|
|
81
|
+
"minimaldata": "^1.0.2",
|
|
82
|
+
"mocha": "^10.0.0",
|
|
83
|
+
"nyc": "^15.1.0",
|
|
84
|
+
"prettier": "^2.8.0",
|
|
85
|
+
"proxyquire": "^2.0.1",
|
|
86
|
+
"randombytes": "^2.1.0",
|
|
87
|
+
"regtest-client": "0.2.0",
|
|
88
|
+
"rimraf": "^2.6.3",
|
|
89
|
+
"tiny-secp256k1": "^2.2.0",
|
|
90
|
+
"ts-node": "^8.3.0",
|
|
91
|
+
"typedoc": "^0.25.1",
|
|
92
|
+
"typescript": "^4.4.4"
|
|
93
|
+
},
|
|
94
|
+
"license": "MIT"
|
|
95
|
+
}
|
package/src/address.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Network } from './networks';
|
|
3
|
+
/** base58check decode result */
|
|
4
|
+
export interface Base58CheckResult {
|
|
5
|
+
/** address hash */
|
|
6
|
+
hash: Buffer;
|
|
7
|
+
/** address version: 0x00 for P2PKH, 0x05 for P2SH */
|
|
8
|
+
version: number;
|
|
9
|
+
}
|
|
10
|
+
/** bech32 decode result */
|
|
11
|
+
export interface Bech32Result {
|
|
12
|
+
/** address version: 0x00 for P2WPKH、P2WSH, 0x01 for P2TR*/
|
|
13
|
+
version: number;
|
|
14
|
+
/** address prefix: bc for P2WPKH、P2WSH、P2TR */
|
|
15
|
+
prefix: string;
|
|
16
|
+
/** address data:20 bytes for P2WPKH, 32 bytes for P2WSH、P2TR */
|
|
17
|
+
data: Buffer;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* decode address with base58 specification, return address version and address hash if valid
|
|
21
|
+
*/
|
|
22
|
+
export declare function fromBase58Check(address: string): Base58CheckResult;
|
|
23
|
+
/**
|
|
24
|
+
* decode address with bech32 specification, return address version、address prefix and address data if valid
|
|
25
|
+
*/
|
|
26
|
+
export declare function fromBech32(address: string): Bech32Result;
|
|
27
|
+
/**
|
|
28
|
+
* encode address hash to base58 address with version
|
|
29
|
+
*/
|
|
30
|
+
export declare function toBase58Check(hash: Buffer, version: number): string;
|
|
31
|
+
/**
|
|
32
|
+
* encode address hash to bech32 address with version and prefix
|
|
33
|
+
*/
|
|
34
|
+
export declare function toBech32(data: Buffer, version: number, prefix: string): string;
|
|
35
|
+
/**
|
|
36
|
+
* decode address from output script with network, return address if matched
|
|
37
|
+
*/
|
|
38
|
+
export declare function fromOutputScript(output: Buffer, network?: Network): string;
|
|
39
|
+
/**
|
|
40
|
+
* encodes address to output script with network, return output script if address matched
|
|
41
|
+
*/
|
|
42
|
+
export declare function toOutputScript(address: string, network?: Network): Buffer;
|
package/src/address.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
|
+
exports.toOutputScript =
|
|
4
|
+
exports.fromOutputScript =
|
|
5
|
+
exports.toBech32 =
|
|
6
|
+
exports.toBase58Check =
|
|
7
|
+
exports.fromBech32 =
|
|
8
|
+
exports.fromBase58Check =
|
|
9
|
+
void 0;
|
|
10
|
+
/**
|
|
11
|
+
* bitcoin address decode and encode tools, include base58、bech32 and output script
|
|
12
|
+
*
|
|
13
|
+
* networks support bitcoin、bitcoin testnet and bitcoin regtest
|
|
14
|
+
*
|
|
15
|
+
* addresses support P2PKH、P2SH、P2WPKH、P2WSH、P2TR and so on
|
|
16
|
+
*
|
|
17
|
+
* @packageDocumentation
|
|
18
|
+
*/
|
|
19
|
+
const networks = require('./networks');
|
|
20
|
+
const payments = require('./payments');
|
|
21
|
+
const bscript = require('./script');
|
|
22
|
+
const types_1 = require('./types');
|
|
23
|
+
const bech32_1 = require('bech32');
|
|
24
|
+
const bs58check = require('bs58check');
|
|
25
|
+
const FUTURE_SEGWIT_MAX_SIZE = 40;
|
|
26
|
+
const FUTURE_SEGWIT_MIN_SIZE = 2;
|
|
27
|
+
const FUTURE_SEGWIT_MAX_VERSION = 16;
|
|
28
|
+
const FUTURE_SEGWIT_MIN_VERSION = 2;
|
|
29
|
+
const FUTURE_SEGWIT_VERSION_DIFF = 0x50;
|
|
30
|
+
const FUTURE_SEGWIT_VERSION_WARNING =
|
|
31
|
+
'WARNING: Sending to a future segwit version address can lead to loss of funds. ' +
|
|
32
|
+
'End users MUST be warned carefully in the GUI and asked if they wish to proceed ' +
|
|
33
|
+
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
|
|
34
|
+
'then decide when it is safe to use which version of segwit.';
|
|
35
|
+
function _toFutureSegwitAddress(output, network) {
|
|
36
|
+
const data = output.slice(2);
|
|
37
|
+
if (
|
|
38
|
+
data.length < FUTURE_SEGWIT_MIN_SIZE ||
|
|
39
|
+
data.length > FUTURE_SEGWIT_MAX_SIZE
|
|
40
|
+
)
|
|
41
|
+
throw new TypeError('Invalid program length for segwit address');
|
|
42
|
+
const version = output[0] - FUTURE_SEGWIT_VERSION_DIFF;
|
|
43
|
+
if (
|
|
44
|
+
version < FUTURE_SEGWIT_MIN_VERSION ||
|
|
45
|
+
version > FUTURE_SEGWIT_MAX_VERSION
|
|
46
|
+
)
|
|
47
|
+
throw new TypeError('Invalid version for segwit address');
|
|
48
|
+
if (output[1] !== data.length)
|
|
49
|
+
throw new TypeError('Invalid script for segwit address');
|
|
50
|
+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
|
|
51
|
+
return toBech32(data, version, network.bech32);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* decode address with base58 specification, return address version and address hash if valid
|
|
55
|
+
*/
|
|
56
|
+
function fromBase58Check(address) {
|
|
57
|
+
const payload = Buffer.from(bs58check.decode(address));
|
|
58
|
+
// TODO: 4.0.0, move to "toOutputScript"
|
|
59
|
+
if (payload.length < 21) throw new TypeError(address + ' is too short');
|
|
60
|
+
if (payload.length > 21) throw new TypeError(address + ' is too long');
|
|
61
|
+
const version = payload.readUInt8(0);
|
|
62
|
+
const hash = payload.slice(1);
|
|
63
|
+
return { version, hash };
|
|
64
|
+
}
|
|
65
|
+
exports.fromBase58Check = fromBase58Check;
|
|
66
|
+
/**
|
|
67
|
+
* decode address with bech32 specification, return address version、address prefix and address data if valid
|
|
68
|
+
*/
|
|
69
|
+
function fromBech32(address) {
|
|
70
|
+
let result;
|
|
71
|
+
let version;
|
|
72
|
+
try {
|
|
73
|
+
result = bech32_1.bech32.decode(address);
|
|
74
|
+
} catch (e) {}
|
|
75
|
+
if (result) {
|
|
76
|
+
version = result.words[0];
|
|
77
|
+
if (version !== 0)
|
|
78
|
+
throw new TypeError(address + ' uses wrong encoding');
|
|
79
|
+
} else {
|
|
80
|
+
result = bech32_1.bech32m.decode(address);
|
|
81
|
+
version = result.words[0];
|
|
82
|
+
if (version === 0)
|
|
83
|
+
throw new TypeError(address + ' uses wrong encoding');
|
|
84
|
+
}
|
|
85
|
+
const data = bech32_1.bech32.fromWords(result.words.slice(1));
|
|
86
|
+
return {
|
|
87
|
+
version,
|
|
88
|
+
prefix: result.prefix,
|
|
89
|
+
data: Buffer.from(data),
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
exports.fromBech32 = fromBech32;
|
|
93
|
+
/**
|
|
94
|
+
* encode address hash to base58 address with version
|
|
95
|
+
*/
|
|
96
|
+
function toBase58Check(hash, version) {
|
|
97
|
+
(0, types_1.typeforce)(
|
|
98
|
+
(0, types_1.tuple)(types_1.Hash160bit, types_1.UInt8),
|
|
99
|
+
arguments,
|
|
100
|
+
);
|
|
101
|
+
const payload = Buffer.allocUnsafe(21);
|
|
102
|
+
payload.writeUInt8(version, 0);
|
|
103
|
+
hash.copy(payload, 1);
|
|
104
|
+
return bs58check.encode(payload);
|
|
105
|
+
}
|
|
106
|
+
exports.toBase58Check = toBase58Check;
|
|
107
|
+
/**
|
|
108
|
+
* encode address hash to bech32 address with version and prefix
|
|
109
|
+
*/
|
|
110
|
+
function toBech32(data, version, prefix) {
|
|
111
|
+
const words = bech32_1.bech32.toWords(data);
|
|
112
|
+
words.unshift(version);
|
|
113
|
+
return version === 0
|
|
114
|
+
? bech32_1.bech32.encode(prefix, words)
|
|
115
|
+
: bech32_1.bech32m.encode(prefix, words);
|
|
116
|
+
}
|
|
117
|
+
exports.toBech32 = toBech32;
|
|
118
|
+
/**
|
|
119
|
+
* decode address from output script with network, return address if matched
|
|
120
|
+
*/
|
|
121
|
+
function fromOutputScript(output, network) {
|
|
122
|
+
// TODO: Network
|
|
123
|
+
network = network || networks.bitcoin;
|
|
124
|
+
try {
|
|
125
|
+
return payments.p2pkh({ output, network }).address;
|
|
126
|
+
} catch (e) {}
|
|
127
|
+
try {
|
|
128
|
+
return payments.p2sh({ output, network }).address;
|
|
129
|
+
} catch (e) {}
|
|
130
|
+
try {
|
|
131
|
+
return payments.p2wpkh({ output, network }).address;
|
|
132
|
+
} catch (e) {}
|
|
133
|
+
try {
|
|
134
|
+
return payments.p2wsh({ output, network }).address;
|
|
135
|
+
} catch (e) {}
|
|
136
|
+
try {
|
|
137
|
+
return payments.p2tr({ output, network }).address;
|
|
138
|
+
} catch (e) {}
|
|
139
|
+
try {
|
|
140
|
+
return _toFutureSegwitAddress(output, network);
|
|
141
|
+
} catch (e) {}
|
|
142
|
+
throw new Error(bscript.toASM(output) + ' has no matching Address');
|
|
143
|
+
}
|
|
144
|
+
exports.fromOutputScript = fromOutputScript;
|
|
145
|
+
/**
|
|
146
|
+
* encodes address to output script with network, return output script if address matched
|
|
147
|
+
*/
|
|
148
|
+
function toOutputScript(address, network) {
|
|
149
|
+
network = network || networks.bitcoin;
|
|
150
|
+
let decodeBase58;
|
|
151
|
+
let decodeBech32;
|
|
152
|
+
try {
|
|
153
|
+
decodeBase58 = fromBase58Check(address);
|
|
154
|
+
} catch (e) {}
|
|
155
|
+
if (decodeBase58) {
|
|
156
|
+
if (decodeBase58.version === network.pubKeyHash)
|
|
157
|
+
return payments.p2pkh({ hash: decodeBase58.hash }).output;
|
|
158
|
+
if (decodeBase58.version === network.scriptHash)
|
|
159
|
+
return payments.p2sh({ hash: decodeBase58.hash }).output;
|
|
160
|
+
} else {
|
|
161
|
+
try {
|
|
162
|
+
decodeBech32 = fromBech32(address);
|
|
163
|
+
} catch (e) {}
|
|
164
|
+
if (decodeBech32) {
|
|
165
|
+
if (decodeBech32.prefix !== network.bech32)
|
|
166
|
+
throw new Error(address + ' has an invalid prefix');
|
|
167
|
+
if (decodeBech32.version === 0) {
|
|
168
|
+
if (decodeBech32.data.length === 20)
|
|
169
|
+
return payments.p2wpkh({ hash: decodeBech32.data }).output;
|
|
170
|
+
if (decodeBech32.data.length === 32)
|
|
171
|
+
return payments.p2wsh({ hash: decodeBech32.data }).output;
|
|
172
|
+
} else if (decodeBech32.version === 1) {
|
|
173
|
+
if (decodeBech32.data.length === 32)
|
|
174
|
+
return payments.p2tr({ pubkey: decodeBech32.data }).output;
|
|
175
|
+
} else if (
|
|
176
|
+
decodeBech32.version >= FUTURE_SEGWIT_MIN_VERSION &&
|
|
177
|
+
decodeBech32.version <= FUTURE_SEGWIT_MAX_VERSION &&
|
|
178
|
+
decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
|
|
179
|
+
decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
|
|
180
|
+
) {
|
|
181
|
+
console.warn(FUTURE_SEGWIT_VERSION_WARNING);
|
|
182
|
+
return bscript.compile([
|
|
183
|
+
decodeBech32.version + FUTURE_SEGWIT_VERSION_DIFF,
|
|
184
|
+
decodeBech32.data,
|
|
185
|
+
]);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return Buffer.from(address, 'hex');
|
|
190
|
+
}
|
|
191
|
+
exports.toOutputScript = toOutputScript;
|