Skip to content

Commit bcc832b

Browse files
committed
feat: add multi part msg support
1 parent 655451d commit bcc832b

13 files changed

+94
-74
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/base_config.cpp

Lines changed: 16 additions & 10 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) {
@@ -45,20 +49,21 @@ Napi::Value ConfigBaseImpl::makeDump(const Napi::CallbackInfo& info) {
4549

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

5258
get_config<ConfigBase>().confirm_pushed(
53-
toCppInteger(info[0], "confirmPushed", false),
54-
toCppString(info[1], "confirmPushed"));
59+
std::get<0>(confirmed_pushed_entry), std::get<1>(confirmed_pushed_entry));
5560
});
5661
}
5762

5863
Napi::Value ConfigBaseImpl::merge(const Napi::CallbackInfo& info) {
5964
return wrapResult(info, [&]() {
6065
assertInfoLength(info, 1);
61-
assertIsArray(info[0]);
66+
assertIsArray(info[0], "ConfigBaseImpl::merge");
6267
Napi::Array asArray = info[0].As<Napi::Array>();
6368

6469
std::vector<std::pair<std::string, std::vector<unsigned char>>> conf_strs;
@@ -75,8 +80,9 @@ Napi::Value ConfigBaseImpl::merge(const Napi::CallbackInfo& info) {
7580
toCppString(itemObject.Get("hash"), "base.merge"),
7681
toCppBuffer(itemObject.Get("data"), "base.merge"));
7782
}
78-
79-
return get_config<ConfigBase>().merge(conf_strs);
83+
std::unordered_set<std::string> merged = get_config<ConfigBase>().merge(conf_strs);
84+
std::vector<std::string> mergedVec(merged.begin(), merged.end());
85+
return mergedVec;
8086
});
8187
}
8288

src/base_config.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ConfigBaseImpl {
3333
// These are exposed as read-only accessors rather than methods:
3434
Napi::Value needsDump(const Napi::CallbackInfo& info);
3535
Napi::Value needsPush(const Napi::CallbackInfo& info);
36-
Napi::Value currentHashes(const Napi::CallbackInfo& info);
36+
Napi::Value activeHashes(const Napi::CallbackInfo& info);
3737

3838
Napi::Value push(const Napi::CallbackInfo& info);
3939
Napi::Value dump(const Napi::CallbackInfo& info);
@@ -53,7 +53,7 @@ class ConfigBaseImpl {
5353

5454
properties.push_back(T::InstanceMethod("needsDump", &T::needsDump));
5555
properties.push_back(T::InstanceMethod("needsPush", &T::needsPush));
56-
properties.push_back(T::InstanceMethod("currentHashes", &T::currentHashes));
56+
properties.push_back(T::InstanceMethod("activeHashes", &T::activeHashes));
5757
properties.push_back(T::InstanceMethod("push", &T::push));
5858
properties.push_back(T::InstanceMethod("dump", &T::dump));
5959
properties.push_back(T::InstanceMethod("makeDump", &T::makeDump));

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: 23 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,24 @@ 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+
236+
auto seqnoJsValue = obj.Get("seqno");
237+
assertIsNumber(seqnoJsValue, "confirm_pushed_entry_from_JS.seqno");
238+
int64_t seqno = toCppInteger(seqnoJsValue, "confirm_pushed_entry_from_JS.seqno", false);
239+
auto hashesJsValue = obj.Get("hashes");
240+
assertIsArray(hashesJsValue, "confirm_pushed_entry_from_JS.hashes");
241+
242+
auto hashesJs = hashesJsValue.As<Napi::Array>();
243+
std::unordered_set<std::string> hashes;
244+
for (uint32_t i = 0; i < hashesJs.Length(); i++) {
245+
auto hashValue = hashesJs.Get(i);
246+
assertIsString(hashValue);
247+
std::string hash = toCppString(hashValue, "confirm_pushed_entry_from_JS.hashes.hash");
248+
hashes.insert(hash);
249+
}
250+
confirm_pushed_entry_t confirmed_pushed_entry{seqno, hashes};
251+
return confirmed_pushed_entry;
252+
}
253+
233254
} // namespace session::nodeapi

src/utilities.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void assertInfoMinLength(const Napi::CallbackInfo& info, const int minLength);
2929

3030
void assertIsStringOrNull(const Napi::Value& value);
3131
void assertIsNumber(const Napi::Value& value, const std::string& identifier);
32-
void assertIsArray(const Napi::Value& value);
32+
void assertIsArray(const Napi::Value& value, const std::string& identifier);
3333
void assertIsObject(const Napi::Value& value);
3434
void assertIsUInt8ArrayOrNull(const Napi::Value& value);
3535
void assertIsUInt8Array(const Napi::Value& value, const std::string& identifier);
@@ -56,7 +56,6 @@ auto getStringArgs(const Napi::CallbackInfo& info) {
5656
std::string toCppString(Napi::Value x, const std::string& identifier);
5757
std::vector<unsigned char> toCppBuffer(Napi::Value x, const std::string& identifier);
5858

59-
6059
int64_t toCppInteger(Napi::Value x, const std::string& identifier, bool allowUndefined = false);
6160
std::optional<int64_t> maybeNonemptyInt(Napi::Value x, const std::string& identifier);
6261
std::optional<bool> maybeNonemptyBoolean(Napi::Value x, const std::string& identifier);
@@ -268,8 +267,11 @@ int64_t unix_timestamp_now();
268267

269268
using push_entry_t = std::tuple<
270269
session::config::seqno_t,
271-
std::vector<unsigned char>,
272-
std::vector<std::string, std::allocator<std::string>>>;
270+
std::vector<std::vector<unsigned char>>,
271+
std::vector<std::string>>;
272+
273+
using confirm_pushed_entry_t =
274+
std::tuple<session::config::seqno_t, std::unordered_set<std::string>>;
273275

274276
Napi::Object push_result_to_JS(
275277
const Napi::Env& env,
@@ -284,4 +286,6 @@ Napi::Object push_key_entry_to_JS(
284286
Napi::Object decrypt_result_to_JS(
285287
const Napi::Env& env, const std::pair<std::string, std::vector<unsigned char>> decrypted);
286288

289+
confirm_pushed_entry_t confirm_pushed_entry_from_JS(const Napi::Env& env, const Napi::Object& obj);
290+
287291
} // namespace session::nodeapi

0 commit comments

Comments
 (0)