btwallet.wallet#
Implementation of the wallet class, which manages balances with staking and transfer. Also manages hotkey and coldkey.
Attributes#
Classes#
The wallet class in the Bittensor framework handles wallet functionality, crucial for participating in the Bittensor network. |
Functions#
|
Display the mnemonic and a warning message to keep the mnemonic safe. |
Module Contents#
- btwallet.wallet.BT_WALLET_NAME = 'default'#
- btwallet.wallet.BT_WALLET_PATH = '~/.bittensor/wallets/'#
- btwallet.wallet.display_mnemonic_msg(keypair, key_type)#
Display the mnemonic and a warning message to keep the mnemonic safe.
- Parameters:
keypair (Keypair) – Keypair object.
key_type (str) – Type of the key (coldkey or hotkey).
- class btwallet.wallet.Wallet(name=None, hotkey=None, path=None, config=None)#
The wallet class in the Bittensor framework handles wallet functionality, crucial for participating in the Bittensor network.
It manages two types of keys: coldkey and hotkey, each serving different purposes in network operations. Each wallet contains a coldkey and a hotkey.
The coldkey is the user’s primary key for holding stake in their wallet and is the only way that users can access Tao. Coldkeys can hold tokens and should be encrypted on your device.
The coldkey is the primary key used for securing the wallet’s stake in the Bittensor network (Tao) and is critical for financial transactions like staking and unstaking tokens. It’s recommended to keep the coldkey encrypted and secure, as it holds the actual tokens.
The hotkey, in contrast, is used for operational tasks like subscribing to and setting weights in the network. It’s linked to the coldkey through the metagraph and does not directly hold tokens, thereby offering a safer way to interact with the network during regular operations.
- Parameters:
name (str) – The name of the wallet, used to identify it among possibly multiple wallets.
hotkey (str) – String identifier for the hotkey.
path (str) – File system path where wallet keys are stored.
config (Config) – Bittensor configuration object.
_hotkey (Keypair) – Internal representations of the hotkey and coldkey.
_coldkey (Keypair) – Internal representations of the hotkey and coldkey.
_coldkeypub (Keypair) – Internal representations of the hotkey and coldkey.
- create_if_non_existent, create, recreate
Methods to handle the creation of wallet keys.
- get_coldkey, get_hotkey, get_coldkeypub
Methods to retrieve specific keys.
- set_coldkey, set_hotkey, set_coldkeypub
Methods to set or update keys.
- hotkey_file, coldkey_file, coldkeypub_file
Properties that return respective key file objects.
- regenerate_coldkey, regenerate_hotkey, regenerate_coldkeypub
Methods to regenerate keys from different sources.
- Config, help, add_args
Utility methods for configuration and assistance.
The wallet class is a fundamental component for users to interact securely with the Bittensor network, facilitating both operational tasks and transactions involving value transfer across the network.
Example Usage:
# Create a new wallet with default coldkey and hotkey names my_wallet = wallet() # Access hotkey and coldkey hotkey = my_wallet.get_hotkey() coldkey = my_wallet.get_coldkey() # Set a new coldkey my_wallet.new_coldkey(n_words=24) # number of seed words to use # Update wallet hotkey my_wallet.set_hotkey(new_hotkey) # Print wallet details print(my_wallet) # Access coldkey property, must use password to unlock my_wallet.coldkey
Initialize the bittensor wallet object containing a hot and coldkey.
- Parameters:
name (str, optional) – The name of the wallet to unlock for running bittensor. Defaults to
default
.hotkey (str, optional) – The name of hotkey used to running the miner. Defaults to
default
.path (str, optional) – The path to your bittensor wallets. Defaults to
~/.bittensor/wallets/
.config (Config, optional) – config.Config(). Defaults to
None
.
- config#
- name#
- property hotkey: substrateinterface.Keypair#
Loads the hotkey from wallet.path/wallet.name/hotkeys/wallet.hotkey or raises an error.
- Returns:
hotkey loaded from Config arguments.
- Return type:
hotkey (Keypair)
- Raises:
KeyFileError – Raised if the file is corrupt of non-existent.
CryptoKeyError – Raised if the user enters an incorrect password for an encrypted keyfile.
- path#
- hotkey_str#
- _hotkey = None#
- _coldkey = None#
- _coldkeypub = None#
- __str__()#
Returns a human-readable string representation of the wallet object.
- Returns:
The string representation.
- Return type:
- __repr__()#
Returns the string representation of the wallet object.
- Returns:
The string representation.
- Return type:
- classmethod help()#
Print help to stdout.
- classmethod add_args(parser, prefix=None)#
Accept specific arguments from parser.
- Parameters:
parser (argparse.ArgumentParser) – Argument parser object.
prefix (str) – Argument prefix.
- create_if_non_existent(coldkey_use_password=True, hotkey_use_password=False)#
Checks for existing coldkeypub and hotkeys, and creates them if non-existent.
- create(coldkey_use_password=True, hotkey_use_password=False)#
Checks for existing coldkeypub and hotkeys, and creates them if non-existent.
- recreate(coldkey_use_password=True, hotkey_use_password=False)#
Checks for existing coldkeypub and hotkeys and creates them if non-existent.
- property hotkey_file: btwallet.keyfile.Keyfile#
Property that returns the hotkey file.
- Returns:
The hotkey file.
- Return type:
- property coldkey_file: btwallet.keyfile.Keyfile#
Property that returns the coldkey file.
- Returns:
The coldkey file.
- Return type:
- property coldkeypub_file: btwallet.keyfile.Keyfile#
Property that returns the coldkeypub file.
- Returns:
The coldkeypub file.
- Return type:
- set_hotkey(keypair, encrypt=False, overwrite=False)#
Sets the hotkey for the wallet.
- set_coldkeypub(keypair, encrypt=False, overwrite=False)#
Sets the coldkeypub for the wallet.
- set_coldkey(keypair, encrypt=True, overwrite=False)#
Sets the coldkey for the wallet.
- Parameters:
- Return type:
Keyfile: The coldkey file.
- get_coldkey(password=None)#
Gets the coldkey from the wallet.
- Parameters:
password (str, optional) – The password to decrypt the coldkey. Defaults to
None
.- Returns:
The coldkey keypair.
- Return type:
Keypair
- get_hotkey(password=None)#
Gets the hotkey from the wallet.
- Parameters:
password (str, optional) – The password to decrypt the hotkey. Defaults to
None
.- Returns:
The hotkey keypair.
- Return type:
Keypair
- get_coldkeypub(password=None)#
Gets the coldkeypub from the wallet.
- Parameters:
password (str, optional) – The password to decrypt the coldkeypub. Defaults to
None
.- Returns:
The coldkeypub keypair.
- Return type:
Keypair
- property coldkey: substrateinterface.Keypair#
Loads the coldkey from wallet.path/wallet.name/coldkey or raises an error.
- Returns:
coldkey loaded from Config arguments.
- Return type:
coldkey (Keypair)
- Raises:
KeyFileError – Raised if the file is corrupt of non-existent.
CryptoKeyError – Raised if the user enters an incorrect password for an encrypted keyfile.
- property coldkeypub: substrateinterface.Keypair#
Loads the coldkeypub from wallet.path/wallet.name/coldkeypub.txt or raises an error.
- Returns:
coldkeypub loaded from Config arguments.
- Return type:
coldkeypub (Keypair)
- Raises:
KeyFileError – Raised if the file is corrupt of non-existent.
CryptoKeyError – Raised if the user enters an incorrect password for an encrypted keyfile.
- create_coldkey_from_uri(uri, use_password=True, overwrite=False, suppress=False)#
Creates coldkey from uri string, optionally encrypts it with the user-provided password.
- Parameters:
uri (str) – (str, required): URI string to use i.e.,
/Alice
or/Bob
.use_password (bool, optional) – Is the created key password protected.
overwrite (bool, optional) – Determines if this operation overwrites the coldkey under the same path
<wallet path>/<wallet name>/coldkey
.suppress (bool, optional) – If
True
, suppresses the display of the mnemonic message. Defaults toFalse
.
- Returns:
This object with newly created coldkey.
- Return type:
wallet (Wallet)
- create_hotkey_from_uri(uri, use_password=False, overwrite=False, suppress=False)#
Creates hotkey from uri string, optionally encrypts it with the user-provided password.
- Parameters:
uri (str) – (str, required): URI string to use i.e.,
/Alice
or/Bob
use_password (bool, optional) – Is the created key password protected.
overwrite (bool, optional) – Determines if this operation overwrites the hotkey under the same path
<wallet path>/<wallet name>/hotkeys/<hotkey>
.suppress (bool, optional) – If
True
, suppresses the display of the mnemonic message. Defaults toFalse
.
- Returns:
This object with newly created hotkey.
- Return type:
wallet (Wallet)
- unlock_coldkey()#
Unlocks the coldkey (prompts password if locked)
- Returns:
the unlocked coldkey Keypair.
- Return type:
coldkey (Keypair)
- unlock_coldkeypub()#
Unlocks the coldkeypub.
- Returns:
the unlocked coldkeypub Keypair.
- Return type:
coldkeypub (Keypair)
- unlock_hotkey()#
Unlocks the hotkey.
- Returns:
the unlocked hotkey Keypair.
- Return type:
hotkey (Keypair)
- new_coldkey(n_words=12, use_password=True, overwrite=False, suppress=False)#
Creates a new coldkey, optionally encrypts it with the user-provided password and saves to disk.
- Parameters:
n_words (int) – (int, optional): Number of mnemonic words to use.
use_password (bool, optional) – Is the created key password protected.
overwrite (bool, optional) – Determines if this operation overwrites the coldkey under the same path
<wallet path>/<wallet name>/coldkey
.suppress (bool, optional) – If
True
, suppresses the display of the mnemonic message. Defaults toFalse
.
- Returns:
This object with newly created coldkey.
- Return type:
wallet (Wallet)
- create_new_coldkey(n_words=12, use_password=True, overwrite=False, suppress=False)#
Creates a new coldkey, optionally encrypts it with the user-provided password and saves to disk.
- Parameters:
n_words (int) – (int, optional): Number of mnemonic words to use.
use_password (bool, optional) – Is the created key password protected.
overwrite (bool, optional) – Determines if this operation overwrites the coldkey under the same path
<wallet path>/<wallet name>/coldkey
.suppress (bool, optional) – If
True
, suppresses the display of the mnemonic message. Defaults toFalse
.
- Returns:
This object with newly created coldkey.
- Return type:
wallet (Wallet)
- new_hotkey(n_words=12, use_password=False, overwrite=False, suppress=False)#
Creates a new hotkey, optionally encrypts it with the user-provided password and saves to disk.
- Parameters:
n_words (int) – (int, optional): Number of mnemonic words to use.
use_password (bool, optional) – Is the created key password protected.
overwrite (bool, optional) – Determines if this operation overwrites the hotkey under the same path
<wallet path>/<wallet name>/hotkeys/<hotkey>
.suppress (bool, optional) – If
True
, suppresses the display of the mnemonic message. Defaults toFalse
.
- Returns:
This object with newly created hotkey.
- Return type:
wallet (Wallet)
- create_new_hotkey(n_words=12, use_password=False, overwrite=False, suppress=False)#
Creates a new hotkey, optionally encrypts it with the user-provided password and saves to disk.
- Parameters:
n_words (int) – (int, optional): Number of mnemonic words to use.
use_password (bool, optional) – Is the created key password protected.
overwrite (bool, optional) – Will this operation overwrite the hotkey under the same path <wallet path>/<wallet name>/hotkeys/<hotkey>
suppress (bool, optional) – If
True
, suppresses the display of the mnemonic message. Defaults toFalse
.
- Returns:
This object with newly created hotkey.
- Return type:
wallet (Wallet)
- regenerate_coldkeypub(ss58_address=None, public_key=None, overwrite=False)#
Regenerates the coldkeypub from the passed
ss58_address
or public_key and saves the file. Requires eitherss58_address
or public_key to be passed.- Parameters:
ss58_address (Optional[str]) – (str, optional): Address as
ss58
string.public_key (Optional[Union[str, bytes]]) – (str | bytes, optional): Public key as hex string or bytes.
overwrite (bool, optional) – False): Determines if this operation overwrites the coldkeypub (if exists) under the same path
<wallet path>/<wallet name>/coldkeypub
.
- Returns:
Newly re-generated wallet with coldkeypub.
- Return type:
wallet (Wallet)
- regenerate_coldkey(mnemonic: list | str | None = None, use_password: bool = True, overwrite: bool = False, suppress: bool = False) Wallet #
- regenerate_coldkey(seed: str | None = None, use_password: bool = True, overwrite: bool = False, suppress: bool = False) Wallet
- regenerate_coldkey(json: Tuple[str | Dict, str] | None = None, use_password: bool = True, overwrite: bool = False, suppress: bool = False) Wallet
Regenerates the coldkey from the passed mnemonic or seed, or JSON encrypts it with the user’s password and saves the file.
- Parameters:
use_password (bool, optional) – Is the created key password protected.
overwrite (bool, optional) – Determines if this operation overwrites the coldkey under the same path
<wallet path>/<wallet name>/coldkey
.suppress (bool, optional) – If
True
, suppresses the display of the mnemonic message. Defaults toFalse
.kwargs["mnemonic"] – (Union[list, str], optional item of kwargs): Key mnemonic as list of words or string space separated words.
kwargs["seed"] – (str, optional item of kwargs): Seed as hex string.
kwargs["json"] – (Tuple[Union[str, Dict], str], optional item of kwargs): Restore from encrypted JSON backup as
(json_data: Union[str, Dict], passphrase: str)
- Returns:
This object with newly created coldkey.
- Return type:
wallet (Wallet)
Note
Uses priority order:
mnemonic > seed > json
.
- regenerate_hotkey(*, mnemonic: list | str | None = None, use_password: bool = True, overwrite: bool = False, suppress: bool = False) Wallet #
- regenerate_hotkey(*, seed: str | None = None, use_password: bool = True, overwrite: bool = False, suppress: bool = False) Wallet
- regenerate_hotkey(*, json: Tuple[str | Dict, str] | None = None, use_password: bool = True, overwrite: bool = False, suppress: bool = False) Wallet
Regenerates the hotkey from passed mnemonic or seed, encrypts it with the user’s password and saves the file.
- Parameters:
mnemonic (Union[list, str], optional) – Key mnemonic as list of words or string space separated words.
seed (str, optional) – Seed as hex string.
json (Tuple[Union[str, Dict], str], optional) – Restore from encrypted JSON backup as
(json_data: Union[str, Dict], passphrase: str)
.use_password (bool, optional) – Is the created key password protected.
overwrite (bool, optional) – Determines if this operation overwrites the hotkey under the same path
<wallet path>/<wallet name>/hotkeys/<hotkey>
.suppress (bool, optional) – If
True
, suppresses the display of the mnemonic message. Defaults toFalse
.
- Returns:
This object with newly created hotkey.
- Return type:
wallet (Wallet)