bitcoinlib.encoding module

exception bitcoinlib.encoding.EncodingError(msg='')[source]

Bases: Exception

Log and raise encoding errors

bitcoinlib.encoding.addr_bech32_to_pubkeyhash(bech, hrp=None, as_hex=False, include_witver=False)[source]

Decode bech32 / segwit address to public key hash

Validate the Bech32 string, and determine HRP and data

Parameters:
  • bech (str) – Bech32 address to convert
  • hrp (str) – Address prefix called Human-readable part. Default is None and tries to derive prefix, for bitcoin specify ‘bc’ and for bitcoin testnet ‘tb’
  • as_hex (bool) – Output public key hash as hex or bytes. Default is False
  • include_witver (bool) – Include witness version in output? Default is False
Return str:

Public Key Hash

bitcoinlib.encoding.addr_convert(addr, prefix)[source]

Convert base-58 encoded address to address with another prefix

Parameters:
  • addr (str) – Base58 address
  • prefix (str, bytes) – New address prefix
Return str:

New converted address

bitcoinlib.encoding.addr_to_pubkeyhash(address, as_hex=False)[source]

Convert address to public key hash

Parameters:
  • address (str) – Crypto currency address in base-58 format
  • as_hex (bool) – Output as hexstring
Return bytes, str:
 

public key hash

bitcoinlib.encoding.change_base(chars, base_from, base_to, min_length=0, output_even=None, output_as_list=None)[source]

Convert input chars from one base to another.

From and to base can be any base. If base is not found a array of index numbers will be returned

Examples: > change_base(‘FF’, 16, 10) will return 256 > change_base(100, 16, 2048) will return [100]

Parameters:
  • chars (any) – Input string
  • base_from (int, str) – Base number or name from input
  • base_to (int, str) – Base number or name for output
  • min_length (int) – Minimal output length. Required for decimal, advised for all output to avoid leading zeros conversion problems.
  • output_even (bool) – Specify if output must contain a even number of characters. Sometimes handy for hex conversions.
  • output_as_list (bool) – Always output as list instead of string.
Return str, list:
 

Base converted input as string or list.

bitcoinlib.encoding.convert_der_sig(s, as_hex=True)[source]

Convert DER encoded signature to signature

Parameters:
  • s (bytes) – DER signature
  • as_hex (bool) – Output as hexstring
Return bytes, str:
 

Signature

bitcoinlib.encoding.convertbits(data, frombits, tobits, pad=True)[source]

‘General power-of-2 base conversion’

Source: https://github.com/sipa/bech32/tree/master/ref/python

Parameters:
  • data (list) – Data values to convert
  • frombits (int) – Number of bits in source data
  • tobits (int) – Number of bits in result data
  • pad (bool) – Use padding zero’s or not. Default is True
Return list:

Converted values

bitcoinlib.encoding.int_to_varbyteint(inp)[source]

Convert integer to CompactSize Variable length integer in byte format.

See https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer for specification

Parameters:inp (int) – Integer to convert
Returns:byteint: 1-9 byte representation as integer
bitcoinlib.encoding.normalize_string(txt)[source]

Normalize a string to the default NFKD unicode format See https://en.wikipedia.org/wiki/Unicode_equivalence#Normalization

Parameters:txt (bytes, bytearray, str) – string value
Returns:string
bitcoinlib.encoding.normalize_var(var, base=256)[source]

For Python 2 convert variabele to string For Python 3 convert to bytes Convert decimals to integer type

Parameters:
  • var (str, byte, bytearray, unicode) – input variable in any format
  • base (int) – specify variable format, i.e. 10 for decimal, 16 for hex
Returns:

Normalized var in string for Python 2, bytes for Python 3, decimal for base10

bitcoinlib.encoding.pubkeyhash_to_addr(pkh, versionbyte=b'\x00')[source]

Convert public key hash to address

Parameters:
  • pkh (bytes, str) – Public key hash
  • versionbyte (bytes) – Prefix version byte of network, default is bitcoin ‘’
Return str:

Base-58 encoded address

bitcoinlib.encoding.pubkeyhash_to_addr_bech32(pubkeyhash, hrp='bc', witver=0, separator='1')[source]

Encode public key hash as bech32 segwit address

Format of address is prefix/hrp + seperator + bech32 address + checksum

For more information see BIP173 proposal at https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki

Parameters:
  • pubkeyhash (str, bytes) – Public key hash
  • hrp (str) – Address prefix called Human-readable part. Default is ‘bc’ an abbreviation of Bitcoin. Use ‘tb’ for testnet.
  • witver (int) – Witness version between 0 and 16
  • separator (str) – Separator char between hrp and data, should always be left to ‘1’ otherwise its not standard.
Return str:

Bech32 encoded address

bitcoinlib.encoding.script_to_pubkeyhash(script)[source]

Creates a RIPEMD-160 hash of a locking, unlocking, redeemscript, etc

Parameters:script (bytes) – Script
Return bytes:RIPEMD-160 hash of script
bitcoinlib.encoding.to_bytearray(s)[source]

Convert String, Unicode or Bytes to Python 2 and 3 compatible ByteArray :param s: String, Unicode, Bytes or ByteArray

Returns:ByteArray
bitcoinlib.encoding.to_bytes(s, unhexlify=True)[source]

Convert String, Unicode or ByteArray to Bytes

Parameters:
  • s (str, unicode, bytes, bytearray) – String to convert
  • unhexlify (bool) – Try to unhexlify hexstring
Returns:

Bytes var

bitcoinlib.encoding.to_hexstring(var)[source]

Convert Bytes or ByteArray to hexadecimal string

Parameters:var (bytes, bytearray, str) – Variable to convert to hex string
Returns:hexstring
bitcoinlib.encoding.varbyteint_to_int(byteint)[source]

Convert CompactSize Variable length integer in byte format to integer.

See https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer for specification

Parameters:byteint (bytes, list, bytearray) – 1-9 byte representation
Return int:normal integer
bitcoinlib.encoding.varstr(s)[source]

Convert string to bytestring preceeded with length byte

Parameters:s (bytes, str) – bytestring
Return bytes:varstring