@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
@@ -0,0 +1,110 @@
1
+ // Android secure input: Kotlin's SecureInputField attaches to the hub
2
+ // through these JNI entry points. The hub's sink holds a global ref to the
3
+ // field and calls its deliver() with a ThreadScope; the field reposts to the
4
+ // main Looper. Submitted bytes are copied into wiping SecretBytes and the
5
+ // Java array is zeroed before submit() returns.
6
+ #include "../host/SecureInput.hpp"
7
+
8
+ #ifdef __ANDROID__
9
+
10
+ #include "../runtime/KernelBuffer.hpp"
11
+
12
+ #include <fbjni/fbjni.h>
13
+ #include <jni.h>
14
+
15
+ #include <cstring>
16
+ #include <optional>
17
+
18
+ namespace qntx {
19
+ namespace {
20
+
21
+ namespace jni = facebook::jni;
22
+
23
+ // The SecureInputField class and its deliver(int kind, String label) method —
24
+ // both cached at JNI_OnLoad.
25
+ jni::global_ref<jclass> g_fieldClass;
26
+ jmethodID g_deliver = nullptr;
27
+
28
+ void deliver(
29
+ const jni::global_ref<jobject>& field,
30
+ const std::optional<SecureInputPrompt>& prompt) {
31
+ jni::ThreadScope threadScope;
32
+ JNIEnv* env = jni::Environment::current();
33
+ if (env == nullptr || g_deliver == nullptr) {
34
+ return;
35
+ }
36
+ jint kind = 0;
37
+ // The SecureInputField.kt KIND_* ordinals, shared with the C++ enum.
38
+ // make_jstring handles all of UTF-8; NewStringUTF's modified UTF-8
39
+ // rejects astral characters and aborts under CheckJNI.
40
+ jni::local_ref<jstring> label;
41
+ if (prompt.has_value()) {
42
+ kind = prompt->kind == SecureInputKind::accessCode
43
+ ? 0
44
+ : prompt->kind == SecureInputKind::shortId
45
+ ? 1
46
+ : prompt->kind == SecureInputKind::backupPassword ? 2 : 3;
47
+ label = jni::make_jstring(prompt->label);
48
+ }
49
+ env->CallVoidMethod(field.get(), g_deliver, kind, label.get());
50
+ }
51
+
52
+ } // namespace
53
+
54
+ void kernelSecureInputInit(jni::alias_ref<jclass> clazz) {
55
+ g_fieldClass = jni::make_global(clazz);
56
+ jni::ThreadScope threadScope;
57
+ JNIEnv* env = jni::Environment::current();
58
+ g_deliver = env->GetMethodID(
59
+ clazz.get(), "deliver", "(ILjava/lang/String;)V");
60
+ }
61
+
62
+ } // namespace qntx
63
+
64
+ extern "C" {
65
+
66
+ // SecureInputField.nativeAttach: register the hub sink; it keeps a global
67
+ // ref to the field until detach() drops the sink and releases it.
68
+ JNIEXPORT jlong JNICALL
69
+ Java_com_margelo_nitro_qntxwallet_SecureInputField_nativeAttach(
70
+ JNIEnv*, jobject thiz) {
71
+ auto field = facebook::jni::make_global(thiz);
72
+ const uint64_t id = qntx::SecureInputHub::shared().attach(
73
+ [field](const std::optional<qntx::SecureInputPrompt>& prompt) {
74
+ qntx::deliver(field, prompt);
75
+ });
76
+ return static_cast<jlong>(id);
77
+ }
78
+
79
+ JNIEXPORT void JNICALL
80
+ Java_com_margelo_nitro_qntxwallet_SecureInputField_nativeDetach(
81
+ JNIEnv*, jobject, jlong id) {
82
+ qntx::SecureInputHub::shared().detach(static_cast<uint64_t>(id));
83
+ }
84
+
85
+ // SecureInputField.nativeSubmit: copy the bytes into wiping storage, zero
86
+ // the Java array in place, then hand the request to the hub.
87
+ JNIEXPORT jboolean JNICALL
88
+ Java_com_margelo_nitro_qntxwallet_SecureInputField_nativeSubmit(
89
+ JNIEnv* env, jobject, jlong id, jbyteArray bytes) {
90
+ if (bytes == nullptr) {
91
+ return JNI_FALSE;
92
+ }
93
+ const jsize size = env->GetArrayLength(bytes);
94
+ jbyte* elements = env->GetByteArrayElements(bytes, nullptr);
95
+ if (elements == nullptr) {
96
+ return JNI_FALSE;
97
+ }
98
+ qntx::SecretBytes value(
99
+ reinterpret_cast<const uint8_t*>(elements), static_cast<size_t>(size));
100
+ std::memset(elements, 0, static_cast<size_t>(size));
101
+ env->ReleaseByteArrayElements(bytes, elements, 0);
102
+ return qntx::SecureInputHub::shared().submit(
103
+ static_cast<uint64_t>(id), std::move(value))
104
+ ? JNI_TRUE
105
+ : JNI_FALSE;
106
+ }
107
+
108
+ } // extern "C"
109
+
110
+ #endif // __ANDROID__
@@ -0,0 +1,282 @@
1
+ // The iOS half of the trusted input surface. The text field lives in
2
+ // Objective-C++ so the typed characters pass to SecureInputHub as bytes —
3
+ // they never become a Swift String and never cross the main runtime. The
4
+ // main runtime sees only QntxSecureInputState transitions.
5
+ #include "../host/SecureInput.hpp"
6
+ #include "QntxSecureInputField.h"
7
+
8
+ #import <Foundation/Foundation.h>
9
+
10
+ #include <optional>
11
+ #include <utility>
12
+ #include <vector>
13
+
14
+ // A text field that never offers the edit menu: the main runtime can read
15
+ // the clipboard, so paste/copy/cut are cut off in both directions.
16
+ @interface QntxSecureTextField : UITextField
17
+ @end
18
+
19
+ @implementation QntxSecureTextField
20
+
21
+ - (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
22
+ return NO;
23
+ }
24
+
25
+ @end
26
+
27
+ @interface QntxSecureInputField () <UITextFieldDelegate>
28
+ @end
29
+
30
+ @implementation QntxSecureInputField {
31
+ UILabel* _label;
32
+ QntxSecureTextField* _field;
33
+ uint64_t _hubView;
34
+ // The pending request's kind while the field is editable.
35
+ qntx::SecureInputKind _kind;
36
+ // Set when this view's own submit() was accepted, so the hub's clear
37
+ // reports `submitted` before `idle`.
38
+ BOOL _justSubmitted;
39
+ QntxSecureInputState _state;
40
+ }
41
+
42
+ - (instancetype)init {
43
+ if (self = [super initWithFrame:CGRectZero]) {
44
+ _state = QntxSecureInputStateIdle;
45
+ _label = [[UILabel alloc] init];
46
+ _label.numberOfLines = 0;
47
+ _field = [[QntxSecureTextField alloc] init];
48
+ _field.delegate = self;
49
+ _field.enabled = NO;
50
+ _field.returnKeyType = UIReturnKeyDone;
51
+ [_field addTarget:self
52
+ action:@selector(textChanged)
53
+ forControlEvents:UIControlEventEditingChanged];
54
+ _label.translatesAutoresizingMaskIntoConstraints = NO;
55
+ _field.translatesAutoresizingMaskIntoConstraints = NO;
56
+ [self addSubview:_label];
57
+ [self addSubview:_field];
58
+ [NSLayoutConstraint activateConstraints:@[
59
+ [_label.topAnchor constraintEqualToAnchor:self.topAnchor],
60
+ [_label.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
61
+ [_label.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
62
+ [_field.topAnchor constraintEqualToAnchor:_label.bottomAnchor constant:8],
63
+ [_field.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
64
+ [_field.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
65
+ [_field.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
66
+ ]];
67
+ }
68
+ return self;
69
+ }
70
+
71
+ - (void)didMoveToWindow {
72
+ [super didMoveToWindow];
73
+ if (self.window != nil) {
74
+ if (_hubView == 0) {
75
+ __weak QntxSecureInputField* weakSelf = self;
76
+ _hubView = qntx::SecureInputHub::shared().attach(
77
+ [weakSelf](const std::optional<qntx::SecureInputPrompt>& prompt) {
78
+ // Blocks capture a C++ reference parameter as a reference: hop
79
+ // with a materialized copy or the main queue reads the caller's
80
+ // dead stack frame.
81
+ const std::optional<qntx::SecureInputPrompt> copy = prompt;
82
+ dispatch_async(dispatch_get_main_queue(), ^{
83
+ [weakSelf applyPrompt:copy];
84
+ });
85
+ });
86
+ }
87
+ } else {
88
+ [self detachHub];
89
+ [self disarm:NO];
90
+ }
91
+ }
92
+
93
+ - (void)dealloc {
94
+ [self detachHub];
95
+ }
96
+
97
+ - (void)detachHub {
98
+ if (_hubView != 0) {
99
+ qntx::SecureInputHub::shared().detach(_hubView);
100
+ _hubView = 0;
101
+ }
102
+ }
103
+
104
+ - (void)emit:(QntxSecureInputState)state {
105
+ if (_state == state) {
106
+ return;
107
+ }
108
+ _state = state;
109
+ if (_onStateChange != nil) {
110
+ _onStateChange(state);
111
+ }
112
+ }
113
+
114
+ - (void)applyPrompt:(const std::optional<qntx::SecureInputPrompt>&)prompt {
115
+ if (!prompt.has_value()) {
116
+ const BOOL submitted = _justSubmitted;
117
+ _justSubmitted = NO;
118
+ [self disarm:submitted];
119
+ return;
120
+ }
121
+ _justSubmitted = NO;
122
+ _kind = prompt->kind;
123
+ _label.text = [[NSString alloc] initWithBytes:prompt->label.data()
124
+ length:prompt->label.size()
125
+ encoding:NSUTF8StringEncoding];
126
+ [self arm];
127
+ }
128
+
129
+ - (void)arm {
130
+ _field.enabled = YES;
131
+ // The secrets stay masked; the short id must stay readable as it is
132
+ // proofread against the printed card, and the approval code is not a
133
+ // secret at all — the label itself shows it.
134
+ const BOOL readable = _kind == qntx::SecureInputKind::shortId ||
135
+ _kind == qntx::SecureInputKind::sessionApproval;
136
+ _field.secureTextEntry = !readable;
137
+ _field.keyboardType = _kind == qntx::SecureInputKind::backupPassword
138
+ ? UIKeyboardTypeDefault
139
+ : _kind == qntx::SecureInputKind::sessionApproval
140
+ ? UIKeyboardTypeNumbersAndPunctuation
141
+ : UIKeyboardTypeASCIICapable;
142
+ _field.autocorrectionType = UITextAutocorrectionTypeNo;
143
+ _field.spellCheckingType = UITextSpellCheckingTypeNo;
144
+ _field.smartQuotesType = UITextSmartQuotesTypeNo;
145
+ _field.smartDashesType = UITextSmartDashesTypeNo;
146
+ _field.smartInsertDeleteType = UITextSmartInsertDeleteTypeNo;
147
+ _field.autocapitalizationType = _kind == qntx::SecureInputKind::shortId
148
+ ? UITextAutocapitalizationTypeAllCharacters
149
+ : UITextAutocapitalizationTypeNone;
150
+ if (readable) {
151
+ _field.font = [UIFont monospacedDigitSystemFontOfSize:17 weight:UIFontWeightRegular];
152
+ } else {
153
+ _field.font = nil;
154
+ }
155
+ [self emit:QntxSecureInputStateEmpty];
156
+ // A mounted view that just armed takes the keyboard; the latest attach
157
+ // wins over fields armed earlier.
158
+ if (_field.window != nil) {
159
+ [_field becomeFirstResponder];
160
+ }
161
+ }
162
+
163
+ - (void)disarm:(BOOL)submitted {
164
+ // The NSString inside UIKit cannot be erased; overwrite the field's copy
165
+ // before clearing it — the best the platform can do.
166
+ if (_field.text.length > 0) {
167
+ _field.text =
168
+ [@"" stringByPaddingToLength:_field.text.length
169
+ withString:@"0"
170
+ startingAtIndex:0];
171
+ }
172
+ _field.text = @"";
173
+ _field.enabled = NO;
174
+ [_field resignFirstResponder];
175
+ _label.text = nil;
176
+ if (submitted) {
177
+ [self emit:QntxSecureInputStateSubmitted];
178
+ }
179
+ [self emit:QntxSecureInputStateIdle];
180
+ }
181
+
182
+ - (void)textChanged {
183
+ [self emit:(_field.text.length > 0 ? QntxSecureInputStateFilled
184
+ : QntxSecureInputStateEmpty)];
185
+ }
186
+
187
+ - (BOOL)textField:(UITextField*)field
188
+ shouldChangeCharactersInRange:(NSRange)range
189
+ replacementString:(NSString*)string {
190
+ NSMutableString* next = [_field.text mutableCopy];
191
+ [next replaceCharactersInRange:range withString:string];
192
+ if (_kind == qntx::SecureInputKind::backupPassword) {
193
+ // Any characters the keyboard produces, bounded in UTF-8 bytes — the
194
+ // wire's own limit; the hub rechecks validity on submit.
195
+ return [next lengthOfBytesUsingEncoding:NSUTF8StringEncoding] <= 1024;
196
+ }
197
+ if (_kind == qntx::SecureInputKind::accessCode) {
198
+ if (next.length > 64) {
199
+ return NO;
200
+ }
201
+ for (NSUInteger i = 0; i < next.length; i++) {
202
+ const unichar c = [next characterAtIndex:i];
203
+ if (c < 0x20 || c > 0x7E) {
204
+ return NO;
205
+ }
206
+ }
207
+ return YES;
208
+ }
209
+ // session-approval: decimal digits only, four at most.
210
+ if (_kind == qntx::SecureInputKind::sessionApproval) {
211
+ NSMutableString* filtered = [[NSMutableString alloc] init];
212
+ for (NSUInteger i = 0; i < next.length; i++) {
213
+ const unichar c = [next characterAtIndex:i];
214
+ if (c < '0' || c > '9' || filtered.length >= 4) {
215
+ return NO;
216
+ }
217
+ [filtered appendString:[NSString stringWithCharacters:&c length:1]];
218
+ }
219
+ // Returning NO suppresses UIControlEventEditingChanged, so the state
220
+ // transition has to be reported from here.
221
+ _field.text = filtered;
222
+ [self textChanged];
223
+ return NO;
224
+ }
225
+ // short-id: keep hex digits only, fold to upper-case, drop typed dashes.
226
+ NSMutableString* filtered = [[NSMutableString alloc] init];
227
+ for (NSUInteger i = 0; i < next.length; i++) {
228
+ unichar c = [next characterAtIndex:i];
229
+ if (c == (unichar)'-') {
230
+ continue;
231
+ }
232
+ if (c >= 'a' && c <= 'f') {
233
+ c -= 'a' - 'A';
234
+ }
235
+ const BOOL hex = (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F');
236
+ if (!hex || filtered.length >= 8) {
237
+ return NO;
238
+ }
239
+ [filtered appendString:[NSString stringWithCharacters:&c length:1]];
240
+ }
241
+ // Returning NO suppresses UIControlEventEditingChanged, so the state
242
+ // transition has to be reported from here.
243
+ _field.text = filtered;
244
+ [self textChanged];
245
+ return NO;
246
+ }
247
+
248
+ - (BOOL)textFieldShouldReturn:(UITextField*)field {
249
+ NSString* text = _field.text;
250
+ const BOOL ready =
251
+ _kind == qntx::SecureInputKind::shortId
252
+ ? text.length == 8
253
+ : _kind == qntx::SecureInputKind::sessionApproval ? text.length == 4
254
+ : text.length >= 1;
255
+ if (!ready) {
256
+ return YES;
257
+ }
258
+ qntx::SecretBytes value;
259
+ if (_kind == qntx::SecureInputKind::backupPassword) {
260
+ // A password is arbitrary Unicode — hand over its real UTF-8 bytes.
261
+ NSData* data = [text dataUsingEncoding:NSUTF8StringEncoding];
262
+ value =
263
+ qntx::SecretBytes(static_cast<const uint8_t*>(data.bytes), data.length);
264
+ } else {
265
+ // Every accepted character is ASCII, so one UTF-16 unit is one byte.
266
+ std::vector<uint8_t> bytes(text.length);
267
+ for (NSUInteger i = 0; i < text.length; i++) {
268
+ bytes[i] = static_cast<uint8_t>([text characterAtIndex:i]);
269
+ }
270
+ value = qntx::SecretBytes(std::move(bytes));
271
+ }
272
+ _justSubmitted =
273
+ qntx::SecureInputHub::shared().submit(_hubView, std::move(value));
274
+ if (!_justSubmitted) {
275
+ // The hub's own clear follows on this queue; drop the text now too.
276
+ _field.text = @"";
277
+ [self emit:QntxSecureInputStateEmpty];
278
+ }
279
+ return YES;
280
+ }
281
+
282
+ @end
@@ -0,0 +1,76 @@
1
+ // Android vault anchor: the record lives in Android Keystore entry aliases
2
+ // (`qntx-wallet.anchor.<seq>.<record hex>`) — see VaultAnchor.kt for why the
3
+ // alias itself is the byte store. This file only forwards the two platform
4
+ // primitives through fbjni; the CAS mutex and compare live in host/VaultAnchor.
5
+ #include "../host/VaultAnchor.hpp"
6
+
7
+ #ifdef __ANDROID__
8
+
9
+ #include <fbjni/fbjni.h>
10
+ #include <jni.h>
11
+
12
+ #include <stdexcept>
13
+ #include <vector>
14
+
15
+ namespace qntx {
16
+ namespace {
17
+
18
+ namespace jni = facebook::jni;
19
+
20
+ jni::global_ref<jclass> g_anchorClass;
21
+
22
+ jni::alias_ref<jclass> anchorClass() {
23
+ if (!g_anchorClass) {
24
+ throw std::runtime_error("vault anchor: JNI_OnLoad init did not run");
25
+ }
26
+ return g_anchorClass;
27
+ }
28
+
29
+ } // namespace
30
+
31
+ void kernelAnchorInit(jni::alias_ref<jclass> clazz) {
32
+ g_anchorClass = jni::make_global(clazz);
33
+ }
34
+
35
+ std::optional<std::vector<uint8_t>> platformAnchorLoad() {
36
+ try {
37
+ jni::ThreadScope threadScope;
38
+ auto method =
39
+ anchorClass()->getStaticMethod<jni::JArrayByte::javaobject()>("load");
40
+ auto bytes = method(anchorClass());
41
+ if (!bytes) {
42
+ return std::nullopt;
43
+ }
44
+ const auto size = static_cast<size_t>(bytes->size());
45
+ std::vector<uint8_t> out(size);
46
+ if (size > 0) {
47
+ bytes->getRegion(0, static_cast<jsize>(size),
48
+ reinterpret_cast<jbyte*>(out.data()));
49
+ }
50
+ return out;
51
+ } catch (const jni::JniException& error) {
52
+ throw std::runtime_error(
53
+ std::string("vault anchor: ") + error.what());
54
+ }
55
+ }
56
+
57
+ void platformAnchorStore(const std::vector<uint8_t>& bytes) {
58
+ try {
59
+ jni::ThreadScope threadScope;
60
+ auto method = anchorClass()->getStaticMethod<void(
61
+ jni::alias_ref<jni::JArrayByte>)>("store");
62
+ auto array = jni::JArrayByte::newArray(bytes.size());
63
+ if (!bytes.empty()) {
64
+ array->setRegion(0, static_cast<jsize>(bytes.size()),
65
+ reinterpret_cast<const jbyte*>(bytes.data()));
66
+ }
67
+ method(anchorClass(), array);
68
+ } catch (const jni::JniException& error) {
69
+ throw std::runtime_error(
70
+ std::string("vault anchor: ") + error.what());
71
+ }
72
+ }
73
+
74
+ } // namespace qntx
75
+
76
+ #endif // __ANDROID__
@@ -0,0 +1,76 @@
1
+ // iOS vault anchor: one generic-password keychain item (service
2
+ // "qntx-wallet.anchor", account "vault") holding the kernel's anchor record.
3
+ // The accessibility class matches the NSFileProtectionComplete vault file —
4
+ // when one is unreadable so is the other — and the item never syncs, so a host
5
+ // that restores an older vault image cannot restore the revision floor or the
6
+ // failure counters with it. Keychain items survive app reinstall, which is
7
+ // exactly why the kernel establishes at anchor.revision + 1.
8
+ #include "../host/VaultAnchor.hpp"
9
+
10
+ #import <Foundation/Foundation.h>
11
+ #import <Security/Security.h>
12
+
13
+ #include <stdexcept>
14
+ #include <vector>
15
+
16
+ namespace qntx {
17
+ namespace {
18
+
19
+ // The OSStatus is reduced to its number: failure messages propagate through
20
+ // the kernel's error path and must never carry item detail.
21
+ [[noreturn]] void failStep(const char* step, OSStatus status) {
22
+ throw std::runtime_error(
23
+ std::string(step) + " (OSStatus " + std::to_string(status) + ")");
24
+ }
25
+
26
+ NSDictionary* anchorQuery() {
27
+ return @{
28
+ (id)kSecClass : (id)kSecClassGenericPassword,
29
+ (id)kSecAttrService : @"qntx-wallet.anchor",
30
+ (id)kSecAttrAccount : @"vault",
31
+ (id)kSecAttrSynchronizable : @NO,
32
+ };
33
+ }
34
+
35
+ } // namespace
36
+
37
+ std::optional<std::vector<uint8_t>> platformAnchorLoad() {
38
+ NSMutableDictionary* query = [anchorQuery() mutableCopy];
39
+ query[(id)kSecReturnData] = @YES;
40
+ CFTypeRef found = nullptr;
41
+ const OSStatus status =
42
+ SecItemCopyMatching((__bridge CFDictionaryRef)query, &found);
43
+ if (status == errSecItemNotFound) {
44
+ return std::nullopt;
45
+ }
46
+ if (status != errSecSuccess || found == nullptr) {
47
+ failStep("read vault anchor", status);
48
+ }
49
+ NSData* data = (__bridge_transfer NSData*)found;
50
+ const auto* bytes = static_cast<const uint8_t*>(data.bytes);
51
+ return std::vector<uint8_t>(bytes, bytes + data.length);
52
+ }
53
+
54
+ void platformAnchorStore(const std::vector<uint8_t>& bytes) {
55
+ NSData* data = [NSData dataWithBytes:bytes.data() length:bytes.size()];
56
+ const OSStatus updated = SecItemUpdate(
57
+ (__bridge CFDictionaryRef)anchorQuery(),
58
+ (__bridge CFDictionaryRef) @{(id)kSecValueData : data});
59
+ if (updated == errSecSuccess) {
60
+ return;
61
+ }
62
+ if (updated != errSecItemNotFound) {
63
+ failStep("update vault anchor", updated);
64
+ }
65
+ NSMutableDictionary* item = [anchorQuery() mutableCopy];
66
+ item[(id)kSecAttrAccessible] =
67
+ (id)kSecAttrAccessibleWhenUnlockedThisDeviceOnly;
68
+ item[(id)kSecValueData] = data;
69
+ const OSStatus added =
70
+ SecItemAdd((__bridge CFDictionaryRef)item, nullptr);
71
+ if (added != errSecSuccess) {
72
+ failStep("store vault anchor", added);
73
+ }
74
+ }
75
+
76
+ } // namespace qntx
@@ -17,6 +17,34 @@ namespace {
17
17
 
18
18
  constexpr double kUint32Max = 4294967295.0;
19
19
 
20
+ void exact32(jsi::Runtime& rt, const ByteView& view, const char* name) {
21
+ if (view.size != 32) {
22
+ throw jsi::JSError(rt, std::string(name) + " expects a 32-byte view");
23
+ }
24
+ }
25
+
26
+ void wipe(uint8_t* data, size_t size) {
27
+ volatile uint8_t* p = data;
28
+ while (size--) {
29
+ *p++ = 0;
30
+ }
31
+ }
32
+
33
+ // Releases (and wipes) a library-owned qs_* output on every path, including a
34
+ // throw while it is being copied into the runtime.
35
+ struct QsOutput {
36
+ uint8_t* data = nullptr;
37
+ size_t size = 0;
38
+ QsOutput() = default;
39
+ QsOutput(const QsOutput&) = delete;
40
+ QsOutput& operator=(const QsOutput&) = delete;
41
+ ~QsOutput() {
42
+ if (data != nullptr) {
43
+ qs_free(data, size);
44
+ }
45
+ }
46
+ };
47
+
20
48
  // Every qs_* call shares this shape: byte inputs go in as borrowed (data,
21
49
  // size) views; the library-owned output is copied into a fresh Uint8Array
22
50
  // before qs_free wipes and releases it. A nonzero code throws
@@ -26,22 +54,13 @@ jsi::Value callOut(
26
54
  jsi::Runtime& rt,
27
55
  const KernelIntrinsics& intrinsics,
28
56
  Invoke&& invoke) {
29
- uint8_t* out = nullptr;
30
- size_t outLen = 0;
31
- const int32_t code = invoke(&out, &outLen);
32
- jsi::Value result;
33
- if (code == 0) {
34
- auto buffer = arrayBufferFromBytes(rt, out, outLen);
35
- result = jsi::Value(
36
- rt, makeUint8Array(rt, intrinsics, std::move(buffer)));
37
- }
38
- if (out != nullptr) {
39
- qs_free(out, outLen);
40
- }
57
+ QsOutput out;
58
+ const int32_t code = invoke(&out.data, &out.size);
41
59
  if (code != 0) {
42
60
  throw jsi::JSError(rt, "qntx_spark:" + std::to_string(code));
43
61
  }
44
- return result;
62
+ auto buffer = arrayBufferFromBytes(rt, out.data, out.size);
63
+ return jsi::Value(rt, makeUint8Array(rt, intrinsics, std::move(buffer)));
45
64
  }
46
65
 
47
66
  uint32_t uint32Arg(
@@ -183,22 +202,10 @@ void installKernelBackends(
183
202
  nostr.setProperty(
184
203
  rt, "abiVersion", jsi::Value(static_cast<double>(nostr_native_abi_version())));
185
204
 
186
- auto exact32 = [](jsi::Runtime& rt, const ByteView& view, const char* name) {
187
- if (view.size != 32) {
188
- throw jsi::JSError(rt, std::string(name) + " expects a 32-byte view");
189
- }
190
- };
191
- auto wipe = [](uint8_t* data, size_t size) {
192
- volatile uint8_t* p = data;
193
- while (size--) {
194
- *p++ = 0;
195
- }
196
- };
197
-
198
205
  nostr.setProperty(
199
206
  rt,
200
207
  "publicKey",
201
- hostFn(rt, "nostr.publicKey", 1, [&intrinsics, &exact32, &wipe](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
208
+ hostFn(rt, "nostr.publicKey", 1, [&intrinsics](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
202
209
  if (count < 1) {
203
210
  throw jsi::JSError(rt, "__qntx.backends.nostr.publicKey expects (secretKey)");
204
211
  }
@@ -220,7 +227,7 @@ void installKernelBackends(
220
227
  nostr.setProperty(
221
228
  rt,
222
229
  "sign",
223
- hostFn(rt, "nostr.sign", 3, [&intrinsics, &exact32, &wipe](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
230
+ hostFn(rt, "nostr.sign", 3, [&intrinsics](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
224
231
  if (count < 3) {
225
232
  throw jsi::JSError(rt, "__qntx.backends.nostr.sign expects (id, secretKey, auxRand)");
226
233
  }
@@ -264,7 +271,7 @@ void installKernelBackends(
264
271
  nostr.setProperty(
265
272
  rt,
266
273
  "ecdhX",
267
- hostFn(rt, "nostr.ecdhX", 2, [&intrinsics, &exact32, &wipe](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
274
+ hostFn(rt, "nostr.ecdhX", 2, [&intrinsics](jsi::Runtime& rt, const jsi::Value&, const jsi::Value* args, size_t count) -> jsi::Value {
268
275
  if (count < 2) {
269
276
  throw jsi::JSError(rt, "__qntx.backends.nostr.ecdhX expects (secretKey, publicKeyX)");
270
277
  }
@@ -77,11 +77,8 @@ class SecretBytes {
77
77
  // back ArrayBuffers that can hold secrets.
78
78
  class OwningBuffer final : public jsi::MutableBuffer {
79
79
  public:
80
- explicit OwningBuffer(std::vector<uint8_t> storage) : storage_(std::move(storage)) {}
80
+ explicit OwningBuffer(SecretBytes storage) : storage_(std::move(storage)) {}
81
81
  explicit OwningBuffer(size_t size) : storage_(size) {}
82
- ~OwningBuffer() override {
83
- kernelWipe(storage_);
84
- }
85
82
  size_t size() const override {
86
83
  return storage_.size();
87
84
  }
@@ -90,7 +87,7 @@ class OwningBuffer final : public jsi::MutableBuffer {
90
87
  }
91
88
 
92
89
  private:
93
- std::vector<uint8_t> storage_;
90
+ SecretBytes storage_;
94
91
  };
95
92
 
96
93
  } // namespace qntx