Skip to content

Commit dfd8373

Browse files
committed
feat: create pro backend wrapper for getting pro proofs
1 parent 655738a commit dfd8373

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

include/pro/pro.hpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../meta/meta_base_wrapper.hpp"
1010
#include "../utilities.hpp"
1111
#include "meta/meta_base_wrapper.hpp"
12+
#include "session/pro_backend.hpp"
1213
#include "session/session_protocol.hpp"
1314

1415
namespace session::nodeapi {
@@ -31,6 +32,10 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
3132
"proFeaturesForMessage",
3233
static_cast<napi_property_attributes>(
3334
napi_writable | napi_configurable)),
35+
StaticMethod<&ProWrapper::proProofRequestBody>(
36+
"proProofRequestBody",
37+
static_cast<napi_property_attributes>(
38+
napi_writable | napi_configurable)),
3439
});
3540
}
3641

@@ -89,6 +94,70 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
8994
return obj;
9095
});
9196
};
97+
98+
static Napi::Value proProofRequestBody(const Napi::CallbackInfo& info) {
99+
return wrapResult(info, [&] {
100+
// we expect arguments that match:
101+
// first: {
102+
// "version": string,
103+
// "master_privkey": Uint8Array,
104+
// "rotating_privkey": Uint8Array,
105+
// "unix_ts": number,
106+
// }
107+
108+
assertInfoLength(info, 1);
109+
assertIsObject(info[0]);
110+
auto env = info.Env();
111+
112+
auto first = info[0].As<Napi::Object>();
113+
114+
if (first.IsEmpty())
115+
throw std::invalid_argument("proProofRequestBody first received empty");
116+
117+
assertIsNumber(first.Get("version"), "proProofRequestBody.version");
118+
assertIsNumber(first.Get("unix_ts"), "proProofRequestBody.unix_ts");
119+
auto version = first.Get("version").As<Napi::Number>();
120+
auto unix_ts = toCppSysMs(first.Get("unix_ts"), "proProofRequestBody.unix_ts");
121+
122+
assertIsUInt8Array(first.Get("master_privkey"), "proProofRequestBody.master_privkey");
123+
assertIsUInt8Array(
124+
first.Get("rotating_privkey"), "proProofRequestBody.rotating_privkey");
125+
126+
// stack allocate to the buffer view so the ref doesnt get deleted
127+
auto master_privkey_napi = first.Get("master_privkey");
128+
auto rotating_privkey_napi = first.Get("rotating_privkey");
129+
auto master_privkey =
130+
toCppBufferView(master_privkey_napi, "proProofRequestBody.master_privkey");
131+
auto rotating_privkey =
132+
toCppBuffer(rotating_privkey_napi, "proProofRequestBody.rotating_privkey");
133+
134+
assert(master_privkey.size() == 64);
135+
assert(rotating_privkey.size() == 64);
136+
137+
pro_backend::GetProProofRequest proProofRequest{
138+
.version = static_cast<uint8_t>(version.Int32Value()),
139+
.unix_ts = unix_ts,
140+
};
141+
142+
auto [master_sig, rotating_sig] = proProofRequest.build_sigs(
143+
proProofRequest.version,
144+
master_privkey,
145+
rotating_privkey,
146+
proProofRequest.unix_ts);
147+
148+
assert(master_sig.size() == 64);
149+
assert(rotating_sig.size() == 64);
150+
151+
proProofRequest.master_sig = master_sig;
152+
proProofRequest.rotating_sig = rotating_sig;
153+
memcpy(proProofRequest.master_pkey.data(), master_privkey.data(), 32);
154+
memcpy(proProofRequest.rotating_pkey.data(), rotating_privkey.data(), 32);
155+
156+
auto json = proProofRequest.to_json();
157+
auto json_str = Napi::String::New(env, json);
158+
return json_str;
159+
});
160+
};
92161
};
93162

94163
}; // namespace session::nodeapi

include/utilities.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ auto getStringArgs(const Napi::CallbackInfo& info) {
6060
}
6161

6262
std::string toCppString(Napi::Value x, const std::string& identifier);
63+
std::span<const unsigned char> toCppBufferView(Napi::Value x, const std::string& identifier);
6364
std::vector<unsigned char> toCppBuffer(Napi::Value x, const std::string& identifier);
6465

6566
int64_t toCppInteger(Napi::Value x, const std::string& identifier, bool allowUndefined = false);

0 commit comments

Comments
 (0)