@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,229 @@
1
+ /**
2
+ * Copyright (c) 2026, Circle Internet Group, Inc. All rights reserved.
3
+ *
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ import { DEVICE_PATH_PREFIX } from '../api/deviceStatus'
20
+ import { fromBase64Url, toBase64Url } from '../crypto/base64url'
21
+ import { matchingCode } from '../crypto/matchingCode'
22
+ import { serializeBody } from '../crypto/serialization'
23
+ import {
24
+ apiFailure,
25
+ contractViolation,
26
+ isDaaError,
27
+ matchingCodeUnavailable,
28
+ transportFailure,
29
+ } from '../errors'
30
+ import { headerValueIgnoringCase } from '../transport/headers'
31
+ import type { DaaHttpResponse, DaaTransport } from '../transport/DaaTransport'
32
+ import type { OwnerAuthKey } from '../securekey/SecureKey'
33
+ import { isUuid } from '../validation/uuid'
34
+ import { readDaaStatusCode } from '../wire/errorEnvelope'
35
+ import { markCodeSecretSent, oneAtATime } from './codeSecrets'
36
+
37
+ const APPROVAL_TOKEN: unique symbol = Symbol('daa.pendingDeviceApproval')
38
+
39
+ /**
40
+ * A new device's registration on an account that already has a device: DAA
41
+ * added this AuthKey as waiting for approval, and an existing device must
42
+ * approve it before it can sign.
43
+ *
44
+ * The app calls {@link sendCodeSecret}, which is what makes DAA tell the
45
+ * partner (`account.device.pending` webhook) to prompt the existing device,
46
+ * and shows {@link getMatchingCode} on this device. The user types the code on
47
+ * the existing device, which approves after checking that the typed code is
48
+ * the one this device's code secret produces. The key can't sign anything
49
+ * until then, and DAA cancels the request at `expiresAt`. Follow the request
50
+ * with `DaaClient.watchDeviceStatus({ authKeyId })`.
51
+ *
52
+ * The code secret is kept with the AuthKey (Keychain on iOS, Keystore-encrypted
53
+ * storage on Android) until DAA says no request is waiting for it, or until
54
+ * {@link sendCodeSecret} sends it. It leaves the device only through
55
+ * {@link sendCodeSecret}, for this request only.
56
+ *
57
+ * If the app discards this object, or restarts, signing in again before
58
+ * `expiresAt` returns the same request and the same code. Once
59
+ * {@link sendCodeSecret} has run, the secret is kept in process memory only:
60
+ * after a restart a sign-in before `expiresAt` still returns the same request,
61
+ * but this device no longer holds the secret its seal commits to. DAA returns
62
+ * that seal, so this SDK can tell: {@link getMatchingCode} and
63
+ * {@link sendCodeSecret} then throw `matching_code_unavailable` and nothing is
64
+ * sent. Have the user wait until `expiresAt` and sign in again.
65
+ *
66
+ * Not constructible by a consumer: exported from `index.ts` as a type only.
67
+ */
68
+ export class PendingDeviceApproval {
69
+ /** The add-a-device request this device waits on. */
70
+ readonly requestId: string
71
+ /** This device's new AuthKey id. It can't sign until the request is approved. */
72
+ readonly authKeyId: string
73
+ /** When DAA cancels the request unless it was approved, as ISO-8601 text. */
74
+ readonly expiresAt: string
75
+
76
+ readonly #thumbprint: Uint8Array
77
+ readonly #codeNonce: Uint8Array
78
+ /** `null` when DAA's seal for the request isn't this device's. */
79
+ readonly #codeSecret: Uint8Array | null
80
+ readonly #authKeyThumbprint: string
81
+ readonly #authKey: OwnerAuthKey
82
+ readonly #transport: DaaTransport
83
+
84
+ /** @internal Constructed only by `PendingDeviceRegistration.complete`. */
85
+ constructor(
86
+ token: symbol,
87
+ fields: {
88
+ readonly requestId: string
89
+ readonly authKeyId: string
90
+ readonly expiresAt: string
91
+ readonly codeNonce: string
92
+ readonly authKeyThumbprint: string
93
+ readonly authKey: OwnerAuthKey
94
+ readonly codeSecret: Uint8Array | null
95
+ readonly transport: DaaTransport
96
+ },
97
+ ) {
98
+ if (token !== APPROVAL_TOKEN) {
99
+ throw contractViolation('PendingDeviceApproval must be constructed by the DAA SDK')
100
+ }
101
+ if (!isUuid(fields.requestId) || !isUuid(fields.authKeyId)) {
102
+ throw contractViolation('PendingDeviceApproval ids must be UUIDs')
103
+ }
104
+ this.requestId = fields.requestId
105
+ this.authKeyId = fields.authKeyId
106
+ this.expiresAt = fields.expiresAt
107
+ this.#thumbprint = fromBase64Url(fields.authKeyThumbprint)
108
+ this.#codeNonce = fromBase64Url(fields.codeNonce)
109
+ // Not copied: `markCodeSecretSent` finds the kept secret by reference.
110
+ // Only `PendingDeviceRegistration` passes it, and nothing writes to it.
111
+ this.#codeSecret = fields.codeSecret
112
+ this.#authKeyThumbprint = fields.authKeyThumbprint
113
+ this.#authKey = fields.authKey
114
+ this.#transport = fields.transport
115
+ // Fails here, at registration, rather than later when the app asks for
116
+ // the code: all three inputs must be 32 bytes.
117
+ if (this.#codeSecret !== null) {
118
+ matchingCode(this.#thumbprint, this.#codeNonce, this.#codeSecret)
119
+ }
120
+ Object.freeze(this)
121
+ }
122
+
123
+ /**
124
+ * The six digits to show on this device, such as `"482913"`. Show them with
125
+ * a space in the middle (`482 913`); the approving device ignores spaces and
126
+ * dashes in what the user types. The same value every time.
127
+ *
128
+ * @throws DaaError with `detail.kind === 'matching_code_unavailable'` when
129
+ * DAA's seal for this request isn't one this device holds the secret for
130
+ * (the app restarted after the secret was sent): no code it shows
131
+ * could match. Tell the user to wait until `expiresAt` and sign in again.
132
+ */
133
+ getMatchingCode(): string {
134
+ if (this.#codeSecret === null) {
135
+ throw matchingCodeUnavailable()
136
+ }
137
+ return matchingCode(this.#thumbprint, this.#codeNonce, this.#codeSecret)
138
+ }
139
+
140
+ /**
141
+ * Send this device's code secret to DAA for this request:
142
+ * `POST /v1/accounts/auth/devices/{authKeyId}/codeSecret`. DAA checks it
143
+ * against the seal this device signed into its registration, stores it, and
144
+ * sends the `account.device.pending` webhook so the partner can prompt the
145
+ * existing device. Until it is sent, the existing device can't check the
146
+ * code. Call it once the request is `PENDING_APPROVAL`, before or while
147
+ * showing {@link getMatchingCode}.
148
+ *
149
+ * Safe to call again: DAA accepts the same secret for the same request again
150
+ * (`204`) and re-sends the webhook while the request is waiting. That is the
151
+ * retry for a `500` or a transport failure, which may mean the secret was
152
+ * saved but the webhook was not sent.
153
+ *
154
+ * Once sent, this SDK never seals the secret into another request: the
155
+ * key's next registration seals a new one (a registration that timed out
156
+ * before the send may still reach DAA later with it; see `codeSecrets.ts`). If DAA answers that registration with
157
+ * this same request (it was still waiting), the code still comes from this
158
+ * secret. Waits for any registration of this key in flight: if DAA answers it
159
+ * with a different request, or doesn't answer, this request is no longer the
160
+ * one the secret is sealed into, and nothing is sent.
161
+ *
162
+ * @throws DaaError with `detail.kind === 'transport'` (retryable) when the
163
+ * transport throws; `api` for any status but `204`: retryable for `429`
164
+ * and `5xx` (call again), and not retryable for the `409` with
165
+ * `DEVICE_APPROVAL_INVALID` (`420117`), which DAA returns when this is no
166
+ * longer the key's latest request, the request has ended, or the secret
167
+ * doesn't match the seal; `contract_violation` (not retryable, nothing
168
+ * sent) when this SDK already knows the request is not the one the secret
169
+ * is sealed into: a later sign-in of this key returned another request or
170
+ * got no answer, or DAA said no request is waiting for the key. Sign in
171
+ * again for the current request. `matching_code_unavailable` (nothing
172
+ * sent), as for {@link getMatchingCode}: this device doesn't hold the
173
+ * secret DAA's seal for the request commits to. A secure-key error kind
174
+ * (nothing sent, call again) when deleting the secret from the AuthKey's
175
+ * storage failed, or `transport` (retryable, nothing sent) when that
176
+ * delete didn't finish within 5 seconds: it is deleted before the send so
177
+ * it is never sealed into another request, even after a restart.
178
+ */
179
+ async sendCodeSecret(): Promise<void> {
180
+ if (this.#codeSecret === null) {
181
+ throw matchingCodeUnavailable()
182
+ }
183
+ const codeSecret = this.#codeSecret
184
+ const bodyBytes = serializeBody({
185
+ requestId: this.requestId,
186
+ codeSecret: toBase64Url(codeSecret),
187
+ })
188
+ // Decided after any registration of this key in flight has its answer,
189
+ // and recorded before sending: a request whose response is lost may still
190
+ // have reached DAA.
191
+ const maySend = await oneAtATime(this.#authKeyThumbprint, () =>
192
+ markCodeSecretSent(this.#authKeyThumbprint, codeSecret, this.requestId, this.#authKey),
193
+ )
194
+ if (!maySend) {
195
+ throw contractViolation(
196
+ 'this request is no longer the one this code secret is sealed into; ' +
197
+ 'use the PendingDeviceApproval from the latest sign-in',
198
+ )
199
+ }
200
+ let response: DaaHttpResponse
201
+ try {
202
+ response = await this.#transport.send({
203
+ method: 'POST',
204
+ path: `${DEVICE_PATH_PREFIX}${this.authKeyId}/codeSecret`,
205
+ query: null,
206
+ headers: { 'Content-Type': 'application/json' },
207
+ bodyBytes,
208
+ })
209
+ } catch (err) {
210
+ // A DaaError the consumer's transport threw keeps its own kind and
211
+ // retryability, as in `asTransportFailure`.
212
+ throw isDaaError(err) ? err : transportFailure(err)
213
+ }
214
+ if (response.status !== 204) {
215
+ throw apiFailure({
216
+ httpStatus: response.status,
217
+ daaStatusCode: readDaaStatusCode(response.bodyBytes),
218
+ correlationId: headerValueIgnoringCase(response.headers, 'X-Request-Id'),
219
+ })
220
+ }
221
+ }
222
+ }
223
+
224
+ /** @internal */
225
+ export function newPendingDeviceApproval(
226
+ fields: ConstructorParameters<typeof PendingDeviceApproval>[1],
227
+ ): PendingDeviceApproval {
228
+ return new PendingDeviceApproval(APPROVAL_TOKEN, fields)
229
+ }