chore: get rid of secp lib (#114)

* chore: get rid of secp lib

* fixup!
This commit is contained in:
dni ⚡ 2025-11-04 10:34:24 +01:00 committed by GitHub
parent 3a8c16d155
commit 9c00adbf2d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 12 additions and 24 deletions

View file

@ -1,16 +1,16 @@
import base64 import base64
import secrets import secrets
import secp256k1 import coincurve
from bech32 import bech32_decode, convertbits from bech32 import bech32_decode, convertbits
from cffi import FFI
from cryptography.hazmat.primitives import padding from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
def get_shared_secret(privkey: str, pubkey: str): def get_shared_secret(privkey: str, pubkey: str):
point = secp256k1.PublicKey(bytes.fromhex("02" + pubkey), True) pk = coincurve.PublicKey(bytes.fromhex("02" + pubkey))
return point.ecdh(bytes.fromhex(privkey), hashfn=copy_x) sk = coincurve.PrivateKey(bytes.fromhex(privkey))
return sk.ecdh(pk.format())
def decrypt_message(encoded_message: str, encryption_key) -> str: 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: def sign_message_hash(private_key: str, hash_: bytes) -> str:
privkey = secp256k1.PrivateKey(bytes.fromhex(private_key)) privkey = coincurve.PrivateKey(bytes.fromhex(private_key))
sig = privkey.schnorr_sign(hash_, None, raw=True) sig = privkey.sign_schnorr(hash_)
return sig.hex() return sig.hex()
@ -64,17 +64,6 @@ def test_decrypt_encrypt(encoded_message: str, encryption_key):
), f"expected '{encoded_message}', but got '{ecrypted_msg}'" ), 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: def normalize_public_key(pubkey: str) -> str:
if pubkey.startswith("npub1"): if pubkey.startswith("npub1"):
_, decoded_data = bech32_decode(pubkey) _, decoded_data = bech32_decode(pubkey)

View file

@ -2,8 +2,8 @@ import hashlib
import json import json
from typing import List, Optional from typing import List, Optional
from coincurve import PublicKeyXOnly
from pydantic import BaseModel from pydantic import BaseModel
from secp256k1 import PublicKey
class NostrEvent(BaseModel): class NostrEvent(BaseModel):
@ -35,14 +35,14 @@ class NostrEvent(BaseModel):
f"Invalid event id. Expected: '{event_id}' got '{self.id}'" f"Invalid event id. Expected: '{event_id}' got '{self.id}'"
) )
try: try:
pub_key = PublicKey(bytes.fromhex("02" + self.pubkey), True) pub_key = PublicKeyXOnly(bytes.fromhex(self.pubkey))
except Exception: except Exception as exc:
raise ValueError( raise ValueError(
f"Invalid public key: '{self.pubkey}' for event '{self.id}'" f"Invalid public key: '{self.pubkey}' for event '{self.id}'"
) ) from exc
valid_signature = self.sig and pub_key.schnorr_verify( valid_signature = self.sig and pub_key.verify(
bytes.fromhex(event_id), bytes.fromhex(self.sig), None, raw=True bytes.fromhex(self.sig), bytes.fromhex(event_id)
) )
if not valid_signature: if not valid_signature:
raise ValueError(f"Invalid signature: '{self.sig}' for event '{self.id}'") raise ValueError(f"Invalid signature: '{self.sig}' for event '{self.id}'")

View file

@ -29,7 +29,6 @@ plugins = ["pydantic.mypy"]
[[tool.mypy.overrides]] [[tool.mypy.overrides]]
module = [ module = [
"nostr.*", "nostr.*",
"secp256k1.*",
] ]
ignore_missing_imports = "True" ignore_missing_imports = "True"