aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar buttercat1791 <mjjurkoic@gmail.com>2024-06-15 10:04:52 -0500
committerLibravatar buttercat1791 <mjjurkoic@gmail.com>2024-06-15 10:04:52 -0500
commit5dd823a942afb9461426d2cbba9952526a22a755 (patch)
tree9815d09dba800aee3665ac42b526f965caa5b4b6
parentb3854adaa0af1db6f8f7eecebf63229108608bb4 (diff)
Ping signer before asking it to sign an event
-rw-r--r--include/signer/noscrypt_signer.hpp8
-rw-r--r--src/signer/noscrypt_signer.cpp35
2 files changed, 35 insertions, 8 deletions
diff --git a/include/signer/noscrypt_signer.hpp b/include/signer/noscrypt_signer.hpp
index de8aab4..79359f3 100644
--- a/include/signer/noscrypt_signer.hpp
+++ b/include/signer/noscrypt_signer.hpp
@@ -90,10 +90,10 @@ private:
std::string _generateSignerRequestId();
/**
- * @brief Builds a wrapper event for JRPC-like signer messages.
+ * @brief Builds and signs a wrapper event for JRPC-like signer messages.
* @param jrpc The JRPC-like payload that will comprise the event content, as specified by
* NIP-46.
- * @returns A shared pointer to the wrapper event.
+ * @returns A shared pointer to the signed wrapper event.
*/
std::shared_ptr<nostr::data::Event> _wrapSignerMessage(nlohmann::json jrpc);
@@ -108,9 +108,9 @@ private:
/**
* @brief Pings the remote signer to confirm that it is online and available.
- * @returns `true` if the signer is available, `false` otherwise.
+ * @returns A promise that will be set to `true` if the signer is available, `false` otherwise.
*/
- bool _pingSigner();
+ std::promise<bool> _pingSigner();
#pragma region Cryptography
diff --git a/src/signer/noscrypt_signer.cpp b/src/signer/noscrypt_signer.cpp
index 4d39393..00e8b5e 100644
--- a/src/signer/noscrypt_signer.cpp
+++ b/src/signer/noscrypt_signer.cpp
@@ -116,8 +116,8 @@ shared_ptr<promise<bool>> NoscryptSigner::sign(shared_ptr<Event> event)
{
auto signingPromise = make_shared<promise<bool>>();
- bool signerAvailable = this->_pingSigner();
- if (!signerAvailable)
+ auto signerAvailable = this->_pingSigner().get_future();
+ if (signerAvailable.get() == false)
{
PLOG_ERROR << "Ping to the remote signer failed - the remote signer may be unavailable.";
signingPromise->set_value(false);
@@ -408,20 +408,47 @@ string NoscryptSigner::_unwrapSignerMessage(shared_ptr<Event> event)
return decryptedContent;
};
-bool NoscryptSigner::_pingSigner()
+promise<bool> NoscryptSigner::_pingSigner()
{
+ promise<bool> pingPromise;
+
+ // Generate a ping message and wrap it for the signer.
nlohmann::json jrpc =
{
{ "id", this->_generateSignerRequestId() },
{ "method", "ping" },
{ "params", nlohmann::json::array() }
};
-
auto messageEvent = this->_wrapSignerMessage(jrpc);
+ // Generate a filter to receive the response.
+ auto pingFilter = make_shared<Filters>();
+ pingFilter->authors.push_back(this->_remotePublicKey);
+ pingFilter->kinds.push_back(this->_nostrConnectKind);
+ pingFilter->tags["p"] = { this->_localPublicKey };
+ pingFilter->since = time(nullptr);
+
this->_nostrService->publishEvent(messageEvent);
// TODO: Handle the relay response.
+ this->_nostrService->queryRelays(
+ pingFilter,
+ [this, &pingPromise](const string&, shared_ptr<Event> pongEvent)
+ {
+ //
+ string pongMessage = this->_unwrapSignerMessage(pongEvent);
+ pingPromise.set_value(pongMessage == "pong");
+ },
+ [&pingPromise](const string&)
+ {
+ pingPromise.set_value(false);
+ },
+ [&pingPromise](const string&, const string&)
+ {
+ pingPromise.set_value(false);
+ });
+
+ return pingPromise;
};
#pragma region Cryptography