feat: nostr event layer (builders + sign/publish service)

Implements ADR-0001. kinds.py pins the allocations; events.py has pure
builders (30402 listing, 30078 encrypted reservation, 31923 blocked-range
calendar, 22001 availability response); service.py signs/encrypts via
resolve_signer (spirekeeper hybrid pattern — no nsec at rest) and sketches
publish + inbound subscription. Relay plumbing marked TODO(relay).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019VUQCfdqiLSsFS2jcGnaFD
This commit is contained in:
Padreug 2026-07-19 00:16:39 +02:00
commit 900b682986
4 changed files with 264 additions and 0 deletions

119
nostr/events.py Normal file
View file

@ -0,0 +1,119 @@
"""Builders that turn Chatelet rows into unsigned Nostr events.
These are pure functions: they return unsigned event dicts (kind, tags,
content) with no pubkey/id/sig. Signing + NIP-44 encryption happen in
service.py via lnbits.core.signers.resolve_signer, so no nsec is handled
here and the same code works with a LocalSigner today or a NIP-46 bunker
later. See docs/event-flow.md for who publishes what, when.
"""
import json
from ..models import AvailabilityResult, Booking, Room
from .kinds import (
KIND_AVAILABILITY_RESPONSE,
KIND_CALENDAR_EVENT,
KIND_LISTING,
KIND_RESERVATION,
)
def build_listing_event(room: Room) -> dict:
"""NIP-99 kind:30402 classified listing for a room. Public, signed by
the operator's identity. `d` == room.id so re-publishing replaces."""
tags = [
["d", room.id],
["title", room.title],
["price", str(room.price_amount), room.price_currency, room.price_frequency],
["status", "active"],
]
if room.location:
tags.append(["location", room.location])
if room.geohash:
tags.append(["g", room.geohash])
for url in room.images:
tags.append(["image", url])
for amenity in room.amenities:
tags.append(["t", amenity])
return {
"kind": KIND_LISTING,
"content": room.description,
"tags": tags,
}
def build_reservation_event(booking: Booking, guest_pubkey: str) -> dict:
"""NIP-78 kind:30078 reservation object — the guest's durable, private
copy of their booking. `content` MUST be NIP-44 encrypted to the guest
by the caller (service.py) before signing; here we return the plaintext
payload so the signer can seal it. `d` == booking.id, addressable so
status transitions replace in place.
amount_sat is copied verbatim from the booking (canonical value) do
not recompute it here.
"""
payload = {
"booking_id": booking.id,
"room_id": booking.room_id,
"check_in": booking.check_in,
"check_out": booking.check_out,
"nights": booking.nights,
"num_guests": booking.num_guests,
"status": booking.status.value,
"amount_sat": booking.amount_sat,
"deposit_sat": booking.deposit_sat,
"currency": booking.currency,
"price_fiat": booking.price_fiat,
}
return {
"kind": KIND_RESERVATION,
# placeholder — service.py replaces with NIP-44(payload) for guest
"content": json.dumps(payload),
"tags": [
["d", booking.id],
["p", guest_pubkey],
],
"_plaintext": payload, # consumed + stripped by service.py before signing
}
def build_block_calendar_event(
room: Room, start_date: str, end_date: str, block_id: str
) -> dict:
"""NIP-52 kind:31923 marking a range as unavailable on the room's public
calendar. Deliberately carries NO guest PII just 'these dates are
taken'. Only published when settings.publish_availability is true."""
return {
"kind": KIND_CALENDAR_EVENT,
"content": "",
"tags": [
["d", f"{room.id}:{block_id}"],
["title", f"{room.title} — unavailable"],
["start", start_date],
["end", end_date],
["a", f"{KIND_LISTING}:{{operator_pubkey}}:{room.id}"], # link to listing
],
}
def build_availability_response(
result: AvailabilityResult, requester_pubkey: str
) -> dict:
"""Aiolabs kind:22001 (ephemeral) reply to an availability query. Content
MUST be NIP-44 encrypted to the requester by service.py."""
payload = {
"room_id": result.room_id,
"check_in": result.check_in,
"check_out": result.check_out,
"available": result.available,
"nights": result.nights,
"quote_sat": result.quote_sat,
"quote_fiat": result.quote_fiat,
"currency": result.currency,
}
return {
"kind": KIND_AVAILABILITY_RESPONSE,
"content": json.dumps(payload),
"tags": [["p", requester_pubkey]],
"_plaintext": payload,
}