From 42d5101316f6e8e2c713fd91c5acc492667ba6a5 Mon Sep 17 00:00:00 2001 From: Doug Hoyte Date: Thu, 20 Jul 2023 18:03:40 -0400 Subject: [PATCH] Warn when NIP-11 pubkey is in incorrect format (#52) --- src/apps/relay/RelayWebsocket.cpp | 15 +++++++++------ src/apps/relay/cmd_relay.cpp | 15 +++++++++++++++ strfry.conf | 4 ++-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/apps/relay/RelayWebsocket.cpp b/src/apps/relay/RelayWebsocket.cpp index 9214f6f..2d6116f 100644 --- a/src/apps/relay/RelayWebsocket.cpp +++ b/src/apps/relay/RelayWebsocket.cpp @@ -52,15 +52,18 @@ void RelayServer::runWebsocket(ThreadPool::Thread &thr) { auto getServerInfoHttpResponse = [&supportedNips, ver = uint64_t(0), rendered = std::string("")]() mutable { if (ver != cfg().version()) { - rendered = preGenerateHttpResponse("application/json", tao::json::to_string(tao::json::value({ - { "name", cfg().relay__info__name }, - { "description", cfg().relay__info__description }, - { "pubkey", cfg().relay__info__pubkey }, - { "contact", cfg().relay__info__contact }, + tao::json::value nip11 = tao::json::value({ { "supported_nips", supportedNips }, { "software", "git+https://github.com/hoytech/strfry.git" }, { "version", APP_GIT_VERSION }, - }))); + }); + + if (cfg().relay__info__name.size()) nip11["name"] = cfg().relay__info__name; + if (cfg().relay__info__description.size()) nip11["description"] = cfg().relay__info__description; + if (cfg().relay__info__contact.size()) nip11["contact"] = cfg().relay__info__contact; + if (cfg().relay__info__pubkey.size()) nip11["pubkey"] = cfg().relay__info__pubkey; + + rendered = preGenerateHttpResponse("application/json", tao::json::to_string(nip11)); ver = cfg().version(); } diff --git a/src/apps/relay/cmd_relay.cpp b/src/apps/relay/cmd_relay.cpp index 0ba5c0c..dc69215 100644 --- a/src/apps/relay/cmd_relay.cpp +++ b/src/apps/relay/cmd_relay.cpp @@ -5,6 +5,18 @@ +static void checkConfig() { + if (cfg().relay__info__pubkey.size()) { + try { + auto p = from_hex(cfg().relay__info__pubkey); + if (p.size() != 32) throw herr("bad size"); + } catch (std::exception &e) { + LW << "Your relay.info.pubkey is incorrectly formatted. It should be 64 hex digits."; + } + } +} + + void cmd_relay(const std::vector &subArgs) { RelayServer s; s.run(); @@ -53,12 +65,15 @@ void RelayServer::run() { // Monitor for config file reloads + checkConfig(); + auto configFileChangeWatcher = hoytech::file_change_monitor(configFile); configFileChangeWatcher.setDebounce(100); configFileChangeWatcher.run([&](){ loadConfig(configFile); + checkConfig(); }); diff --git a/strfry.conf b/strfry.conf index 4b04eed..4a39f41 100644 --- a/strfry.conf +++ b/strfry.conf @@ -57,10 +57,10 @@ relay { description = "This is a strfry instance." # NIP-11: Administrative nostr pubkey, for contact purposes - pubkey = "unset" + pubkey = "" # NIP-11: Alternative administrative contact (email, website, etc) - contact = "unset" + contact = "" } # Maximum accepted incoming websocket frame size (should be larger than max event) (restart required)