@qntx/wallet-native 0.13.0 → 0.14.0

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 (135) hide show
  1. package/README.md +184 -31
  2. package/WalletNative.podspec +11 -2
  3. package/android/CMakeLists.txt +7 -1
  4. package/android/build.gradle +4 -0
  5. package/android/src/main/AndroidManifest.xml +1 -2
  6. package/android/src/main/cpp/cpp-adapter.cpp +9 -0
  7. package/android/src/main/java/com/margelo/nitro/qntxwallet/CardReader.kt +316 -0
  8. package/android/src/main/java/com/margelo/nitro/qntxwallet/HybridGoogleDriveAuth.kt +142 -0
  9. package/android/src/main/java/com/margelo/nitro/qntxwallet/HybridSecureInputView.kt +25 -0
  10. package/android/src/main/java/com/margelo/nitro/qntxwallet/Keystore.kt +148 -9
  11. package/android/src/main/java/com/margelo/nitro/qntxwallet/SecureInputField.kt +422 -0
  12. package/android/src/main/java/com/margelo/nitro/qntxwallet/VaultAnchor.kt +90 -0
  13. package/android/src/main/java/com/margelo/nitro/qntxwallet/WalletNativePackage.java +10 -0
  14. package/cpp/host/CardReader.hpp +70 -0
  15. package/cpp/host/HybridKernelHost.cpp +90 -15
  16. package/cpp/host/HybridKernelHost.hpp +21 -5
  17. package/cpp/host/KernelFile.cpp +79 -15
  18. package/cpp/host/KernelFile.hpp +17 -6
  19. package/cpp/host/KernelSlot.hpp +103 -0
  20. package/cpp/host/Keystore.hpp +35 -11
  21. package/cpp/host/Lifecycle.cpp +11 -0
  22. package/cpp/host/Lifecycle.hpp +3 -0
  23. package/cpp/host/Passkey.hpp +8 -0
  24. package/cpp/host/SecureInput.cpp +233 -0
  25. package/cpp/host/SecureInput.hpp +103 -0
  26. package/cpp/host/VaultAnchor.cpp +39 -0
  27. package/cpp/host/VaultAnchor.hpp +28 -0
  28. package/cpp/host/VaultDirectory.hpp +1 -1
  29. package/cpp/platform/CardReaderAndroid.cpp +146 -0
  30. package/cpp/platform/CardReaderIos.mm +318 -0
  31. package/cpp/platform/KeystoreAndroid.cpp +48 -1
  32. package/cpp/platform/KeystoreIos.mm +109 -19
  33. package/cpp/platform/PasskeyAndroid.cpp +2 -0
  34. package/cpp/platform/PasskeyIos.mm +20 -18
  35. package/cpp/platform/QntxSecureInputField.h +24 -0
  36. package/cpp/platform/SecureInputAndroid.cpp +110 -0
  37. package/cpp/platform/SecureInputIos.mm +282 -0
  38. package/cpp/platform/VaultAnchorAndroid.cpp +76 -0
  39. package/cpp/platform/VaultAnchorIos.mm +76 -0
  40. package/cpp/runtime/KernelBackends.cpp +35 -28
  41. package/cpp/runtime/KernelBuffer.hpp +2 -5
  42. package/cpp/runtime/KernelCardReader.cpp +385 -0
  43. package/cpp/runtime/KernelCardReader.hpp +27 -0
  44. package/cpp/runtime/KernelGlobals.cpp +176 -63
  45. package/cpp/runtime/KernelGlobals.hpp +13 -7
  46. package/cpp/runtime/KernelJs.cpp +14 -0
  47. package/cpp/runtime/KernelJs.hpp +4 -0
  48. package/cpp/runtime/KernelKeystore.cpp +78 -2
  49. package/cpp/runtime/KernelKeystore.hpp +1 -0
  50. package/cpp/runtime/KernelPasskey.cpp +15 -12
  51. package/cpp/runtime/KernelRuntime.cpp +24 -7
  52. package/cpp/runtime/KernelRuntime.hpp +14 -6
  53. package/cpp/runtime/KernelSecureInput.cpp +154 -0
  54. package/cpp/runtime/KernelSecureInput.hpp +25 -0
  55. package/cpp/runtime/platform/KernelArgon2.cpp +34 -0
  56. package/cpp/runtime/platform/KernelArgon2.hpp +23 -0
  57. package/cpp/vendor/argon2/LICENSE +314 -0
  58. package/cpp/vendor/argon2/README.md +20 -0
  59. package/cpp/vendor/argon2/include/argon2.h +437 -0
  60. package/cpp/vendor/argon2/src/argon2.c +452 -0
  61. package/cpp/vendor/argon2/src/blake2/blake2-impl.h +156 -0
  62. package/cpp/vendor/argon2/src/blake2/blake2.h +89 -0
  63. package/cpp/vendor/argon2/src/blake2/blake2b.c +390 -0
  64. package/cpp/vendor/argon2/src/blake2/blamka-round-ref.h +56 -0
  65. package/cpp/vendor/argon2/src/core.c +648 -0
  66. package/cpp/vendor/argon2/src/core.h +228 -0
  67. package/cpp/vendor/argon2/src/encoding.c +463 -0
  68. package/cpp/vendor/argon2/src/encoding.h +57 -0
  69. package/cpp/vendor/argon2/src/ref.c +194 -0
  70. package/cpp/vendor/argon2/src/thread.c +57 -0
  71. package/cpp/vendor/argon2/src/thread.h +67 -0
  72. package/dist/cloud.d.mts +58 -0
  73. package/dist/cloud.mjs +114 -0
  74. package/dist/cloud.mjs.map +1 -0
  75. package/dist/index.d.mts +13 -4
  76. package/dist/index.mjs +89 -14
  77. package/dist/index.mjs.map +1 -1
  78. package/dist/secure-input.d.mts +24 -0
  79. package/dist/secure-input.mjs +25 -0
  80. package/dist/secure-input.mjs.map +1 -0
  81. package/ios/HybridCloudKitStore.swift +159 -0
  82. package/ios/HybridSecureInputView.swift +35 -0
  83. package/ios/cloud/CloudKitEnvelopeStore.swift +179 -0
  84. package/kernel/generated/kernel-entry.js +42170 -12741
  85. package/kernel/generated/kernel.manifest.json +17 -7
  86. package/nitrogen/generated/android/WalletNative+autolinking.cmake +7 -1
  87. package/nitrogen/generated/android/WalletNativeOnLoad.cpp +37 -2
  88. package/nitrogen/generated/android/c++/JFunc_void_SecureInputState.hpp +77 -0
  89. package/nitrogen/generated/android/c++/JHybridGoogleDriveAuthSpec.cpp +82 -0
  90. package/nitrogen/generated/android/c++/JHybridGoogleDriveAuthSpec.hpp +64 -0
  91. package/nitrogen/generated/android/c++/JHybridSecureInputViewSpec.cpp +70 -0
  92. package/nitrogen/generated/android/c++/JHybridSecureInputViewSpec.hpp +64 -0
  93. package/nitrogen/generated/android/c++/JSecureInputState.hpp +64 -0
  94. package/nitrogen/generated/android/c++/views/JHybridSecureInputViewStateUpdater.cpp +74 -0
  95. package/nitrogen/generated/android/c++/views/JHybridSecureInputViewStateUpdater.hpp +54 -0
  96. package/nitrogen/generated/android/kotlin/com/margelo/nitro/qntxwallet/Func_void_SecureInputState.kt +78 -0
  97. package/nitrogen/generated/android/kotlin/com/margelo/nitro/qntxwallet/HybridGoogleDriveAuthSpec.kt +61 -0
  98. package/nitrogen/generated/android/kotlin/com/margelo/nitro/qntxwallet/HybridSecureInputViewSpec.kt +67 -0
  99. package/nitrogen/generated/android/kotlin/com/margelo/nitro/qntxwallet/SecureInputState.kt +25 -0
  100. package/nitrogen/generated/android/kotlin/com/margelo/nitro/qntxwallet/views/HybridSecureInputViewManager.kt +96 -0
  101. package/nitrogen/generated/android/kotlin/com/margelo/nitro/qntxwallet/views/HybridSecureInputViewStateUpdater.kt +23 -0
  102. package/nitrogen/generated/ios/WalletNative-Swift-Cxx-Bridge.cpp +74 -1
  103. package/nitrogen/generated/ios/WalletNative-Swift-Cxx-Bridge.hpp +252 -3
  104. package/nitrogen/generated/ios/WalletNative-Swift-Cxx-Umbrella.hpp +21 -3
  105. package/nitrogen/generated/ios/WalletNativeAutolinking.mm +17 -1
  106. package/nitrogen/generated/ios/WalletNativeAutolinking.swift +22 -0
  107. package/nitrogen/generated/ios/c++/HybridCloudKitStoreSpecSwift.cpp +11 -0
  108. package/nitrogen/generated/ios/c++/HybridCloudKitStoreSpecSwift.hpp +116 -0
  109. package/nitrogen/generated/ios/c++/HybridSecureInputViewSpecSwift.cpp +11 -0
  110. package/nitrogen/generated/ios/c++/HybridSecureInputViewSpecSwift.hpp +83 -0
  111. package/nitrogen/generated/ios/c++/views/HybridSecureInputViewComponent.mm +144 -0
  112. package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
  113. package/nitrogen/generated/ios/swift/Func_void_SecureInputState.swift +46 -0
  114. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
  115. package/nitrogen/generated/ios/swift/Func_void_std__optional_std__string_.swift +53 -0
  116. package/nitrogen/generated/ios/swift/Func_void_std__vector_std__string_.swift +46 -0
  117. package/nitrogen/generated/ios/swift/HybridCloudKitStoreSpec.swift +59 -0
  118. package/nitrogen/generated/ios/swift/HybridCloudKitStoreSpec_cxx.swift +230 -0
  119. package/nitrogen/generated/ios/swift/HybridSecureInputViewSpec.swift +55 -0
  120. package/nitrogen/generated/ios/swift/HybridSecureInputViewSpec_cxx.swift +164 -0
  121. package/nitrogen/generated/ios/swift/SecureInputState.swift +48 -0
  122. package/nitrogen/generated/shared/c++/HybridCloudKitStoreSpec.cpp +25 -0
  123. package/nitrogen/generated/shared/c++/HybridCloudKitStoreSpec.hpp +70 -0
  124. package/nitrogen/generated/shared/c++/HybridGoogleDriveAuthSpec.cpp +22 -0
  125. package/nitrogen/generated/shared/c++/HybridGoogleDriveAuthSpec.hpp +64 -0
  126. package/nitrogen/generated/shared/c++/HybridKernelHostSpec.cpp +1 -0
  127. package/nitrogen/generated/shared/c++/HybridKernelHostSpec.hpp +1 -0
  128. package/nitrogen/generated/shared/c++/HybridSecureInputViewSpec.cpp +22 -0
  129. package/nitrogen/generated/shared/c++/HybridSecureInputViewSpec.hpp +65 -0
  130. package/nitrogen/generated/shared/c++/SecureInputState.hpp +84 -0
  131. package/nitrogen/generated/shared/c++/views/HybridSecureInputViewComponent.cpp +34 -0
  132. package/nitrogen/generated/shared/c++/views/HybridSecureInputViewComponent.hpp +85 -0
  133. package/nitrogen/generated/shared/json/SecureInputViewConfig.json +10 -0
  134. package/package.json +24 -6
  135. package/scripts/compile-kernel.mjs +7 -3
@@ -1,12 +1,16 @@
1
1
  #include "KernelGlobals.hpp"
2
2
 
3
3
  #include "../host/KernelFile.hpp"
4
+ #include "../host/VaultAnchor.hpp"
4
5
 
5
6
  #include "KernelBackends.hpp"
7
+ #include "KernelCardReader.hpp"
6
8
  #include "KernelKeystore.hpp"
7
9
  #include "KernelPasskey.hpp"
10
+ #include "KernelSecureInput.hpp"
8
11
  #include "KernelBuffer.hpp"
9
12
  #include "KernelJs.hpp"
13
+ #include "platform/KernelArgon2.hpp"
10
14
  #include "platform/KernelPbkdf2.hpp"
11
15
 
12
16
  #include <atomic>
@@ -23,10 +27,12 @@
23
27
 
24
28
  #if defined(__APPLE__) || defined(__ANDROID__)
25
29
  #include <pthread.h>
30
+ #include <time.h>
26
31
  #include <unistd.h>
27
32
  #elif defined(__linux__)
28
33
  #include <cerrno>
29
34
  #include <sys/random.h>
35
+ #include <time.h>
30
36
  #include <unistd.h>
31
37
  #endif
32
38
 
@@ -42,8 +48,38 @@ constexpr size_t kRandomLimit = 65536;
42
48
  constexpr double kTimerDelayMax = 2147483647.0;
43
49
  constexpr double kPbkdf2IterationsMax = 10000000.0;
44
50
  constexpr size_t kPbkdf2LengthMax = 1024;
51
+
52
+ // Argon2id bounds: generous around the one cost the kernel asks for, tight
53
+ // enough that a script cannot make the worker allocate without limit.
54
+ constexpr double kArgon2MemoryKiBMin = 8.0;
55
+ constexpr double kArgon2MemoryKiBMax = 1048576.0;
56
+ constexpr double kArgon2PassesMax = 16.0;
57
+ constexpr double kArgon2LanesMax = 16.0;
58
+ constexpr double kArgon2LengthMin = 4.0;
59
+ constexpr double kArgon2LengthMax = 1024.0;
45
60
  constexpr size_t kKdfQueueLimit = 8;
46
61
 
62
+ // __qntx.elapsed — the session clock behind the 24 h cap. It must keep running
63
+ // while the device is suspended, like the background lock does
64
+ // (cpp/platform/Lifecycle*): steady_clock on Android/Linux is CLOCK_MONOTONIC,
65
+ // which freezes in suspend (LifecycleAndroid.cpp's own comment), so read the
66
+ // suspend-inclusive clock directly. Apple CLOCK_MONOTONIC counts sleep
67
+ // (LifecycleIos.mm relies on the same). Timers stay on steady_clock — a timer
68
+ // delayed by suspend fires on wake, which is the intended policy.
69
+ double elapsedMillis() {
70
+ #if defined(__APPLE__)
71
+ return static_cast<double>(clock_gettime_nsec_np(CLOCK_MONOTONIC)) / 1e6;
72
+ #elif defined(__ANDROID__) || defined(__linux__)
73
+ timespec ts{};
74
+ ::clock_gettime(CLOCK_BOOTTIME, &ts);
75
+ return static_cast<double>(ts.tv_sec) * 1e3 + static_cast<double>(ts.tv_nsec) / 1e6;
76
+ #else
77
+ return std::chrono::duration<double, std::milli>(
78
+ std::chrono::steady_clock::now().time_since_epoch())
79
+ .count();
80
+ #endif
81
+ }
82
+
47
83
  void fillRandom(uint8_t* out, size_t size) {
48
84
  #if defined(__APPLE__) || defined(__ANDROID__)
49
85
  arc4random_buf(out, size);
@@ -64,16 +100,14 @@ void fillRandom(uint8_t* out, size_t size) {
64
100
  #endif
65
101
  }
66
102
 
67
- // One process-wide worker runs every PBKDF2 job, so concurrent kernels cannot
68
- // spawn unbounded derivation threads. The queue is bounded; the worker thread
69
- // is intentionally leaked rather than joined (it parks on the condition
103
+ // One process-wide worker runs every key derivation, so concurrent kernels
104
+ // cannot spawn unbounded derivation threads. The queue is bounded; the worker
105
+ // thread is intentionally leaked rather than joined (it parks on the condition
70
106
  // variable and holds no runtime state).
71
107
  struct KdfJob {
72
- KernelPbkdf2Hash hash;
73
- SecretBytes password;
74
- SecretBytes salt;
75
- uint32_t iterations;
76
- size_t length;
108
+ // Owns its inputs as SecretBytes behind shared_ptr (std::function needs a
109
+ // copyable closure); whichever copy drops last wipes them.
110
+ std::function<SecretBytes()> derive;
77
111
  std::shared_ptr<KernelPendingCall> call;
78
112
  std::shared_ptr<KernelGlobalsState> state;
79
113
  };
@@ -106,12 +140,11 @@ void kdfWorkerRun() {
106
140
  SecretBytes derived;
107
141
  std::string error;
108
142
  try {
109
- derived = kernelPbkdf2(
110
- job.hash, job.password, job.salt, job.iterations, job.length);
143
+ derived = job.derive();
111
144
  } catch (const std::exception& e) {
112
145
  error = e.what();
113
146
  } catch (...) {
114
- error = "unknown pbkdf2 error";
147
+ error = "unknown key derivation error";
115
148
  }
116
149
  if (!job.state->active->load(std::memory_order_acquire)) {
117
150
  // Runtime torn down mid-derivation: drop the job; the parked call's
@@ -177,6 +210,30 @@ bool kdfSubmit(KdfJob job) {
177
210
  return true;
178
211
  }
179
212
 
213
+ // Parks `done` as a pending call and queues the derivation; a full queue
214
+ // answers through `done` on the kernel thread like any other failure.
215
+ void submitDerivation(
216
+ jsi::Runtime& rt,
217
+ const std::shared_ptr<KernelGlobalsState>& state,
218
+ const jsi::Value& done,
219
+ std::function<SecretBytes()> derive) {
220
+ auto call = std::make_shared<KernelPendingCall>();
221
+ call->done = done.asObject(rt).asFunction(rt);
222
+ state->pending.insert(call);
223
+ if (!kdfSubmit(KdfJob{std::move(derive), call, state})) {
224
+ state->enqueueTask([state, call](jsi::Runtime& rt) mutable {
225
+ auto parked = std::move(call->done);
226
+ state->pending.erase(call);
227
+ if (parked.has_value()) {
228
+ parked->call(
229
+ rt,
230
+ jsi::Value::undefined(),
231
+ jsi::Value(rt, jsi::String::createFromAscii(rt, "kdf queue full")));
232
+ }
233
+ });
234
+ }
235
+ }
236
+
180
237
  void installPbkdf2(
181
238
  jsi::Runtime& rt,
182
239
  jsi::Object& qntx,
@@ -205,10 +262,11 @@ void installPbkdf2(
205
262
  }
206
263
  auto passwordView =
207
264
  byteViewFromValue(rt, args[1], "__qntx.pbkdf2 password", intrinsics);
208
- SecretBytes password(passwordView.data, passwordView.size);
265
+ auto password =
266
+ std::make_shared<SecretBytes>(passwordView.data, passwordView.size);
209
267
  auto saltView =
210
268
  byteViewFromValue(rt, args[2], "__qntx.pbkdf2 salt", intrinsics);
211
- SecretBytes salt(saltView.data, saltView.size);
269
+ auto salt = std::make_shared<SecretBytes>(saltView.data, saltView.size);
212
270
  if (!args[5].isObject() || !args[5].asObject(rt).isFunction(rt)) {
213
271
  throw jsi::JSError(
214
272
  rt, "__qntx.pbkdf2 expects (hash, password, salt, iterations, length, done)");
@@ -217,30 +275,45 @@ void installPbkdf2(
217
275
  integerInRange(rt, args[3], "__qntx.pbkdf2 iterations", 1, kPbkdf2IterationsMax));
218
276
  const auto length = static_cast<size_t>(
219
277
  integerInRange(rt, args[4], "__qntx.pbkdf2 length", 1, kPbkdf2LengthMax));
220
- auto call = std::make_shared<KernelPendingCall>();
221
- call->done = args[5].asObject(rt).asFunction(rt);
222
- state->pending.insert(call);
223
-
224
- KdfJob job{
225
- hash,
226
- std::move(password),
227
- std::move(salt),
228
- iterations,
229
- length,
230
- call,
231
- state};
232
- if (!kdfSubmit(std::move(job))) {
233
- state->enqueueTask([state, call](jsi::Runtime& rt) mutable {
234
- auto done = std::move(call->done);
235
- state->pending.erase(call);
236
- if (done.has_value()) {
237
- done->call(
238
- rt,
239
- jsi::Value::undefined(),
240
- jsi::Value(rt, jsi::String::createFromAscii(rt, "kdf queue full")));
241
- }
242
- });
278
+ submitDerivation(rt, state, args[5], [hash, password, salt, iterations, length] {
279
+ return kernelPbkdf2(hash, *password, *salt, iterations, length);
280
+ });
281
+ return jsi::Value::undefined();
282
+ }));
283
+ }
284
+
285
+ void installArgon2id(
286
+ jsi::Runtime& rt,
287
+ jsi::Object& qntx,
288
+ const std::shared_ptr<KernelGlobalsState>& state) {
289
+ qntx.setProperty(
290
+ rt,
291
+ "argon2id",
292
+ hostFn(rt, "argon2id", 7, [state](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
293
+ const char* usage =
294
+ "__qntx.argon2id expects (password, salt, memoryKiB, passes, lanes, length, done)";
295
+ if (count < 7 || !args[6].isObject() || !args[6].asObject(rt).isFunction(rt)) {
296
+ throw jsi::JSError(rt, usage);
243
297
  }
298
+ const auto& intrinsics = *state->intrinsics;
299
+ auto passwordView =
300
+ byteViewFromValue(rt, args[0], "__qntx.argon2id password", intrinsics);
301
+ auto password =
302
+ std::make_shared<SecretBytes>(passwordView.data, passwordView.size);
303
+ auto saltView =
304
+ byteViewFromValue(rt, args[1], "__qntx.argon2id salt", intrinsics);
305
+ auto salt = std::make_shared<SecretBytes>(saltView.data, saltView.size);
306
+ const auto memoryKiB = static_cast<uint32_t>(integerInRange(
307
+ rt, args[2], "__qntx.argon2id memoryKiB", kArgon2MemoryKiBMin, kArgon2MemoryKiBMax));
308
+ const auto passes = static_cast<uint32_t>(
309
+ integerInRange(rt, args[3], "__qntx.argon2id passes", 1, kArgon2PassesMax));
310
+ const auto lanes = static_cast<uint32_t>(
311
+ integerInRange(rt, args[4], "__qntx.argon2id lanes", 1, kArgon2LanesMax));
312
+ const auto length = static_cast<size_t>(integerInRange(
313
+ rt, args[5], "__qntx.argon2id length", kArgon2LengthMin, kArgon2LengthMax));
314
+ submitDerivation(rt, state, args[6], [password, salt, memoryKiB, passes, lanes, length] {
315
+ return kernelArgon2id(*password, *salt, memoryKiB, passes, lanes, length);
316
+ });
244
317
  return jsi::Value::undefined();
245
318
  }));
246
319
  }
@@ -356,9 +429,7 @@ void installKernelGlobals(
356
429
  rt,
357
430
  "elapsed",
358
431
  hostFn(rt, "elapsed", 0, [](jsi::Runtime&, const jsi::Value&, const jsi::Value*, size_t) -> jsi::Value {
359
- return jsi::Value(std::chrono::duration<double, std::milli>(
360
- std::chrono::steady_clock::now().time_since_epoch())
361
- .count());
432
+ return jsi::Value(elapsedMillis());
362
433
  }));
363
434
 
364
435
  const auto makeSetTimer = [&state](jsi::Runtime& rt) -> jsi::Function {
@@ -382,10 +453,10 @@ void installKernelGlobals(
382
453
  qntx.setProperty(rt, "setTimer", makeSetTimer(rt));
383
454
 
384
455
  installPbkdf2(rt, qntx, state);
456
+ installArgon2id(rt, qntx, state);
385
457
 
386
458
  // __qntx.frames — the W2b injection-table shape over the same plumbing:
387
- // post, onMessage, and onIntegrityViolation (the host fires that handler
388
- // when it refuses a frame on integrity grounds).
459
+ // post and onMessage.
389
460
  auto frames = jsi::Object(rt);
390
461
  frames.setProperty(
391
462
  rt,
@@ -396,7 +467,7 @@ void installKernelGlobals(
396
467
  }
397
468
  auto buffer = args[0].asObject(rt).getArrayBuffer(rt);
398
469
  const auto* data = buffer.data(rt);
399
- state->postToHost(std::vector<uint8_t>(data, data + buffer.size(rt)));
470
+ state->postToHost(qntx::SecretBytes(data, buffer.size(rt)));
400
471
  return jsi::Value::undefined();
401
472
  }));
402
473
  frames.setProperty(
@@ -409,23 +480,16 @@ void installKernelGlobals(
409
480
  state->messageHandler = args[0].asObject(rt).asFunction(rt);
410
481
  return jsi::Value::undefined();
411
482
  }));
412
- frames.setProperty(
413
- rt,
414
- "onIntegrityViolation",
415
- hostFn(rt, "frames.onIntegrityViolation", 1, [state](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
416
- if (count < 1 || !args[0].isObject() || !args[0].asObject(rt).isFunction(rt)) {
417
- throw jsi::JSError(rt, "__qntx.frames.onIntegrityViolation expects a function");
418
- }
419
- state->integrityHandler = args[0].asObject(rt).asFunction(rt);
420
- return jsi::Value::undefined();
421
- }));
422
483
  intrinsics->freeze.call(rt, jsi::Value(rt, frames));
423
484
  qntx.setProperty(rt, "frames", jsi::Value(std::move(frames)));
424
485
 
425
486
  // __qntx.file — one opaque file image: read() -> Uint8Array | undefined,
426
- // write(bytes) atomically replaces. The C++ primitive owns durability
427
- // (temp + fsync + rename + dir fsync); failures throw so the file store
428
- // rejects instead of silently degrading.
487
+ // replace(next, expected?) atomically swaps the file only when its current
488
+ // bytes equal `expected` (undefined = the file must not exist), returning
489
+ // false on a lost race so the file store fails `conflict` instead of
490
+ // clobbering a concurrent writer. The C++ primitive serializes writers under
491
+ // a flock and owns durability (temp + fsync + rename + dir fsync); failures
492
+ // throw so the file store rejects instead of silently degrading.
429
493
  auto file = jsi::Object(rt);
430
494
  file.setProperty(
431
495
  rt,
@@ -440,23 +504,72 @@ void installKernelGlobals(
440
504
  }));
441
505
  file.setProperty(
442
506
  rt,
443
- "write",
444
- hostFn(rt, "file.write", 1, [state](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
507
+ "replace",
508
+ hostFn(rt, "file.replace", 2, [state](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
445
509
  if (count < 1) {
446
- throw jsi::JSError(rt, "__qntx.file.write expects an ArrayBuffer or TypedArray");
510
+ throw jsi::JSError(rt, "__qntx.file.replace expects (next, expected?)");
447
511
  }
448
- const auto view =
449
- byteViewFromValue(rt, args[0], "__qntx.file.write", *state->intrinsics);
450
- kernelFileWrite(
451
- state->vaultDirectory, std::vector<uint8_t>(view.data, view.data + view.size));
452
- return jsi::Value::undefined();
512
+ const auto next =
513
+ byteViewFromValue(rt, args[0], "__qntx.file.replace next", *state->intrinsics);
514
+ std::optional<std::vector<uint8_t>> expected;
515
+ if (count >= 2 && !args[1].isUndefined()) {
516
+ const auto expectedView = byteViewFromValue(
517
+ rt, args[1], "__qntx.file.replace expected", *state->intrinsics);
518
+ expected =
519
+ std::vector<uint8_t>(expectedView.data, expectedView.data + expectedView.size);
520
+ }
521
+ return jsi::Value(
522
+ kernelFileReplace(state->vaultDirectory, next.data, next.size, expected));
453
523
  }));
454
524
  intrinsics->freeze.call(rt, jsi::Value(rt, file));
455
525
  qntx.setProperty(rt, "file", jsi::Value(std::move(file)));
456
526
 
527
+ // __qntx.anchor — the vault anchor behind platform.anchor: a few bytes of
528
+ // kernel state held outside the vault file (platform keystore) so a host
529
+ // that restores an older vault image cannot restore the revision floor or
530
+ // the failure counters with it. read() -> Uint8Array | undefined;
531
+ // replace(next, expected?) is the compare-and-swap the C++ side serializes
532
+ // under one process-wide mutex. Platform failures throw so the kernel maps
533
+ // them to `unavailable` instead of silently degrading.
534
+ auto anchor = jsi::Object(rt);
535
+ anchor.setProperty(
536
+ rt,
537
+ "read",
538
+ hostFn(rt, "anchor.read", 0, [state](jsi::Runtime& rt, const jsi::Value&, const jsi::Value*, size_t) -> jsi::Value {
539
+ const auto bytes = vaultAnchorRead();
540
+ if (!bytes.has_value()) {
541
+ return jsi::Value::undefined();
542
+ }
543
+ auto buffer = arrayBufferFromBytes(rt, bytes->data(), bytes->size());
544
+ return jsi::Value(rt, makeUint8Array(rt, *state->intrinsics, std::move(buffer)));
545
+ }));
546
+ anchor.setProperty(
547
+ rt,
548
+ "replace",
549
+ hostFn(rt, "anchor.replace", 2, [state](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
550
+ if (count < 1) {
551
+ throw jsi::JSError(rt, "__qntx.anchor.replace expects (next, expected?)");
552
+ }
553
+ const auto next =
554
+ byteViewFromValue(rt, args[0], "__qntx.anchor.replace next", *state->intrinsics);
555
+ std::optional<std::vector<uint8_t>> expected;
556
+ if (count >= 2 && !args[1].isUndefined()) {
557
+ const auto expectedView = byteViewFromValue(
558
+ rt, args[1], "__qntx.anchor.replace expected", *state->intrinsics);
559
+ expected =
560
+ std::vector<uint8_t>(expectedView.data, expectedView.data + expectedView.size);
561
+ }
562
+ return jsi::Value(
563
+ vaultAnchorReplace(next.data, next.size, expected));
564
+ }));
565
+ intrinsics->freeze.call(rt, jsi::Value(rt, anchor));
566
+ qntx.setProperty(rt, "anchor", jsi::Value(std::move(anchor)));
567
+
457
568
  installKernelBackends(rt, qntx, *intrinsics);
458
569
  installKernelKeystore(rt, qntx, state);
459
570
  installKernelPasskey(rt, qntx, state);
571
+ installKernelCardReader(rt, qntx, state);
572
+ installKernelSecureInput(rt, qntx, state);
460
573
 
461
574
  intrinsics->freeze.call(rt, jsi::Value(rt, qntx));
462
575
  defineHiddenGlobal(rt, *intrinsics, "__qntx", jsi::Value(std::move(qntx)));
@@ -2,6 +2,9 @@
2
2
 
3
3
  #include <jsi/jsi.h>
4
4
 
5
+ #include "../host/CardReader.hpp"
6
+ #include "KernelBuffer.hpp"
7
+
5
8
  #include <atomic>
6
9
  #include <chrono>
7
10
  #include <cstdint>
@@ -33,12 +36,14 @@ struct KernelIntrinsics;
33
36
  struct KernelGlobalsState {
34
37
  // Directory the vault image file lives in — resolved by the host once per
35
38
  // KernelRuntime::start (platformVaultDirectory on device builds) and passed
36
- // to kernelFileRead/kernelFileWrite by the __qntx.file lambdas. Kernel
39
+ // to kernelFileRead/kernelFileReplace by the __qntx.file lambdas. Kernel
37
40
  // thread only after start().
38
41
  std::string vaultDirectory;
39
42
 
40
43
  // Copies the posted bytes out of the runtime and reports them to the host.
41
- std::function<void(std::vector<uint8_t>)> postToHost;
44
+ // The SecretBytes wipes itself on scope exit — the host's copy into the JS
45
+ // ArrayBuffer is the only one that outlives the call.
46
+ std::function<void(SecretBytes)> postToHost;
42
47
 
43
48
  // Records a fatal kernel failure reported by JS (unhandled promise
44
49
  // rejection). The runtime tears down after the current task drains.
@@ -74,10 +79,10 @@ struct KernelGlobalsState {
74
79
  // Kernel thread only.
75
80
  std::optional<facebook::jsi::Function> messageHandler;
76
81
 
77
- // Kernel-side handler the host calls when it refuses an inbound frame on
78
- // integrity grounds (oversize, queue full); installed through
79
- // __qntx.frames.onIntegrityViolation. Kernel thread only.
80
- std::optional<facebook::jsi::Function> integrityHandler;
82
+ // The process-shared NFC reader behind __qntx.cardReader; injected by the
83
+ // host through KernelRuntime::Hooks so tests and platforms can substitute
84
+ // their own.
85
+ std::shared_ptr<CardReader> cardReader;
81
86
 
82
87
  // Dismiss in-flight system prompts (keystore, passkey); each installer adds
83
88
  // one and teardown runs them all. Kernel thread only.
@@ -86,7 +91,8 @@ struct KernelGlobalsState {
86
91
 
87
92
  // Installs the kernel's capabilities on a fresh runtime:
88
93
  // globalThis.__qntx frozen { random, now, elapsed, setTimer, pbkdf2, file,
89
- // frames, backends, keystore, passkey }
94
+ // anchor, frames, backends, keystore, passkey,
95
+ // cardReader, secureInput }
90
96
  // globalThis.crypto frozen { getRandomValues }
91
97
  // globalThis.TextDecoder the native Hermes decoder (260318099+), frozen
92
98
  // and rebound hidden
@@ -23,6 +23,20 @@ double finiteNumber(jsi::Runtime& rt, const jsi::Value& value, const char* name)
23
23
  return value.getNumber();
24
24
  }
25
25
 
26
+ jsi::Value detailValue(jsi::Runtime& rt, const std::string& detail) {
27
+ if (detail.empty() || detail.size() > 80) {
28
+ return jsi::Value::undefined();
29
+ }
30
+ for (const char c : detail) {
31
+ const bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
32
+ (c >= '0' && c <= '9') || c == '_' || c == '.' || c == ':' || c == '-';
33
+ if (!ok) {
34
+ return jsi::Value::undefined();
35
+ }
36
+ }
37
+ return jsi::Value(rt, jsi::String::createFromAscii(rt, detail));
38
+ }
39
+
26
40
  uint64_t integerInRange(
27
41
  jsi::Runtime& rt,
28
42
  const jsi::Value& value,
@@ -42,6 +42,10 @@ void defineHiddenGlobal(
42
42
 
43
43
  double finiteNumber(jsi::Runtime& rt, const jsi::Value& value, const char* name);
44
44
 
45
+ // A detail is a "<domain>:<code>" diagnostic the JS side validates again;
46
+ // pass it through only while it stays a short token string.
47
+ jsi::Value detailValue(jsi::Runtime& rt, const std::string& detail);
48
+
45
49
  uint64_t integerInRange(
46
50
  jsi::Runtime& rt,
47
51
  const jsi::Value& value,
@@ -71,8 +71,8 @@ const char* errorCode(KeystoreStatus status) {
71
71
  return "cancelled";
72
72
  case KeystoreStatus::invalidated:
73
73
  return "factor-invalidated";
74
- case KeystoreStatus::noDeviceCredential:
75
- return "no-device-credential";
74
+ case KeystoreStatus::noLockScreen:
75
+ return "verification-unavailable";
76
76
  case KeystoreStatus::ok:
77
77
  case KeystoreStatus::unavailable:
78
78
  case KeystoreStatus::failed:
@@ -173,6 +173,7 @@ void installKernelKeystore(
173
173
 
174
174
  auto keystore = jsi::Object(rt);
175
175
  keystore.setProperty(rt, "locale", jsi::String::createFromUtf8(rt, keystoreLocale()));
176
+ keystore.setProperty(rt, "platform", jsi::String::createFromUtf8(rt, keystorePlatform()));
176
177
  keystore.setProperty(
177
178
  rt,
178
179
  "seal",
@@ -211,6 +212,81 @@ void installKernelKeystore(
211
212
  keystoreOpen(id, std::move(sealed), std::move(prompt), std::move(done));
212
213
  return jsi::Value::undefined();
213
214
  }));
215
+ keystore.setProperty(
216
+ rt,
217
+ "sealDevice",
218
+ hostFn(rt, "keystore.sealDevice", 3, [state, calls](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
219
+ if (count < 3) {
220
+ throw jsi::JSError(
221
+ rt, "__qntx.keystore.sealDevice expects (id, secret, done)");
222
+ }
223
+ auto id = factorId(rt, args[0], "__qntx.keystore.sealDevice");
224
+ auto view =
225
+ byteViewFromValue(rt, args[1], "__qntx.keystore.sealDevice secret", *state->intrinsics);
226
+ if (view.size != kSecretLength) {
227
+ throw jsi::JSError(rt, "__qntx.keystore.sealDevice secret must be 32 bytes");
228
+ }
229
+ SecretBytes secret(view.data, view.size);
230
+ auto done = park(state, calls, callbackFrom(rt, args[2], "__qntx.keystore.sealDevice"), true);
231
+ keystoreSealDevice(id, std::move(secret), std::move(done));
232
+ return jsi::Value::undefined();
233
+ }));
234
+ keystore.setProperty(
235
+ rt,
236
+ "openDevice",
237
+ hostFn(rt, "keystore.openDevice", 3, [state, calls](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
238
+ if (count < 3) {
239
+ throw jsi::JSError(
240
+ rt, "__qntx.keystore.openDevice expects (id, sealed, done)");
241
+ }
242
+ auto id = factorId(rt, args[0], "__qntx.keystore.openDevice");
243
+ auto view =
244
+ byteViewFromValue(rt, args[1], "__qntx.keystore.openDevice sealed", *state->intrinsics);
245
+ if (view.size == 0 || view.size > kSealedMax) {
246
+ throw jsi::JSError(rt, "__qntx.keystore.openDevice sealed length is out of range");
247
+ }
248
+ std::vector<uint8_t> sealed(view.data, view.data + view.size);
249
+ auto done = park(state, calls, callbackFrom(rt, args[2], "__qntx.keystore.openDevice"), true);
250
+ keystoreOpenDevice(id, std::move(sealed), std::move(done));
251
+ return jsi::Value::undefined();
252
+ }));
253
+ keystore.setProperty(
254
+ rt,
255
+ "probe",
256
+ hostFn(rt, "keystore.probe", 3, [state, calls](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
257
+ if (count < 3) {
258
+ throw jsi::JSError(rt, "__qntx.keystore.probe expects (id, protection, done)");
259
+ }
260
+ auto id = factorId(rt, args[0], "__qntx.keystore.probe");
261
+ if (!args[1].isString()) {
262
+ throw jsi::JSError(rt, "__qntx.keystore.probe protection must be a string");
263
+ }
264
+ const auto protection = args[1].asString(rt).utf8(rt);
265
+ KeyProtection kind;
266
+ if (protection == "device") {
267
+ kind = KeyProtection::device;
268
+ } else if (protection == "presence") {
269
+ kind = KeyProtection::presence;
270
+ } else {
271
+ throw jsi::JSError(
272
+ rt, "__qntx.keystore.probe protection must be \"device\" or \"presence\"");
273
+ }
274
+ auto done = park(state, calls, callbackFrom(rt, args[2], "__qntx.keystore.probe"), false);
275
+ keystoreProbe(id, kind, std::move(done));
276
+ return jsi::Value::undefined();
277
+ }));
278
+ keystore.setProperty(
279
+ rt,
280
+ "remove",
281
+ hostFn(rt, "keystore.remove", 2, [state, calls](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
282
+ if (count < 2) {
283
+ throw jsi::JSError(rt, "__qntx.keystore.remove expects (id, done)");
284
+ }
285
+ auto id = factorId(rt, args[0], "__qntx.keystore.remove");
286
+ auto done = park(state, calls, callbackFrom(rt, args[1], "__qntx.keystore.remove"), false);
287
+ keystoreRemove(id, std::move(done));
288
+ return jsi::Value::undefined();
289
+ }));
214
290
  keystore.setProperty(
215
291
  rt,
216
292
  "authenticate",
@@ -11,6 +11,7 @@ struct KernelGlobalsState;
11
11
  // Installs the frozen `keystore` object on `__qntx` before the caller freezes
12
12
  // it:
13
13
  // locale platform language (BCP 47)
14
+ // platform "ios" | "android"
14
15
  // seal(id, secret, title, description, done) done(sealed | undefined, code | undefined)
15
16
  // open(id, sealed, title, description, done) done(kek | undefined, code | undefined)
16
17
  // authenticate(title, description, done) done(undefined, code | undefined)
@@ -4,6 +4,7 @@
4
4
  #include <utility>
5
5
 
6
6
  #include "../host/Passkey.hpp"
7
+ #include "KernelBuffer.hpp"
7
8
  #include "KernelGlobals.hpp"
8
9
  #include "KernelJs.hpp"
9
10
 
@@ -31,20 +32,21 @@ const char* errorCode(PasskeyStatus status) {
31
32
  return "unavailable";
32
33
  }
33
34
 
34
- // A detail is a "<domain>:<code>" diagnostic the JS side validates again;
35
- // pass it through only while it stays a short token string.
36
- jsi::Value detailValue(jsi::Runtime& rt, const std::string& detail) {
37
- if (detail.empty() || detail.size() > 80) {
35
+ // `{ first, second? }` Uint8Arrays over the platform's PRF outputs, or undefined when it gave none.
36
+ jsi::Value prfValue(jsi::Runtime& rt, KernelGlobalsState& state, const PasskeyResult& result) {
37
+ if (result.prfFirst.size() == 0) {
38
38
  return jsi::Value::undefined();
39
39
  }
40
- for (const char c : detail) {
41
- const bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
42
- (c >= '0' && c <= '9') || c == '_' || c == '.' || c == ':' || c == '-';
43
- if (!ok) {
44
- return jsi::Value::undefined();
45
- }
40
+ auto bytes = [&](const SecretBytes& secret) {
41
+ return makeUint8Array(
42
+ rt, *state.intrinsics, arrayBufferFromBytes(rt, secret.data(), secret.size()));
43
+ };
44
+ auto prf = jsi::Object(rt);
45
+ prf.setProperty(rt, "first", bytes(result.prfFirst));
46
+ if (result.prfSecond.size() != 0) {
47
+ prf.setProperty(rt, "second", bytes(result.prfSecond));
46
48
  }
47
- return jsi::Value(rt, jsi::String::createFromAscii(rt, detail));
49
+ return jsi::Value(rt, prf);
48
50
  }
49
51
 
50
52
  struct PasskeyCalls {
@@ -101,7 +103,8 @@ PasskeyDone park(
101
103
  rt,
102
104
  jsi::Value(rt, jsi::String::createFromUtf8(rt, shared->responseJson)),
103
105
  jsi::Value::undefined(),
104
- jsi::Value::undefined());
106
+ jsi::Value::undefined(),
107
+ prfValue(rt, *state, *shared));
105
108
  });
106
109
  };
107
110
  }