diff --git a/operate/constants.py b/operate/constants.py index 5659eee8..37bd9301 100644 --- a/operate/constants.py +++ b/operate/constants.py @@ -31,7 +31,6 @@ DEPLOYMENT_JSON = "deployment.json" CONFIG = "config.json" KEY = "key" -KEYS = "keys" KEYS_JSON = "keys.json" DOCKER_COMPOSE_YAML = "docker-compose.yaml" SERVICE_YAML = "service.yaml" diff --git a/operate/keys.py b/operate/keys.py index 5c740aec..e7e52f8e 100644 --- a/operate/keys.py +++ b/operate/keys.py @@ -22,6 +22,7 @@ import json import logging import os +import shutil import typing as t from dataclasses import dataclass from pathlib import Path @@ -85,21 +86,25 @@ def get(self, key: str) -> Key: def create(self) -> str: """Creates new key.""" crypto = EthereumCrypto() - path = self.path / crypto.address - if path.is_file(): - return crypto.address - - path.write_text( - json.dumps( - Key( - ledger=LedgerType.ETHEREUM, - address=crypto.address, - private_key=crypto.private_key, - ).json, - indent=4, - ), - encoding="utf-8", - ) + for path in ( + self.path / f"{crypto.address}.bak", + self.path / crypto.address, + ): + if path.is_file(): + continue + + path.write_text( + json.dumps( + Key( + ledger=LedgerType.ETHEREUM, + address=crypto.address, + private_key=crypto.private_key, + ).json, + indent=4, + ), + encoding="utf-8", + ) + return crypto.address def delete(self, key: str) -> None: @@ -109,17 +114,22 @@ def delete(self, key: str) -> None: @classmethod def migrate_format(cls, path: Path) -> bool: """Migrate the JSON file format if needed.""" + migrated = False + backup_path = path.with_suffix(".bak") + if not backup_path.is_file(): + shutil.copyfile(path, backup_path) + migrated = True + with open(path, "r", encoding="utf-8") as file: data = json.load(file) old_to_new_ledgers = {0: "ethereum", 1: "solana"} - - migrated = False if data.get("ledger") in old_to_new_ledgers: data["ledger"] = old_to_new_ledgers.get(data["ledger"]) migrated = True - with open(path, "w", encoding="utf-8") as file: - json.dump(data, file, indent=2) + if migrated: + with open(path, "w", encoding="utf-8") as file: + json.dump(data, file, indent=2) return migrated