@qntx/wallet-native 0.12.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 +51913 -20526
  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
@@ -3,6 +3,8 @@
3
3
  #include <functional>
4
4
  #include <string>
5
5
 
6
+ #include "../runtime/KernelBuffer.hpp"
7
+
6
8
  namespace qntx {
7
9
 
8
10
  // The platform passkey authenticator behind `__qntx.passkey` (W6). The kernel
@@ -32,6 +34,12 @@ struct PasskeyResult {
32
34
  std::string responseJson;
33
35
  // A "<domain>:<code>" diagnostic (e.g. "ASAuthorizationErrorDomain:1004").
34
36
  std::string detail;
37
+ // The PRF outputs, which for a passkey wallet are the wallet entropy: delivered as wiped bytes,
38
+ // never inside `responseJson`, and empty when the platform returned none. iOS fills them;
39
+ // Android's Credential Manager returns PRF results only inside its response JSON string (a
40
+ // platform limit), so there they stay empty and the kernel reads the JSON.
41
+ SecretBytes prfFirst;
42
+ SecretBytes prfSecond;
35
43
  };
36
44
 
37
45
  // Completion callbacks run exactly once, on any thread.
@@ -0,0 +1,233 @@
1
+ #include "SecureInput.hpp"
2
+
3
+ #include <utility>
4
+ #include <vector>
5
+
6
+ namespace qntx {
7
+
8
+ namespace {
9
+
10
+ // The kind contract the platform views are asked to enforce; rechecked here
11
+ // so a buggy view cannot feed the kernel out-of-shape bytes.
12
+ // Strict UTF-8 (RFC 3629): well-formed sequences only, no overlong forms,
13
+ // no surrogates, nothing past U+10FFFF.
14
+ bool validUtf8(const SecretBytes& value) {
15
+ size_t i = 0;
16
+ while (i < value.size()) {
17
+ const uint8_t b = value.data()[i];
18
+ size_t extra;
19
+ uint32_t min;
20
+ if (b < 0x80) {
21
+ i += 1;
22
+ continue;
23
+ } else if ((b & 0xE0) == 0xC0) {
24
+ extra = 1;
25
+ min = 0x80;
26
+ if (b < 0xC2) return false;
27
+ } else if ((b & 0xF0) == 0xE0) {
28
+ extra = 2;
29
+ min = 0x800;
30
+ } else if ((b & 0xF8) == 0xF0) {
31
+ extra = 3;
32
+ min = 0x10000;
33
+ if (b > 0xF4) return false;
34
+ } else {
35
+ return false;
36
+ }
37
+ if (i + extra >= value.size()) {
38
+ return false;
39
+ }
40
+ uint32_t cp = b & (0x7F >> (extra + 1));
41
+ for (size_t j = 1; j <= extra; j++) {
42
+ const uint8_t c = value.data()[i + j];
43
+ if ((c & 0xC0) != 0x80) {
44
+ return false;
45
+ }
46
+ cp = (cp << 6) | (c & 0x3F);
47
+ }
48
+ if (cp < min || (cp >= 0xD800 && cp <= 0xDFFF)) {
49
+ return false;
50
+ }
51
+ i += extra + 1;
52
+ }
53
+ return true;
54
+ }
55
+
56
+ bool validFor(SecureInputKind kind, const SecretBytes& value) {
57
+ switch (kind) {
58
+ case SecureInputKind::accessCode: {
59
+ if (value.size() < 1 || value.size() > 64) {
60
+ return false;
61
+ }
62
+ for (size_t i = 0; i < value.size(); i++) {
63
+ if (value.data()[i] < 0x20 || value.data()[i] > 0x7E) {
64
+ return false;
65
+ }
66
+ }
67
+ return true;
68
+ }
69
+ case SecureInputKind::shortId: {
70
+ if (value.size() != 8) {
71
+ return false;
72
+ }
73
+ for (size_t i = 0; i < value.size(); i++) {
74
+ const uint8_t c = value.data()[i];
75
+ const bool hex = (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F');
76
+ if (!hex) {
77
+ return false;
78
+ }
79
+ }
80
+ return true;
81
+ }
82
+ case SecureInputKind::backupPassword: {
83
+ return value.size() >= 1 && value.size() <= 1024 && validUtf8(value);
84
+ }
85
+ case SecureInputKind::sessionApproval: {
86
+ if (value.size() != 4) {
87
+ return false;
88
+ }
89
+ for (size_t i = 0; i < value.size(); i++) {
90
+ const uint8_t c = value.data()[i];
91
+ if (c < '0' || c > '9') {
92
+ return false;
93
+ }
94
+ }
95
+ return true;
96
+ }
97
+ }
98
+ return false;
99
+ }
100
+
101
+ } // namespace
102
+
103
+ SecureInputHub& SecureInputHub::shared() {
104
+ static auto* hub = new SecureInputHub();
105
+ return *hub;
106
+ }
107
+
108
+ uint64_t SecureInputHub::attach(SecureInputSink sink) {
109
+ uint64_t view;
110
+ std::optional<SecureInputPrompt> prompt;
111
+ {
112
+ std::lock_guard<std::mutex> lock(mutex_);
113
+ view = ++nextView_;
114
+ sinks_.emplace(view, sink);
115
+ if (pending_.has_value()) {
116
+ prompt = SecureInputPrompt{pending_->kind, pending_->label};
117
+ }
118
+ }
119
+ sink(prompt);
120
+ return view;
121
+ }
122
+
123
+ void SecureInputHub::detach(uint64_t view) {
124
+ std::lock_guard<std::mutex> lock(mutex_);
125
+ sinks_.erase(view);
126
+ }
127
+
128
+ bool SecureInputHub::submit(uint64_t view, SecretBytes value) {
129
+ SecureInputDone done;
130
+ SecureInputSink sink;
131
+ std::vector<SecureInputSink> sinks;
132
+ {
133
+ std::lock_guard<std::mutex> lock(mutex_);
134
+ if (pending_.has_value() && validFor(pending_->kind, value)) {
135
+ done = std::move(pending_->done);
136
+ pending_.reset();
137
+ sinks.reserve(sinks_.size());
138
+ for (const auto& [_, s] : sinks_) {
139
+ sinks.push_back(s);
140
+ }
141
+ } else {
142
+ // Nothing pending, or bad bytes: never complete — clear just the
143
+ // submitting view. A live request stays pending for the others.
144
+ const auto found = sinks_.find(view);
145
+ if (found != sinks_.end()) {
146
+ sink = found->second;
147
+ }
148
+ }
149
+ }
150
+ // The clear reaches the views before the completion: a re-request issued
151
+ // from inside done() can never be overtaken by this clear.
152
+ for (const auto& s : sinks) {
153
+ s(std::nullopt);
154
+ }
155
+ if (done) {
156
+ done(SecureInputResult{std::nullopt, std::move(value)});
157
+ return true;
158
+ }
159
+ if (sink) {
160
+ sink(std::nullopt);
161
+ }
162
+ return false;
163
+ }
164
+
165
+ SecureInputArm SecureInputHub::request(
166
+ const void* owner,
167
+ SecureInputKind kind,
168
+ std::string label,
169
+ SecureInputDone done) {
170
+ std::vector<SecureInputSink> sinks;
171
+ SecureInputPrompt prompt{kind, label};
172
+ {
173
+ std::lock_guard<std::mutex> lock(mutex_);
174
+ if (pending_.has_value()) {
175
+ return pending_->owner == owner ? SecureInputArm::busySelf
176
+ : SecureInputArm::busyOther;
177
+ }
178
+ pending_ = Pending{owner, kind, std::move(label), std::move(done)};
179
+ sinks.reserve(sinks_.size());
180
+ for (const auto& [_, s] : sinks_) {
181
+ sinks.push_back(s);
182
+ }
183
+ }
184
+ for (const auto& s : sinks) {
185
+ s(prompt);
186
+ }
187
+ return SecureInputArm::armed;
188
+ }
189
+
190
+ void SecureInputHub::cancel(const void* owner) {
191
+ SecureInputDone done;
192
+ std::vector<SecureInputSink> sinks;
193
+ {
194
+ std::lock_guard<std::mutex> lock(mutex_);
195
+ if (!pending_.has_value() || pending_->owner != owner) {
196
+ return;
197
+ }
198
+ done = std::move(pending_->done);
199
+ pending_.reset();
200
+ sinks.reserve(sinks_.size());
201
+ for (const auto& [_, s] : sinks_) {
202
+ sinks.push_back(s);
203
+ }
204
+ }
205
+ // Same ordering as submit: views learn the request is gone before the
206
+ // requester's completion can arm the next one.
207
+ for (const auto& s : sinks) {
208
+ s(std::nullopt);
209
+ }
210
+ if (done) {
211
+ done(SecureInputResult{SecureInputFailure::cancelled, {}});
212
+ }
213
+ }
214
+
215
+ void SecureInputHub::release(const void* owner) {
216
+ std::vector<SecureInputSink> sinks;
217
+ {
218
+ std::lock_guard<std::mutex> lock(mutex_);
219
+ if (!pending_.has_value() || pending_->owner != owner) {
220
+ return;
221
+ }
222
+ pending_.reset();
223
+ sinks.reserve(sinks_.size());
224
+ for (const auto& [_, s] : sinks_) {
225
+ sinks.push_back(s);
226
+ }
227
+ }
228
+ for (const auto& s : sinks) {
229
+ s(std::nullopt);
230
+ }
231
+ }
232
+
233
+ } // namespace qntx
@@ -0,0 +1,103 @@
1
+ #pragma once
2
+
3
+ #include <cstdint>
4
+ #include <functional>
5
+ #include <mutex>
6
+ #include <optional>
7
+ #include <string>
8
+ #include <unordered_map>
9
+
10
+ #include "../runtime/KernelBuffer.hpp"
11
+
12
+ namespace qntx {
13
+
14
+ // The process-wide hub between kernel `secureInput` requests and the native
15
+ // secure-input views (one per mounted field). Typed characters travel view →
16
+ // hub → kernel as bytes only; the main runtime sees the view's state, never
17
+ // the text. A request is editable only while pending, so every keystroke
18
+ // answers a request the kernel actually made. The platform views register a
19
+ // sink; the kernel registers a request; the hub matches them under a mutex
20
+ // and runs every callback outside the lock.
21
+ //
22
+ // Submit validation is the guard against a platform bug: accessCode is 1..64
23
+ // bytes, each 0x20..0x7E (protocol.md §7.1 — the 6/8 minimums are the card
24
+ // module's policy, not this seam's); shortId is exactly 8 bytes of [0-9A-F]
25
+ // (protocol.md §1 short ID — the view already uppercases and drops typed
26
+ // dashes, this is the recheck); backupPassword is 1..1024 bytes of valid
27
+ // UTF-8 (the cloud-backup password — the wire's own bound); sessionApproval
28
+ // is exactly 4 bytes of '0'..'9' (the confirmation code the label shows —
29
+ // not a secret, the label itself is the protected content).
30
+
31
+ enum class SecureInputKind { accessCode, shortId, backupPassword, sessionApproval };
32
+
33
+ // What every registered view shows: a pending request's prompt, or nothing
34
+ // (cleared, not editable).
35
+ struct SecureInputPrompt {
36
+ SecureInputKind kind;
37
+ std::string label;
38
+ };
39
+
40
+ using SecureInputSink = std::function<void(const std::optional<SecureInputPrompt>&)>;
41
+
42
+ enum class SecureInputFailure { cancelled, unavailable };
43
+
44
+ struct SecureInputResult {
45
+ std::optional<SecureInputFailure> failure;
46
+ SecretBytes value; // only when !failure
47
+ };
48
+
49
+ // Runs exactly once per request, on the requesting or a view thread.
50
+ using SecureInputDone = std::function<void(SecureInputResult)>;
51
+
52
+ enum class SecureInputArm {
53
+ armed,
54
+ busySelf, // this owner already has a pending request
55
+ busyOther, // another owner has one
56
+ };
57
+
58
+ class SecureInputHub {
59
+ public:
60
+ // The process instance; kernel runtimes share it, so at most one input
61
+ // request exists at a time across runtimes.
62
+ static SecureInputHub& shared();
63
+
64
+ // Registers a view; calls `sink` once with the current prompt. Only a view
65
+ // attached to a window calls this. Returns the view id for detach/submit.
66
+ uint64_t attach(SecureInputSink sink);
67
+ void detach(uint64_t view);
68
+
69
+ // A view's completed input. False when nothing is pending or the bytes are
70
+ // invalid for the kind — the value is dropped (and wiped) either way, and
71
+ // the submitting view is cleared. True completes the request, then clears
72
+ // every view back to idle.
73
+ bool submit(uint64_t view, SecretBytes value);
74
+
75
+ // `owner` is the KernelGlobalsState* of the requesting runtime — opaque
76
+ // here, it only distinguishes whose pending request a cancel/release hits.
77
+ SecureInputArm request(
78
+ const void* owner,
79
+ SecureInputKind kind,
80
+ std::string label,
81
+ SecureInputDone done);
82
+ // Completes owner's pending request with `cancelled` and clears the views;
83
+ // a no-op when owner has none.
84
+ void cancel(const void* owner);
85
+ // Drops owner's pending request WITHOUT calling done (runtime teardown) and
86
+ // clears the views.
87
+ void release(const void* owner);
88
+
89
+ private:
90
+ struct Pending {
91
+ const void* owner;
92
+ SecureInputKind kind;
93
+ std::string label;
94
+ SecureInputDone done;
95
+ };
96
+
97
+ std::mutex mutex_;
98
+ std::unordered_map<uint64_t, SecureInputSink> sinks_;
99
+ std::optional<Pending> pending_;
100
+ uint64_t nextView_ = 0;
101
+ };
102
+
103
+ } // namespace qntx
@@ -0,0 +1,39 @@
1
+ // The vault anchor: a few bytes of kernel state kept outside the vault file —
2
+ // the revision floor and password-failure counters — where a host that puts an
3
+ // older vault image back cannot put this back too. This file owns the
4
+ // process-wide mutex and the compare: one load, one equality test, one store,
5
+ // all serialized so two kernel runtimes on the same anchor can never interleave
6
+ // a read-modify-write. Storage itself is per-platform.
7
+ #include "VaultAnchor.hpp"
8
+
9
+ #include <mutex>
10
+ #include <stdexcept>
11
+ #include <vector>
12
+
13
+ namespace qntx {
14
+ namespace {
15
+
16
+ std::mutex g_anchorMutex;
17
+
18
+ } // namespace
19
+
20
+ std::optional<std::vector<uint8_t>> vaultAnchorRead() {
21
+ std::lock_guard<std::mutex> lock(g_anchorMutex);
22
+ return platformAnchorLoad();
23
+ }
24
+
25
+ bool vaultAnchorReplace(
26
+ const uint8_t* next,
27
+ size_t size,
28
+ const std::optional<std::vector<uint8_t>>& expected) {
29
+ std::lock_guard<std::mutex> lock(g_anchorMutex);
30
+ const auto current = platformAnchorLoad();
31
+ if (expected.has_value() != current.has_value() ||
32
+ (expected.has_value() && *current != *expected)) {
33
+ return false;
34
+ }
35
+ platformAnchorStore(std::vector<uint8_t>(next, next + size));
36
+ return true;
37
+ }
38
+
39
+ } // namespace qntx
@@ -0,0 +1,28 @@
1
+ #pragma once
2
+
3
+ #include <cstddef>
4
+ #include <cstdint>
5
+ #include <optional>
6
+ #include <vector>
7
+
8
+ namespace qntx {
9
+
10
+ // Per-platform primitives the target supplies (iOS keychain, Android keystore
11
+ // aliases). Both throw on a platform failure; the shared compare-and-swap
12
+ // above them retries nothing — the kernel's updateAnchor owns the retry loop.
13
+ std::optional<std::vector<uint8_t>> platformAnchorLoad();
14
+ void platformAnchorStore(const std::vector<uint8_t>& bytes);
15
+
16
+ // nullopt when nothing is stored. Throws std::runtime_error on a platform
17
+ // failure.
18
+ std::optional<std::vector<uint8_t>> vaultAnchorRead();
19
+
20
+ // Compare-and-swap under one process-wide mutex (both kernel runtimes share
21
+ // it): writes `next` only when the stored bytes equal `expected` (nullopt =
22
+ // nothing stored); false otherwise, writing nothing.
23
+ bool vaultAnchorReplace(
24
+ const uint8_t* next,
25
+ size_t size,
26
+ const std::optional<std::vector<uint8_t>>& expected);
27
+
28
+ } // namespace qntx
@@ -5,7 +5,7 @@
5
5
  namespace qntx {
6
6
 
7
7
  // The directory the vault image file lives in — resolved once per
8
- // KernelRuntime::start and passed explicitly to kernelFileRead/kernelFileWrite:
8
+ // KernelRuntime::start and passed explicitly to kernelFileRead/kernelFileReplace:
9
9
  // iOS <Application Support>/qntx-wallet
10
10
  // Android <noBackupFilesDir>/qntx-wallet
11
11
  // Defined only by a platform translation unit (platform/VaultDirectory.mm on iOS,
@@ -0,0 +1,146 @@
1
+ // Android NFC reader: the Kotlin CardReader object runs reader mode and the
2
+ // card sheet; this file forwards calls through fbjni with completion tokens
3
+ // and resolves the parked completion when Kotlin calls nativeComplete.
4
+ #include "../host/CardReader.hpp"
5
+
6
+ #ifdef __ANDROID__
7
+
8
+ #include <fbjni/fbjni.h>
9
+ #include <jni.h>
10
+
11
+ #include <cstdint>
12
+ #include <mutex>
13
+ #include <stdexcept>
14
+ #include <string>
15
+ #include <unordered_map>
16
+ #include <utility>
17
+ #include <vector>
18
+
19
+ namespace qntx {
20
+ namespace {
21
+
22
+ namespace jni = facebook::jni;
23
+
24
+ jni::global_ref<jclass> g_readerClass;
25
+
26
+ std::mutex g_mutex;
27
+ int64_t g_nextToken = 1;
28
+ std::unordered_map<int64_t, CardReaderDone> g_pending;
29
+
30
+ int64_t park(CardReaderDone done) {
31
+ std::lock_guard<std::mutex> lock(g_mutex);
32
+ const int64_t token = g_nextToken++;
33
+ g_pending.emplace(token, std::move(done));
34
+ return token;
35
+ }
36
+
37
+ CardReaderDone take(int64_t token) {
38
+ std::lock_guard<std::mutex> lock(g_mutex);
39
+ auto found = g_pending.find(token);
40
+ if (found == g_pending.end()) {
41
+ return nullptr;
42
+ }
43
+ auto done = std::move(found->second);
44
+ g_pending.erase(found);
45
+ return done;
46
+ }
47
+
48
+ jni::alias_ref<jclass> readerClass() {
49
+ if (!g_readerClass) {
50
+ throw std::runtime_error("card reader: JNI_OnLoad init did not run");
51
+ }
52
+ return g_readerClass;
53
+ }
54
+
55
+ // Runs one Kotlin entry point; a JNI failure completes the call as
56
+ // `unavailable` instead of leaving it parked forever.
57
+ template <typename Call>
58
+ void dispatch(CardReaderDone done, Call&& call) {
59
+ const int64_t token = park(std::move(done));
60
+ try {
61
+ jni::ThreadScope threadScope;
62
+ call(static_cast<jlong>(token));
63
+ } catch (...) {
64
+ if (auto parked = take(token)) {
65
+ parked(CardReaderResult{CardReaderStatus::unavailable, {}, "android:jni"});
66
+ }
67
+ }
68
+ }
69
+
70
+ class AndroidCardReader final : public CardReader {
71
+ public:
72
+ void open(std::string prompt, CardReaderDone done) override {
73
+ dispatch(std::move(done), [&prompt](jlong token) {
74
+ auto entry =
75
+ readerClass()->getStaticMethod<void(jlong, jni::alias_ref<jstring>)>("open");
76
+ entry(readerClass(), token, jni::make_jstring(prompt));
77
+ });
78
+ }
79
+
80
+ void transmit(std::vector<uint8_t> apdu, CardReaderDone done) override {
81
+ dispatch(std::move(done), [&apdu](jlong token) {
82
+ auto bytes = jni::JArrayByte::newArray(apdu.size());
83
+ bytes->setRegion(
84
+ 0,
85
+ static_cast<jsize>(apdu.size()),
86
+ reinterpret_cast<const jbyte*>(apdu.data()));
87
+ auto entry = readerClass()
88
+ ->getStaticMethod<void(jlong, jni::alias_ref<jni::JArrayByte>)>(
89
+ "transmit");
90
+ entry(readerClass(), token, bytes);
91
+ });
92
+ }
93
+
94
+ void close(bool success, std::string text) override {
95
+ jni::ThreadScope threadScope;
96
+ auto method =
97
+ readerClass()->getStaticMethod<void(jboolean, jni::alias_ref<jstring>)>("close");
98
+ method(readerClass(), success ? JNI_TRUE : JNI_FALSE, jni::make_jstring(text));
99
+ }
100
+ };
101
+
102
+ } // namespace
103
+
104
+ void kernelCardReaderInit(jni::alias_ref<jclass> clazz) {
105
+ g_readerClass = jni::make_global(clazz);
106
+ }
107
+
108
+ std::shared_ptr<CardReader> platformCardReader() {
109
+ static const auto reader = std::make_shared<AndroidCardReader>();
110
+ return reader;
111
+ }
112
+
113
+ } // namespace qntx
114
+
115
+ // Kotlin's CardReader.nativeComplete resolves the parked call. Status ints
116
+ // are the CardReaderStatus JNI contract; an out-of-range value degrades to
117
+ // unavailable rather than inventing a status.
118
+ extern "C" JNIEXPORT void JNICALL
119
+ Java_com_margelo_nitro_qntxwallet_CardReader_nativeComplete(
120
+ JNIEnv* env, jclass, jlong token, jint status, jbyteArray response, jstring detail) {
121
+ auto done = qntx::take(static_cast<int64_t>(token));
122
+ if (!done) {
123
+ return;
124
+ }
125
+ qntx::CardReaderResult result;
126
+ result.status = static_cast<qntx::CardReaderStatus>(status);
127
+ if (status < 0 || status > static_cast<jint>(qntx::CardReaderStatus::unavailable)) {
128
+ result.status = qntx::CardReaderStatus::unavailable;
129
+ }
130
+ if (response != nullptr && result.status == qntx::CardReaderStatus::ok) {
131
+ const jsize size = env->GetArrayLength(response);
132
+ result.response.resize(static_cast<size_t>(size));
133
+ env->GetByteArrayRegion(
134
+ response, 0, size, reinterpret_cast<jbyte*>(result.response.data()));
135
+ }
136
+ if (detail != nullptr) {
137
+ const char* utf = env->GetStringUTFChars(detail, nullptr);
138
+ if (utf != nullptr) {
139
+ result.detail = utf;
140
+ env->ReleaseStringUTFChars(detail, utf);
141
+ }
142
+ }
143
+ done(std::move(result));
144
+ }
145
+
146
+ #endif // __ANDROID__