Switch to nostr-sdk

This commit is contained in:
thePR0M3TH3AN
2025-06-30 22:49:04 -04:00
parent 0ea1a7b54f
commit 9594c5a2f8
6 changed files with 111 additions and 92 deletions

View File

@@ -16,11 +16,11 @@ def test_nostr_client_uses_custom_relays():
enc_mgr = EncryptionManager(key, Path(tmpdir))
custom_relays = ["wss://relay1", "wss://relay2"]
with patch("nostr.client.WebSocketRelayManager") as MockPool, patch(
with patch("nostr.client.ClientBuilder") as MockBuilder, patch(
"nostr.client.KeyManager"
):
), patch.object(NostrClient, "initialize_client_pool"):
mock_builder = MockBuilder.return_value
with patch.object(enc_mgr, "decrypt_parent_seed", return_value="seed"):
client = NostrClient(enc_mgr, "fp", relays=custom_relays)
MockPool.assert_called_with()
assert client.relays == custom_relays

View File

@@ -4,9 +4,10 @@ import threading
import time
from websocket import create_connection
import asyncio
import websockets
from nostr.key_manager import KeyManager
from pynostr.event import Event, EventKind
from nostr_sdk import nostr_sdk as sdk
class FakeRelay:
@@ -35,7 +36,7 @@ def run_relay(relay, host="localhost", port=8765):
asyncio.run(main())
def test_pynostr_send_receive(tmp_path):
def test_nostr_sdk_send_receive(tmp_path):
relay = FakeRelay()
thread = threading.Thread(target=run_relay, args=(relay,), daemon=True)
thread.start()
@@ -48,12 +49,13 @@ def test_pynostr_send_receive(tmp_path):
ws = create_connection("ws://localhost:8765")
event = Event(kind=EventKind.TEXT_NOTE, content="hello")
event.pubkey = km.get_public_key_hex()
event.created_at = int(time.time())
event.sign(km.get_private_key_hex())
ws.send(event.to_message())
keys = sdk.Keys.parse(km.get_private_key_hex())
event = (
sdk.EventBuilder.text_note("hello")
.build(keys.public_key())
.sign_with_keys(keys)
)
ws.send(json.dumps(["EVENT", json.loads(event.as_json())]))
sub_id = "1"
ws.send(json.dumps(["REQ", sub_id, {}]))

View File

@@ -14,31 +14,40 @@ def setup_client(tmp_path):
key = Fernet.generate_key()
enc_mgr = EncryptionManager(key, tmp_path)
with patch("nostr.client.WebSocketRelayManager"), patch(
with patch("nostr.client.ClientBuilder"), patch(
"nostr.client.KeyManager"
), patch.object(NostrClient, "initialize_client_pool"), patch.object(
) as MockKM, patch.object(NostrClient, "initialize_client_pool"), patch.object(
enc_mgr, "decrypt_parent_seed", return_value="seed"
):
km_inst = MockKM.return_value
km_inst.keys.private_key_hex.return_value = "1" * 64
km_inst.keys.public_key_hex.return_value = "2" * 64
client = NostrClient(enc_mgr, "fp")
return client
class FakeEvent:
KIND_TEXT_NOTE = 1
KIND_ENCRYPT = 2
def __init__(self):
self._id = "id"
def __init__(self, kind, content, pub_key=None):
self.kind = kind
self.content = content
self.pubkey = pub_key
self.id = "id"
def id(self):
return self._id
def sign(self, _):
pass
class FakeUnsignedEvent:
def sign_with_keys(self, _):
return FakeEvent()
class FakeBuilder:
def build(self, _):
return FakeUnsignedEvent()
def test_publish_json_success():
with TemporaryDirectory() as tmpdir, patch("nostr.client.Event", FakeEvent):
with TemporaryDirectory() as tmpdir, patch(
"nostr.client.EventBuilder.text_note", return_value=FakeBuilder()
):
client = setup_client(Path(tmpdir))
with patch.object(client, "publish_event") as mock_pub:
assert client.publish_json_to_nostr(b"data") is True
@@ -46,7 +55,9 @@ def test_publish_json_success():
def test_publish_json_failure():
with TemporaryDirectory() as tmpdir, patch("nostr.client.Event", FakeEvent):
with TemporaryDirectory() as tmpdir, patch(
"nostr.client.EventBuilder.text_note", return_value=FakeBuilder()
):
client = setup_client(Path(tmpdir))
with patch.object(client, "publish_event", side_effect=Exception("boom")):
assert client.publish_json_to_nostr(b"data") is False