Skip to content

Refactor: Create backups of keys #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
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
1 change: 0 additions & 1 deletion operate/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
48 changes: 29 additions & 19 deletions operate/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import json
import logging
import os
import shutil
import typing as t
from dataclasses import dataclass
from pathlib import Path
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Copy link
Member Author

@OjusWiZard OjusWiZard Jun 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kalraash I Need a second opinion, that this method of copying file should not corrupt the original file, even if the process is killed in between, right?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fine @OjusWiZard as shutil function never modifies or locks the original file. Even if the process is terminated mid-copy, only the destination file (backup_path) is at risk of being incomplete or corrupted.

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