Crypto utils

This package provides a set of tools for working with cryptographic keys and key stores.

Seed phrases

Seed phrases are a list of words that can be used to generate a master private key. This master private key can then be used to derive other private keys for different blockchains.

xchainpy2_crypto.utils.create_address(public_key: bytes, prefix='thor') str

Create address from public key :param bytes public_key: Public key :param str prefix: Address prefix :return: str: Address

xchainpy2_crypto.utils.decode_address(address: str, prefix: str) bytes

Decode address to bytes :param str address: Address :param str prefix: Address prefix (e.g. ‘thor’) :return: bytes: Decoded address

xchainpy2_crypto.utils.derive_address(mnemonic: str, derivation_path: str, prefix='thor', lang: str | None = None) str

Derive address from mnemonic and derivation path :param str mnemonic: Mnemonic phrase or list of words :param str derivation_path: Derivation path :param str prefix: Address prefix :param str lang: Language of mnemonic words :return: str: Address

xchainpy2_crypto.utils.derive_private_key(mnemonic: str, derivation_path: str) bytes

Derive private key from mnemonic and derivation path :param str mnemonic: Mnemonic phrase or list of words :param str derivation_path: Derivation path :return: bytes: Private key

xchainpy2_crypto.utils.encode_address(value: str | bytes, prefix='thor') str

Encode address from the string or bytes :param str value: Input :param str prefix: Address prefix ‘thor’ by default :return: str: Address

xchainpy2_crypto.utils.generate_mnemonic(words_number=Bip39WordsNum.WORDS_NUM_12, lang=Bip39Languages.ENGLISH)

Generate a mnemonic phrase :param words_number: Words number 12/24, default: 12 :param lang: Language, default: English :return: str: Mnemonic phrase

xchainpy2_crypto.utils.get_bip32(seed: bytes, derivation_path: str) Bip32Slip10Secp256k1
xchainpy2_crypto.utils.get_private_key(key: Bip32Slip10Secp256k1) bytes
xchainpy2_crypto.utils.get_public_key(key: Bip32Slip10Secp256k1) bytes
xchainpy2_crypto.utils.get_seed(mnemonic, lang: str | None = None) bytes
Parameters:
  • mnemonic – Mnemonic phrase or list of words

  • lang – Bip39Languages or None

Returns:

bytes: Seed

xchainpy2_crypto.utils.sha256ripemd160(hex_str: str) str

Calculate ripemd160(sha256(hex)) from the hex string :param str hex_str: Input hex string :return: str: Output hex string

xchainpy2_crypto.utils.validate_mnemonic(mnemonic: str, lang: str | None = None) bool
Parameters:
  • mnemonic – Mnemonic phrase or list of words

  • lang – Bip39Languages or None

Returns:

bool: True if valid, False otherwise

Keystore

exception xchainpy2_crypto.keystore.InvalidPasswordException

Bases: Exception

class xchainpy2_crypto.keystore.KeyStore(cipher, ciphertext, cipherparams_iv, kdf, kdfparams_prf, kdfparams_dklen, kdfparams_salt, kdfparams_c, mac, id, version, meta)

Bases: NamedTuple

cipher: str

Alias for field number 0

cipherparams_iv: str

Alias for field number 2

ciphertext: str

Alias for field number 1

decrypt_from_keystore(password: str)

Derives a mnemonic from a keystore with a password :param str password: password :return: str Mnemonic phrase

classmethod encrypt_to_keystore(mnemonic: str, password: str)

Encrypts a mnemonic to a keystore with a password :param str mnemonic: BIP39 mnemonic :param str password: Password :return: KeyStore

classmethod from_dict(j)
classmethod from_file(path)
classmethod generate_and_encrypt(password: str, *args, **kwargs)
id: str

Alias for field number 9

kdf: str

Alias for field number 3

kdfparams_c: int

Alias for field number 7

kdfparams_dklen: int

Alias for field number 5

kdfparams_prf: str

Alias for field number 4

kdfparams_salt: str

Alias for field number 6

mac: str

Alias for field number 8

meta: str

Alias for field number 11

save(path: str, indent=4)
property to_dict
property to_json
version: int

Alias for field number 10