Skip to main content

xchainpy/xchainpy_bitcoin

Bitcoin Module for XChainPy Clients

Modules

  • client - Custom client for communicating with bitcoinlib
  • models - model wrapper for bitcoin required types
  • util - Utitilies for using bitcoinlib and bitcoin chain

Following dependencies have to be installed into your project

bip_utils, bitcoinlib , http3 , xchainpy_client , xchainpy_crypto , xchainpy_util

Service Providers

This package uses the following service providers:

Function Service Notes
Balances Sochain https://sochain.com/api#get-balance
Transaction history Sochain https://sochain.com/api#get-display-data-address, https://sochain.com/api#get-tx
Transaction details by hash Sochain https://sochain.com/api#get-tx
Transaction fees Bitgo https://app.bitgo.com/docs/#operation/v2.tx.getfeeestimate
Transaction broadcast Sochain https://sochain.com/api#send-transaction
Explorer Blockstream https://blockstream.info

Sochain API rate limits: https://sochain.com/api#rate-limits (300 requests/minute)

Installation

pip install xchainpy_bitcoin

Before install the package on M1 Mac, execute these commands:

brew install gmp openblas openssl autoconf automake libffi libtool pkg-config

CFLAGS=-I/opt/homebrew/opt/gmp/include LDFLAGS=-L/opt/homebrew/opt/gmp/lib pip3 install fastecdsa

CFLAGS=-I$(brew --prefix openssl)/include LDFLAGS=-L$(brew --prefix openssl)/lib pip3 install scrypt

export OPENBLAS="$(brew --prefix openblas) $OPENBLAS"

Bitcoin Client module

Initialize a client

from xchainpy_client.models.types import Network
from xchainpy_bitcoin.client import Client
from xchainpy_bitcoin.models.client_types import BitcoinClientParams

# Note: This phrase is created by https://iancoleman.io/bip39/ and will never been used in a real-world
phrase = 'atom green various power must another rent imitate gadget creek fat then'
client = Client(BitcoinClientParams(phrase=phrase, network=Network.Testnet))

# if you want to change phrase after initialize the client
client.set_phrase('caution pear excite vicious exotic slow elite marble attend science strategy rude')

# if you want to change network after initialize the client
client.purge_client()
client.set_network(Network.Mainnet)

# when you are done with the client, call this
client.purge_client()

Address methods

from xchainpy_client.models.types import Network
from xchainpy_bitcoin.client import Client
from xchainpy_bitcoin.models.client_types import BitcoinClientParams

phrase = 'atom green various power must another rent imitate gadget creek fat then'
client = Client(BitcoinClientParams(phrase=phrase, network=Network.Testnet))

address = client.get_address()
is_valid = client.validate_address(client.network, address) # bool
print(address)
print(is_valid)

# change index
address = client.get_address(1)
is_valid = client.validate_address(client.network, address) # bool
print(address)
print(is_valid)

Fees

from xchainpy_bitcoin.client import Client
from xchainpy_bitcoin.models.client_types import BitcoinClientParams

client = Client(BitcoinClientParams())

# Get feeRate estimations
fee_rates = await client.get_fee_rates()

# Get fee estimations
fees = await client.get_fees()

# Get fee estimations with memo
memo = 'SWAP:THOR.RUNE'
fees_with_memo = await client.get_fees(memo)


print(f'''fee rates: 
average: {fee_rates.average}
fast: {fee_rates.fast}
fastest: {fee_rates.fastest}\n''')

print(f'''fees: 
average: {fees.average}
fast: {fees.fast}
fastest: {fees.fastest}\n''')

print(f'''fees with memo: 
average: {fees_with_memo.average}
fast: {fees_with_memo.fast}
fastest: {fees_with_memo.fastest}\n''')

Balance

from xchainpy_client.models.types import Network
from xchainpy_bitcoin.client import Client
from xchainpy_bitcoin.models.client_types import BitcoinClientParams

phrase = 'atom green various power must another rent imitate gadget creek fat then'
client = Client(BitcoinClientParams(phrase=phrase, network=Network.Testnet))

address = client.get_address()

balance = await client.get_balance(address=address)
balance = balance[0]

print(f'asset: {balance.asset}, amount: {balance.amount}')

Transactions and Transaction_data

from xchainpy_client.models.tx_types import TxHistoryParams
from xchainpy_client.models.types import Network
from xchainpy_bitcoin.client import Client
from xchainpy_bitcoin.models.client_types import BitcoinClientParams

phrase = 'atom green various power must another rent imitate gadget creek fat then'
client = Client(BitcoinClientParams(phrase=phrase, network=Network.Testnet))

address = client.get_address()

params = TxHistoryParams(address=address, limit=1)
transactions = await client.get_transactions(params)
# type of transactions is xchainpy_client.models.tx_types.TxPage

t = transactions.txs[0]
print(t.asset)
print(t.tx_from[0].amount)
print(t.tx_from[0].address)
print(t.tx_to[0].amount)
print(t.tx_to[0].address)
print(t.tx_date)
print(t.tx_type)
print(t.tx_hash)

transaction = await client.get_transaction_data(t.tx_hash)
# transaction object is equal by t object

Transfer

from xchainpy_client.models.types import Network
from xchainpy_bitcoin.client import Client
from xchainpy_bitcoin.models.client_types import BitcoinClientParams, BitcoinTxParams

phrase = 'atom green various power must another rent imitate gadget creek fat then'
client = Client(BitcoinClientParams(phrase=phrase, network=Network.Testnet))

address = client.get_address()

params = BitcoinTxParams(amount=0.0000001, recipient=address, memo='memo')
tx_hash = await client.transfer(params)

print(tx_hash)

Explorer url

from xchainpy_client.models.types import Network
from xchainpy_bitcoin.client import Client
from xchainpy_bitcoin.models.client_types import BitcoinClientParams

client = Client(BitcoinClientParams())

print(client.get_explorer_url())
print(client.get_explorer_address_url('testAddressHere'))
print(client.get_explorer_tx_url('testTxHere'))

client.purge_client()
client.set_network(Network.Mainnet)

print(client.get_explorer_url())
print(client.get_explorer_address_url('testAddressHere'))
print(client.get_explorer_tx_url('testTxHere'))

Tests

These packages needed to run tests:

  • pytest pip install pytest
  • pytest-asyncio pip install pytest-asyncio

How to run test ?

$ python -m pytest xchainpy/xchainpy_bitcoin/tests

Metadata

Release files for xchainpy-bitcoin 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xchainpy-bitcoin 0.2.3
File Size Uploaded Provenance Actions
xchainpy_bitcoin-0.2.3.tar.gz 16.3 kB Without provenance Details for xchainpy_bitcoin-0.2.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xchainpy-bitcoin 0.2.3
File Interpreter ABI Platform Actions
xchainpy_bitcoin-0.2.3-py3-none-any.whl Python 3 none any Details for xchainpy_bitcoin-0.2.3-py3-none-any.whl

Total release size: 34.9 kB

Release files / xchainpy_bitcoin-0.2.3.tar.gz

Download URL xchainpy_bitcoin-0.2.3.tar.gz
Size 16.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a0ce844e9ef4edd08ceb503f67efff0946098c1be372361809dcebca601b267c
BLAKE2b-256 checksum
How to use checksums
7b1134d0ca532b00caec561bde6de4fb8d3d4340fa2ec0b8c6f0e62a3860b379
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.2

Release files / xchainpy_bitcoin-0.2.3-py3-none-any.whl

Download URL xchainpy_bitcoin-0.2.3-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e65807df3df4dc7d0a10aa28f62f54787baae639bda0a3bdb6d68783c0e30ab
BLAKE2b-256 checksum
How to use checksums
f59b48f78489bcc807276fca66f0f07b456a308086a88fd3063c3350c9a28350
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.2

Security for xchainpy-bitcoin 0.2.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page