bitcoinlib.db module

class bitcoinlib.db.Db(db_uri=None, password=None)[source]

Bases: object

Bitcoinlib Database object used by Service() and HDWallet() class. Initialize database and open session when creating database object.

Create new database if is doesn’t exist yet

drop_db(yes_i_am_sure=False)[source]
class bitcoinlib.db.DbConfig(**kwargs)[source]

Bases: sqlalchemy.ext.declarative.api.Base

BitcoinLib configuration variables

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

value
variable
class bitcoinlib.db.DbKey(**kwargs)[source]

Bases: sqlalchemy.ext.declarative.api.Base

Database definitions for keys in Sqlalchemy format

Part of a wallet, and used by transactions

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

account_id

ID of account if key is part of a HD structure

address

Address representation of key. An cryptocurrency address is a hash of the public key

address_index

Index of address in HD key structure address level

balance

Total balance of UTXO’s linked to this key

change

Change or normal address: Normal=0, Change=1

compressed

Is key compressed or not. Default is True

cosigner_id

ID of cosigner, used if key is part of HD Wallet

depth

Depth of key if it is part of a HD structure. Depth=0 means masterkey, depth=1 are the masterkeys children.

encoding

Encoding used to represent address: base58 or bech32

id

Unique Key ID

is_private

Is key private or not?

key_type

Type of key: single, bip32 or multisig. Default is bip32

latest_txid

TxId of latest transaction downloaded from the blockchain

multisig_children

List of children keys

multisig_parents

List of parent keys

name

Key name string

network

DbNetwork object for this key

network_name

Name of key network, i.e. bitcoin, litecoin, dash

parent_id

Parent Key ID. Used in HD wallets

path

String of BIP-32 key path

private

Bytes representation of private key

public

Bytes representation of public key

purpose

Purpose ID, default is 44

transaction_inputs

All DbTransactionInput objects this key is part of

transaction_outputs

All DbTransactionOutput objects this key is part of

used

Has key already been used on the blockchain in as input or output? Default is False

wallet

Related Wallet object

wallet_id

Wallet ID which contains this key

wif

Public or private WIF (Wallet Import Format) representation

class bitcoinlib.db.DbKeyMultisigChildren(**kwargs)[source]

Bases: sqlalchemy.ext.declarative.api.Base

Use many-to-many relationship for multisig keys. A multisig keys contains 2 or more child keys and a child key can be used in more then one multisig key.

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

child_id
key_order
parent_id
class bitcoinlib.db.DbNetwork(**kwargs)[source]

Bases: sqlalchemy.ext.declarative.api.Base

Database definitions for networks in Sqlalchemy format

Most network settings and variables can be found outside the database in the libraries configurations settings. Use the bitcoinlib/data/networks.json file to view and manage settings.

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

description
name

Network name, i.e.: bitcoin, litecoin, dash

class bitcoinlib.db.DbTransaction(**kwargs)[source]

Bases: sqlalchemy.ext.declarative.api.Base

Database definitions for transactions in Sqlalchemy format

Refers to 1 or more keys which can be part of a wallet

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

account_id

ID of account

block_height

Number of block this transaction is included in

coinbase

Is True when this is a coinbase transaction, default is False

confirmations

Number of confirmation when this transaction is included in a block. Default is 0: unconfirmed

date

Date when transaction was confirmed and included in a block. Or when it was created when transaction is not send or confirmed

fee

Transaction fee

id

Unique transaction index for internal usage

input_total

Total value of the inputs of this transaction. Input total = Output total + fee. Default is 0

inputs

List of all inputs as DbTransactionInput objects

is_complete

Allow to store incomplete transactions, for instance if not all inputs are known when retrieving UTXO’s

locktime

Transaction level locktime. Locks the transaction until a specified block (value from 1 to 5 million) or until a certain time (Timestamp in seconds after 1-jan-1970). Default value is 0 for transactions without locktime

network

Link to DbNetwork object

network_name

Blockchain network name of this transaction

output_total

Total value of the outputs of this transaction. Output total = Input total - fee

outputs

List of all outputs as DbTransactionOutput objects

raw

Raw transaction hexadecimal string. Transaction is included in raw format on the blockchain

size

Size of the raw transaction in bytes

status

Current status of transaction, can be one of the following: new’, ‘unconfirmed’, ‘confirmed’. Default is ‘new’

txid

Bytes representation of transaction ID

verified

Is transaction verified. Default is False

version

Tranaction version. Default is 1 but some wallets use another version number

wallet

Link to Wallet object which contains this transaction

wallet_id

ID of wallet which contains this transaction

witness_type

Is this a legacy or segwit transaction?

class bitcoinlib.db.DbTransactionInput(**kwargs)[source]

Bases: sqlalchemy.ext.declarative.api.Base

Transaction Input Table

Relates to Transaction table and Key table

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

address

Address string of input, used if no key is associated. An cryptocurrency address is a hash of the public key or a redeemscript

double_spend

Indicates if a service provider tagged this transaction as double spend

index_n

Index number of transaction input

key

Related DbKey object

key_id

ID of key used in this input

output_n

Output_n of previous transaction output that is spent in this input

prev_txid

Transaction hash of previous transaction. Previous unspent outputs (UTXO) is spent in this input

script

Unlocking script to unlock previous locked output

script_type

Unlocking script type. Can be ‘coinbase’, ‘sig_pubkey’, ‘p2sh_multisig’, ‘signature’, ‘unknown’, ‘p2sh_p2wpkh’ or ‘p2sh_p2wsh’. Default is sig_pubkey

sequence

Transaction sequence number. Used for timelock transaction inputs

transaction

Related DbTransaction object

transaction_id

Input is part of transaction with this ID

value

Value of transaction input

witness_type

Type of transaction, can be legacy, segwit or p2sh-segwit. Default is legacy

class bitcoinlib.db.DbTransactionOutput(**kwargs)[source]

Bases: sqlalchemy.ext.declarative.api.Base

Transaction Output Table

Relates to Transaction and Key table

When spent is False output is considered an UTXO

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

address

Address string of output, used if no key is associated. An cryptocurrency address is a hash of the public key or a redeemscript

key

List of DbKey object used in this output

key_id

ID of key used in this transaction output

output_n

Sequence number of transaction output

script

Locking script which locks transaction output

script_type

Locking script type. Can be one of these values: ‘p2pkh’, ‘multisig’, ‘p2sh’, ‘p2pk’, ‘nulldata’, ‘unknown’, ‘p2wpkh’ or ‘p2wsh’. Default is p2pkh

spending_index_n

Index number of transaction input which spends this output

spending_txid

Transaction hash of input which spends this output

spent

Indicated if output is already spent in another transaction

transaction

Link to transaction object

transaction_id

Transaction ID of parent transaction

value

Total transaction output value

class bitcoinlib.db.DbWallet(**kwargs)[source]

Bases: sqlalchemy.ext.declarative.api.Base

Database definitions for wallets in Sqlalchemy format

Contains one or more keys.

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

children

Wallet IDs of children wallets, used in multisig wallets

cosigner_id

ID of cosigner of this wallet. Used in multisig wallets to differentiate between different wallets

default_account_id

ID of default account for this wallet if multiple accounts are used

encoding

Default encoding to use for address generation, i.e. base58 or bech32. Default is base58.

id

Unique wallet ID

key_path

Key path structure used in this wallet. Key path for multisig wallet, use to create your own non-standard key path. Key path must follow the following rules: * Path start with masterkey (m) and end with change / address_index * If accounts are used, the account level must be 3. I.e.: m/purpose/coin_type/account/ * All keys must be hardened, except for change, address_index or cosigner_id Max length of path is 8 levels

keys

Link to keys (DbKeys objects) in this wallet

main_key_id

Masterkey ID for this wallet. All other keys are derived from the masterkey in a HD wallet bip32 wallet

multisig

Indicates if wallet is a multisig wallet. Default is True

multisig_n_required

Number of required signature for multisig, only used for multisignature master key

name

Unique wallet name

network

Link to DbNetwork object

network_name

Name of network, i.e.: bitcoin, litecoin

owner

Wallet owner

parent_id

Wallet ID of parent wallet, used in multisig wallets

purpose

Wallet purpose ID. BIP-44 purpose field, indicating which key-scheme is used default is 44

scheme

Key structure type, can be BIP-32 or single

sort_keys

Sort keys in multisig wallet

transactions

Link to transaction (DbTransactions) in this wallet

witness_type

Wallet witness type. Can be ‘legacy’, ‘segwit’ or ‘p2sh-segwit’. Default is legacy.

bitcoinlib.db.add_column(engine, table_name, column)[source]

Used to add new column to database with migration and update scripts

Parameters
  • engine

  • table_name

  • column

Returns

bitcoinlib.db.compile_largebinary_mysql(type_, compiler, **kwargs)[source]
bitcoinlib.db.db_update(db, version_db, code_version='0.6.1')[source]
bitcoinlib.db.db_update_version_id(db, version)[source]