bittensor.keyfile#

Module Contents#

Classes#

keyfile

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

Mockkeyfile

The Mockkeyfile is a mock object representing a keyfile that does not exist on the 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 true if the keyfile data is encrypted.

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.

Attributes#

NACL_SALT

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

Serializes keypair object into keyfile data. :param keypair: The keypair object to be serialized. :type keypair: bittensor.Keypair

Returns:

Serialized keypair data.

Return type:

data (bytes)

Parameters:

keypair (bittensor.Keypair) –

bittensor.keyfile.deserialize_keypair_from_keyfile_data(keyfile_data)#

Deserializes Keypair object from passed keyfile data. :param keyfile_data: The keyfile data as bytes to be loaded. :type keyfile_data: bytes

Returns:

The Keypair loaded from bytes.

Return type:

keypair (bittensor.Keypair)

Raises:

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

Parameters:

keyfile_data (bytes) –

bittensor.keyfile.validate_password(password)#

Validates the password against a password policy. :param password: The password to verify. :type password: str

Returns:

True if the password meets validity requirements.

Return type:

valid (bool)

Parameters:

password (str) –

bittensor.keyfile.ask_password_to_encrypt()#

Prompts the user to enter a password for key encryption. :returns: The valid password entered by the user. :rtype: password (str)

Return type:

str

bittensor.keyfile.keyfile_data_is_encrypted_nacl(keyfile_data)#

Returns true if the keyfile data is NaCl encrypted. :param keyfile_data: Bytes to validate :type keyfile_data: bytes, required

Returns:

True if data is ansible encrypted.

Return type:

is_nacl (bool)

Parameters:

keyfile_data (bytes) –

bittensor.keyfile.keyfile_data_is_encrypted_ansible(keyfile_data)#

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

Returns:

True if the data is ansible encrypted.

Return type:

is_ansible (bool)

Parameters:

keyfile_data (bytes) –

bittensor.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) –

bittensor.keyfile.keyfile_data_is_encrypted(keyfile_data)#

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

Returns:

True if the data is encrypted.

Return type:

is_encrypted (bool)

Parameters:

keyfile_data (bytes) –

bittensor.keyfile.keyfile_data_encryption_method(keyfile_data)#

Returns true if the keyfile data is encrypted. :param keyfile_data: Bytes to validate :type keyfile_data: bytes, required

Returns:

True if data is encrypted.

Return type:

encryption_method (bool)

Parameters:

keyfile_data (bytes) –

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

  • password (str) –

Return type:

bytes

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

Encrypts the passed keyfile data using ansible vault. :param keyfile_data: The bytes to encrypt. :type keyfile_data: bytes :param password: The password used to encrypt the data. If None, asks for user input. :type password: str, optional

Returns:

The encrypted data.

Return type:

encrypted_data (bytes)

Parameters:
  • keyfile_data (bytes) –

  • password (str) –

bittensor.keyfile.get_coldkey_password_from_environment(coldkey_name)#

Retrieves the cold key password from the environment variables. :param coldkey_name: The name of the cold key. :type coldkey_name: str

Returns:

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

Return type:

password (str)

Parameters:

coldkey_name (str) –

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

Decrypts the passed keyfile data using ansible vault. :param keyfile_data: The bytes to decrypt. :type keyfile_data: bytes :param password: The password used to decrypt the data. If None, asks for user input. :type password: str, optional :param coldkey_name: The name of the cold key. If provided, retrieves the password from environment variables. :type coldkey_name: str, optional

Returns:

The decrypted data.

Return type:

decrypted_data (bytes)

Raises:

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

Parameters:
  • keyfile_data (bytes) –

  • password (str) –

  • coldkey_name (Optional[str]) –

class bittensor.keyfile.keyfile(path)#

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

Parameters:

path (str) –

property keypair: bittensor.Keypair#

Returns the keypair from path, decrypts data if the file is encrypted. :returns: The keypair stored under the path. :rtype: keypair (bittensor.Keypair)

Raises:

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

Return type:

bittensor.Keypair

property data: bytes#

Returns the keyfile data under path. :returns: The keyfile data stored under the path. :rtype: keyfile_data (bytes)

Raises:

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

Return type:

bytes

property keyfile_data: bytes#

Returns the keyfile data under path. :returns: The keyfile data stored under the path. :rtype: keyfile_data (bytes)

Raises:

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

Return type:

bytes

__str__()#

Return str(self).

__repr__()#

Return repr(self).

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

Writes the keypair to the file and optionally encrypts data. :param keypair: The keypair to store under the path. :type keypair: bittensor.Keypair :param encrypt: If True, encrypts the file under the path. Default is True. :type encrypt: bool, optional :param overwrite: If True, forces overwrite of the current file. Default is False. :type overwrite: bool, optional :param password: The password used to encrypt the file. If None, asks for user input. :type password: str, optional

Raises:

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

Parameters:
  • keypair (bittensor.Keypair) –

  • encrypt (bool) –

  • overwrite (bool) –

  • password (str) –

get_keypair(password=None)#

Returns the keypair from the path, decrypts data if the file is encrypted. :param password: The password used to decrypt the file. If None, asks for user input. :type password: str, optional

Returns:

The keypair stored under the path.

Return type:

keypair (bittensor.Keypair)

Raises:

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

Parameters:

password (str) –

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. :rtype: on_device (bool)

Return type:

bool

is_readable()#

Returns True if the file under path is readable. :returns: True if the file is readable. :rtype: readable (bool)

Return type:

bool

is_writable()#

Returns True if the file under path is writable. :returns: True if the file is writable. :rtype: writable (bool)

Return type:

bool

is_encrypted()#

Returns True if the file under path is encrypted. :returns: True if the file is encrypted. :rtype: encrypted (bool)

Return type:

bool

_may_overwrite()#

Asks the user if it’s okay to overwrite the file. :returns: True if the user allows overwriting the file. :rtype: may_overwrite (bool)

Return type:

bool

check_and_update_encryption(print_result=True, no_prompt=False)#

Check the version of keyfile and update if needed. :param print_result: Print the checking result or not. :type print_result: bool :param no_prompt: Skip if no prompt. :type no_prompt: bool

Raises:

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

Returns:

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

Return type:

result (bool)

Parameters:
  • print_result (bool) –

  • no_prompt (bool) –

encrypt(password=None)#

Encrypts the file under the path. :param password: The password for encryption. If None, asks for user input. :type password: str, optional

Raises:

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

Parameters:

password (str) –

decrypt(password=None)#

Decrypts the file under the path. :param password: The password for decryption. If None, asks for user input. :type password: str, optional

Raises:

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

Parameters:

password (str) –

_read_keyfile_data_from_file()#

Reads the keyfile data from the file. :returns: The keyfile data stored under the path. :rtype: keyfile_data (bytes)

Raises:

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

Return type:

bytes

_write_keyfile_data_to_file(keyfile_data, overwrite=False)#

Writes the keyfile data to the file. :param keyfile_data: The byte data to store under the path. :type keyfile_data: bytes :param overwrite: If True, overwrites the data without asking for permission from the user. Default is False. :type overwrite: bool, optional

Raises:

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

Parameters:
  • keyfile_data (bytes) –

  • overwrite (bool) –

class bittensor.keyfile.Mockkeyfile(path)#

The Mockkeyfile is a mock object representing a keyfile that does not exist on the device. It is designed for use in testing scenarios and simulations where actual filesystem operations are not required. The keypair stored in the Mockkeyfile is treated as non-encrypted and the data is stored as a serialized string.

Parameters:

path (str) –

property keypair#

Returns the mock keypair stored in the keyfile.

Returns:

The mock keypair.

Return type:

bittensor.Keypair

property data#

Returns the serialized keypair data stored in the keyfile.

Returns:

The serialized keypair data.

Return type:

bytes

__str__()#

Returns a string representation of the Mockkeyfile. The representation will indicate if the keyfile is empty, encrypted, or decrypted.

Returns:

The string representation of the Mockkeyfile.

Return type:

str

__repr__()#

Returns a string representation of the Mockkeyfile, same as __str__().

Returns:

The string representation of the Mockkeyfile.

Return type:

str

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

Sets the mock keypair in the keyfile. The encrypt and overwrite parameters are ignored.

Parameters:
  • keypair (bittensor.Keypair) – The mock keypair to be set.

  • encrypt (bool, optional) – Ignored in this context. Defaults to True.

  • overwrite (bool, optional) – Ignored in this context. Defaults to False.

  • password (str, optional) – Ignored in this context. Defaults to None.

get_keypair(password=None)#

Returns the mock keypair stored in the keyfile. The password parameter is ignored.

Parameters:

password (str, optional) – Ignored in this context. Defaults to None.

Returns:

The mock keypair stored in the keyfile.

Return type:

bittensor.Keypair

make_dirs()#

Creates the directories for the mock keyfile. Does nothing in this class, since no actual filesystem operations are needed.

exists_on_device()#

Returns True indicating that the mock keyfile exists on the device (although it is not created on the actual file system).

Returns:

Always returns True for Mockkeyfile.

Return type:

bool

is_readable()#

Returns True indicating that the mock keyfile is readable (although it is not read from the actual file system).

Returns:

Always returns True for Mockkeyfile.

Return type:

bool

is_writable()#

Returns True indicating that the mock keyfile is writable (although it is not written to the actual file system).

Returns:

Always returns True for Mockkeyfile.

Return type:

bool

is_encrypted()#

Returns False indicating that the mock keyfile is not encrypted.

Returns:

Always returns False for Mockkeyfile.

Return type:

bool

encrypt(password=None)#

Raises a ValueError since encryption is not supported for the mock keyfile.

Parameters:

password (str, optional) – Ignored in this context. Defaults to None.

Raises:

ValueError – Always raises this exception for Mockkeyfile.

decrypt(password=None)#

Returns without doing anything since the mock keyfile is not encrypted.

Parameters:

password (str, optional) – Ignored in this context. Defaults to None.

check_and_update_encryption(no_prompt=None, print_result=False)#