Pure functions reference

Base function primitives implemented in functional programming paradigm.

Mnemonic(BIP39)

pybtc.generate_entropy(strength=256, hex=True)[source]

Generate 128-256 bits entropy bytes string

Parameters:
  • strength (int) – entropy bits strength, by default is 256 bit.
  • hex (boolean) – return HEX encoded string result flag, by default True.
Returns:

HEX encoded or bytes entropy string.

pybtc.load_word_list(language='english', word_list_dir=None)[source]

Load the word list from local file.

Parameters:
  • language (str) – (optional) uses word list language (chinese_simplified, chinese_traditional, english, french, italian, japanese, korean, spanish), by default is english.
  • word_list_dir (str) – (optional) path to a directory containing a list of words, by default None (use BIP39 standard list)
Returns:

list of words.

pybtc.entropy_to_mnemonic(entropy, language='english', word_list_dir=None, word_list=None)[source]

Convert entropy to mnemonic words string.

Parameters:
  • entropy (str,bytes) – random entropy HEX encoded or bytes string.
  • language (str) – (optional) uses word list language (chinese_simplified, chinese_traditional, english, french, italian, japanese, korean, spanish), by default is english.
  • word_list_dir (str) – (optional) path to a directory containing a list of words, by default None (use BIP39 standard list)
  • word_list (list) – (optional) already loaded word list, by default None
Returns:

mnemonic words string.

pybtc.mnemonic_to_entropy(mnemonic, language='english', word_list_dir=None, word_list=None, hex=True)[source]

Converting mnemonic words to entropy.

Parameters:
  • mnemonic (str) – mnemonic words string (space separated)
  • language (str) – (optional) uses word list language (chinese_simplified, chinese_traditional, english, french, italian, japanese, korean, spanish), by default is english.
  • word_list_dir (str) – (optional) path to a directory containing a list of words, by default None (use BIP39 standard list)
  • word_list (list) – (optional) already loaded word list, by default None
  • hex (boolean) – return HEX encoded string result flag, by default True.
Returns:

bytes string.

pybtc.mnemonic_to_seed(mnemonic, passphrase='', hex=True)[source]

Converting mnemonic words string to seed for uses in key derivation (BIP-0032).

Parameters:
  • mnemonic (str) – mnemonic words string (space separated)
  • passphrase (str) – (optional) passphrase to get ability use 2FA approach for creating seed, by default empty string.
  • hex (boolean) – return HEX encoded string result flag, by default True.
Returns:

HEX encoded or bytes string.

Private keys

pybtc.create_private_key(compressed=True, testnet=False, wif=True, hex=False)[source]

Create private key

Parameters:
  • compressed – (optional) Type of public key, by default set to compressed. Using uncompressed public keys is deprecated in new SEGWIT addresses, use this option only for backward compatibility.
  • testnet – (optional) flag for testnet network, by default is False.
  • wif – (optional) If set to True return key in WIF format, by default is True.
  • hex – (optional) If set to True return key in HEX format, by default is False.
Returns:

Private key in wif format (default), hex encoded byte string in case of hex flag or raw bytes string in case wif and hex flags set to False.

pybtc.private_key_to_wif(h, compressed=True, testnet=False)[source]

Encode private key in HEX or RAW bytes format to WIF format.

Parameters:
  • h – private key 32 byte string or HEX encoded string.
  • compressed – (optional) flag of public key compressed format, by default set to True.
  • testnet – (optional) flag for testnet network, by default is False.
Returns:

Private key in WIF format.

pybtc.wif_to_private_key(h, hex=True)[source]

Decode WIF private key to bytes string or HEX encoded string

Parameters:hex – (optional) if set to True return key in HEX format, by default is True.
Returns:Private key HEX encoded string or raw bytes string.
pybtc.is_wif_valid(wif)[source]

Check is private key in WIF format string is valid.

Parameters:wif – private key in WIF format string.
Returns:boolean.

Public keys

Warning

Using uncompressed public keys is deprecated in a new SEGWIT address format. To avoid potential future funds loss, users MUST NOT use uncompressed keys in version 0 witness programs. Use uncompressed keys only for backward compatibilitylegacy in legacy address format (PUBKEY, P2PKH).

pybtc.private_to_public_key(private_key, compressed=True, hex=True)[source]

Get public key from private key using ECDSA secp256k1

Parameters:
  • private_key – private key in WIF, HEX or bytes.
  • compressed – (optional) flag of public key compressed format, by default set to True. In case private_key in WIF format, this flag is set in accordance with the key format specified in WIF string.
  • hex – (optional) if set to True return key in HEX format, by default is True.
Returns:

33/65 bytes public key in HEX or bytes string.

pybtc.is_public_key_valid(key)[source]

Check public key is valid.

Parameters:key – public key in HEX or bytes string format.
Returns:boolean.

Extended keys(BIP32)

pybtc.create_master_xprivate_key(seed, testnet=False, base58=True, hex=False)[source]

Create extended private key from seed

Parameters:
  • key (str,bytes) – seed HEX or bytes string.
  • base58 (boolean) – (optional) return result as base58 encoded string, by default True.
  • hex (boolean) – (optional) return result as HEX encoded string, by default False. In case True base58 flag value will be ignored.
Returns:

extended private key in base58, HEX or bytes string format.

pybtc.xprivate_to_xpublic_key(xprivate_key, base58=True, hex=False)[source]

Get extended public key from extended private key using ECDSA secp256k1

Parameters:
  • key (str,bytes) – extended private key in base58, HEX or bytes string.
  • base58 (boolean) – (optional) return result as base58 encoded string, by default True.
  • hex (boolean) – (optional) return result as HEX encoded string, by default False. In case True base58 flag value will be ignored.
Returns:

extended public key in base58, HEX or bytes string format.

pybtc.derive_xkey(xkey, *path_level, base58=True, hex=False)[source]

Child Key derivation for extended private/public keys

Parameters:
  • xkey (bytes) – extended private/public in base58, HEX or bytes string format.
  • path_level (list) – list of derivation path levels. For hardened derivation use HARDENED_KEY flag.
  • base58 (boolean) – (optional) return result as base58 encoded string, by default True.
  • hex (boolean) – (optional) return result as HEX encoded string, by default False. In case True base58 flag value will be ignored.
Returns:

extended child private/public key in base58, HEX or bytes string format.

pybtc.public_from_xpublic_key(xpublic_key, hex=True)[source]

Get public key from extended public key

Parameters:
  • xpublic_key (bytes) – extended public in base58, HEX or bytes string format.
  • base58 (boolean) – (optional) return result as base58 encoded string, by default True.
  • hex (boolean) – (optional) return result as HEX encoded string, by default False. In case True base58 flag value will be ignored.
Returns:

public key in HEX or bytes string format.

pybtc.private_from_xprivate_key(xprivate_key, wif=True, hex=False)[source]

Get private key from extended private key

Parameters:
  • xprivate_key (bytes) – extended public in base58, HEX or bytes string format.
  • wif (boolean) – (optional) return result as WIF format, by default True.
  • hex (boolean) – (optional) return result as HEX encoded string, by default False. In case True WIF flag value will be ignored.
Returns:

private key in HEX or bytes string format.

Addresses

pybtc.hash_to_address(address_hash, testnet=False, script_hash=False, witness_version=0)[source]

Get address from public key/script hash. In case PUBKEY, P2PKH, P2PKH public key/script hash is SHA256+RIPEMD160, P2WSH script hash is SHA256.

Parameters:
  • address_hash – public key hash or script hash in HEX or bytes string format.
  • testnet – (optional) flag for testnet network, by default is False.
  • script_hash – (optional) flag for script hash (P2SH address), by default is False.
  • witness_version – (optional) witness program version, by default is 0, for legacy address format use None.
Returns:

address in base58 or bech32 format.

pybtc.address_to_hash(address, hex=True)[source]

Get address hash from base58 or bech32 address format.

Parameters:
  • address – address in base58 or bech32 format.
  • hex – (optional) If set to True return key in HEX format, by default is True.
Returns:

script in HEX or bytes string.

pybtc.public_key_to_address(pubkey, testnet=False, p2sh_p2wpkh=False, witness_version=0)[source]

Get address from public key/script hash. In case PUBKEY, P2PKH, P2PKH public key/script hash is SHA256+RIPEMD160, P2WSH script hash is SHA256.

Parameters:
  • pubkey – public key HEX or bytes string format.
  • testnet – (optional) flag for testnet network, by default is False.
  • p2sh_p2wpkh – (optional) flag for P2WPKH inside P2SH address, by default is False.
  • witness_version – (optional) witness program version, by default is 0, for legacy address format use None.
Returns:

address in base58 or bech32 format.

pybtc.address_type(address, num=False)[source]

Get address type.

Parameters:
  • address – address in base58 or bech32 format.
  • num – (optional) If set to True return type in numeric format, by default is False.
Returns:

address type in string or numeric format.

pybtc.address_to_script(address, hex=False)[source]

Get public key script from address.

Parameters:
  • address – address in base58 or bech32 format.
  • hex – (optional) If set to True return key in HEX format, by default is True.
Returns:

public key script in HEX or bytes string.

pybtc.is_address_valid(address, testnet=False)[source]

Check is address valid.

Parameters:
  • address – address in base58 or bech32 format.
  • testnet – (optional) flag for testnet network, by default is False.
Returns:

boolean.

Script

pybtc.decode_script(script, asm=False)[source]

Decode script to ASM format or to human readable OPCODES string.

Parameters:
  • script – script in bytes string or HEX encoded string format.
  • asm – (optional) If set to True decode to ASM format, by default set to False.
Returns:

script in ASM format string or OPCODES string.

pybtc.parse_script(script, segwit=True)[source]

Parse script and return script type, script address and required signatures count.

Parameters:
  • script – script in bytes string or HEX encoded string format.
  • segwit – (optional) If set to True recognize P2WPKH and P2WSH sripts, by default set to True.
Returns:

dictionary:

  • nType - numeric script type
  • type - script type
  • addressHash - address hash in case address recognized
  • script - script if no address recognized
  • reqSigs - required signatures count

pybtc.delete_from_script(script, sub_script)[source]

Decode OPCODE or subscript from script.

Parameters:
  • script – traget script in bytes or HEX encoded string.
  • sub_script – sub_script which is necessary to remove from target script in bytes or HEX encoded string.
Returns:

script in bytes or HEX encoded string corresponding to the format of target script.

pybtc.script_to_hash(script, witness=False, hex=True)[source]

Encode script to hash HASH160 or SHA256 in dependency of the witness.

Parameters:
  • script – script in bytes or HEX encoded string.
  • witness – (optional) If set to True return SHA256 hash for P2WSH, by default is False.
  • hex – (optional) If set to True return key in HEX format, by default is True.
  • sub_script – sub_script which is necessary to remove from target script in bytes or HEX encoded string.
Returns:

script in bytes or HEX encoded string corresponding to the format of target script.

Signatures

pybtc.verify_signature(sig, pub_key, msg)[source]

Verify signature for message and given public key

Parameters:
  • sig – signature in bytes or HEX encoded string.
  • pub_key – public key in bytes or HEX encoded string.
  • msg – message in bytes or HEX encoded string.
Returns:

boolean.

pybtc.sign_message(msg, private_key, hex=True)[source]

Sign message

Parameters:
  • msg – message to sign bytes or HEX encoded string.
  • private_key – private key (bytes, hex encoded string or WIF format)
  • hex – (optional) If set to True return key in HEX format, by default is True.
Returns:

DER encoded signature in bytes or HEX encoded string.

pybtc.is_valid_signature_encoding(sig)[source]

Check is valid signature encoded in DER format

Parameters:sig – signature in bytes or HEX encoded string.
Returns:boolean.

Hash encoding

pybtc.rh2s(raw_hash)[source]

Encode raw transaction hash to HEX string with bytes order change

Parameters:raw_hash – transaction hash in bytes string.
Returns:HEX encoded string.
pybtc.s2rh(hash_string)[source]

Decode HEX transaction hash to bytes with byte order change

Parameters:raw_hash – transaction hash in bytes string.
Returns:bytes string.
pybtc.reverse_hash(raw_hash)[source]

Reverse hash order

Parameters:raw_hash – bytes string.
Returns:bytes string.

Merkle root

pybtc.merkle_root(tx_hash_list, hex=True)[source]

Calculate merkle root from transaction hash list

Parameters:
  • tx_hash_list – list of transaction hashes in bytes or HEX encoded string.
  • hex – (optional) If set to True return result in HEX format, by default is True.
Returns:

merkle root in bytes or HEX encoded string corresponding hex flag.

pybtc.merkle_branches(tx_hash_list, hex=True)[source]

Calculate merkle branches for coinbase transacton

Parameters:
  • tx_hash_list – list of transaction hashes in bytes or HEX encoded string.
  • hex – (optional) If set to True return result in HEX format, by default is True.
Returns:

list of merkle branches in bytes or HEX encoded string corresponding hex flag.

pybtc.merkleroot_from_branches(merkle_branches, coinbase_hash, hex=True)[source]

Calculate merkle root from merkle branches and coinbase transacton hash

Parameters:
  • merkle_branches – list merkle branches in bytes or HEX encoded string.
  • coinbase_hash – list coinbase transaction hash in bytes or HEX encoded string.
  • hex – (optional) If set to True return result in HEX format, by default is True.
Returns:

merkle root in bytes or HEX encoded string corresponding hex flag.

Difficulty

pybtc.bits_to_target(bits)[source]

Calculate target from bits

Parameters:bits – HEX string, bytes string or integer representation of bits.
Returns:integer.
pybtc.target_to_difficulty(target)[source]

Calculate difficulty from target

Parameters:target – integer.
Returns:float.
pybtc.bits_to_difficulty(bits)[source]

Calculate difficulty from bits

Parameters:bits – HEX string, bytes string or integer representation of bits.
Returns:integer.
pybtc.difficulty_to_target(difficulty)[source]

Calculate target from difficulty

Parameters:target – integer.
Returns:float.

Tools

pybtc.bytes_needed(n)[source]

Calculate bytes needed to convert integer to bytes.

Parameters:n – integer.
Returns:integer.
pybtc.int_to_bytes(i, byteorder='big')[source]

Convert integer to bytes.

Parameters:
  • n – integer.
  • byteorder – (optional) byte order ‘big’ or ‘little’, by default ‘big’.
Returns:

bytes.

pybtc.bytes_to_int(i, byteorder='big')[source]

Convert bytes to integer.

Parameters:
  • i – bytes.
  • byteorder – (optional) byte order ‘big’ or ‘little’, by default ‘big’.
Returns:

integer.

pybtc.int_to_var_int(i)[source]

Convert integer to variable integer

Parameters:i – integer.
Returns:bytes.
pybtc.var_int_to_int(data)[source]

Convert variable integer to integer

Parameters:data – bytes variable integer.
Returns:integer.
pybtc.var_int_len(n)[source]

Get variable integer length in bytes from integer value

Parameters:n – integer.
Returns:integer.
pybtc.get_var_int_len(bytes)[source]

Get variable integer length in bytes from bytes

Parameters:bytes – bytes.
Returns:integer.
pybtc.read_var_int(stream)[source]

Read variable integer from io.BytesIO stream to bytes

Parameters:stream – io.BytesIO stream.
Returns:bytes.
pybtc.read_var_list(stream, data_type)[source]

Read variable integer list from io.BytesIO stream to bytes

Parameters:
  • stream – io.BytesIO stream.
  • data_type – list data type.
Returns:

list of data_type.

pybtc.int_to_c_int(n, base_bytes=1)[source]

Convert integer to compresed integer

Parameters:
  • n – integer.
  • base_bytes – len of bytes base from which start compression.
Returns:

bytes.

pybtc.c_int_to_int(b, base_bytes=1)[source]

Convert compressed integer bytes to integer

Parameters:
  • b – compressed integer bytes.
  • base_bytes – len of bytes base from which start compression.
Returns:

integer.

pybtc.c_int_len(n, base_bytes=1)[source]

Get length of compressed integer from integer value

Parameters:
  • n – bytes.
  • base_bytes – len of bytes base from which start compression.
Returns:

integer.