btwallet.keyfile#

Attributes#

Classes#

Keyfile

Defines an interface for a substrate interface keypair stored on device.

Functions#

serialized_keypair_to_keyfile_data(keypair)

Serializes keypair object into keyfile data.

deserialize_keypair_from_keyfile_data(keyfile_data)

Deserializes Keypair object from passed keyfile data.

validate_password(password)

Validates the password against a password policy.

ask_password_to_encrypt()

Prompts the user to enter a password for key encryption.

keyfile_data_is_encrypted_nacl(keyfile_data)

Returns true if the keyfile data is NaCl encrypted.

keyfile_data_is_encrypted_ansible(keyfile_data)

Returns true if the keyfile data is ansible encrypted.

keyfile_data_is_encrypted_legacy(keyfile_data)

Returns true if the keyfile data is legacy encrypted.

keyfile_data_is_encrypted(keyfile_data)

Returns true if the keyfile data is encrypted.

keyfile_data_encryption_method(keyfile_data)

Returns type of encryption method as a string.

legacy_encrypt_keyfile_data(keyfile_data[, password])

encrypt_keyfile_data(keyfile_data[, password])

Encrypts the passed keyfile data using ansible vault.

get_coldkey_password_from_environment(coldkey_name)

Retrieves the cold key password from the environment variables.

decrypt_keyfile_data(keyfile_data[, password, ...])

Decrypts the passed keyfile data using ansible vault.

Module Contents#

btwallet.keyfile.NACL_SALT = b'\x13q\x83\xdf\xf1Z\t\xbc\x9c\x90\xb5Q\x879\xe9\xb1'#
btwallet.keyfile.console#
btwallet.keyfile.serialized_keypair_to_keyfile_data(keypair)#

Serializes keypair object into keyfile data.

Parameters:

keypair (Keypair) – The keypair object to be serialized.

Returns:

Serialized keypair data.

Return type:

data (bytes)

btwallet.keyfile.deserialize_keypair_from_keyfile_data(keyfile_data)#

Deserializes Keypair object from passed keyfile data.

Parameters:

keyfile_data (bytes) – The keyfile data as bytes to be loaded.

Returns:

The Keypair loaded from bytes.

Return type:

keypair (Keypair)

Raises:

KeyFileError – Raised if the passed bytes cannot construct a keypair object.

btwallet.keyfile.validate_password(password)#

Validates the password against a password policy.

Parameters:

password (str) – The password to verify.

Returns:

True if the password meets validity requirements.

Return type:

valid (bool)

btwallet.keyfile.ask_password_to_encrypt()#

Prompts the user to enter a password for key encryption.

Returns:

The valid password entered by the user.

Return type:

password (str)

btwallet.keyfile.keyfile_data_is_encrypted_nacl(keyfile_data)#

Returns true if the keyfile data is NaCl encrypted.

Parameters:

keyfile_data (bytes, required) – Bytes to validate.

Returns:

True if data is ansible encrypted.

Return type:

is_nacl (bool)

btwallet.keyfile.keyfile_data_is_encrypted_ansible(keyfile_data)#

Returns true if the keyfile data is ansible encrypted.

Parameters:

keyfile_data (bytes) – The bytes to validate.

Returns:

True if the data is ansible encrypted.

Return type:

is_ansible (bool)

btwallet.keyfile.keyfile_data_is_encrypted_legacy(keyfile_data)#

Returns true if the keyfile data is legacy encrypted. :param keyfile_data: The bytes to validate. :type keyfile_data: bytes

Returns:

True if the data is legacy encrypted.

Return type:

is_legacy (bool)

Parameters:

keyfile_data (bytes)

btwallet.keyfile.keyfile_data_is_encrypted(keyfile_data)#

Returns true if the keyfile data is encrypted.

Parameters:

keyfile_data (bytes) – The bytes to validate.

Returns:

True if the data is encrypted.

Return type:

is_encrypted (bool)

btwallet.keyfile.keyfile_data_encryption_method(keyfile_data)#

Returns type of encryption method as a string.

Parameters:

keyfile_data (bytes, required) – Bytes to validate

Returns:

returns the name of encryption method.

Return type:

encryption_method (str)

btwallet.keyfile.legacy_encrypt_keyfile_data(keyfile_data, password=None)#
Parameters:
  • keyfile_data (bytes)

  • password (Optional[str])

Return type:

bytes

btwallet.keyfile.encrypt_keyfile_data(keyfile_data, password=None)#

Encrypts the passed keyfile data using ansible vault.

Parameters:
  • keyfile_data (bytes) – The bytes to encrypt.

  • password (str, optional) – The password used to encrypt the data. If None, asks for user input.

Returns:

The encrypted data.

Return type:

encrypted_data (bytes)

btwallet.keyfile.get_coldkey_password_from_environment(coldkey_name)#

Retrieves the cold key password from the environment variables.

Parameters:

coldkey_name (str) – The name of the cold key.

Returns:

The password retrieved from the environment variables, or None if not found.

Return type:

password (str)

btwallet.keyfile.decrypt_keyfile_data(keyfile_data, password=None, coldkey_name=None)#

Decrypts the passed keyfile data using ansible vault.

Parameters:
  • keyfile_data (bytes) – The bytes to decrypt.

  • password (str, optional) – The password used to decrypt the data. If None, asks for user input.

  • coldkey_name (str, optional) – The name of the cold key. If provided, retrieves the password from environment variables.

Returns:

The decrypted data.

Return type:

decrypted_data (bytes)

Raises:

KeyFileError – Raised if the file is corrupted or if the password is incorrect.

class btwallet.keyfile.Keyfile(path, name)#

Defines an interface for a substrate interface keypair stored on device.

Parameters:
path#
name#
_name#
__str__()#
__repr__()#
property keypair: substrateinterface.Keypair#

Returns the keypair from path, decrypts data if the file is encrypted.

Returns:

The keypair stored under the path.

Return type:

keypair (Keypair)

Raises:

KeyFileError – Raised if the file does not exist, is not readable, writable, corrupted, or if the password is incorrect.

property data: bytes#

Returns the keyfile data under path.

Returns:

The keyfile data stored under the path.

Return type:

keyfile_data (bytes)

Raises:

KeyFileError – Raised if the file does not exist, is not readable, or writable.

property keyfile_data: bytes#

Returns the keyfile data under path.

Returns:

The keyfile data stored under the path.

Return type:

keyfile_data (bytes)

Raises:

KeyFileError – Raised if the file does not exist, is not readable, or writable.

set_keypair(keypair, encrypt=True, overwrite=False, password=None)#

Writes the keypair to the file and optionally encrypts data.

Parameters:
  • keypair (Keypair) – The keypair to store under the path.

  • encrypt (bool, optional) – If True, encrypts the file under the path. Default is True.

  • overwrite (bool, optional) – If True, forces overwrite of the current file. Default is False.

  • password (str, optional) – The password used to encrypt the file. If None, asks for user input.

Raises:

KeyFileError – Raised if the file does not exist, is not readable, writable, or if the password is incorrect.

get_keypair(password=None)#

Returns the keypair from the path, decrypts data if the file is encrypted.

Parameters:

password (str, optional) – The password used to decrypt the file. If None, asks for user input.

Returns:

The keypair stored under the path.

Return type:

keypair (Keypair)

Raises:

KeyFileError – Raised if the file does not exist, is not readable, writable, corrupted, or if the password is incorrect.

make_dirs()#

Creates directories for the path if they do not exist.

exists_on_device()#

Returns True if the file exists on the device.

Returns:

True if the file is on the device.

Return type:

on_device (bool)

is_readable()#

Returns True if the file under path is readable.

Returns:

True if the file is readable.

Return type:

readable (bool)

is_writable()#

Returns True if the file under path is writable.

Returns:

True if the file is writable.

Return type:

writable (bool)

is_encrypted()#

Returns True if the file under path is encrypted.

Returns:

True if the file is encrypted.

Return type:

encrypted (bool)

_may_overwrite()#

Asks the user if it is okay to overwrite the file.

Returns:

True if the user allows overwriting the file.

Return type:

may_overwrite (bool)

check_and_update_encryption(print_result=True, no_prompt=False)#

Check the version of keyfile and update if needed.

Parameters:
  • print_result (bool) – Print the checking result or not.

  • no_prompt (bool) – Skip if no prompt.

Raises:

KeyFileError – Raised if the file does not exist, is not readable, writable.

Returns:

Return True if the keyfile is the most updated with nacl, else False.

Return type:

result (bool)

encrypt(password=None)#

Encrypts the file under the path.

Parameters:

password (str, optional) – The password for encryption. If None, asks for user input.

Raises:

KeyFileError – Raised if the file does not exist, is not readable, or writable.

decrypt(password=None)#

Decrypts the file under the path.

Parameters:

password (str, optional) – The password for decryption. If None, asks for user input.

Raises:

KeyFileError – Raised if the file does not exist, is not readable, writable, corrupted, or if the password is incorrect.

_read_keyfile_data_from_file()#

Reads the keyfile data from the file.

Returns:

The keyfile data stored under the path.

Return type:

keyfile_data (bytes)

Raises:

KeyFileError – Raised if the file does not exist or is not readable.

_write_keyfile_data_to_file(keyfile_data, overwrite=False)#

Writes the keyfile data to the file.

Parameters:
  • keyfile_data (bytes) – The byte data to store under the path.

  • overwrite (bool, optional) – If True, overwrites the data without asking for permission from the user. Default is False.

Raises:

KeyFileError – Raised if the file is not writable or the user responds No to the overwrite prompt.