Skip to content

Commit ae5993c

Browse files
authored
Merge pull request #20 from Bilb/add-blinding
chore: bump libsession-util and use oxen fork
2 parents 825c1fd + 130c011 commit ae5993c

File tree

11 files changed

+177
-5
lines changed

11 files changed

+177
-5
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[submodule "libsession-util"]
22
path = libsession-util
3-
url = https://github.com/Bilb/libsession-util.git
3+
url = https://github.com/oxen-io/libsession-util.git

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// <reference path="./shared.d.ts" />
22
/// <reference path="./user/index.d.ts" />
3+
/// <reference path="./types/index.d.ts" />
34

45
declare module 'libsession_util_nodejs' {
56
/**

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.3.20",
5+
"version": "0.3.21",
66
"license": "GPL-3.0",
77
"author": {
88
"name": "Oxen Project",

src/addon.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <napi.h>
22

3+
#include "blinding/blinding.hpp"
34
#include "constants.hpp"
45
#include "contacts_config.hpp"
56
#include "convo_info_volatile_config.hpp"
@@ -15,6 +16,9 @@ Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
1516
UserGroupsWrapper::Init(env, exports);
1617
ConvoInfoVolatileWrapper::Init(env, exports);
1718

19+
// Fully static wrappers init
20+
BlindingWrapper::Init(env, exports);
21+
1822
return exports;
1923
}
2024

src/base_config.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ namespace session::nodeapi {
1515

1616
class ConfigBaseImpl;
1717
template <typename T>
18-
inline constexpr bool is_derived_napi_wrapper =
19-
std::is_base_of_v<ConfigBaseImpl, T> && std::is_base_of_v<Napi::ObjectWrap<T>, T>;
18+
inline constexpr bool is_derived_napi_wrapper = std::is_base_of_v<Napi::ObjectWrap<T>, T>;
2019

2120
/// Base implementation class for config types; this provides the napi wrappers for the base
2221
/// methods. Subclasses should inherit from this (alongside Napi::ObjectWrap<ConfigBaseWrapper>)

src/blinding/blinding.hpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#pragma once
2+
3+
#include <napi.h>
4+
5+
#include <algorithm>
6+
7+
#include "../meta/meta_base_wrapper.hpp"
8+
#include "../utilities.hpp"
9+
#include "oxenc/hex.h"
10+
#include "session/blinding.hpp"
11+
#include "session/config/user_profile.hpp"
12+
#include "session/platform.hpp"
13+
#include "session/random.hpp"
14+
15+
namespace session::nodeapi {
16+
17+
class BlindingWrapper : public Napi::ObjectWrap<BlindingWrapper> {
18+
19+
public:
20+
BlindingWrapper(const Napi::CallbackInfo& info) : Napi::ObjectWrap<BlindingWrapper>{info} {
21+
throw std::invalid_argument(
22+
"BlindingWrapper is all static and don't need to be constructed");
23+
}
24+
25+
static void Init(Napi::Env env, Napi::Object exports) {
26+
MetaBaseWrapper::NoBaseClassInitHelper<BlindingWrapper>(
27+
env,
28+
exports,
29+
"BlindingWrapperNode",
30+
{
31+
StaticMethod<&BlindingWrapper::blindVersionPubkey>(
32+
"blindVersionPubkey",
33+
static_cast<napi_property_attributes>(
34+
napi_writable | napi_configurable)),
35+
StaticMethod<&BlindingWrapper::blindVersionSign>(
36+
"blindVersionSign",
37+
static_cast<napi_property_attributes>(
38+
napi_writable | napi_configurable)),
39+
40+
});
41+
}
42+
43+
private:
44+
static Napi::Value blindVersionPubkey(const Napi::CallbackInfo& info) {
45+
return wrapResult(info, [&] {
46+
assertInfoLength(info, 1);
47+
assertIsObject(info[0]);
48+
auto obj = info[0].As<Napi::Object>();
49+
50+
if (obj.IsEmpty())
51+
throw std::invalid_argument("blindVersionPubkey received empty");
52+
53+
assertIsUInt8Array(obj.Get("ed25519SecretKey"));
54+
auto ed25519_secret_key =
55+
toCppBuffer(obj.Get("ed25519SecretKey"), "blindVersionPubkey.ed25519SecretKey");
56+
57+
auto keypair = session::blind_version_key_pair(ed25519_secret_key);
58+
session::uc32 pk_arr = std::get<0>(keypair);
59+
ustring blinded_pk = session::ustring(
60+
session::to_unsigned_sv(std::string(pk_arr.begin(), pk_arr.end())));
61+
std::string blinded_pk_hex;
62+
blinded_pk_hex.reserve(66);
63+
blinded_pk_hex += "07";
64+
oxenc::to_hex(blinded_pk.begin(), blinded_pk.end(), std::back_inserter(blinded_pk_hex));
65+
66+
return blinded_pk_hex;
67+
});
68+
};
69+
70+
static Napi::Value blindVersionSign(const Napi::CallbackInfo& info) {
71+
return wrapResult(info, [&] {
72+
assertInfoLength(info, 1);
73+
assertIsObject(info[0]);
74+
auto obj = info[0].As<Napi::Object>();
75+
76+
if (obj.IsEmpty())
77+
throw std::invalid_argument("blindVersionSign received empty");
78+
79+
assertIsUInt8Array(obj.Get("ed25519SecretKey"));
80+
auto ed25519_secret_key =
81+
toCppBuffer(obj.Get("ed25519SecretKey"), "blindVersionSign.ed25519SecretKey");
82+
83+
assertIsNumber(obj.Get("sigTimestampSeconds"));
84+
auto sig_timestamp = toCppInteger(
85+
obj.Get("sigTimestampSeconds"), "blindVersionSign.sigTimestampSeconds", false);
86+
87+
return session::blind_version_sign(
88+
ed25519_secret_key, Platform::desktop, sig_timestamp);
89+
});
90+
};
91+
};
92+
93+
} // namespace session::nodeapi

src/meta/meta_base_wrapper.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include <napi.h>
4+
5+
#include "../base_config.hpp"
6+
7+
namespace session::nodeapi {
8+
9+
class MetaBaseWrapper {
10+
11+
public:
12+
explicit MetaBaseWrapper(){};
13+
14+
virtual ~MetaBaseWrapper() = default;
15+
16+
template <typename T, std::enable_if_t<is_derived_napi_wrapper<T>, int> = 0>
17+
static void NoBaseClassInitHelper(
18+
Napi::Env env,
19+
Napi::Object exports,
20+
const char* class_name,
21+
std::vector<typename T::PropertyDescriptor> properties) {
22+
23+
// not adding the baseMethods here from withBaseMethods()
24+
Napi::Function cls = T::DefineClass(env, class_name, std::move(properties));
25+
26+
auto ref = std::make_unique<Napi::FunctionReference>();
27+
*ref = Napi::Persistent(cls);
28+
env.SetInstanceData(ref.release());
29+
30+
exports.Set(class_name, cls);
31+
}
32+
};
33+
34+
} // namespace session::nodeapi

types/blinding/blinding.d.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/// <reference path="../../shared.d.ts" />
2+
3+
declare module 'libsession_util_nodejs' {
4+
type BlindingWrapper = {
5+
blindVersionPubkey: (opts: {
6+
/**
7+
* len 64: ed25519 secretKey with pubkey
8+
*/
9+
ed25519SecretKey: Uint8Array;
10+
}) => Uint8Array;
11+
blindVersionSign: (opts: {
12+
/**
13+
* len 64: ed25519 secretKey with pubkey
14+
*/
15+
ed25519SecretKey: Uint8Array;
16+
sigTimestampSeconds: number;
17+
}) => Uint8Array;
18+
};
19+
20+
export type BlindingActionsCalls = MakeWrapperActionCalls<BlindingWrapper>;
21+
22+
/**
23+
* To be used inside the web worker only (calls are synchronous and won't work asynchrously)
24+
*/
25+
export class BlindingWrapperNode {
26+
public static blindVersionPubkey: BlindingWrapper['blindVersionPubkey'];
27+
public static blindVersionSign: BlindingWrapper['blindVersionSign'];
28+
}
29+
30+
/**
31+
* Those actions are used internally for the web worker communication.
32+
* You should never need to import them in Session directly
33+
* You will need to add an entry here if you add a new function
34+
*/
35+
export type BlindingActionsType =
36+
| MakeActionCall<BlindingWrapper, 'blindVersionPubkey'>
37+
| MakeActionCall<BlindingWrapper, 'blindVersionSign'>;
38+
}

types/blinding/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// <reference path="../../shared.d.ts" />
2+
/// <reference path="./blinding.d.ts" />

0 commit comments

Comments
 (0)