Skip to content

Commit 4e95c55

Browse files
authored
Merge pull request #25 from Bilb/feat/multi-part
feat: add multi part msg support
2 parents 655451d + 8f570f3 commit 4e95c55

19 files changed

+135
-96
lines changed

libsession-util

Submodule libsession-util updated 69 files

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"main": "index.js",
33
"name": "libsession_util_nodejs",
44
"description": "Wrappers for the Session Util Library",
5-
"version": "0.4.34",
5+
"version": "0.5.0",
66
"license": "GPL-3.0",
77
"author": {
88
"name": "Oxen Project",

src/addon.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111

1212
Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
1313
using namespace session::nodeapi;
14+
15+
session::add_logger([env](std::string_view msg) {
16+
std::string toLog = std::string("libsession-util: ") + std::string(msg) + "\n";
17+
18+
Napi::Function consoleLog =
19+
env.Global().Get("console").As<Napi::Object>().Get("log").As<Napi::Function>();
20+
consoleLog.Call({Napi::String::New(env, toLog)});
21+
});
22+
23+
// session::logger_set_level_default(session::LogLevel::debug);
24+
1425
ConstantsWrapper::Init(env, exports);
1526

1627
// Group wrappers init

src/base_config.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ Napi::Value ConfigBaseImpl::needsPush(const Napi::CallbackInfo& info) {
1515
return wrapResult(info, [&] { return get_config<ConfigBase>().needs_push(); });
1616
}
1717

18-
Napi::Value ConfigBaseImpl::currentHashes(const Napi::CallbackInfo& info) {
19-
return wrapResult(info, [&] { return (get_config<ConfigBase>().current_hashes()); });
18+
Napi::Value ConfigBaseImpl::activeHashes(const Napi::CallbackInfo& info) {
19+
return wrapResult(info, [&] {
20+
std::unordered_set<std::string> hashes = get_config<ConfigBase>().active_hashes();
21+
std::vector<std::string> hashesVec(hashes.begin(), hashes.end());
22+
return hashesVec;
23+
});
2024
}
2125

2226
Napi::Value ConfigBaseImpl::push(const Napi::CallbackInfo& info) {
@@ -44,21 +48,21 @@ Napi::Value ConfigBaseImpl::makeDump(const Napi::CallbackInfo& info) {
4448
}
4549

4650
void ConfigBaseImpl::confirmPushed(const Napi::CallbackInfo& info) {
47-
return wrapResult(info, [&]() {
48-
assertInfoLength(info, 2);
49-
assertIsNumber(info[0], "confirmPushed");
50-
assertIsString(info[1]);
51+
return wrapExceptions(info, [&]() {
52+
assertInfoLength(info, 1);
53+
assertIsObject(info[0]);
54+
auto obj = info[0].As<Napi::Object>();
55+
auto confirmed_pushed_entry = confirm_pushed_entry_from_JS(info.Env(), obj);
5156

5257
get_config<ConfigBase>().confirm_pushed(
53-
toCppInteger(info[0], "confirmPushed", false),
54-
toCppString(info[1], "confirmPushed"));
58+
std::get<0>(confirmed_pushed_entry), std::get<1>(confirmed_pushed_entry));
5559
});
5660
}
5761

5862
Napi::Value ConfigBaseImpl::merge(const Napi::CallbackInfo& info) {
5963
return wrapResult(info, [&]() {
6064
assertInfoLength(info, 1);
61-
assertIsArray(info[0]);
65+
assertIsArray(info[0], "ConfigBaseImpl::merge");
6266
Napi::Array asArray = info[0].As<Napi::Array>();
6367

6468
std::vector<std::pair<std::string, std::vector<unsigned char>>> conf_strs;
@@ -75,8 +79,9 @@ Napi::Value ConfigBaseImpl::merge(const Napi::CallbackInfo& info) {
7579
toCppString(itemObject.Get("hash"), "base.merge"),
7680
toCppBuffer(itemObject.Get("data"), "base.merge"));
7781
}
78-
79-
return get_config<ConfigBase>().merge(conf_strs);
82+
std::unordered_set<std::string> merged = get_config<ConfigBase>().merge(conf_strs);
83+
std::vector<std::string> mergedVec(merged.begin(), merged.end());
84+
return mergedVec;
8085
});
8186
}
8287

src/base_config.hpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include <cassert>
77
#include <memory>
8+
#include <oxen/log.hpp>
89
#include <span>
910
#include <stdexcept>
1011
#include <unordered_set>
@@ -33,7 +34,7 @@ class ConfigBaseImpl {
3334
// These are exposed as read-only accessors rather than methods:
3435
Napi::Value needsDump(const Napi::CallbackInfo& info);
3536
Napi::Value needsPush(const Napi::CallbackInfo& info);
36-
Napi::Value currentHashes(const Napi::CallbackInfo& info);
37+
Napi::Value activeHashes(const Napi::CallbackInfo& info);
3738

3839
Napi::Value push(const Napi::CallbackInfo& info);
3940
Napi::Value dump(const Napi::CallbackInfo& info);
@@ -53,7 +54,7 @@ class ConfigBaseImpl {
5354

5455
properties.push_back(T::InstanceMethod("needsDump", &T::needsDump));
5556
properties.push_back(T::InstanceMethod("needsPush", &T::needsPush));
56-
properties.push_back(T::InstanceMethod("currentHashes", &T::currentHashes));
57+
properties.push_back(T::InstanceMethod("activeHashes", &T::activeHashes));
5758
properties.push_back(T::InstanceMethod("push", &T::push));
5859
properties.push_back(T::InstanceMethod("dump", &T::dump));
5960
properties.push_back(T::InstanceMethod("makeDump", &T::makeDump));
@@ -101,24 +102,7 @@ class ConfigBaseImpl {
101102
if (!second.IsEmpty() && !second.IsNull() && !second.IsUndefined())
102103
dump = toCppBuffer(second, class_name + ".new");
103104

104-
// return std::make_shared<Config>(secretKey, dump);
105-
std::shared_ptr<Config> config = std::make_shared<Config>(secretKey, dump);
106-
107-
Napi::Env env = info.Env();
108-
109-
session::add_logger([env, class_name](auto msg) {
110-
std::string toLog =
111-
"libsession-util:" + std::string(class_name) + ": " + std::string(msg) + "\n";
112-
113-
Napi::Function consoleLog = env.Global()
114-
.Get("console")
115-
.As<Napi::Object>()
116-
.Get("log")
117-
.As<Napi::Function>();
118-
consoleLog.Call({Napi::String::New(env, toLog)});
119-
});
120-
121-
return config;
105+
return std::make_shared<Config>(secretKey, dump);
122106
});
123107
}
124108

src/groups/meta_group_wrapper.cpp

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void MetaGroupWrapper::Init(Napi::Env env, Napi::Object exports) {
140140
InstanceMethod("keysNeedsRekey", &MetaGroupWrapper::keysNeedsRekey),
141141
InstanceMethod("keyRekey", &MetaGroupWrapper::keyRekey),
142142
InstanceMethod("keyGetAll", &MetaGroupWrapper::keyGetAll),
143-
InstanceMethod("currentHashes", &MetaGroupWrapper::currentHashes),
143+
InstanceMethod("activeHashes", &MetaGroupWrapper::activeHashes),
144144
InstanceMethod("loadKeyMessage", &MetaGroupWrapper::loadKeyMessage),
145145
InstanceMethod("keyGetCurrentGen", &MetaGroupWrapper::keyGetCurrentGen),
146146
InstanceMethod("encryptMessages", &MetaGroupWrapper::encryptMessages),
@@ -244,35 +244,21 @@ void MetaGroupWrapper::metaConfirmPushed(const Napi::CallbackInfo& info) {
244244
auto groupMember = obj.Get("groupMember");
245245

246246
if (!groupInfo.IsNull() && !groupInfo.IsUndefined()) {
247-
assertIsArray(groupInfo);
248-
auto groupInfoArr = groupInfo.As<Napi::Array>();
249-
if (groupInfoArr.Length() != 2) {
250-
throw std::invalid_argument("groupInfoArr length was not 2");
251-
}
247+
assertIsObject(groupInfo);
248+
auto groupInfoObj = groupInfo.As<Napi::Object>();
249+
auto groupInfoConfirmed = confirm_pushed_entry_from_JS(info.Env(), groupInfoObj);
252250

253-
auto seqno = maybeNonemptyInt(
254-
groupInfoArr.Get("0"), "MetaGroupWrapper::metaConfirmPushed groupInfo seqno");
255-
auto hash = maybeNonemptyString(
256-
groupInfoArr.Get("1"), "MetaGroupWrapper::metaConfirmPushed groupInfo hash");
257-
if (seqno && hash)
258-
this->meta_group->info->confirm_pushed(*seqno, *hash);
251+
this->meta_group->info->confirm_pushed(
252+
std::get<0>(groupInfoConfirmed), std::get<1>(groupInfoConfirmed));
259253
}
260254

261255
if (!groupMember.IsNull() && !groupMember.IsUndefined()) {
262-
assertIsArray(groupMember);
263-
auto groupMemberArr = groupMember.As<Napi::Array>();
264-
if (groupMemberArr.Length() != 2) {
265-
throw std::invalid_argument("groupMemberArr length was not 2");
266-
}
256+
assertIsObject(groupMember);
257+
auto groupMemberObj = groupMember.As<Napi::Object>();
258+
auto groupMemberConfirmed = confirm_pushed_entry_from_JS(info.Env(), groupMemberObj);
267259

268-
auto seqno = maybeNonemptyInt(
269-
groupMemberArr.Get("0"),
270-
"MetaGroupWrapper::metaConfirmPushed groupMemberArr seqno");
271-
auto hash = maybeNonemptyString(
272-
groupMemberArr.Get("1"),
273-
"MetaGroupWrapper::metaConfirmPushed groupMemberArr hash");
274-
if (seqno && hash)
275-
this->meta_group->members->confirm_pushed(*seqno, *hash);
260+
this->meta_group->members->confirm_pushed(
261+
std::get<0>(groupMemberConfirmed), std::get<1>(groupMemberConfirmed));
276262
}
277263
});
278264
};
@@ -283,17 +269,17 @@ Napi::Value MetaGroupWrapper::metaMerge(const Napi::CallbackInfo& info) {
283269
auto arg = info[0];
284270
assertIsObject(arg);
285271
auto obj = arg.As<Napi::Object>();
286-
287272
auto groupInfo = obj.Get("groupInfo");
288273
auto groupMember = obj.Get("groupMember");
289274
auto groupKeys = obj.Get("groupKeys");
290275

291276
auto count_merged = 0;
292277

278+
293279
// Note: we need to process keys first as they might allow us the incoming info+members
294280
// details
295281
if (!groupKeys.IsNull() && !groupKeys.IsUndefined()) {
296-
assertIsArray(groupKeys);
282+
assertIsArray(groupKeys, "metaMerge groupKeys");
297283
auto asArr = groupKeys.As<Napi::Array>();
298284

299285
for (uint32_t i = 0; i < asArr.Length(); i++) {
@@ -318,13 +304,13 @@ Napi::Value MetaGroupWrapper::metaMerge(const Napi::CallbackInfo& info) {
318304
timestamp_ms,
319305
*(this->meta_group->info),
320306
*(this->meta_group->members));
321-
count_merged++; // load_key_message doesn't necessarely merge something as not
307+
count_merged++; // load_key_message doesn't necessarily merge something as not
322308
// all keys are for us.
323309
}
324310
}
325311

326312
if (!groupInfo.IsNull() && !groupInfo.IsUndefined()) {
327-
assertIsArray(groupInfo);
313+
assertIsArray(groupInfo, "metaMerge groupInfo");
328314
auto asArr = groupInfo.As<Napi::Array>();
329315

330316
std::vector<std::pair<std::string, std::vector<unsigned char>>> conf_strs;
@@ -349,9 +335,8 @@ Napi::Value MetaGroupWrapper::metaMerge(const Napi::CallbackInfo& info) {
349335
count_merged += info_merged.size();
350336
}
351337
}
352-
353338
if (!groupMember.IsNull() && !groupMember.IsUndefined()) {
354-
assertIsArray(groupMember);
339+
assertIsArray(groupMember, "metaMerge groupMember");
355340
auto asArr = groupMember.As<Napi::Array>();
356341

357342
std::vector<std::pair<std::string, std::vector<unsigned char>>> conf_strs;
@@ -376,7 +361,6 @@ Napi::Value MetaGroupWrapper::metaMerge(const Napi::CallbackInfo& info) {
376361
count_merged += member_merged.size();
377362
}
378363
}
379-
380364
if (this->meta_group->keys->needs_rekey()) {
381365
this->meta_group->keys->rekey(*(this->meta_group->info), *(this->meta_group->members));
382366
}
@@ -693,7 +677,7 @@ void MetaGroupWrapper::membersMarkPendingRemoval(const Napi::CallbackInfo& info)
693677
auto toUpdateJSValue = info[0];
694678
auto withMessageJSValue = info[1];
695679

696-
assertIsArray(toUpdateJSValue);
680+
assertIsArray(toUpdateJSValue, "membersMarkPendingRemoval");
697681
assertIsBoolean(withMessageJSValue);
698682
bool withMessages = toCppBoolean(withMessageJSValue, "membersMarkPendingRemoval");
699683

@@ -714,7 +698,7 @@ Napi::Value MetaGroupWrapper::memberEraseAndRekey(const Napi::CallbackInfo& info
714698
assertInfoLength(info, 1);
715699
auto toRemoveJSValue = info[0];
716700

717-
assertIsArray(toRemoveJSValue);
701+
assertIsArray(toRemoveJSValue, "memberEraseAndRekey");
718702

719703
auto toRemoveJS = toRemoveJSValue.As<Napi::Array>();
720704
auto rekeyed = false;
@@ -771,23 +755,24 @@ Napi::Value MetaGroupWrapper::keyGetCurrentGen(const Napi::CallbackInfo& info) {
771755
});
772756
}
773757

774-
Napi::Value MetaGroupWrapper::currentHashes(const Napi::CallbackInfo& info) {
758+
Napi::Value MetaGroupWrapper::activeHashes(const Napi::CallbackInfo& info) {
775759
return wrapResult(info, [&] {
776-
auto keysHashes = meta_group->keys->current_hashes();
777-
auto infoHashes = meta_group->info->current_hashes();
778-
auto memberHashes = meta_group->members->current_hashes();
760+
auto keysHashes = meta_group->keys->active_hashes();
761+
auto infoHashes = meta_group->info->active_hashes();
762+
auto memberHashes = meta_group->members->active_hashes();
779763
std::vector<std::string> merged;
780764
std::copy(std::begin(keysHashes), std::end(keysHashes), std::back_inserter(merged));
781765
std::copy(std::begin(infoHashes), std::end(infoHashes), std::back_inserter(merged));
782766
std::copy(std::begin(memberHashes), std::end(memberHashes), std::back_inserter(merged));
767+
783768
return merged;
784769
});
785770
}
786771

787772
Napi::Value MetaGroupWrapper::encryptMessages(const Napi::CallbackInfo& info) {
788773
return wrapResult(info, [&] {
789774
assertInfoLength(info, 1);
790-
assertIsArray(info[0]);
775+
assertIsArray(info[0], "encryptMessages");
791776

792777
auto plaintextsJS = info[0].As<Napi::Array>();
793778
uint32_t arrayLength = plaintextsJS.Length();
@@ -880,7 +865,7 @@ Napi::Value MetaGroupWrapper::generateSupplementKeys(const Napi::CallbackInfo& i
880865
return wrapResult(info, [&] {
881866
assertInfoLength(info, 1);
882867
auto membersJSValue = info[0];
883-
assertIsArray(membersJSValue);
868+
assertIsArray(membersJSValue, "generateSupplementKeys");
884869

885870
auto membersJS = membersJSValue.As<Napi::Array>();
886871
uint32_t arrayLength = membersJS.Length();

src/groups/meta_group_wrapper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class MetaGroupWrapper : public Napi::ObjectWrap<MetaGroupWrapper> {
8383
Napi::Value keyGetAll(const Napi::CallbackInfo& info);
8484
Napi::Value loadKeyMessage(const Napi::CallbackInfo& info);
8585
Napi::Value keyGetCurrentGen(const Napi::CallbackInfo& info);
86-
Napi::Value currentHashes(const Napi::CallbackInfo& info);
86+
Napi::Value activeHashes(const Napi::CallbackInfo& info);
8787
Napi::Value encryptMessages(const Napi::CallbackInfo& info);
8888
Napi::Value decryptMessage(const Napi::CallbackInfo& info);
8989
Napi::Value makeSwarmSubAccount(const Napi::CallbackInfo& info);

src/multi_encrypt/multi_encrypt.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
5858

5959
// handle the messages conversion
6060
auto messagesJSValue = obj.Get("messages");
61-
assertIsArray(messagesJSValue);
61+
assertIsArray(messagesJSValue, "multiEncrypt");
6262
auto messagesJS = messagesJSValue.As<Napi::Array>();
6363
std::vector<std::vector<unsigned char>> messages;
6464
messages.reserve(messagesJS.Length());
@@ -71,7 +71,7 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
7171

7272
// handle the recipients conversion
7373
auto recipientsJSValue = obj.Get("recipients");
74-
assertIsArray(recipientsJSValue);
74+
assertIsArray(recipientsJSValue, "multiEncrypt");
7575
auto recipientsJS = recipientsJSValue.As<Napi::Array>();
7676
std::vector<std::vector<unsigned char>> recipients;
7777
recipients.reserve(recipientsJS.Length());

src/user_groups_config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ void UserGroupsWrapper::setLegacyGroup(const Napi::CallbackInfo& info) {
213213
true)};
214214

215215
auto membersJSValue = obj.Get("members");
216-
assertIsArray(membersJSValue);
216+
assertIsArray(membersJSValue, "setLegacyGroup.membersJSValue");
217217
auto membersJS = membersJSValue.As<Napi::Array>();
218218
uint32_t arrayLength = membersJS.Length();
219219
std::vector<std::pair<std::string, bool>> membersToAddOrUpdate;

src/utilities.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ void assertIsNumber(const Napi::Value& val, const std::string& identifier) {
2727
std::string("Wrong arguments: expected number" + identifier).c_str());
2828
}
2929

30-
void assertIsArray(const Napi::Value& val) {
31-
checkOrThrow(val.IsArray(), "Wrong arguments: expected array");
30+
void assertIsArray(const Napi::Value& val, const std::string& identifier) {
31+
checkOrThrow(
32+
val.IsArray(), std::string("Wrong arguments: expected array:" + identifier).c_str());
3233
}
3334

3435
void assertIsObject(const Napi::Value& val) {
@@ -230,4 +231,23 @@ Napi::Object decrypt_result_to_JS(
230231
return obj;
231232
}
232233

234+
confirm_pushed_entry_t confirm_pushed_entry_from_JS(const Napi::Env& env, const Napi::Object& obj) {
235+
auto seqnoJsValue = obj.Get("seqno");
236+
assertIsNumber(seqnoJsValue, "confirm_pushed_entry_from_JS.seqno");
237+
int64_t seqno = toCppInteger(seqnoJsValue, "confirm_pushed_entry_from_JS.seqno", false);
238+
auto hashesJsValue = obj.Get("hashes");
239+
assertIsArray(hashesJsValue, "confirm_pushed_entry_from_JS.hashes");
240+
241+
auto hashesJs = hashesJsValue.As<Napi::Array>();
242+
std::unordered_set<std::string> hashes;
243+
for (uint32_t i = 0; i < hashesJs.Length(); i++) {
244+
auto hashValue = hashesJs.Get(i);
245+
assertIsString(hashValue);
246+
std::string hash = toCppString(hashValue, "confirm_pushed_entry_from_JS.hashes.hash");
247+
hashes.insert(hash);
248+
}
249+
confirm_pushed_entry_t confirmed_pushed_entry{seqno, hashes};
250+
return confirmed_pushed_entry;
251+
}
252+
233253
} // namespace session::nodeapi

0 commit comments

Comments
 (0)