npm.io
2.5.1 • Published 3d ago

bsv2

Licence
MIT
Version
2.5.1
Deps
9
Size
8.1 MB
Vulns
0
Weekly
0
Stars
2

bsv2

npm version license node

A JavaScript library for Bitcoin SV (BSV). bsv2 is the maintained continuation of the bsv 2.x codebase: same class-based API, published on npm as bsv2 and developed at github.com/samooth/bsv.

Features

  • Transactions and scripts with BSV SIGHASH_FORKID semantics.
  • Chronicle support: the Original Transaction Digest Algorithm (OTDA), the restored opcodes and the opt-in malleability policy.
  • A script interpreter with the standard verification flags, plus opt-in pre-Genesis and Chronicle policy modes.
  • Keys, addresses, BIP32 hierarchical deterministic wallets and BIP39 mnemonics (English, Spanish and Japanese wordlists).
  • Block and block-header serialization, mining utilities, and BEEF/BUMP SPV proofs.
  • ECIES and AES encryption helpers.
  • Web Worker support for cryptography-heavy operations.
  • Dependency-light, auditable source under lib/.
  • Ships as ESM, CommonJS and UMD bundles with sourcemaps.

Install

npm install bsv2

Node.js 20.19 or newer is required. The development toolchain uses Vite 7.

// ESM (package.json "type": "module", or a .mjs file)
import { Bn, Tx, Script, KeyPair } from 'bsv2'

// CommonJS
const { Bn, Tx, Script, KeyPair } = require('bsv2')

For the browser, use the UMD bundle from a CDN (it exposes the global bsv2):

<script src="https://unpkg.com/bsv2/dist/bsv2.umd.js"></script>
<script>
  const { Bn, Tx } = bsv2
</script>
Bundle Use
dist/bsv2.mjs ESM (import), browsers and modern bundlers
dist/bsv2.cjs CommonJS (require)
dist/bsv2.umd.js <script> tag / UMD (global bsv2)

Each bundle has a sibling .map file. The package version is also exported as version, and selected third-party dependencies as deps.

Quick start

Keys and addresses
import { Address, KeyPair } from 'bsv2'

const keyPair = new KeyPair().fromRandom()
const address = Address.fromPubKey(keyPair.pubKey)

console.log(address.toString()) // 1...
Mnemonic and HD wallet
import { Address, Bip32, Bip39 } from 'bsv2'

const mnemonic = new Bip39().fromRandom(128)
const seed = mnemonic.toSeed('') // optional BIP39 passphrase
const accountKey = Bip32.fromSeed(seed).derive("m/44'/0'/0'/0/0")

console.log(mnemonic.toString()) // twelve words for 128 bits of entropy
console.log(Address.fromPubKey(accountKey.pubKey).toString())
Sign and verify a transaction input

This signs a pay-to-public-key (P2PK) input with SIGHASH_ALL | SIGHASH_FORKID and checks it with the script interpreter. The funding transaction is fabricated here; in an application you would obtain it from your indexer or node.

import { Bn, Interp, KeyPair, Script, Sig, Tx } from 'bsv2'

const keyPair = new KeyPair().fromRandom()
const scriptPubKey = new Script()
  .writeBuffer(keyPair.pubKey.toDer(true))
  .writeString('OP_CHECKSIG')

const valueBn = new Bn(1000)
const fundingTx = new Tx()
  .addTxIn(Buffer.alloc(32), 0xffffffff, new Script(), 0xffffffff)
  .addTxOut(valueBn, scriptPubKey)

const spendTx = new Tx()
  .addTxIn(fundingTx.hash(), 0, new Script(), 0xffffffff)
  .addTxOut(new Bn(900), scriptPubKey)

const nHashType = Sig.SIGHASH_ALL | Sig.SIGHASH_FORKID
const sig = spendTx.sign(keyPair, nHashType, 0, scriptPubKey, valueBn)
const scriptSig = new Script().writeBuffer(sig.toTxFormat())
spendTx.txIns[0].setScript(scriptSig)

const valid = new Interp().verify(
  scriptSig,
  scriptPubKey,
  spendTx,
  0,
  undefined,
  valueBn
)
console.log(valid) // true

TxVerifier.verify(tx, txOutMap) is available when you want to validate every input of a transaction at once.

Pre-Genesis script policy

BSV Genesis raised several legacy Bitcoin limits. If you need to evaluate an old script under the original rules, add Interp.SCRIPT_VERIFY_PREGENESIS_LIMITS to the verification flags:

import { Bn, Interp, Script, Tx } from 'bsv2'

const flags = Interp.SCRIPT_VERIFY_P2SH |
  Interp.SCRIPT_VERIFY_STRICTENC |
  Interp.SCRIPT_VERIFY_PREGENESIS_LIMITS

const scriptSig = new Script().fromBitcoindString('2147483648 0 ADD')
const scriptPubKey = new Script().fromBitcoindString('NOP')

// false with the flag: 5-byte script numbers are rejected (4-byte limit)
const valid = new Interp().verify(scriptSig, scriptPubKey, new Tx(), 0, flags, new Bn(0))

The flag restores the 4-byte script number limit, the 520-byte push limit, the 10 KB script limit, the 1000-element stack limit and the pre-Genesis disabled opcodes. It is not part of Interp.defaultFlags and must be requested explicitly.

Chronicle release

Chronicle activates at mainnet height 943,835, testnet 1,713,022 and regtest 15,000. The library exposes its rules without requiring a block height.

  • Set Sig.SIGHASH_CHRONICLE (0x20) as part of a signature's hash type to select the Original Transaction Digest Algorithm (OTDA). ForkID signatures without it keep using BIP143. OTDA ignores valueBn, and the bit may be mixed across inputs.
  • A transaction version above 1, or the Interp.SCRIPT_VERIFY_CHRONICLE flag, enables the relaxed script policy: minimal encoding, Low-S, MINIMALIF, NULLFAIL, NULLDUMMY, clean stack and push-only unlocking scripts are no longer enforced. Version-1 transactions keep the checks enabled by the flags.
  • Restored opcodes: OP_VER, OP_VERIF, OP_VERNOTIF, OP_SUBSTR, OP_LEFT, OP_RIGHT, OP_2MUL, OP_2DIV, OP_LSHIFTNUM, OP_RSHIFTNUM. OpCode.OP_SUBSTR_CHRONICLE, OP_LEFT_CHRONICLE, OP_RIGHT_CHRONICLE, OP_LSHIFTNUM and OP_RSHIFTNUM hold the new numbers because Genesis repurposed the original opcodes.
  • The script-number consensus limit rises to 32 MB; nodes may still set a lower policy limit.

Interp.getFlags('CHRONICLE') includes the flag, which is not part of Interp.defaultFlags. To sign with OTDA, add the bit to the hash type:

const nHashType = Sig.SIGHASH_ALL | Sig.SIGHASH_FORKID | Sig.SIGHASH_CHRONICLE
const sig = spendTx.sign(keyPair, nHashType, 0, scriptPubKey, valueBn)

API map

All symbols below are exported from the package root (entry.js).

Area Exports
Keys and addresses KeyPair, PrivKey, PubKey, Address
HD wallets / mnemonics Bip32, Bip39, and the en, es, jp wordlists
Transactions Tx, TxIn, TxOut, TxOutMap, TxBuilder, TxVerifier, Sig, SigOperations
Scripts Script, OpCode, Interp
Blocks and mining Block, MinableBlock, BlockHeader, MiningUtils, getConstants
Cryptography and math Bn, Point, Ecdsa, Hash, Random, Aes, Aescbc, Cbc, Ecies, Ach
Serialization / utilities Struct, Br, Bw, Bsm, VarInt, Base58, Base58Check, cmp, Constants
Workers Workers, WorkersResult
BRC helpers P2PKH, RPuzzle, PushDrop, AesGcm, SafeRecord, Reader, Writer, Beef, Bump
Fees and sizing FeeModel, SatoshisPerKilobyte, varUintSize, transactionInputSize, transactionOutputSize, transactionSize
Package deps, version

Additional modules under lib/ (for example Msg, Version, Merkle and HashCache) are used internally and are not re-exported from the package root.

For the full method-level reference, generate the HTML documentation:

yarn doc   # writes doc/index.html

Instance methods generally mutate the object and return this. For example:

const tx = new Tx().addTxIn(...).addTxOut(...)
const hex = tx.toHex()
const sameTx = Tx.fromHex(hex)

Development

corepack yarn install       # install from yarn.lock
yarn test:run               # run the full suite once (Vitest)
yarn lint                   # ESLint
yarn build                  # build dist/bsv2.{mjs,cjs,umd.js}
yarn doc                    # regenerate API documentation

The suite has over 8,500 tests, including reference vectors from bitcoind, Bitcoin ABC and Bitcoin SV, dedicated pre-Genesis and Chronicle tests, and deterministic property tests for script, Bn and transaction round-trips. yarn test starts Vitest in watch mode.

Scripts with truncated pushdata are parsed leniently and rejected during execution; the pre-Genesis reference vectors rely on that behavior.

Commits and release tags are GPG-signed, and commit.gpgsign is enabled for future commits. Releases are published to npm and GitHub Releases with the same version number. resolutions (Yarn) and overrides (npm) keep transitive dependencies pinned to patched versions.

Differences from upstream bsv 2.x

  • Published as bsv2; ESM-first with a matching CommonJS and UMD build.
  • Default sighash behavior follows BSV (SIGHASH_FORKID). Pre-Genesis rules and the Chronicle release rules are opt-in flags.
  • The Bn wrapper was rewritten and the script interpreter received targeted correctness fixes; see the commit history for details.
  • Japanese BIP39 vectors, Buffer/pbkdf2 interoperability and mnemonic whitespace validation were fixed.
  • Pre-BIP143 sighash vectors are evaluated with ForkID disabled, and pre-Chronicle ABC vectors whose random 0x20 bit changed meaning are excluded from the test suite.

Ecosystem: BRC-100 and ts-stack

bsv2 is a low-level primitives library: transactions, scripts, keys, cryptography and serialization. It does not implement the BRC-100 wallet-to-application interface.

For BRC-100 integrations use the BSV Association reference implementations in bsv-blockchain/ts-stack:

  • @bsv/sdk — primitives, identities, overlays and the BRC-100 WalletClient.
  • @bsv/wallet-toolbox — BRC-100 storage, services, signing and wallet tooling (WalletSigner, baskets, BEEF, BKDS key derivation).

See BRC-100 for the normative interface.

BRC helpers

  • P2PKH, RPuzzle and PushDrop build locking/unlocking scripts from BRC-16, BRC-17 and BRC-48.
  • AesGcm implements AES-256-GCM with BRC-2 framing (IV || ciphertext || tag, with a 32-byte IV).
  • SatoshisPerKilobyte computes a fee from the exact serialized size; the transactionSize helpers can estimate it before signing.
  • Reader/Writer are bounds-checked byte helpers with canonical varint decoding; SafeRecord rejects prototype-polluting keys in untrusted JSON.

BEEF (BRC-62) and BUMP (BRC-74) can be parsed/serialized with Beef and Bump. Bump.computeRoot() verifies inclusion proofs; header lookup is left to the caller (ChainTracker-style service):

import { Beef } from 'bsv2'

const beef = Beef.fromHex(beefHex)
const anchored = beef.transactions.find(entry => entry.bumpIndex !== undefined)
const merkleRoot = beef.bumps[anchored.bumpIndex].computeRoot(anchored.tx.id())

beef.topologicalOrder() returns the transactions oldest first.

Known differences from the C++ reference

  • Activation is expressed with SCRIPT_VERIFY_CHRONICLE or a transaction version above 1; the library has no block height, so policy is not tied to a chain height.
  • Pre-Genesis rules are selected explicitly with SCRIPT_VERIFY_PREGENESIS_LIMITS; outside Chronicle some legacy policy differences (for example multiple OP_ELSE) are permitted because older reference vectors rely on them.
  • Sig.toTxFormat() serializes the hash type as one byte, as required inside scripts; C++ SigHashType also supports a 32-bit form with a fork value.
  • Bip39 buffer serialization stores the mnemonic and seed but not the wordlist; re-create non-English mnemonics with Bip39Jp/Bip39Es after a binary round-trip.

Security

Please report security issues privately through the repository's security advisories on GitHub rather than opening a public issue.

Credits and license

Based on the original bsv library by Manuel Araoz and contributors, with contributions from the bsv2 community. See package.json for the full contributor list.

MIT

Keywords