Skip to content

Commit

Permalink
Cam/no excepts (#32)
Browse files Browse the repository at this point in the history
* Remove except usage

* use alias for now

* use different constructor format

* fix log

* remove extra import

* fix keypair.rs errors

* chore: ruff

* chore: clippy

* chore: fmt
  • Loading branch information
camfairchild authored Oct 3, 2024
1 parent 591889d commit 6685852
Show file tree
Hide file tree
Showing 18 changed files with 190 additions and 130 deletions.
15 changes: 13 additions & 2 deletions bittensor_wallet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
from bittensor_wallet.bittensor_wallet import Config, Keyfile, Keypair, Wallet, utils, config, errors, keypair, keyfile, wallet
from bittensor_wallet.bittensor_wallet import (
Config,
Keyfile,
Keypair,
Wallet,
utils,
config,
errors,
keypair,
keyfile,
wallet,
)

__version__ = "2.0.0"
__version__ = "2.0.0"
2 changes: 1 addition & 1 deletion bittensor_wallet/errors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bittensor_wallet.bittensor_wallet import errors as _

ConfigurationError = _.ConfigurationError
KeyFileError = _.KeyFileError
KeyFileError = _.KeyFileError
2 changes: 1 addition & 1 deletion bittensor_wallet/keyfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
deserialize_keypair_from_keyfile_data = _.deserialize_keypair_from_keyfile_data
validate_password = _.validate_password
ask_password = _.ask_password
ask_password_to_encrypt = ask_password # backwards compatibility
ask_password_to_encrypt = ask_password # backwards compatibility
keyfile_data_is_encrypted_nacl = _.keyfile_data_is_encrypted_nacl
keyfile_data_is_encrypted_ansible = _.keyfile_data_is_encrypted_ansible
keyfile_data_is_encrypted_legacy = _.keyfile_data_is_encrypted_legacy
Expand Down
2 changes: 1 addition & 1 deletion bittensor_wallet/keypair/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from bittensor_wallet.bittensor_wallet import keypair as _

Keypair = _.Keypair
Keypair = _.Keypair
2 changes: 0 additions & 2 deletions bittensor_wallet/mock/keyfile_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
from bittensor_wallet import Keypair




class MockKeyfile(Keyfile):
"""Defines an interface to a mocked keyfile object (nothing is created on device) keypair is treated as non encrypted and the data is just the string version."""

Expand Down
6 changes: 4 additions & 2 deletions bittensor_wallet/mock/wallet_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MockWallet(Wallet):
"""
Mocked Version of the bittensor wallet class, meant to be used for testing
"""

def __init__(self, *args, **kwargs):
pass

Expand All @@ -38,7 +38,9 @@ def __new__(cls, name=None, hotkey=None, path=None, config=None, *args, **kwargs
_mock (required=True, default=False):
If true creates a mock wallet with random keys.
"""
cls = super().__new__(cls, name=name, hotkey=hotkey, path=path, config=config, *args, **kwargs)
cls = super().__new__(
cls, name=name, hotkey=hotkey, path=path, config=config, *args, **kwargs
)
# For mocking.
cls._is_mock = True
cls._mocked_coldkey_keyfile = None
Expand Down
2 changes: 1 addition & 1 deletion bittensor_wallet/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
is_valid_ss58_address = _.is_valid_ss58_address
is_valid_ed25519_pubkey = _.is_valid_ed25519_pubkey
is_valid_bittensor_address_or_public_key = _.is_valid_bittensor_address_or_public_key
SS58_FORMAT = _.SS58_FORMAT
SS58_FORMAT = _.SS58_FORMAT
2 changes: 1 addition & 1 deletion bittensor_wallet/wallet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bittensor_wallet.bittensor_wallet import wallet as _

display_mnemonic_msg = _.display_mnemonic_msg
Wallet = _.Wallet
Wallet = _.Wallet
13 changes: 12 additions & 1 deletion btwallet/keyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def serialized_keypair_to_keyfile_data(keypair: "Keypair") -> bytes:
data = json.dumps(json_data).encode()
return data


# sdk - deprecated import
# cli - no
# wallet - inside keyfile.py
Expand Down Expand Up @@ -129,6 +130,7 @@ def deserialize_keypair_from_keyfile_data(keyfile_data: bytes) -> "Keypair":
"Keypair could not be created from keyfile data: {}".format(keyfile_dict)
)


# sdk - deprecated import
# cli - no
# wallet - inside keyfile.py
Expand Down Expand Up @@ -158,6 +160,7 @@ def validate_password(password: str) -> bool:
return False
return True


# sdk - deprecated import
# cli - no
# wallet - inside keyfile.py
Expand All @@ -173,6 +176,7 @@ def ask_password_to_encrypt() -> str:
valid = validate_password(password)
return password


# sdk - deprecated import
# cli - no
# wallet - inside keyfile.py
Expand All @@ -188,6 +192,7 @@ def keyfile_data_is_encrypted_nacl(keyfile_data: bytes) -> bool:
"""
return keyfile_data[: len("$NACL")] == b"$NACL"


# sdk - deprecated import
# cli - no
# wallet - inside keyfile.py
Expand All @@ -201,6 +206,7 @@ def keyfile_data_is_encrypted_ansible(keyfile_data: bytes) -> bool:
"""
return keyfile_data[:14] == b"$ANSIBLE_VAULT"


# sdk - deprecated import
# cli - no
# wallet - inside keyfile.py
Expand All @@ -213,6 +219,7 @@ def keyfile_data_is_encrypted_legacy(keyfile_data: bytes) -> bool:
"""
return keyfile_data[:6] == b"gAAAAA"


# sdk - deprecated import
# cli - no
# wallet - inside keyfile.py
Expand All @@ -230,6 +237,7 @@ def keyfile_data_is_encrypted(keyfile_data: bytes) -> bool:
or keyfile_data_is_encrypted_legacy(keyfile_data)
)


# sdk - deprecated import
# cli - no
# wallet - inside keyfile.py
Expand All @@ -249,6 +257,7 @@ def keyfile_data_encryption_method(keyfile_data: bytes) -> str:
elif keyfile_data_is_encrypted_legacy(keyfile_data):
return "legacy"


# sdk - deprecated import
# cli - no
# wallet - inside keyfile.py
Expand All @@ -262,6 +271,7 @@ def legacy_encrypt_keyfile_data(
vault = Vault(password)
return vault.vault.encrypt(keyfile_data)


# sdk - deprecated import
# cli - no
# wallet - inside keyfile.py
Expand All @@ -288,6 +298,7 @@ def encrypt_keyfile_data(keyfile_data: bytes, password: Optional[str] = None) ->
encrypted = box.encrypt(keyfile_data)
return b"$NACL" + encrypted


# sdk - deprecated import
# cli - no
# wallet - inside keyfile.py
Expand All @@ -306,6 +317,7 @@ def get_coldkey_password_from_environment(coldkey_name: str) -> Optional[str]:
}
return envs.get(f"BT_COLD_PW_{coldkey_name.replace('-', '_').upper()}")


# sdk - deprecated import
# cli - no
# wallet - inside keyfile.py
Expand Down Expand Up @@ -464,7 +476,6 @@ def set_keypair(
if encrypt:
keyfile_data = encrypt_keyfile_data(keyfile_data, password)
self._write_keyfile_data_to_file(keyfile_data, overwrite=overwrite)


def get_keypair(self, password: Optional[str] = None) -> "Keypair":
"""Returns the keypair from the path, decrypts data if the file is encrypted.
Expand Down
2 changes: 1 addition & 1 deletion btwallet/mock/wallet_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MockWallet(Wallet):
def __init__(self, *args, **kwargs) -> None:
pass

def __new__(cls, *args, **kwargs) -> 'MockWallet':
def __new__(cls, *args, **kwargs) -> "MockWallet":
r"""Init bittensor wallet object containing a hot and coldkey.
Args:
_mock (required=True, default=False):
Expand Down
16 changes: 8 additions & 8 deletions btwallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,13 +881,13 @@ def regenerate_hotkey(
) -> "Wallet": ...

def regenerate_hotkey(
self,
mnemonic: Optional[str] = None,
seed: Optional[str] = None,
json: Optional[Tuple[str, str]] = None,
use_password: bool = True,
overwrite: bool = False,
suppress: bool = False,
self,
mnemonic: Optional[str] = None,
seed: Optional[str] = None,
json: Optional[Tuple[str, str]] = 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.
Expand Down Expand Up @@ -930,4 +930,4 @@ def regenerate_hotkey(
keypair = Keypair.create_from_encrypted_json(json_data, passphrase)

self.set_hotkey(keypair, encrypt=use_password, overwrite=overwrite)
return self
return self
Loading

0 comments on commit 6685852

Please sign in to comment.