diff --git a/helpers.py b/helpers.py index bb5efaf..478c0e1 100644 --- a/helpers.py +++ b/helpers.py @@ -1,16 +1,16 @@ import base64 import secrets -import secp256k1 +import coincurve from bech32 import bech32_decode, convertbits -from cffi import FFI from cryptography.hazmat.primitives import padding from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes def get_shared_secret(privkey: str, pubkey: str): - point = secp256k1.PublicKey(bytes.fromhex("02" + pubkey), True) - return point.ecdh(bytes.fromhex(privkey), hashfn=copy_x) + pk = coincurve.PublicKey(bytes.fromhex("02" + pubkey)) + sk = coincurve.PrivateKey(bytes.fromhex(privkey)) + return sk.ecdh(pk.format()) def decrypt_message(encoded_message: str, encryption_key) -> str: @@ -48,8 +48,8 @@ def encrypt_message(message: str, encryption_key, iv: bytes | None = None) -> st def sign_message_hash(private_key: str, hash_: bytes) -> str: - privkey = secp256k1.PrivateKey(bytes.fromhex(private_key)) - sig = privkey.schnorr_sign(hash_, None, raw=True) + privkey = coincurve.PrivateKey(bytes.fromhex(private_key)) + sig = privkey.sign_schnorr(hash_) return sig.hex() @@ -64,17 +64,6 @@ def test_decrypt_encrypt(encoded_message: str, encryption_key): ), f"expected '{encoded_message}', but got '{ecrypted_msg}'" -ffi = FFI() - - -@ffi.callback( - "int (unsigned char *, const unsigned char *, const unsigned char *, void *)" -) -def copy_x(output, x32, y32, data): - ffi.memmove(output, x32, 32) - return 1 - - def normalize_public_key(pubkey: str) -> str: if pubkey.startswith("npub1"): _, decoded_data = bech32_decode(pubkey) diff --git a/nostr/event.py b/nostr/event.py index a3216cf..fa775ea 100644 --- a/nostr/event.py +++ b/nostr/event.py @@ -2,8 +2,8 @@ import hashlib import json from typing import List, Optional +from coincurve import PublicKeyXOnly from pydantic import BaseModel -from secp256k1 import PublicKey class NostrEvent(BaseModel): @@ -35,14 +35,14 @@ class NostrEvent(BaseModel): f"Invalid event id. Expected: '{event_id}' got '{self.id}'" ) try: - pub_key = PublicKey(bytes.fromhex("02" + self.pubkey), True) - except Exception: + 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 = self.sig and pub_key.schnorr_verify( - bytes.fromhex(event_id), bytes.fromhex(self.sig), None, raw=True + valid_signature = self.sig and 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/pyproject.toml b/pyproject.toml index b0a9d98..7a0c533 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,6 @@ plugins = ["pydantic.mypy"] [[tool.mypy.overrides]] module = [ "nostr.*", - "secp256k1.*", ] ignore_missing_imports = "True"