Skip to content

Commit

Permalink
Async in nostr
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaSat committed Aug 7, 2024
1 parent 2e47f19 commit d703988
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions api/nostr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pygeohash
import hashlib
import uuid
from asgiref.sync import async_to_sync
from nostr_sdk import Keys, Client, EventBuilder, NostrSigner
from api.models import Order
from decouple import config
Expand All @@ -9,7 +10,7 @@
class Nostr:
"""Simple nostr events manager to be used as a cache system for clients"""

async def send_order_event(self, order):
def send_order_event(self, order):
"""Creates the event and sends it to the coordinator relay"""

if config("NOSTR_NSEC", cast=str, default="") == "":
Expand All @@ -23,12 +24,13 @@ async def send_order_event(self, order):
client = Client(signer)

# Add relays and connect
await client.add_relays(["ws://localhost:7777"])
await client.connect()

async_to_sync(client.add_relays)(["ws://localhost:7777"])
async_to_sync(client.connect)()

event = EventBuilder(38383, "", self.generate_tags(order)).to_event(keys)
event.custom_created_at(order.created_at.timestamp())
output = await client.send_event(event)
output = async_to_sync(client.send_event)(event)
print(f"Nostr event sent: {output}")

def generate_tags(self, order):
Expand Down

0 comments on commit d703988

Please sign in to comment.