@@ -25,7 +25,7 @@ def __init__(
25
25
if not isinstance (content , str ):
26
26
raise TypeError ("Argument 'content' must be of type str" )
27
27
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 )
29
29
self .public_key = public_key
30
30
self .content = content
31
31
self .created_at = created_at
@@ -39,14 +39,19 @@ def serialize(public_key: str, created_at: int, kind: int, tags: "list[list[str]
39
39
data_str = json .dumps (data , separators = (',' , ':' ))
40
40
return data_str .encode ()
41
41
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 )
45
49
self .signature = sig .hex ()
46
50
47
51
def verify (self ) -> bool :
48
52
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 )
50
55
51
56
def to_json_object (self ) -> dict :
52
57
return {
0 commit comments