| | 1 | | using NostrSure.Domain.Entities; |
| | 2 | | using NostrSure.Infrastructure.Client.Abstractions; |
| | 3 | | using NostrSure.Infrastructure.Client.Messages; |
| | 4 | | using NostrSure.Infrastructure.Serialization; |
| | 5 | | using System.Text.Json; |
| | 6 | |
|
| | 7 | | namespace NostrSure.Infrastructure.Client.Implementation; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// JSON serializer for Nostr protocol messages |
| | 11 | | /// </summary> |
| | 12 | | public class JsonMessageSerializer : IMessageSerializer |
| | 13 | | { |
| | 14 | | private readonly JsonSerializerOptions _options; |
| | 15 | |
|
| 43 | 16 | | public JsonMessageSerializer() |
| 43 | 17 | | { |
| 43 | 18 | | _options = new JsonSerializerOptions |
| 43 | 19 | | { |
| 43 | 20 | | PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, |
| 43 | 21 | | Converters = { new NostrEventJsonConverter() } |
| 43 | 22 | | }; |
| 43 | 23 | | } |
| | 24 | |
|
| | 25 | | public string Serialize(object[] message) |
| 15 | 26 | | { |
| 15 | 27 | | if (message == null || message.Length == 0) |
| 4 | 28 | | throw new ArgumentException("Message cannot be null or empty", nameof(message)); |
| | 29 | |
|
| | 30 | | // All outbound messages are valid JSON arrays (requirement R2) since we serialize object arrays |
| 11 | 31 | | return JsonSerializer.Serialize(message, _options); |
| 11 | 32 | | } |
| | 33 | |
|
| | 34 | | public NostrMessage Deserialize(string json) |
| 19 | 35 | | { |
| 19 | 36 | | if (string.IsNullOrWhiteSpace(json)) |
| 2 | 37 | | throw new ArgumentException("JSON cannot be null or empty", nameof(json)); |
| | 38 | |
|
| | 39 | | try |
| 17 | 40 | | { |
| 17 | 41 | | using var document = JsonDocument.Parse(json); |
| 16 | 42 | | var root = document.RootElement; |
| | 43 | |
|
| 16 | 44 | | if (root.ValueKind != JsonValueKind.Array) |
| 1 | 45 | | throw new ArgumentException("Nostr messages must be JSON arrays"); |
| | 46 | |
|
| 15 | 47 | | var arrayLength = root.GetArrayLength(); |
| 15 | 48 | | if (arrayLength == 0) |
| 1 | 49 | | throw new ArgumentException("Nostr message array cannot be empty"); |
| | 50 | |
|
| 14 | 51 | | var messageType = root[0].GetString(); |
| 14 | 52 | | if (string.IsNullOrEmpty(messageType)) |
| 0 | 53 | | throw new ArgumentException("Message type cannot be null or empty"); |
| | 54 | |
|
| 14 | 55 | | return messageType switch |
| 14 | 56 | | { |
| 2 | 57 | | "EVENT" when arrayLength == 3 => ParseRelayEventMessage(root), |
| 6 | 58 | | "EOSE" when arrayLength == 2 => new EoseMessage(root[1].GetString()!), |
| 6 | 59 | | "NOTICE" when arrayLength == 2 => new NoticeMessage(root[1].GetString()!), |
| 4 | 60 | | "CLOSED" when arrayLength >= 2 => new ClosedMessage( |
| 2 | 61 | | root[1].GetString()!, |
| 2 | 62 | | arrayLength > 2 ? root[2].GetString()! : ""), |
| 8 | 63 | | "OK" when arrayLength >= 3 => new OkMessage( |
| 4 | 64 | | root[1].GetString()!, |
| 4 | 65 | | root[2].GetBoolean(), |
| 4 | 66 | | arrayLength > 3 ? root[3].GetString()! : ""), |
| 1 | 67 | | _ => throw new ArgumentException($"Unknown or malformed message type: {messageType}") |
| 14 | 68 | | }; |
| | 69 | | } |
| 1 | 70 | | catch (JsonException ex) |
| 1 | 71 | | { |
| 1 | 72 | | throw new ArgumentException($"Invalid JSON: {ex.Message}", ex); |
| | 73 | | } |
| 13 | 74 | | } |
| | 75 | |
|
| | 76 | | // Removed IsValidJsonArray method; validation is handled in Serialize. |
| | 77 | |
|
| | 78 | | private RelayEventMessage ParseRelayEventMessage(JsonElement root) |
| 1 | 79 | | { |
| 1 | 80 | | var subscriptionId = root[1].GetString()!; |
| 1 | 81 | | var eventJson = root[2].GetRawText(); |
| | 82 | |
|
| 1 | 83 | | var nostrEvent = JsonSerializer.Deserialize<NostrEvent>(eventJson, _options); |
| 1 | 84 | | if (nostrEvent == null) |
| 0 | 85 | | throw new ArgumentException("Failed to parse NostrEvent from relay EVENT message"); |
| | 86 | |
|
| 1 | 87 | | return new RelayEventMessage(subscriptionId, nostrEvent); |
| 1 | 88 | | } |
| | 89 | | } |