|
8 | 8 | #include "../../node_modules/node-addon-api/napi.h" |
9 | 9 | #include "../meta/meta_base_wrapper.hpp" |
10 | 10 | #include "../utilities.hpp" |
| 11 | +#include "./types.hpp" |
11 | 12 | #include "meta/meta_base_wrapper.hpp" |
12 | 13 | #include "session/pro_backend.hpp" |
13 | 14 | #include "session/session_protocol.hpp" |
@@ -36,6 +37,26 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> { |
36 | 37 | "proProofRequestBody", |
37 | 38 | static_cast<napi_property_attributes>( |
38 | 39 | 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)), |
39 | 60 | }); |
40 | 61 | } |
41 | 62 |
|
@@ -99,7 +120,7 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> { |
99 | 120 | return wrapResult(info, [&] { |
100 | 121 | // we expect arguments that match: |
101 | 122 | // first: { |
102 | | - // "requestVersion": string, |
| 123 | + // "requestVersion": number, |
103 | 124 | // "masterPrivkey": Uint8Array, |
104 | 125 | // "rotatingPrivkey": Uint8Array, |
105 | 126 | // "unixTsMs": number, |
@@ -141,6 +162,182 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> { |
141 | 162 | return json; |
142 | 163 | }); |
143 | 164 | }; |
| 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 | + }; |
144 | 341 | }; |
145 | 342 |
|
146 | 343 | }; // namespace session::nodeapi |
0 commit comments