Radix Relay
Hybrid mesh communications with Signal Protocol encryption
Loading...
Searching...
No Matches
protocol.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <nlohmann/json.hpp>
5#include <optional>
6#include <span>
7#include <stdexcept>
8#include <string>
9#include <vector>
10
12
14inline constexpr auto bundle_announcement_minimum_version = "0.4.0";
15
21enum class kind : std::uint16_t {
23 text_note = 1,
25 contact_list = 3,
26 encrypted_dm = 4,
27 reaction = 7,
28
30 bundle_announcement = 30078,
31
32 encrypted_message = 40001,
34 session_request = 40003,
35 node_status = 40004,
36};
37
44{
45 std::string id;
46 std::string pubkey;
47 std::uint64_t created_at{};
48 enum kind kind {};
49 std::vector<std::vector<std::string>> tags;
50 std::string content;
51 std::string sig;
52
59 static auto deserialize(std::span<const std::byte> bytes) -> std::optional<event_data>;
60
67 static auto deserialize(const std::string &json) -> std::optional<event_data>;
68
74 [[nodiscard]] auto serialize() const -> std::vector<std::byte>;
75
85 [[nodiscard]] static auto create_identity_announcement(const std::string &sender_pubkey,
86 std::uint64_t timestamp,
87 const std::string &signal_fingerprint,
88 const std::string &capabilities = "mesh,nostr") -> event_data;
89
98 [[nodiscard]] static auto create_bundle_announcement(const std::string &sender_pubkey,
99 std::uint64_t timestamp,
100 const std::string &bundle_hex) -> event_data;
101
110 [[nodiscard]] static auto create_encrypted_message(std::uint64_t timestamp,
111 const std::string &recipient_pubkey,
112 const std::string &encrypted_payload) -> event_data;
113
123 [[nodiscard]] static auto create_session_request(const std::string &sender_pubkey,
124 std::uint64_t timestamp,
125 const std::string &recipient_pubkey,
126 const std::string &prekey_bundle) -> event_data;
127
133 [[nodiscard]] auto is_radix_message() const -> bool;
134
140 [[nodiscard]] auto get_kind() const -> std::optional<enum kind>;
141};
142
148struct ok
149{
150 std::string event_id;
151 bool accepted{};
152 std::string message;
153
160 static auto deserialize(const std::string &json) -> std::optional<ok>;
161};
162
168struct eose
169{
170 std::string subscription_id;
171
178 static auto deserialize(const std::string &json) -> std::optional<eose>;
179};
180
186struct req
187{
188 std::string subscription_id;
189 nlohmann::json filters;
190
196 [[nodiscard]] auto serialize() const -> std::string;
197
204 static auto deserialize(const std::string &json) -> std::optional<req>;
205};
206
212struct event
213{
214 std::string subscription_id;
216
223 static auto from_event_data(const event_data &evt) -> event;
224
230 [[nodiscard]] auto serialize() const -> std::string;
231
238 static auto deserialize(const std::string &json) -> std::optional<event>;
239};
240
242constexpr std::size_t max_subscription_id_length = 64;
243
250inline auto validate_subscription_id(const std::string &subscription_id) -> void
251{
252 if (subscription_id.empty()) { throw std::invalid_argument("Subscription ID cannot be empty"); }
253 if (subscription_id.length() > max_subscription_id_length) {
254 throw std::invalid_argument("Subscription ID exceeds maximum length of 64 characters");
255 }
256}
257
258}// namespace radix_relay::nostr::protocol
constexpr auto bundle_announcement_minimum_version
Minimum protocol version required for bundle announcements.
Definition protocol.hpp:14
constexpr std::size_t max_subscription_id_length
Maximum allowed subscription ID length.
Definition protocol.hpp:242
kind
Nostr event kind identifiers.
Definition protocol.hpp:21
@ identity_announcement
Radix: Node identity announcement.
@ node_status
Radix: Node status update.
@ bundle_announcement
Radix: Signal Protocol prekey bundle.
@ encrypted_dm
Encrypted direct message (NIP-04)
@ profile_metadata
User profile metadata (NIP-01)
@ contact_list
Contact list (NIP-02)
@ session_request
Radix: Session establishment request.
@ recommend_relay
Relay recommendation (NIP-01)
@ reaction
Reaction to an event (NIP-25)
@ encrypted_message
Radix: Encrypted message via Signal Protocol.
@ text_note
Text note/post (NIP-01)
@ parameterized_replaceable_start
Start of parameterized replaceable range.
End of Stored Events marker.
Definition protocol.hpp:169
std::string subscription_id
Subscription this EOSE applies to.
Definition protocol.hpp:170
static auto deserialize(const std::string &json) -> std::optional< eose >
Deserializes EOSE message from JSON.
Nostr event data structure.
Definition protocol.hpp:44
static auto deserialize(const std::string &json) -> std::optional< event_data >
Deserializes event data from JSON string.
static auto create_identity_announcement(const std::string &sender_pubkey, std::uint64_t timestamp, const std::string &signal_fingerprint, const std::string &capabilities="mesh,nostr") -> event_data
Creates an identity announcement event.
std::string sig
Schnorr signature (64-byte hex)
Definition protocol.hpp:51
std::string id
Event ID (32-byte hex hash)
Definition protocol.hpp:45
static auto deserialize(std::span< const std::byte > bytes) -> std::optional< event_data >
Deserializes event data from byte array.
auto get_kind() const -> std::optional< enum kind >
Returns the event kind.
static auto create_session_request(const std::string &sender_pubkey, std::uint64_t timestamp, const std::string &recipient_pubkey, const std::string &prekey_bundle) -> event_data
Creates a session establishment request event.
auto serialize() const -> std::vector< std::byte >
Serializes event data to byte array.
static auto create_encrypted_message(std::uint64_t timestamp, const std::string &recipient_pubkey, const std::string &encrypted_payload) -> event_data
Creates an encrypted message event.
std::string content
Event content.
Definition protocol.hpp:50
static auto create_bundle_announcement(const std::string &sender_pubkey, std::uint64_t timestamp, const std::string &bundle_hex) -> event_data
Creates a bundle announcement event.
std::string pubkey
Public key of event creator (32-byte hex)
Definition protocol.hpp:46
auto is_radix_message() const -> bool
Checks if event is a Radix-specific message type.
std::uint64_t created_at
Unix timestamp.
Definition protocol.hpp:47
std::vector< std::vector< std::string > > tags
Event tags (arbitrary string arrays)
Definition protocol.hpp:49
Nostr EVENT message wrapper.
Definition protocol.hpp:213
event_data data
The event itself.
Definition protocol.hpp:215
std::string subscription_id
Subscription this event matches.
Definition protocol.hpp:214
auto serialize() const -> std::string
Serializes event to JSON string.
static auto from_event_data(const event_data &evt) -> event
Creates an event message from event_data.
Nostr OK response message.
Definition protocol.hpp:149
static auto deserialize(const std::string &json) -> std::optional< ok >
Deserializes OK message from JSON.
std::string message
Human-readable status message.
Definition protocol.hpp:152
std::string event_id
ID of the event this responds to.
Definition protocol.hpp:150
Nostr REQ subscription request.
Definition protocol.hpp:187
auto serialize() const -> std::string
Serializes REQ to JSON string.
std::string subscription_id
Unique identifier for this subscription.
Definition protocol.hpp:188
nlohmann::json filters
Filter criteria (NIP-01 format)
Definition protocol.hpp:189