Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 38 additions & 42 deletions bittensor_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def get_n_words(n_words: Optional[int]) -> int:
while n_words not in [12, 15, 18, 21, 24]:
n_words = int(
Prompt.ask(
"Choose number of words: 12, 15, 18, 21, 24",
"Choose number of words",
choices=["12", "15", "18", "21", "24"],
default=12,
)
Expand Down Expand Up @@ -903,9 +903,9 @@ def wallet_transfer(

def wallet_swap_hotkey(
self,
wallet_name: Optional[str] = Options.wallet_name,
wallet_name: Optional[str] = Options.wallet_name_req,
wallet_path: Optional[str] = Options.wallet_path,
wallet_hotkey: Optional[str] = Options.wallet_hotkey,
wallet_hotkey: Optional[str] = Options.wallet_hk_req,
network: Optional[str] = Options.network,
chain: Optional[str] = Options.chain,
destination_hotkey_name: Optional[str] = typer.Argument(
Expand Down Expand Up @@ -1099,15 +1099,14 @@ def wallet_faucet(

def wallet_regen_coldkey(
self,
wallet_name: Optional[str] = Options.wallet_name,
wallet_name: Optional[str] = Options.wallet_name_req,
wallet_path: Optional[str] = Options.wallet_path,
wallet_hotkey: Optional[str] = Options.wallet_hotkey,
mnemonic: Optional[str] = Options.mnemonic,
seed: Optional[str] = Options.seed,
json: Optional[str] = Options.json,
json_password: Optional[str] = Options.json_password,
use_password: Optional[bool] = Options.use_password,
overwrite_coldkey: Optional[bool] = Options.overwrite_coldkey,
):
"""
[blue]Regenerate a coldkey[/blue] for a wallet on the Bittensor network.
Expand Down Expand Up @@ -1142,22 +1141,16 @@ def wallet_regen_coldkey(
json,
json_password,
use_password,
overwrite_coldkey,
)
)

def wallet_regen_coldkey_pub(
self,
wallet_name: Optional[str] = Options.wallet_name,
wallet_name: Optional[str] = Options.wallet_name_req,
wallet_path: Optional[str] = Options.wallet_path,
wallet_hotkey: Optional[str] = Options.wallet_hotkey,
public_key_hex: Optional[str] = Options.public_hex_key,
ss58_address: Optional[str] = Options.ss58_address,
overwrite_coldkeypub: Optional[bool] = typer.Option(
False,
help="Overwrites the existing coldkeypub file with the new one.",
prompt=True,
),
):
"""
[red]Regenerate[/red] the public part of a coldkey [blue](coldkeypub)[/blue] for a wallet.
Expand Down Expand Up @@ -1194,22 +1187,19 @@ def wallet_regen_coldkey_pub(
rich.print("[red]Error: Invalid SS58 address or public key![/red]")
raise typer.Exit()
return self._run_command(
wallets.regen_coldkey_pub(
wallet, ss58_address, public_key_hex, overwrite_coldkeypub
)
wallets.regen_coldkey_pub(wallet, ss58_address, public_key_hex)
)

def wallet_regen_hotkey(
self,
wallet_name: Optional[str] = Options.wallet_name,
wallet_name: Optional[str] = Options.wallet_name_req,
wallet_path: Optional[str] = Options.wallet_path,
wallet_hotkey: Optional[str] = Options.wallet_hk_req,
mnemonic: Optional[str] = Options.mnemonic,
seed: Optional[str] = Options.seed,
json: Optional[str] = Options.json,
json_password: Optional[str] = Options.json_password,
use_password: Optional[bool] = Options.use_password,
overwrite_hotkey: Optional[bool] = Options.overwrite_hotkey,
):
"""
[red]Regenerates a hotkey[/red] for a wallet.
Expand Down Expand Up @@ -1241,7 +1231,6 @@ def wallet_regen_hotkey(
json,
json_password,
use_password,
overwrite_hotkey,
)
)

Expand All @@ -1252,7 +1241,6 @@ def wallet_new_hotkey(
wallet_hotkey: Optional[str] = Options.wallet_hk_req,
n_words: Optional[int] = None,
use_password: bool = Options.use_password,
overwrite_hotkey: bool = Options.overwrite_hotkey,
):
"""
Create a [red]new hotkey[/red] under a wallet.
Expand All @@ -1274,9 +1262,7 @@ def wallet_new_hotkey(
wallet_name, wallet_path, wallet_hotkey, validate=False
)
n_words = get_n_words(n_words)
return self._run_command(
wallets.new_hotkey(wallet, n_words, use_password, overwrite_hotkey)
)
return self._run_command(wallets.new_hotkey(wallet, n_words, use_password))

def wallet_new_coldkey(
self,
Expand All @@ -1285,7 +1271,6 @@ def wallet_new_coldkey(
wallet_hotkey: Optional[str] = Options.wallet_hotkey,
n_words: Optional[int] = None,
use_password: Optional[bool] = Options.use_password,
overwrite_coldkey: Optional[bool] = typer.Option(),
):
"""
[blue]Create a new coldkey[/blue] under a wallet. A coldkey, is essential for holding balances and performing high-value transactions.
Expand All @@ -1307,9 +1292,7 @@ def wallet_new_coldkey(
wallet_name, wallet_path, wallet_hotkey, validate=False
)
n_words = get_n_words(n_words)
return self._run_command(
wallets.new_coldkey(wallet, n_words, use_password, overwrite_coldkey)
)
return self._run_command(wallets.new_coldkey(wallet, n_words, use_password))

def wallet_check_ck_swap(
self,
Expand Down Expand Up @@ -1343,8 +1326,6 @@ def wallet_create_wallet(
wallet_hotkey: Optional[str] = Options.wallet_hk_req,
n_words: Optional[int] = None,
use_password: bool = Options.use_password,
overwrite_hotkey: bool = Options.overwrite_hotkey,
overwrite_coldkey: bool = Options.overwrite_coldkey,
):
"""
[red]Generate[/red] both a new [blue]coldkey[/blue] and [red]hotkey[/red] under a specified wallet.
Expand All @@ -1370,7 +1351,9 @@ def wallet_create_wallet(
n_words = get_n_words(n_words)
return self._run_command(
wallets.wallet_create(
wallet, n_words, use_password, overwrite_coldkey, overwrite_hotkey
wallet,
n_words,
use_password,
)
)

Expand Down Expand Up @@ -1608,8 +1591,13 @@ def wallet_get_id(
def wallet_sign(
self,
wallet_path: str = Options.wallet_path,
wallet_name: str = Options.wallet_name,
wallet_name: str = Options.wallet_name_req,
wallet_hotkey: str = Options.wallet_hotkey,
use_hotkey: bool = typer.Option(
False,
"--use-hotkey",
help="If specified, the message will be signed by the hotkey",
),
message: str = typer.Option("", help="The message to encode and sign"),
):
"""
Expand All @@ -1630,10 +1618,20 @@ def wallet_sign(
on your preference for brevity or clarity. This command is essential for users to easily prove their ownership
over a coldkey or a hotkey.
"""
if not message:
message = typer.prompt("Enter the message to encode and sign: ")
wallet = self.wallet_ask(wallet_name, wallet_path, wallet_hotkey)
return self._run_command(wallets.sign(wallet, message))
if not use_hotkey:
use_hotkey = typer.confirm(
"Do you want to sign using the hotkey?", default=False
)

if use_hotkey and not wallet_hotkey:
wallet_hotkey = typer.prompt("Enter your hotkey name")
wallet = self.wallet_ask(wallet.name, wallet_path, wallet_hotkey)

if not message:
message = typer.prompt("Enter the message to encode and sign")

return self._run_command(wallets.sign(wallet, message, use_hotkey))

def root_list(
self,
Expand Down Expand Up @@ -2666,7 +2664,7 @@ def stake_remove(

def stake_get_children(
self,
wallet_name: Optional[str] = Options.wallet_name,
wallet_name: Optional[str] = Options.wallet_name_req,
wallet_hotkey: Optional[str] = Options.wallet_hk_req,
wallet_path: Optional[str] = Options.wallet_path,
network: Optional[str] = Options.network,
Expand Down Expand Up @@ -2708,7 +2706,7 @@ def stake_set_children(
children: list[str] = typer.Option(
[], "--children", "-c", help="Enter children hotkeys (ss58)", prompt=False
),
wallet_name: str = Options.wallet_name,
wallet_name: str = Options.wallet_name_req,
wallet_hotkey: str = Options.wallet_hk_req,
wallet_path: str = Options.wallet_path,
network: str = Options.network,
Expand All @@ -2725,7 +2723,7 @@ def stake_set_children(
wait_for_finalization: bool = Options.wait_for_finalization,
):
"""
[Red]Add children hotkeys[/red] on a specified subnet on the Bittensor network.
[red]Add children hotkeys[/red] on a specified subnet on the Bittensor network.

This command is used to delegate authority to different hotkeys, securing their position and influence on the
subnet.
Expand Down Expand Up @@ -2773,7 +2771,7 @@ def stake_set_children(

def stake_revoke_children(
self,
wallet_name: Optional[str] = Options.wallet_name,
wallet_name: Optional[str] = Options.wallet_name_req,
wallet_hotkey: Optional[str] = Options.wallet_hk_req,
wallet_path: Optional[str] = Options.wallet_path,
network: Optional[str] = Options.network,
Expand All @@ -2783,8 +2781,7 @@ def stake_revoke_children(
wait_for_finalization: bool = Options.wait_for_finalization,
):
"""
[red]Remove all children hotkeys[/red] on a specified subnet on the Bittensor
network.
[red]Remove all children hotkeys[/red] on a specified subnet on the Bittensor network.

This command is used to remove delegated authority from all child hotkeys, removing their position and influence
on the subnet.
Expand Down Expand Up @@ -2816,7 +2813,7 @@ def stake_revoke_children(

def stake_childkey_take(
self,
wallet_name: Optional[str] = Options.wallet_name,
wallet_name: Optional[str] = Options.wallet_name_req,
wallet_hotkey: Optional[str] = Options.wallet_hk_req,
wallet_path: Optional[str] = Options.wallet_path,
network: Optional[str] = Options.network,
Expand All @@ -2833,8 +2830,7 @@ def stake_childkey_take(
),
):
"""
Get and set your [red]childkey take[/red] on a specified subnet on the Bittensor
network.
Get and set your [red]childkey take[/red] on a specified subnet on the Bittensor network.

This command is used to set the take on your child hotkeys with limits between 0 - 18%.

Expand Down
34 changes: 14 additions & 20 deletions bittensor_cli/src/commands/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ async def regen_coldkey(
json_path: Optional[str] = None,
json_password: Optional[str] = "",
use_password: Optional[bool] = True,
overwrite_coldkey: Optional[bool] = False,
):
"""Creates a new coldkey under this wallet"""
json_str: Optional[str] = None
Expand All @@ -67,18 +66,18 @@ async def regen_coldkey(
seed=seed,
json=(json_str, json_password),
use_password=use_password,
overwrite=overwrite_coldkey,
overwrite=False,
)


async def regen_coldkey_pub(
wallet: Wallet, ss58_address: str, public_key_hex: str, overwrite_coldkeypub: bool
wallet: Wallet, ss58_address: str, public_key_hex: str,
):
"""Creates a new coldkeypub under this wallet."""
wallet.regenerate_coldkeypub(
ss58_address=ss58_address,
public_key=public_key_hex,
overwrite=overwrite_coldkeypub,
overwrite=False,
)


Expand All @@ -89,7 +88,6 @@ async def regen_hotkey(
json_path: Optional[str],
json_password: Optional[str] = "",
use_password: Optional[bool] = True,
overwrite_hotkey: Optional[bool] = False,
):
"""Creates a new hotkey under this wallet."""
json_str: Optional[str] = None
Expand All @@ -105,49 +103,47 @@ async def regen_hotkey(
seed=seed,
json=(json_str, json_password),
use_password=use_password,
overwrite=overwrite_hotkey,
overwrite=False,
)


async def new_hotkey(
wallet: Wallet, n_words: int, use_password: bool, overwrite_hotkey: bool
wallet: Wallet, n_words: int, use_password: bool,
):
"""Creates a new hotkey under this wallet."""
wallet.create_new_hotkey(
n_words=n_words,
use_password=use_password,
overwrite=overwrite_hotkey,
overwrite=False,
)


async def new_coldkey(
wallet: Wallet, n_words: int, use_password: bool, overwrite_coldkey: bool
wallet: Wallet, n_words: int, use_password: bool,
):
"""Creates a new coldkey under this wallet."""
wallet.create_new_coldkey(
n_words=n_words,
use_password=use_password,
overwrite=overwrite_coldkey,
overwrite=False,
)


async def wallet_create(
wallet: Wallet,
n_words: int = 12,
use_password: bool = True,
overwrite_coldkey: bool = False,
overwrite_hotkey: bool = False,
):
"""Creates a new wallet."""
wallet.create_new_coldkey(
n_words=n_words,
use_password=use_password,
overwrite=overwrite_coldkey,
overwrite=False,
)
wallet.create_new_hotkey(
n_words=n_words,
use_password=False,
overwrite=overwrite_hotkey,
overwrite=False,
)


Expand Down Expand Up @@ -1503,7 +1499,7 @@ async def check_coldkey_swap(wallet: Wallet, subtensor: SubtensorInterface):
)


async def sign(wallet: Wallet, message: str):
async def sign(wallet: Wallet, message: str, use_hotkey: str):
"""Sign a message using the provided wallet or hotkey."""

try:
Expand All @@ -1513,11 +1509,9 @@ async def sign(wallet: Wallet, message: str):
":cross_mark: [red]Keyfile is corrupt, non-writable, non-readable or the password used to decrypt is "
"invalid[/red]:[bold white]\n [/bold white]"
)

keypair = wallet.coldkey

# Use a hotkey if the user specified it
if wallet.hotkey:
if not use_hotkey:
keypair = wallet.coldkey
else:
keypair = wallet.hotkey

signed_message = keypair.sign(message.encode("utf-8")).hex()
Expand Down