Skip to content

Commit dfd0676

Browse files
committed
feat: add pro backend request response wrappers
1 parent 60f407b commit dfd0676

File tree

3 files changed

+246
-2
lines changed

3 files changed

+246
-2
lines changed

include/pro/pro.hpp

Lines changed: 198 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "../../node_modules/node-addon-api/napi.h"
99
#include "../meta/meta_base_wrapper.hpp"
1010
#include "../utilities.hpp"
11+
#include "./types.hpp"
1112
#include "meta/meta_base_wrapper.hpp"
1213
#include "session/pro_backend.hpp"
1314
#include "session/session_protocol.hpp"
@@ -36,6 +37,26 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
3637
"proProofRequestBody",
3738
static_cast<napi_property_attributes>(
3839
napi_writable | napi_configurable)),
40+
StaticMethod<&ProWrapper::proProofResponseBody>(
41+
"proProofResponseBody",
42+
static_cast<napi_property_attributes>(
43+
napi_writable | napi_configurable)),
44+
StaticMethod<&ProWrapper::proRevocationsRequestBody>(
45+
"proRevocationsRequestBody",
46+
static_cast<napi_property_attributes>(
47+
napi_writable | napi_configurable)),
48+
StaticMethod<&ProWrapper::proRevocationsResponseBody>(
49+
"proRevocationsResponseBody",
50+
static_cast<napi_property_attributes>(
51+
napi_writable | napi_configurable)),
52+
StaticMethod<&ProWrapper::proStatusRequestBody>(
53+
"proStatusRequestBody",
54+
static_cast<napi_property_attributes>(
55+
napi_writable | napi_configurable)),
56+
StaticMethod<&ProWrapper::proStatusResponseBody>(
57+
"proStatusResponseBody",
58+
static_cast<napi_property_attributes>(
59+
napi_writable | napi_configurable)),
3960
});
4061
}
4162

@@ -99,7 +120,7 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
99120
return wrapResult(info, [&] {
100121
// we expect arguments that match:
101122
// first: {
102-
// "requestVersion": string,
123+
// "requestVersion": number,
103124
// "masterPrivkey": Uint8Array,
104125
// "rotatingPrivkey": Uint8Array,
105126
// "unixTsMs": number,
@@ -141,6 +162,182 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
141162
return json;
142163
});
143164
};
165+
166+
static Napi::Value proProofResponseBody(const Napi::CallbackInfo& info) {
167+
return wrapResult(info, [&] {
168+
// we expect arguments that match:
169+
// first: {
170+
// "json": string,
171+
// }
172+
173+
assertInfoLength(info, 1);
174+
assertIsObject(info[0]);
175+
auto env = info.Env();
176+
177+
auto first = info[0].As<Napi::Object>();
178+
179+
if (first.IsEmpty())
180+
throw std::invalid_argument("proProofResponseBody first received empty");
181+
182+
assertIsString(first.Get("json"), "proProofResponseBody.jsonStr");
183+
auto json_str = toCppString(first.Get("json"), "proProofResponseBody.jsonStr");
184+
auto json = pro_backend::AddProPaymentOrGetProProofResponse::parse(json_str);
185+
186+
auto obj = Napi::Object::New(env);
187+
188+
obj["status"] = toJs(env, json.status);
189+
obj["errors"] = toJs(env, json.errors);
190+
obj["proof"] = json.errors.empty() ? toJs(env, json.proof) : env.Null();
191+
192+
return obj;
193+
});
194+
};
195+
196+
static Napi::Value proRevocationsRequestBody(const Napi::CallbackInfo& info) {
197+
return wrapResult(info, [&] {
198+
// we expect arguments that match:
199+
// first: {
200+
// "requestVersion": number,
201+
// "ticket": number,
202+
// }
203+
204+
assertInfoLength(info, 1);
205+
assertIsObject(info[0]);
206+
auto env = info.Env();
207+
208+
auto first = info[0].As<Napi::Object>();
209+
210+
if (first.IsEmpty())
211+
throw std::invalid_argument("proRevocationsRequestBody first received empty");
212+
213+
assertIsNumber(first.Get("requestVersion"), "proRevocationsRequestBody.requestVersion");
214+
assertIsNumber(first.Get("ticket"), "proRevocationsRequestBody.ticket");
215+
auto requestVersion = first.Get("requestVersion").As<Napi::Number>();
216+
auto ticket = first.Get("ticket").As<Napi::Number>();
217+
218+
auto revocationsRequest = pro_backend::GetProRevocationsRequest{
219+
.version = static_cast<uint8_t>(requestVersion.Int32Value()),
220+
.ticket = ticket.Uint32Value(),
221+
};
222+
223+
auto json = revocationsRequest.to_json();
224+
225+
return json;
226+
});
227+
};
228+
229+
static Napi::Value proRevocationsResponseBody(const Napi::CallbackInfo& info) {
230+
return wrapResult(info, [&] {
231+
// we expect arguments that match:
232+
// first: {
233+
// "json": string,
234+
// }
235+
236+
assertInfoLength(info, 1);
237+
assertIsObject(info[0]);
238+
auto env = info.Env();
239+
240+
auto first = info[0].As<Napi::Object>();
241+
242+
if (first.IsEmpty())
243+
throw std::invalid_argument("proRevocationsResponseBody first received empty");
244+
245+
assertIsString(first.Get("json"), "proRevocationsResponseBody.jsonStr");
246+
auto json_str = toCppString(first.Get("json"), "proRevocationsResponseBody.jsonStr");
247+
auto json = pro_backend::GetProRevocationsResponse::parse(json_str);
248+
249+
auto obj = Napi::Object::New(env);
250+
251+
obj["status"] = toJs(env, json.status);
252+
obj["errors"] = toJs(env, json.errors);
253+
obj["ticket"] = json.errors.empty() ? toJs(env, json.ticket) : env.Null();
254+
obj["items"] = json.errors.empty() ? toJs(env, json.items) : env.Null();
255+
256+
return obj;
257+
});
258+
};
259+
260+
static Napi::Value proStatusRequestBody(const Napi::CallbackInfo& info) {
261+
return wrapResult(info, [&] {
262+
// we expect arguments that match:
263+
// first: {
264+
// "requestVersion": number,
265+
// "masterPrivkey": Uint8Array,
266+
// "unixTsMs": number,
267+
// "withPaymentHistory": boolean,
268+
// }
269+
270+
assertInfoLength(info, 1);
271+
assertIsObject(info[0]);
272+
auto env = info.Env();
273+
274+
auto first = info[0].As<Napi::Object>();
275+
276+
if (first.IsEmpty())
277+
throw std::invalid_argument("proStatusRequestBody first received empty");
278+
279+
assertIsNumber(first.Get("requestVersion"), "proStatusRequestBody.requestVersion");
280+
assertIsNumber(first.Get("unixTsMs"), "proStatusRequestBody.unixTsMs");
281+
assertIsBoolean(
282+
first.Get("withPaymentHistory"), "proStatusRequestBody.withPaymentHistory");
283+
auto requestVersion = first.Get("requestVersion").As<Napi::Number>();
284+
auto unix_ts_ms = toCppSysMs(first.Get("unixTsMs"), "proStatusRequestBody.unixTsMs");
285+
auto withPaymentHistory = toCppBoolean(
286+
first.Get("withPaymentHistory"), "proStatusRequestBody.withPaymentHistory");
287+
assertIsUInt8Array(first.Get("masterPrivkey"), "proStatusRequestBody.masterPrivkey");
288+
289+
auto master_privkey_js = first.Get("masterPrivkey");
290+
auto master_privkey =
291+
toCppBuffer(master_privkey_js, "proStatusRequestBody.masterPrivkey");
292+
293+
assert_length(master_privkey, 64, "master_privkey");
294+
295+
auto json = pro_backend::GetProStatusRequest::build_to_json(
296+
static_cast<uint8_t>(requestVersion.Int32Value()),
297+
master_privkey,
298+
unix_ts_ms,
299+
withPaymentHistory);
300+
301+
return json;
302+
});
303+
};
304+
305+
static Napi::Value proStatusResponseBody(const Napi::CallbackInfo& info) {
306+
return wrapResult(info, [&] {
307+
// we expect arguments that match:
308+
// first: {
309+
// "json": string,
310+
// }
311+
312+
assertInfoLength(info, 1);
313+
assertIsObject(info[0]);
314+
auto env = info.Env();
315+
316+
auto first = info[0].As<Napi::Object>();
317+
318+
if (first.IsEmpty())
319+
throw std::invalid_argument("proStatusResponseBody first received empty");
320+
321+
assertIsString(first.Get("json"), "proStatusResponseBody.jsonStr");
322+
auto json_str = toCppString(first.Get("json"), "proStatusResponseBody.jsonStr");
323+
auto json = pro_backend::GetProStatusResponse::parse(json_str);
324+
325+
auto obj = Napi::Object::New(env);
326+
327+
obj["status"] = toJs(env, json.status);
328+
obj["errors"] = toJs(env, json.errors);
329+
obj["items"] = json.errors.empty() ? toJs(env, json.items) : env.Null();
330+
obj["userStatus"] = json.errors.empty() ? toJs(env, json.user_status) : env.Null();
331+
obj["errorReport"] = json.errors.empty() ? toJs(env, json.error_report) : env.Null();
332+
obj["autoRenewing"] = json.errors.empty() ? toJs(env, json.auto_renewing) : env.Null();
333+
obj["expiryUnixTsMs"] =
334+
json.errors.empty() ? toJs(env, json.expiry_unix_ts_ms) : env.Null();
335+
obj["gracePeriodDurationMs"] =
336+
json.errors.empty() ? toJs(env, json.grace_period_duration_ms) : env.Null();
337+
338+
return obj;
339+
});
340+
};
144341
};
145342

146343
}; // namespace session::nodeapi

include/utilities.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <napi.h>
44

5+
#include <chrono>
56
#include <optional>
67
#include <span>
78
#include <stdexcept>

types/pro/pro.d.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ declare module 'libsession_util_nodejs' {
3333
proProof: ProProof;
3434
};
3535

36+
type WithProBackendResponse = {
37+
status: number;
38+
errors: Array<string>;
39+
}
40+
3641
// Must match session-desktop
3742
export enum ProOriginatingPlatform {
3843
Nil = 'Nil',
@@ -75,12 +80,53 @@ declare module 'libsession_util_nodejs' {
7580
proFeatures: ProFeatures;
7681
}) => WithProFeatures & { success: boolean; error: string | null; codepointCount: number };
7782
proProofRequestBody: (args: {
78-
requestVersion: string,
83+
requestVersion: number,
7984
masterPrivkey: Uint8Array,
8085
rotatingPrivkey: Uint8Array,
8186
unixTsMs: number,
8287
}
8388
) => string;
89+
90+
proProofResponseBody: (args: {
91+
json: string,
92+
}
93+
) => WithProBackendResponse & { proof: ProProof | null };
94+
95+
proRevocationsRequestBody: (args: {
96+
requestVersion: number,
97+
ticket: number,
98+
}
99+
) => string;
100+
101+
proRevocationsResponseBody: (args: {
102+
json: string,
103+
}
104+
) => WithProBackendResponse & {
105+
ticket: number | null;
106+
items: Array<ProRevocationItem>
107+
};
108+
109+
proStatusRequestBody: (args: {
110+
requestVersion: number,
111+
masterPrivkey: Uint8Array,
112+
unixTsMs: number,
113+
withPaymentHistory: boolean,
114+
}
115+
) => string;
116+
117+
proStatusResponseBody: (args: {
118+
json: string,
119+
}
120+
) => WithProBackendResponse & {
121+
ticket: number | null;
122+
items: Array<ProPaymentItem>;
123+
userStatus: number;
124+
errorReport: number;
125+
autoRenewing: boolean;
126+
expiryUnixTsMs: number;
127+
gracePeriodDurationMs: number;
128+
};
129+
84130
};
85131

86132
export type ProActionsCalls = MakeWrapperActionCalls<ProWrapper>;

0 commit comments

Comments
 (0)