bsv2 2.1.8 → 2.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.
package/README.md CHANGED
@@ -1,53 +1,259 @@
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
+ - Chronicle support: the Original Transaction Digest Algorithm (OTDA), the
16
+ restored opcodes and the opt-in malleability policy.
17
+ - A script interpreter with the standard verification flags, plus opt-in
18
+ pre-Genesis and Chronicle policy modes.
19
+ - Keys, addresses, BIP32 hierarchical deterministic wallets and BIP39 mnemonics
20
+ (English, Spanish and Japanese wordlists).
21
+ - Block and block-header serialization, plus mining utilities.
22
+ - ECIES and AES encryption helpers.
23
+ - Web Worker support for cryptography-heavy operations.
24
+ - Dependency-light, auditable source under `lib/`.
25
+ - Ships as ESM, CommonJS and UMD bundles with sourcemaps.
18
26
 
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.
27
+ ## Install
25
28
 
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.
29
+ ```bash
30
+ npm install bsv2
31
+ ```
32
+
33
+ Node.js 18 or newer is required.
34
+
35
+ ```js
36
+ // ESM (package.json "type": "module", or a .mjs file)
37
+ import { Bn, Tx, Script, KeyPair } from 'bsv2'
38
+
39
+ // CommonJS
40
+ const { Bn, Tx, Script, KeyPair } = require('bsv2')
41
+ ```
42
+
43
+ For the browser, use the UMD bundle from a CDN (it exposes the global `bsv2`):
30
44
 
31
- 6. Use standard javascript conventions wherever possible so that other
32
- developers find the code easy to understand.
45
+ ```html
46
+ <script src="https://unpkg.com/bsv2/dist/bsv2.umd.js"></script>
47
+ <script>
48
+ const { Bn, Tx } = bsv2
49
+ </script>
50
+ ```
33
51
 
34
- 7. Minimize the use of dependencies so that all code can be easily audited.
52
+ | Bundle | Use |
53
+ | --- | --- |
54
+ | `dist/bsv2.mjs` | ESM (`import`), browsers and modern bundlers |
55
+ | `dist/bsv2.cjs` | CommonJS (`require`) |
56
+ | `dist/bsv2.umd.js` | `<script>` tag / UMD (global `bsv2`) |
35
57
 
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.
58
+ Each bundle has a sibling `.map` file. The package version is also exported as
59
+ `version`, and selected third-party dependencies as `deps`.
39
60
 
40
- 9. Support web workers to unblock web wallet UIs when performing cryptography.
61
+ ## Quick start
41
62
 
42
- ## Environment Variables
63
+ ### Keys and addresses
43
64
 
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"
65
+ ```js
66
+ import { Address, KeyPair } from 'bsv2'
48
67
 
49
- You can change the network to run the CLI in testnet mode:
68
+ const keyPair = new KeyPair().fromRandom()
69
+ const address = Address.fromPubKey(keyPair.pubKey)
50
70
 
71
+ console.log(address.toString()) // 1...
51
72
  ```
52
- NETWORK=testnet ./bin/bsv.js
73
+
74
+ ### Mnemonic and HD wallet
75
+
76
+ ```js
77
+ import { Address, Bip32, Bip39 } from 'bsv2'
78
+
79
+ const mnemonic = new Bip39().fromRandom(128)
80
+ const seed = mnemonic.toSeed('') // optional BIP39 passphrase
81
+ const accountKey = Bip32.fromSeed(seed).derive("m/44'/0'/0'/0/0")
82
+
83
+ console.log(mnemonic.toString()) // twelve words for 128 bits of entropy
84
+ console.log(Address.fromPubKey(accountKey.pubKey).toString())
53
85
  ```
86
+
87
+ ### Sign and verify a transaction input
88
+
89
+ This signs a pay-to-pubkey input with `SIGHASH_ALL | SIGHASH_FORKID` and checks
90
+ it with the script interpreter. The funding transaction is fabricated here; in
91
+ an application you would obtain it from your indexer or node.
92
+
93
+ ```js
94
+ import { Bn, Interp, KeyPair, Script, Sig, Tx } from 'bsv2'
95
+
96
+ const keyPair = new KeyPair().fromRandom()
97
+ const scriptPubKey = new Script()
98
+ .writeBuffer(keyPair.pubKey.toDer(true))
99
+ .writeString('OP_CHECKSIG')
100
+
101
+ const valueBn = new Bn(1000)
102
+ const fundingTx = new Tx()
103
+ .addTxIn(Buffer.alloc(32), 0xffffffff, new Script(), 0xffffffff)
104
+ .addTxOut(valueBn, scriptPubKey)
105
+
106
+ const spendTx = new Tx()
107
+ .addTxIn(fundingTx.hash(), 0, new Script(), 0xffffffff)
108
+ .addTxOut(new Bn(900), scriptPubKey)
109
+
110
+ const nHashType = Sig.SIGHASH_ALL | Sig.SIGHASH_FORKID
111
+ const sig = spendTx.sign(keyPair, nHashType, 0, scriptPubKey, valueBn)
112
+ const scriptSig = new Script().writeBuffer(sig.toTxFormat())
113
+ spendTx.txIns[0].setScript(scriptSig)
114
+
115
+ const valid = new Interp().verify(
116
+ scriptSig,
117
+ scriptPubKey,
118
+ spendTx,
119
+ 0,
120
+ undefined,
121
+ valueBn
122
+ )
123
+ console.log(valid) // true
124
+ ```
125
+
126
+ `TxVerifier.verify(tx, txOutMap)` is available when you want to validate every
127
+ input of a transaction at once.
128
+
129
+ ### Pre-Genesis script policy
130
+
131
+ BSV Genesis raised several legacy Bitcoin limits. If you need to evaluate an
132
+ old script under the original rules, add `Interp.SCRIPT_VERIFY_PREGENESIS_LIMITS`
133
+ to the verification flags:
134
+
135
+ ```js
136
+ import { Bn, Interp, Script, Tx } from 'bsv2'
137
+
138
+ const flags = Interp.SCRIPT_VERIFY_P2SH |
139
+ Interp.SCRIPT_VERIFY_STRICTENC |
140
+ Interp.SCRIPT_VERIFY_PREGENESIS_LIMITS
141
+
142
+ const scriptSig = new Script().fromBitcoindString('2147483648 0 ADD')
143
+ const scriptPubKey = new Script().fromBitcoindString('NOP')
144
+
145
+ // false with the flag: 5-byte script numbers are rejected (4-byte limit)
146
+ const valid = new Interp().verify(scriptSig, scriptPubKey, new Tx(), 0, flags, new Bn(0))
147
+ ```
148
+
149
+ The flag restores: 4-byte script numbers, 520-byte push limit, 10 KB script
150
+ limit, 1000-element stack limit and the pre-Genesis disabled opcodes. It is not
151
+ part of `Interp.defaultFlags` and must be requested explicitly.
152
+
153
+ ## Chronicle release
154
+
155
+ Chronicle activates at mainnet height 943,835, testnet 1,713,022 and regtest
156
+ 15,000. The library exposes its rules without requiring a block height.
157
+
158
+ - Set `Sig.SIGHASH_CHRONICLE` (`0x20`) in a signature hash type to select the
159
+ Original Transaction Digest Algorithm (OTDA). ForkID signatures without it
160
+ keep using BIP143. OTDA ignores `valueBn`, and the bit may be mixed across
161
+ inputs.
162
+ - `tx.versionBytesNum > 1`, or `Interp.SCRIPT_VERIFY_CHRONICLE`, enables the
163
+ relaxed script policy: minimal encoding, Low-S, NULLDUMMY, clean stack and
164
+ push-only unlocking scripts are no longer enforced. Version-1 transactions
165
+ keep the existing checks. MINIMALIF and NULLFAIL are not enforced by this
166
+ library for either version.
167
+ - Restored opcodes: `OP_VER`, `OP_VERIF`, `OP_VERNOTIF`, `OP_SUBSTR`,
168
+ `OP_LEFT`, `OP_RIGHT`, `OP_2MUL`, `OP_2DIV`, `OP_LSHIFTNUM`, `OP_RSHIFTNUM`.
169
+ `OpCode.OP_SUBSTR_CHRONICLE`, `OP_LEFT_CHRONICLE`, `OP_RIGHT_CHRONICLE`,
170
+ `OP_LSHIFTNUM` and `OP_RSHIFTNUM` hold the new numbers because Genesis
171
+ reassigned the original ones.
172
+ - The script-number consensus limit rises to 32 MB; nodes may still set a
173
+ lower policy limit.
174
+
175
+ `Interp.getFlags('CHRONICLE')` includes the flag, which is not part of
176
+ `Interp.defaultFlags`. To sign with OTDA, add the bit to the hash type:
177
+
178
+ ```js
179
+ const nHashType = Sig.SIGHASH_ALL | Sig.SIGHASH_FORKID | Sig.SIGHASH_CHRONICLE
180
+ const sig = spendTx.sign(keyPair, nHashType, 0, scriptPubKey, valueBn)
181
+ ```
182
+
183
+ ## API map
184
+
185
+ All symbols below are exported from the package root (`entry.js`).
186
+
187
+ | Area | Exports |
188
+ | --- | --- |
189
+ | Keys and addresses | `KeyPair`, `PrivKey`, `PubKey`, `Address` |
190
+ | HD wallets / mnemonics | `Bip32`, `Bip39`, and the `en`, `es`, `jp` wordlists |
191
+ | Transactions | `Tx`, `TxIn`, `TxOut`, `TxOutMap`, `TxBuilder`, `TxVerifier`, `Sig`, `SigOperations` |
192
+ | Scripts | `Script`, `OpCode`, `Interp` |
193
+ | Blocks and mining | `Block`, `MinableBlock`, `BlockHeader`, `MiningUtils`, `getConstants` |
194
+ | Cryptography and math | `Bn`, `Point`, `Ecdsa`, `Hash`, `Random`, `Aes`, `Aescbc`, `Cbc`, `Ecies`, `Ach` |
195
+ | Serialization / utilities | `Struct`, `Br`, `Bw`, `Bsm`, `VarInt`, `Base58`, `Base58Check`, `cmp`, `Constants` |
196
+ | Workers | `Workers`, `WorkersResult` |
197
+ | Package | `deps`, `version` |
198
+
199
+ Additional modules under `lib/` (for example `Msg`, `Version`, `Merkle` and
200
+ `HashCache`) are used internally and are not re-exported from the package root.
201
+
202
+ For the full method-level reference, generate the HTML documentation:
203
+
204
+ ```bash
205
+ yarn doc # writes doc/index.html
206
+ ```
207
+
208
+ Instance methods generally mutate the object and return `this`. For example:
209
+
210
+ ```js
211
+ const tx = new Tx().addTxIn(...).addTxOut(...)
212
+ const hex = tx.toHex()
213
+ const sameTx = Tx.fromHex(hex)
214
+ ```
215
+
216
+ ## Development
217
+
218
+ ```bash
219
+ corepack yarn install # install from yarn.lock
220
+ yarn test:run # run the full suite once (Vitest)
221
+ yarn lint # ESLint
222
+ yarn build # build dist/bsv2.{mjs,cjs,umd.js}
223
+ yarn doc # regenerate API documentation
224
+ ```
225
+
226
+ The suite has over 8,500 tests, including reference vectors from bitcoind,
227
+ Bitcoin ABC and Bitcoin SV, plus dedicated tests for the pre-Genesis and
228
+ Chronicle policies. `yarn test` starts Vitest in watch mode.
229
+
230
+ Commits and release tags are GPG-signed, and `commit.gpgsign` is enabled for
231
+ future commits. Releases are published to npm and GitHub Releases with the same
232
+ version number. `resolutions` (Yarn) and `overrides` (npm) keep transitive
233
+ dependencies pinned to patched versions.
234
+
235
+ ## Differences from upstream `bsv` 2.x
236
+
237
+ - Published as **`bsv2`**; ESM-first with a matching CommonJS and UMD build.
238
+ - Default sighash behaviour follows BSV (`SIGHASH_FORKID`). Pre-Genesis rules
239
+ and the Chronicle release rules are opt-in flags.
240
+ - The `Bn` wrapper was rewritten and the script interpreter received targeted
241
+ correctness fixes; see the commit history for details.
242
+ - Japanese BIP39 vectors, `Buffer`/`pbkdf2` interoperability and mnemonic
243
+ whitespace validation were fixed.
244
+ - Pre-BIP143 sighash vectors are evaluated with forkid disabled, and
245
+ pre-Chronicle ABC vectors whose random `0x20` bit changed meaning are
246
+ excluded from the test suite.
247
+
248
+ ## Security
249
+
250
+ Please report security issues privately through the repository's security
251
+ advisories on GitHub rather than opening a public issue.
252
+
253
+ ## Credits and license
254
+
255
+ Based on the original `bsv` library by Manuel Araoz and contributors, with
256
+ contributions from the `bsv2` community. See `package.json` for the full
257
+ contributor list.
258
+
259
+ [MIT](./LICENSE)