nostr_tools.verify_sig¶
- nostr_tools.verify_sig(event_id, pubkey, signature)[source]¶
Verify an event signature using Schnorr verification (secp256k1).
This function verifies that the given Schnorr signature was created by the private key corresponding to the public key for the given event ID. Uses the secp256k1 elliptic curve as specified in the Nostr protocol.
- Parameters:
event_id (
str) – Event ID in hexadecimal format (64 characters). This is the SHA-256 hash of the serialized event.pubkey (
str) – Public key in hexadecimal format (64 characters). This is the x-only public key (Schnorr format).signature (
str) – Schnorr signature in hexadecimal format (128 characters).
- Returns:
- True if signature is valid and matches the event ID and public key.
False if signature is invalid, or if there’s any error during verification.
- Return type:
Examples
Verify event signature:
>>> event_id = "abc123..." # 64-char hex >>> pubkey = "def456..." # 64-char hex >>> signature = "789ghi..." # 128-char hex >>> is_valid = verify_sig(event_id, pubkey, signature) >>> if is_valid: ... print("Signature is valid!")
Validate event from relay:
>>> event_dict = { ... "id": "...", ... "pubkey": "...", ... "sig": "...", ... # ... other fields ... } >>> if verify_sig(event_dict['id'], event_dict['pubkey'], event_dict['sig']): ... event = Event.from_dict(event_dict)
Handle invalid signatures:
>>> if not verify_sig(event_id, pubkey, signature): ... print("Invalid signature - event may be forged") ... return None