Skip to content
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

feat: load_public_key_files function #20

Merged
merged 1 commit into from
May 1, 2024
Merged
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
34 changes: 30 additions & 4 deletions web3_utils/load_public_keys_from_files.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
from os import path, listdir, getcwd
from typing import TypedDict


def read_public_keys_from_file(file_path: str):
with open(file_path, "r") as f:
lines = [line.strip() for line in f if line.strip()]
return lines


def load_public_keys_from_files(type: str, dir_path: str = path.join(getcwd(), "tmp", "public_keys")):
Expand All @@ -8,9 +15,28 @@ def load_public_keys_from_files(type: str, dir_path: str = path.join(getcwd(), "
for file in files:
if file.startswith(type):
file_path = path.join(dir_path, file)

with open(file_path, "r") as f:
lines = [line.strip() for line in f if line.strip()]
result.extend(lines)
public_keys = read_public_keys_from_file(file_path)
result.extend(public_keys)

return result


class PublicKeyFile(TypedDict):
public_keys: list[str]
file_name: str
type: str


# Seperate function introduced that returns the pubkeys on a per file basis as to not break existing code that uses the previous function
def load_public_key_files(type: str, dir_path: str = path.join(getcwd(), "tmp", "public_keys")) -> list[PublicKeyFile]:
files = listdir(dir_path)
public_key_files: list[PublicKeyFile] = []

for file in files:
if file.startswith(type):
file_name = file.split(".")[0] # strip file extension
file_path = path.join(dir_path, file)
public_keys = read_public_keys_from_file(file_path)
public_key_files.append({"public_keys": public_keys, "file_name": file_name, "type": type})

return public_key_files
Loading