aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/nostr.hpp9
-rw-r--r--src/event.cpp36
2 files changed, 38 insertions, 7 deletions
diff --git a/include/nostr.hpp b/include/nostr.hpp
index ce25446..fa407ef 100644
--- a/include/nostr.hpp
+++ b/include/nostr.hpp
@@ -54,6 +54,15 @@ private:
* @remark The `createdAt` field defaults to the present if it is not already set.
*/
void validate();
+
+ /**
+ * @brief Generates an ID for the event.
+ * @param serializedData The serialized JSON string of all of the event data except the ID and
+ * the signature.
+ * @return A valid Nostr event ID.
+ * @remark The ID is a 32-bytes lowercase hex-encoded sha256 of the serialized event data.
+ */
+ std::string generateId(std::string serializedData) const;
};
/**
diff --git a/src/event.cpp b/src/event.cpp
index 6a179fa..a95657e 100644
--- a/src/event.cpp
+++ b/src/event.cpp
@@ -1,12 +1,19 @@
#include <ctime>
+#include <iomanip>
+#include <sstream>
#include <string>
#include <nlohmann/json.hpp>
+#include <openssl/sha.h>
#include "nostr.hpp"
using nlohmann::json;
+using std::hex;
using std::invalid_argument;
+using std::setw;
+using std::setfill;
using std::string;
+using std::stringstream;
using std::time;
namespace nostr
@@ -23,7 +30,6 @@ string Event::serialize()
}
json j = {
- {"id", this->id},
{"pubkey", this->pubkey},
{"created_at", this->createdAt},
{"kind", this->kind},
@@ -31,6 +37,11 @@ string Event::serialize()
{"content", this->content},
{"sig", this->sig}
};
+
+ j["id"] = this->generateId(j.dump());
+
+ // TODO: Reach out to a signer to sign the event, then set the signature.
+
return j.dump();
};
@@ -48,12 +59,6 @@ void Event::deserialize(string jsonString)
void Event::validate()
{
- bool hasId = this->id.length() > 0;
- if (!hasId)
- {
- throw std::invalid_argument("Event::validate: The event id is required.");
- }
-
bool hasPubkey = this->pubkey.length() > 0;
if (!hasPubkey)
{
@@ -78,4 +83,21 @@ void Event::validate()
throw std::invalid_argument("Event::validate: The event must be signed.");
}
};
+
+string Event::generateId(string serializedData) const
+{
+ unsigned char hash[SHA256_DIGEST_LENGTH];
+ SHA256_CTX sha256;
+ SHA256_Init(&sha256);
+ SHA256_Update(&sha256, serializedData.c_str(), serializedData.length());
+ SHA256_Final(hash, &sha256);
+
+ stringstream ss;
+ for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
+ {
+ ss << hex << setw(2) << setfill('0') << (int)hash[i];
+ }
+
+ return ss.str();
+};
} // namespace nostr