Skip to content
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
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ set(ENABLE_ONIONREQ OFF)
# as it is not part of the archive. We actually don't care about it on session-desktop
set(SUBMODULE_CHECK OFF)

file(GLOB SOURCE_FILES src/*.cpp src/groups/*.cpp src/multi_encrypt/*.cpp)
file(GLOB SOURCE_FILES src/*.cpp src/groups/*.cpp )

add_subdirectory(libsession-util)

Expand All @@ -47,7 +47,7 @@ if(MSVC)
endif()

add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC} "include/" "node_modules/node-addon-api" "../../node_modules/node-addon-api" "node_modules/node-api-headers/include" "../../node_modules/node-api-headers/include")
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC} "include/" "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/node-addon-api" "${CMAKE_CURRENT_SOURCE_DIR}/node_modules" "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/node-api-headers/include")

set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_JS_LIB} ${LIBSESSION_STATIC_BUNDLE_LIBS})
Expand Down
6 changes: 5 additions & 1 deletion include/groups/meta_group.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <napi.h>

#include <vector>

#include "session/config/groups/info.hpp"
Expand Down Expand Up @@ -35,7 +36,10 @@ class MetaGroup {
shared_ptr<config::groups::Keys> keys,
std::vector<unsigned char> edGroupPubKey,
std::optional<std::vector<unsigned char>> edGroupSecKey) :
info{info}, members{members}, keys{keys}, edGroupPubKey{oxenc::to_hex(edGroupPubKey.begin(), edGroupPubKey.end())} {
info{info},
members{members},
keys{keys},
edGroupPubKey{oxenc::to_hex(edGroupPubKey.begin(), edGroupPubKey.end())} {

if (edGroupSecKey.has_value()) {
this->edGroupSecKey = oxenc::to_hex(edGroupSecKey->begin(), edGroupSecKey->end());
Expand Down
3 changes: 1 addition & 2 deletions include/meta/meta_base_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <napi.h>

#include <optional>
#include <span>
#include <vector>

#include "../base_config.hpp"
Expand All @@ -13,7 +12,7 @@ namespace session::nodeapi {
class MetaBaseWrapper {

public:
explicit MetaBaseWrapper(){};
explicit MetaBaseWrapper() {};

virtual ~MetaBaseWrapper() = default;

Expand Down
17 changes: 6 additions & 11 deletions include/multi_encrypt/multi_encrypt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ namespace session::nodeapi {

namespace log = oxen::log;

auto cat = log::Cat("multi_encrypt");

inline std::vector<unsigned char> extractPlaintext(
const Napi::Object& obj, const std::string identifier) {

Expand Down Expand Up @@ -718,9 +716,8 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {

auto contentOrEnvelope = extractContentOrEnvelope(
obj, "decryptForCommunity.obj.contentOrEnvelope");
decrypted.push_back(
session::decode_for_community(
contentOrEnvelope, nowMs, proBackendPubkeyHex));
decrypted.push_back(session::decode_for_community(
contentOrEnvelope, nowMs, proBackendPubkeyHex));
decryptedServerIds.push_back(serverId);

} catch (const std::exception& e) {
Expand Down Expand Up @@ -819,9 +816,8 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {

auto envelopePayload =
extractEnvelopePayload(obj, "decryptFor1o1.obj.envelopePayload");
decrypted.push_back(
session::decode_envelope(
keys, envelopePayload, nowMs, proBackendPubkeyHex));
decrypted.push_back(session::decode_envelope(
keys, envelopePayload, nowMs, proBackendPubkeyHex));
decryptedMessageHashes.push_back(messageHash);
} catch (const std::exception& e) {
log::warning(
Expand Down Expand Up @@ -919,9 +915,8 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {

auto envelopePayload =
extractEnvelopePayload(obj, "decryptForGroup.obj.envelopePayload");
decrypted.push_back(
session::decode_envelope(
keys, envelopePayload, nowMs, proBackendPubkeyHex));
decrypted.push_back(session::decode_envelope(
keys, envelopePayload, nowMs, proBackendPubkeyHex));
decryptedMessageHashes.push_back(messageHash);
} catch (const std::exception& e) {
log::warning(
Expand Down
37 changes: 23 additions & 14 deletions include/pro/pro.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,18 @@
#include <oxenc/base64.h>
#include <oxenc/hex.h>

#include <algorithm>
#include <span>
#include <vector>

#include "../meta/meta_base_wrapper.hpp"
#include "../utilities.hpp"
#include "oxen/log.hpp"
#include "pro/types.hpp"
#include "session/attachments.hpp"
#include "session/config/user_profile.hpp"
#include "session/random.hpp"
#include "meta/meta_base_wrapper.hpp"
#include "session/session_protocol.hpp"

namespace session::nodeapi {
namespace log = oxen::log;

class ProWrapper : public Napi::ObjectWrap<ProWrapper> {

public:
ProWrapper(const Napi::CallbackInfo& info) : Napi::ObjectWrap<ProWrapper>{info} {
throw std::invalid_argument("ProWrapper is static and doesn't need to be constructed");
Expand Down Expand Up @@ -49,6 +46,7 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {

assertInfoLength(info, 1);
assertIsObject(info[0]);
auto env = info.Env();

auto first = info[0].As<Napi::Object>();

Expand All @@ -67,20 +65,31 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
proFeatures.push_back(item);
}

SESSION_PROTOCOL_PRO_EXTRA_FEATURES flags;
SESSION_PROTOCOL_PRO_EXTRA_FEATURES flags = 0;
for (std::string& feature : proFeatures) {
if (feature == "10K_CHARACTER_LIMIT") {
flags |= SESSION_PROTOCOL_PRO_FEATURES_10K_CHARACTER_LIMIT;
} else if (feature == "PRO_BADGE") {
flags |= SESSION_PROTOCOL_PRO_FEATURES_PRO_BADGE;
// Note: 10K_CHARACTER_LIMIT cannot be requested by the caller
if (feature == "PRO_BADGE") {
flags |= SESSION_PROTOCOL_PRO_EXTRA_FEATURES_PRO_BADGE;
} else if (feature == "ANIMATED_AVATAR") {
flags |= SESSION_PROTOCOL_PRO_FEATURES_ANIMATED_AVATAR;
flags |= SESSION_PROTOCOL_PRO_EXTRA_FEATURES_ANIMATED_AVATAR;
}
}
assertIsString(first.Get("utf16"), "proFeaturesForMessage.utf16");
std::u16string utf16 = first.Get("utf16").As<Napi::String>().Utf16Value();
return session::pro_features_for_utf16((utf16.data()), utf16.length(), flags);
auto pro_features_msg =
session::pro_features_for_utf16((utf16.data()), utf16.length(), flags);

auto obj = Napi::Object::New(env);

obj["success"] = toJs(env, pro_features_msg.success);
obj["error"] =
pro_features_msg.error.size() ? toJs(env, pro_features_msg.error) : env.Null();
obj["codepointCount"] = toJs(env, pro_features_msg.codepoint_count);
obj["proFeatures"] = proFeaturesToJs(env, pro_features_msg.features);

return obj;
});
};
};

}; // namespace session::nodeapi
56 changes: 4 additions & 52 deletions include/pro/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@
#include <oxenc/base64.h>
#include <oxenc/hex.h>

#include <algorithm>
#include <span>
#include <vector>

#include "../utilities.hpp"
#include "oxen/log.hpp"
#include "session/attachments.hpp"
#include "session/config/user_profile.hpp"
#include "session/multi_encrypt.hpp"
#include "session/random.hpp"
#include "session/config/pro.hpp"
#include "session/session_protocol.hpp"
#include "utilities.hpp"

namespace session::nodeapi {

Expand Down Expand Up @@ -72,32 +65,6 @@ struct toJs_impl<session::DecodedEnvelope> {
}
};

template <>
struct toJs_impl<SESSION_PROTOCOL_PRO_FEATURES> {
Napi::Object operator()(const Napi::Env& env, const SESSION_PROTOCOL_PRO_FEATURES bitset) {
Napi::Array arr = Napi::Array::New(env);
uint32_t index = 0;

if (bitset == SESSION_PROTOCOL_PRO_FEATURES_NIL) {
return arr;
}

if (bitset & (SESSION_PROTOCOL_PRO_FEATURES_10K_CHARACTER_LIMIT)) {
arr[index] = Napi::String::New(env, "10K_CHARACTER_LIMIT");
index++;
}
if (bitset & SESSION_PROTOCOL_PRO_FEATURES_PRO_BADGE) {
arr[index++] = Napi::String::New(env, "PRO_BADGE");
index++;
}
if (bitset & SESSION_PROTOCOL_PRO_FEATURES_ANIMATED_AVATAR) {
arr[index++] = Napi::String::New(env, "ANIMATED_AVATAR");
index++;
}
return arr;
}
};

template <>
struct toJs_impl<session::DecodedPro> {
Napi::Object operator()(const Napi::Env& env, const session::DecodedPro decoded_pro) {
Expand All @@ -110,22 +77,7 @@ struct toJs_impl<session::DecodedPro> {
: decoded_pro.status == ProStatus::Valid ? "Valid"
: "Expired");
obj["proProof"] = toJs(env, decoded_pro.proof);
obj["proFeatures"] = toJs(env, decoded_pro.features);

return obj;
}
};

template <>
struct toJs_impl<session::ProFeaturesForMsg> {
Napi::Object operator()(
const Napi::Env& env, const session::ProFeaturesForMsg pro_features_msg) {
auto obj = Napi::Object::New(env);

obj["success"] = toJs(env, pro_features_msg.success);
obj["error"] = toJs(env, pro_features_msg.error);
obj["codepointCount"] = toJs(env, pro_features_msg.codepoint_count);
obj["proFeatures"] = toJs(env, pro_features_msg.features);
obj["proFeatures"] = proFeaturesToJs(env, decoded_pro.features);

return obj;
}
Expand Down
1 change: 0 additions & 1 deletion include/profile_pic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace session::nodeapi {


// Constructs a profile_pic from a Napi::Value which must be either Null or an Object; if an
// Object then it *must* contain "url" (string or null) and "key" (uint8array of size 32 or
// null) keys; if either is empty or Null then you get back an empty (i.e. clearing)
Expand Down
34 changes: 22 additions & 12 deletions include/utilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
#include <unordered_set>
#include <vector>

#include "oxen/log.hpp"
#include "oxenc/hex.h"
#include "session/config/namespaces.hpp"
#include "session/config/profile_pic.hpp"
#include "session/session_protocol.hpp"
#include "session/types.h"
#include "session/types.hpp"
#include "utilities.hpp"
Expand All @@ -21,6 +23,8 @@ namespace session::nodeapi {

using namespace std::literals;

inline auto cat = oxen::log::Cat("nodeapi");

static void checkOrThrow(bool condition, const char* msg) {
if (!condition)
throw std::invalid_argument{msg};
Expand Down Expand Up @@ -119,6 +123,11 @@ struct toJs_impl<session::config::Namespace> {
}
};

template <>
struct toJs_impl<size_t> {
auto operator()(const Napi::Env& env, size_t b) const { return Napi::Number::New(env, (b)); }
};

template <typename T>
struct toJs_impl<T, std::enable_if_t<std::is_arithmetic_v<T>>> {
auto operator()(const Napi::Env& env, T n) const { return Napi::Number::New(env, n); }
Expand Down Expand Up @@ -294,7 +303,7 @@ auto wrapResult(const Napi::Env& env, Call&& call) {
if constexpr (std::is_base_of_v<Napi::Value, Result>)
return res;
else
return toJs(env, std::move(res));
return toJs<Result>(env, std::move(res));
}
} catch (const std::exception& e) {
throw Napi::Error::New(env, e.what());
Expand Down Expand Up @@ -362,6 +371,8 @@ Napi::Object decrypt_result_to_JS(

confirm_pushed_entry_t confirm_pushed_entry_from_JS(const Napi::Env& env, const Napi::Object& obj);

Napi::Object proFeaturesToJs(const Napi::Env& env, const SESSION_PROTOCOL_PRO_FEATURES bitset);

std::span<const uint8_t> from_hex_to_span(std::string_view x);

template <std::size_t N>
Expand All @@ -371,11 +382,10 @@ template <std::size_t N>
std::array<uint8_t, N> from_hex_to_array(std::string x) {
std::string as_hex = oxenc::from_hex(x);
if (as_hex.size() != N) {
throw std::invalid_argument(
std::format(
"from_hex_to_array: Decoded hex size mismatch: expected {}, got {}",
N,
as_hex.size()));
throw std::invalid_argument(std::format(
"from_hex_to_array: Decoded hex size mismatch: expected {}, got {}",
N,
as_hex.size()));
}

std::array<uint8_t, N> result;
Expand All @@ -386,21 +396,21 @@ std::array<uint8_t, N> from_hex_to_array(std::string x) {
std::vector<unsigned char> from_hex_to_vector(std::string_view x);

std::span<const uint8_t> from_base64_to_span(std::string_view x);
std::vector<unsigned char> from_base64_to_vector(std::string_view x) ;

std::vector<unsigned char> from_base64_to_vector(std::string_view x);

// Concept to match containers with a size() method
template <typename T>
concept HasSize = requires(T t) {
{t.size()}->std::convertible_to<size_t>;
{
t.size()
} -> std::convertible_to<size_t>;
};

template <HasSize T>
void assert_length(const T& x, size_t n, std::string_view base_identifier) {
if (x.size() != n) {
throw std::invalid_argument(
std::format(
"assert_length: expected {}, got {} for {}", n, x.size(), base_identifier));
throw std::invalid_argument(std::format(
"assert_length: expected {}, got {} for {}", n, x.size(), base_identifier));
}
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"update_version": "sh update_version.sh",
"clean": "rimraf .cache build",
"lint:cpp": "cppcheck --std=c++20 -j8 --quiet src libsession-util/src",
"lint": "find src include -name '*.cpp' -o -name '*.hpp' | xargs clang-format-19 -i",
"install": "cmake-js build --runtime=electron --runtime-version=34.2.0 --CDSUBMODULE_CHECK=OFF --CDLOCAL_MIRROR=https://oxen.rocks/deps --CDENABLE_ONIONREQ=OFF --CDWITH_TESTS=OFF",
"prepare_release": "sh prepare_release.sh",
"dedup": "npx --yes yarn-deduplicate yarn.lock"
Expand Down
Loading