@openwallet-foundation/askar-react-native 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. package/LICENSE +191 -0
  2. package/README.md +38 -0
  3. package/android/CMakeLists.txt +60 -0
  4. package/android/build.gradle +261 -0
  5. package/android/cpp-adapter.cpp +36 -0
  6. package/android/src/main/AndroidManifest.xml +3 -0
  7. package/android/src/main/java/foundation/openwallet/askar/AskarModule.java +51 -0
  8. package/android/src/main/java/foundation/openwallet/askar/AskarPackage.java +23 -0
  9. package/android/src/main/java/id/animo/SecureEnvironment.java +5 -0
  10. package/askar.podspec +31 -0
  11. package/build/askar-react-native/src/NativeBindings.d.ts +144 -0
  12. package/build/askar-react-native/src/NativeBindings.js +3 -0
  13. package/build/askar-react-native/src/NativeBindings.js.map +1 -0
  14. package/build/askar-react-native/src/ReactNativeAskar.d.ts +100 -0
  15. package/build/askar-react-native/src/ReactNativeAskar.js +482 -0
  16. package/build/askar-react-native/src/ReactNativeAskar.js.map +1 -0
  17. package/build/askar-react-native/src/index.d.ts +1 -0
  18. package/build/askar-react-native/src/index.js +31 -0
  19. package/build/askar-react-native/src/index.js.map +1 -0
  20. package/build/askar-react-native/src/serialize.d.ts +14 -0
  21. package/build/askar-react-native/src/serialize.js +50 -0
  22. package/build/askar-react-native/src/serialize.js.map +1 -0
  23. package/build/askar-react-native/src/specs/NativeAskar.d.ts +5 -0
  24. package/build/askar-react-native/src/specs/NativeAskar.js +7 -0
  25. package/build/askar-react-native/src/specs/NativeAskar.js.map +1 -0
  26. package/build/askar-shared/src/askar/Askar.d.ts +437 -0
  27. package/build/askar-shared/src/askar/Askar.js +3 -0
  28. package/build/askar-shared/src/askar/Askar.js.map +1 -0
  29. package/build/askar-shared/src/askar/index.d.ts +2 -0
  30. package/build/askar-shared/src/askar/index.js +19 -0
  31. package/build/askar-shared/src/askar/index.js.map +1 -0
  32. package/build/askar-shared/src/askar/register.d.ts +5 -0
  33. package/build/askar-shared/src/askar/register.js +8 -0
  34. package/build/askar-shared/src/askar/register.js.map +1 -0
  35. package/build/askar-shared/src/crypto/AeadParams.d.ts +9 -0
  36. package/build/askar-shared/src/crypto/AeadParams.js +11 -0
  37. package/build/askar-shared/src/crypto/AeadParams.js.map +1 -0
  38. package/build/askar-shared/src/crypto/CryptoBox.d.ts +24 -0
  39. package/build/askar-shared/src/crypto/CryptoBox.js +23 -0
  40. package/build/askar-shared/src/crypto/CryptoBox.js.map +1 -0
  41. package/build/askar-shared/src/crypto/Ecdh1PU.d.ts +58 -0
  42. package/build/askar-shared/src/crypto/Ecdh1PU.js +77 -0
  43. package/build/askar-shared/src/crypto/Ecdh1PU.js.map +1 -0
  44. package/build/askar-shared/src/crypto/EcdhEs.d.ts +46 -0
  45. package/build/askar-shared/src/crypto/EcdhEs.js +64 -0
  46. package/build/askar-shared/src/crypto/EcdhEs.js.map +1 -0
  47. package/build/askar-shared/src/crypto/EncryptedBuffer.d.ts +20 -0
  48. package/build/askar-shared/src/crypto/EncryptedBuffer.js +36 -0
  49. package/build/askar-shared/src/crypto/EncryptedBuffer.js.map +1 -0
  50. package/build/askar-shared/src/crypto/Jwk.d.ts +18 -0
  51. package/build/askar-shared/src/crypto/Jwk.js +24 -0
  52. package/build/askar-shared/src/crypto/Jwk.js.map +1 -0
  53. package/build/askar-shared/src/crypto/Key.d.ts +73 -0
  54. package/build/askar-shared/src/crypto/Key.js +101 -0
  55. package/build/askar-shared/src/crypto/Key.js.map +1 -0
  56. package/build/askar-shared/src/crypto/handles.d.ts +46 -0
  57. package/build/askar-shared/src/crypto/handles.js +123 -0
  58. package/build/askar-shared/src/crypto/handles.js.map +1 -0
  59. package/build/askar-shared/src/crypto/index.d.ts +8 -0
  60. package/build/askar-shared/src/crypto/index.js +25 -0
  61. package/build/askar-shared/src/crypto/index.js.map +1 -0
  62. package/build/askar-shared/src/enums/EntryOperation.d.ts +5 -0
  63. package/build/askar-shared/src/enums/EntryOperation.js +10 -0
  64. package/build/askar-shared/src/enums/EntryOperation.js.map +1 -0
  65. package/build/askar-shared/src/enums/KeyAlgorithm.d.ts +20 -0
  66. package/build/askar-shared/src/enums/KeyAlgorithm.js +38 -0
  67. package/build/askar-shared/src/enums/KeyAlgorithm.js.map +1 -0
  68. package/build/askar-shared/src/enums/KeyBackend.d.ts +6 -0
  69. package/build/askar-shared/src/enums/KeyBackend.js +24 -0
  70. package/build/askar-shared/src/enums/KeyBackend.js.map +1 -0
  71. package/build/askar-shared/src/enums/KeyMethod.d.ts +4 -0
  72. package/build/askar-shared/src/enums/KeyMethod.js +9 -0
  73. package/build/askar-shared/src/enums/KeyMethod.js.map +1 -0
  74. package/build/askar-shared/src/enums/LogLevel.d.ts +9 -0
  75. package/build/askar-shared/src/enums/LogLevel.js +14 -0
  76. package/build/askar-shared/src/enums/LogLevel.js.map +1 -0
  77. package/build/askar-shared/src/enums/SignatureAlgorithm.d.ts +6 -0
  78. package/build/askar-shared/src/enums/SignatureAlgorithm.js +11 -0
  79. package/build/askar-shared/src/enums/SignatureAlgorithm.js.map +1 -0
  80. package/build/askar-shared/src/enums/index.d.ts +5 -0
  81. package/build/askar-shared/src/enums/index.js +22 -0
  82. package/build/askar-shared/src/enums/index.js.map +1 -0
  83. package/build/askar-shared/src/error.d.ts +12 -0
  84. package/build/askar-shared/src/error.js +21 -0
  85. package/build/askar-shared/src/error.js.map +1 -0
  86. package/build/askar-shared/src/index.d.ts +6 -0
  87. package/build/askar-shared/src/index.js +23 -0
  88. package/build/askar-shared/src/index.js.map +1 -0
  89. package/build/askar-shared/src/migration.d.ts +10 -0
  90. package/build/askar-shared/src/migration.js +11 -0
  91. package/build/askar-shared/src/migration.js.map +1 -0
  92. package/build/askar-shared/src/store/Entry.d.ts +21 -0
  93. package/build/askar-shared/src/store/Entry.js +38 -0
  94. package/build/askar-shared/src/store/Entry.js.map +1 -0
  95. package/build/askar-shared/src/store/EntryList.d.ts +17 -0
  96. package/build/askar-shared/src/store/EntryList.js +40 -0
  97. package/build/askar-shared/src/store/EntryList.js.map +1 -0
  98. package/build/askar-shared/src/store/KeyEntry.d.ts +23 -0
  99. package/build/askar-shared/src/store/KeyEntry.js +40 -0
  100. package/build/askar-shared/src/store/KeyEntry.js.map +1 -0
  101. package/build/askar-shared/src/store/KeyEntryList.d.ts +15 -0
  102. package/build/askar-shared/src/store/KeyEntryList.js +33 -0
  103. package/build/askar-shared/src/store/KeyEntryList.js.map +1 -0
  104. package/build/askar-shared/src/store/OpenSession.d.ts +14 -0
  105. package/build/askar-shared/src/store/OpenSession.js +27 -0
  106. package/build/askar-shared/src/store/OpenSession.js.map +1 -0
  107. package/build/askar-shared/src/store/Scan.d.ts +28 -0
  108. package/build/askar-shared/src/store/Scan.js +67 -0
  109. package/build/askar-shared/src/store/Scan.js.map +1 -0
  110. package/build/askar-shared/src/store/Session.d.ts +86 -0
  111. package/build/askar-shared/src/store/Session.js +182 -0
  112. package/build/askar-shared/src/store/Session.js.map +1 -0
  113. package/build/askar-shared/src/store/Store.d.ts +58 -0
  114. package/build/askar-shared/src/store/Store.js +87 -0
  115. package/build/askar-shared/src/store/Store.js.map +1 -0
  116. package/build/askar-shared/src/store/StoreKeyMethod.d.ts +11 -0
  117. package/build/askar-shared/src/store/StoreKeyMethod.js +20 -0
  118. package/build/askar-shared/src/store/StoreKeyMethod.js.map +1 -0
  119. package/build/askar-shared/src/store/index.d.ts +9 -0
  120. package/build/askar-shared/src/store/index.js +26 -0
  121. package/build/askar-shared/src/store/index.js.map +1 -0
  122. package/cpp/HostObject.cpp +198 -0
  123. package/cpp/HostObject.h +23 -0
  124. package/cpp/askar.cpp +1118 -0
  125. package/cpp/askar.h +111 -0
  126. package/cpp/include/libaries_askar.h +635 -0
  127. package/cpp/turboModuleUtility.cpp +598 -0
  128. package/cpp/turboModuleUtility.h +62 -0
  129. package/ios/Askar.h +5 -0
  130. package/ios/Askar.mm +33 -0
  131. package/package.json +59 -0
@@ -0,0 +1,598 @@
1
+ #include <vector>
2
+
3
+ #include "turboModuleUtility.h"
4
+
5
+ namespace askarTurboModuleUtility {
6
+
7
+ using byteVector = std::vector<uint8_t>;
8
+
9
+ std::shared_ptr<react::CallInvoker> invoker;
10
+
11
+ void registerTurboModule(jsi::Runtime &rt,
12
+ std::shared_ptr<react::CallInvoker> jsCallInvoker) {
13
+ // Setting the callInvoker for async code
14
+ invoker = jsCallInvoker;
15
+ // Create a TurboModuleRustHostObject
16
+ auto instance = std::make_shared<AskarTurboModuleHostObject>(rt);
17
+ // Create a JS equivalent object of the instance
18
+ jsi::Object jsInstance = jsi::Object::createFromHostObject(rt, instance);
19
+ // Register the object on global
20
+ rt.global().setProperty(rt, "_askar", std::move(jsInstance));
21
+ }
22
+
23
+ void assertValueIsObject(jsi::Runtime &rt, const jsi::Value *val) {
24
+ val->asObject(rt);
25
+ }
26
+
27
+ template <>
28
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
29
+ const char **value) {
30
+ auto object = jsi::Object(rt);
31
+
32
+ if (code == ErrorCode::Success) {
33
+ auto isNullptr = value == nullptr || *value == nullptr;
34
+ auto valueWithoutNullptr = isNullptr
35
+ ? jsi::Value::null()
36
+ : jsi::String::createFromAscii(rt, *value);
37
+ object.setProperty(rt, "value", valueWithoutNullptr);
38
+ }
39
+
40
+ object.setProperty(rt, "errorCode", int(code));
41
+
42
+ return object;
43
+ }
44
+
45
+ template <>
46
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
47
+ const char *const *value) {
48
+ auto object = jsi::Object(rt);
49
+
50
+ if (code == ErrorCode::Success) {
51
+ auto isNullptr = value == nullptr || *value == nullptr;
52
+ auto valueWithoutNullptr = isNullptr
53
+ ? jsi::Value::null()
54
+ : jsi::String::createFromAscii(rt, *value);
55
+ object.setProperty(rt, "value", valueWithoutNullptr);
56
+ }
57
+
58
+ object.setProperty(rt, "errorCode", int(code));
59
+
60
+ return object;
61
+ }
62
+
63
+ template <>
64
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
65
+ nullptr_t value) {
66
+ auto object = jsi::Object(rt);
67
+
68
+ if (code == ErrorCode::Success) {
69
+ object.setProperty(rt, "value", jsi::Value::null());
70
+ }
71
+
72
+ object.setProperty(rt, "errorCode", int(code));
73
+
74
+ return object;
75
+ }
76
+
77
+ template <>
78
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code, int8_t *value) {
79
+ auto object = jsi::Object(rt);
80
+
81
+ if (code == ErrorCode::Success) {
82
+ auto valueWithoutNullptr =
83
+ value == nullptr ? jsi::Value::null() : jsi::Value(rt, int(*value));
84
+ object.setProperty(rt, "value", valueWithoutNullptr);
85
+ }
86
+
87
+ object.setProperty(rt, "errorCode", int(code));
88
+
89
+ return object;
90
+ }
91
+
92
+ template <>
93
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
94
+ int8_t const *value) {
95
+ auto object = jsi::Object(rt);
96
+
97
+ if (code == ErrorCode::Success) {
98
+ auto valueWithoutNullptr =
99
+ value == nullptr ? jsi::Value::null() : jsi::Value(rt, int(*value));
100
+ object.setProperty(rt, "value", valueWithoutNullptr);
101
+ }
102
+
103
+ object.setProperty(rt, "errorCode", int(code));
104
+
105
+ return object;
106
+ }
107
+
108
+ template <>
109
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code, int32_t *value) {
110
+ auto object = jsi::Object(rt);
111
+
112
+ if (code == ErrorCode::Success) {
113
+ auto valueWithoutNullptr =
114
+ value == nullptr ? jsi::Value::null() : jsi::Value(rt, int(*value));
115
+ object.setProperty(rt, "value", valueWithoutNullptr);
116
+ }
117
+
118
+ object.setProperty(rt, "errorCode", int(code));
119
+
120
+ return object;
121
+ }
122
+
123
+ template <>
124
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code, int64_t *value) {
125
+ auto object = jsi::Object(rt);
126
+
127
+ if (code == ErrorCode::Success) {
128
+ auto valueWithoutNullptr =
129
+ value == nullptr ? jsi::Value::null() : jsi::Value(rt, int(*value));
130
+ object.setProperty(rt, "value", valueWithoutNullptr);
131
+ }
132
+
133
+ object.setProperty(rt, "errorCode", int(code));
134
+
135
+ return object;
136
+ }
137
+
138
+ template <>
139
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
140
+ int64_t const *value) {
141
+ auto object = jsi::Object(rt);
142
+
143
+ if (code == ErrorCode::Success) {
144
+ auto valueWithoutNullptr =
145
+ value == nullptr ? jsi::Value::null() : jsi::Value(rt, int(*value));
146
+ object.setProperty(rt, "value", valueWithoutNullptr);
147
+ }
148
+
149
+ object.setProperty(rt, "errorCode", int(code));
150
+
151
+ return object;
152
+ }
153
+
154
+ template <>
155
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
156
+ size_t const *value) {
157
+ auto object = jsi::Object(rt);
158
+
159
+ if (code == ErrorCode::Success) {
160
+ auto valueWithoutNullptr =
161
+ value == nullptr ? jsi::Value::null() : jsi::Value(rt, int(*value));
162
+ object.setProperty(rt, "value", valueWithoutNullptr);
163
+ }
164
+
165
+ object.setProperty(rt, "errorCode", int(code));
166
+
167
+ return object;
168
+ }
169
+
170
+ template <>
171
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
172
+ std::string value) {
173
+ auto object = jsi::Object(rt);
174
+
175
+ if (code == ErrorCode::Success) {
176
+ object.setProperty(rt, "value", jsi::String::createFromAscii(rt, value));
177
+ }
178
+
179
+ object.setProperty(rt, "errorCode", int(code));
180
+
181
+ return object;
182
+ }
183
+
184
+ template <>
185
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
186
+ SecretBuffer *value) {
187
+ auto object = jsi::Object(rt);
188
+
189
+ if (code == ErrorCode::Success) {
190
+ auto valueWithoutNullptr = value == nullptr
191
+ ? jsi::Value::null()
192
+ : secretBufferToArrayBuffer(rt, *value);
193
+ object.setProperty(rt, "value", valueWithoutNullptr);
194
+ }
195
+
196
+ object.setProperty(rt, "errorCode", int(code));
197
+
198
+ return object;
199
+ }
200
+
201
+ template <>
202
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
203
+ LocalKeyHandle *value) {
204
+ auto isNullptr = value == nullptr || value->_0 == nullptr;
205
+ return isNullptr
206
+ ? createReturnValue(rt, code, nullptr)
207
+ : createReturnValue(rt, code, std::to_string(int64_t(intptr_t(value->_0))));
208
+ }
209
+
210
+ template <>
211
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
212
+ EntryListHandle const *value) {
213
+ auto isNullptr = value == nullptr || value->_0 == nullptr;
214
+ return isNullptr
215
+ ? createReturnValue(rt, code, nullptr)
216
+ : createReturnValue(rt, code, std::to_string(int64_t(intptr_t(value->_0))));
217
+ }
218
+
219
+ template <>
220
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
221
+ KeyEntryListHandle const *value) {
222
+ auto isNullptr = value == nullptr || value->_0 == nullptr;
223
+ return isNullptr
224
+ ? createReturnValue(rt, code, nullptr)
225
+ : createReturnValue(rt, code, std::to_string(int64_t(intptr_t(value->_0))));
226
+ }
227
+
228
+ template <>
229
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
230
+ StringListHandle const *value) {
231
+ auto isNullptr = value == nullptr || value->_0 == nullptr;
232
+ return isNullptr
233
+ ? createReturnValue(rt, code, nullptr)
234
+ : createReturnValue(rt, code, std::to_string(int64_t(intptr_t(value->_0))));
235
+ }
236
+
237
+ template <>
238
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
239
+ EncryptedBuffer *value) {
240
+ auto object = jsi::Object(rt);
241
+
242
+ if (code == ErrorCode::Success) {
243
+ if (value == nullptr) {
244
+ object.setProperty(rt, "value", jsi::Value::null());
245
+ } else {
246
+ auto valueObject = jsi::Object(rt);
247
+
248
+ valueObject.setProperty(rt, "buffer",
249
+ secretBufferToArrayBuffer(rt, value->buffer));
250
+ valueObject.setProperty(rt, "tagPos", int(value->tag_pos));
251
+ valueObject.setProperty(rt, "noncePos", int(value->nonce_pos));
252
+
253
+ object.setProperty(rt, "value", valueObject);
254
+ }
255
+ }
256
+
257
+ object.setProperty(rt, "errorCode", int(code));
258
+
259
+ return object;
260
+ }
261
+
262
+ template <>
263
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code,
264
+ AeadParams *value) {
265
+ auto object = jsi::Object(rt);
266
+
267
+ if (code == ErrorCode::Success) {
268
+ if (value == nullptr) {
269
+ object.setProperty(rt, "value", jsi::Value::null());
270
+ } else {
271
+ auto valueObject = jsi::Object(rt);
272
+
273
+ valueObject.setProperty(rt, "nonceLength", int(value->nonce_length));
274
+ valueObject.setProperty(rt, "tagLength", int(value->tag_length));
275
+
276
+ object.setProperty(rt, "value", object);
277
+ }
278
+ }
279
+
280
+ object.setProperty(rt, "errorCode", int(code));
281
+
282
+ return object;
283
+ }
284
+
285
+ void callback(CallbackId result, ErrorCode code) {
286
+ invoker->invokeAsync([result, code]() {
287
+ State *_state = reinterpret_cast<State *>(result);
288
+ State *state = static_cast<State *>(_state);
289
+ jsi::Function *cb = &state->cb;
290
+ jsi::Runtime *rt = reinterpret_cast<jsi::Runtime *>(state->rt);
291
+
292
+ auto object = jsi::Object(*rt);
293
+ object.setProperty(*rt, "errorCode", int(code));
294
+ cb->call(*rt, object);
295
+ });
296
+ // delete state;
297
+ }
298
+
299
+ // Session, Store and Scan Handle
300
+ template <>
301
+ void callbackWithResponse(CallbackId result, ErrorCode code, size_t response) {
302
+ invoker->invokeAsync([result, code, response]() {
303
+ State *_state = reinterpret_cast<State *>(result);
304
+ State *state = static_cast<State *>(_state);
305
+ jsi::Function *cb = &state->cb;
306
+ jsi::Runtime *rt = reinterpret_cast<jsi::Runtime *>(state->rt);
307
+
308
+ auto out = createReturnValue(*rt, code, &response);
309
+ cb->call(*rt, out);
310
+ });
311
+ }
312
+
313
+ template <>
314
+ void callbackWithResponse(CallbackId result, ErrorCode code,
315
+ const char *response) {
316
+ invoker->invokeAsync([result, code, response]() {
317
+ State *_state = reinterpret_cast<State *>(result);
318
+ State *state = static_cast<State *>(_state);
319
+ jsi::Function *cb = &state->cb;
320
+ jsi::Runtime *rt = reinterpret_cast<jsi::Runtime *>(state->rt);
321
+
322
+ auto out = createReturnValue(*rt, code, &response);
323
+ cb->call(*rt, out);
324
+ });
325
+ }
326
+
327
+ template <>
328
+ void callbackWithResponse(CallbackId result, ErrorCode code,
329
+ EntryListHandle response) {
330
+ invoker->invokeAsync([result, code, response]() {
331
+ State *_state = reinterpret_cast<State *>(result);
332
+ State *state = static_cast<State *>(_state);
333
+ jsi::Function *cb = &state->cb;
334
+ jsi::Runtime *rt = reinterpret_cast<jsi::Runtime *>(state->rt);
335
+
336
+ auto out = createReturnValue(*rt, code, &response);
337
+ cb->call(*rt, out);
338
+ });
339
+ }
340
+
341
+ template <>
342
+ void callbackWithResponse(CallbackId result, ErrorCode code,
343
+ KeyEntryListHandle response) {
344
+ invoker->invokeAsync([result, code, response]() {
345
+ State *_state = reinterpret_cast<State *>(result);
346
+ State *state = static_cast<State *>(_state);
347
+ jsi::Function *cb = &state->cb;
348
+ jsi::Runtime *rt = reinterpret_cast<jsi::Runtime *>(state->rt);
349
+
350
+ auto out = createReturnValue(*rt, code, &response);
351
+ cb->call(*rt, out);
352
+ });
353
+ }
354
+
355
+ template <>
356
+ void callbackWithResponse(CallbackId result, ErrorCode code,
357
+ StringListHandle response) {
358
+ invoker->invokeAsync([result, code, response]() {
359
+ State *_state = reinterpret_cast<State *>(result);
360
+ State *state = static_cast<State *>(_state);
361
+ jsi::Function *cb = &state->cb;
362
+ jsi::Runtime *rt = reinterpret_cast<jsi::Runtime *>(state->rt);
363
+
364
+ auto out = createReturnValue(*rt, code, &response);
365
+ cb->call(*rt, out);
366
+ });
367
+ }
368
+
369
+ template <>
370
+ void callbackWithResponse(CallbackId result, ErrorCode code, int8_t response) {
371
+ invoker->invokeAsync([result, code, response]() {
372
+ State *_state = reinterpret_cast<State *>(result);
373
+ State *state = static_cast<State *>(_state);
374
+ jsi::Function *cb = &state->cb;
375
+ jsi::Runtime *rt = reinterpret_cast<jsi::Runtime *>(state->rt);
376
+
377
+ auto out = createReturnValue(*rt, code, &response);
378
+ cb->call(*rt, out);
379
+ });
380
+ }
381
+
382
+ template <>
383
+ void callbackWithResponse(CallbackId result, ErrorCode code, int64_t response) {
384
+ invoker->invokeAsync([result, code, response]() {
385
+ State *_state = reinterpret_cast<State *>(result);
386
+ State *state = static_cast<State *>(_state);
387
+ jsi::Function *cb = &state->cb;
388
+ jsi::Runtime *rt = reinterpret_cast<jsi::Runtime *>(state->rt);
389
+
390
+ auto out = createReturnValue(*rt, code, &response);
391
+ cb->call(*rt, out);
392
+ });
393
+ }
394
+
395
+ template <>
396
+ uint8_t jsiToValue(jsi::Runtime &rt, jsi::Object &options, const char *name,
397
+ bool optional) {
398
+ jsi::Value value = options.getProperty(rt, name);
399
+ if ((value.isNull() || value.isUndefined()) && optional)
400
+ return 0;
401
+
402
+ if (value.isNumber())
403
+ return value.asNumber();
404
+
405
+ throw jsi::JSError(rt, errorPrefix + name + errorInfix + "number");
406
+ };
407
+
408
+ template <>
409
+ int8_t jsiToValue(jsi::Runtime &rt, jsi::Object &options, const char *name,
410
+ bool optional) {
411
+ jsi::Value value = options.getProperty(rt, name);
412
+ if ((value.isNull() || value.isUndefined()) && optional)
413
+ return 0;
414
+
415
+ if (value.isNumber())
416
+ return value.asNumber();
417
+
418
+ throw jsi::JSError(rt, errorPrefix + name + errorInfix + "number");
419
+ };
420
+
421
+ template <>
422
+ std::string jsiToValue<std::string>(jsi::Runtime &rt, jsi::Object &options,
423
+ const char *name, bool optional) {
424
+ jsi::Value value = options.getProperty(rt, name);
425
+
426
+ if ((value.isNull() || value.isUndefined()) && optional)
427
+ return std::string();
428
+
429
+ if (value.isString()) {
430
+ auto x = value.asString(rt).utf8(rt);
431
+ return x;
432
+ }
433
+
434
+ throw jsi::JSError(rt, errorPrefix + name + errorInfix + "string");
435
+ }
436
+
437
+ template <>
438
+ int64_t jsiToValue(jsi::Runtime &rt, jsi::Object &options, const char *name,
439
+ bool optional) {
440
+ jsi::Value value = options.getProperty(rt, name);
441
+ if ((value.isNull() || value.isUndefined()) && optional)
442
+ return 0;
443
+
444
+ if (value.isNumber())
445
+ return value.asNumber();
446
+
447
+ throw jsi::JSError(rt, errorPrefix + name + errorInfix + "number");
448
+ };
449
+
450
+ template <>
451
+ uint64_t jsiToValue(jsi::Runtime &rt, jsi::Object &options, const char *name,
452
+ bool optional) {
453
+ jsi::Value value = options.getProperty(rt, name);
454
+ if ((value.isNull() || value.isUndefined()) && optional)
455
+ return 0;
456
+
457
+ if (value.isNumber())
458
+ return value.asNumber();
459
+
460
+ throw jsi::JSError(rt, errorPrefix + name + errorInfix + "number");
461
+ };
462
+
463
+ template <>
464
+ int32_t jsiToValue(jsi::Runtime &rt, jsi::Object &options, const char *name,
465
+ bool optional) {
466
+ jsi::Value value = options.getProperty(rt, name);
467
+ if ((value.isNull() || value.isUndefined()) && optional)
468
+ return 0;
469
+
470
+ if (value.isNumber())
471
+ return value.asNumber();
472
+
473
+ throw jsi::JSError(rt, errorPrefix + name + errorInfix + "number");
474
+ };
475
+
476
+ template <>
477
+ uint32_t jsiToValue(jsi::Runtime &rt, jsi::Object &options, const char *name,
478
+ bool optional) {
479
+ jsi::Value value = options.getProperty(rt, name);
480
+ if ((value.isNull() || value.isUndefined()) && optional)
481
+ return 0;
482
+
483
+ if (value.isNumber())
484
+ return value.asNumber();
485
+
486
+ throw jsi::JSError(rt, errorPrefix + name + errorInfix + "number");
487
+ };
488
+
489
+ template <>
490
+ KeyEntryListHandle jsiToValue(jsi::Runtime &rt, jsi::Object &options,
491
+ const char *name, bool optional) {
492
+ std::string handle = jsiToValue<std::string>(rt, options, name, optional);
493
+ FfiKeyEntryList *ffiKeyEntryListPtr =
494
+ reinterpret_cast<FfiKeyEntryList *>(std::stol(handle));
495
+ KeyEntryListHandle keyEntryListHandle =
496
+ KeyEntryListHandle{._0 = ffiKeyEntryListPtr};
497
+
498
+ return keyEntryListHandle;
499
+ };
500
+
501
+ template <>
502
+ EntryListHandle jsiToValue(jsi::Runtime &rt, jsi::Object &options,
503
+ const char *name, bool optional) {
504
+ std::string handle = jsiToValue<std::string>(rt, options, name, optional);
505
+ FfiEntryList *ffiEntryListPtr =
506
+ reinterpret_cast<FfiEntryList *>(std::stol(handle));
507
+ EntryListHandle entryListHandle = EntryListHandle{._0 = ffiEntryListPtr};
508
+
509
+ return entryListHandle;
510
+ };
511
+
512
+ template <>
513
+ StringListHandle jsiToValue(jsi::Runtime &rt, jsi::Object &options,
514
+ const char *name, bool optional) {
515
+ std::string handle = jsiToValue<std::string>(rt, options, name, optional);
516
+ FfiStringList *ffiStringListPtr =
517
+ reinterpret_cast<FfiStringList *>(std::stol(handle));
518
+ StringListHandle stringListHandle = StringListHandle{._0 = ffiStringListPtr};
519
+
520
+ return stringListHandle;
521
+ };
522
+
523
+ template <>
524
+ LocalKeyHandle jsiToValue(jsi::Runtime &rt, jsi::Object &options,
525
+ const char *name, bool optional) {
526
+ std::string handle = jsiToValue<std::string>(rt, options, name, optional);
527
+ LocalKey *localKeyPtr = reinterpret_cast<LocalKey *>(std::stol(handle));
528
+ LocalKeyHandle localKeyHandle = LocalKeyHandle{._0 = localKeyPtr};
529
+
530
+ return localKeyHandle;
531
+ };
532
+
533
+ template <>
534
+ std::vector<int32_t>
535
+ jsiToValue<std::vector<int32_t>>(jsi::Runtime &rt, jsi::Object &options,
536
+ const char *name, bool optional) {
537
+ jsi::Value value = options.getProperty(rt, name);
538
+
539
+ if (value.isObject() && value.asObject(rt).isArray(rt)) {
540
+ std::vector<int32_t> vec = {};
541
+ jsi::Array arr = value.asObject(rt).asArray(rt);
542
+ size_t length = arr.length(rt);
543
+ for (int i = 0; i < length; i++) {
544
+ jsi::Value element = arr.getValueAtIndex(rt, i);
545
+ if (element.isNumber()) {
546
+ vec.push_back(element.asNumber());
547
+ } else {
548
+ throw jsi::JSError(rt, errorPrefix + name + errorInfix + "number");
549
+ }
550
+ }
551
+ return vec;
552
+ }
553
+
554
+ if (optional)
555
+ return {};
556
+
557
+ throw jsi::JSError(rt, errorPrefix + name + errorInfix + "Array<number>");
558
+ }
559
+
560
+ template <>
561
+ ByteBuffer jsiToValue<ByteBuffer>(jsi::Runtime &rt, jsi::Object &options,
562
+ const char *name, bool optional) {
563
+ jsi::Value value = options.getProperty(rt, name);
564
+
565
+ if (value.isObject() && value.asObject(rt).isArrayBuffer(rt)) {
566
+ jsi::ArrayBuffer arrayBuffer = value.getObject(rt).getArrayBuffer(rt);
567
+ return ByteBuffer{int(arrayBuffer.size(rt)), arrayBuffer.data(rt)};
568
+ }
569
+
570
+ if (optional)
571
+ return ByteBuffer{0, 0};
572
+
573
+ throw jsi::JSError(rt, errorPrefix + name + errorInfix + "Uint8Array");
574
+ }
575
+
576
+ jsi::ArrayBuffer byteBufferToArrayBuffer(jsi::Runtime &rt, ByteBuffer bb) {
577
+ jsi::ArrayBuffer arrayBuffer = rt.global()
578
+ .getPropertyAsFunction(rt, "ArrayBuffer")
579
+ .callAsConstructor(rt, int(bb.len))
580
+ .getObject(rt)
581
+ .getArrayBuffer(rt);
582
+
583
+ memcpy(arrayBuffer.data(rt), bb.data, bb.len);
584
+ return arrayBuffer;
585
+ }
586
+
587
+ jsi::ArrayBuffer secretBufferToArrayBuffer(jsi::Runtime &rt, SecretBuffer sb) {
588
+ jsi::ArrayBuffer arrayBuffer = rt.global()
589
+ .getPropertyAsFunction(rt, "ArrayBuffer")
590
+ .callAsConstructor(rt, int(sb.len))
591
+ .getObject(rt)
592
+ .getArrayBuffer(rt);
593
+
594
+ memcpy(arrayBuffer.data(rt), sb.data, sb.len);
595
+ return arrayBuffer;
596
+ }
597
+
598
+ } // namespace askarTurboModuleUtility
@@ -0,0 +1,62 @@
1
+ #pragma once
2
+
3
+ #include <ReactCommon/CallInvoker.h>
4
+ #include <jsi/jsi.h>
5
+
6
+ #include "HostObject.h"
7
+ // TODO(rename): when lib has changed
8
+ #include "include/libaries_askar.h"
9
+
10
+ using namespace facebook;
11
+
12
+ namespace askarTurboModuleUtility {
13
+
14
+ static const std::string errorPrefix = "Value `";
15
+ static const std::string errorInfix = "` is not of type ";
16
+
17
+ // state of a callback function
18
+ struct State {
19
+ jsi::Function cb;
20
+ jsi::Runtime *rt;
21
+
22
+ State(jsi::Function *cb_) : cb(std::move(*cb_)) {}
23
+ };
24
+
25
+ // Install the Turbomodule
26
+ void registerTurboModule(jsi::Runtime &rt,
27
+ std::shared_ptr<react::CallInvoker> jsCallInvoker);
28
+
29
+ // Asserts that a jsi::Value is an object and can be safely transformed
30
+ void assertValueIsObject(jsi::Runtime &rt, const jsi::Value *val);
31
+
32
+ // Converts jsi values to regular cpp values
33
+ template <typename T>
34
+ T jsiToValue(jsi::Runtime &rt, jsi::Object &options, const char *name,
35
+ bool optional = false);
36
+
37
+ // Callback function that makes the host function async
38
+ void callback(CallbackId result, ErrorCode code);
39
+
40
+ // Callback function that makes the host function async with response from rust
41
+ // side
42
+ template <typename T>
43
+ void callbackWithResponse(CallbackId result, ErrorCode code, T response);
44
+
45
+ // Instantiate a return object for JS side.
46
+ // ```typescript
47
+ // type ReturnObject = {
48
+ // errorCode: number
49
+ // value?: unknown | null
50
+ // }
51
+ // ```
52
+ //
53
+ // Value will be defined if there is no error.
54
+ // Value will be `null` if there is no value to return
55
+ // Value will be undefined if there is an error, e.g. error code != 0
56
+ template <typename T>
57
+ jsi::Value createReturnValue(jsi::Runtime &rt, ErrorCode code, T out);
58
+
59
+ jsi::ArrayBuffer byteBufferToArrayBuffer(jsi::Runtime &rt, ByteBuffer bb);
60
+ jsi::ArrayBuffer secretBufferToArrayBuffer(jsi::Runtime &rt, SecretBuffer sb);
61
+
62
+ } // namespace askarTurboModuleUtility
package/ios/Askar.h ADDED
@@ -0,0 +1,5 @@
1
+ #import <React/RCTBridgeModule.h>
2
+
3
+ @interface Askar : NSObject <RCTBridgeModule>
4
+
5
+ @end
package/ios/Askar.mm ADDED
@@ -0,0 +1,33 @@
1
+ #import <React/RCTBridge+Private.h>
2
+ #import <jsi/jsi.h>
3
+ #import <React/RCTUtils.h>
4
+ #import <ReactCommon/RCTTurboModule.h>
5
+ #import <askar/turboModuleUtility.h>
6
+
7
+ #import "Askar.h"
8
+
9
+ using namespace facebook;
10
+
11
+ @implementation Askar
12
+
13
+ RCT_EXPORT_MODULE()
14
+
15
+ RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install)
16
+ {
17
+ RCTBridge* bridge = [RCTBridge currentBridge];
18
+ RCTCxxBridge* cxxBridge = (RCTCxxBridge*)bridge;
19
+ if (cxxBridge == nil) {
20
+ return @false;
21
+ }
22
+
23
+ jsi::Runtime* jsiRuntime = (jsi::Runtime*) cxxBridge.runtime;
24
+ if (jsiRuntime == nil) {
25
+ return @false;
26
+ }
27
+
28
+ auto callInvoker = bridge.jsCallInvoker;
29
+ askarTurboModuleUtility::registerTurboModule(*jsiRuntime, callInvoker);
30
+ return @true;
31
+ }
32
+
33
+ @end