@qntx/wallet-native 0.10.0 → 0.12.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.
package/README.md CHANGED
@@ -87,9 +87,8 @@ const wallet = await createNativeWallet({
87
87
  Passkey wallets need the platform's domain association:
88
88
 
89
89
  - **iOS** — `webcredentials:<rpId>` associated domain (requires a paid developer account; free
90
- signing cannot hold the entitlement). **Native iOS passkeys are not implemented yet** — the
91
- ceremony returns `factor-unavailable`, so `createWithPasskey`/`restoreWithPasskey` cannot be
92
- offered on iOS; ship mnemonic restore.
90
+ signing cannot hold the entitlement). Without the entitlement the ceremony returns
91
+ `passkey-not-configured`, and below iOS 18 it returns `unsupported`.
93
92
  - **Android** — Credential Manager resolves the RP against
94
93
  `https://<rpId>/.well-known/assetlinks.json` with
95
94
  `delegate_permission/common.get_login_creds` naming the app's package and SHA-256 signing
@@ -111,8 +110,10 @@ Passkey wallets need the platform's domain association:
111
110
  `spark.approve` request open a platform authentication ceremony with kernel-owned prompt text
112
111
  (Simplified Chinese under a `zh*` locale, English otherwise).
113
112
  - `vault.reset` authenticates the device owner first, then erases the vault and all hardware
114
- keys. Devices without a lock screen return `factor-unavailable` — the host should send the
115
- user to set one up.
113
+ keys. Devices without a lock screen return `no-device-credential` — the host should send the
114
+ user to set one up. A `WalletError.detail` string carries the platform diagnostic
115
+ (`"<domain>:<code>"`, e.g. `"LAErrorDomain:-5"` or
116
+ `"android:KeyPermanentlyInvalidatedException"`) when the native layer can produce one.
116
117
  - `chain.sign` prompts _before_ any secret derivation: the kernel normalizes and decodes the
117
118
  intent, shows the prompt, and only then touches key material.
118
119
  - The kernel serializes ceremonies — two concurrent signing requests cannot race two system
@@ -51,6 +51,7 @@ object Keystore {
51
51
  private const val INVALIDATED = 2
52
52
  private const val UNAVAILABLE = 3
53
53
  private const val FAILED = 4
54
+ private const val NO_DEVICE_CREDENTIAL = 5
54
55
 
55
56
  @Volatile private var current: BiometricPrompt? = null
56
57
 
@@ -60,9 +61,13 @@ object Keystore {
60
61
  @JvmStatic
61
62
  fun seal(token: Long, id: String, secret: ByteArray, title: String, description: String) {
62
63
  val activity = foregroundActivity()
63
- if (activity == null || !deviceSecure(activity)) {
64
+ if (activity == null) {
64
65
  secret.fill(0)
65
- return nativeComplete(token, UNAVAILABLE, null)
66
+ return nativeComplete(token, UNAVAILABLE, null, "android:no-activity")
67
+ }
68
+ if (!deviceSecure(activity)) {
69
+ secret.fill(0)
70
+ return nativeComplete(token, NO_DEVICE_CREDENTIAL, null, null)
66
71
  }
67
72
  val alias = PREFIX + id
68
73
  val cipher = try {
@@ -70,20 +75,20 @@ object Keystore {
70
75
  Cipher.getInstance(TRANSFORMATION).apply { init(Cipher.ENCRYPT_MODE, generate(alias, activity)) }
71
76
  } catch (e: Exception) {
72
77
  secret.fill(0)
73
- return nativeComplete(token, UNAVAILABLE, null)
78
+ return nativeComplete(token, UNAVAILABLE, null, detailOf(e))
74
79
  }
75
- prompt(activity, title, description, BiometricPrompt.CryptoObject(cipher)) { status, crypto ->
80
+ prompt(activity, title, description, BiometricPrompt.CryptoObject(cipher)) { status, detail, crypto ->
76
81
  try {
77
82
  val authenticated = crypto?.cipher
78
83
  if (status != OK || authenticated == null) {
79
84
  keyStore().deleteEntry(alias)
80
- nativeComplete(token, if (status == OK) FAILED else status, null)
85
+ nativeComplete(token, if (status == OK) FAILED else status, null, detail)
81
86
  } else {
82
- nativeComplete(token, OK, authenticated.iv + authenticated.doFinal(secret))
87
+ nativeComplete(token, OK, authenticated.iv + authenticated.doFinal(secret), null)
83
88
  }
84
89
  } catch (e: Exception) {
85
90
  keyStore().deleteEntry(alias)
86
- nativeComplete(token, FAILED, null)
91
+ nativeComplete(token, FAILED, null, detailOf(e))
87
92
  } finally {
88
93
  secret.fill(0)
89
94
  }
@@ -92,39 +97,40 @@ object Keystore {
92
97
 
93
98
  @JvmStatic
94
99
  fun open(token: Long, id: String, sealed: ByteArray, title: String, description: String) {
95
- val activity = foregroundActivity() ?: return nativeComplete(token, UNAVAILABLE, null)
100
+ val activity = foregroundActivity()
101
+ ?: return nativeComplete(token, UNAVAILABLE, null, "android:no-activity")
96
102
  if (sealed.size <= IV_BYTES + TAG_BITS / 8) {
97
- return nativeComplete(token, FAILED, null)
103
+ return nativeComplete(token, FAILED, null, null)
98
104
  }
99
105
  val alias = PREFIX + id
100
106
  val key = try {
101
107
  keyStore().getKey(alias, null) as? SecretKey
102
108
  } catch (e: UnrecoverableKeyException) {
103
109
  null
104
- } ?: return nativeComplete(token, INVALIDATED, null)
110
+ } ?: return nativeComplete(token, INVALIDATED, null, null)
105
111
  val cipher = try {
106
112
  Cipher.getInstance(TRANSFORMATION).apply {
107
113
  init(Cipher.DECRYPT_MODE, key, GCMParameterSpec(TAG_BITS, sealed, 0, IV_BYTES))
108
114
  }
109
115
  } catch (e: KeyPermanentlyInvalidatedException) {
110
116
  keyStore().deleteEntry(alias)
111
- return nativeComplete(token, INVALIDATED, null)
117
+ return nativeComplete(token, INVALIDATED, null, detailOf(e))
112
118
  } catch (e: Exception) {
113
- return nativeComplete(token, FAILED, null)
119
+ return nativeComplete(token, FAILED, null, detailOf(e))
114
120
  }
115
- prompt(activity, title, description, BiometricPrompt.CryptoObject(cipher)) { status, crypto ->
121
+ prompt(activity, title, description, BiometricPrompt.CryptoObject(cipher)) { status, detail, crypto ->
116
122
  val authenticated = crypto?.cipher
117
123
  if (status != OK || authenticated == null) {
118
- nativeComplete(token, if (status == OK) FAILED else status, null)
124
+ nativeComplete(token, if (status == OK) FAILED else status, null, detail)
119
125
  return@prompt
120
126
  }
121
127
  val plain = try {
122
128
  authenticated.doFinal(sealed, IV_BYTES, sealed.size - IV_BYTES)
123
129
  } catch (e: Exception) {
124
- return@prompt nativeComplete(token, FAILED, null)
130
+ return@prompt nativeComplete(token, FAILED, null, detailOf(e))
125
131
  }
126
132
  try {
127
- nativeComplete(token, OK, plain)
133
+ nativeComplete(token, OK, plain, null)
128
134
  } finally {
129
135
  plain.fill(0)
130
136
  }
@@ -134,10 +140,15 @@ object Keystore {
134
140
  @JvmStatic
135
141
  fun authenticate(token: Long, title: String, description: String) {
136
142
  val activity = foregroundActivity()
137
- if (activity == null || !deviceSecure(activity)) {
138
- return nativeComplete(token, UNAVAILABLE, null)
143
+ if (activity == null) {
144
+ return nativeComplete(token, UNAVAILABLE, null, "android:no-activity")
145
+ }
146
+ if (!deviceSecure(activity)) {
147
+ return nativeComplete(token, NO_DEVICE_CREDENTIAL, null, null)
148
+ }
149
+ prompt(activity, title, description, null) { status, detail, _ ->
150
+ nativeComplete(token, status, null, detail)
139
151
  }
140
- prompt(activity, title, description, null) { status, _ -> nativeComplete(token, status, null) }
141
152
  }
142
153
 
143
154
  @JvmStatic
@@ -149,7 +160,7 @@ object Keystore {
149
160
  } catch (e: Exception) {
150
161
  FAILED
151
162
  }
152
- nativeComplete(token, status, null)
163
+ nativeComplete(token, status, null, null)
153
164
  }
154
165
 
155
166
  @JvmStatic
@@ -203,7 +214,7 @@ object Keystore {
203
214
  title: String,
204
215
  description: String,
205
216
  crypto: BiometricPrompt.CryptoObject?,
206
- done: (Int, BiometricPrompt.CryptoObject?) -> Unit,
217
+ done: (Int, String?, BiometricPrompt.CryptoObject?) -> Unit,
207
218
  ) {
208
219
  val executor = ContextCompat.getMainExecutor(activity)
209
220
  executor.execute { whenResumed(activity) { show(activity, executor, title, description, crypto, done) } }
@@ -232,7 +243,7 @@ object Keystore {
232
243
  title: String,
233
244
  description: String,
234
245
  crypto: BiometricPrompt.CryptoObject?,
235
- done: (Int, BiometricPrompt.CryptoObject?) -> Unit,
246
+ done: (Int, String?, BiometricPrompt.CryptoObject?) -> Unit,
236
247
  ) {
237
248
  val info = BiometricPrompt.PromptInfo.Builder()
238
249
  .setTitle(title)
@@ -245,12 +256,13 @@ object Keystore {
245
256
  object : BiometricPrompt.AuthenticationCallback() {
246
257
  override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
247
258
  current = null
248
- done(OK, result.cryptoObject)
259
+ done(OK, null, result.cryptoObject)
249
260
  }
250
261
 
251
262
  override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
252
263
  current = null
253
- done(statusOf(errorCode), null)
264
+ // The numeric code only — errString is localized free text.
265
+ done(statusOf(errorCode), "android:BiometricPrompt:$errorCode", null)
254
266
  }
255
267
  },
256
268
  )
@@ -259,7 +271,7 @@ object Keystore {
259
271
  if (crypto == null) biometricPrompt.authenticate(info) else biometricPrompt.authenticate(info, crypto)
260
272
  } catch (e: Exception) {
261
273
  current = null
262
- done(FAILED, null)
274
+ done(FAILED, detailOf(e), null)
263
275
  }
264
276
  }
265
277
 
@@ -268,13 +280,16 @@ object Keystore {
268
280
  BiometricPrompt.ERROR_NEGATIVE_BUTTON,
269
281
  BiometricPrompt.ERROR_CANCELED,
270
282
  BiometricPrompt.ERROR_TIMEOUT -> CANCELLED
271
- BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL,
283
+ BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL -> NO_DEVICE_CREDENTIAL
272
284
  BiometricPrompt.ERROR_HW_UNAVAILABLE,
273
285
  BiometricPrompt.ERROR_HW_NOT_PRESENT,
274
286
  BiometricPrompt.ERROR_NO_BIOMETRICS -> UNAVAILABLE
275
287
  else -> FAILED
276
288
  }
277
289
 
290
+ // The exception's simple class name only — the message can name the account.
291
+ private fun detailOf(e: Exception): String = "android:" + e.javaClass.simpleName
292
+
278
293
  @JvmStatic
279
- private external fun nativeComplete(token: Long, status: Int, bytes: ByteArray?)
294
+ private external fun nativeComplete(token: Long, status: Int, bytes: ByteArray?, detail: String?)
280
295
  }
@@ -44,6 +44,8 @@ object Passkey {
44
44
  private const val CANCELLED = 1
45
45
  private const val UNAVAILABLE = 3
46
46
  private const val FAILED = 4
47
+ private const val NOT_CONFIGURED = 5
48
+ private const val UNSUPPORTED = 6
47
49
  // The exception type only — never the message, which can name the account.
48
50
  private const val TAG = "QntxPasskey"
49
51
 
@@ -51,7 +53,8 @@ object Passkey {
51
53
 
52
54
  @JvmStatic
53
55
  fun create(token: Long, requestJson: String) {
54
- val activity = foregroundActivity() ?: return nativeComplete(token, UNAVAILABLE, null)
56
+ val activity = foregroundActivity()
57
+ ?: return nativeComplete(token, UNAVAILABLE, null, "android:no-activity")
55
58
  val cancel = CancellationSignal().also { signal = it }
56
59
  CredentialManager.create(activity).createCredentialAsync(
57
60
  activity,
@@ -66,13 +69,14 @@ object Passkey {
66
69
  token,
67
70
  if (response == null) FAILED else OK,
68
71
  response?.registrationResponseJson,
72
+ null,
69
73
  )
70
74
  }
71
75
 
72
76
  override fun onError(e: CreateCredentialException) {
73
77
  signal = null
74
78
  Log.w(TAG, "create failed: ${e.type}")
75
- nativeComplete(token, statusOf(e), null)
79
+ nativeComplete(token, statusOf(e), null, detailOf(e))
76
80
  }
77
81
  },
78
82
  )
@@ -80,7 +84,8 @@ object Passkey {
80
84
 
81
85
  @JvmStatic
82
86
  fun get(token: Long, requestJson: String) {
83
- val activity = foregroundActivity() ?: return nativeComplete(token, UNAVAILABLE, null)
87
+ val activity = foregroundActivity()
88
+ ?: return nativeComplete(token, UNAVAILABLE, null, "android:no-activity")
84
89
  val cancel = CancellationSignal().also { signal = it }
85
90
  CredentialManager.create(activity).getCredentialAsync(
86
91
  activity,
@@ -95,13 +100,14 @@ object Passkey {
95
100
  token,
96
101
  if (credential == null) FAILED else OK,
97
102
  credential?.authenticationResponseJson,
103
+ null,
98
104
  )
99
105
  }
100
106
 
101
107
  override fun onError(e: GetCredentialException) {
102
108
  signal = null
103
109
  Log.w(TAG, "get failed: ${e.type}")
104
- nativeComplete(token, statusOf(e), null)
110
+ nativeComplete(token, statusOf(e), null, detailOf(e))
105
111
  }
106
112
  },
107
113
  )
@@ -121,27 +127,36 @@ object Passkey {
121
127
  private fun statusOf(e: CreateCredentialException): Int = when (e) {
122
128
  is CreateCredentialCancellationException -> CANCELLED
123
129
  is CreateCredentialNoCreateOptionException,
124
- is CreateCredentialProviderConfigurationException,
125
- is CreateCredentialUnsupportedException -> UNAVAILABLE
130
+ is CreateCredentialProviderConfigurationException -> NOT_CONFIGURED
131
+ is CreateCredentialUnsupportedException -> UNSUPPORTED
126
132
  is CreatePublicKeyCredentialDomException -> domStatus(e.type)
127
133
  else -> FAILED
128
134
  }
129
135
 
130
136
  private fun statusOf(e: GetCredentialException): Int = when (e) {
131
137
  is GetCredentialCancellationException -> CANCELLED
132
- is NoCredentialException,
133
- is GetCredentialProviderConfigurationException,
134
- is GetCredentialUnsupportedException -> UNAVAILABLE
138
+ is GetCredentialProviderConfigurationException -> NOT_CONFIGURED
139
+ is GetCredentialUnsupportedException -> UNSUPPORTED
140
+ is NoCredentialException -> UNAVAILABLE
135
141
  is GetPublicKeyCredentialDomException -> domStatus(e.type)
136
142
  else -> FAILED
137
143
  }
138
144
 
139
145
  private fun domStatus(type: String): Int = when {
140
146
  type.endsWith("NotAllowedError") -> CANCELLED
141
- type.endsWith("SecurityError") || type.endsWith("NotSupportedError") -> UNAVAILABLE
147
+ type.endsWith("NotSupportedError") -> UNSUPPORTED
148
+ type.endsWith("SecurityError") -> UNAVAILABLE
142
149
  else -> FAILED
143
150
  }
144
151
 
152
+ // The exception's simple class name only — the message can name the account.
153
+ private fun detailOf(e: Exception): String = "android:" + e.javaClass.simpleName
154
+
145
155
  @JvmStatic
146
- private external fun nativeComplete(token: Long, status: Int, responseJson: String?)
156
+ private external fun nativeComplete(
157
+ token: Long,
158
+ status: Int,
159
+ responseJson: String?,
160
+ detail: String?,
161
+ )
147
162
  }
@@ -22,14 +22,24 @@ struct KeystorePrompt {
22
22
 
23
23
  // How a keystore call ended. The binding maps each status to the kernel's
24
24
  // error code: cancelled → "cancelled", invalidated → "factor-invalidated",
25
- // unavailable → "factor-unavailable", failed → "unavailable". The values are
26
- // part of the Android JNI contract (Keystore.kt).
27
- enum class KeystoreStatus { ok = 0, cancelled = 1, invalidated = 2, unavailable = 3, failed = 4 };
25
+ // unavailable → "unavailable", failed → "unavailable",
26
+ // noDeviceCredential → "no-device-credential". The values are part of the
27
+ // Android JNI contract (Keystore.kt).
28
+ enum class KeystoreStatus {
29
+ ok = 0,
30
+ cancelled = 1,
31
+ invalidated = 2,
32
+ unavailable = 3,
33
+ failed = 4,
34
+ noDeviceCredential = 5,
35
+ };
28
36
 
29
37
  struct KeystoreResult {
30
38
  KeystoreStatus status = KeystoreStatus::failed;
31
39
  // seal: the platform ciphertext; open: the KEK; empty otherwise.
32
40
  SecretBytes bytes;
41
+ // A "<domain>:<code>" diagnostic (e.g. "LAErrorDomain:-5"), empty when none.
42
+ std::string detail;
33
43
  };
34
44
 
35
45
  // Completion callbacks run exactly once, on any thread.
@@ -14,14 +14,24 @@ namespace qntx {
14
14
  // PasskeyIos.mm); host tests link a fake.
15
15
 
16
16
  // The binding maps each status to the kernel's error code: cancelled →
17
- // "cancelled", unavailable → "factor-unavailable", failed → "unavailable".
18
- // The values are part of the Android JNI contract (Passkey.kt).
19
- enum class PasskeyStatus { ok = 0, cancelled = 1, unavailable = 3, failed = 4 };
17
+ // "cancelled", unavailable → "unavailable", failed → "unavailable",
18
+ // notConfigured → "passkey-not-configured", unsupported → "unsupported". The
19
+ // values are part of the Android JNI contract (Passkey.kt).
20
+ enum class PasskeyStatus {
21
+ ok = 0,
22
+ cancelled = 1,
23
+ unavailable = 3,
24
+ failed = 4,
25
+ notConfigured = 5,
26
+ unsupported = 6,
27
+ };
20
28
 
21
29
  struct PasskeyResult {
22
30
  PasskeyStatus status = PasskeyStatus::failed;
23
31
  // The WebAuthn registration or authentication response JSON on success.
24
32
  std::string responseJson;
33
+ // A "<domain>:<code>" diagnostic (e.g. "ASAuthorizationErrorDomain:1004").
34
+ std::string detail;
25
35
  };
26
36
 
27
37
  // Completion callbacks run exactly once, on any thread.
@@ -153,14 +153,15 @@ std::string keystoreLocale() {
153
153
  // is copied into wiping storage; Kotlin wipes its own copy afterwards.
154
154
  extern "C" JNIEXPORT void JNICALL
155
155
  Java_com_margelo_nitro_qntxwallet_Keystore_nativeComplete(
156
- JNIEnv* env, jclass, jlong token, jint status, jbyteArray bytes) {
156
+ JNIEnv* env, jclass, jlong token, jint status, jbyteArray bytes, jstring detail) {
157
157
  auto done = qntx::take(static_cast<int64_t>(token));
158
158
  if (!done) {
159
159
  return;
160
160
  }
161
161
  qntx::KeystoreResult result;
162
162
  result.status = static_cast<qntx::KeystoreStatus>(status);
163
- if (status < 0 || status > static_cast<jint>(qntx::KeystoreStatus::failed)) {
163
+ if (status < 0 ||
164
+ status > static_cast<jint>(qntx::KeystoreStatus::noDeviceCredential)) {
164
165
  result.status = qntx::KeystoreStatus::failed;
165
166
  }
166
167
  if (bytes != nullptr && result.status == qntx::KeystoreStatus::ok) {
@@ -169,6 +170,13 @@ Java_com_margelo_nitro_qntxwallet_Keystore_nativeComplete(
169
170
  env->GetByteArrayRegion(bytes, 0, size, reinterpret_cast<jbyte*>(copy.data()));
170
171
  result.bytes = std::move(copy);
171
172
  }
173
+ if (detail != nullptr) {
174
+ const char* utf = env->GetStringUTFChars(detail, nullptr);
175
+ if (utf != nullptr) {
176
+ result.detail = utf;
177
+ env->ReleaseStringUTFChars(detail, utf);
178
+ }
179
+ }
172
180
  done(std::move(result));
173
181
  }
174
182
 
@@ -16,6 +16,8 @@
16
16
  #include <cstring>
17
17
  #include <memory>
18
18
  #include <mutex>
19
+ #include <optional>
20
+ #include <string>
19
21
  #include <utility>
20
22
 
21
23
  namespace qntx {
@@ -60,7 +62,7 @@ KeystoreStatus statusOf(NSError* error) {
60
62
  case LAErrorUserFallback:
61
63
  return KeystoreStatus::cancelled;
62
64
  case LAErrorPasscodeNotSet:
63
- return KeystoreStatus::unavailable;
65
+ return KeystoreStatus::noDeviceCredential;
64
66
  default:
65
67
  return KeystoreStatus::failed;
66
68
  }
@@ -82,6 +84,39 @@ KeystoreStatus statusOf(CFErrorRef error) {
82
84
  return statusOf((__bridge NSError*)error);
83
85
  }
84
86
 
87
+ // The "<domain>:<code>" diagnostic — never userInfo or localizedDescription,
88
+ // which can name the account.
89
+ std::string detailOf(NSError* error) {
90
+ if (error == nil) {
91
+ return {};
92
+ }
93
+ char buffer[96];
94
+ std::snprintf(
95
+ buffer,
96
+ sizeof(buffer),
97
+ "%s:%ld",
98
+ error.domain.UTF8String,
99
+ static_cast<long>(error.code));
100
+ return buffer;
101
+ }
102
+
103
+ std::string detailOf(CFErrorRef error) {
104
+ return detailOf((__bridge NSError*)error);
105
+ }
106
+
107
+ // A failed canEvaluatePolicy: LAErrorPasscodeNotSet is the one failure a user
108
+ // can act on (no device passcode → no hardware factor can exist), so it gets
109
+ // its own status; every other LAError is a plain platform failure.
110
+ KeystoreResult credentialProbeFailure(NSError* error) {
111
+ return {
112
+ error != nil && [error.domain isEqualToString:LAErrorDomain] &&
113
+ error.code == LAErrorPasscodeNotSet
114
+ ? KeystoreStatus::noDeviceCredential
115
+ : KeystoreStatus::unavailable,
116
+ {},
117
+ detailOf(error)};
118
+ }
119
+
85
120
  void deleteKey(NSData* tag) {
86
121
  SecItemDelete((__bridge CFDictionaryRef) @{
87
122
  (id)kSecClass : (id)kSecClassKey,
@@ -89,14 +124,20 @@ void deleteKey(NSData* tag) {
89
124
  });
90
125
  }
91
126
 
92
- bool deviceOwnerAuthenticationAvailable() {
127
+ // The device-credential gate: seal cannot create a hardware key while the
128
+ // policy cannot be evaluated at all.
129
+ std::optional<KeystoreResult> deviceCredentialMissing() {
93
130
  LAContext* probe = [LAContext new];
94
- return [probe canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:nil];
131
+ NSError* error = nil;
132
+ if ([probe canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error]) {
133
+ return std::nullopt;
134
+ }
135
+ return credentialProbeFailure(error);
95
136
  }
96
137
 
97
138
  KeystoreResult sealNow(const std::string& factorId, const SecretBytes& secret) {
98
- if (!deviceOwnerAuthenticationAvailable()) {
99
- return {KeystoreStatus::unavailable, {}};
139
+ if (auto missing = deviceCredentialMissing()) {
140
+ return std::move(*missing);
100
141
  }
101
142
  NSData* tag = tagFor(factorId);
102
143
  deleteKey(tag);
@@ -108,10 +149,11 @@ KeystoreResult sealNow(const std::string& factorId, const SecretBytes& secret) {
108
149
  SecAccessControlRef access = SecAccessControlCreateWithFlags(
109
150
  kCFAllocatorDefault, kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly, flags, &error);
110
151
  if (access == nullptr) {
152
+ KeystoreResult out{KeystoreStatus::unavailable, {}, detailOf(error)};
111
153
  if (error != nullptr) {
112
154
  CFRelease(error);
113
155
  }
114
- return {KeystoreStatus::unavailable, {}};
156
+ return out;
115
157
  }
116
158
  NSMutableDictionary* attributes = [@{
117
159
  (id)kSecAttrKeyType : (id)kSecAttrKeyTypeECSECPrimeRandom,
@@ -128,10 +170,11 @@ KeystoreResult sealNow(const std::string& factorId, const SecretBytes& secret) {
128
170
  SecKeyRef privateKey = SecKeyCreateRandomKey((__bridge CFDictionaryRef)attributes, &error);
129
171
  CFRelease(access);
130
172
  if (privateKey == nullptr) {
173
+ KeystoreResult out{KeystoreStatus::unavailable, {}, detailOf(error)};
131
174
  if (error != nullptr) {
132
175
  CFRelease(error);
133
176
  }
134
- return {KeystoreStatus::unavailable, {}};
177
+ return out;
135
178
  }
136
179
  SecKeyRef publicKey = SecKeyCopyPublicKey(privateKey);
137
180
  CFRelease(privateKey);
@@ -145,11 +188,12 @@ KeystoreResult sealNow(const std::string& factorId, const SecretBytes& secret) {
145
188
  CFRelease(plain);
146
189
  CFRelease(publicKey);
147
190
  if (sealed == nullptr) {
191
+ KeystoreResult out{KeystoreStatus::unavailable, {}, detailOf(error)};
148
192
  if (error != nullptr) {
149
193
  CFRelease(error);
150
194
  }
151
195
  deleteKey(tag);
152
- return {KeystoreStatus::failed, {}};
196
+ return out;
153
197
  }
154
198
  SecretBytes out(CFDataGetBytePtr(sealed), static_cast<size_t>(CFDataGetLength(sealed)));
155
199
  CFRelease(sealed);
@@ -239,9 +283,9 @@ void keystoreAuthenticate(KeystorePrompt prompt, KeystoreDone done) {
239
283
  dispatch_async(keystoreQueue(), ^{
240
284
  LAContext* context = [LAContext new];
241
285
  context.touchIDAuthenticationAllowableReuseDuration = 0;
242
- NSError* unavailable = nil;
243
- if (![context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&unavailable]) {
244
- (*finish)(KeystoreResult{KeystoreStatus::unavailable, {}});
286
+ NSError* error = nil;
287
+ if (![context canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&error]) {
288
+ (*finish)(credentialProbeFailure(error));
245
289
  return;
246
290
  }
247
291
  setCurrentContext(context);
@@ -90,7 +90,7 @@ void passkeyCancel() {
90
90
 
91
91
  extern "C" JNIEXPORT void JNICALL
92
92
  Java_com_margelo_nitro_qntxwallet_Passkey_nativeComplete(
93
- JNIEnv* env, jclass, jlong token, jint status, jstring responseJson) {
93
+ JNIEnv* env, jclass, jlong token, jint status, jstring responseJson, jstring detail) {
94
94
  auto done = qntx::take(static_cast<int64_t>(token));
95
95
  if (!done) {
96
96
  return;
@@ -100,6 +100,8 @@ Java_com_margelo_nitro_qntxwallet_Passkey_nativeComplete(
100
100
  case static_cast<jint>(qntx::PasskeyStatus::ok):
101
101
  case static_cast<jint>(qntx::PasskeyStatus::cancelled):
102
102
  case static_cast<jint>(qntx::PasskeyStatus::unavailable):
103
+ case static_cast<jint>(qntx::PasskeyStatus::notConfigured):
104
+ case static_cast<jint>(qntx::PasskeyStatus::unsupported):
103
105
  result.status = static_cast<qntx::PasskeyStatus>(status);
104
106
  break;
105
107
  default:
@@ -112,6 +114,13 @@ Java_com_margelo_nitro_qntxwallet_Passkey_nativeComplete(
112
114
  env->ReleaseStringUTFChars(responseJson, utf);
113
115
  }
114
116
  }
117
+ if (detail != nullptr) {
118
+ const char* utf = env->GetStringUTFChars(detail, nullptr);
119
+ if (utf != nullptr) {
120
+ result.detail = utf;
121
+ env->ReleaseStringUTFChars(detail, utf);
122
+ }
123
+ }
115
124
  done(std::move(result));
116
125
  }
117
126
 
@@ -145,6 +145,22 @@ std::string jsonString(NSDictionary<NSString*, id>* json) {
145
145
  return std::string(static_cast<const char*>(data.bytes), data.length);
146
146
  }
147
147
 
148
+ // The "<domain>:<code>" diagnostic — never userInfo or localizedDescription,
149
+ // which can name the account.
150
+ std::string detailOf(NSError* error) {
151
+ if (error == nil) {
152
+ return {};
153
+ }
154
+ char buffer[96];
155
+ std::snprintf(
156
+ buffer,
157
+ sizeof(buffer),
158
+ "%s:%ld",
159
+ error.domain.UTF8String,
160
+ static_cast<long>(error.code));
161
+ return buffer;
162
+ }
163
+
148
164
  } // namespace
149
165
 
150
166
  API_AVAILABLE(ios(18.0))
@@ -197,7 +213,8 @@ API_AVAILABLE(ios(18.0))
197
213
  }];
198
214
  return;
199
215
  }
200
- [self finish:qntx::PasskeyResult{qntx::PasskeyStatus::unavailable, {}}];
216
+ [self finish:qntx::PasskeyResult{
217
+ qntx::PasskeyStatus::unavailable, {}, "ios:no-key-window"}];
201
218
  }
202
219
 
203
220
  - (void)sceneActivated {
@@ -205,7 +222,8 @@ API_AVAILABLE(ios(18.0))
205
222
  _observer = nil;
206
223
  ASPresentationAnchor anchor = activeKeyWindow();
207
224
  if (anchor == nil) {
208
- [self finish:qntx::PasskeyResult{qntx::PasskeyStatus::unavailable, {}}];
225
+ [self finish:qntx::PasskeyResult{
226
+ qntx::PasskeyStatus::unavailable, {}, "ios:no-key-window"}];
209
227
  return;
210
228
  }
211
229
  [self performWithAnchor:anchor];
@@ -329,6 +347,8 @@ API_AVAILABLE(ios(18.0))
329
347
  status = qntx::PasskeyStatus::cancelled;
330
348
  break;
331
349
  case ASAuthorizationErrorNotHandled:
350
+ status = qntx::PasskeyStatus::notConfigured;
351
+ break;
332
352
  case ASAuthorizationErrorFailed:
333
353
  case ASAuthorizationErrorNotInteractive:
334
354
  status = qntx::PasskeyStatus::unavailable;
@@ -336,7 +356,7 @@ API_AVAILABLE(ios(18.0))
336
356
  default:
337
357
  // 1010 = ASAuthorizationErrorDeviceNotConfiguredForPasskeyCreation,
338
358
  // compared raw so the iOS 19 symbol needs no availability guard.
339
- status = error.code == 1010 ? qntx::PasskeyStatus::unavailable
359
+ status = error.code == 1010 ? qntx::PasskeyStatus::notConfigured
340
360
  : qntx::PasskeyStatus::failed;
341
361
  }
342
362
  }
@@ -346,7 +366,7 @@ API_AVAILABLE(ios(18.0))
346
366
  "passkey ceremony failed: %{public}@/%ld",
347
367
  error.domain,
348
368
  static_cast<long>(error.code));
349
- [self finish:qntx::PasskeyResult{status, {}}];
369
+ [self finish:qntx::PasskeyResult{status, {}, detailOf(error)}];
350
370
  }
351
371
 
352
372
  @end
@@ -452,7 +472,7 @@ void passkeyCreate(std::string requestJson, PasskeyDone done) {
452
472
  if (@available(iOS 18.0, *)) {
453
473
  run(registrationRequest(jsonOf(*json)), std::move(*finish));
454
474
  } else {
455
- (*finish)(PasskeyResult{PasskeyStatus::unavailable, {}});
475
+ (*finish)(PasskeyResult{PasskeyStatus::unsupported, {}, "ios:passkeys-unavailable"});
456
476
  }
457
477
  });
458
478
  }
@@ -464,7 +484,7 @@ void passkeyGet(std::string requestJson, PasskeyDone done) {
464
484
  if (@available(iOS 18.0, *)) {
465
485
  run(assertionRequest(jsonOf(*json)), std::move(*finish));
466
486
  } else {
467
- (*finish)(PasskeyResult{PasskeyStatus::unavailable, {}});
487
+ (*finish)(PasskeyResult{PasskeyStatus::unsupported, {}, "ios:passkeys-unavailable"});
468
488
  }
469
489
  });
470
490
  }