bittensor.utils#

Subpackages#

Submodules#

Attributes#

Exceptions#

VersionCheckError

Exception raised for errors in the version check process.

Functions#

_get_explorer_root_url_by_network_from_map(network, ...)

Returns the explorer root url for the given network name from the given network map.

_is_valid_ed25519_pubkey(public_key)

Checks if the given public_key is a valid ed25519 key.

check_version([timeout])

Check if the current version of Bittensor is up-to-date with the latest version on PyPi.

decode_hex_identity_dict(info_dictionary)

Decodes hex-encoded strings in a dictionary.

format_error_message(error_message, substrate)

Formats an error message from the Subtensor error information for use in extrinsics.

get_explorer_url_for_network(network, block_hash, ...)

Returns the explorer url for the given block hash and network.

get_hash(content[, encoding])

is_valid_bittensor_address_or_public_key(address)

Checks if the given address is a valid destination address.

is_valid_ss58_address(address)

Checks if the given address is a valid ss58 address.

ss58_address_to_bytes(ss58_address)

Converts a ss58 address to a bytes object.

ss58_to_vec_u8(ss58_address)

strtobool(val)

Converts a string to a boolean value.

u16_normalized_float(x)

u64_normalized_float(x)

unlock_key(wallet[, unlock_type])

Attempts to decrypt a wallet's coldkey or hotkey

use_torch()

Force the use of torch over numpy for certain operations.

validate_chain_endpoint(endpoint_url)

Validates if the provided endpoint URL is a valid WebSocket URL.

version_checking([timeout])

Deprecated, kept for backwards compatibility. Use check_version() instead.

Package Contents#

bittensor.utils.RAOPERTAO = 1000000000.0#
bittensor.utils.SS58_FORMAT = 42#
bittensor.utils.U16_MAX = 65535#
bittensor.utils.U64_MAX = 18446744073709551615#
bittensor.utils.UnlockStatus#
exception bittensor.utils.VersionCheckError#

Bases: Exception

Exception raised for errors in the version check process.

Initialize self. See help(type(self)) for accurate signature.

bittensor.utils._get_explorer_root_url_by_network_from_map(network, network_map)#

Returns the explorer root url for the given network name from the given network map.

Parameters:
  • network (str) – The network to get the explorer url for.

  • network_map (dict[str, str]) – The network map to get the explorer url from.

Returns:

The explorer url for the given network. Or None if the network is not in the network map.

Return type:

Optional[dict[str, str]]

bittensor.utils._is_valid_ed25519_pubkey(public_key)#

Checks if the given public_key is a valid ed25519 key.

Parameters:

public_key (Union[str, bytes]) – The public_key to check.

Returns:

True if the public_key is a valid ed25519 key, False otherwise.

Return type:

bool

bittensor.utils.check_version(timeout=15)#

Check if the current version of Bittensor is up-to-date with the latest version on PyPi. Raises a VersionCheckError if the version check fails.

Parameters:

timeout (int) – The timeout for the request to PyPI in seconds. Default is 15.

bittensor.utils.decode_hex_identity_dict(info_dictionary)#

Decodes hex-encoded strings in a dictionary.

This function traverses the given dictionary, identifies hex-encoded strings, and decodes them into readable strings. It handles nested dictionaries and lists within the dictionary.

Parameters:

info_dictionary (dict) – The dictionary containing hex-encoded strings to decode.

Returns:

The dictionary with decoded strings.

Return type:

dict

Examples

input_dict = { … “name”: {“value”: “0x6a6f686e”}, … “additional”: [ … [{“data”: “0x64617461”}] … ] … } decode_hex_identity_dict(input_dict) {‘name’: ‘john’, ‘additional’: [(‘data’, ‘data’)]}

bittensor.utils.format_error_message(error_message, substrate)#

Formats an error message from the Subtensor error information for use in extrinsics.

Parameters:
  • error_message (Union[dict, Exception]) – A dictionary containing the error information from Subtensor, or a SubstrateRequestException containing dictionary literal args.

  • substrate (Union[async_substrate_interface.AsyncSubstrateInterface, substrateinterface.SubstrateInterface]) – The initialised SubstrateInterface object to use.

Returns:

A formatted error message string.

Return type:

str

bittensor.utils.get_explorer_url_for_network(network, block_hash, network_map)#

Returns the explorer url for the given block hash and network.

Parameters:
  • network (str) – The network to get the explorer url for.

  • block_hash (str) – The block hash to get the explorer url for.

  • network_map (dict[str, dict[str, str]]) – The network maps to get the explorer urls from.

Returns:

The explorer url for the given block hash and network. Or None if the network is not known.

Return type:

Optional[dict[str, str]]

bittensor.utils.get_hash(content, encoding='utf-8')#
bittensor.utils.is_valid_bittensor_address_or_public_key(address)#

Checks if the given address is a valid destination address.

Parameters:

address (Union[str, bytes]) – The address to check.

Returns:

True if the address is a valid destination address, False otherwise.

Return type:

bool

bittensor.utils.is_valid_ss58_address(address)#

Checks if the given address is a valid ss58 address.

Parameters:

address (str) – The address to check.

Returns:

True if the address is a valid ss58 address for Bittensor, False otherwise.

Return type:

bool

bittensor.utils.logging#
bittensor.utils.ss58_address_to_bytes(ss58_address)#

Converts a ss58 address to a bytes object.

Parameters:

ss58_address (str)

Return type:

bytes

bittensor.utils.ss58_to_vec_u8(ss58_address)#
Parameters:

ss58_address (str)

Return type:

list[int]

bittensor.utils.strtobool(val)#

Converts a string to a boolean value.

truth-y values are ‘y’, ‘yes’, ‘t’, ‘true’, ‘on’, and ‘1’; false-y values are ‘n’, ‘no’, ‘f’, ‘false’, ‘off’, and ‘0’.

Raises ValueError if ‘val’ is anything else.

Parameters:

val (str)

Return type:

Union[bool, Literal[‘==SUPRESS==’]]

bittensor.utils.torch#
bittensor.utils.u16_normalized_float(x)#
Parameters:

x (int)

Return type:

float

bittensor.utils.u64_normalized_float(x)#
Parameters:

x (int)

Return type:

float

bittensor.utils.unlock_key(wallet, unlock_type='coldkey')#

Attempts to decrypt a wallet’s coldkey or hotkey :param wallet: a Wallet object :param unlock_type: the key type, ‘coldkey’ or ‘hotkey’

Returns: UnlockStatus for success status of unlock, with error message if unsuccessful

Parameters:

wallet (bittensor_wallet.Wallet)

Return type:

UnlockStatus

bittensor.utils.use_torch()#

Force the use of torch over numpy for certain operations.

Return type:

bool

bittensor.utils.validate_chain_endpoint(endpoint_url)#

Validates if the provided endpoint URL is a valid WebSocket URL.

Parameters:

endpoint_url (str)

Return type:

tuple[bool, str]

bittensor.utils.version_checking(timeout=15)#

Deprecated, kept for backwards compatibility. Use check_version() instead.

Parameters:

timeout (int) – The timeout for calling :func:check_version function. Default is 15.