@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
@@ -59,6 +59,7 @@ hermes::vm::RuntimeConfig runtimeConfigFor(KernelProfile profile) {
59
59
  return builder.withMicrotaskQueue(true)
60
60
  .withEnableEval(false)
61
61
  .withAsyncBreakCheckInEval(true)
62
+ .withES6BlockScoping(true)
62
63
  .build();
63
64
  }
64
65
 
@@ -126,7 +127,7 @@ struct KernelRuntime::Impl {
126
127
  void loop();
127
128
  void fail(std::string message);
128
129
  void teardown();
129
- void deliver(std::vector<uint8_t> bytes);
130
+ void deliver(std::shared_ptr<OwningBuffer> frame);
130
131
  // Queues a task that runs fn(rt) on the kernel thread. Accepts work even
131
132
  // after `stopping`: queued-but-unrun tasks are destroyed by teardown, which
132
133
  // runs before the runtime itself is destroyed — so jsi values captured by
@@ -158,6 +159,7 @@ std::shared_ptr<KernelRuntime> KernelRuntime::start(
158
159
  impl.hooks = std::move(hooks);
159
160
  impl.globals = std::make_shared<KernelGlobalsState>();
160
161
  impl.globals->vaultDirectory = std::move(vaultDirectory);
162
+ impl.globals->cardReader = impl.hooks.cardReader;
161
163
  impl.globals->active = std::make_shared<std::atomic_bool>(true);
162
164
  impl.globals->postToHost = impl.hooks.onPost;
163
165
  impl.globals->reportFatal = [&impl](std::string message) {
@@ -194,8 +196,12 @@ bool KernelRuntime::waitUntilStopped(std::chrono::milliseconds timeout) const {
194
196
  return impl.stoppedCv.wait_for(lock, timeout, [&] { return impl.stopped; });
195
197
  }
196
198
 
197
- void KernelRuntime::post(std::vector<uint8_t> bytes) {
199
+ void KernelRuntime::post(SecretBytes bytes) {
198
200
  auto& impl = *impl_;
201
+ // Wrap before any early return: the shared owner wipes the bytes whichever
202
+ // path drops it — stopping, cap refusal, teardown clearing the queue, or the
203
+ // delivered ArrayBuffer's last release.
204
+ auto frame = std::make_shared<OwningBuffer>(std::move(bytes));
199
205
  std::function<void()> integrity;
200
206
  {
201
207
  std::lock_guard<std::mutex> lock(impl.mutex);
@@ -207,12 +213,12 @@ void KernelRuntime::post(std::vector<uint8_t> bytes) {
207
213
  integrity = impl.hooks.onIntegrity;
208
214
  } else {
209
215
  impl.pendingPosts += 1;
210
- impl.immediate.push_back([&impl, bytes = std::move(bytes)]() mutable {
216
+ impl.immediate.push_back([&impl, frame = std::move(frame)]() mutable {
211
217
  {
212
218
  std::lock_guard<std::mutex> inner(impl.mutex);
213
219
  impl.pendingPosts -= 1;
214
220
  }
215
- impl.deliver(std::move(bytes));
221
+ impl.deliver(std::move(frame));
216
222
  });
217
223
  }
218
224
  }
@@ -298,6 +304,11 @@ void KernelRuntime::Impl::initRuntime() {
298
304
  if (hooks.beforeEvaluate) {
299
305
  hooks.beforeEvaluate(*rt);
300
306
  }
307
+ if (stopping.load(std::memory_order_acquire)) {
308
+ // destroy() arrived while the hook waited (a predecessor still stopping):
309
+ // a runtime already told to stop never evaluates the kernel.
310
+ return;
311
+ }
301
312
 
302
313
  evaluate();
303
314
  rt->drainMicrotasks();
@@ -394,11 +405,11 @@ void KernelRuntime::Impl::loop() {
394
405
  }
395
406
  }
396
407
 
397
- void KernelRuntime::Impl::deliver(std::vector<uint8_t> bytes) {
408
+ void KernelRuntime::Impl::deliver(std::shared_ptr<OwningBuffer> frame) {
398
409
  if (!globals->messageHandler.has_value()) {
399
410
  return;
400
411
  }
401
- auto buffer = rt->createArrayBuffer(std::make_shared<OwningBuffer>(std::move(bytes)));
412
+ auto buffer = rt->createArrayBuffer(std::move(frame));
402
413
  globals->messageHandler->call(*rt, jsi::Value(std::move(buffer)));
403
414
  }
404
415
 
@@ -465,7 +476,6 @@ void KernelRuntime::Impl::teardown() {
465
476
  // Drop every jsi value before the runtime itself.
466
477
  globals->intrinsics.reset();
467
478
  globals->messageHandler.reset();
468
- globals->integrityHandler.reset();
469
479
  for (const auto& call : globals->pending) {
470
480
  call->done.reset();
471
481
  }
@@ -484,6 +494,13 @@ void KernelRuntime::Impl::teardown() {
484
494
  std::lock_guard<std::mutex> lock(interruptMutex);
485
495
  interruptable = nullptr;
486
496
  }
497
+ // The hook may capture the predecessor runtime and the script a full kernel
498
+ // image: a stopped Impl would otherwise keep its whole predecessor chain —
499
+ // every runtime ever started — reachable. Both are touched only on this
500
+ // thread; hooks.onIntegrity is read from post() on other threads, so the
501
+ // other members stay.
502
+ hooks.beforeEvaluate = nullptr;
503
+ script.bytes.reset();
487
504
  rt.reset();
488
505
  running.store(false, std::memory_order_release);
489
506
  {
@@ -3,6 +3,9 @@
3
3
  #include <hermes/hermes.h>
4
4
  #include <jsi/jsi.h>
5
5
 
6
+ #include "../host/CardReader.hpp"
7
+ #include "KernelBuffer.hpp"
8
+
6
9
  #include <chrono>
7
10
  #include <cstdint>
8
11
  #include <functional>
@@ -38,13 +41,15 @@ struct KernelScript {
38
41
  // interrupts running JS via Hermes' async timeout. Destruction calls it.
39
42
  // - running() is true from start() until teardown finishes on the kernel
40
43
  // thread.
41
- // - waitUntilStopped() blocks until teardown finished; it exists for tests
42
- // and probes only — never call it on a UI thread.
44
+ // - waitUntilStopped() blocks until teardown finished; called on a kernel
45
+ // thread (a successor's beforeEvaluate) and by tests — never on a UI or
46
+ // JS thread.
43
47
  //
44
48
  // Host integration:
45
49
  // - Hooks.beforeEvaluate runs on the kernel thread after globals install and
46
50
  // before the script evaluates.
47
- // - Hooks.onPost receives bytes the script posts via __qntx.post.
51
+ // - Hooks.onPost receives bytes the script posts via __qntx.post, in
52
+ // wipe-on-release storage — the hook's copy is zeroed when it drops.
48
53
  // - Hooks.onError receives a single fatal error (script exception, task
49
54
  // exception, unhandled rejection); the runtime is torn down afterwards.
50
55
  // Errors that only exist because shutdown was requested never reach it.
@@ -52,16 +57,19 @@ class KernelRuntime final : public std::enable_shared_from_this<KernelRuntime> {
52
57
  public:
53
58
  struct Hooks {
54
59
  std::function<void(facebook::jsi::Runtime&)> beforeEvaluate;
55
- std::function<void(std::vector<uint8_t>)> onPost;
60
+ std::function<void(SecretBytes)> onPost;
56
61
  std::function<void(std::string)> onError;
57
62
  // Fired when post() exceeds the inbound frame queue cap (64) — a transport
58
63
  // violation the host maps to an `integrity` exit, not a kernel crash.
59
64
  std::function<void()> onIntegrity;
65
+ // The NFC reader behind __qntx.cardReader; the platform's instance by
66
+ // default, substituted by tests and the harness.
67
+ std::shared_ptr<CardReader> cardReader = platformCardReader();
60
68
  };
61
69
 
62
70
  // `vaultDirectory` is the directory the kernel's vault file lives in (the
63
71
  // host resolves it via platformVaultDirectory); the __qntx.file capabilities
64
- // capture it for kernelFileRead/kernelFileWrite.
72
+ // capture it for kernelFileRead/kernelFileReplace.
65
73
  static std::shared_ptr<KernelRuntime> start(
66
74
  KernelScript script,
67
75
  Hooks hooks,
@@ -77,7 +85,7 @@ class KernelRuntime final : public std::enable_shared_from_this<KernelRuntime> {
77
85
 
78
86
  // Delivers bytes to the script's message handler. Safe after destroy():
79
87
  // returns immediately if the runtime is stopping or gone.
80
- void post(std::vector<uint8_t> bytes);
88
+ void post(SecretBytes bytes);
81
89
 
82
90
  void destroy() noexcept;
83
91
  bool running() const noexcept;
@@ -0,0 +1,154 @@
1
+ #include "KernelSecureInput.hpp"
2
+
3
+ #include <string>
4
+ #include <utility>
5
+
6
+ #include "../host/SecureInput.hpp"
7
+ #include "KernelBuffer.hpp"
8
+ #include "KernelGlobals.hpp"
9
+ #include "KernelJs.hpp"
10
+
11
+ namespace qntx {
12
+
13
+ namespace jsi = facebook::jsi;
14
+
15
+ namespace {
16
+
17
+ // The session-approval label carries the whole limits prompt: the iOS bound
18
+ // admits a 400-code-point description plus the title and the code line, at 4
19
+ // UTF-8 bytes a code point.
20
+ constexpr size_t kLabelMax = 2048;
21
+
22
+ const char* errorCode(SecureInputFailure failure) {
23
+ switch (failure) {
24
+ case SecureInputFailure::cancelled:
25
+ return "cancelled";
26
+ case SecureInputFailure::unavailable:
27
+ break;
28
+ }
29
+ return "unavailable";
30
+ }
31
+
32
+ } // namespace
33
+
34
+ void installKernelSecureInput(
35
+ jsi::Runtime& rt,
36
+ jsi::Object& qntx,
37
+ const std::shared_ptr<KernelGlobalsState>& state) {
38
+ auto& hub = SecureInputHub::shared();
39
+
40
+ // Teardown drops this runtime's pending request without completing it — the
41
+ // parked jsi::Function is already dead by then — and clears the views. The
42
+ // ceremony captures the raw owner identity, not `state`: a shared_ptr here
43
+ // would cycle state → cancelCeremonies → state.
44
+ const void* owner = state.get();
45
+ state->cancelCeremonies.emplace_back(
46
+ [&hub, owner] { hub.release(owner); });
47
+
48
+ auto secureInput = jsi::Object(rt);
49
+ secureInput.setProperty(
50
+ rt,
51
+ "request",
52
+ hostFn(rt, "secureInput.request", 3, [state, &hub](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
53
+ if (count < 3) {
54
+ throw jsi::JSError(rt, "__qntx.secureInput.request expects (kind, label, done)");
55
+ }
56
+ if (!args[0].isString()) {
57
+ throw jsi::JSError(rt, "__qntx.secureInput.request kind must be a string");
58
+ }
59
+ const auto kindText = args[0].asString(rt).utf8(rt);
60
+ SecureInputKind kind;
61
+ if (kindText == "access-code") {
62
+ kind = SecureInputKind::accessCode;
63
+ } else if (kindText == "short-id") {
64
+ kind = SecureInputKind::shortId;
65
+ } else if (kindText == "backup-password") {
66
+ kind = SecureInputKind::backupPassword;
67
+ } else if (kindText == "session-approval") {
68
+ kind = SecureInputKind::sessionApproval;
69
+ } else {
70
+ throw jsi::JSError(rt, "__qntx.secureInput.request kind is unknown");
71
+ }
72
+ if (!args[1].isString()) {
73
+ throw jsi::JSError(rt, "__qntx.secureInput.request label must be a string");
74
+ }
75
+ auto label = args[1].asString(rt).utf8(rt);
76
+ if (label.empty() || label.size() > kLabelMax) {
77
+ throw jsi::JSError(rt, "__qntx.secureInput.request label length is out of range");
78
+ }
79
+ if (!args[2].isObject() || !args[2].asObject(rt).isFunction(rt)) {
80
+ throw jsi::JSError(rt, "__qntx.secureInput.request expects a completion function");
81
+ }
82
+ auto call = std::make_shared<KernelPendingCall>();
83
+ call->done = args[2].asObject(rt).asFunction(rt);
84
+ state->pending.insert(call);
85
+ std::weak_ptr<KernelGlobalsState> weak = state;
86
+ const auto arm = hub.request(
87
+ state.get(), kind, std::move(label), [weak, call](SecureInputResult result) {
88
+ auto state = weak.lock();
89
+ if (state == nullptr || !state->active->load(std::memory_order_acquire)) {
90
+ return;
91
+ }
92
+ auto shared = std::make_shared<SecureInputResult>(std::move(result));
93
+ state->enqueueTask([state, call, shared](jsi::Runtime& rt) {
94
+ auto done = std::move(call->done);
95
+ call->done.reset();
96
+ state->pending.erase(call);
97
+ if (!done.has_value()) {
98
+ return;
99
+ }
100
+ if (shared->failure.has_value()) {
101
+ done->call(
102
+ rt,
103
+ jsi::Value::undefined(),
104
+ jsi::Value(
105
+ rt,
106
+ jsi::String::createFromAscii(rt, errorCode(*shared->failure))));
107
+ return;
108
+ }
109
+ // A fresh Uint8Array holds the only copy that outlives this
110
+ // call; the SecretBytes in `shared` wipes itself.
111
+ auto bytes = arrayBufferFromBytes(
112
+ rt, shared->value.data(), shared->value.size());
113
+ done->call(
114
+ rt,
115
+ jsi::Value(rt, makeUint8Array(rt, *state->intrinsics, std::move(bytes))),
116
+ jsi::Value::undefined());
117
+ });
118
+ });
119
+ if (arm == SecureInputArm::busySelf) {
120
+ // Kernel bug: a runtime may only have one pending request.
121
+ call->done.reset();
122
+ state->pending.erase(call);
123
+ throw jsi::JSError(
124
+ rt, "__qntx.secureInput.request while a request is pending");
125
+ }
126
+ if (arm == SecureInputArm::busyOther) {
127
+ state->enqueueTask([state, call](jsi::Runtime& rt) {
128
+ auto done = std::move(call->done);
129
+ call->done.reset();
130
+ state->pending.erase(call);
131
+ if (!done.has_value()) {
132
+ return;
133
+ }
134
+ done->call(
135
+ rt,
136
+ jsi::Value::undefined(),
137
+ jsi::Value(rt, jsi::String::createFromAscii(rt, "unavailable")));
138
+ });
139
+ }
140
+ return jsi::Value::undefined();
141
+ }));
142
+ secureInput.setProperty(
143
+ rt,
144
+ "cancel",
145
+ hostFn(rt, "secureInput.cancel", 0, [state, &hub](jsi::Runtime&, const jsi::Value&, const jsi::Value*, size_t) -> jsi::Value {
146
+ hub.cancel(state.get());
147
+ return jsi::Value::undefined();
148
+ }));
149
+
150
+ state->intrinsics->freeze.call(rt, jsi::Value(rt, secureInput));
151
+ qntx.setProperty(rt, "secureInput", jsi::Value(std::move(secureInput)));
152
+ }
153
+
154
+ } // namespace qntx
@@ -0,0 +1,25 @@
1
+ #pragma once
2
+
3
+ #include <jsi/jsi.h>
4
+
5
+ #include <memory>
6
+
7
+ namespace qntx {
8
+
9
+ struct KernelGlobalsState;
10
+
11
+ // Installs the frozen `secureInput` object on `__qntx` before the caller
12
+ // freezes it:
13
+ // request(kind, label, done) done(value | undefined, code | undefined)
14
+ // cancel()
15
+ // `kind` is "access-code" or "short-id"; `label` is 1..512 UTF-8 bytes shown
16
+ // inside the secure view. Requests go to the process-wide SecureInputHub, so
17
+ // at most one request exists at a time: a second request of this runtime is
18
+ // a kernel bug (throws), another runtime's completes "unavailable". Teardown
19
+ // releases this runtime's pending request without calling its done.
20
+ void installKernelSecureInput(
21
+ facebook::jsi::Runtime& rt,
22
+ facebook::jsi::Object& qntx,
23
+ const std::shared_ptr<KernelGlobalsState>& state);
24
+
25
+ } // namespace qntx
@@ -0,0 +1,34 @@
1
+ #include "KernelArgon2.hpp"
2
+
3
+ #include <stdexcept>
4
+ #include <string>
5
+
6
+ extern "C" {
7
+ #include "../../vendor/argon2/include/argon2.h"
8
+ }
9
+
10
+ qntx::SecretBytes qntx::kernelArgon2id(
11
+ const SecretBytes& password,
12
+ const SecretBytes& salt,
13
+ uint32_t memoryKiB,
14
+ uint32_t passes,
15
+ uint32_t lanes,
16
+ size_t length) {
17
+ SecretBytes out(length);
18
+ // One thread per lane; the library wipes its working memory before freeing it
19
+ // (FLAG_clear_internal_memory is on by default).
20
+ const int status = argon2id_hash_raw(
21
+ passes,
22
+ memoryKiB,
23
+ lanes,
24
+ password.data(),
25
+ password.size(),
26
+ salt.data(),
27
+ salt.size(),
28
+ out.data(),
29
+ out.size());
30
+ if (status != ARGON2_OK) {
31
+ throw std::runtime_error(std::string("argon2id failed: ") + argon2_error_message(status));
32
+ }
33
+ return out;
34
+ }
@@ -0,0 +1,23 @@
1
+ #pragma once
2
+
3
+ #include "../KernelBuffer.hpp"
4
+
5
+ #include <cstddef>
6
+ #include <cstdint>
7
+
8
+ namespace qntx {
9
+
10
+ // Synchronous Argon2id (version 0x13, no secret, no associated data) through
11
+ // the vendored reference implementation — neither iOS nor Android ships one.
12
+ // Callers run it on the KDF worker thread, never on the kernel thread. Throws
13
+ // std::runtime_error on failure. Inputs and the result are SecretBytes, so any
14
+ // path that drops them still wipes them.
15
+ SecretBytes kernelArgon2id(
16
+ const SecretBytes& password,
17
+ const SecretBytes& salt,
18
+ uint32_t memoryKiB,
19
+ uint32_t passes,
20
+ uint32_t lanes,
21
+ size_t length);
22
+
23
+ } // namespace qntx