Skip to content

Commit

Permalink
refactor: make methods clean.
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy committed Sep 20, 2024
1 parent 16fb4ad commit de3c558
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
__pycache__/
*.py[cod]
*$py.class
.ruff_cache
.ruff_cache
config.json
Empty file removed source/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion source/config/config_example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "immortal relay basic test.",
"target": "wss://nos.lol",
"target": "ws://localhost:7777",
"tests": {
"protocol": {
"outdir": "/reports/znrt_immo",
Expand Down
8 changes: 5 additions & 3 deletions source/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ async def main() -> None:

if cfg.tests["protocol"]:
async with connect(cfg.target) as connection:
private_key_bytes = os.urandom(32)
sec = PrivateKey(private_key_bytes)
sec = PrivateKey.get_random_key()
pub = sec.public_key.hex()

event = Event(pub, 1726846204, 1, [], "test")
event.sign_valid(sec)
event.set_id_valid()

print(event.to_json())
msg = Message("EVENT", event)

msg = Message.event(event)
await connection.send(msg.to_json())

resp = await connection.recv()
Expand Down
7 changes: 7 additions & 0 deletions source/nostr/keys.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import annotations

import secrets
import os

import secp256k1

Expand Down Expand Up @@ -33,5 +36,9 @@ def sign_message_hash(self, hash: bytes) -> str:
sig = sk.schnorr_sign(hash, None, raw=True)
return sig.hex()

@classmethod
def get_random_key(cls) -> PrivateKey:
return cls(os.urandom(32))

def __eq__(self, other):
return self.raw_secret == other.raw_secret
4 changes: 4 additions & 0 deletions source/nostr/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def __init__(self, message_type: str, data: Event) -> None:
def to_json(self) -> str:
return json.dumps([self.message_type, json.loads(self.data.to_json())])

@classmethod
def event(cls, event: Event) -> Message:
return cls("EVENT", event)

@classmethod
def from_json(cls, json_string: str) -> Message:
data = json.loads(json_string)
Expand Down

0 comments on commit de3c558

Please sign in to comment.