-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathBaseMessageDecoder.java
More file actions
97 lines (85 loc) · 3.63 KB
/
BaseMessageDecoder.java
File metadata and controls
97 lines (85 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package nostr.event.json.codec;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import nostr.base.IDecoder;
import nostr.event.BaseMessage;
import nostr.event.message.CanonicalAuthenticationMessage;
import nostr.event.message.CloseMessage;
import nostr.event.message.EoseMessage;
import nostr.event.message.EventMessage;
import nostr.event.message.NoticeMessage;
import nostr.event.message.OkMessage;
import nostr.event.message.RelayAuthenticationMessage;
import nostr.event.message.ReqMessage;
import java.util.Map;
/**
* @author eric
*/
@NoArgsConstructor
public class BaseMessageDecoder<T extends BaseMessage> implements IDecoder<T> {
public static final int COMMAND_INDEX = 0;
public static final int ARG_INDEX = 1;
/**
* Decodes a Nostr protocol message from its JSON representation.
*
* @param jsonString JSON representation of the message
* @return decoded message
* @throws nostr.event.json.codec.EventEncodingException if decoding fails
*/
@Override
public T decode(@NonNull String jsonString) throws EventEncodingException {
ValidNostrJsonStructure validNostrJsonStructure = validateProperlyFormedJson(jsonString);
String command = validNostrJsonStructure.getCommand();
Object subscriptionId = validNostrJsonStructure.getSubscriptionId();
return switch (command) {
// client <-> relay messages
case "AUTH" -> {
if (subscriptionId instanceof Map<?, ?> map) {
yield CanonicalAuthenticationMessage.decode((Map<String, Object>) map);
} else {
yield RelayAuthenticationMessage.decode(subscriptionId);
}
}
case "EVENT" -> EventMessage.decode(jsonString);
// missing client <-> relay handlers
// case "COUNT" -> CountMessage.decode(subscriptionId);
// client -> relay messages
case "CLOSE" -> CloseMessage.decode(subscriptionId);
case "REQ" -> ReqMessage.decode(subscriptionId, jsonString);
// relay -> client handlers
case "EOSE" -> EoseMessage.decode(subscriptionId);
case "NOTICE" -> NoticeMessage.decode(subscriptionId);
case "OK" -> OkMessage.decode(jsonString);
// missing relay -> client handlers
// case "CLOSED" -> Closed.message.decode(subscriptionId);
default ->
throw new IllegalArgumentException(
String.format("Invalid JSON command [%s] in JSON string [%s] ", command, jsonString));
};
}
private ValidNostrJsonStructure validateProperlyFormedJson(@NonNull String jsonString)
throws EventEncodingException {
try {
JsonNode root = I_DECODER_MAPPER_BLACKBIRD.readTree(jsonString);
JsonNode commandNode = root.get(COMMAND_INDEX);
JsonNode argNode = root.get(ARG_INDEX);
if (commandNode == null || argNode == null) {
String missingFields =
(commandNode == null ? "commandNode" : "")
+ (commandNode == null && argNode == null ? " and " : "")
+ (argNode == null ? "argNode" : "");
throw new IllegalArgumentException(
String.format(
"Invalid JSON structure: Missing %s in JSON string [%s]",
missingFields, jsonString));
}
return new ValidNostrJsonStructure(commandNode.asText(), argNode.asText());
} catch (JsonProcessingException e) {
throw new EventEncodingException("Failed to decode message", e);
}
}
private record ValidNostrJsonStructure(
@NonNull String getCommand, @NonNull Object getSubscriptionId) {}
}