Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adapt NostrSDK to vcpkg build system #4

Merged
merged 5 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@ project(NostrSDK VERSION 0.0.1)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../out/${CMAKE_BUILD_TYPE}/bin/)
set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../out/${CMAKE_BUILD_TYPE}/lib/)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/../out/${CMAKE_BUILD_TYPE}/lib/)

if(DEFINED ENV{WORKSPACE})
list(APPEND CMAKE_PREFIX_PATH $ENV{WORKSPACE}/env/uuid_v4)
else()
list(APPEND CMAKE_PREFIX_PATH ../env/uuid_v4)
endif()

# Build the project.
set(INCLUDE_DIR ./include)
set(CLIENT_INCLUDE_DIR ./include/client)
include_directories(${INCLUDE_DIR})
include_directories(${CLIENT_INCLUDE_DIR})

set(HEADERS
${INCLUDE_DIR}/nostr.hpp
${CLIENT_INCLUDE_DIR}/web_socket_client.hpp
Expand All @@ -27,21 +35,20 @@ set(SOURCES
${CLIENT_SOURCE_DIR}/websocketpp_client.cpp
)

find_package(Boost REQUIRED COMPONENTS random system)
find_package(nlohmann_json CONFIG REQUIRED)
find_package(uuid_v4 REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(plog CONFIG REQUIRED)
find_package(websocketpp CONFIG REQUIRED)

add_library(NostrSDK ${SOURCES} ${HEADERS})
target_link_libraries(NostrSDK PRIVATE
Boost::random
Boost::system
Comment on lines -38 to -39
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stating for the record that we may need to re-add Boost at a later date. I believe websocketpp uses it as a dependency, but we don't test the websocketpp usage directly, as of yet.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, websocketpp uses it as a dependency but vcpkg downloads the required parts of boost (already compiled) for websocketpp

But if we directly use boost, vcpkg will have to download all of it fr and compile it from source which took me around 2hrs, and all that just for generating UUIDs... it's a lot of overhead in my opinion.

If we need it in the future, we'll cross that bridge when we get to it.

nlohmann_json::nlohmann_json
OpenSSL::SSL
OpenSSL::Crypto
plog::plog
websocketpp::websocketpp
uuid_v4::uuid_v4
)
set_target_properties(NostrSDK PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS YES)

Expand Down Expand Up @@ -73,6 +80,7 @@ target_link_libraries(NostrSDKTest PRIVATE
NostrSDK
plog::plog
websocketpp::websocketpp
uuid_v4::uuid_v4
)
set_target_properties(NostrSDKTest PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS YES)

Expand Down
13 changes: 13 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 2,
"configurePresets": [
{
"name": "default",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
}
}
]
}
22 changes: 5 additions & 17 deletions src/event.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
#include <ctime>
#include <openssl/evp.h>
#include <iomanip>
#include <sstream>
#include <string>
#include <nlohmann/json.hpp>
#include <sha.h>
#include <openssl/sha.h>

#include "nostr.hpp"

using nlohmann::json;
using std::hex;
using std::invalid_argument;
using std::make_shared;
using std::ostringstream;
using std::setw;
using std::setfill;
using std::shared_ptr;
using std::string;
using std::stringstream;
using std::time;

namespace nostr
using namespace nlohmann;
using namespace std;

namespace nostr
{
string Event::serialize()
{
Expand Down
10 changes: 2 additions & 8 deletions src/filters.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
#include <ctime>
#include <sstream>
#include <string>
#include <nlohmann/json.hpp>

#include "nostr.hpp"
using namespace nlohmann;
using namespace std;

using nlohmann::json;
using std::invalid_argument;
using std::stringstream;
using std::string;
using std::time;

namespace nostr
{
Expand Down
42 changes: 11 additions & 31 deletions src/nostr_service.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,18 @@
#include <algorithm>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>

#include <nlohmann/json.hpp>
#include <plog/Init.h>
#include <plog/Log.h>
#include <websocketpp/client.hpp>
#include <websocketpp/config/asio_client.hpp>
#include <uuid_v4/uuid_v4.h>

#include "nostr.hpp"
#include "client/web_socket_client.hpp"

using boost::uuids::random_generator;
using boost::uuids::to_string;
using boost::uuids::uuid;
using nlohmann::json;
using std::async;
using std::exception;
using std::find_if;
using std::function;
using std::future;
using std::invalid_argument;
using std::lock_guard;
using std::make_shared;
using std::make_tuple;
using std::move;
using std::mutex;
using std::out_of_range;
using std::promise;
using std::shared_ptr;
using std::string;
using std::thread;
using std::tuple;
using std::unique_ptr;
using std::vector;
using namespace nlohmann;
using namespace std;
using namespace UUIDv4;

namespace nostr
{
Expand All @@ -49,7 +28,7 @@ NostrService::NostrService(
shared_ptr<ISigner> signer,
RelayList relays)
: _defaultRelays(relays), _client(client), _signer(signer)
{
{
plog::init(plog::debug, appender.get());
client->start();
};
Expand Down Expand Up @@ -178,7 +157,7 @@ tuple<RelayList, RelayList> NostrService::publishEvent(shared_ptr<Event> event)
}
});
});

if (!success)
{
PLOG_WARNING << "Failed to send event to relay: " << relay;
Expand Down Expand Up @@ -288,7 +267,7 @@ string NostrService::queryRelays(
for (const string relay : this->_activeRelays)
{
this->_subscriptions[relay].push_back(subscriptionId);

promise<tuple<string, bool>> requestPromise;
requestFutures.push_back(move(requestPromise.get_future()));
future<tuple<string, bool>> requestFuture = async(
Expand Down Expand Up @@ -517,8 +496,9 @@ void NostrService::disconnect(string relay)

string NostrService::generateSubscriptionId()
{
uuid uuid = random_generator()();
return to_string(uuid);
UUIDGenerator<std::mt19937_64> uuidGenerator;
UUID uuid = uuidGenerator.getUUID();
return uuid.bytes();
};

string NostrService::generateCloseRequest(string subscriptionId)
Expand Down
14 changes: 14 additions & 0 deletions vcpkg-configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"default-registry": {
"kind": "git",
"baseline": "582a4de14bef91df217f4f49624cf5b2b04bd7ca",
"repository": "https://github.com/microsoft/vcpkg"
},
"registries": [
{
"kind": "artifact",
"location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip",
"name": "microsoft"
}
]
}
8 changes: 8 additions & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies": [
"nlohmann-json",
"openssl",
"plog",
"websocketpp"
]
}