Welcome to Bitcoinlib’s documentation!

Bitcoin and other Cryptocurrencies Library for Python. Includes a fully functional wallet, Mnemonic key generation and management and connection with various service providers to receive and send blockchain and transaction information.


The bitcoinlibrary contains a wallet implementation using sqlalchemy and sqllite3 to import, create and manage keys in a Hierarchical Deterministic Way.

Example: Create wallet and generate new key to receive bitcoins

>>> from bitcoinlib.wallets import HDWallet
>>> w = HDWallet.create('Wallet1')
>>> w
<HDWallet (id=1, name=Wallet1, network=bitcoin)>
>>> key1 = w.new_key()
>>> key1
<HDWalletKey (name=Key 0, wif=xprvA4B..etc..6HZKGW7Kozc, path=m/44'/0'/0'/0/0)>
>>> key1.address

When your wallet received a payment and has unspent transaction outputs, you can send bitcoins easily. If succesfull a transaction ID is returned

>>> w.send_to('12ooWd8Xag7hsgP9PBPnmyGe36VeUrpMSH', 100000)

Segregated Witness Wallet

Easily create and manage segwit wallets. Both native segwit with base32/bech32 addresses and P2SH nested segwit wallets with traditional addresses are available.

Create a native single key P2WPKH wallet:

>>> from bitcoinlib.wallets import HDWallet
>>> w = HDWallet.create('segwit_p2wpkh', witness_type='segwit')
>>> w.get_key().address

Or create a P2SH nested single key P2SH_P2WPKH wallet:

>>> from bitcoinlib.wallets import HDWallet
>>> w = HDWallet.create('segwit_p2sh_p2wpkh', witness_type='p2sh-segwit')
>>> w.get_key().address

Wallet from passphrase with accounts and multiple currencies

The following code creates a wallet with two bitcoin and one litecoin account from a Mnemonic passphrase. The complete wallet can be recovered from the passphrase which is the masterkey.

from bitcoinlib.wallets import HDWallet, wallet_delete
from bitcoinlib.mnemonic import Mnemonic

passphrase = Mnemonic().generate()
w = HDWallet.create("Wallet2", key=passphrase, network='bitcoin')
account_btc2 = w.new_account('Account BTC 2')
account_ltc1 = w.new_account('Account LTC', network='litecoin')

Multi Signature Wallets

Create a Multisig wallet with 2 cosigner which both need to sign a transaction.

from bitcoinlib.wallets import HDWallet
from bitcoinlib.keys import HDKey

NETWORK = 'testnet'
pk1 = HDKey('tprv8ZgxMBicQKsPd1Q44tfDiZC98iYouKRC2CzjT3HGt1yYw2zuX2awTotzGAZQEAU9bi2M5MCj8iedP9MREPjUgpDEBwBgGi2C8eK'
            '5zNYeiX8', network=NETWORK)
pk2 = HDKey('tprv8ZgxMBicQKsPeUbMS6kswJc11zgVEXUnUZuGo3bF6bBrAg1ieFfUdPc9UHqbD5HcXizThrcKike1c4z6xHrz6MWGwy8L6YKVbgJ'
            'MeQHdWDp', network=NETWORK)
w1 = HDWallet.create_multisig('multisig_2of2_cosigner1', sigs_required=2,
                               key_list=[pk1, pk2.account_multisig_key().wif_public()], network=NETWORK)
w2 = HDWallet.create_multisig('multisig_2of2_cosigner2',  sigs_required=2,
                               key_list=[pk1.account_multisig_key().wif_public(), pk2], network=NETWORK)
print("Deposit testnet bitcoin to this address to create transaction: ", w1.get_key().address)

Create a transaction in the first wallet

t = w1.sweep('mwCwTceJvYV27KXBc3NJZys6CjsgsoeHmf', min_confirms=0)

And then import the transaction in the second wallet, sign it and push it to the network

t2 = w2.transaction_import(t)

Command Line Tool

With the command line tool you can create and manage wallet without any Python programming.

To create a new Bitcoin wallet

$ cli-wallet NewWallet
Command Line Wallet for BitcoinLib

Wallet newwallet does not exist, create new wallet [yN]? y

CREATE wallet 'newwallet' (bitcoin network)

Your mnemonic private key sentence is: force humble chair kiss season ready elbow cool awake divorce famous tunnel

Please write down on paper and backup. With this key you can restore your wallet and all keys

You can use ‘cli-wallet’ to create simple or multisig wallets for various networks, manage public and private keys and managing transactions.

For the full command line wallet documentation please read


Service providers

Communicates with pools of bitcoin service providers to retreive transaction, address, blockchain information. To push a transaction to the network. To determine optimal service fee for a transaction. Or to update your wallet’s balance.

Example: Get estimated transactionfee in sathosis per Kb for confirmation within 5 blocks

>>> from bitcoinlib.services.services import Service
>>> Service().estimatefee(5)


This library is still in development, please use at your own risk and test sufficiently before using it in a production environment.

Schematic overview

_images/classes-overview.jpg _images/classes-overview-detailed.jpg

Indices and tables