Pure functions reference¶
Base function primitives implemented in functional programming paradigm.
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.
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.
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.
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 fromat, 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.
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.
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.
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 vriable 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.