From b3854adaa0af1db6f8f7eecebf63229108608bb4 Mon Sep 17 00:00:00 2001 From: buttercat1791 Date: Sat, 15 Jun 2024 00:44:56 -0500 Subject: Fill out noscrypt signer sign method --- include/signer/noscrypt_signer.hpp | 52 ++++++++++++++++++++++++++++++++++++-- include/signer/signer.hpp | 6 +++++ 2 files changed, 56 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/signer/noscrypt_signer.hpp b/include/signer/noscrypt_signer.hpp index 7201c12..de8aab4 100644 --- a/include/signer/noscrypt_signer.hpp +++ b/include/signer/noscrypt_signer.hpp @@ -31,6 +31,10 @@ public: std::shared_ptr> sign(std::shared_ptr event) override; private: + const int _nostrConnectKind = 24133; // Kind 24133 is reserved for NIP-46 events. + + Encryption _nostrConnectEncryption; + std::shared_ptr _noscryptContext; std::shared_ptr _nostrService; @@ -85,6 +89,29 @@ private: */ std::string _generateSignerRequestId(); + /** + * @brief Builds 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. + */ + std::shared_ptr _wrapSignerMessage(nlohmann::json jrpc); + + /** + * @brief Unwraps the JRPC-like payload from a signer message, typically one received from the + * remote signer in response to a request. + * @param event An event containing a NIP-46 message payload. + * @returns The unwrapped payload. The returned object will be empty if no valid payload could + * be extracted from the given event. + */ + std::string _unwrapSignerMessage(std::shared_ptr event); + + /** + * @brief Pings the remote signer to confirm that it is online and available. + * @returns `true` if the signer is available, `false` otherwise. + */ + bool _pingSigner(); + #pragma region Cryptography /** @@ -93,15 +120,36 @@ private: */ void _reseedRandomNumberGenerator(uint32_t bufferSize = 32); - std::string _encryptNip04(); + /** + * @brief Encrypts a string according to the standard specified in NIP-04. + * @param input The string to be encrypted. + * @return The resulting encrypted string, or an empty string if the input could not be + * encrypted. + */ + std::string _encryptNip04(const std::string input); + + /** + * @brief Decrypts a NIP-04 encrypted string. + * @param input The string to be decrypted. + * @return The decrypted string, or an empty string if the input could not be decrypted. + */ + std::string _decryptNip04(const std::string input); /** * @brief Encrypts a string according to the standard specified in NIP-44. * @param input The string to be encrypted. - * @return The encrypted input. + * @return The resulting encrypted string, or an empty string if the input could not be + * encrypted. */ std::string _encryptNip44(const std::string input); // TODO: Return or set HMAC? + /** + * @brief Decrypts a NIP-44 encrypted string. + * @param input The string to be decrypted. + * @return The decrypted string, or an empty string if the input could not be decrypted. + */ + std::string _decryptNip44(const std::string input); + #pragma endregion #pragma region Logging diff --git a/include/signer/signer.hpp b/include/signer/signer.hpp index 0faaff8..10e54c6 100644 --- a/include/signer/signer.hpp +++ b/include/signer/signer.hpp @@ -11,6 +11,12 @@ namespace nostr { namespace signer { +enum class Encryption +{ + NIP04, + NIP44 +}; + /** * @brief An interface for Nostr event signing that implements NIP-46. */ -- cgit