@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
@@ -0,0 +1,321 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) 2026, Circle Internet Group, Inc. All rights reserved.
4
+ *
5
+ * SPDX-License-Identifier: Apache-2.0
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.holdCodeSecret = holdCodeSecret;
21
+ exports.oneAtATime = oneAtATime;
22
+ exports.codeSecretFor = codeSecretFor;
23
+ exports.sealingCodeSecret = sealingCodeSecret;
24
+ exports.codeSecretForRequest = codeSecretForRequest;
25
+ exports.markCodeSecretSent = markCodeSecretSent;
26
+ exports.dropCodeSecret = dropCodeSecret;
27
+ exports.deleteStoredCodeSecret = deleteStoredCodeSecret;
28
+ const base64url_1 = require("../crypto/base64url");
29
+ const matchingCode_1 = require("../crypto/matchingCode");
30
+ const random_1 = require("../crypto/random");
31
+ const errors_1 = require("../errors");
32
+ /**
33
+ * The add-a-device code secrets of each AuthKey, by thumbprint, for this
34
+ * process.
35
+ *
36
+ * `unsent` is what the key's next registration seals. When a key whose request
37
+ * is still waiting registers again (the app dropped its
38
+ * `PendingDeviceApproval`, or the response was lost), DAA returns that earlier
39
+ * request with its `codeNonce`, and the enclave keeps the first registration's
40
+ * `codeSeal`. A new secret would then show a code that can't match, so
41
+ * `PendingDeviceRegistration.complete()` keeps sealing the same `unsent`
42
+ * secret until DAA says no request is waiting for the key: it comes back
43
+ * `ACTIVE` or `PENDING_ACTIVATION`, or DAA refuses it with one of the four
44
+ * add-a-device codes (DAA checks for a waiting request before any of them).
45
+ * There is no expiry on this device's clock, which may be wrong.
46
+ *
47
+ * Reusing a secret only commits a later request to the same seal, which is
48
+ * harmless while the secret never left the device. So
49
+ * `PendingDeviceApproval.sendCodeSecret()` sends `unsent` only for
50
+ * `sealedInto`, the request DAA last returned for its seal. Any other request
51
+ * has ended, or was never sealed with it, and DAA would refuse it anyway.
52
+ * `sealedInto` is `null` while a registration that sealed `unsent` has no
53
+ * answer: DAA may have put that seal into a new request. Once sent for request
54
+ * R, the secret moves to `sent` and the next registration seals a fresh
55
+ * `unsent` secret, whether or not this process ever learns that R ended.
56
+ * `sent` is kept so that the send can be retried, and so that if DAA returns R
57
+ * itself (it was still waiting and kept its first seal), the code is shown
58
+ * from the secret R's seal commits to.
59
+ *
60
+ * DAA returns the request's stored `codeSeal`, and a secret that doesn't open
61
+ * it is never shown or sent for that request (`codeSecretForRequest` returns
62
+ * `null`). That happens when the request was sealed with a secret this SDK no
63
+ * longer holds: the process restarted after the secret was sent, or a registration
64
+ * that sealed a secret and got no answer (it passed its deadline, say) reached
65
+ * DAA later, while its signature was valid (five minutes, plus skew), after
66
+ * that secret was sent for an earlier request R that has since expired. The
67
+ * code is unavailable until that request expires. Nothing else learns more
68
+ * than the send already told DAA.
69
+ *
70
+ * Every read and update runs in the key's {@link oneAtATime} queue: a
71
+ * registration holds it from choosing the secret until DAA's answer is
72
+ * recorded, so a send can't reveal a secret a registration in flight is
73
+ * sealing into a new request.
74
+ *
75
+ * Only `unsent` outlives the process: it is read from, and deleted from, the
76
+ * key's own storage (`OwnerAuthKey.codeSecret` / `deleteCodeSecret`, the
77
+ * Keychain or Keystore), so after an app restart a sign-in that DAA answers
78
+ * with the request still waiting shows the same code and can still send it.
79
+ * The send deletes the secret from that storage first, so a secret that left
80
+ * the device is never sealed again, even after a restart; `sent` and
81
+ * `sealedInto` are process memory only. A restart after the send therefore
82
+ * loses the code for that request: the next registration seals a new secret,
83
+ * which doesn't open the request's seal. A key whose store has no
84
+ * `codeSecret` keeps `unsent` in process memory too.
85
+ */
86
+ const codeSecrets = new Map();
87
+ /**
88
+ * Secrets sealed by a `complete()` that got no answer from DAA: it passed its
89
+ * deadline, `send()` threw, or the response was neither a success nor one of
90
+ * the four add-a-device refusals (a 504 from a relay that timed out). DAA may
91
+ * still add that request as waiting with this secret's seal after a later
92
+ * Complete for the key was refused, so a refusal no longer drops it. Keeping a
93
+ * secret too long is harmless while it was never sent. Process memory only: a
94
+ * restart forgets which secrets are held.
95
+ */
96
+ const heldCodeSecrets = new WeakSet();
97
+ /**
98
+ * Keep `secret` even when a later refusal says no request is waiting. `null`
99
+ * (a registration sent without `addDeviceApproval`, so with no secret) does
100
+ * nothing.
101
+ */
102
+ function holdCodeSecret(secret) {
103
+ if (secret !== null) {
104
+ heldCodeSecrets.add(secret);
105
+ }
106
+ }
107
+ /** The last queued step of each AuthKey, by thumbprint. */
108
+ const keyQueue = new Map();
109
+ /**
110
+ * Runs `step` once every earlier step for the same key has settled: each
111
+ * `complete()` of the key, and each `sendCodeSecret()` deciding what to send.
112
+ *
113
+ * Two overlapping Completes for one key send the same secret. DAA can refuse
114
+ * the first (say, 409 conflict: another device was waiting, so both requests
115
+ * were cancelled) and then add the second as waiting with that secret's seal.
116
+ * If the refusal reached the app last, it would drop the secret the waiting
117
+ * request needs, and the next login would show a code that can't match.
118
+ */
119
+ async function oneAtATime(thumbprint, step) {
120
+ const previous = keyQueue.get(thumbprint) ?? Promise.resolve();
121
+ const run = previous.then(step);
122
+ const settled = run.then(() => undefined, () => undefined);
123
+ keyQueue.set(thumbprint, settled);
124
+ try {
125
+ return await run;
126
+ }
127
+ finally {
128
+ if (keyQueue.get(thumbprint) === settled) {
129
+ keyQueue.delete(thumbprint);
130
+ }
131
+ }
132
+ }
133
+ /**
134
+ * The secret the key's next registration seals: the one this process holds,
135
+ * else the key's stored one (`authKey.codeSecret()`, which creates it on first
136
+ * use), else, for a key without that method, a new 32-byte one. Call it in the
137
+ * key's {@link oneAtATime} queue.
138
+ *
139
+ * A key must have both `codeSecret` and `deleteCodeSecret`, or neither: with
140
+ * only `codeSecret`, the send couldn't delete the secret and the next
141
+ * registration would read the sent secret back and seal it again.
142
+ *
143
+ * Records nothing once `late()` is true: the queue has moved on, and a
144
+ * registration queued after this one may already have its own secret here.
145
+ */
146
+ async function codeSecretFor(thumbprint, authKey, late) {
147
+ if ((authKey.codeSecret === undefined) !== (authKey.deleteCodeSecret === undefined)) {
148
+ throw (0, errors_1.contractViolation)('an AuthKey needs both codeSecret and deleteCodeSecret, or neither');
149
+ }
150
+ const kept = codeSecrets.get(thumbprint);
151
+ if (kept?.unsent != null) {
152
+ return kept.unsent;
153
+ }
154
+ const secret = authKey.codeSecret === undefined ? (0, random_1.randomBytes)(32) : await authKey.codeSecret();
155
+ if (secret.length !== 32) {
156
+ throw (0, errors_1.contractViolation)('the AuthKey code secret must be 32 bytes');
157
+ }
158
+ if (late()) {
159
+ return secret;
160
+ }
161
+ if (kept === undefined) {
162
+ codeSecrets.set(thumbprint, { unsent: secret, sealedInto: null, sent: null });
163
+ }
164
+ else {
165
+ kept.unsent = secret;
166
+ kept.sealedInto = null;
167
+ }
168
+ return secret;
169
+ }
170
+ /**
171
+ * A registration that sealed `secret` is about to go out: until DAA answers,
172
+ * the request that seal ends up in is unknown.
173
+ */
174
+ function sealingCodeSecret(thumbprint, secret) {
175
+ const kept = codeSecrets.get(thumbprint);
176
+ if (kept?.unsent === secret) {
177
+ kept.sealedInto = null;
178
+ }
179
+ }
180
+ /**
181
+ * The secret behind `requestId`, which DAA returned as waiting after a
182
+ * registration that sealed `sealed`: the sent secret if DAA returned the
183
+ * request it was sent for, else `sealed`, now recorded as sealed into
184
+ * `requestId`. Forgets a sent secret whose request DAA no longer returns,
185
+ * since a key has only one waiting request.
186
+ *
187
+ * `requestSeal` is the request's stored `codeSeal` as DAA returned it. If the
188
+ * chosen secret doesn't open it, returns `null` and records nothing against
189
+ * that request: the request was sealed with a secret this SDK no longer
190
+ * holds, so no code from this device can match it and nothing may be sent for
191
+ * it. `sealed` stays the key's unsent secret, harmless to seal again since it
192
+ * never left the device.
193
+ * A `null` `requestSeal` (a DAA that doesn't return it) is not checked.
194
+ */
195
+ function codeSecretForRequest(thumbprint, thumbprintBytes, sealed, requestId, requestSeal) {
196
+ const opens = (secret) => requestSeal === null || (0, base64url_1.toBase64Url)((0, matchingCode_1.codeSeal)(thumbprintBytes, secret)) === requestSeal;
197
+ const kept = codeSecrets.get(thumbprint);
198
+ if (kept?.sent != null && kept.sent.requestId === requestId) {
199
+ return opens(kept.sent.secret) ? kept.sent.secret : null;
200
+ }
201
+ if (kept !== undefined) {
202
+ kept.sent = null;
203
+ }
204
+ if (!opens(sealed)) {
205
+ return null;
206
+ }
207
+ if (kept?.unsent === sealed) {
208
+ kept.sealedInto = requestId;
209
+ }
210
+ return sealed;
211
+ }
212
+ /**
213
+ * Whether `secret` may be sent for `requestId`: it was already sent for that
214
+ * request (a retry), or it is the unsent secret DAA last returned that request
215
+ * for. If so, records it as sent, before the request goes out (a send whose
216
+ * response is lost may still have reached DAA): deletes it from the key's
217
+ * storage, so the key's next registration seals a new secret, then keeps it
218
+ * here as `sent`. If that delete throws or passes its time limit
219
+ * ({@link deleteWithinLimit}), nothing changes and nothing may be sent; a
220
+ * retry deletes again. Compares by reference, so `secret` must be the array
221
+ * this module handed out. Call it in the key's {@link oneAtATime} queue.
222
+ */
223
+ async function markCodeSecretSent(thumbprint, secret, requestId, authKey) {
224
+ const kept = codeSecrets.get(thumbprint);
225
+ if (kept === undefined) {
226
+ return false;
227
+ }
228
+ if (kept.sent?.secret === secret && kept.sent.requestId === requestId) {
229
+ return true;
230
+ }
231
+ if (kept.unsent !== secret || kept.sealedInto !== requestId) {
232
+ return false;
233
+ }
234
+ await deleteWithinLimit(authKey, secret);
235
+ kept.sent = { secret, requestId };
236
+ kept.unsent = null;
237
+ kept.sealedInto = null;
238
+ return true;
239
+ }
240
+ /**
241
+ * DAA said no request is waiting for the key after a registration that sealed
242
+ * `secret`: forget the key's secrets, unless a later registration or send
243
+ * already replaced `secret`. Compares by reference: don't copy it
244
+ * (`.slice()`) on the way here, or nothing is ever dropped.
245
+ *
246
+ * Returns `true` when it forgot the unsent secret, which the caller then
247
+ * deletes from the key's storage with {@link deleteStoredCodeSecret}. That is
248
+ * left to the caller so a registration can save DAA's answer first.
249
+ */
250
+ function dropCodeSecret(thumbprint, secret) {
251
+ if (secret === null || heldCodeSecrets.has(secret)) {
252
+ return false;
253
+ }
254
+ const kept = codeSecrets.get(thumbprint);
255
+ if (kept?.unsent === secret) {
256
+ codeSecrets.delete(thumbprint);
257
+ return true;
258
+ }
259
+ else if (kept?.sent?.secret === secret) {
260
+ // Not reached today: the queue stops a registration from sealing a secret
261
+ // after it was sent, and a late registration holds its secret first (so it
262
+ // returned above). Kept so a future path that does reach it forgets the
263
+ // sent secret rather than leaving it to show a code.
264
+ kept.sent = null;
265
+ }
266
+ return false;
267
+ }
268
+ /**
269
+ * Delete a secret {@link dropCodeSecret} forgot from the key's storage. A
270
+ * failure or a timeout is ignored: the secret never left the device, so
271
+ * keeping it is harmless, and the next registration reads it back and seals it
272
+ * again.
273
+ */
274
+ async function deleteStoredCodeSecret(authKey, secret) {
275
+ try {
276
+ await deleteWithinLimit(authKey, secret);
277
+ }
278
+ catch {
279
+ // Kept; see above.
280
+ }
281
+ }
282
+ /** How long a code-secret delete may take before the key's queue moves on. */
283
+ const CODE_SECRET_DELETE_LIMIT_MS = 5000;
284
+ /**
285
+ * `authKey.deleteCodeSecret(secret)`, failing with a retryable `transport`
286
+ * error once it takes longer than {@link CODE_SECRET_DELETE_LIMIT_MS}. Both
287
+ * deletes run in the key's {@link oneAtATime} queue, which has no deadline of
288
+ * its own: a Keychain or Keystore delete that hung would hold every later
289
+ * `complete()` and `sendCodeSecret()` of the key until the app is killed.
290
+ *
291
+ * This bounds only the wait here: the native delete is not cancelled. Both
292
+ * native SDKs run it one at a time with the key's other storage calls
293
+ * (Android's `resolutionLock`, iOS's metadata executor), so while storage
294
+ * stays hung the key's next native calls wait behind it too. They still end:
295
+ * `complete()` reads the key and its secret under its own deadline and fails
296
+ * with `transport`, and a retried send hits this limit again. Once storage
297
+ * recovers, the next call succeeds.
298
+ *
299
+ * Accepted trade-off: a delete that timed out may still finish later. The
300
+ * native SDKs delete only a stored secret equal to `secret`, so it can't
301
+ * remove a newer one. But the key's next registration may read `secret` back
302
+ * from storage and seal it again before the late delete removes it. The code
303
+ * still shows in this process, which keeps the secret in memory; only after a
304
+ * restart does that request answer `matching_code_unavailable`, as it did
305
+ * before secrets were stored. This only happens after a hang.
306
+ */
307
+ async function deleteWithinLimit(authKey, secret) {
308
+ if (authKey.deleteCodeSecret === undefined) {
309
+ return;
310
+ }
311
+ let timer;
312
+ const limit = new Promise((_, reject) => {
313
+ timer = setTimeout(() => reject(new errors_1.DaaError({ kind: 'transport', retryable: true }, `DAA: deleting the code secret from the device key's storage did not finish within ${CODE_SECRET_DELETE_LIMIT_MS / 1000} seconds`)), CODE_SECRET_DELETE_LIMIT_MS);
314
+ });
315
+ try {
316
+ await Promise.race([authKey.deleteCodeSecret(secret), limit]);
317
+ }
318
+ finally {
319
+ clearTimeout(timer);
320
+ }
321
+ }
@@ -15,9 +15,11 @@
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
17
  */
18
- import type { OwnerAuthKey, OwnerKeyIdentity, SecureKeyStore } from './SecureKey';
18
+ import type { OwnerAuthKey, OwnerKeyIdentity, RegisteredOwnerAuthKey, SecureKeyStore } from './SecureKey';
19
19
  export declare class InMemorySecureKeyStore implements SecureKeyStore {
20
20
  #private;
21
21
  constructor();
22
22
  resolveOrCreate(identity: OwnerKeyIdentity): Promise<OwnerAuthKey>;
23
+ resolveExisting(identity: OwnerKeyIdentity): Promise<RegisteredOwnerAuthKey | null>;
24
+ forget(identity: OwnerKeyIdentity): Promise<void>;
23
25
  }
@@ -27,13 +27,15 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
27
27
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
28
28
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
29
29
  };
30
- var _InMemoryOwnerAuthKey_acknowledged, _InMemoryOwnerAuthKey_keyPair, _InMemorySecureKeyStore_instances, _InMemorySecureKeyStore_keys, _InMemorySecureKeyStore_generate;
30
+ var _InMemoryOwnerAuthKey_registration, _InMemoryOwnerAuthKey_codeSecret, _InMemoryOwnerAuthKey_keyPair, _InMemorySecureKeyStore_instances, _InMemorySecureKeyStore_keys, _InMemorySecureKeyStore_generate;
31
31
  Object.defineProperty(exports, "__esModule", { value: true });
32
32
  exports.InMemorySecureKeyStore = void 0;
33
33
  const base64url_1 = require("../crypto/base64url");
34
34
  const bytes_1 = require("../crypto/bytes");
35
35
  const jwkThumbprint_1 = require("../crypto/jwkThumbprint");
36
+ const random_1 = require("../crypto/random");
36
37
  const alias_1 = require("./alias");
38
+ const registration_1 = require("./registration");
37
39
  // A WebCrypto-backed, in-memory implementation of the DAA Owner-key API. It
38
40
  // produces real P-256/ES256 signatures so it can drive golden vectors and unit
39
41
  // tests WITHOUT native hardware. It is NOT a production secure-key
@@ -64,15 +66,22 @@ class InMemoryOwnerAuthKey {
64
66
  // There is no durable store behind this double, so a handle always starts
65
67
  // unacknowledged and the state lives only as long as the handle does. The
66
68
  // native store persists the equivalent state in the OS keystore.
67
- _InMemoryOwnerAuthKey_acknowledged.set(this, false);
69
+ _InMemoryOwnerAuthKey_registration.set(this, null);
70
+ _InMemoryOwnerAuthKey_codeSecret.set(this, null);
68
71
  _InMemoryOwnerAuthKey_keyPair.set(this, void 0);
69
72
  __classPrivateFieldSet(this, _InMemoryOwnerAuthKey_keyPair, keyPair, "f");
70
73
  }
71
74
  resolution() {
72
- return __classPrivateFieldGet(this, _InMemoryOwnerAuthKey_acknowledged, "f") ? 'existing' : 'created';
75
+ return __classPrivateFieldGet(this, _InMemoryOwnerAuthKey_registration, "f") === null ? 'created' : 'existing';
73
76
  }
74
- async acknowledgeRegistration() {
75
- __classPrivateFieldSet(this, _InMemoryOwnerAuthKey_acknowledged, true, "f");
77
+ /** @internal The recorded registration, read by the store's `resolveExisting`. */
78
+ registration() {
79
+ return __classPrivateFieldGet(this, _InMemoryOwnerAuthKey_registration, "f");
80
+ }
81
+ async acknowledgeRegistration(registration) {
82
+ // Same contract as the native store: UUID identifiers, always an upsert.
83
+ const { authKeyId, ownerBindingId } = (0, registration_1.assertRegistration)(registration);
84
+ __classPrivateFieldSet(this, _InMemoryOwnerAuthKey_registration, Object.freeze({ authKeyId, ownerBindingId }), "f");
76
85
  }
77
86
  async publicKeySpkiBase64Url() {
78
87
  const spki = await getSubtle().exportKey('spki', __classPrivateFieldGet(this, _InMemoryOwnerAuthKey_keyPair, "f").publicKey);
@@ -95,8 +104,25 @@ class InMemoryOwnerAuthKey {
95
104
  // caller reached; native's two validators are what actually diverge.
96
105
  return this.signExactBytes(message);
97
106
  }
107
+ async signDeviceApproval(message) {
108
+ // Same as above: one WebCrypto key. Native's body, seal and typed-code
109
+ // checks are not repeated here; `approveDevice` runs its own before it
110
+ // signs, and the native SDKs' tests pin theirs.
111
+ return this.signExactBytes(message);
112
+ }
113
+ async codeSecret() {
114
+ // Same contract as native: kept until deleted, a copy on every read.
115
+ __classPrivateFieldSet(this, _InMemoryOwnerAuthKey_codeSecret, __classPrivateFieldGet(this, _InMemoryOwnerAuthKey_codeSecret, "f") ?? (0, random_1.randomBytes)(32), "f");
116
+ return __classPrivateFieldGet(this, _InMemoryOwnerAuthKey_codeSecret, "f").slice();
117
+ }
118
+ async deleteCodeSecret(secret) {
119
+ const kept = __classPrivateFieldGet(this, _InMemoryOwnerAuthKey_codeSecret, "f");
120
+ if (kept !== null && kept.length === secret.length && kept.every((b, i) => b === secret[i])) {
121
+ __classPrivateFieldSet(this, _InMemoryOwnerAuthKey_codeSecret, null, "f");
122
+ }
123
+ }
98
124
  }
99
- _InMemoryOwnerAuthKey_acknowledged = new WeakMap(), _InMemoryOwnerAuthKey_keyPair = new WeakMap();
125
+ _InMemoryOwnerAuthKey_registration = new WeakMap(), _InMemoryOwnerAuthKey_codeSecret = new WeakMap(), _InMemoryOwnerAuthKey_keyPair = new WeakMap();
100
126
  class InMemorySecureKeyStore {
101
127
  constructor() {
102
128
  _InMemorySecureKeyStore_instances.add(this);
@@ -117,6 +143,21 @@ class InMemorySecureKeyStore {
117
143
  }
118
144
  return existing;
119
145
  }
146
+ async resolveExisting(identity) {
147
+ // Read-only, like the native store: an absent or unacknowledged key is
148
+ // `null`, and nothing is generated. A failed generation rethrows, as a
149
+ // native read failure does — a failure is never "no key".
150
+ const pending = __classPrivateFieldGet(this, _InMemorySecureKeyStore_keys, "f").get((0, alias_1.secureKeyAlias)(identity));
151
+ if (pending === undefined) {
152
+ return null;
153
+ }
154
+ const authKey = await pending;
155
+ const registration = authKey.registration();
156
+ return registration === null ? null : Object.freeze({ authKey, ...registration });
157
+ }
158
+ async forget(identity) {
159
+ __classPrivateFieldGet(this, _InMemorySecureKeyStore_keys, "f").delete((0, alias_1.secureKeyAlias)(identity));
160
+ }
120
161
  }
121
162
  exports.InMemorySecureKeyStore = InMemorySecureKeyStore;
122
163
  _InMemorySecureKeyStore_keys = new WeakMap(), _InMemorySecureKeyStore_instances = new WeakSet(), _InMemorySecureKeyStore_generate = async function _InMemorySecureKeyStore_generate() {
@@ -16,9 +16,11 @@
16
16
  * limitations under the License.
17
17
  */
18
18
  import type { NativeSecureKeyBridge } from './NativeSecureKeyBridge';
19
- import type { OwnerAuthKey, OwnerKeyIdentity, SecureKeyStore } from './SecureKey';
19
+ import type { OwnerAuthKey, OwnerKeyIdentity, RegisteredOwnerAuthKey, SecureKeyStore } from './SecureKey';
20
20
  export declare class NativeSecureKeyStore implements SecureKeyStore {
21
21
  #private;
22
22
  constructor(bridge: NativeSecureKeyBridge);
23
23
  resolveOrCreate(identity: OwnerKeyIdentity): Promise<OwnerAuthKey>;
24
+ resolveExisting(identity: OwnerKeyIdentity): Promise<RegisteredOwnerAuthKey | null>;
25
+ forget(identity: OwnerKeyIdentity): Promise<void>;
24
26
  }