Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@bsv/sdk

bitcoin-sv7.2kSEE LICENSE IN LICENSE.txt1.4.20TypeScript support: included

BSV Blockchain Software Development Kit

BSV, Blockchain, SDK, Bitcoin, SV

readme

BSV SDK

BSV BLOCKCHAIN | Software Development Kit for JavaScript and TypeScript

Welcome to the BSV Blockchain Libraries Project, the comprehensive TypeScript SDK designed to provide an updated and unified layer for developing scalable applications on the BSV Blockchain. This SDK addresses the limitations of previous tools by offering a fresh, peer-to-peer approach, adhering to SPV, and ensuring privacy and scalability.

Table of Contents

  1. Objective
  2. Getting Started
  3. Features & Deliverables
  4. Documentation
  5. Contribution Guidelines
  6. Support & Contacts

Objective

The BSV Blockchain Libraries Project aims to structure and maintain a middleware layer of the BSV Blockchain technology stack. By facilitating the development and maintenance of core libraries, it serves as an essential toolkit for developers looking to build on the BSV Blockchain.

Getting Started

Installation

To install the SDK, run:

npm install @bsv/sdk

Basic Usage

Here's a simple example of using the SDK to create and sign a transaction:

import { PrivateKey, P2PKH, Transaction, ARC } from '@bsv/sdk'

const privKey = PrivateKey.fromWif('L5EY1SbTvvPNSdCYQe1EJHfXCBBT4PmnF6CDbzCm9iifZptUvDGB')

const sourceTransaction = Transaction.fromHex('0200000001849c6419aec8b65d747cb72282cc02f3fc26dd018b46962f5de48957fac50528020000006a473044022008a60c611f3b48eaf0d07b5425d75f6ce65c3730bd43e6208560648081f9661b0220278fa51877100054d0d08e38e069b0afdb4f0f9d38844c68ee2233ace8e0de2141210360cd30f72e805be1f00d53f9ccd47dfd249cbb65b0d4aee5cfaf005a5258be37ffffffff03d0070000000000001976a914acc4d7c37bc9d0be0a4987483058a2d842f2265d88ac75330100000000001976a914db5b7964eecb19fcab929bf6bd29297ec005d52988ac809f7c09000000001976a914c0b0a42e92f062bdbc6a881b1777eed1213c19eb88ac00000000')

const version = 1
const input = {
  sourceTransaction,
  sourceOutputIndex: 0,
  unlockingScriptTemplate: new P2PKH().unlock(privKey),
}
const output = {
  lockingScript: new P2PKH().lock(privKey.toAddress()),
  change: true
}

const tx = new Transaction(version, [input], [output])
await tx.fee()
await tx.sign()

await tx.broadcast()

For a more detailed tutorial and advanced examples, check our Documentation.

Features & Deliverables

  • Sound Cryptographic Primitives: Secure key management, signature computations, and encryption protocols.

  • Script Level Constructs: Network-compliant script interpreter with support for custom scripts and serialization formats.

  • Transaction Construction and Signing: Comprehensive transaction builder API, ensuring versatile and secure transaction creation.

  • Transaction Broadcast Management: Mechanisms to send transactions to both miners and overlays, ensuring extensibility and future-proofing.

  • Merkle Proof Verification: Tools for representing and verifying merkle proofs, adhering to various serialization standards.

  • Serializable SPV Structures: Structures and interfaces for full SPV verification.

  • Secure Encryption and Signed Messages: Enhanced mechanisms for encryption and digital signatures, replacing outdated methods.

Documentation

The Docs are available here on Github pages.
Example code is available over on our gitbook.

The SDK is richly documented with code-level annotations. This should show up well within editors like VSCode.

Contribution Guidelines

We're always looking for contributors to help us improve the SDK. Whether it's bug reports, feature requests, or pull requests - all contributions are welcome.

  1. Fork & Clone: Fork this repository and clone it to your local machine.
  2. Set Up: Run npm install to install all dependencies.
  3. Make Changes: Create a new branch and make your changes.
  4. Test: Ensure all tests pass by running npm test.
  5. Commit: Commit your changes and push to your fork.
  6. Pull Request: Open a pull request from your fork to this repository. For more details, check the contribution guidelines.

For information on past releases, check out the changelog. For future plans, check the roadmap!

Support & Contacts

Project Owners: Thomas Giacomo and Darren Kellenschwiler

Development Team Lead: Ty Everett

For questions, bug reports, or feature requests, please open an issue on GitHub or contact us directly.

License

The license for the code in this repository is the Open BSV License. Refer to LICENSE.txt for the license text.

Thank you for being a part of the BSV Blockchain Libraries Project. Let's build the future of BSV Blockchain together!

changelog

CHANGELOG for @bsv/sdk

All notable changes to this project will be documented in this file. The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Table of Contents

[Unreleased]

Added

Changed

Deprecated

Removed

Fixed

Security


[1.4.20] - 2025-04-23

Fixed

  • Export HTTPWalletJSON substrate.

[1.4.19] - 2025-04-09

Added

  • find, list, and renew functions to the StorageUploader class along with tests for each

[1.4.18] - 2025-04-02

Changes

  • LocalKVStore: Additional testing, passing.

Added

  • WERR_REVIEW_ACTIONS error class and support through HTTPWalletJson

[1.4.17] - 2025-04-01

Added

  • Key value storage implementation

[1.4.16] - 2025-04-01

Fix

  • Updating jest.config.js to remove warning of use of globals

Security

  • Updating dependencies

[1.4.15] - 2025-03-31

Change

  • Simplified AuthFetch error handling to avoid intercepting external errors, improving the developer experience.

[1.4.14] - 2025-03-31

Change

  • Tx fee update to 1 sat per kilobyte as default.

[1.4.13] - 2025-03-30

Fix

  • Exported DRBG from Primitives
  • Added Dev Dependencies @types/node and @jest/globals

[1.4.12] - 2025-03-28

Fix

  • Removed registryOperator check from token parseLockingScript helper function.

[1.4.11] - 2025-03-25

Fix

  • Sorts AuthFetch request and response headers by key before serialization to ensure a consistent order for signing and verification.

[1.4.10] - 2025-03-24

Fix

  • Fix signature pre-image mismatch caused by browser-modified content-type header in AuthFetch.

[1.4.9] - 2025-03-18

Add

  • Add action status 'failed' which is the result of abortAction.

[1.4.8] - 2025-03-17

Fixed

  • Fix timeout in SHIP broadcaster

[1.4.7] - 2025-03-17

Fixed

  • Fixed midding headers in StorageUploader
  • Added check for expiryTime in StorageDownloader
  • Fix timeout in lookup resolver

[1.4.6] - 2025-03-15

Fixed

  • Fixed a bug in local-only broadcast mode

[1.4.5] - 2025-03-15

Fixed

  • Fixed a bug in local-only broadcast mode

[1.4.4] - 2025-03-15

Fixed

  • Fixed allow HTTP bug in broadcast facilitator

[1.4.3] - 2025-03-14

Fixed

  • Bug fixes and removed ambiguity with the registration process in the RegistryClient.

[1.4.2] - 2025-03-13

Added

  • Implement UHRP Storage Downloader

[1.4.1] - 2025-03-12

Added

  • Implement IdentityClient
  • Implement RegistryClient

[1.4.0] - 2025-03-10

Added

  • Implement UHRP Storage Uploader
  • Implement UHRP Storage Downloader
  • Test coverage WIP

[1.3.36] - 2025-03-7

Fixed

  • AuthFetch waits for pending certificate requests before exchanging data

[1.3.35] - 2025-03-7

Fixed

  • Beef toBinary was missing a sortTxs() call.

[1.3.34] - 2025-03-6

Added

  • Support for the Babbage testnet SLAP tracker
  • Support for overlay network presets for mainnet, testnet, or local

[1.3.33] - 2025-03-6

Fixed

  • Add support for privileged certificates in MasterCertificate and VerifiableCertificate classes.

[1.3.32] - 2025-03-6

Fixed

  • Removed ambiguity with the keyID used in master and verifiable certificate field encryption.

[1.3.31] - 2025-03-6

Fixed

  • Beef verify now checks that BUMPs contain required txids.

[1.3.30] - 2025-03-5

Fixed

  • Re-handshake in AuthFetch if a server forgets about a session that the client maintained

[1.3.29] - 2025-03-5

Added

  • Concurrent session management for the same peer across devices

[1.3.28] - 2025-02-28

Fixed

  • Persisted payee derivation information in AuthFetch
  • Use the first output index for AuthFetch payments

[1.3.27] - 2025-02-28

Fixed

  • Added defaults for undefined AuthFetch request body (specifically for content-type of application/json).
  • This prevents signature verification errors due to express defaults for requests with undefined body.

[1.3.26] - 2025-02-28

Fixed

  • Fixed a bug with AuthFetch where when responding to error 402, the derivationSuffix was not sent to the server.
  • Updated used createNonce for the derivationSuffix creation to link it to the sender.

[1.3.25] - 2025-02-27

Fixed

  • Previously, the function split each character’s 16-bit code unit into two bytes (if the high byte was non-zero), which only worked for ASCII and failed on non-ASCII/multi-byte characters. Now emojis can be encoded correctly!

[1.3.24] - 2025-02-22

Added

  • Originator support to PushDrop template

[1.3.23] - 2025-02-21

Fixed

  • Fixed a bug with SHIPCast's default configuration.

[1.3.22] - 2025-02-19

Fixed

  • Fixed a bug with how the HTTPWalletJSON response was parsed to check for errors.

[1.3.21] - 2025-02-17

Fixed

  • Update type returned in Hash.SHA512._digestHex method so that it doesn't fail tsc builds.

[1.3.20] - 2025-02-17

Fixed

  • ATOMIC BEEF should use little endian encoding of the txid, rather than big endian.

[1.3.19] - 2025-02-16

Change

  • Make URL, httpClient, getHttpHeaders protected instead of private to support extending WhatsOnChain class.

[1.3.18] - 2025-02-12

Fixed

  • Avoid collapsing doc details. Not supported by github pages.

[1.3.17] - 2025-02-11

Fixed

  • Added error handling to HTTPWalletJSON

[1.3.15] - 2025-02-07

Fixed

  • Hot fix for SimplifiedFetchTransport due to a bug introduced with linting fixes.

[1.3.14] - 2025-02-07

Changed

  • Fixed build issue, and conformed to ts-standard

[1.3.12] - 2025-01-29

Changed

  • Modified ProtoWallet to make the KeyDeriver optional, and to allow an optional originator argument on each supported method.
  • This allows any Wallet that implements WalletInterface to be a ProtoWallet.

[1.3.11] - 2025-01-28

Changed

  • Refactored certificate classes and helper functions based on usage requirements in the new acquireCertificate method via the issuance path.
  • Changed certain MasterCertificate methods to static functions for use-cases where an instantiated class isn't convenient.

[1.3.10] - 2025-01-27

Changed

  • Create and Verify Nonce utility functions now support optional counterparty param.
  • This enables the verification of nonces created by a counterparty.

Changed

  • Export WalletInterface and ProtoWallet but not Wallet
  • ProtoWallet does not deal with privilege or originators

[1.3.8] - 2025-01-20

Fixed

  • Fixed the double export of Beef class as named and default which was messing up the constructor somehow. Now it's a named export and that means the BEEF_V1 and V2 constants are also available as exported values.

[1.3.7] - 2025-01-18

Fixed

  • Fixed the double export of Beef class as named and default which was messing up the constructor somehow. Now it's a named export and that means the BEEF_V1 and V2 constants are also available as exported values.

[1.3.6] - 2025-01-17

Added

  • Consolidated certificate functionality
  • Added certificate tests

[1.3.4] - 2025-01-17

Changed

Changes to cleanup and normalize types in Wallet.interfaces.ts and dependent files.

  • Wallet.interfaces.ts

  • KeyDeriver.ts

  • CachedKeyDeriver.ts
  • ProtoWallet.ts

  • WalletClient.ts

  • HttpWalletJSON.ts
  • WalletWireTranceiver.ts
  • XDM.ts
  • window.CWI.ts

Added

  • WalletCrypto.ts: Pulled out of ProtoWallet for reuse as a base class.

[1.3.3] - 2025-01-13

Changed

  • Removed unnecessary length byte of signatures in the encoding of a Certificate in Binary.

[1.3.2] - 2025-01-13

Added

  • Support for last session persistence in Peer

[1.3.1] - 2025-01-13

Fixed

  • Updated the Auth SimplifiedFetchTransport to bind window.fetch in a browser context.

[1.3.0] - 2025-01-11

Changed

  • Minor Version release (should have been done at 1.2.21 due to Mutual Authentication Scope increase)
  • Renamed toBin => toBinary and fromBin => fromBinary so that the methods match other classes.

[1.2.22] - 2025-01-06

Fixed

  • 158

  • Export certificate helpers

[1.2.21] - 2025-01-03

Added

  • Implemented a Mutual Authentication and Service Monetization Framework

[1.2.19] - 2024-12-19

Added

  • new method on the interface for ChainTrackers which returns the current height of the blockchain.
  • Implemented a Coinbase specific MerklePath .verify conditional block which ensures the input is spendable if it's a coinbase output.

[1.2.18] - 2024-12-19

Added

  • Enumerated constants for the tx data type in beef encoded transaction lists.

Removed

  • Removed the idea of "V1" and "V2" beef. There is already a version number which is encoded in the bytes, I think it's clearer if we stick to one.
  • Removed the V1 capability to use txidOnly.

Fixed

  • There was a slice of the BeefTxs array in Beef which should be a splice (remove a tx)

Changed

  • Refactored a bunch of functions on advice from Sonar Qube which suggested they were a little to dense for most people to follow.
  • Refactored for loops into for-of loops where possible.
  • Refactored loops where the loop counter is updated within the loop which is advised against.

[1.2.17] - 2024-12-18

Added

Beef addComputedLeaves MerklePath.fromReader and MerklePath constructor legalOffsetsOnly optional argument

Fixed

Beef toBinaryAtomic now prunes transactions newer than target txid. Beef mergeBeefTx error if both isTxidOnly Bug #162


[1.2.15] - 2024-12-16

Added

  • A helper function for most commonly used output type. tx.addP2PKHOutput(address, satoshis)
  • Make sure to check the provided hash is 20 bytes.

[1.2.14] - 2024-12-14

Added

  • fromCoinbaseTxidAndHeight method to the MerklePath class for that special case
  • added test demonstrating how to validate scripts only for one tx even when you don't have a merkle path for the previous tx.
    // merklePath just has to be set to any object.
    sourceTransaction.merklePath = { assumeValid: true }
    tx.inputs[0].sourceTransaction = sourceTransaction
    await tx.verify('scripts only')

[1.2.13] - 2024-12-13

Added

  • JSON HTTP substrate, with corresponding swagger-ui documentation.
  • Example args and responses to the swagger-ui

Added

  • PrivateKey test linter errors

[1.2.12] - 2024-12-12

Added

  • Added optional broadcastMany to Broadcaster interface.

[1.2.11] - 2024-12-10

Added

  • Added a fix to ensure PrivateKeys as hex are always 64 chars in length ensuring 256 bit keys.

[1.2.10] - 2024-12-10

Added

Beef makeTxidOnly

Fixed

Beef sortTx fix for partially valid and txidOnly data.

Security


[1.2.9] - 2024-12-06

Added

  • Added support for returning coinsRemoved in an overlay Submitted Transaction Execution AcKnowledgment (STEAK) message.

[1.2.8] - 2024-12-02

Added

  • Stop people inadvertently creating corrupted public keys.

[1.2.7] - 2024-12-02

Added

  • Define the security level of protocolIDs as a type so that we can attach a JSDoc explaining what each value means.

Added

  • Use a default value in the async fee() function when no args are used rather than requiring a value and then setting if undefined.

[1.2.6] - 2024-11-30

Added

  • revealSpecificKeyLinkage requires a counterparty.
  • ProtoWallet now correctly implements the wallet interface.

[1.2.5] - 2024-11-30

Added

  • Testnet capabilities & config override for node to function without error to defaultBroadcaster
  • broadcastMany function to ARCBroadcaster

[1.2.4] - 2024-11-29

Added

  • A "random" distributioun mode for change allocation which approximates Benford's Law in attempt to distribute the remainder of: (inputSats - knownOutputSats - txFee) across the outputs marked "change: true".

[1.2.3] - 2024-11-26

Added

  • Added support for Overlay broadcast and lookup timeouts.

[1.2.2] - 2024-11-26

Added

  • Updated the default ShipBroadcast config, tests, and docs.

[1.2.1] - 2024-11-25

Added


[1.2.0] - 2024-11-25

Added

  • Implement BRC-100 wallet interface
  • Add PushDrop token template
  • SHIP and SLAP overlay tooling including broadcaster and lookup

[1.1.33] - 2024-11-22

Added

Beef.toBinaryAtomic(txid: string) to serialize Beef with AtomicBEEF header.

Fixed

Transaction.fromAtomicBEEF

  1. Test for all required dependencies was ignoring BUMP hashes.
  2. Test for unused transactions required additional fix.

[1.1.32] - 2024-11-22

Added

  1. allowPartial optional argument to Transaction toBEEF and toAtomicBEEF to avoid errors on missing sourceTransactions.
  2. findBump, findTransactionForSigning, findAtomicTransaction to Beef class.
  3. mergeBeefFromParty to BeefParty
  4. isValid to BeefTx, used by new sortTx.
  5. Add serialized AtomicBEEF deserialization to Beef class.
  6. Add atomicTxid property to Beef class.

Fixed

  1. sortTxs of Beef class fixed to handle incompletely valid data, order now matches spec for isValid true BEEF.

Removed

  1. degree property from BeefTx class, was used only by original sortTxs algorithm.

[1.1.30] - 2024-11-02

Added

  • Feature - Schorr class which allows ZKP creation and verification. BRC-94 and BRC-100 related.

Changed

  • electrumEncrypt correctly allows fromPrivateKey to be omitted

[1.1.29] - 2024-10-23

Fixed

  • ECIES ElectrumDecrypt counterparty decryption bug

[1.1.28] - 2024-10-23

Added

  • UMD support added for use in non-standard environments.

[1.1.26] - 2024-10-22

Added

  • Atomic BEEF serializer and deserializer for Transaction class
  • Ability to select a TXID from a BEEF when construction a Transaction.fromBEEF()

[1.1.25] - 2024-10-21

Added

  • Beef, BeefTx, and BeefParty classes

[1.1.24] - 2024-10-04

Fixed

  • Addressed #125
  • Optimized SPV verification

[1.1.22] - 2024-09-02

Added

  • Base64 mode support for BSM

[1.1.21] - 2024-09-02

Added

  • Broadcaster types have been updated to better align with Arc
  • Added some convenience functions for toDER and fromDER on the point and pubkey classes where missing.

    Changed

  • In the PublicKey class pubkey.toDER() to defaults to return a number[] rather than string. If a string is desired .toString() or .toDER('hex') have equivalent functionality.

[1.1.17] - 2024-08-21

Added

Transaction static fromReader method is now public.

[1.1.14] - 2024-07-30

Added

Ability to split a private key into shares using Shamir's Secret Sharing Scheme. Use like this:

const key = PrivateKey.fromRandom()
const recovery = key.split(2, 5)
const sameKey = PrivateKey.fromShares(recovery.shares, recovery.threshold)

[1.1.13] - 2024-07-19

Fixed

Transaction SPV verification now correctly returns true for "scripts only" verification when a merkle proof is found.

[1.1.10] - 2024-06-28

Fixed

ARC Broadcaster correctly parses status -> code, details -> description. Adds optional txid and more to error response if provided.

[1.1.8] - 2024-06-19

Added

TOTP class which allows the generation of time based pass codes. Use varies but originally included for validating shared secrets between remote counterparties over a secure channel.

[1.1.6] - 2024-06-12

Added

  • Allow Fees in historic transaction to be validated against a FeeModel.

[1.1.5] - 2024-06-11

Fixed

  • Unnecessary Buffer.from in pbkdf2 function has been removed.

[1.1.4] - 2024-05-10

Added

  • MerklePath trim function which removes data if it can be calculated. Backported from go-sdk by tonesnotes. Only affects compound Merkle paths which are not yet widely used.

Changed

  • The corresponding error messages associated with invalid MerklePaths which no longer check empty levels if they are above level 0.

[1.1.0] - 2024-05-06

Added

  • Ability to create TransactionInputs from a utxo, creating a partial sourceTransaction. Use is like so: `javascript const input = fromUtxo({ txid: '434555433eaca96dff6e71a4d02febd0dd3832e5ca4e5734623ca914522e17d5', vout: 0, script: '76a914d01b0b702ee90e00944342f97c772a8be83e42a288ac', satoshis: 1234 }, new P2PKH().unlock(key))

tx.addInput(input)

- Ability to create a transaction from Extended Format bytes or hex. The result being a partial sourceTransaction in each input.
```javascript
const tx = Transaction.fromHexEF('020000000000000000ef01b2faffe1e1d3c88f4092f34646c060ea2b6a93acc3010484c747ed4c051c2555080000006a4730440220392bcec91f190ce38db9bf53d03886ab63d9bd24fcf7174e8a8df21d23382ba7022038f20c1f3f6583951d01af0be30612a6c0b46d949b4aae60f42644ce513f3e55412103ea0ff49ec6fbb9cbc942d9c1fce9c04e12a91c1209b239466e0a29147da55db1ffffffff01f45500000000001976a914de337957f543c8d1fad2cfff0b57bb5b4264d91788ac0390010000000000001976a9144d255baa50a14bef4cce1eb8012a02768e8ffaa888acd3600000000000001976a91447e22d8011bb446cc3f606179e333f64a9b6206b88ac04915500000000001976a914d24cb016397008a85c88b1278a36434fdd4e801f88ac00000000')

Removed

  • Use of sourceSatoshis as a parameter of TransactionInput type.

Fixed

New Contributors

[1.0.0] - 2024-02-10

Added

  • Initial release of the BSV Blockchain Libraries Project SDK.
  • Sound Cryptographic Primitives for key management, signature computations, and encryption protocols.
  • Script Level Constructs with network-compliant script interpreter.
  • Comprehensive Transaction Construction and Signing API.
  • Mechanisms for Transaction Broadcast Management.
  • Tools for Merkle Proof Verification and representation.
  • Structures and interfaces for full Serializable SPV Structures.
  • Enhanced mechanisms for Secure Encryption and Signed Messages.

Template for New Releases:

Replace X.X.X with the new version number and YYYY-MM-DD with the release date:

## [X.X.X] - YYYY-MM-DD

### Added
- 

### Changed
- 

### Deprecated
- 

### Removed
- 

### Fixed
- 

### Security
- 

Use this template as the starting point for each new version. Always update the "Unreleased" section with changes as they're implemented, and then move them under the new version header when that version is released.