From a53d2d7767e766f8f554e670431cdf51dc412412 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Tue, 4 Nov 2025 09:30:43 +0100 Subject: [PATCH] refactor: get rid of secp lib (#40) --- pyproject.toml | 6 ------ relay/event.py | 9 +++++---- tests/conftest.py | 11 ++++++++--- tests/test_events.py | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 51b273b..fcd6bfc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,12 +30,6 @@ init_typed = true warn_required_dynamic_aliases = true warn_untyped_fields = true -[[tool.mypy.overrides]] -module = [ - "secp256k1.*", -] -ignore_missing_imports = "True" - [tool.pytest.ini_options] log_cli = false testpaths = [ diff --git a/relay/event.py b/relay/event.py index 44d34ae..7154ece 100644 --- a/relay/event.py +++ b/relay/event.py @@ -2,8 +2,8 @@ import hashlib import json from enum import Enum +from coincurve import PublicKeyXOnly from pydantic import BaseModel, Field -from secp256k1 import PublicKey class NostrEventType(str, Enum): @@ -82,14 +82,15 @@ class NostrEvent(BaseModel): f"Invalid event id. Expected: '{event_id}' got '{self.id}'" ) try: - pub_key = PublicKey(bytes.fromhex("02" + self.pubkey), True) + pub_key = PublicKeyXOnly(bytes.fromhex(self.pubkey)) except Exception as exc: raise ValueError( f"Invalid public key: '{self.pubkey}' for event '{self.id}'" ) from exc - valid_signature = pub_key.schnorr_verify( - bytes.fromhex(event_id), bytes.fromhex(self.sig), None, raw=True + valid_signature = pub_key.verify( + bytes.fromhex(self.sig), + bytes.fromhex(event_id), ) if not valid_signature: raise ValueError(f"Invalid signature: '{self.sig}' for event '{self.id}'") diff --git a/tests/conftest.py b/tests/conftest.py index 80fa60c..c5ee8a9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -27,13 +27,18 @@ def event_loop(): @pytest_asyncio.fixture(scope="session", autouse=True) async def migrate_db(): - print("#### 999") db = Database("ext_nostrrelay") + await db.execute("DROP TABLE IF EXISTS nostrrelay.events;") + await db.execute("DROP TABLE IF EXISTS nostrrelay.relays;") + await db.execute("DROP TABLE IF EXISTS nostrrelay.event_tags;") + await db.execute("DROP TABLE IF EXISTS nostrrelay.accounts;") + + # check if exists else skip migrations for key, migrate in inspect.getmembers(migrations, inspect.isfunction): - print("### 1000") logger.info(f"Running migration '{key}'.") await migrate(db) - return db + + yield db @pytest.fixture(scope="session") diff --git a/tests/test_events.py b/tests/test_events.py index 669fe31..f2cbe28 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -20,7 +20,7 @@ def test_valid_event_id_and_signature(valid_events: list[EventFixture]): try: f.data.check_signature() except Exception as e: - logger.error(f"Invalid 'id' ot 'signature' for fixture: '{f.name}'") + logger.error(f"Invalid 'id' of 'signature' for fixture: '{f.name}'") raise e