aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Michael Jurkoic <mjjurkoic@gmail.com>2024-03-18 21:28:19 -0500
committerLibravatar Michael Jurkoic <mjjurkoic@gmail.com>2024-03-18 21:28:19 -0500
commit6dde23e6c66e846c64d49c5258f0dbf44e3d0374 (patch)
tree7602ceb68ddb012ff13217095a528b0e44e44d96
parenta437d34b29d2a65113f3f67ffa1a6c3391b7e836 (diff)
Declare a signer interface
-rw-r--r--include/nostr.hpp16
-rw-r--r--src/event.cpp6
-rw-r--r--src/nostr_service.cpp2
3 files changed, 18 insertions, 6 deletions
diff --git a/include/nostr.hpp b/include/nostr.hpp
index 645090a..3e60d7b 100644
--- a/include/nostr.hpp
+++ b/include/nostr.hpp
@@ -18,7 +18,9 @@ namespace nostr
typedef std::vector<std::string> RelayList;
typedef std::unordered_map<std::string, std::vector<std::string>> TagMap;
-// TODO: Add null checking to seralization and deserialization methods.
+class ISigner;
+class NostrService;
+
/**
* @brief A Nostr event.
* @remark All data transmitted over the Nostr protocol is encoded in JSON blobs. This struct
@@ -40,7 +42,7 @@ struct Event
* @returns A stringified JSON object representing the event.
* @throws `std::invalid_argument` if the event object is invalid.
*/
- std::string serialize();
+ std::string serialize(std::shared_ptr<ISigner> signer);
/**
* @brief Deserializes the event from a JSON string.
@@ -103,6 +105,7 @@ private:
class NostrService
{
+// TODO: Setup signer in the constructor.
public:
NostrService(
std::shared_ptr<plog::IAppender> appender,
@@ -213,6 +216,9 @@ private:
///< The WebSocket client used to communicate with relays.
std::shared_ptr<client::IWebSocketClient> _client;
+ ///< The signer used to sign Nostr events.
+ std::shared_ptr<ISigner> _signer;
+
///< A mutex to protect the instance properties.
std::mutex _propertyMutex;
///< The default set of Nostr relays to which the service will attempt to connect.
@@ -287,4 +293,10 @@ private:
*/
void onEvent(std::string subscriptionId, Event event);
};
+
+class ISigner
+{
+public:
+ virtual std::string generateSignature(std::shared_ptr<Event> event) = 0;
+};
} // namespace nostr
diff --git a/src/event.cpp b/src/event.cpp
index 4ba87d2..a24a594 100644
--- a/src/event.cpp
+++ b/src/event.cpp
@@ -13,13 +13,14 @@ using std::hex;
using std::invalid_argument;
using std::setw;
using std::setfill;
+using std::shared_ptr;
using std::string;
using std::stringstream;
using std::time;
namespace nostr
{
-string Event::serialize()
+string Event::serialize(shared_ptr<ISigner> signer)
{
try
{
@@ -40,8 +41,7 @@ string Event::serialize()
};
j["id"] = this->generateId(j.dump());
-
- // TODO: Reach out to a signer to sign the event, then set the signature.
+ j["sig"] = signer->generateSignature(shared_ptr<Event>(this));
json jarr = json::array({ "EVENT", j });
diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp
index 0409a0d..7efc11e 100644
--- a/src/nostr_service.cpp
+++ b/src/nostr_service.cpp
@@ -129,7 +129,7 @@ tuple<RelayList, RelayList> NostrService::publishEvent(Event event)
for (const string& relay : this->_activeRelays)
{
future<tuple<string, bool>> publishFuture = async([this, &relay, &event]() {
- return this->_client->send(event.serialize(), relay);
+ return this->_client->send(event.serialize(this->_signer), relay);
});
publishFutures.push_back(move(publishFuture));