Skip to content

Commit 5891fc3

Browse files
committed
feat: added generate master pro key & get/set pro config
1 parent 1b8b269 commit 5891fc3

File tree

10 files changed

+307
-118
lines changed

10 files changed

+307
-118
lines changed

include/multi_encrypt/multi_encrypt.hpp

Lines changed: 6 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "../utilities.hpp"
1212
#include "oxen/log.hpp"
13+
#include "pro/types.hpp"
1314
#include "session/attachments.hpp"
1415
#include "session/config/user_profile.hpp"
1516
#include "session/multi_encrypt.hpp"
@@ -21,93 +22,6 @@ namespace log = oxen::log;
2122

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

24-
template <>
25-
struct toJs_impl<session::ProProof> {
26-
Napi::Object operator()(const Napi::Env& env, const session::ProProof pro_proof) {
27-
auto obj = Napi::Object::New(env);
28-
29-
obj["version"] = toJs(env, pro_proof.version);
30-
obj["genIndexHashB64"] = toJs(env, oxenc::to_base64(pro_proof.gen_index_hash));
31-
obj["rotatingPubkeyHex"] = toJs(env, oxenc::to_hex(pro_proof.rotating_pubkey));
32-
obj["expiryMs"] = toJs(env, pro_proof.expiry_unix_ts.time_since_epoch().count());
33-
34-
return obj;
35-
}
36-
};
37-
38-
template <>
39-
struct toJs_impl<session::Envelope> {
40-
Napi::Object operator()(const Napi::Env& env, const session::Envelope envelope) {
41-
auto obj = Napi::Object::New(env);
42-
43-
obj["timestampMs"] = toJs(env, envelope.timestamp.count());
44-
obj["source"] = envelope.source.size() ? toJs(env, envelope.source) : env.Null();
45-
obj["proSigHex"] =
46-
envelope.pro_sig.size() ? toJs(env, oxenc::to_hex(envelope.pro_sig)) : env.Null();
47-
48-
return obj;
49-
}
50-
};
51-
52-
template <>
53-
struct toJs_impl<session::DecodedEnvelope> {
54-
Napi::Object operator()(const Napi::Env& env, const session::DecodedEnvelope decoded_envelope) {
55-
auto obj = Napi::Object::New(env);
56-
57-
obj.Set("envelope", toJs(env, decoded_envelope.envelope));
58-
obj.Set("contentPlaintextUnpadded", toJs(env, decoded_envelope.content_plaintext));
59-
obj.Set("sessionId",
60-
toJs(env, "05" + oxenc::to_hex(decoded_envelope.sender_x25519_pubkey)));
61-
obj.Set("decodedPro", decoded_envelope.pro ? toJs(env, decoded_envelope.pro) : env.Null());
62-
63-
return obj;
64-
}
65-
};
66-
67-
template <>
68-
struct toJs_impl<SESSION_PROTOCOL_PRO_FEATURES> {
69-
Napi::Object operator()(const Napi::Env& env, const SESSION_PROTOCOL_PRO_FEATURES bitset) {
70-
Napi::Array arr = Napi::Array::New(env);
71-
uint32_t index = 0;
72-
73-
if (bitset == SESSION_PROTOCOL_PRO_FEATURES_NIL) {
74-
return arr;
75-
}
76-
77-
if (bitset & (SESSION_PROTOCOL_PRO_FEATURES_10K_CHARACTER_LIMIT)) {
78-
arr[index] = Napi::String::New(env, "10K_CHARACTER_LIMIT");
79-
index++;
80-
}
81-
if (bitset & SESSION_PROTOCOL_PRO_FEATURES_PRO_BADGE) {
82-
arr[index++] = Napi::String::New(env, "PRO_BADGE");
83-
index++;
84-
}
85-
if (bitset & SESSION_PROTOCOL_PRO_FEATURES_ANIMATED_AVATAR) {
86-
arr[index++] = Napi::String::New(env, "ANIMATED_AVATAR");
87-
index++;
88-
}
89-
return arr;
90-
}
91-
};
92-
93-
template <>
94-
struct toJs_impl<session::DecodedPro> {
95-
Napi::Object operator()(const Napi::Env& env, const session::DecodedPro decoded_pro) {
96-
auto obj = Napi::Object::New(env);
97-
98-
obj["proStatus"] =
99-
toJs(env,
100-
decoded_pro.status == ProStatus::InvalidProBackendSig ? "InvalidProBackendSig"
101-
: decoded_pro.status == ProStatus::InvalidUserSig ? "InvalidUserSig"
102-
: decoded_pro.status == ProStatus::Valid ? "Valid"
103-
: "Expired");
104-
obj["proProof"] = toJs(env, decoded_pro.proof);
105-
obj["proFeatures"] = toJs(env, decoded_pro.features);
106-
107-
return obj;
108-
}
109-
};
110-
11125
inline std::vector<unsigned char> extractPlaintext(
11226
const Napi::Object& obj, const std::string identifier) {
11327

@@ -181,7 +95,7 @@ inline cleared_uc32 extractGroupEncKeyAsArray(
18195
return result;
18296
}
18397

184-
inline std::optional<std::vector<unsigned char>> extractProRotatingEd25519PrivKeyAsSpan(
98+
inline std::optional<std::vector<unsigned char>> extractProRotatingEd25519PrivKeyAsVector(
18599
const Napi::Object& obj, const std::string identifier) {
186100
assertIsStringOrNull(obj.Get("proRotatingEd25519PrivKey"), identifier);
187101
auto proRotatingEd25519PrivKeyHex =
@@ -588,7 +502,7 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
588502
obj, "encryptFor1o1.obj.senderEd25519Seed"),
589503
extractSentTimestampMs(obj, "encryptFor1o1.obj.sentTimestampMs"),
590504
extractRecipientPubkeyAsArray(obj, "encryptFor1o1.obj.recipientPubkey"),
591-
extractProRotatingEd25519PrivKeyAsSpan(
505+
extractProRotatingEd25519PrivKeyAsVector(
592506
obj, "encryptFor1o1.obj.proRotatingEd25519PrivKey"));
593507
}
594508

@@ -639,7 +553,7 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
639553
obj, "encryptForCommunityInbox.obj.recipientPubkey"),
640554
extractCommunityPubkeyAsArray(
641555
obj, "encryptForCommunityInbox.obj.communityPubkey"),
642-
extractProRotatingEd25519PrivKeyAsSpan(
556+
extractProRotatingEd25519PrivKeyAsVector(
643557
obj, "encryptForCommunityInbox.obj.proRotatingEd25519PrivKey"));
644558
}
645559

@@ -678,7 +592,7 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
678592

679593
ready_to_send[i] = session::encode_for_community(
680594
extractPlaintext(obj, "encryptForCommunity.obj.plaintext"),
681-
extractProRotatingEd25519PrivKeyAsSpan(
595+
extractProRotatingEd25519PrivKeyAsVector(
682596
obj, "encryptForCommunity.obj.proRotatingEd25519PrivKey"));
683597
}
684598

@@ -731,7 +645,7 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
731645

732646
auto groupEncKey =
733647
extractGroupEncKeyAsArray(obj, "encryptForGroup.obj.groupEncKey");
734-
auto proRotatingEd25519PrivKey = extractProRotatingEd25519PrivKeyAsSpan(
648+
auto proRotatingEd25519PrivKey = extractProRotatingEd25519PrivKeyAsVector(
735649
obj, "encryptForGroup.obj.proRotatingEd25519PrivKey");
736650

737651
ready_to_send[i] = session::encode_for_group(

include/pro/types.hpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#pragma once
2+
3+
#include <napi.h>
4+
#include <oxenc/base64.h>
5+
#include <oxenc/hex.h>
6+
7+
#include <algorithm>
8+
#include <span>
9+
#include <vector>
10+
11+
#include "../utilities.hpp"
12+
#include "oxen/log.hpp"
13+
#include "session/attachments.hpp"
14+
#include "session/config/user_profile.hpp"
15+
#include "session/multi_encrypt.hpp"
16+
#include "session/random.hpp"
17+
18+
namespace session::nodeapi {
19+
20+
template <>
21+
struct toJs_impl<session::ProProof> {
22+
Napi::Object operator()(const Napi::Env& env, const session::ProProof pro_proof) {
23+
auto obj = Napi::Object::New(env);
24+
25+
obj["version"] = toJs(env, pro_proof.version);
26+
obj["genIndexHashB64"] = toJs(env, oxenc::to_base64(pro_proof.gen_index_hash));
27+
obj["rotatingPubkeyHex"] = toJs(env, oxenc::to_hex(pro_proof.rotating_pubkey));
28+
obj["expiryMs"] = toJs(env, pro_proof.expiry_unix_ts.time_since_epoch().count());
29+
30+
return obj;
31+
}
32+
};
33+
34+
template <>
35+
struct toJs_impl<session::config::ProConfig> {
36+
Napi::Value operator()(const Napi::Env& env, const session::config::ProConfig pro_config) {
37+
auto obj = Napi::Object::New(env);
38+
39+
obj["rotatingPrivKeyHex"] = toJs(env, oxenc::to_hex(pro_config.rotating_privkey));
40+
obj["proProof"] = toJs(env, pro_config.proof);
41+
42+
return obj;
43+
}
44+
};
45+
46+
template <>
47+
struct toJs_impl<session::Envelope> {
48+
Napi::Object operator()(const Napi::Env& env, const session::Envelope envelope) {
49+
auto obj = Napi::Object::New(env);
50+
51+
obj["timestampMs"] = toJs(env, envelope.timestamp.count());
52+
obj["source"] = envelope.source.size() ? toJs(env, envelope.source) : env.Null();
53+
obj["proSigHex"] =
54+
envelope.pro_sig.size() ? toJs(env, oxenc::to_hex(envelope.pro_sig)) : env.Null();
55+
56+
return obj;
57+
}
58+
};
59+
60+
template <>
61+
struct toJs_impl<session::DecodedEnvelope> {
62+
Napi::Object operator()(const Napi::Env& env, const session::DecodedEnvelope decoded_envelope) {
63+
auto obj = Napi::Object::New(env);
64+
65+
obj.Set("envelope", toJs(env, decoded_envelope.envelope));
66+
obj.Set("contentPlaintextUnpadded", toJs(env, decoded_envelope.content_plaintext));
67+
obj.Set("sessionId",
68+
toJs(env, "05" + oxenc::to_hex(decoded_envelope.sender_x25519_pubkey)));
69+
obj.Set("decodedPro", decoded_envelope.pro ? toJs(env, decoded_envelope.pro) : env.Null());
70+
71+
return obj;
72+
}
73+
};
74+
75+
template <>
76+
struct toJs_impl<SESSION_PROTOCOL_PRO_FEATURES> {
77+
Napi::Object operator()(const Napi::Env& env, const SESSION_PROTOCOL_PRO_FEATURES bitset) {
78+
Napi::Array arr = Napi::Array::New(env);
79+
uint32_t index = 0;
80+
81+
if (bitset == SESSION_PROTOCOL_PRO_FEATURES_NIL) {
82+
return arr;
83+
}
84+
85+
if (bitset & (SESSION_PROTOCOL_PRO_FEATURES_10K_CHARACTER_LIMIT)) {
86+
arr[index] = Napi::String::New(env, "10K_CHARACTER_LIMIT");
87+
index++;
88+
}
89+
if (bitset & SESSION_PROTOCOL_PRO_FEATURES_PRO_BADGE) {
90+
arr[index++] = Napi::String::New(env, "PRO_BADGE");
91+
index++;
92+
}
93+
if (bitset & SESSION_PROTOCOL_PRO_FEATURES_ANIMATED_AVATAR) {
94+
arr[index++] = Napi::String::New(env, "ANIMATED_AVATAR");
95+
index++;
96+
}
97+
return arr;
98+
}
99+
};
100+
101+
template <>
102+
struct toJs_impl<session::DecodedPro> {
103+
Napi::Object operator()(const Napi::Env& env, const session::DecodedPro decoded_pro) {
104+
auto obj = Napi::Object::New(env);
105+
106+
obj["proStatus"] =
107+
toJs(env,
108+
decoded_pro.status == ProStatus::InvalidProBackendSig ? "InvalidProBackendSig"
109+
: decoded_pro.status == ProStatus::InvalidUserSig ? "InvalidUserSig"
110+
: decoded_pro.status == ProStatus::Valid ? "Valid"
111+
: "Expired");
112+
obj["proProof"] = toJs(env, decoded_pro.proof);
113+
obj["proFeatures"] = toJs(env, decoded_pro.features);
114+
115+
return obj;
116+
}
117+
};
118+
119+
}; // namespace session::nodeapi

include/user_config.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class UserConfigWrapper : public ConfigBaseImpl, public Napi::ObjectWrap<UserCon
3434

3535
Napi::Value getNoteToSelfExpiry(const Napi::CallbackInfo& info);
3636
void setNoteToSelfExpiry(const Napi::CallbackInfo& info);
37-
};
3837

39-
} // namespace session::nodeapi
38+
Napi::Value getProConfig(const Napi::CallbackInfo& info);
39+
void setProConfig(const Napi::CallbackInfo& info);
40+
Napi::Value generateProMasterKey(const Napi::CallbackInfo& info);
41+
};
42+
}; // namespace session::nodeapi

include/utilities.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,11 @@ std::array<uint8_t, N> from_hex_to_array(std::string x) {
377377

378378
std::vector<unsigned char> from_hex_to_vector(std::string_view x);
379379

380+
std::span<const uint8_t> from_base64_to_span(std::string_view x);
381+
std::vector<unsigned char> from_base64_to_vector(std::string_view x) ;
382+
383+
384+
// Concept to match containers with a size() method
380385
// Concept to match containers with a size() method
381386
template <typename T>
382387
concept HasSize = requires(T t) {

0 commit comments

Comments
 (0)