Skip to content

Commit 0d86a09

Browse files
committed
Retyped the libSession wrapper code in preparation of the 1.3.0 changes
1 parent 7a27a0a commit 0d86a09

File tree

10 files changed

+75
-64
lines changed

10 files changed

+75
-64
lines changed

src/base_config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Napi::Value ConfigBaseImpl::merge(const Napi::CallbackInfo& info) {
6161
assertIsArray(info[0]);
6262
Napi::Array asArray = info[0].As<Napi::Array>();
6363

64-
std::vector<std::pair<std::string, ustring_view>> conf_strs;
64+
std::vector<std::pair<std::string, std::span<const unsigned char>>> conf_strs;
6565
conf_strs.reserve(asArray.Length());
6666

6767
for (uint32_t i = 0; i < asArray.Length(); i++) {

src/base_config.hpp

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

66
#include <cassert>
77
#include <memory>
8+
#include <span>
89
#include <stdexcept>
910
#include <unordered_set>
1011

@@ -13,8 +14,6 @@
1314
#include "session/types.hpp"
1415
#include "utilities.hpp"
1516

16-
using ustring_view = std::basic_string_view<unsigned char>;
17-
1817
namespace session::nodeapi {
1918

2019
class ConfigBaseImpl;
@@ -95,9 +94,9 @@ class ConfigBaseImpl {
9594
// we should get secret key as first arg and optional dumped as second argument
9695
assertIsUInt8Array(info[0], "base construct");
9796
assertIsUInt8ArrayOrNull(info[1]);
98-
ustring_view secretKey = toCppBufferView(info[0], class_name + ".new");
97+
std::span<const unsigned char> secretKey = toCppBufferView(info[0], class_name + ".new");
9998

100-
std::optional<ustring_view> dump;
99+
std::optional<std::span<const unsigned char>> dump;
101100
auto second = info[1];
102101
if (!second.IsEmpty() && !second.IsNull() && !second.IsUndefined())
103102
dump = toCppBufferView(second, class_name + ".new");

src/blinding/blinding.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <napi.h>
44

55
#include <algorithm>
6+
#include <vector>
67

78
#include "../meta/meta_base_wrapper.hpp"
89
#include "../utilities.hpp"
@@ -58,8 +59,7 @@ class BlindingWrapper : public Napi::ObjectWrap<BlindingWrapper> {
5859

5960
auto keypair = session::blind_version_key_pair(ed25519_secret_key);
6061
session::uc32 pk_arr = std::get<0>(keypair);
61-
ustring blinded_pk = session::ustring(
62-
session::to_unsigned_sv(std::string(pk_arr.begin(), pk_arr.end())));
62+
std::vector<unsigned char> blinded_pk = session::to_vector(pk_arr);
6363
std::string blinded_pk_hex;
6464
blinded_pk_hex.reserve(66);
6565
blinded_pk_hex += "07";

src/groups/meta_group.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <napi.h>
4+
#include <vector>
45

56
#include "session/config/groups/info.hpp"
67
#include "session/config/groups/keys.hpp"
@@ -32,12 +33,12 @@ class MetaGroup {
3233
shared_ptr<config::groups::Info> info,
3334
shared_ptr<config::groups::Members> members,
3435
shared_ptr<config::groups::Keys> keys,
35-
session::ustring edGroupPubKey,
36-
std::optional<session::ustring> edGroupSecKey) :
37-
info{info}, members{members}, keys{keys}, edGroupPubKey{oxenc::to_hex(edGroupPubKey)} {
36+
std::vector<unsigned char> edGroupPubKey,
37+
std::optional<std::vector<unsigned char>> edGroupSecKey) :
38+
info{info}, members{members}, keys{keys}, edGroupPubKey{oxenc::to_hex(edGroupPubKey.begin(), edGroupPubKey.end())} {
3839

3940
if (edGroupSecKey.has_value()) {
40-
this->edGroupSecKey = oxenc::to_hex(*edGroupSecKey);
41+
this->edGroupSecKey = oxenc::to_hex(edGroupSecKey->begin(), edGroupSecKey->end());
4142
} else {
4243
this->edGroupSecKey = std::nullopt;
4344
}

src/groups/meta_group_wrapper.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <napi.h>
44

55
#include <memory>
6+
#include <span>
7+
#include <vector>
68

79
#include "oxenc/bt_producer.h"
810
#include "session/types.hpp"
@@ -208,12 +210,12 @@ Napi::Value MetaGroupWrapper::metaDump(const Napi::CallbackInfo& info) {
208210
oxenc::bt_dict_producer combined;
209211

210212
// NOTE: the keys have to be in ascii-sorted order:
211-
combined.append("info", session::from_unsigned_sv(this->meta_group->info->dump()));
212-
combined.append("keys", session::from_unsigned_sv(this->meta_group->keys->dump()));
213-
combined.append("members", session::from_unsigned_sv(this->meta_group->members->dump()));
213+
combined.append("info", session::to_string(this->meta_group->info->dump()));
214+
combined.append("keys", session::to_string(this->meta_group->keys->dump()));
215+
combined.append("members", session::to_string(this->meta_group->members->dump()));
214216
auto to_dump = std::move(combined).str();
215217

216-
return session::ustring{to_unsigned_sv(to_dump)};
218+
return session::to_vector(to_dump);
217219
});
218220
}
219221

@@ -222,13 +224,13 @@ Napi::Value MetaGroupWrapper::metaMakeDump(const Napi::CallbackInfo& info) {
222224
oxenc::bt_dict_producer combined;
223225

224226
// NOTE: the keys have to be in ascii-sorted order:
225-
combined.append("info", session::from_unsigned_sv(this->meta_group->info->make_dump()));
226-
combined.append("keys", session::from_unsigned_sv(this->meta_group->keys->make_dump()));
227+
combined.append("info", session::to_string(this->meta_group->info->make_dump()));
228+
combined.append("keys", session::to_string(this->meta_group->keys->make_dump()));
227229
combined.append(
228-
"members", session::from_unsigned_sv(this->meta_group->members->make_dump()));
230+
"members", session::to_string(this->meta_group->members->make_dump()));
229231
auto to_dump = std::move(combined).str();
230232

231-
return ustring{to_unsigned_sv(to_dump)};
233+
return session::to_vector(to_dump);
232234
});
233235
}
234236

@@ -326,7 +328,7 @@ Napi::Value MetaGroupWrapper::metaMerge(const Napi::CallbackInfo& info) {
326328
assertIsArray(groupInfo);
327329
auto asArr = groupInfo.As<Napi::Array>();
328330

329-
std::vector<std::pair<std::string, ustring_view>> conf_strs;
331+
std::vector<std::pair<std::string, std::span<const unsigned char>>> conf_strs;
330332
conf_strs.reserve(asArr.Length());
331333

332334
for (uint32_t i = 0; i < asArr.Length(); i++) {
@@ -353,7 +355,7 @@ Napi::Value MetaGroupWrapper::metaMerge(const Napi::CallbackInfo& info) {
353355
assertIsArray(groupMember);
354356
auto asArr = groupMember.As<Napi::Array>();
355357

356-
std::vector<std::pair<std::string, ustring_view>> conf_strs;
358+
std::vector<std::pair<std::string, std::span<const unsigned char>>> conf_strs;
357359
conf_strs.reserve(asArr.Length());
358360

359361
for (uint32_t i = 0; i < asArr.Length(); i++) {
@@ -790,7 +792,7 @@ Napi::Value MetaGroupWrapper::encryptMessages(const Napi::CallbackInfo& info) {
790792

791793
auto plaintextsJS = info[0].As<Napi::Array>();
792794
uint32_t arrayLength = plaintextsJS.Length();
793-
std::vector<session::ustring> encryptedMessages;
795+
std::vector<std::vector<unsigned char>> encryptedMessages;
794796
encryptedMessages.reserve(arrayLength);
795797

796798
for (uint32_t i = 0; i < plaintextsJS.Length(); i++) {
@@ -820,10 +822,10 @@ Napi::Value MetaGroupWrapper::makeSwarmSubAccount(const Napi::CallbackInfo& info
820822
assertIsString(info[0]);
821823

822824
auto memberPk = toCppString(info[0], "makeSwarmSubAccount");
823-
ustring subaccount = this->meta_group->keys->swarm_make_subaccount(memberPk);
825+
std::vector<unsigned char> subaccount = this->meta_group->keys->swarm_make_subaccount(memberPk);
824826

825827
session::nodeapi::checkOrThrow(
826-
subaccount.length() == 100, "expected subaccount to be 100 bytes long");
828+
subaccount.size() == 100, "expected subaccount to be 100 bytes long");
827829

828830
return subaccount;
829831
});
@@ -835,12 +837,12 @@ Napi::Value MetaGroupWrapper::swarmSubAccountToken(const Napi::CallbackInfo& inf
835837
assertIsString(info[0]);
836838

837839
auto memberPk = toCppString(info[0], "swarmSubAccountToken");
838-
ustring subaccount = this->meta_group->keys->swarm_subaccount_token(memberPk);
840+
std::vector<unsigned char> subaccount = this->meta_group->keys->swarm_subaccount_token(memberPk);
839841

840842
session::nodeapi::checkOrThrow(
841-
subaccount.length() == 36, "expected subaccount token to be 36 bytes long");
843+
subaccount.size() == 36, "expected subaccount token to be 36 bytes long");
842844

843-
return oxenc::to_hex(subaccount);
845+
return oxenc::to_hex(subaccount.begin(), subaccount.end());
844846
});
845847
}
846848

src/meta/meta_base_wrapper.hpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

33
#include <napi.h>
4+
#include <vector>
5+
#include <span>
46

57
#include "../base_config.hpp"
68
#include "../groups/meta_group.hpp"
@@ -56,33 +58,33 @@ class MetaBaseWrapper {
5658
obj.Get("groupEd25519Pubkey"),
5759
class_name + ":constructGroupWrapper.groupEd25519Pubkey");
5860

59-
std::optional<ustring> group_ed25519_secretkey = maybeNonemptyBuffer(
61+
std::optional<std::vector<unsigned char>> group_ed25519_secretkey = maybeNonemptyBuffer(
6062
obj.Get("groupEd25519Secretkey"),
6163
class_name + ":constructGroupWrapper.groupEd25519Secretkey");
6264

63-
std::optional<ustring> dumped_meta = maybeNonemptyBuffer(
65+
std::optional<std::vector<unsigned char>> dumped_meta = maybeNonemptyBuffer(
6466
obj.Get("metaDumped"), class_name + ":constructGroupWrapper.metaDumped");
6567

66-
std::optional<ustring_view> dumped_info;
67-
std::optional<ustring_view> dumped_members;
68-
std::optional<ustring_view> dumped_keys;
68+
std::optional<std::span<const unsigned char>> dumped_info;
69+
std::optional<std::span<const unsigned char>> dumped_members;
70+
std::optional<std::span<const unsigned char>> dumped_keys;
6971

7072
if (dumped_meta) {
71-
auto dumped_meta_str = from_unsigned_sv(*dumped_meta);
73+
auto dumped_meta_str = to_string(*dumped_meta);
7274

7375
oxenc::bt_dict_consumer combined{dumped_meta_str};
7476
// NB: must read in ascii-sorted order:
7577
if (!combined.skip_until("info"))
7678
throw std::runtime_error{"info dump not found in combined dump!"};
77-
dumped_info = session::to_unsigned_sv(combined.consume_string_view());
79+
dumped_info = session::to_span(combined.consume_string_view());
7880

7981
if (!combined.skip_until("keys"))
8082
throw std::runtime_error{"keys dump not found in combined dump!"};
81-
dumped_keys = session::to_unsigned_sv(combined.consume_string_view());
83+
dumped_keys = session::to_span(combined.consume_string_view());
8284

8385
if (!combined.skip_until("members"))
8486
throw std::runtime_error{"members dump not found in combined dump!"};
85-
dumped_members = session::to_unsigned_sv(combined.consume_string_view());
87+
dumped_members = session::to_span(combined.consume_string_view());
8688
}
8789

8890
// Note, we keep shared_ptr for those as the Keys one need a reference to Members and

src/multi_encrypt/multi_encrypt.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#pragma once
22

33
#include <napi.h>
4+
#include <span>
5+
#include <vector>
46

57
#include <algorithm>
68

@@ -58,7 +60,7 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
5860
auto messagesJSValue = obj.Get("messages");
5961
assertIsArray(messagesJSValue);
6062
auto messagesJS = messagesJSValue.As<Napi::Array>();
61-
std::vector<ustring> messages;
63+
std::vector<std::vector<unsigned char>> messages;
6264
messages.reserve(messagesJS.Length());
6365
for (uint32_t i = 0; i < messagesJS.Length(); i++) {
6466
auto itemValue = messagesJS.Get(i);
@@ -71,18 +73,18 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
7173
auto recipientsJSValue = obj.Get("recipients");
7274
assertIsArray(recipientsJSValue);
7375
auto recipientsJS = recipientsJSValue.As<Napi::Array>();
74-
std::vector<ustring> recipients;
76+
std::vector<std::vector<unsigned char>> recipients;
7577
recipients.reserve(recipientsJS.Length());
7678
for (uint32_t i = 0; i < recipientsJS.Length(); i++) {
7779
auto itemValue = recipientsJS.Get(i);
7880
assertIsUInt8Array(itemValue, "multiEncrypt.itemValue.recipient");
7981
auto item = toCppBuffer(itemValue, "multiEncrypt.itemValue.recipient");
8082
recipients.push_back(item);
8183
}
82-
ustring random_nonce = session::random::random(24);
84+
std::vector<unsigned char> random_nonce = session::random::random(24);
8385

84-
std::vector<ustring_view> messages_sv(messages.begin(), messages.end());
85-
std::vector<ustring_view> recipients_sv(recipients.begin(), recipients.end());
86+
std::vector<std::span<const unsigned char>> messages_sv(messages.begin(), messages.end());
87+
std::vector<std::span<const unsigned char>> recipients_sv(recipients.begin(), recipients.end());
8688

8789
// Note: this function needs the first 2 args to be vector of sv explicitly
8890
return session::encrypt_for_multiple_simple(

src/user_groups_config.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <iostream>
66
#include <optional>
7+
#include <vector>
78

89
#include "base_config.hpp"
910
#include "community.hpp"
@@ -158,7 +159,7 @@ Napi::Value UserGroupsWrapper::buildFullUrlFromDetails(const Napi::CallbackInfo&
158159
return wrapResult(env, [&]() {
159160
auto [baseUrl, roomId, pubkeyHex] = getStringArgs<3>(info);
160161

161-
ustring pubkey_bytes;
162+
std::vector<unsigned char> pubkey_bytes;
162163
if (!oxenc::is_hex(pubkeyHex.begin(), pubkeyHex.end()))
163164
throw std::invalid_argument{"community pubkey is not hex!"};
164165
oxenc::from_hex(pubkeyHex.begin(), pubkeyHex.end(), std::back_inserter(pubkey_bytes));

src/utilities.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <chrono>
66

77
#include "session/config/namespaces.hpp"
8+
#include "session/util.hpp"
89

910
namespace session::nodeapi {
1011

@@ -87,9 +88,9 @@ std::optional<std::string> maybeNonemptyString(Napi::Value x, const std::string&
8788
throw std::invalid_argument{"maybeNonemptyString with invalid type, called from " + identifier};
8889
}
8990

90-
// Converts to a ustring_view that views directly into the Uint8Array data of `x`. Throws if x is
91+
// Converts to a std::span<const unsigned char> that views directly into the Uint8Array data of `x`. Throws if x is
9192
// not a Uint8Array. The view must not be used beyond the lifetime of `x`.
92-
ustring_view toCppBufferView(Napi::Value x, const std::string& identifier) {
93+
std::span<const unsigned char> toCppBufferView(Napi::Value x, const std::string& identifier) {
9394
if (x.IsNull() || x.IsUndefined())
9495
throw std::invalid_argument(
9596
"toCppBuffer called with null or undefined with identifier: " + identifier);
@@ -101,15 +102,15 @@ ustring_view toCppBufferView(Napi::Value x, const std::string& identifier) {
101102
return {u8Array.Data(), u8Array.ByteLength()};
102103
}
103104

104-
ustring toCppBuffer(Napi::Value x, const std::string& identifier) {
105-
return ustring{toCppBufferView(x, std::move(identifier))};
105+
std::vector<unsigned char> toCppBuffer(Napi::Value x, const std::string& identifier) {
106+
return session::to_vector(toCppBufferView(x, std::move(identifier)));
106107
}
107108

108-
std::optional<ustring> maybeNonemptyBuffer(Napi::Value x, const std::string& identifier) {
109+
std::optional<std::vector<unsigned char>> maybeNonemptyBuffer(Napi::Value x, const std::string& identifier) {
109110
if (x.IsNull() || x.IsUndefined())
110111
return std::nullopt;
111112

112-
std::optional<ustring> buf{toCppBuffer(x, identifier)};
113+
std::optional<std::vector<unsigned char>> buf{toCppBuffer(x, identifier)};
113114
if (buf->empty())
114115
buf.reset();
115116
return buf;
@@ -172,7 +173,7 @@ std::string printable(const char* x, size_t n) {
172173
return printable(std::string_view{x, n});
173174
}
174175

175-
std::string printable(ustring_view x) {
176+
std::string printable(std::span<const unsigned char> x) {
176177
return printable(reinterpret_cast<const char*>(x.data()), x.size());
177178
}
178179

@@ -208,7 +209,7 @@ Napi::Object push_result_to_JS(
208209

209210
Napi::Object push_key_entry_to_JS(
210211
const Napi::Env& env,
211-
const session::ustring_view& key_data,
212+
const std::span<const unsigned char>& key_data,
212213
const session::config::Namespace& push_namespace) {
213214
auto obj = Napi::Object::New(env);
214215

@@ -219,7 +220,7 @@ Napi::Object push_key_entry_to_JS(
219220
};
220221

221222
Napi::Object decrypt_result_to_JS(
222-
const Napi::Env& env, const std::pair<std::string, ustring> decrypted) {
223+
const Napi::Env& env, const std::pair<std::string, std::vector<unsigned char>> decrypted) {
223224
auto obj = Napi::Object::New(env);
224225

225226
obj["pubkeyHex"] = toJs(env, decrypted.first);

0 commit comments

Comments
 (0)