@circle-fin/daa-react-native-sdk 0.4.0 → 0.5.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 (157) hide show
  1. package/DaaReactNativeSdk.podspec +14 -8
  2. package/README.md +498 -24
  3. package/android/build.gradle +12 -7
  4. package/android/src/main/java/com/circle/daa/reactnative/DaaRegistrationModule.kt +1 -18
  5. package/android/src/main/java/com/circle/daa/reactnative/DaaSecureKeyExceptions.kt +38 -4
  6. package/android/src/main/java/com/circle/daa/reactnative/DaaSecureKeyModule.kt +139 -6
  7. package/android/src/main/java/com/circle/daa/reactnative/InstallationIdentityStore.kt +50 -1
  8. package/build/DaaClient.d.ts +183 -3
  9. package/build/DaaClient.js +296 -12
  10. package/build/api/AccountsClient.d.ts +3 -0
  11. package/build/api/AccountsClient.js +3 -0
  12. package/build/api/AuthClient.d.ts +17 -42
  13. package/build/api/AuthClient.js +26 -62
  14. package/build/api/ContractExecutionsClient.d.ts +114 -0
  15. package/build/api/ContractExecutionsClient.js +462 -0
  16. package/build/api/DaaUserClient.d.ts +74 -11
  17. package/build/api/DaaUserClient.js +84 -17
  18. package/build/api/TransactionsClient.d.ts +1 -1
  19. package/build/api/TransactionsClient.js +1 -0
  20. package/build/api/TransfersClient.d.ts +3 -0
  21. package/build/api/TransfersClient.js +3 -0
  22. package/build/api/WithdrawalsClient.d.ts +3 -0
  23. package/build/api/WithdrawalsClient.js +3 -0
  24. package/build/api/deviceApproval.d.ts +48 -0
  25. package/build/api/deviceApproval.js +247 -0
  26. package/build/api/deviceStatus.d.ts +61 -0
  27. package/build/api/deviceStatus.js +216 -0
  28. package/build/api/loginFailure.d.ts +10 -2
  29. package/build/api/loginFailure.js +33 -2
  30. package/build/api/signedResource.d.ts +49 -2
  31. package/build/api/signedResource.js +135 -36
  32. package/build/auth/RegistrationFlowKey.js +1 -1
  33. package/build/auth/daaRegistrationNative.d.ts +0 -22
  34. package/build/auth/daaRegistrationNative.js +3 -29
  35. package/build/auth/deviceAuthorization.d.ts +17 -3
  36. package/build/auth/deviceAuthorization.js +60 -8
  37. package/build/auth/registrationAuthorization.d.ts +2 -2
  38. package/build/auth/registrationAuthorization.js +13 -10
  39. package/build/config.d.ts +27 -0
  40. package/build/config.js +15 -0
  41. package/build/crypto/matchingCode.d.ts +47 -0
  42. package/build/crypto/matchingCode.js +93 -0
  43. package/build/errors.d.ts +150 -33
  44. package/build/errors.js +208 -47
  45. package/build/index.d.ts +11 -3
  46. package/build/models/ContractExecution.d.ts +197 -0
  47. package/build/models/ContractExecution.js +130 -0
  48. package/build/models/CreateContractExecutionRequest.d.ts +81 -0
  49. package/build/models/CreateContractExecutionRequest.js +19 -0
  50. package/build/models/DaaUserContext.d.ts +53 -3
  51. package/build/models/DaaUserContext.js +33 -2
  52. package/build/models/DeviceStatus.d.ts +69 -0
  53. package/build/models/DeviceStatus.js +19 -0
  54. package/build/models/PendingDeviceApproval.d.ts +121 -0
  55. package/build/models/PendingDeviceApproval.js +208 -0
  56. package/build/models/PendingDeviceRegistration.d.ts +162 -23
  57. package/build/models/PendingDeviceRegistration.js +525 -69
  58. package/build/models/PreparedContractExecution.d.ts +70 -0
  59. package/build/models/PreparedContractExecution.js +77 -0
  60. package/build/models/PreparedSocialLogin.d.ts +19 -26
  61. package/build/models/PreparedSocialLogin.js +32 -15
  62. package/build/models/Transaction.d.ts +48 -2
  63. package/build/models/Transaction.js +52 -4
  64. package/build/models/codeSecrets.d.ts +100 -0
  65. package/build/models/codeSecrets.js +321 -0
  66. package/build/securekey/InMemorySecureKey.d.ts +3 -1
  67. package/build/securekey/InMemorySecureKey.js +47 -6
  68. package/build/securekey/NativeSecureKey.d.ts +3 -1
  69. package/build/securekey/NativeSecureKey.js +204 -27
  70. package/build/securekey/NativeSecureKeyBridge.d.ts +72 -4
  71. package/build/securekey/SecureKey.d.ts +128 -8
  72. package/build/securekey/alias.d.ts +9 -0
  73. package/build/securekey/alias.js +11 -2
  74. package/build/securekey/ownerAuthKeyResolver.d.ts +26 -9
  75. package/build/securekey/ownerAuthKeyResolver.js +35 -20
  76. package/build/securekey/registration.d.ts +19 -0
  77. package/build/securekey/registration.js +38 -0
  78. package/build/securekey/resolveNativeBridge.d.ts +3 -0
  79. package/build/securekey/resolveNativeBridge.js +88 -7
  80. package/build/transport/headers.d.ts +11 -0
  81. package/build/transport/headers.js +36 -0
  82. package/build/wire/contractExecutionPayload.d.ts +24 -0
  83. package/build/wire/contractExecutionPayload.js +130 -0
  84. package/build/wire/daaStatusCodes.d.ts +20 -3
  85. package/build/wire/daaStatusCodes.js +20 -3
  86. package/build/wire/deviceContextPayload.d.ts +9 -7
  87. package/build/wire/deviceContextPayload.js +9 -7
  88. package/build/wire/deviceRegistrationResultPayload.d.ts +55 -10
  89. package/build/wire/deviceRegistrationResultPayload.js +116 -82
  90. package/build/wire/deviceStatusPayload.d.ts +20 -0
  91. package/build/wire/deviceStatusPayload.js +66 -0
  92. package/build/wire/instant.js +4 -1
  93. package/build/wire/pendingRegistrationPayload.d.ts +16 -0
  94. package/build/wire/pendingRegistrationPayload.js +18 -0
  95. package/build/wire/socialLoginRequestPayload.d.ts +0 -5
  96. package/build/wire/socialLoginRequestPayload.js +2 -19
  97. package/build/wire/transactionPayload.d.ts +2 -2
  98. package/build/wire/transactionPayload.js +27 -4
  99. package/ios/DaaRegistrationModule.swift +1 -19
  100. package/ios/DaaSecureKeyExceptions.swift +53 -5
  101. package/ios/DaaSecureKeyModule.swift +149 -5
  102. package/ios/InstallationIdentityStore.swift +43 -7
  103. package/package.json +1 -1
  104. package/src/DaaClient.ts +353 -15
  105. package/src/api/AccountsClient.ts +3 -0
  106. package/src/api/AuthClient.ts +33 -98
  107. package/src/api/ContractExecutionsClient.ts +527 -0
  108. package/src/api/DaaUserClient.ts +92 -17
  109. package/src/api/TransactionsClient.ts +1 -0
  110. package/src/api/TransfersClient.ts +3 -0
  111. package/src/api/WithdrawalsClient.ts +3 -0
  112. package/src/api/deviceApproval.ts +306 -0
  113. package/src/api/deviceStatus.ts +238 -0
  114. package/src/api/loginFailure.ts +39 -2
  115. package/src/api/signedResource.ts +206 -46
  116. package/src/auth/RegistrationFlowKey.ts +1 -1
  117. package/src/auth/daaRegistrationNative.ts +4 -49
  118. package/src/auth/deviceAuthorization.ts +74 -11
  119. package/src/auth/registrationAuthorization.ts +12 -9
  120. package/src/config.ts +36 -0
  121. package/src/crypto/matchingCode.ts +100 -0
  122. package/src/errors.ts +309 -52
  123. package/src/index.ts +36 -1
  124. package/src/models/ContractExecution.ts +250 -0
  125. package/src/models/CreateContractExecutionRequest.ts +100 -0
  126. package/src/models/DaaUserContext.ts +59 -3
  127. package/src/models/DeviceStatus.ts +50 -0
  128. package/src/models/PendingDeviceApproval.ts +229 -0
  129. package/src/models/PendingDeviceRegistration.ts +673 -79
  130. package/src/models/PreparedContractExecution.ts +128 -0
  131. package/src/models/PreparedSocialLogin.ts +41 -31
  132. package/src/models/Transaction.ts +77 -2
  133. package/src/models/codeSecrets.ts +360 -0
  134. package/src/securekey/InMemorySecureKey.ts +61 -7
  135. package/src/securekey/NativeSecureKey.ts +234 -25
  136. package/src/securekey/NativeSecureKeyBridge.ts +84 -4
  137. package/src/securekey/SecureKey.ts +138 -8
  138. package/src/securekey/alias.ts +11 -3
  139. package/src/securekey/ownerAuthKeyResolver.ts +63 -24
  140. package/src/securekey/registration.ts +40 -0
  141. package/src/securekey/resolveNativeBridge.ts +167 -9
  142. package/src/transport/headers.ts +47 -0
  143. package/src/wire/contractExecutionPayload.ts +152 -0
  144. package/src/wire/daaStatusCodes.ts +20 -3
  145. package/src/wire/deviceContextPayload.ts +9 -7
  146. package/src/wire/deviceRegistrationResultPayload.ts +156 -98
  147. package/src/wire/deviceStatusPayload.ts +68 -0
  148. package/src/wire/instant.ts +4 -1
  149. package/src/wire/pendingRegistrationPayload.ts +44 -1
  150. package/src/wire/socialLoginRequestPayload.ts +2 -25
  151. package/src/wire/transactionPayload.ts +40 -4
  152. package/build/auth/prepareReceipt.d.ts +0 -150
  153. package/build/auth/prepareReceipt.js +0 -357
  154. package/build/crypto/es256Jws.d.ts +0 -56
  155. package/build/crypto/es256Jws.js +0 -196
  156. package/src/auth/prepareReceipt.ts +0 -522
  157. package/src/crypto/es256Jws.ts +0 -240
@@ -21,8 +21,7 @@ import Foundation
21
21
  // Thin Expo bridge to the native registration primitives. THIS IS NOT
22
22
  // DaaSecureKey. Verifier bytes come from DaaSecureRandom; the ephemeral
23
23
  // flow key is DaaRegistrationFlowKey, cached here so the private key never
24
- // crosses into JavaScript; the Prepare receipt signature is checked by
25
- // DaaPrepareReceiptSignature.
24
+ // crosses into JavaScript.
26
25
 
27
26
  public class DaaRegistrationModule: Module {
28
27
  private let flowKeys = RegistrationFlowKeyCache()
@@ -80,23 +79,6 @@ public class DaaRegistrationModule: Module {
80
79
  throw mappedRegistrationException(error)
81
80
  }
82
81
  }
83
-
84
- // Verify-only, against a key JS pinned from the SDK's own trust anchor —
85
- // never a key from the token. DaaPrepareReceiptSignature (daa-ios-sdk) does
86
- // the check with CryptoKit: a signature problem is `false`, and a key that
87
- // is not a P-256 point throws and maps to CONTRACT_VIOLATION.
88
- AsyncFunction("verifyPrepareReceiptSignature") {
89
- (publicKeyX963Base64Url: String, signingInputBase64Url: String, signatureBase64Url: String) throws -> Bool in
90
- do {
91
- return try DaaPrepareReceiptSignature.verify(
92
- publicKeyX963: try decodeUnpaddedBase64URL(publicKeyX963Base64Url),
93
- signingInput: try decodeUnpaddedBase64URL(signingInputBase64Url),
94
- signature: try decodeUnpaddedBase64URL(signatureBase64Url)
95
- )
96
- } catch {
97
- throw mappedRegistrationException(error)
98
- }
99
- }
100
82
  }
101
83
  }
102
84
 
@@ -19,20 +19,61 @@ import ExpoModulesCore
19
19
 
20
20
  // Stable `code` values for mapNativeError. Reasons are canned — never copy
21
21
  // Keychain / Secure Enclave text into the JS message.
22
+ //
23
+ // Every function this module exposes is async, and an async function's
24
+ // rejection reaches JS without the exception's code (Expo wraps it as
25
+ // "Calling the '…' function has failed → Caused by: …"), so each reason also
26
+ // starts with its code in brackets. mapNativeError reads that tag from the
27
+ // innermost cause when no known code survives — the same rule DaaSca uses
28
+ // (see DaaScaExceptions.swift).
29
+
30
+ private func secureKeyReason(_ code: String, _ detail: String) -> String {
31
+ "[\(code)] \(detail)"
32
+ }
22
33
 
23
34
  internal final class UserAuthRequiredException: Exception, @unchecked Sendable {
24
35
  override var code: String { "USER_AUTH_REQUIRED" }
25
- override var reason: String { "User authentication is required" }
36
+ override var reason: String { secureKeyReason(code, "User authentication is required") }
37
+ }
38
+
39
+ // The user dismissed the LocalAuthentication prompt. Kept distinct from
40
+ // USER_AUTH_REQUIRED: a cancel is the user's choice, not a credential problem,
41
+ // and the TypeScript side reports it as `user_cancelled`.
42
+ internal final class UserCancelledException: Exception, @unchecked Sendable {
43
+ override var code: String { "USER_CANCELLED" }
44
+ override var reason: String { secureKeyReason(code, "User authentication was cancelled") }
26
45
  }
27
46
 
28
47
  internal final class NotInitializedException: Exception, @unchecked Sendable {
29
48
  override var code: String { "NOT_INITIALIZED" }
30
- override var reason: String { "Secure key is not initialized" }
49
+ override var reason: String { secureKeyReason(code, "Secure key is not initialized") }
50
+ }
51
+
52
+ // No passcode is set, so no device key can be created. The user fixes it in
53
+ // Settings; retrying does not help.
54
+ internal final class DeviceLockNotSetException: Exception, @unchecked Sendable {
55
+ override var code: String { "DEVICE_LOCK_NOT_SET" }
56
+ override var reason: String { secureKeyReason(code, "The device has no passcode set") }
57
+ }
58
+
59
+ // No Secure Enclave on this device (or simulator runtime), so it can never hold
60
+ // a device key.
61
+ internal final class DeviceNotSupportedException: Exception, @unchecked Sendable {
62
+ override var code: String { "DEVICE_NOT_SUPPORTED" }
63
+ override var reason: String { secureKeyReason(code, "The device has no secure hardware for a device key") }
64
+ }
65
+
66
+ // The typed matching code is not the code the approval body and code secret
67
+ // produce. Checked before the Face ID / Touch ID prompt; the user can type it
68
+ // again.
69
+ internal final class MatchingCodeMismatchException: Exception, @unchecked Sendable {
70
+ override var code: String { "MATCHING_CODE_MISMATCH" }
71
+ override var reason: String { secureKeyReason(code, "The typed code is not the matching code") }
31
72
  }
32
73
 
33
74
  internal final class ContractViolationException: Exception, @unchecked Sendable {
34
75
  override var code: String { "CONTRACT_VIOLATION" }
35
- override var reason: String { "Native secure-key operation failed" }
76
+ override var reason: String { secureKeyReason(code, "Native secure-key operation failed") }
36
77
  }
37
78
 
38
79
  internal func mappedSecureKeyException(_ error: Error) -> Exception {
@@ -43,14 +84,21 @@ internal func mappedSecureKeyException(_ error: Error) -> Exception {
43
84
  return ContractViolationException()
44
85
  }
45
86
  switch keyError {
46
- case .authenticationFailed, .userCancelled, .deviceLocked, .devicePasscodeNotSet:
87
+ case .userCancelled:
88
+ return UserCancelledException()
89
+ case .authenticationFailed, .deviceLocked:
47
90
  return UserAuthRequiredException()
91
+ case .devicePasscodeNotSet:
92
+ return DeviceLockNotSetException()
93
+ case .secureEnclaveUnavailable:
94
+ return DeviceNotSupportedException()
48
95
  case .keyNotFound, .resolutionRequired:
49
96
  return NotInitializedException()
97
+ case .matchingCodeMismatch:
98
+ return MatchingCodeMismatchException()
50
99
  case .invalidAppID,
51
100
  .invalidKeychainAccessGroup,
52
101
  .invalidSigningInput,
53
- .secureEnclaveUnavailable,
54
102
  .keyCreationFailed,
55
103
  .keyAccessFailed,
56
104
  .keychainEntitlementMissing,
@@ -64,12 +64,61 @@ public class DaaSecureKeyModule: Module, @unchecked Sendable {
64
64
  }
65
65
  }
66
66
 
67
- // Reached from OwnerAuthKey.acknowledgeRegistration() via the TS bridge.
68
- // This is the only registration-ack entry point; do not add a parallel one.
69
- AsyncFunction("acknowledgeRegistration") { (appId: String, environment: String) async throws in
67
+ // Reached from OwnerAuthKey.acknowledgeRegistration(registration) via the
68
+ // TS bridge. This is the only registration-ack entry point; do not add a
69
+ // parallel one. Native always upserts the identifiers into the durable
70
+ // registration record, so a re-registration that assigned new ones
71
+ // replaces the old ones. The deprecated no-identifier variant is not
72
+ // reached: it records nothing resolveExisting could return.
73
+ AsyncFunction("acknowledgeRegistration") { (
74
+ appId: String,
75
+ environment: String,
76
+ authKeyId: String,
77
+ ownerBindingId: String
78
+ ) async throws in
70
79
  do {
71
80
  let key = try await cache.secureKey(appId: appId, environment: environment)
72
- try await key.acknowledgeRegistration()
81
+ try await key.acknowledgeRegistration(
82
+ authKeyId: authKeyId,
83
+ ownerBindingId: ownerBindingId
84
+ )
85
+ } catch {
86
+ throw mappedSecureKeyException(error)
87
+ }
88
+ }
89
+
90
+ // Read-only lookup: never mints a key and never shows a
91
+ // LocalAuthentication prompt. Returns the registration record, or nil
92
+ // (JS null) when there is no key or it has no recorded identifiers.
93
+ AsyncFunction("resolveExisting") { (appId: String, environment: String) async throws -> [String: String]? in
94
+ do {
95
+ // existingSecureKey, not secureKey: this lookup must not mint an
96
+ // installation ID, or replace a malformed one, on its way to "no key".
97
+ guard let key = try await cache.existingSecureKey(appId: appId, environment: environment),
98
+ let registered = try await key.resolveExisting()
99
+ else {
100
+ return nil
101
+ }
102
+ return [
103
+ "authKeyId": registered.authKeyId,
104
+ "ownerBindingId": registered.ownerBindingId,
105
+ ]
106
+ } catch {
107
+ throw mappedSecureKeyException(error)
108
+ }
109
+ }
110
+
111
+ // Explicit, user-initiated local delete of the key and its metadata. Local
112
+ // only: it does not revoke the key with Circle. Idempotent.
113
+ AsyncFunction("forgetLocalKey") { (appId: String, environment: String) async throws in
114
+ do {
115
+ // existingSecureKey, not secureKey: a delete must not mint an
116
+ // installation ID. With none, no key can exist, so there is nothing
117
+ // to delete.
118
+ guard let key = try await cache.existingSecureKey(appId: appId, environment: environment) else {
119
+ return
120
+ }
121
+ try await key.forgetLocalKey()
73
122
  } catch {
74
123
  throw mappedSecureKeyException(error)
75
124
  }
@@ -96,6 +145,68 @@ public class DaaSecureKeyModule: Module, @unchecked Sendable {
96
145
  }
97
146
  }
98
147
 
148
+ // Typed native device approval — signDeviceAuthorization with the
149
+ // approve_device initializer of DaaDeviceAuthorizationSigningInput, which
150
+ // checks that the approval body is the one the signing input commits to,
151
+ // that the code secret opens its codeSeal, and that enteredCode is the
152
+ // code they produce, all before the Face ID / Touch ID prompt. A wrong
153
+ // code throws matchingCodeMismatch (MATCHING_CODE_MISMATCH). The prompt
154
+ // shows the code; nothing here sets its text.
155
+ AsyncFunction("signDeviceApproval") { (
156
+ appId: String,
157
+ environment: String,
158
+ signingInputBase64Url: String,
159
+ requestBodyBase64Url: String,
160
+ codeSecretBase64Url: String,
161
+ enteredCode: String
162
+ ) async throws -> String in
163
+ do {
164
+ let signingInput = try DaaDeviceAuthorizationSigningInput(
165
+ exactBytes: try decodeUnpaddedBase64URL(signingInputBase64Url),
166
+ approvalRequestBody: try decodeUnpaddedBase64URL(requestBodyBase64Url),
167
+ codeSecret: try decodeUnpaddedBase64URL(codeSecretBase64Url),
168
+ enteredCode: enteredCode
169
+ )
170
+ let key = try await cache.secureKey(appId: appId, environment: environment)
171
+ let signature = try await key.signDeviceAuthorization(signingInput: signingInput)
172
+ return unpaddedBase64URL(signature)
173
+ } catch {
174
+ throw mappedSecureKeyException(error)
175
+ }
176
+ }
177
+
178
+ // The key's add-a-device code secret, kept in the Keychain across
179
+ // restarts. Never prompts. Only the unsent-secret half of DaaSdk's API is
180
+ // bridged: once the TS side sends a secret it deletes it here, and keeps
181
+ // the sent copy in process memory only.
182
+ AsyncFunction("codeSecret") { (appId: String, environment: String) async throws -> String in
183
+ do {
184
+ let key = try await cache.secureKey(appId: appId, environment: environment)
185
+ return unpaddedBase64URL(try await key.codeSecret())
186
+ } catch {
187
+ throw mappedSecureKeyException(error)
188
+ }
189
+ }
190
+
191
+ // Deletes the stored code secret only when it equals these bytes.
192
+ AsyncFunction("deleteCodeSecret") { (
193
+ appId: String,
194
+ environment: String,
195
+ secretBase64Url: String
196
+ ) async throws in
197
+ do {
198
+ let secret = try decodeUnpaddedBase64URL(secretBase64Url)
199
+ // existingSecureKey, not secureKey: a delete must not mint an
200
+ // installation ID. With none, no key or secret can exist.
201
+ guard let key = try await cache.existingSecureKey(appId: appId, environment: environment) else {
202
+ return
203
+ }
204
+ try await key.deleteCodeSecret(secret)
205
+ } catch {
206
+ throw mappedSecureKeyException(error)
207
+ }
208
+ }
209
+
99
210
  // Typed native signRegistrationDeviceAuthorization — the registration
100
211
  // counterpart of signDeviceAuthorization above, NOT the same call. The
101
212
  // Base64URL payload is the exact Device Registration Complete device-proof
@@ -129,6 +240,19 @@ private actor SecureKeyCache {
129
240
  private var installationID: UUID?
130
241
 
131
242
  func secureKey(appId: String, environment raw: String) throws -> DaaSecureKey {
243
+ guard let key = try resolveKey(appId: appId, environment: raw, mintInstallationID: true) else {
244
+ throw ContractViolationException()
245
+ }
246
+ return key
247
+ }
248
+
249
+ // For resolveExisting: like secureKey, but never mints or replaces the
250
+ // installation ID. nil when this install has none yet, so no key can exist.
251
+ func existingSecureKey(appId: String, environment raw: String) throws -> DaaSecureKey? {
252
+ try resolveKey(appId: appId, environment: raw, mintInstallationID: false)
253
+ }
254
+
255
+ private func resolveKey(appId: String, environment raw: String, mintInstallationID: Bool) throws -> DaaSecureKey? {
132
256
  guard let environment = DaaEnvironment(rawValue: raw) else {
133
257
  throw ContractViolationException()
134
258
  }
@@ -136,10 +260,14 @@ private actor SecureKeyCache {
136
260
  if let existing = keys[alias] {
137
261
  return existing
138
262
  }
263
+ let installationID: UUID? = try mintInstallationID ? installationUUID() : existingInstallationUUID()
264
+ guard let installationID else {
265
+ return nil
266
+ }
139
267
  let identity = try DaaAuthKeyIdentity(
140
268
  appID: appId,
141
269
  environment: environment,
142
- installationID: try installationUUID()
270
+ installationID: installationID
143
271
  )
144
272
  let key = DaaSecureKey(identity: identity)
145
273
  keys[alias] = key
@@ -159,4 +287,20 @@ private actor SecureKeyCache {
159
287
  installationID = id
160
288
  return id
161
289
  }
290
+
291
+ private func existingInstallationUUID() throws -> UUID? {
292
+ if let installationID {
293
+ return installationID
294
+ }
295
+ let id: UUID?
296
+ do {
297
+ id = try InstallationIdentityStore.live().loadExisting()
298
+ } catch {
299
+ throw ContractViolationException()
300
+ }
301
+ if let id {
302
+ installationID = id
303
+ }
304
+ return id
305
+ }
162
306
  }
@@ -35,6 +35,7 @@ import Foundation
35
35
  enum InstallationIdentityStoreError: Error, Equatable {
36
36
  case applicationSupportUnavailable
37
37
  case unreadable
38
+ case malformed
38
39
  case unwritable
39
40
  }
40
41
 
@@ -73,21 +74,56 @@ struct InstallationIdentityStore {
73
74
  return id
74
75
  }
75
76
 
76
- private func read(_ file: URL) throws -> UUID? {
77
- guard FileManager.default.fileExists(atPath: file.path) else {
78
- return nil
79
- }
77
+ /// Read-only lookup for `resolveExisting`: never mints and never deletes.
78
+ /// `nil` only when the file does not exist (no key can exist under an
79
+ /// installation ID that was never minted). An unreadable or malformed file
80
+ /// throws and is left in place, so `resolveExisting` reports an error rather than
81
+ /// "no key"; `loadOrCreate`'s delete-and-remint stays on the create path.
82
+ func loadExisting() throws -> UUID? {
83
+ Self.lock.lock()
84
+ defer { Self.lock.unlock() }
85
+
86
+ let file = directory.appendingPathComponent(Self.fileName, isDirectory: false)
87
+ // Read directly instead of checking fileExists first: fileExists also
88
+ // returns false when the path cannot be inspected (for example a
89
+ // directory without search permission), which would report "no key"
90
+ // for an ID that exists.
80
91
  let data: Data
81
92
  do {
82
93
  data = try Data(contentsOf: file)
94
+ } catch let error as CocoaError where error.code == .fileReadNoSuchFile {
95
+ return nil
96
+ } catch {
97
+ throw InstallationIdentityStoreError.unreadable
98
+ }
99
+ guard let id = Self.parse(data) else {
100
+ throw InstallationIdentityStoreError.malformed
101
+ }
102
+ return id
103
+ }
104
+
105
+ private func contents(of file: URL) throws -> Data {
106
+ do {
107
+ return try Data(contentsOf: file)
83
108
  } catch {
84
109
  // Present but unreadable (for example before the first unlock after
85
110
  // boot): fail and let the next call retry. Never remint over it.
86
111
  throw InstallationIdentityStoreError.unreadable
87
112
  }
88
- guard let string = String(data: data, encoding: .utf8),
89
- let id = UUID(uuidString: string.trimmingCharacters(in: .whitespacesAndNewlines))
90
- else {
113
+ }
114
+
115
+ private static func parse(_ data: Data) -> UUID? {
116
+ guard let string = String(data: data, encoding: .utf8) else {
117
+ return nil
118
+ }
119
+ return UUID(uuidString: string.trimmingCharacters(in: .whitespacesAndNewlines))
120
+ }
121
+
122
+ private func read(_ file: URL) throws -> UUID? {
123
+ guard FileManager.default.fileExists(atPath: file.path) else {
124
+ return nil
125
+ }
126
+ guard let id = Self.parse(try contents(of: file)) else {
91
127
  // Malformed file: delete and remint. The previous Enclave key is not
92
128
  // deleted and is not revoked at Circle. This keeps resolveOrCreate
93
129
  // usable; it is not equivalent to an uninstall.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@circle-fin/daa-react-native-sdk",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "React Native SDK for Digital Asset Accounts (DAA) — attested authentication and self-custody DAA API access",
5
5
  "main": "build/index.js",
6
6
  "source": "src/index.ts",