# 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)

  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)

  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)

  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)

  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)

  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)

  Check public key is valid.

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

## Addresses

`pybtc.``hash_to_address`(address_hash, testnet=False, script_hash=False, witness_version=0)

  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)

  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)

  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)

  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)

  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)

  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)

  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)

  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)

  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)

  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)

  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)

  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)

  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)

  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)

  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)

  Reverse hash order

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

## Merkle root

`pybtc.``merkle_root`(tx_hash_list, hex=True)

  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)

  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)

  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)

  Calculate target from bits

  Parameters: | bits – HEX string, bytes string or integer representation of bits.
  Returns: | integer.

`pybtc.``target_to_difficulty`(target)

  Calculate difficulty from target

  Parameters: | target – integer.
  Returns: | float.

`pybtc.``bits_to_difficulty`(bits)

  Calculate difficulty from bits

  Parameters: | bits – HEX string, bytes string or integer representation of bits.
  Returns: | integer.

`pybtc.``difficulty_to_target`(difficulty)

  Calculate target from difficulty

  Parameters: | target – integer.
  Returns: | float.

## Tools

`pybtc.``bytes_needed`(n)

  Calculate bytes needed to convert integer to bytes.

  Parameters: | n – integer.
  Returns: | integer.

`pybtc.``int_to_bytes`(i, byteorder='big')

  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')

  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)

  Convert integer to variable integer

  Parameters: | i – integer.
  Returns: | bytes.

`pybtc.``var_int_to_int`(data)

  Convert variable integer to integer

  Parameters: | data – bytes vriable integer.
  Returns: | integer.

`pybtc.``var_int_len`(n)

  Get variable integer length in bytes from integer value

  Parameters: | n – integer.
  Returns: | integer.

`pybtc.``get_var_int_len`(bytes)

  Get variable integer length in bytes from bytes

  Parameters: | bytes – bytes.
  Returns: | integer.

`pybtc.``read_var_int`(stream)

  Read variable integer from io.BytesIO stream to bytes

  Parameters: | stream – io.BytesIO stream.
  Returns: | bytes.

`pybtc.``read_var_list`(stream, data_type)

  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)

  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)

  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)

  Get length of compressed integer from integer value

  Parameters:

  - n – bytes.
  - base_bytes – len of bytes base from which start compression.

  Returns: | integer.