bitcoinlib.encoding module

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

Bases: Exception

Log and raise encoding errors

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

Convert Base58 encoded address to public key hash

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

Public Key Hash

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

Decode bech32 / segwit address to public key hash

Validate the Bech32 string, and determine HRP and data

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

Public Key Hash

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

Convert base58 or bech32 address to public key hash

  • address (str) – Crypto currency address in base-58 format
  • as_hex (bool) – Output as hexstring
  • encoding (str) – Address encoding used: base58 or bech32
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]

  • 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(signature, as_hex=True)[source]

Convert DER encoded signature to signature

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


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

‘General power-of-2 base conversion’


  • data (list, bytearray) – 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.double_sha256(string, as_hex=False)[source]

Get double SHA256 hash of string

  • string (bytes) – String to be hashed
  • as_hex – Return value as hexadecimal string. Default is False

:type as_hex

Return bytes, str:

Creates a RIPEMD-160 + SHA256 hash of the input string

Parameters:string (bytes) – Script
Return bytes:RIPEMD-160 hash of script

Convert integer to CompactSize Variable length integer in byte format.

See for specification

Parameters:inp (int) – Integer to convert
Returns:byteint: 1-9 byte representation as integer

Normalize a string to the default NFKD unicode format See

Parameters:string (bytes, bytearray, str) – string value
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

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

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

bitcoinlib.encoding.pubkeyhash_to_addr(pubkeyhash, prefix=None, encoding='base58')[source]

Convert public key hash to base58 encoded address

  • pubkeyhash (bytes, str) – Public key hash
  • prefix (str, bytes) – Prefix version byte of network, default is bitcoin ‘’
  • encoding (str) – Encoding of address to calculate: base58 or bech32. Default is base58
Return str:

Base58 or bech32 encoded address

bitcoinlib.encoding.pubkeyhash_to_addr_base58(pubkeyhash, prefix=b'\x00')[source]

Convert public key hash to base58 encoded address

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

Base-58 encoded address

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

Encode public key hash as bech32 encoded (segwit) address

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

For more information see BIP173 proposal at

  • pubkeyhash (str, bytes, bytearray) – Public key hash
  • prefix (str) – Address prefix or 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


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

Parameters:string (bytes, str, bytearray) – String, Unicode, Bytes or ByteArray
Return bytearray:
bitcoinlib.encoding.to_bytes(string, unhexlify=True)[source]

Convert String, Unicode or ByteArray to Bytes

  • string (str, unicode, bytes, bytearray) – String to convert
  • unhexlify (bool) – Try to unhexlify hexstring

Bytes var


Convert Bytes or ByteArray to hexadecimal string

Parameters:string (bytes, bytearray, str) – Variable to convert to hex string

Convert CompactSize Variable length integer in byte format to integer.

See for specification

Parameters:byteint (bytes, list, bytearray) – 1-9 byte representation
Return int:normal integer

Convert string to variably sized string: Bytestring preceeded with length byte

Parameters:string (bytes, str) – String input
Return bytes:varstring