Skip to main content

BSV BLOCKCHAIN | Software Development Kit for Python

Project description

BSV SDK

build Coverage PyPI version Python versions

Welcome to the BSV Blockchain Libraries Project, the comprehensive Python 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. Testing & Quality
  6. Tutorial
  7. Contribution Guidelines
  8. 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

Requirements

Python 3.10 or higher pip package manager

Installation

pip install bsv-sdk

Development Setup

For contributors and developers, install with test and dev dependencies:

pip install -e .[test,dev]

This installs the package in development mode along with all testing and development dependencies.

Basic Usage

import asyncio
from bsv import (
    PrivateKey, P2PKH, Transaction, TransactionInput, TransactionOutput
)


# Replace with your private key (WIF format)
PRIVATE_KEY = 'KyEox4cjFbwR---------VdgvRNQpDv11nBW2Ufak'

# Replace with your source tx which contains UTXO that you want to spend (raw hex format)
SOURCE_TX_HEX = '01000000018128b0286d9c6c7b610239bfd8f6dcaed43726ca57c33aa43341b2f360430f23020000006b483045022100b6a60f7221bf898f48e4a49244e43c99109c7d60e1cd6b1f87da30dce6f8067f02203cac1fb58df3d4bf26ea2aa54e508842cb88cc3b3cec9b644fb34656ff3360b5412102cdc6711a310920d8fefbe8ee73b591142eaa7f8668e6be44b837359bfa3f2cb2ffffffff0201000000000000001976a914dd2898df82e086d729854fc0d35a449f30f3cdcc88acce070000000000001976a914dd2898df82e086d729854fc0d35a449f30f3cdcc88ac00000000'

async def create_and_broadcast_transaction():
    priv_key = PrivateKey(PRIVATE_KEY)
    source_tx = Transaction.from_hex(SOURCE_TX_HEX)

    tx_input = TransactionInput(
        source_transaction=source_tx,
        source_txid=source_tx.txid(),
        source_output_index=1,
        unlocking_script_template=P2PKH().unlock(priv_key),
    )

    tx_output = TransactionOutput(
        locking_script=P2PKH().lock(priv_key.address()),
        change=True
    )

    tx = Transaction([tx_input], [tx_output], version=1)

    tx.fee()
    tx.sign()

    await tx.broadcast()

    print(f"Transaction ID: {tx.txid()}")
    print(f"Raw hex: {tx.hex()}")

if __name__ == "__main__":
    asyncio.run(create_and_broadcast_transaction())

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

Features & Deliverables

Advanced Transaction Building:

  • Support for P2PKH, P2PK, OP_RETURN, and BareMultisig scripts
  • Automated fee calculation and change output management
  • Custom script development
  • Support for various SIGHASH types

HD Wallet Capabilities:

  • Full BIP32/39/44 implementation for hierarchical deterministic wallets
  • Multiple language support for mnemonic phrases (English, Chinese)
  • Advanced key derivation and management

SPV & Validation:

  • Built-in SPV verification with BEEF format support
  • Merkle proof validation
  • Efficient transaction broadcast with Arc
  • Support for chain tracking and verification

Wallet Infrastructure:

  • Complete wallet implementation with BIP270 payment protocols
  • Action serializers for creating, signing, and broadcasting transactions
  • Substrate support for various wallet backends (HTTP, Wire protocol)
  • Key derivation with caching for performance

Authentication & Security:

  • Peer-to-peer authentication with certificate management
  • Session handling with automatic renewal
  • Multiple transport protocols (HTTP, simplified transports)
  • Encrypted communications with AES-GCM

Script Interpreter:

  • Full Bitcoin script execution engine
  • Comprehensive opcode support (arithmetic, crypto, stack operations)
  • Configurable script flags for different validation modes
  • Thread-based execution for complex scripts

Storage & Overlay Services:

  • Upload/download interfaces with encryption support
  • Overlay network tools (SHIP broadcaster, lookup resolver)
  • Historian for tracking overlay data
  • Host reputation tracking
  • Registry client for overlay management

Identity & Registry:

  • Identity client with certificate management
  • Contacts manager for identity relationships
  • Registry services for overlay network coordination
  • Headers client for blockchain synchronization

Enhanced Cryptography & Protocols:

  • Schnorr signatures for advanced signing schemes
  • DRBG (Deterministic Random Bit Generator)
  • BSM (Bitcoin Signed Message) compatibility
  • ECIES encryption compatibility
  • TOTP (Time-based One-Time Password) 2FA support
  • BIP-276 payment destination encoding
  • PushDrop token protocol implementation
  • Teranode broadcaster support

Documentation

Detailed documentation of the SDK with code examples can be found at BSV Skills Center.

You can also refer to the User Test Report for insights and feedback provided by Yenpoint.

Testing & Quality

This project maintains high code quality standards with comprehensive test coverage:

  • 5,400+ tests covering core functionality
  • 84.6%+ code coverage across the entire codebase
  • Automated testing with GitHub Actions CI/CD
  • Python 3.10, 3.11, 3.12, and 3.13 supported

Running Tests & Coverage

# Install test dependencies
pip install -e .[test]

# Run all tests
pytest

# Run tests with coverage analysis (includes branch coverage)
pytest --cov=bsv --cov-branch --cov-report=html --cov-report=term

# View detailed coverage report
xdg-open htmlcov/index.html

We welcome contributions that improve test coverage, especially in currently under-tested areas.

Chronicle Live Tests

The SDK includes a comprehensive live test suite for the Chronicle network upgrade, located in tests/bsv/live/. These tests validate real transaction building, signing, script execution, and broadcasting across all sighash flag combinations and script types.

Mock Tests (no network required)

250 tests that build real Transaction objects, sign them with real keys, and validate every input through the Spend script interpreter — without touching the network:

# Run all mock live tests
pytest tests/bsv/live/ -v -m "not testnet"
Test File Tests Coverage
test_live_sighash_matrix.py 102 All 12 sighash flags x 2 tx versions x P2PKH/P2PK/Multisig
test_live_chronicle_opcodes.py 43 10 restored opcodes (OP_VER, OP_VERIF, OP_2MUL, OP_SUBSTR, etc.) x BIP143 + OTDA
test_live_standard_opcodes.py 93 Stack, arithmetic, bitwise, crypto, flow control opcodes
test_live_malleability.py 12 6 malleability restrictions x v1-rejects/v2-relaxes pairs

Testnet Broadcast Tests (requires funded key)

98 tests that broadcast real transactions to BSV testnet via ARC, verifying end-to-end correctness:

# Set funded testnet private key
export FUNDED_TESTNET_WIF="cYourTestnetWifHere"

# Run testnet broadcast tests
pytest tests/bsv/live/test_live_testnet.py -v

Mainnet broadcast tests (optional, real funds)

Same structure as testnet, but uses mainnet APIs and .utxo_pool_mainnet.json. Costs real BSV; use a dedicated key.

export FUNDED_MAINNET_WIF="YourMainnetWifHere"
pytest tests/bsv/live/test_live_mainnet.py -v -m mainnet

How it works:

  1. UTXO Fan-out: A single funded UTXO is split into ~130 small outputs via a fan-out transaction
  2. Two-step transactions: For non-P2PKH scripts (P2PK, Multisig, custom opcodes), a setup tx converts the P2PKH UTXO into the test script type, then a second tx spends it with the target sighash
  3. Spend pre-validation: Every test tx is validated through the Spend interpreter before broadcasting
  4. UTXO persistence: The UTXO pool is saved to .utxo_pool.json between test runs. Failed broadcasts automatically return UTXOs to the pool
  5. ARC headers: Uses X-SkipScriptValidation header to bypass ARC's script validator (which may lag behind the node's Chronicle support)
Test Class Tests Coverage
TestTestnetP2PKH 24 P2PKH x 12 sighash flags x 2 tx versions
TestTestnetP2PK 24 P2PK x 12 sighash flags x 2 tx versions
TestTestnetMultisig 24 2-of-3 BareMultisig x 12 sighash flags x 2 tx versions
TestTestnetChronicleOpcodes 20 10 Chronicle opcodes x BIP143 + OTDA paths
TestTestnetStandardOpcodes 7 ADD, SUB, MUL, CAT, HASH160, IF/ELSE, CHECKSIGVERIFY

Sighash flags tested (all 12):

Flag Value Algorithm
ALL_FORKID 0x41 BIP143
NONE_FORKID 0x42 BIP143
SINGLE_FORKID 0x43 BIP143
ALL_ANYONECANPAY_FORKID 0xC1 BIP143
NONE_ANYONECANPAY_FORKID 0xC2 BIP143
SINGLE_ANYONECANPAY_FORKID 0xC3 BIP143
ALL_FORKID_CHRONICLE 0x61 OTDA
NONE_FORKID_CHRONICLE 0x62 OTDA
SINGLE_FORKID_CHRONICLE 0x63 OTDA
ALL_ANYONECANPAY_FORKID_CHRONICLE 0xE1 OTDA
NONE_ANYONECANPAY_FORKID_CHRONICLE 0xE2 OTDA
SINGLE_ANYONECANPAY_FORKID_CHRONICLE 0xE3 OTDA

Beginner Tutorial

Step-by-Step BSV Tutorial: Sending BSV and NFTs

This beginner-friendly guide will walk you through sending BSV (Bitcoin SV) and creating NFTs using the BSV Python SDK. We'll take it step-by-step so you can learn at your own pace.

Contribution Guidelines

We're always looking for contributors to help us improve the project. 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: Install in development mode with test dependencies:

    pip install -e .[test]
    
  3. Make Changes: Create a new branch and make your changes.

  4. Test: Ensure all tests pass and check code coverage:

    # Run tests with coverage report
    pytest --cov=bsv --cov-report=html --cov-report=term
    
    # View detailed HTML coverage report
    open htmlcov/index.html  # or xdg-open htmlcov/index.html on Linux
    

    Help us improve coverage by adding tests for uncovered areas!

  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.

Support & Contacts

Project Owners: Thomas Giacomo and Darren Kellenschwiler Development Team Lead: sCrypt Maintainer: Ken Sato @ Yenpoint inc. & Yosuke Sato @ Yenpoint inc. 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 ecosystem. Let's build the future of BSV Blockchain together!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bsv_sdk-2.1.2.tar.gz (322.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bsv_sdk-2.1.2-py3-none-any.whl (384.7 kB view details)

Uploaded Python 3

File details

Details for the file bsv_sdk-2.1.2.tar.gz.

File metadata

  • Download URL: bsv_sdk-2.1.2.tar.gz
  • Upload date:
  • Size: 322.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bsv_sdk-2.1.2.tar.gz
Algorithm Hash digest
SHA256 afecf8ba9953e7dd5e01fa7cc0ee4af19ebdacddaa69aa9e3078be6f83bd701f
MD5 b5625ad3b0137de4aa85ae2ee8eed97a
BLAKE2b-256 368a9bc41223e43e131823258315b6a7c99d4fdee5d30509b333c1a2e0701fd3

See more details on using hashes here.

Provenance

The following attestation bundles were made for bsv_sdk-2.1.2.tar.gz:

Publisher: workflow.yml on bsv-blockchain/py-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bsv_sdk-2.1.2-py3-none-any.whl.

File metadata

  • Download URL: bsv_sdk-2.1.2-py3-none-any.whl
  • Upload date:
  • Size: 384.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bsv_sdk-2.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5e10fd38457dec8d4ddba1bbb853ff4173a1ce067f554191e1d2f2251d697528
MD5 6aa4bc94ede7efa7a42b693df94cf7b6
BLAKE2b-256 3977ff2ce0624a6666cf9d0e7178f435d21d560da2bee766c0dfa190c9038c60

See more details on using hashes here.

Provenance

The following attestation bundles were made for bsv_sdk-2.1.2-py3-none-any.whl:

Publisher: workflow.yml on bsv-blockchain/py-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page