2024-07-01 16:00:17 +00:00
|
|
|
import pygeohash
|
2024-07-15 10:33:09 +00:00
|
|
|
import hashlib
|
|
|
|
import uuid
|
2024-07-01 16:43:35 +00:00
|
|
|
from nostr_sdk import Keys, Client, EventBuilder, NostrSigner
|
2024-07-01 16:00:17 +00:00
|
|
|
from api.models import Order
|
|
|
|
from decouple import config
|
|
|
|
|
|
|
|
|
|
|
|
class Nostr:
|
|
|
|
"""Simple nostr events manager to be used as a cache system for clients"""
|
|
|
|
|
2024-07-15 10:33:09 +00:00
|
|
|
async def send_order_event(self, order):
|
2024-07-01 16:00:17 +00:00
|
|
|
"""Creates the event and sends it to the coordinator relay"""
|
2024-07-15 14:36:30 +00:00
|
|
|
|
2024-07-16 09:11:08 +00:00
|
|
|
if config("NOSTR_NSEC", cast=str, default="") == "":
|
|
|
|
return
|
|
|
|
|
2024-07-15 14:36:30 +00:00
|
|
|
print("Sending nostr event")
|
|
|
|
|
2024-07-01 16:00:17 +00:00
|
|
|
# Initialize with coordinator Keys
|
2024-07-16 09:11:08 +00:00
|
|
|
keys = Keys.parse(config("NOSTR_NSEC", cast=str))
|
2024-07-01 16:00:17 +00:00
|
|
|
signer = NostrSigner.keys(keys)
|
|
|
|
client = Client(signer)
|
|
|
|
|
|
|
|
# Add relays and connect
|
2024-07-15 08:19:53 +00:00
|
|
|
await client.add_relays(["ws://localhost:7777"])
|
2024-07-01 16:00:17 +00:00
|
|
|
await client.connect()
|
|
|
|
|
2024-07-01 16:43:35 +00:00
|
|
|
event = EventBuilder(38383, "", self.generate_tags(order)).to_event(keys)
|
2024-07-16 09:11:08 +00:00
|
|
|
event.custom_created_at(order.created_at.timestamp())
|
2024-07-01 16:43:35 +00:00
|
|
|
output = await client.send_event(event)
|
2024-07-01 16:00:17 +00:00
|
|
|
print(f"Nostr event sent: {output}")
|
|
|
|
|
|
|
|
def generate_tags(self, order):
|
2024-07-15 10:33:09 +00:00
|
|
|
hashed_id = hashlib.md5(
|
|
|
|
f"{config("COORDINATOR_ALIAS", cast=str)}{order.id}".encode("utf-8")
|
|
|
|
).hexdigest()
|
|
|
|
|
2024-07-01 16:00:17 +00:00
|
|
|
return [
|
2024-07-15 10:33:09 +00:00
|
|
|
["d", uuid.UUID(hashed_id)],
|
2024-07-01 16:00:17 +00:00
|
|
|
["name", order.maker.robot_name],
|
|
|
|
["k", order.type.lower()],
|
|
|
|
["f", order.currency],
|
2024-07-15 10:33:09 +00:00
|
|
|
["s", self.get_status_tag(order)],
|
2024-07-16 09:11:08 +00:00
|
|
|
["amt", "0"],
|
2024-07-01 16:00:17 +00:00
|
|
|
["fa", order.amount],
|
|
|
|
["pm", order.payment_method.split(" ")],
|
|
|
|
["premium", order.premium_percentile],
|
2024-07-01 16:43:35 +00:00
|
|
|
[
|
|
|
|
"source",
|
|
|
|
f"{config("HOST_NAME")}/{config("COORDINATOR_ALIAS")}/order/{order.id}",
|
|
|
|
],
|
2024-07-01 16:00:17 +00:00
|
|
|
["expiration", order.expires_at.timestamp()],
|
2024-07-16 09:11:08 +00:00
|
|
|
["y", "robosats", config("COORDINATOR_ALIAS", cast=str)],
|
2024-07-01 16:00:17 +00:00
|
|
|
["n", order.network],
|
2024-07-16 11:16:48 +00:00
|
|
|
["layer", self.get_layer_tag(order)],
|
2024-07-01 16:00:17 +00:00
|
|
|
["g", pygeohash.encode(order.latitude, order.longitude)],
|
2024-07-01 16:43:35 +00:00
|
|
|
["bond", order.bond],
|
2024-07-15 10:33:09 +00:00
|
|
|
["z", "order"],
|
2024-07-01 16:00:17 +00:00
|
|
|
]
|
2024-07-15 10:33:09 +00:00
|
|
|
|
|
|
|
def get_status_tag(self, order):
|
|
|
|
if order.status == Order.Status.PUB:
|
|
|
|
return "pending"
|
|
|
|
else:
|
2024-07-16 11:16:48 +00:00
|
|
|
return "success"
|
|
|
|
|
|
|
|
def get_layer_tag(self, order):
|
|
|
|
if order.type == Order.Types.SELL:
|
|
|
|
return ["onchain", "lightning"]
|
|
|
|
else:
|
|
|
|
return ["lightning"]
|