Pure functions reference

Base function primitives implemented in functional programming paradigm.

Mnemonic(BIP39)

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

Generate 128-256 bits entropy bytes string

Parameters
  • strength – (optional) entropy bits strength, by default is 256 bit.

  • hex – (optional) return HEX encoded string result flag, by default is True.

Returns

HEX encoded or bytes entropy string.

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

Load the word list from local file.

Parameters
  • language – (optional) uses word list language (chinese_simplified, chinese_traditional, english, french, italian, japanese, korean, spanish), by default is english.

  • word_list_dir – (optional) path to a directory containing a list of words, by default is None (use BIP39 standard list)

Returns

list of words.

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

Convert entropy to mnemonic words string.

Parameters
  • entropy – random entropy HEX encoded or bytes string.

  • language – (optional) uses word list language (chinese_simplified, chinese_traditional, english, french, italian, japanese, korean, spanish), by default is english.

  • word_list_dir – (optional) path to a directory containing a list of words, by default is None (use BIP39 standard list)

  • word_list – (optional) already loaded word list, by default is None

Returns

mnemonic words string.

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

Converting mnemonic words to entropy.

Parameters
  • mnemonic – mnemonic words string (space separated)

  • language – (optional) uses word list language (chinese_simplified, chinese_traditional, english, french, italian, japanese, korean, spanish), by default is english.

  • word_list_dir – (optional) path to a directory containing a list of words, by default None (use BIP39 standard list)

  • word_list – (optional) already loaded word list, by default is None

  • hex – (optional) return HEX encoded string result flag, by default is True.

Returns

HEX encoded or bytes string.

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

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

Parameters
  • mnemonic – mnemonic words string (space separated)

  • passphrase – (optional) passphrase to get ability use 2FA approach for creating seed, by default is empty string.

  • hex – (optional) return HEX encoded string result flag, by default is True.

Returns

HEX encoded or bytes string.

Private keys

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

Create private key

Parameters
  • compressed – (optional) flag of private key compressed format, by default is True.

  • 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.

pybgl.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 is True.

  • testnet – (optional) flag for testnet network, by default is False.

Returns

Private key in WIF format.

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

Decode WIF private key to bytes string or HEX encoded string

Parameters
  • h – private key in WIF format string.

  • 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.

pybgl.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).

pybgl.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 is 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.

pybgl.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)

pybgl.create_master_xprivate_key(seed, testnet=False, base58=None, hex=None)[source]

Create extended private key from seed

Parameters
  • seed – seed HEX or bytes string.

  • testnet – (optional) flag for testnet network, by default is False.

  • base58 – (optional) return result as base58 encoded string, by default is True.

  • hex – (optional) return result as HEX encoded string, by default is False. In case True base58 flag value will be ignored.

Returns

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

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

Get extended public key from extended private key using ECDSA secp256k1

Parameters
  • xprivate_key – extended private key in base58, HEX or bytes string.

  • base58 – (optional) return result as base58 encoded string, by default is True.

  • hex – (optional) return result as HEX encoded string, by default is False. In case True base58 flag value will be ignored.

Returns

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

pybgl.derive_xkey(xkey, path, base58=None, hex=None)[source]

Child Key derivation for extended private/public keys

Parameters
  • xkey – extended private/public in base58, HEX or bytes string format.

  • path – list of derivation path levels. For hardened derivation use HARDENED_KEY flag.

  • base58 – (optional) return result as base58 encoded string, by default is True.

  • hex – (optional) return result as HEX encoded string, by default is False. In case True base58 flag value will be ignored.

Returns

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

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

Get public key from extended public key

Parameters
  • xpublic_key – extended public in base58, HEX or bytes string format.

  • base58 – (optional) return result as base58 encoded string, by default is True.

  • hex – (optional) return result as HEX encoded string, by default is False. In case True base58 flag value will be ignored.

Returns

public key in HEX or bytes string format.

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

Get private key from extended private key

Parameters
  • xprivate_key – extended private in base58, HEX or bytes string format.

  • wif – (optional) return result as WIF format, by default is True.

  • hex – (optional) return result as HEX encoded string, by default is False. In case True WIF flag value will be ignored.

Returns

private key in HEX or bytes string format.

Addresses

pybgl.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.

pybgl.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.

pybgl.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.

pybgl.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.

pybgl.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.

pybgl.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

pybgl.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 is False.

Returns

script in ASM format string or OPCODES string.

pybgl.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 is 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

pybgl.delete_from_script(script, sub_script)[source]

Delete OP_CODE or subscript from script.

Parameters
  • script – target 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.

pybgl.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

hash HASH160 or SHA256 of script in bytes or HEX encoded.

Signatures

pybgl.verify_signature(sig, pub_key, msg, encoding='hex')[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.

pybgl.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.

pybgl.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

pybgl.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.

pybgl.s2rh(hash_string)[source]

Decode HEX transaction hash to bytes with byte order change

Parameters

hash_string – HEX encoded string.

Returns

bytes string.

pybgl.reverse_hash(raw_hash)[source]

Reverse hash order

Parameters

raw_hash – bytes string.

Returns

bytes string.

Merkle root

pybgl.merkle_root_double_sha256(tx_hash_list, return_hex=True, receive_hex=True)[source]

Calculate merkle root from transaction hash list with double sha256

Parameters
  • tx_hash_list – list of transaction hashes in bytes or HEX encoded string.

  • return_hex – (optional) If set to True return result in HEX format, by default is True.

  • receive_hex – (optional) If set to False no internal check or decode from hex to bytes, by default is True.

Returns

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

pybgl.merkle_root_sha256(tx_hash_list, return_hex=True, receive_hex=True)[source]

Calculate merkle root from transaction hash list with sha256

Parameters
  • tx_hash_list – list of transaction hashes in bytes or HEX encoded string.

  • return_hex – (optional) If set to True return result in HEX format, by default is True.

  • receive_hex – (optional) If set to False no internal check or decode from hex to bytes, by default is True.

Returns

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

pybgl.merkle_root_sha3_256(tx_hash_list, return_hex=True, receive_hex=True)[source]

Calculate merkle root from transaction hash list with sha3 256

Parameters
  • tx_hash_list – list of transaction hashes in bytes or HEX encoded string.

  • return_hex – (optional) If set to True return result in HEX format, by default is True.

  • receive_hex – (optional) If set to False no internal check or decode from hex to bytes, by default is True.

Returns

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

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

Calculate merkle branches for coinbase transacton :param tx_hash_list: list of transaction hashes in bytes or HEX encoded string. :param hex: (optional) If set to True return result in HEX format, by default is True. :return: list of merkle branches in bytes or HEX encoded string corresponding hex flag.

Difficulty

pybgl.bits_to_target(bits)[source]

Calculate target from bits

Parameters

bits – HEX string, bytes string or integer representation of bits.

Returns

integer.

pybgl.target_to_difficulty(target)[source]

Calculate difficulty from target

Parameters

target – integer.

Returns

float.

pybgl.bits_to_difficulty(bits)[source]

Calculate difficulty from bits

Parameters

bits – HEX string, bytes string or integer representation of bits.

Returns

integer.

pybgl.difficulty_to_target(difficulty)[source]

Calculate target from difficulty

Parameters

target – integer.

Returns

float.

Tools

pybgl.encode_base58(bytes, checksum=False)[source]

Encode bytes to a base58-encoded string :param bytes: bytes string. :param checksum: (optional) boolean, by default is False. :return: base58 string format.

pybgl.decode_base58(string_base58, hex=False, checksum=False, verify_checksum=False)[source]

Decode a base58-encoding string :param string_base58: base58 string. :param hex: (optional) return result as HEX encoded string, by default is False. :param checksum: (optional) boolean, by default is False. :param verify_checksum: (optional) boolean, by default is False. :return: HEX or bytes string format.

pybgl.bytes_needed(n)[source]

Calculate bytes needed to convert integer to bytes.

Parameters

n – integer.

Returns

integer.

pybgl.int_to_bytes(i, byteorder='big')[source]

Convert integer to bytes.

Parameters
  • i – integer.

  • byteorder – (optional) byte order ‘big’ or ‘little’, by default is ‘big’.

Returns

bytes.

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

Convert bytes to integer.

Parameters
  • i – bytes.

  • byteorder – (optional) byte order ‘big’ or ‘little’, by default is ‘big’.

Returns

integer.

pybgl.int_to_var_int(i)[source]

Convert integer to variable integer

Parameters

i – integer.

Returns

bytes.

pybgl.var_int_to_int(data)[source]

Convert variable integer to integer

Parameters

data – bytes variable integer.

Returns

integer.

pybgl.var_int_len(n)[source]

Get variable integer length in bytes from integer value

Parameters

n – integer.

Returns

integer.

pybgl.get_var_int_len(bytes)[source]

Get variable integer length in bytes from bytes

Parameters

bytes – bytes.

Returns

integer.

pybgl.read_var_int(stream)[source]

Read variable integer from io.BytesIO stream to bytes

Parameters

stream – io.BytesIO stream.

Returns

bytes.

pybgl.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.

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

Convert integer to compressed integer :param n: integer. :param base_bytes: len of bytes base from which start compression. :return: bytes.

pybgl.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.

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

Get length of compressed integer from integer value :param n: bytes. :param base_bytes: len of bytes base from which start compression. :return: integer.