From 910371e6056648b2f3a1daff220e5db721f7803e Mon Sep 17 00:00:00 2001 From: Volod <141863857+volod-vana@users.noreply.github.com> Date: Mon, 2 Dec 2024 15:58:09 -0500 Subject: [PATCH] Wallet from mnemonics (#49) --- pyproject.toml | 2 +- vana/__init__.py | 2 +- vana/wallet.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e373396..893a9b1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "vana" -version = "0.31.0" +version = "0.32.0" description = "" authors = ["Tim Nunamaker ", "Volodymyr Isai ", "Kahtaf Alam "] readme = "README.md" diff --git a/vana/__init__.py b/vana/__init__.py index ded3e3e..17c340c 100644 --- a/vana/__init__.py +++ b/vana/__init__.py @@ -15,7 +15,7 @@ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -__version__ = "0.31.0" +__version__ = "0.32.0" import rich diff --git a/vana/wallet.py b/vana/wallet.py index 07fe1ba..80c091a 100644 --- a/vana/wallet.py +++ b/vana/wallet.py @@ -227,6 +227,65 @@ def __init__( self._coldkeypub = None self._mnemonics = {} + # Create necessary directories + self._create_wallet_directories() + + # Check for and handle environment variable based wallet initialization + self._init_from_environment() + + def _create_wallet_directories(self): + """Create the necessary wallet directory structure if it doesn't exist.""" + # Create main wallet directory + wallet_path = os.path.expanduser(os.path.join(self.path, self.name)) + # Create hotkeys directory + hotkeys_path = os.path.join(wallet_path, "hotkeys") + + # Create directories if they don't exist + os.makedirs(hotkeys_path, exist_ok=True) + + def _init_from_environment(self): + """ + Initialize wallet components from environment variables if they exist. + Currently supports hotkey initialization from HOTKEY_MNEMONIC. + Creates the wallet files on disk without password protection. + """ + hotkey_mnemonic = os.getenv("HOTKEY_MNEMONIC") + if hotkey_mnemonic: + try: + vana.logging.info("Found HOTKEY_MNEMONIC environment variable. Initializing hotkey...") + + # Enable mnemonic features + Account.enable_unaudited_hdwallet_features() + + # Create account from mnemonic + account = Account.from_mnemonic(hotkey_mnemonic) + + # Create the hotkey file + wallet_path = os.path.expanduser(os.path.join(self.path, self.name)) + hotkey_path = os.path.join(wallet_path, "hotkeys", self.hotkey_str) + + # Create the keyfile + keyfile = vana.keyfile(path=hotkey_path) + + # Set the hotkey without password protection + keyfile.set_keypair( + keypair=account, + encrypt=False, # No encryption for environment-based keys + overwrite=True # Always overwrite when using environment variables + ) + + # Update the wallet's hotkey reference + self._hotkey = account + + vana.logging.success( + f"Successfully initialized hotkey from environment variable and created file at {hotkey_path}" + ) + + except Exception as e: + vana.logging.error(f"Failed to initialize hotkey from environment variable: {str(e)}") + # Don't raise the exception - allow fallback to normal initialization + pass + def __str__(self): """ Returns the string representation of the Wallet object.