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.

print_value(value)[source]

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()
b'ˆ²
‘ # xpub
>>> Network('bitcoin').wif_prefix(is_private=True, witness_type='segwit', multisig=True)
b'ªz™'     # Zprv
param is_private:
 Private or public key, default is True
type is_private:
 bool
param witness_type:
 Legacy, segwit or p2sh-segwit
type witness_type:
 str
param multisig:Multisignature or single signature wallet. Default is not multisig
type multisig:True
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 adress 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’) []

Parameters:
  • field (str) – Prefix name from networks definitions (networks.json)
  • value (str, bytes) – Value of network prefix
Return list:

Of network name strings

bitcoinlib.networks.network_defined(network)[source]

Is network defined?

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

>>> network_defined('bitcoin')
True
>>> network_defined('ethereum')
False
Parameters:network (str) – Network name
Return bool:
bitcoinlib.networks.network_values_for(field, output_as='default')[source]

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

>>> network_values_for('prefix_wif')
[b'™', b'€', b'ï', b'°', b'°', b'ï', b'Ì', b'ï']
>>> network_values_for('prefix_address_p2sh')
[b'•', b'', b'Ä', b'2', b'', b':', b'', b'']
Parameters:
  • field (str) – Prefix name from networks definitions (networks.json)
  • output_as (str) – Output as string or hexstring. Default is string or hexstring depending on field type.
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’]

Parameters:
  • 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: