Skip to content
This repository was archived by the owner on May 22, 2024. It is now read-only.

Commit 033e1f1

Browse files
committed
add compute_id method to event class
1 parent 66d95a5 commit 033e1f1

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

nostr/event.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(
2525
if not isinstance(content, str):
2626
raise TypeError("Argument 'content' must be of type str")
2727

28-
self.id = id if id != None else sha256(Event.serialize(public_key, created_at, kind, tags, content)).hexdigest()
28+
self.id = id if not id is None else Event.compute_id(public_key, created_at, kind, tags, content)
2929
self.public_key = public_key
3030
self.content = content
3131
self.created_at = created_at
@@ -39,14 +39,19 @@ def serialize(public_key: str, created_at: int, kind: int, tags: "list[list[str]
3939
data_str = json.dumps(data, separators=(',', ':'))
4040
return data_str.encode()
4141

42-
def sign(self, sk: str) -> None:
43-
private_key = PrivateKey(bytes.fromhex(sk))
44-
sig = private_key.schnorr_sign(bytes.fromhex(self.id), None, raw=True)
42+
@staticmethod
43+
def compute_id(public_key: str, created_at: int, kind: int, tags: "list[list[str]]", content: str) -> str:
44+
return sha256(Event.serialize(public_key, created_at, kind, tags, content)).hexdigest()
45+
46+
def sign(self, private_key_hex: str) -> None:
47+
sk = PrivateKey(bytes.fromhex(private_key_hex))
48+
sig = sk.schnorr_sign(bytes.fromhex(self.id), None, raw=True)
4549
self.signature = sig.hex()
4650

4751
def verify(self) -> bool:
4852
pub_key = PublicKey(bytes.fromhex("02" + self.public_key), True) # add 02 for schnorr (bip340)
49-
return pub_key.schnorr_verify(bytes.fromhex(self.id), bytes.fromhex(self.signature), None, raw=True)
53+
event_id = Event.compute_id(self.public_key, self.created_at, self.kind, self.tags, self.content)
54+
return pub_key.schnorr_verify(event_id, bytes.fromhex(self.signature), None, raw=True)
5055

5156
def to_json_object(self) -> dict:
5257
return {

0 commit comments

Comments
 (0)