From c5949f75269e852d4de8d22ae8feafad5086da78 Mon Sep 17 00:00:00 2001 From: PascalR <24775431+mroxso@users.noreply.github.com> Date: Wed, 12 Apr 2023 16:06:00 +0200 Subject: [PATCH] dev2main (#1) * initial code commit * fixes * fix prints not showing in docker * add github workflow * fix naming of docker image build workflow --- .github/workflows/docker-image.yml | 38 +++++++++++++++++++++++++++ Dockerfile | 6 +++++ createAccount.py | 9 +++++++ main.py | 41 ++++++++++++++++++++++++++++++ requirements.txt | 3 +++ 5 files changed, 97 insertions(+) create mode 100644 .github/workflows/docker-image.yml create mode 100644 Dockerfile create mode 100644 createAccount.py create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..2d21312 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,38 @@ +name: Docker Build and Push + +on: + push: + branches: + - main + +jobs: + build_and_push: + name: Build and Push + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push + id: docker_build + uses: docker/build-push-action@v4 + with: + context: . + push: true + tags: ghcr.io/${{ github.repository_owner }}/timechain-nostr-bot:latest + + - name: Image digest + run: echo ${{ steps.docker_build.outputs.digest }} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..181b945 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3.9 +RUN pip install --upgrade pip +COPY . /app +WORKDIR /app +RUN pip install -r requirements.txt +CMD ["python", "-u" ,"main.py"] diff --git a/createAccount.py b/createAccount.py new file mode 100644 index 0000000..5047fb4 --- /dev/null +++ b/createAccount.py @@ -0,0 +1,9 @@ +from nostr.key import PrivateKey + +private_key = PrivateKey() +public_key = private_key.public_key +print(f"Private key: {private_key.bech32()}") +print(f"Public key: {public_key.bech32()}") +print("--------------------") +print(f"Private key: {private_key.hex()}") +print(f"Public key: {public_key.hex()}") \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..a6264dc --- /dev/null +++ b/main.py @@ -0,0 +1,41 @@ +import requests +import time +import ssl +import os +from nostr.event import Event +from nostr.relay_manager import RelayManager +from nostr.message_type import ClientMessageType +from nostr.key import PrivateKey + +relay_manager = RelayManager() +relay_manager.add_relay("wss://nostr.0x50.tech") +# relay_manager.add_relay("wss://relay.damus.io") +relay_manager.open_connections({"cert_reqs": ssl.CERT_NONE}) # NOTE: This disables ssl certificate verification +time.sleep(1.25) # allow the connections to open + +env_private_key = os.environ.get("PRIVATE_KEY") +# print the value of the environment variable, if it exists +if not env_private_key: + print('The environment variable "PRIVATE_KEY" is not set.') + exit(1) + +private_key = PrivateKey(bytes.fromhex(env_private_key)) + +while True: + old_block_height = 0 + url = "https://blockchain.info/latestblock" + response = requests.get(url) + data = response.json() + block_height = data["height"] + + if(block_height > old_block_height): + print("Die aktuelle Bitcoin-Blockhöhe beträgt:", block_height) + event = Event( + content=str(block_height), + public_key=private_key.public_key.hex() + ) + private_key.sign_event(event) + relay_manager.publish_event(event) + relay_manager.close_connections() # NEEDED?! + old_block_height = block_height + time.sleep(60) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fb6d37a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +requests +# time +nostr \ No newline at end of file