bitcoinlib.networks module

class bitcoinlib.networks.Network(network_name='bitcoin')[source]

Bases: object

Network class with all network definitions.

Prefixes for WIF, P2SH keys, HD public and private keys, addresses. A currency symbol and type, the denominator (such as satoshi) and a BIP0044 cointype.


Return the value as string with currency symbol

Print value for 100000 satoshi as string in human readable format

>>> Network('bitcoin').print_value(100000)
'0.00100000 BTC'
Parameters:value (int, float) – Value in smallest denominitor such as Satoshi
Return str:
wif_prefix(is_private=False, witness_type='legacy', multisig=False)[source]

Get WIF prefix for this network and specifications in arguments

>>> Network('bitcoin').wif_prefix()  # xpub
>>> Network('bitcoin').wif_prefix(is_private=True, witness_type='segwit', multisig=True)  # Zprv
  • is_private (bool) – Private or public key, default is True
  • witness_type (str) – Legacy, segwit or p2sh-segwit
  • multisig (bool) – Multisignature or single signature wallet. Default is False: no multisig
Return bytes:
exception bitcoinlib.networks.NetworkError(msg='')[source]

Bases: Exception

Network Exception class

bitcoinlib.networks.network_by_value(field, value)[source]

Return all networks for field and (prefix) value.

Example, get available networks for WIF or address prefix

>>> network_by_value('prefix_wif', 'B0')
['litecoin', 'litecoin_legacy']
>>> network_by_value('prefix_address', '6f')
['testnet', 'litecoin_testnet']

This method does not work for HD prefixes, use ‘wif_prefix_search’ instead

>>> network_by_value('prefix_address', '043587CF')
  • field (str) – Prefix name from networks definitions (networks.json)
  • value (str, bytes) – Value of network prefix
Return list:

Of network name strings


Is network defined?

Networks of this library are defined in networks.json in the operating systems user path.

>>> network_defined('bitcoin')
>>> network_defined('ethereum')
Parameters:network (str) – Network name
Return bool:

Return all prefixes for field, i.e.: prefix_wif, prefix_address_p2sh, etc

>>> network_values_for('prefix_wif')
[b'\x99', b'\x80', b'\xef', b'\xb0', b'\xb0', b'\xef', b'\xcc', b'\xef', b'\x9e', b'\xf1']
>>> network_values_for('prefix_address_p2sh')
[b'\x95', b'\x05', b'\xc4', b'2', b'\x05', b':', b'\x10', b'\x13', b'\x16', b'\xc4']
Parameters:field (str) – Prefix name from networks definitions (networks.json)
Return str:

Extract network, script type and public/private information from HDKey WIF or WIF prefix.

Example, get bitcoin ‘xprv’ info:

>>> wif_prefix_search('0488ADE4', network='bitcoin', multisig=False)
[{'prefix': '0488ADE4', 'is_private': True, 'prefix_str': 'xprv', 'network': 'bitcoin', 'witness_type': 'legacy', 'multisig': False, 'script_type': 'p2pkh'}]

Or retreive info with full WIF string:

>>> wif_prefix_search('xprv9wTYmMFdV23N21MM6dLNavSQV7Sj7meSPXx6AV5eTdqqGLjycVjb115Ec5LgRAXscPZgy5G4jQ9csyyZLN3PZLxoM1h3BoPuEJzsgeypdKj', network='bitcoin', multisig=False)
[{'prefix': '0488ADE4', 'is_private': True, 'prefix_str': 'xprv', 'network': 'bitcoin', 'witness_type': 'legacy', 'multisig': False, 'script_type': 'p2pkh'}]

Can return multiple items if no network is specified:

>>> [nw['network'] for nw in wif_prefix_search('0488ADE4', multisig=True)]
['bitcoin', 'dash', 'dogecoin']
  • wif (str, bytes) – WIF string or prefix in bytes or hexadecimal string
  • witness_type (str) – Limit search to specific witness type
  • multisig (bool) – Limit search to multisig: false, true or None for both. Default is both
  • network (str) – Limit search to specified network
Return dict: