forked from hoytech/strfry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents.h
120 lines (85 loc) · 3.48 KB
/
events.h
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#pragma once
#include <secp256k1_schnorrsig.h>
#include "golpe.h"
#include "Decompressor.h"
inline bool isReplaceableKind(uint64_t kind) {
return (
kind == 0 ||
kind == 3 ||
kind == 41 ||
(kind >= 10'000 && kind < 20'000)
);
}
inline bool isParamReplaceableKind(uint64_t kind) {
return (
(kind >= 30'000 && kind < 40'000)
);
}
inline bool isEphemeralKind(uint64_t kind) {
return (
(kind >= 20'000 && kind < 30'000)
);
}
std::string nostrJsonToFlat(const tao::json::value &v);
std::string nostrHash(const tao::json::value &origJson);
bool verifySig(secp256k1_context* ctx, std::string_view sig, std::string_view hash, std::string_view pubkey);
void verifyNostrEvent(secp256k1_context *secpCtx, const NostrIndex::Event *flat, const tao::json::value &origJson);
void verifyNostrEventJsonSize(std::string_view jsonStr);
void verifyEventTimestamp(const NostrIndex::Event *flat);
void parseAndVerifyEvent(const tao::json::value &origJson, secp256k1_context *secpCtx, bool verifyMsg, bool verifyTime, std::string &flatStr, std::string &jsonStr);
// Does not do verification!
inline const NostrIndex::Event *flatStrToFlatEvent(std::string_view flatStr) {
return flatbuffers::GetRoot<NostrIndex::Event>(flatStr.data());
}
std::optional<defaultDb::environment::View_Event> lookupEventById(lmdb::txn &txn, std::string_view id);
defaultDb::environment::View_Event lookupEventByLevId(lmdb::txn &txn, uint64_t levId); // throws if can't find
uint64_t getMostRecentLevId(lmdb::txn &txn);
std::string_view decodeEventPayload(lmdb::txn &txn, Decompressor &decomp, std::string_view raw, uint32_t *outDictId, size_t *outCompressedSize);
std::string_view getEventJson(lmdb::txn &txn, Decompressor &decomp, uint64_t levId);
std::string_view getEventJson(lmdb::txn &txn, Decompressor &decomp, uint64_t levId, std::string_view eventPayload);
enum class EventSourceType {
None = 0,
IP4 = 1,
IP6 = 2,
Import = 3,
Stream = 4,
Sync = 5,
};
inline std::string eventSourceTypeToStr(EventSourceType t) {
if (t == EventSourceType::IP4) return "IP4";
else if (t == EventSourceType::IP6) return "IP6";
else if (t == EventSourceType::Import) return "Import";
else if (t == EventSourceType::Stream) return "Stream";
else if (t == EventSourceType::Sync) return "Sync";
else return "?";
}
enum class EventWriteStatus {
Pending,
Written,
Duplicate,
Replaced,
Deleted,
};
struct EventToWrite {
std::string flatStr;
std::string jsonStr;
uint64_t receivedAt;
EventSourceType sourceType;
std::string sourceInfo;
void *userData = nullptr;
EventWriteStatus status = EventWriteStatus::Pending;
uint64_t levId = 0;
EventToWrite() {}
EventToWrite(std::string flatStr, std::string jsonStr, uint64_t receivedAt, EventSourceType sourceType, std::string sourceInfo, void *userData = nullptr) : flatStr(flatStr), jsonStr(jsonStr), receivedAt(receivedAt), sourceType(sourceType), sourceInfo(sourceInfo), userData(userData) {
}
std::string_view id() {
const NostrIndex::Event *flat = flatbuffers::GetRoot<NostrIndex::Event>(flatStr.data());
return sv(flat->id());
}
uint64_t createdAt() {
const NostrIndex::Event *flat = flatbuffers::GetRoot<NostrIndex::Event>(flatStr.data());
return flat->created_at();
}
};
void writeEvents(lmdb::txn &txn, std::vector<EventToWrite> &evs, uint64_t logLevel = 1);
bool deleteEvent(lmdb::txn &txn, uint64_t levId);