bsv2 2.1.8 → 2.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,53 +1,226 @@
1
- # bsv
1
+ # bsv2
2
2
 
3
- bsv is a javascript library for Bitcoin SV (BSV) intended to satisfy certain
4
- goals:
3
+ [![npm version](https://img.shields.io/npm/v/bsv2.svg)](https://www.npmjs.com/package/bsv2)
4
+ [![license](https://img.shields.io/npm/l/bsv2.svg)](./LICENSE)
5
+ [![node](https://img.shields.io/badge/node-%3E%3D18-brightgreen.svg)](https://nodejs.org/)
5
6
 
6
- 1. Support ease-of-use by being internally consistent. It should not be
7
- necessary to read the source code of a class or function to know how to use it.
8
- Once you know how to use part of the library, the other parts should feel
9
- natural.
7
+ A JavaScript library for Bitcoin SV (BSV). `bsv2` is the maintained continuation
8
+ of the `bsv` 2.x codebase: same class-based API, published on npm as
9
+ [`bsv2`](https://www.npmjs.com/package/bsv2) and developed at
10
+ [github.com/samooth/bsv](https://github.com/samooth/bsv).
10
11
 
11
- 2. Have 100% test coverage, or nearly so, so that the library is known to be
12
- reliable. This should include running standard test vectors from the reference
13
- implementation.
12
+ ## Features
14
13
 
15
- 3. Library objects have an interface suitable for use with a command-line
16
- interface or other libraries and tools, in particular having toString,
17
- fromString, toJSON, fromJSON, toBuffer, fromBuffer, toHex, fromHex methods.
14
+ - Transactions and scripts with BSV `SIGHASH_FORKID` semantics.
15
+ - A script interpreter with the standard verification flags, plus an opt-in
16
+ pre-Genesis policy mode.
17
+ - Keys, addresses, BIP32 hierarchical deterministic wallets and BIP39 mnemonics
18
+ (English, Spanish and Japanese wordlists).
19
+ - Block and block-header serialization, plus mining utilities.
20
+ - ECIES and AES encryption helpers.
21
+ - Web Worker support for cryptography-heavy operations.
22
+ - Dependency-light, auditable source under `lib/`.
23
+ - Ships as ESM, CommonJS and UMD bundles with sourcemaps.
18
24
 
19
- 4. All standard features of the blockchain are implemented (or will be) and
20
- saved in lib/. All BIPs are correctly implemented and, where appropriate, saved
21
- as bip-xx.js in lib/ (since that is their standard name). In order to allow
22
- rapid development, Yours Bitcoin includes non-standard and experimental
23
- features. Any non-standard features (such as colored coins or stealth
24
- addresses) are labeled as such in index.js as well as in comments.
25
+ ## Install
25
26
 
26
- 5. Expose everything, including dependencies. This makes it possible to develop
27
- apps that require fine-grained control over the basics, such as big numbers and
28
- points. However, it also means that you can hurt yourself if you misuse these
29
- primitives.
27
+ ```bash
28
+ npm install bsv2
29
+ ```
30
+
31
+ Node.js 18 or newer is required.
30
32
 
31
- 6. Use standard javascript conventions wherever possible so that other
32
- developers find the code easy to understand.
33
+ ```js
34
+ // ESM (package.json "type": "module", or a .mjs file)
35
+ import { Bn, Tx, Script, KeyPair } from 'bsv2'
36
+
37
+ // CommonJS
38
+ const { Bn, Tx, Script, KeyPair } = require('bsv2')
39
+ ```
40
+
41
+ For the browser, use the UMD bundle from a CDN (it exposes the global `bsv2`):
42
+
43
+ ```html
44
+ <script src="https://unpkg.com/bsv2/dist/bsv2.umd.js"></script>
45
+ <script>
46
+ const { Bn, Tx } = bsv2
47
+ </script>
48
+ ```
33
49
 
34
- 7. Minimize the use of dependencies so that all code can be easily audited.
50
+ | Bundle | Use |
51
+ | --- | --- |
52
+ | `dist/bsv2.mjs` | ESM (`import`), browsers and modern bundlers |
53
+ | `dist/bsv2.cjs` | CommonJS (`require`) |
54
+ | `dist/bsv2.umd.js` | `<script>` tag / UMD (global `bsv2`) |
35
55
 
36
- 8. All instance methods modify the state of the object and return the object,
37
- unless there is a good reason to do something different. To access the result
38
- of an instance method, you must access the object property(s) that it modifies.
56
+ Each bundle has a sibling `.map` file. The package version is also exported as
57
+ `version`, and selected third-party dependencies as `deps`.
39
58
 
40
- 9. Support web workers to unblock web wallet UIs when performing cryptography.
59
+ ## Quick start
41
60
 
42
- ## Environment Variables
61
+ ### Keys and addresses
43
62
 
44
- * `BSV_JS_BASE_URL` - Default "/".
45
- * `BSV_JS_BUNDLE_FILE` - Default "bsv.js"
46
- * `BSV_JS_WORKER_FILE` - Default "bsv-worker.js"
47
- * `NETWORK` - Default "mainnet"
63
+ ```js
64
+ import { Address, KeyPair } from 'bsv2'
48
65
 
49
- You can change the network to run the CLI in testnet mode:
66
+ const keyPair = new KeyPair().fromRandom()
67
+ const address = Address.fromPubKey(keyPair.pubKey)
50
68
 
69
+ console.log(address.toString()) // 1...
51
70
  ```
52
- NETWORK=testnet ./bin/bsv.js
71
+
72
+ ### Mnemonic and HD wallet
73
+
74
+ ```js
75
+ import { Address, Bip32, Bip39 } from 'bsv2'
76
+
77
+ const mnemonic = new Bip39().fromRandom(128)
78
+ const seed = mnemonic.toSeed('') // optional BIP39 passphrase
79
+ const accountKey = Bip32.fromSeed(seed).derive("m/44'/0'/0'/0/0")
80
+
81
+ console.log(mnemonic.toString()) // twelve words for 128 bits of entropy
82
+ console.log(Address.fromPubKey(accountKey.pubKey).toString())
83
+ ```
84
+
85
+ ### Sign and verify a transaction input
86
+
87
+ This signs a pay-to-pubkey input with `SIGHASH_ALL | SIGHASH_FORKID` and checks
88
+ it with the script interpreter. The funding transaction is fabricated here; in
89
+ an application you would obtain it from your indexer or node.
90
+
91
+ ```js
92
+ import { Bn, Interp, KeyPair, Script, Sig, Tx } from 'bsv2'
93
+
94
+ const keyPair = new KeyPair().fromRandom()
95
+ const scriptPubKey = new Script()
96
+ .writeBuffer(keyPair.pubKey.toDer(true))
97
+ .writeString('OP_CHECKSIG')
98
+
99
+ const valueBn = new Bn(1000)
100
+ const fundingTx = new Tx()
101
+ .addTxIn(Buffer.alloc(32), 0xffffffff, new Script(), 0xffffffff)
102
+ .addTxOut(valueBn, scriptPubKey)
103
+
104
+ const spendTx = new Tx()
105
+ .addTxIn(fundingTx.hash(), 0, new Script(), 0xffffffff)
106
+ .addTxOut(new Bn(900), scriptPubKey)
107
+
108
+ const nHashType = Sig.SIGHASH_ALL | Sig.SIGHASH_FORKID
109
+ const sig = spendTx.sign(keyPair, nHashType, 0, scriptPubKey, valueBn)
110
+ const scriptSig = new Script().writeBuffer(sig.toTxFormat())
111
+ spendTx.txIns[0].setScript(scriptSig)
112
+
113
+ const valid = new Interp().verify(
114
+ scriptSig,
115
+ scriptPubKey,
116
+ spendTx,
117
+ 0,
118
+ undefined,
119
+ valueBn
120
+ )
121
+ console.log(valid) // true
53
122
  ```
123
+
124
+ `TxVerifier.verify(tx, txOutMap)` is available when you want to validate every
125
+ input of a transaction at once.
126
+
127
+ ### Pre-Genesis script policy
128
+
129
+ BSV Genesis raised several legacy Bitcoin limits. If you need to evaluate an
130
+ old script under the original rules, add `Interp.SCRIPT_VERIFY_PREGENESIS_LIMITS`
131
+ to the verification flags:
132
+
133
+ ```js
134
+ import { Bn, Interp, Script, Tx } from 'bsv2'
135
+
136
+ const flags = Interp.SCRIPT_VERIFY_P2SH |
137
+ Interp.SCRIPT_VERIFY_STRICTENC |
138
+ Interp.SCRIPT_VERIFY_PREGENESIS_LIMITS
139
+
140
+ const scriptSig = new Script().fromBitcoindString('2147483648 0 ADD')
141
+ const scriptPubKey = new Script().fromBitcoindString('NOP')
142
+
143
+ // false with the flag: 5-byte script numbers are rejected (4-byte limit)
144
+ const valid = new Interp().verify(scriptSig, scriptPubKey, new Tx(), 0, flags, new Bn(0))
145
+ ```
146
+
147
+ The flag restores: 4-byte script numbers, 520-byte push limit, 10 KB script
148
+ limit, 1000-element stack limit and the pre-Genesis disabled opcodes. It is not
149
+ part of `Interp.defaultFlags` and must be requested explicitly.
150
+
151
+ ## API map
152
+
153
+ All symbols below are exported from the package root (`entry.js`).
154
+
155
+ | Area | Exports |
156
+ | --- | --- |
157
+ | Keys and addresses | `KeyPair`, `PrivKey`, `PubKey`, `Address` |
158
+ | HD wallets / mnemonics | `Bip32`, `Bip39`, and the `en`, `es`, `jp` wordlists |
159
+ | Transactions | `Tx`, `TxIn`, `TxOut`, `TxOutMap`, `TxBuilder`, `TxVerifier`, `Sig`, `SigOperations` |
160
+ | Scripts | `Script`, `OpCode`, `Interp` |
161
+ | Blocks and mining | `Block`, `MinableBlock`, `BlockHeader`, `MiningUtils`, `getConstants` |
162
+ | Cryptography and math | `Bn`, `Point`, `Ecdsa`, `Hash`, `Random`, `Aes`, `Aescbc`, `Cbc`, `Ecies`, `Ach` |
163
+ | Serialization / utilities | `Struct`, `Br`, `Bw`, `Bsm`, `VarInt`, `Base58`, `Base58Check`, `cmp`, `Constants` |
164
+ | Workers | `Workers`, `WorkersResult` |
165
+ | Package | `deps`, `version` |
166
+
167
+ Additional modules under `lib/` (for example `Msg`, `Version`, `Merkle` and
168
+ `HashCache`) are used internally and are not re-exported from the package root.
169
+
170
+ For the full method-level reference, generate the HTML documentation:
171
+
172
+ ```bash
173
+ yarn doc # writes doc/index.html
174
+ ```
175
+
176
+ Instance methods generally mutate the object and return `this`. For example:
177
+
178
+ ```js
179
+ const tx = new Tx().addTxIn(...).addTxOut(...)
180
+ const hex = tx.toHex()
181
+ const sameTx = Tx.fromHex(hex)
182
+ ```
183
+
184
+ ## Development
185
+
186
+ ```bash
187
+ corepack yarn install # install from yarn.lock
188
+ yarn test:run # run the full suite once (Vitest)
189
+ yarn lint # ESLint
190
+ yarn build # build dist/bsv2.{mjs,cjs,umd.js}
191
+ yarn doc # regenerate API documentation
192
+ ```
193
+
194
+ The suite has over 9,000 tests, including the reference vectors from bitcoind,
195
+ Bitcoin ABC and Bitcoin SV. It also includes dedicated tests for the legacy
196
+ script policy flag. `yarn test` starts Vitest in watch mode.
197
+
198
+ Commits and release tags are GPG-signed, and `commit.gpgsign` is enabled for
199
+ future commits. Releases are published to npm and GitHub Releases with the same
200
+ version number. `resolutions` (Yarn) and `overrides` (npm) keep transitive
201
+ dependencies pinned to patched versions.
202
+
203
+ ## Differences from upstream `bsv` 2.x
204
+
205
+ - Published as **`bsv2`**; ESM-first with a matching CommonJS and UMD build.
206
+ - Default sighash behaviour follows BSV (`SIGHASH_FORKID`); pre-Genesis rules
207
+ are available through the explicit flag described above.
208
+ - The `Bn` wrapper was rewritten and the script interpreter received targeted
209
+ correctness fixes; see the commit history for details.
210
+ - Japanese BIP39 vectors, `Buffer`/`pbkdf2` interoperability and mnemonic
211
+ whitespace validation were fixed.
212
+ - Legacy bitcoind sighash vectors are evaluated with forkid disabled in the
213
+ test suite, since they predate BSV.
214
+
215
+ ## Security
216
+
217
+ Please report security issues privately through the repository's security
218
+ advisories on GitHub rather than opening a public issue.
219
+
220
+ ## Credits and license
221
+
222
+ Based on the original `bsv` library by Manuel Araoz and contributors, with
223
+ contributions from the `bsv2` community. See `package.json` for the full
224
+ contributor list.
225
+
226
+ [MIT](./LICENSE)