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, rep='string', denominator=1, decimals=None)[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 the smallest denominator such as Satoshi
rep (str) – Currency representation: ‘string’, ‘symbol’, ‘none’ or your own custom name
denominator (float) – Unit to use in representation. Default is 1. I.e. 1 = 1 BTC, 0.001 = milli BTC / mBTC
decimals (int) – Number of digits after the decimal point, leave empty for automatic determination based on value. Use integer value between 0 and 8
- 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 b'\x04\x88\xb2\x1e' >>> Network('bitcoin').wif_prefix(is_private=True, witness_type='segwit', multisig=True) # Zprv b'\x02\xaaz\x99'
- Parameters
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') []
- Parameters
field (str) – Prefix name from networks definitions (networks.json)
value (str) – 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)[source]
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'\xcc', b'\x9e', b'\xf1'] >>> network_values_for('prefix_address_p2sh') [b'\x95', b'\x05', b'\xc4', b'2', b':', b'\x10', b'\x13', b'\x16']
- Parameters
field (str) – Prefix name from networks definitions (networks.json)
- Return str
- bitcoinlib.networks.print_value(value, network='bitcoin', rep='string', denominator=1, decimals=None)[source]
Return the value as string with currency symbol
Wrapper for the Network().print_value method.
- Parameters
value (int, float) – Value in the smallest denominator such as Satoshi
network (str) – Network name as string, default is ‘bitcoin’
rep (str) – Currency representation: ‘string’, ‘symbol’, ‘none’ or your own custom name
denominator (float) – Unit to use in representation. Default is 1. I.e. 1 = 1 BTC, 0.001 = milli BTC / mBTC, 1e-8 = Satoshi’s
decimals (int) – Number of digits after the decimal point, leave empty for automatic determination based on value. Use integer value between 0 and 8
- Return str
- bitcoinlib.networks.wif_prefix_search(wif, witness_type=None, multisig=None, network=None)[source]
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', 'regtest', 'dash', 'dogecoin']
- Parameters
wif (str) – WIF string or prefix as 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