Skip to content

Commit bf910a7

Browse files
committed
fix: wrap up requestbody for pro backend
1 parent a7da666 commit bf910a7

File tree

2 files changed

+86
-76
lines changed

2 files changed

+86
-76
lines changed

include/pro/pro.hpp

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,21 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
110110
static_cast<napi_property_attributes>(
111111
napi_writable | napi_configurable)),
112112

113+
// Note: those are not plugged in for now as we do this parsing through zod
114+
// on desktop.
113115
// Pro responses parsing
114-
StaticMethod<&ProWrapper::proProofParseResponse>(
115-
"proProofParseResponse",
116-
static_cast<napi_property_attributes>(
117-
napi_writable | napi_configurable)),
118-
StaticMethod<&ProWrapper::proRevocationsParseResponse>(
119-
"proRevocationsParseResponse",
120-
static_cast<napi_property_attributes>(
121-
napi_writable | napi_configurable)),
122-
StaticMethod<&ProWrapper::proStatusParseResponse>(
123-
"proStatusParseResponse",
124-
static_cast<napi_property_attributes>(
125-
napi_writable | napi_configurable)),
116+
// StaticMethod<&ProWrapper::proProofParseResponse>(
117+
// "proProofParseResponse",
118+
// static_cast<napi_property_attributes>(
119+
// napi_writable | napi_configurable)),
120+
// StaticMethod<&ProWrapper::proRevocationsParseResponse>(
121+
// "proRevocationsParseResponse",
122+
// static_cast<napi_property_attributes>(
123+
// napi_writable | napi_configurable)),
124+
// StaticMethod<&ProWrapper::proStatusParseResponse>(
125+
// "proStatusParseResponse",
126+
// static_cast<napi_property_attributes>(
127+
// napi_writable | napi_configurable)),
126128
});
127129
}
128130

@@ -187,8 +189,8 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
187189
// we expect arguments that match:
188190
// first: {
189191
// "requestVersion": number,
190-
// "masterPrivkey": Uint8Array,
191-
// "rotatingPrivkey": Uint8Array,
192+
// "masterPrivKeyHex": string,
193+
// "rotatingPrivKeyHex": string,
192194
// "unixTsMs": number,
193195
// }
194196

@@ -197,32 +199,35 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
197199
auto env = info.Env();
198200

199201
auto first = info[0].As<Napi::Object>();
200-
201202
if (first.IsEmpty())
202203
throw std::invalid_argument("proProofRequestBody first received empty");
203204

204205
assertIsNumber(first.Get("requestVersion"), "proProofRequestBody.requestVersion");
206+
Napi::Number requestVersion = first.Get("requestVersion").As<Napi::Number>();
205207
assertIsNumber(first.Get("unixTsMs"), "proProofRequestBody.unixTsMs");
206-
auto requestVersion = first.Get("requestVersion").As<Napi::Number>();
207208
auto unix_ts_ms = toCppSysMs(first.Get("unixTsMs"), "proProofRequestBody.unixTsMs");
208209

209-
assertIsUInt8Array(first.Get("masterPrivkey"), "proProofRequestBody.masterPrivkey");
210-
assertIsUInt8Array(first.Get("rotatingPrivkey"), "proProofRequestBody.rotatingPrivkey");
210+
assertIsString(first.Get("masterPrivKeyHex"), "proProofRequestBody.masterPrivKeyHex");
211+
assertIsString(
212+
first.Get("rotatingPrivKeyHex"), "proProofRequestBody.rotatingPrivKeyHex");
211213

212-
auto master_privkey_js = first.Get("masterPrivkey");
213-
auto rotating_privkey_js = first.Get("rotatingPrivkey");
214-
auto master_privkey =
215-
toCppBuffer(master_privkey_js, "proProofRequestBody.masterPrivkey");
216-
auto rotating_privkey =
217-
toCppBuffer(rotating_privkey_js, "proProofRequestBody.rotatingPrivkey");
214+
auto master_privkey_js = first.Get("masterPrivKeyHex");
215+
auto rotating_privkey_js = first.Get("rotatingPrivKeyHex");
216+
std::string master_privkey =
217+
toCppString(master_privkey_js, "proProofRequestBody.masterPrivKeyHex");
218+
std::string rotating_privkey =
219+
toCppString(rotating_privkey_js, "proProofRequestBody.rotatingPrivKeyHex");
220+
221+
assert_length(master_privkey, 64, "masterPrivKeyHex");
222+
assert_length(rotating_privkey, 64, "rotatingPrivkey");
218223

219-
assert_length(master_privkey, 64, "master_privkey");
220-
assert_length(rotating_privkey, 64, "rotating_prevkey");
224+
auto master_privkey_decoded = from_hex(master_privkey);
225+
auto rotating_privkey_decoded = from_hex(rotating_privkey);
221226

222-
auto json = pro_backend::GetProProofRequest::build_to_json(
227+
std::string json = pro_backend::GetProProofRequest::build_to_json(
223228
static_cast<uint8_t>(requestVersion.Int32Value()),
224-
master_privkey,
225-
rotating_privkey,
229+
to_span(master_privkey_decoded),
230+
to_span(rotating_privkey_decoded),
226231
unix_ts_ms);
227232

228233
return json;
@@ -321,7 +326,7 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
321326
// we expect arguments that match:
322327
// first: {
323328
// "requestVersion": number,
324-
// "masterPrivkey": Uint8Array,
329+
// "masterPrivKeyHex": string,
325330
// "unixTsMs": number,
326331
// "withPaymentHistory": boolean,
327332
// }
@@ -343,17 +348,17 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
343348
auto unix_ts_ms = toCppSysMs(first.Get("unixTsMs"), "proStatusRequestBody.unixTsMs");
344349
auto withPaymentHistory = toCppBoolean(
345350
first.Get("withPaymentHistory"), "proStatusRequestBody.withPaymentHistory");
346-
assertIsUInt8Array(first.Get("masterPrivkey"), "proStatusRequestBody.masterPrivkey");
351+
assertIsString(first.Get("masterPrivKeyHex"), "proStatusRequestBody.masterPrivKeyHex");
347352

348-
auto master_privkey_js = first.Get("masterPrivkey");
353+
auto master_privkey_js = first.Get("masterPrivKeyHex");
349354
auto master_privkey =
350-
toCppBuffer(master_privkey_js, "proStatusRequestBody.masterPrivkey");
355+
toCppString(master_privkey_js, "proStatusRequestBody.masterPrivKeyHex");
351356

352-
assert_length(master_privkey, 64, "proStatusRequestBody.master_privkey");
357+
assert_length(master_privkey, 64, "proStatusRequestBody.masterPrivKeyHex");
353358

354359
auto json = pro_backend::GetProStatusRequest::build_to_json(
355360
static_cast<uint8_t>(requestVersion.Int32Value()),
356-
master_privkey,
361+
to_span(from_hex(master_privkey)),
357362
unix_ts_ms,
358363
withPaymentHistory);
359364

types/pro/pro.d.ts

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ declare module 'libsession_util_nodejs' {
1616
type WithProFeatures = { proFeatures: ProFeatures };
1717
type WithGenIndexHash = { genIndexHashB64: string };
1818

19+
type WithRequestVersion = { requestVersion: number };
20+
21+
type WithUnixTsMs = {
22+
unixTsMs: number;
23+
};
24+
1925
type ProProof = WithGenIndexHash & {
2026
version: number;
2127
/**
@@ -25,19 +31,22 @@ declare module 'libsession_util_nodejs' {
2531
expiryMs: number;
2632
};
2733

28-
type ProConfig = {
34+
type WithRotatingPrivKeyHex = {
2935
/**
3036
* 64 bytes, 128 chars
3137
*/
3238
rotatingPrivKeyHex: string;
33-
proProof: ProProof;
3439
};
3540

36-
type WithProBackendResponse = {
37-
status: number;
38-
errors: Array<string>;
41+
type ProConfig = WithRotatingPrivKeyHex & {
42+
proProof: ProProof;
3943
};
4044

45+
// type WithProBackendResponse = {
46+
// status: number;
47+
// errors: Array<string>;
48+
// };
49+
4150
// Must match session-desktop
4251
export enum ProOriginatingPlatform {
4352
Nil = 'Nil',
@@ -74,7 +83,7 @@ declare module 'libsession_util_nodejs' {
7483
expiryUnixTsMs: number;
7584
};
7685

77-
type WithMasterPrivKey = { masterPrivKey: Uint8Array };
86+
type WithMasterPrivKeyHex = { masterPrivKeyHex: string };
7887

7988
type ProPaymentItem = {
8089
/**
@@ -163,47 +172,43 @@ declare module 'libsession_util_nodejs' {
163172
proFeatures: ProFeatures;
164173
}) => WithProFeatures & { success: boolean; error: string | null; codepointCount: number };
165174
proProofRequestBody: (
166-
args: WithMasterPrivKey & {
167-
requestVersion: number;
168-
rotatingPrivkey: Uint8Array;
169-
unixTsMs: number;
170-
}
175+
args: WithMasterPrivKeyHex & WithRequestVersion & WithUnixTsMs & WithRotatingPrivKeyHex
171176
) => string;
172177

173-
proProofParseResponse: (args: {
174-
json: string;
175-
}) => WithProBackendResponse & { proof: ProProof | null };
178+
// proProofParseResponse: (args: {
179+
// json: string;
180+
// }) => WithProBackendResponse & { proof: ProProof | null };
176181

177182
/**
178183
* @param version: Request version. The latest accepted version is 0
179184
* @param ticket: 4-byte monotonic integer for the caller's revocation list iteration. Set to 0 if unknown; otherwise, use the latest known `ticket` from a prior `GetProRevocationsResponse` to allow
180185
the Session Pro Backend to omit the revocation list if it has not changed.
181186
* @returns the stringified body to include in the request
182187
*/
183-
proRevocationsRequestBody: (args: { requestVersion: number; ticket: number }) => string;
188+
proRevocationsRequestBody: (args: WithRequestVersion & { ticket: number }) => string;
184189

185-
proRevocationsParseResponse: (args: { json: string }) => WithProBackendResponse & {
186-
ticket: number | null;
187-
items: Array<ProRevocationItem> | null;
188-
};
190+
// proRevocationsParseResponse: (args: { json: string }) => WithProBackendResponse & {
191+
// ticket: number | null;
192+
// items: Array<ProRevocationItem> | null;
193+
// };
189194

190195
proStatusRequestBody: (
191-
args: WithMasterPrivKey & {
192-
requestVersion: number;
193-
unixTsMs: number;
194-
withPaymentHistory: boolean;
195-
}
196+
args: WithMasterPrivKeyHex &
197+
WithRequestVersion &
198+
WithUnixTsMs & {
199+
withPaymentHistory: boolean;
200+
}
196201
) => string;
197202

198-
proStatusParseResponse: (args: { json: string }) => WithProBackendResponse & {
199-
ticket: number | null;
200-
items: Array<ProPaymentItem>;
201-
userStatus: number;
202-
errorReport: number;
203-
autoRenewing: boolean;
204-
expiryUnixTsMs: number;
205-
gracePeriodDurationMs: number;
206-
};
203+
// proStatusParseResponse: (args: { json: string }) => WithProBackendResponse & {
204+
// ticket: number | null;
205+
// items: Array<ProPaymentItem>;
206+
// userStatus: number;
207+
// errorReport: number;
208+
// autoRenewing: boolean;
209+
// expiryUnixTsMs: number;
210+
// gracePeriodDurationMs: number;
211+
// };
207212
};
208213

209214
export type ProActionsCalls = MakeWrapperActionCalls<ProWrapper>;
@@ -214,11 +219,11 @@ declare module 'libsession_util_nodejs' {
214219
export class ProWrapperNode {
215220
public static proFeaturesForMessage: ProWrapper['proFeaturesForMessage'];
216221
public static proProofRequestBody: ProWrapper['proProofRequestBody'];
217-
public static proProofParseResponse: ProWrapper['proProofParseResponse'];
218-
public static proRevocationRequestBody: ProWrapper['proRevocationsRequestBody'];
219-
public static proRevocationParseResponse: ProWrapper['proRevocationsParseResponse'];
222+
public static proRevocationsRequestBody: ProWrapper['proRevocationsRequestBody'];
220223
public static proStatusRequestBody: ProWrapper['proStatusRequestBody'];
221-
public static proStatusParseResponse: ProWrapper['proStatusParseResponse'];
224+
// public static proProofParseResponse: ProWrapper['proProofParseResponse'];
225+
// public static proRevocationsParseResponse: ProWrapper['proRevocationsParseResponse'];
226+
// public static proStatusParseResponse: ProWrapper['proStatusParseResponse'];
222227
}
223228

224229
/**
@@ -229,9 +234,9 @@ declare module 'libsession_util_nodejs' {
229234
export type ProActionsType =
230235
| MakeActionCall<ProWrapper, 'proFeaturesForMessage'>
231236
| MakeActionCall<ProWrapper, 'proProofRequestBody'>
232-
| MakeActionCall<ProWrapper, 'proProofParseResponse'>
233237
| MakeActionCall<ProWrapper, 'proRevocationsRequestBody'>
234-
| MakeActionCall<ProWrapper, 'proRevocationsParseResponse'>
235-
| MakeActionCall<ProWrapper, 'proStatusRequestBody'>
236-
| MakeActionCall<ProWrapper, 'proStatusParseResponse'>;
238+
| MakeActionCall<ProWrapper, 'proStatusRequestBody'>;
239+
// | MakeActionCall<ProWrapper, 'proProofParseResponse'>
240+
// | MakeActionCall<ProWrapper, 'proRevocationsParseResponse'>
241+
// | MakeActionCall<ProWrapper, 'proStatusParseResponse'>
237242
}

0 commit comments

Comments
 (0)