Skip to main content

Bitcoin SV (BSV) Python Library

Project description

bsvlib

build codecov PyPI version Python versions MIT license

A Bitcoin SV (BSV) Python Library that is extremely simple to use but more.

  • MAINNET and TESTNET supported
  • P2PKH and P2PK supported
  • All the SIGHASH flags supported
  • Additional script types can be customized
  • MetaSV, SensibleQuery, and WhatsOnChain API integrated
  • Ability to adapt to different service providers
  • Fully ECDSA implementation
  • ECDH and Electrum ECIES (aka BIE1) implementation
  • HD implementation (BIP-32, BIP-39, BIP-44)

👉 Join our Telegram group for discussions or support.

Installation

$ pip install bsvlib

Examples

  1. Send BSV in one line
from bsvlib import Wallet

# Donate to aaron67!
print(Wallet(['YOUR_WIF_GOES_HERE']).send_transaction(outputs=[('1HYeFCE2KG4CW4Jwz5NmDqAZK9Q626ChmN', 724996)]))
  1. Send unspent locked by different keys in one transaction, support OP_RETURN output as well
from bsvlib import Wallet
from bsvlib.constants import Chain

w = Wallet(chain=Chain.TEST)

w.add_key('cVwfreZB3i8iv9JpdSStd9PWhZZGGJCFLS4rEKWfbkahibwhticA')
w.add_key('93UnxexmsTYCmDJdctz4zacuwxQd5prDmH6rfpEyKkQViAVA3me')
print(w.get_balance(refresh=True))

outputs = [('mqBuyzdHfD87VfgxaYeM9pex3sJn4ihYHY', 724), ('mr1FHq6GwWzmD1y8Jxq6rNDGsiiQ9caF7r', 996)]
pushdatas = ['hello', b'world']
print(w.send_transaction(outputs=outputs, pushdatas=pushdatas, combine=True))
  1. Operate P2PK
from bsvlib import Wallet, TxOutput, Transaction
from bsvlib.keys import Key
from bsvlib.script import P2pkScriptType
from bsvlib.service import SensibleQuery

private_key = Key('L5agPjZKceSTkhqZF2dmFptT5LFrbr6ZGPvP7u4A6dvhTrr71WZ9')

w = Wallet(provider=SensibleQuery())
w.add_key(private_key)
w.add_key('5KiANv9EHEU4o9oLzZ6A7z4xJJ3uvfK2RLEubBtTz1fSwAbpJ2U')

t = Transaction()
t.add_inputs(w.get_unspents(refresh=True))
t.add_output(TxOutput(P2pkScriptType.locking(private_key.public_key().serialize()), 996, P2pkScriptType()))
t.add_change(private_key.address())

print(t.sign().broadcast())
  1. Sign with different SIGHASH flags, more examples
from bsvlib import Wallet, Transaction, TxInput, TxOutput
from bsvlib.constants import SIGHASH
from bsvlib.keys import Key
from bsvlib.service import WhatsOnChain

provider = WhatsOnChain()
private_key = Key('L5agPjZKceSTkhqZF2dmFptT5LFrbr6ZGPvP7u4A6dvhTrr71WZ9')
unspents = Wallet([private_key]).get_unspents(refresh=True, provider=provider)

t = Transaction(provider=provider)
t.add_input(TxInput(unspents[0], sighash=SIGHASH.SINGLE_FORKID))
t.add_output(TxOutput(private_key.address(), 135))
t.sign()

# it's good to append any outputs AFTER the first output, no need to sign, can broadcast directly
print(t.add_change().broadcast())
  1. Sign arbitrary text with private key
from bsvlib import Key, verify_signed_text

private_key = Key('L5agPjZKceSTkhqZF2dmFptT5LFrbr6ZGPvP7u4A6dvhTrr71WZ9')
text = 'hello world'

# sign arbitrary text with bitcoin private key
address, signature = private_key.sign_text(text)

# verify https://reinproject.org/bitcoin-signature-tool/
print(address, signature)

# verify
print(verify_signed_text(text, address, signature))
  1. Encrypt message with public key, decrypt with the corresponding private key
from bsvlib import Key

private_key = Key('L5agPjZKceSTkhqZF2dmFptT5LFrbr6ZGPvP7u4A6dvhTrr71WZ9')
public_key = private_key.public_key()

plain = 'hello world'

# use public key to encrypt
encrypted = public_key.encrypt_text(plain)
print(encrypted)

# decrypt with the corresponding private key
print(private_key.decrypt_text(encrypted))
  1. Process HD wallet derivation

image

from typing import List

from bsvlib.hd import mnemonic_from_entropy, Xprv, derive_from_mnemonic

#
# HD derivation
#
entropy = 'cd9b819d9c62f0027116c1849e7d497f'

# snow swing guess decide congress abuse session subway loyal view false zebra
mnemonic: str = mnemonic_from_entropy(entropy)
print(mnemonic)

keys: List[Xprv] = derive_from_mnemonic(mnemonic, path="m/44'/0'/0'", change=1, index_start=0, index_end=5)
for key in keys:
    print(key.address(), key.private_key().wif())

#
# random mnemonic
#
print()
print(mnemonic_from_entropy())
print(mnemonic_from_entropy(lang='en'))
print(mnemonic_from_entropy(lang='zh-cn'))

Credits

Donation

If you like my work or have found this library useful, feel free to donate me a cup of coffee.

Every little satoshi helps. 👏

1HYeFCE2KG4CW4Jwz5NmDqAZK9Q626ChmN

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

bsvlib-0.6.2.tar.gz (27.6 kB view details)

Uploaded Source

Built Distribution

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

bsvlib-0.6.2-py3-none-any.whl (32.9 kB view details)

Uploaded Python 3

File details

Details for the file bsvlib-0.6.2.tar.gz.

File metadata

  • Download URL: bsvlib-0.6.2.tar.gz
  • Upload date:
  • Size: 27.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for bsvlib-0.6.2.tar.gz
Algorithm Hash digest
SHA256 07e1979b2e4296afb0da107ab9e5a06e267287f13596005e60fb56186370d68a
MD5 fe04f64546969809587e5a8968dfff00
BLAKE2b-256 bdaeeabe22c4e98db57507fc2c99972687b6f6597264b29b981f564503f826b7

See more details on using hashes here.

File details

Details for the file bsvlib-0.6.2-py3-none-any.whl.

File metadata

  • Download URL: bsvlib-0.6.2-py3-none-any.whl
  • Upload date:
  • Size: 32.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1

File hashes

Hashes for bsvlib-0.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 10f8c56230e1813c02d9cfe7eabc8a960799a9ada9d4c493b9b41152855b5f0e
MD5 b5d78f589e551bc760149f921f83eff2
BLAKE2b-256 d27f85179e67826cebbc026155e1963d8bfada04c8eed77aeaa90b7e001e2aec

See more details on using hashes here.

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