Skip to content

Commit 2d542db

Browse files
Optimize JSI to ByteBuffer conversion in RevocationRegistry (#8)
Signed-off-by: AhmedFatthy1040 <[email protected]>
1 parent bce6fd4 commit 2d542db

File tree

2 files changed

+79
-72
lines changed

2 files changed

+79
-72
lines changed

packages/anoncreds-react-native/cpp/anoncreds.cpp

Lines changed: 53 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -120,234 +120,217 @@ ByteBuffer stringToByteBuffer(std::string str) {
120120

121121
jsi::Value revocationRegistryDefinitionFromJson(jsi::Runtime &rt,
122122
jsi::Object options) {
123-
auto json = jsiToValue<std::string>(rt, options, "json");
123+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
124124

125125
ObjectHandle out;
126126

127-
ByteBuffer b = stringToByteBuffer(json);
128-
ErrorCode code = anoncreds_revocation_registry_definition_from_json(b, &out);
129-
127+
ErrorCode code = anoncreds_revocation_registry_definition_from_json(json, &out);
130128
auto returnValue = createReturnValue(rt, code, &out);
131129

132-
// free data
133-
delete[] b.data;
130+
// Free memory
131+
delete[] json.data;
134132

135133
return returnValue;
136134
};
137135

138136
jsi::Value revocationRegistryFromJson(jsi::Runtime &rt, jsi::Object options) {
139-
auto json = jsiToValue<std::string>(rt, options, "json");
137+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
140138

141139
ObjectHandle out;
142-
ByteBuffer b = stringToByteBuffer(json);
143140

144-
ErrorCode code = anoncreds_revocation_registry_from_json(b, &out);
141+
ErrorCode code = anoncreds_revocation_registry_from_json(json, &out);
145142
auto returnValue = createReturnValue(rt, code, &out);
146143

147144
// Free memory
148-
delete[] b.data;
145+
delete[] json.data;
149146

150147
return returnValue;
151148
};
152149

153150
jsi::Value revocationStatusListFromJson(jsi::Runtime &rt,
154151
jsi::Object options) {
155-
auto json = jsiToValue<std::string>(rt, options, "json");
152+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
156153

157154
ObjectHandle out;
158155

159-
ByteBuffer b = stringToByteBuffer(json);
160-
ErrorCode code = anoncreds_revocation_status_list_from_json(b, &out);
161-
156+
ErrorCode code = anoncreds_revocation_status_list_from_json(json, &out);
162157
auto returnValue = createReturnValue(rt, code, &out);
163158

164-
// free data
165-
delete[] b.data;
159+
// Free memory
160+
delete[] json.data;
166161

167162
return returnValue;
168163
};
169164

170165

171166
jsi::Value presentationFromJson(jsi::Runtime &rt, jsi::Object options) {
172-
auto json = jsiToValue<std::string>(rt, options, "json");
167+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
173168

174169
ObjectHandle out;
175-
ByteBuffer b = stringToByteBuffer(json);
176170

177-
ErrorCode code = anoncreds_presentation_from_json(b, &out);
171+
ErrorCode code = anoncreds_presentation_from_json(json, &out);
178172
auto returnValue = createReturnValue(rt, code, &out);
179173

180174
// Free memory
181-
delete[] b.data;
175+
delete[] json.data;
182176

183177
return returnValue;
184178
};
185179

186180
jsi::Value presentationRequestFromJson(jsi::Runtime &rt, jsi::Object options) {
187-
auto json = jsiToValue<std::string>(rt, options, "json");
181+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
188182

189183
ObjectHandle out;
190-
ByteBuffer b = stringToByteBuffer(json);
191184

192-
ErrorCode code = anoncreds_presentation_request_from_json(b, &out);
185+
ErrorCode code = anoncreds_presentation_request_from_json(json, &out);
193186
auto returnValue = createReturnValue(rt, code, &out);
194187

195188
// Free memory
196-
delete[] b.data;
189+
delete[] json.data;
197190

198191
return returnValue;
199192
};
200193

201194
jsi::Value credentialOfferFromJson(jsi::Runtime &rt, jsi::Object options) {
202-
auto json = jsiToValue<std::string>(rt, options, "json");
195+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
203196

204197
ObjectHandle out;
205-
ByteBuffer b = stringToByteBuffer(json);
206198

207-
ErrorCode code = anoncreds_credential_offer_from_json(b, &out);
199+
ErrorCode code = anoncreds_credential_offer_from_json(json, &out);
208200
auto returnValue = createReturnValue(rt, code, &out);
209201

210202
// Free memory
211-
delete[] b.data;
203+
delete[] json.data;
212204

213205
return returnValue;
214206
};
215207

216208
jsi::Value schemaFromJson(jsi::Runtime &rt, jsi::Object options) {
217-
auto json = jsiToValue<std::string>(rt, options, "json");
209+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
218210

219211
ObjectHandle out;
220-
ByteBuffer b = stringToByteBuffer(json);
221212

222-
ErrorCode code = anoncreds_schema_from_json(b, &out);
213+
ErrorCode code = anoncreds_schema_from_json(json, &out);
223214
auto returnValue = createReturnValue(rt, code, &out);
224215

225216
// Free memory
226-
delete[] b.data;
217+
delete[] json.data;
227218

228219
return returnValue;
229220
};
230221

231222
jsi::Value credentialRequestFromJson(jsi::Runtime &rt, jsi::Object options) {
232-
auto json = jsiToValue<std::string>(rt, options, "json");
223+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
233224

234225
ObjectHandle out;
235-
ByteBuffer b = stringToByteBuffer(json);
236226

237-
ErrorCode code = anoncreds_credential_request_from_json(b, &out);
227+
ErrorCode code = anoncreds_credential_request_from_json(json, &out);
238228
auto returnValue = createReturnValue(rt, code, &out);
239229

240230
// Free memory
241-
delete[] b.data;
231+
delete[] json.data;
242232

243233
return returnValue;
244234
};
245235

246236
jsi::Value credentialRequestMetadataFromJson(jsi::Runtime &rt,
247237
jsi::Object options) {
248-
auto json = jsiToValue<std::string>(rt, options, "json");
238+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
249239

250240
ObjectHandle out;
251-
ByteBuffer b = stringToByteBuffer(json);
252241

253-
ErrorCode code = anoncreds_credential_request_metadata_from_json(b, &out);
242+
ErrorCode code = anoncreds_credential_request_metadata_from_json(json, &out);
254243
auto returnValue = createReturnValue(rt, code, &out);
255244

256245
// Free memory
257-
delete[] b.data;
246+
delete[] json.data;
258247

259248
return returnValue;
260249
};
261250

262251
jsi::Value credentialFromJson(jsi::Runtime &rt, jsi::Object options) {
263-
auto json = jsiToValue<std::string>(rt, options, "json");
252+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
264253

265254
ObjectHandle out;
266-
ByteBuffer b = stringToByteBuffer(json);
267255

268-
ErrorCode code = anoncreds_credential_from_json(b, &out);
256+
ErrorCode code = anoncreds_credential_from_json(json, &out);
269257
auto returnValue = createReturnValue(rt, code, &out);
270258

271259
// Free memory
272-
delete[] b.data;
260+
delete[] json.data;
273261

274262
return returnValue;
275263
};
276264

277265
jsi::Value revocationRegistryDefinitionPrivateFromJson(jsi::Runtime &rt,
278266
jsi::Object options) {
279-
auto json = jsiToValue<std::string>(rt, options, "json");
267+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
280268

281269
ObjectHandle out;
282-
ByteBuffer b = stringToByteBuffer(json);
283270

284271
ErrorCode code =
285-
anoncreds_revocation_registry_definition_private_from_json(b, &out);
272+
anoncreds_revocation_registry_definition_private_from_json(json, &out);
286273
auto returnValue = createReturnValue(rt, code, &out);
287274

288275
// Free memory
289-
delete[] b.data;
276+
delete[] json.data;
290277

291278
return returnValue;
292279
};
293280

294281
jsi::Value revocationStateFromJson(jsi::Runtime &rt, jsi::Object options) {
295-
auto json = jsiToValue<std::string>(rt, options, "json");
282+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
296283

297284
ObjectHandle out;
298-
ByteBuffer b = stringToByteBuffer(json);
299285

300-
ErrorCode code = anoncreds_revocation_state_from_json(b, &out);
286+
ErrorCode code = anoncreds_revocation_state_from_json(json, &out);
301287
auto returnValue = createReturnValue(rt, code, &out);
302288

303289
// Free memory
304-
delete[] b.data;
290+
delete[] json.data;
305291

306292
return returnValue;
307293
};
308294

309295
jsi::Value credentialDefinitionFromJson(jsi::Runtime &rt, jsi::Object options) {
310-
auto json = jsiToValue<std::string>(rt, options, "json");
296+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
311297

312298
ObjectHandle out;
313-
ByteBuffer b = stringToByteBuffer(json);
314299

315-
ErrorCode code = anoncreds_credential_definition_from_json(b, &out);
300+
ErrorCode code = anoncreds_credential_definition_from_json(json, &out);
316301
auto returnValue = createReturnValue(rt, code, &out);
317302

318303
// Free memory
319-
delete[] b.data;
304+
delete[] json.data;
320305

321306
return returnValue;
322307
};
323308

324309
jsi::Value credentialDefinitionPrivateFromJson(jsi::Runtime &rt,
325310
jsi::Object options) {
326-
auto json = jsiToValue<std::string>(rt, options, "json");
311+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
327312

328313
ObjectHandle out;
329-
ByteBuffer b = stringToByteBuffer(json);
330314

331-
ErrorCode code = anoncreds_credential_definition_private_from_json(b, &out);
315+
ErrorCode code = anoncreds_credential_definition_private_from_json(json, &out);
332316
auto returnValue = createReturnValue(rt, code, &out);
333317

334318
// Free memory
335-
delete[] b.data;
319+
delete[] json.data;
336320

337321
return returnValue;
338322
};
339323

340324
jsi::Value keyCorrectnessProofFromJson(jsi::Runtime &rt, jsi::Object options) {
341-
auto json = jsiToValue<std::string>(rt, options, "json");
325+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
342326

343327
ObjectHandle out;
344-
ByteBuffer b = stringToByteBuffer(json);
345328

346-
ErrorCode code = anoncreds_key_correctness_proof_from_json(b, &out);
329+
ErrorCode code = anoncreds_key_correctness_proof_from_json(json, &out);
347330
auto returnValue = createReturnValue(rt, code, &out);
348331

349332
// Free memory
350-
delete[] b.data;
333+
delete[] json.data;
351334

352335
return returnValue;
353336
};
@@ -683,31 +666,29 @@ jsi::Value revocationRegistryDefinitionGetAttribute(jsi::Runtime &rt,
683666
};
684667

685668
jsi::Value w3cPresentationFromJson(jsi::Runtime &rt, jsi::Object options) {
686-
auto json = jsiToValue<std::string>(rt, options, "json");
669+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
687670

688671
ObjectHandle out;
689-
ByteBuffer b = stringToByteBuffer(json);
690672

691-
ErrorCode code = anoncreds_w3c_presentation_from_json(b, &out);
673+
ErrorCode code = anoncreds_w3c_presentation_from_json(json, &out);
692674
auto returnValue = createReturnValue(rt, code, &out);
693675

694676
// Free memory
695-
delete[] b.data;
677+
delete[] json.data;
696678

697679
return returnValue;
698680
};
699681

700682
jsi::Value w3cCredentialFromJson(jsi::Runtime &rt, jsi::Object options) {
701-
auto json = jsiToValue<std::string>(rt, options, "json");
683+
auto json = jsiToValue<ByteBuffer>(rt, options, "json");
702684

703685
ObjectHandle out;
704-
ByteBuffer b = stringToByteBuffer(json);
705686

706-
ErrorCode code = anoncreds_w3c_credential_from_json(b, &out);
687+
ErrorCode code = anoncreds_w3c_credential_from_json(json, &out);
707688
auto returnValue = createReturnValue(rt, code, &out);
708689

709690
// Free memory
710-
delete[] b.data;
691+
delete[] json.data;
711692

712693
return returnValue;
713694
};

packages/anoncreds-react-native/cpp/turboModuleUtility.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,4 +608,30 @@ jsiToValue<FfiList_FfiNonrevokedIntervalOverride>(jsi::Runtime &rt,
608608
"Array<NonRevokedIntervalOverride>");
609609
}
610610

611+
template <>
612+
ByteBuffer jsiToValue<ByteBuffer>(jsi::Runtime &rt, jsi::Object &options,
613+
const char *name, bool optional) {
614+
ByteBuffer buffer;
615+
616+
if (optional && !options.hasProperty(rt, name)) {
617+
buffer.data = nullptr;
618+
buffer.len = 0;
619+
return buffer;
620+
}
621+
622+
auto value = options.getProperty(rt, name);
623+
if (value.isString()) {
624+
std::string str = value.asString(rt).utf8(rt);
625+
size_t len = str.size();
626+
uint8_t *c = new uint8_t[len + 1];
627+
std::copy(str.begin(), str.end(), c);
628+
c[len] = '\0';
629+
buffer.data = c;
630+
buffer.len = len;
631+
return buffer;
632+
}
633+
634+
throw jsi::JSError(rt, errorPrefix + name + errorInfix + "string");
635+
}
636+
611637
} // namespace anoncredsTurboModuleUtility

0 commit comments

Comments
 (0)