Verified Commit b3ecc586 authored by Jakob Moser's avatar Jakob Moser
Browse files

Add basic ingestion script

parent 38608433
Loading
Loading
Loading
Loading
+0 −0

Empty file added.

+51 −0
Changes for poolpay/wire/ingest/__main__.py: 51 added lines, 0 removed lines.
Original line number Diff line number Diff line
import json
from pathlib import Path
from typing import Annotated

import typer
from pyrage import decrypt, x25519

from poolpay.paths import age_secret_key_file
from poolpay.paths import socket_path as default_socket_path
from poolpay.wire.Client import Client

app = typer.Typer()


def _decrypt_if_necessary(content: str) -> str:
    if content.startswith("-----BEGIN AGE ENCRYPTED FILE-----"):
        identity_string = "\n".join(
            l
            for l in age_secret_key_file.read_text(encoding="utf-8").splitlines()
            if not l.startswith("#")
        )
        identity = x25519.Identity.from_str(identity_string)

        return decrypt(content.encode("utf-8"), [identity]).decode("utf-8")
    else:
        return content


@app.command()
def ingest(
    messages_path: Annotated[
        Path,
        typer.Argument(
            help="Path to a *.json file or an *.json.age file containing (encrypted) messages"
        ),
    ],
    socket_path: Annotated[
        Path, typer.Argument(help="Path to the wire socket to send the messages to")
    ] = default_socket_path,
) -> None:
    messages_string = _decrypt_if_necessary(messages_path.read_text(encoding="utf-8"))

    messages = tuple(json.loads(m) for m in messages_string.splitlines())

    with Client(socket_path) as client:
        for m in messages:
            client.send(m)


if __name__ == "__main__":
    app()