@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,133 +21,191 @@ import {
21
21
  DECODER_TOKEN,
22
22
  type DeviceRegistrationResult,
23
23
  } from '../models/DaaUserContext'
24
+ import { canonicalBytes32 } from '../crypto/matchingCode'
25
+ import { isUuid } from '../validation/uuid'
24
26
  import { toDeviceContext } from './deviceContextPayload'
25
27
  import { requiredDateTime } from './instant'
26
- import {
27
- asObject,
28
- MalformedPayloadError,
29
- optionalObject,
30
- optionalString,
31
- requiredString,
32
- } from './payload'
28
+ import { asObject, MalformedPayloadError, optionalString, requiredString } from './payload'
33
29
 
34
- const RECOVERY_REF_MIN = 16
35
- const RECOVERY_REF_MAX = 512
30
+ /** The only `type` a device key registration may echo. */
31
+ const DEVICE_KEY_TYPE = 'deviceKey'
36
32
 
37
- function toActiveContext(
38
- payload: Record<string, unknown>,
39
- sessionClientEntityId: string,
40
- ): DaaUserContext {
41
- return new DaaUserContext(DECODER_TOKEN, {
42
- clientEntityId: sessionClientEntityId,
43
- deviceContext: toDeviceContext(payload),
44
- })
45
- }
33
+ /**
34
+ * The `KeyCredential` members that `DeviceContext` names differently, by their
35
+ * `DeviceContext` name. A decoder error names the `KeyCredential` member.
36
+ */
37
+ const KEY_CREDENTIAL_NAMES: Readonly<Record<string, string>> = Object.freeze({
38
+ deviceCredentialStatus: 'credentialStatus',
39
+ authKeyId: 'credentialId',
40
+ })
46
41
 
47
42
  /**
48
- * The DeviceContext-shaped object Complete returns: a flat payload, or
49
- * nested `context.deviceContext` / `context` when `status: "ACTIVE"`.
43
+ * A `pending_approval` registration as DAA sends it. `PendingDeviceRegistration`
44
+ * adds the local code secret and AuthKey thumbprint to make the public
45
+ * `PendingDeviceApproval`.
50
46
  */
51
- function deviceContextPayloadFromComplete(
52
- payload: Record<string, unknown>,
53
- ): Record<string, unknown> {
54
- if (optionalString(payload, 'status') !== 'ACTIVE') {
55
- return payload
56
- }
57
- const contextPayload = optionalObject(payload, 'context')
58
- if (contextPayload === null) {
59
- throw new MalformedPayloadError('context')
60
- }
61
- return optionalObject(contextPayload, 'deviceContext') ?? contextPayload
62
- }
47
+ export type PendingApprovalPayload = Readonly<{
48
+ status: 'PENDING_APPROVAL'
49
+ requestId: string
50
+ authKeyId: string
51
+ expiresAt: string
52
+ codeNonce: string
53
+ /** The request's stored seal, or `null` from a DAA that doesn't send it. */
54
+ codeSeal: string | null
55
+ }>
63
56
 
64
57
  /**
65
- * Map Complete's `data` payload.
58
+ * A `pending_activation` registration as DAA sends it: a device approved from
59
+ * another device, signing in again during the account's cool-down.
60
+ * `PendingDeviceRegistration` acknowledges the key with `ownerBindingId`,
61
+ * which the public result leaves out.
62
+ */
63
+ export type PendingActivationPayload = Readonly<{
64
+ status: 'PENDING_ACTIVATION'
65
+ authKeyId: string
66
+ ownerBindingId: string
67
+ readyAt: string
68
+ }>
69
+
70
+ /**
71
+ * Complete's decoded payload, before the pending variants get their local state.
72
+ * `/v1/accounts/auth/keys` never returns `RECOVERY_PENDING`, so this decoder
73
+ * never produces it, although the public `DeviceRegistrationResult` keeps it.
74
+ */
75
+ export type DecodedDeviceRegistration =
76
+ | Extract<DeviceRegistrationResult, { status: 'ACTIVE' }>
77
+ | PendingApprovalPayload
78
+ | PendingActivationPayload
79
+
80
+ /**
81
+ * Map Complete's `data` payload: the `KeyCredential` that
82
+ * `POST /v1/accounts/auth/keys` returns for `"type": "deviceKey"`.
66
83
  *
67
- * The service returns a flat `DeviceContext`. The wrapped `{ status, context }`
68
- * shape and `RECOVERY_PENDING` are also decoded when present. An absent
69
- * `status` is read as that flat shape only on a positive signal: it still needs
70
- * `deviceCredentialStatus === 'active'` (enforced by `toDeviceContext`), and
71
- * it must carry neither `recoveryReference` nor `expiresAt` — the fields that
72
- * belong to the `RECOVERY_PENDING` shape. A payload carrying either alongside
73
- * an absent `status` is ambiguous and is rejected rather than resolved toward
74
- * the more privileged `ACTIVE` outcome. Extra members are otherwise ignored.
84
+ * DAA builds a `KeyCredential` from the same `DeviceContext` that
85
+ * `get_device_context` returns, with names that fit both key types. For a
86
+ * device key, `credentialStatus` is the `deviceCredentialStatus` and
87
+ * `credentialId` is the `authKeyId` (the `kid` that later
88
+ * `X-Device-Authorization` requests sign with). Every other member keeps its
89
+ * name. So the payload is checked for `type: "deviceKey"`, those two members
90
+ * are renamed, and the result is decoded as a `DeviceContext`:
91
+ *
92
+ * - `active` is `ACTIVE` (`toDeviceContext`), a first device or a key the
93
+ * account already holds;
94
+ * - `pending_approval` is a new device on an account that already has one (see
95
+ * `toPendingApproval`);
96
+ * - `pending_activation` is an approved device signing in again during its
97
+ * cool-down (see `toPendingActivation`);
98
+ * - any other status, or a missing one, fails closed, as does a malformed
99
+ * UUID or timestamp.
100
+ *
101
+ * Any other `type` is refused. Extra members are otherwise ignored.
75
102
  * `clientEntityId` comes from Social Session, not this payload.
76
103
  */
77
104
  export function toDeviceRegistrationResult(
78
105
  value: unknown,
79
106
  sessionClientEntityId: string,
80
- ): DeviceRegistrationResult {
81
- const payload = asObject(value, 'payload')
82
- const status = optionalString(payload, 'status')
83
- if (status === 'RECOVERY_PENDING') {
84
- if (optionalObject(payload, 'context') !== null) {
85
- throw new MalformedPayloadError('context')
86
- }
87
- const recoveryReference = requiredString(payload, 'recoveryReference')
88
- if (
89
- recoveryReference.length < RECOVERY_REF_MIN ||
90
- recoveryReference.length > RECOVERY_REF_MAX
91
- ) {
92
- throw new MalformedPayloadError('recoveryReference')
93
- }
94
- const result: DeviceRegistrationResult = Object.freeze({
95
- status: 'RECOVERY_PENDING',
96
- recoveryReference,
97
- // `expiresAt` accepts both timestamp forms, like the other login-flow
98
- // timestamps on this response.
99
- expiresAt: requiredDateTime(payload, 'expiresAt'),
100
- })
101
- return result
102
- }
103
- if (status === 'ACTIVE') {
104
- const contextPayload = optionalObject(payload, 'context')
105
- if (contextPayload === null) {
106
- throw new MalformedPayloadError('context')
107
+ ): DecodedDeviceRegistration {
108
+ const payload = deviceContextFromKeyCredential(asObject(value, 'payload'))
109
+ try {
110
+ const credentialStatus = optionalString(payload, 'deviceCredentialStatus')
111
+ if (credentialStatus === 'pending_approval') {
112
+ return toPendingApproval(payload)
107
113
  }
108
- const nestedClient = optionalString(contextPayload, 'clientEntityId')
109
- if (nestedClient !== null && nestedClient !== sessionClientEntityId) {
110
- throw new MalformedPayloadError('clientEntityId')
114
+ if (credentialStatus === 'pending_activation') {
115
+ return toPendingActivation(payload)
111
116
  }
112
- const devicePayload = deviceContextPayloadFromComplete(payload)
113
- const result: DeviceRegistrationResult = Object.freeze({
117
+ const result: DecodedDeviceRegistration = Object.freeze({
114
118
  status: 'ACTIVE',
115
119
  context: new DaaUserContext(DECODER_TOKEN, {
116
120
  clientEntityId: sessionClientEntityId,
117
- deviceContext: toDeviceContext(devicePayload),
121
+ deviceContext: toDeviceContext(payload),
118
122
  }),
119
123
  })
120
124
  return result
125
+ } catch (error) {
126
+ if (error instanceof MalformedPayloadError) {
127
+ throw new MalformedPayloadError(KEY_CREDENTIAL_NAMES[error.fieldName] ?? error.fieldName)
128
+ }
129
+ throw error
121
130
  }
122
- if (status !== null) {
123
- throw new MalformedPayloadError('status')
131
+ }
132
+
133
+ /**
134
+ * The `KeyCredential` as a flat `DeviceContext`: `credentialStatus` becomes
135
+ * `deviceCredentialStatus` and `credentialId` becomes `authKeyId`; every other
136
+ * member is kept as it is.
137
+ */
138
+ function deviceContextFromKeyCredential(payload: Record<string, unknown>): Record<string, unknown> {
139
+ if (requiredString(payload, 'type') !== DEVICE_KEY_TYPE) {
140
+ throw new MalformedPayloadError('type')
124
141
  }
142
+ const deviceContext: Record<string, unknown> = { ...payload }
143
+ delete deviceContext.type
144
+ delete deviceContext.credentialStatus
145
+ delete deviceContext.credentialId
146
+ deviceContext.deviceCredentialStatus = payload.credentialStatus
147
+ deviceContext.authKeyId = payload.credentialId
148
+ return deviceContext
149
+ }
125
150
 
126
- // Flat shape: only decodable on a positive ACTIVE signal, never on
127
- // the mere absence of `status`. `toActiveContext` still requires
128
- // `deviceCredentialStatus === 'active'`; this additionally refuses a
129
- // payload that also carries a `RECOVERY_PENDING` field, rather than
130
- // silently preferring the more privileged outcome. The error names
131
- // whichever `RECOVERY_PENDING` field is actually present — `status` itself
132
- // is absent here, not malformed.
133
- const conflictingRecoveryField = hasPresentField(payload, 'recoveryReference')
134
- ? 'recoveryReference'
135
- : hasPresentField(payload, 'expiresAt')
136
- ? 'expiresAt'
137
- : null
138
- if (conflictingRecoveryField !== null) {
139
- throw new MalformedPayloadError(conflictingRecoveryField)
151
+ /**
152
+ * The `DeviceContext` DAA returns for a new device on an account that
153
+ * already has one: `deviceCredentialStatus: "pending_approval"` with the
154
+ * request's `requestId`, `expiresAt` and the enclave's `codeNonce` (32 bytes,
155
+ * canonical unpadded Base64URL, so `PendingDeviceApproval` can decode it).
156
+ * `codeSeal`, when present, is the seal the request was created with (32
157
+ * bytes, the same encoding): for a key that was already waiting it is the one
158
+ * from its first registration, not this body's. A DAA that doesn't send it
159
+ * yet decodes as `null`. `ownerBindingId` and `resolvedAt` are not read:
160
+ * nothing here signs until the request is approved.
161
+ */
162
+ function toPendingApproval(payload: Record<string, unknown>): PendingApprovalPayload {
163
+ const codeNonce = requiredString(payload, 'codeNonce')
164
+ if (canonicalBytes32(codeNonce) === null) {
165
+ throw new MalformedPayloadError('codeNonce')
166
+ }
167
+ const codeSeal = optionalString(payload, 'codeSeal')
168
+ if (codeSeal !== null && canonicalBytes32(codeSeal) === null) {
169
+ throw new MalformedPayloadError('codeSeal')
140
170
  }
171
+ return Object.freeze({
172
+ status: 'PENDING_APPROVAL',
173
+ requestId: requiredUuid(payload, 'requestId'),
174
+ authKeyId: requiredUuid(payload, 'authKeyId'),
175
+ expiresAt: requiredDateTime(payload, 'expiresAt'),
176
+ codeNonce,
177
+ codeSeal,
178
+ })
179
+ }
141
180
 
142
- const result: DeviceRegistrationResult = Object.freeze({
143
- status: 'ACTIVE',
144
- context: toActiveContext(payload, sessionClientEntityId),
181
+ /**
182
+ * The `DeviceContext` DAA returns for a device an existing device already
183
+ * approved, signing in again before the account's cool-down ends:
184
+ * `deviceCredentialStatus: "pending_activation"` with `readyAt`, when the key
185
+ * may sign. The key is the account's, so `ownerBindingId` and `authKeyId` are
186
+ * final. The `pending_approval` fields (`requestId`, `expiresAt`, `codeNonce`,
187
+ * `codeSeal`) make the payload ambiguous, so a payload carrying any of them is
188
+ * refused. `resolvedAt` is not read.
189
+ */
190
+ function toPendingActivation(payload: Record<string, unknown>): PendingActivationPayload {
191
+ for (const name of ['requestId', 'expiresAt', 'codeNonce', 'codeSeal']) {
192
+ const value = payload[name]
193
+ if (value !== undefined && value !== null) {
194
+ throw new MalformedPayloadError(name)
195
+ }
196
+ }
197
+ return Object.freeze({
198
+ status: 'PENDING_ACTIVATION',
199
+ authKeyId: requiredUuid(payload, 'authKeyId'),
200
+ ownerBindingId: requiredUuid(payload, 'ownerBindingId'),
201
+ readyAt: requiredDateTime(payload, 'readyAt'),
145
202
  })
146
- return result
147
203
  }
148
204
 
149
- /** JSON `null` is treated as absence, matching `optionalString`/`optionalObject`. */
150
- function hasPresentField(payload: Record<string, unknown>, name: string): boolean {
151
- const value = payload[name]
152
- return value !== undefined && value !== null
205
+ function requiredUuid(payload: Record<string, unknown>, name: string): string {
206
+ const value = requiredString(payload, name)
207
+ if (!isUuid(value)) {
208
+ throw new MalformedPayloadError(name)
209
+ }
210
+ return value
153
211
  }
@@ -0,0 +1,68 @@
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 type { DeviceStatus } from '../models/DeviceStatus'
20
+ import { isUuid } from '../validation/uuid'
21
+ import { requiredDateTime } from './instant'
22
+ import { asObject, MalformedPayloadError, optionalString, requiredString } from './payload'
23
+
24
+ /** Decode the status route's `data`. An unknown `status` is kept as `unrecognized`. */
25
+ export function toDeviceStatus(value: unknown): DeviceStatus {
26
+ const payload = asObject(value, 'payload')
27
+ const authKeyId = requiredUuid(payload, 'authKeyId')
28
+ const status = requiredString(payload, 'status')
29
+ switch (status) {
30
+ case 'pending_approval':
31
+ case 'expired':
32
+ return Object.freeze({
33
+ status,
34
+ authKeyId,
35
+ requestId: requiredUuid(payload, 'requestId'),
36
+ expiresAt: requiredDateTime(payload, 'expiresAt'),
37
+ })
38
+ case 'denied':
39
+ case 'cancelled':
40
+ return Object.freeze({ status, authKeyId, requestId: requiredUuid(payload, 'requestId') })
41
+ case 'pending_activation':
42
+ return Object.freeze({
43
+ status,
44
+ authKeyId,
45
+ ...optionalRequestId(payload),
46
+ readyAt: requiredDateTime(payload, 'readyAt'),
47
+ })
48
+ case 'active':
49
+ return Object.freeze({ status, authKeyId, ...optionalRequestId(payload) })
50
+ default:
51
+ // Preserved, not refused: DAA may add a status before this SDK knows it.
52
+ return Object.freeze({ status: 'unrecognized', authKeyId, rawValue: status })
53
+ }
54
+ }
55
+
56
+ function optionalRequestId(payload: Record<string, unknown>): { requestId?: string } {
57
+ return optionalString(payload, 'requestId') === null
58
+ ? {}
59
+ : { requestId: requiredUuid(payload, 'requestId') }
60
+ }
61
+
62
+ function requiredUuid(payload: Record<string, unknown>, name: string): string {
63
+ const value = requiredString(payload, name)
64
+ if (!isUuid(value)) {
65
+ throw new MalformedPayloadError(name)
66
+ }
67
+ return value
68
+ }
@@ -35,7 +35,10 @@ import { MalformedPayloadError, requiredString } from './payload'
35
35
  //
36
36
  // The three fields are `PreparedSocialLogin.expiresAt`,
37
37
  // `SocialSession.expiresAt` and `DeviceContext.resolvedAt`. They are the same
38
- // kind of field, serialised the same way, so all three accept both forms.
38
+ // kind of field, serialised the same way, so all three accept both forms. The
39
+ // device context's other timestamps (`expiresAt` on `pending_approval`,
40
+ // `readyAt` on `pending_activation`) come in the same response and accept both
41
+ // forms too.
39
42
 
40
43
  /**
41
44
  * The ISO-8601 form the contract declares for an `Instant`, with a `T`
@@ -18,7 +18,13 @@
18
18
 
19
19
  import { isUuid } from '../validation/uuid'
20
20
  import { requiredDateTime } from './instant'
21
- import { asObject, MalformedPayloadError, optionalString, requiredString } from './payload'
21
+ import {
22
+ asObject,
23
+ MalformedPayloadError,
24
+ optionalObject,
25
+ optionalString,
26
+ requiredString,
27
+ } from './payload'
22
28
 
23
29
  /** The service caps `SocialSession.registrationHandle` at 200 characters. */
24
30
  const HANDLE_MIN = 1
@@ -36,8 +42,44 @@ export type PendingRegistrationPayload = Readonly<{
36
42
  * the response does not carry it.
37
43
  */
38
44
  ownerGrant: string | null
45
+ /** `SocialSession.deviceApproval`, or null when the service doesn't send it. */
46
+ deviceApproval: DeviceApprovalHint | null
39
47
  }>
40
48
 
49
+ /**
50
+ * DAA's answer, when the Session was answered, to whether a new device key for
51
+ * this end user would wait for approval from an existing device. A hint for
52
+ * asking the user first, not the outcome: the registration response decides.
53
+ *
54
+ * - `required`: device approval is on and the end user already has an account,
55
+ * so a new device key would wait for approval.
56
+ * - `hasActiveDevice`: the account had a device that can approve. Always
57
+ * `false` when `required` is `false`.
58
+ */
59
+ export type DeviceApprovalHint = Readonly<{
60
+ required: boolean
61
+ hasActiveDevice: boolean
62
+ }>
63
+
64
+ function requiredBoolean(payload: Record<string, unknown>, name: string, label: string): boolean {
65
+ const value = payload[name]
66
+ if (typeof value !== 'boolean') {
67
+ throw new MalformedPayloadError(label)
68
+ }
69
+ return value
70
+ }
71
+
72
+ function optionalDeviceApproval(payload: Record<string, unknown>): DeviceApprovalHint | null {
73
+ const value = optionalObject(payload, 'deviceApproval')
74
+ if (value === null) {
75
+ return null
76
+ }
77
+ return Object.freeze({
78
+ required: requiredBoolean(value, 'required', 'deviceApproval.required'),
79
+ hasActiveDevice: requiredBoolean(value, 'hasActiveDevice', 'deviceApproval.hasActiveDevice'),
80
+ })
81
+ }
82
+
41
83
  function requiredBounded(
42
84
  payload: Record<string, unknown>,
43
85
  name: string,
@@ -71,5 +113,6 @@ export function toPendingRegistration(value: unknown): PendingRegistrationPayloa
71
113
  // is accepted here.
72
114
  expiresAt: requiredDateTime(payload, 'expiresAt'),
73
115
  ownerGrant,
116
+ deviceApproval: optionalDeviceApproval(payload),
74
117
  }
75
118
  }
@@ -22,17 +22,8 @@ import { asObject, MalformedPayloadError, requiredString } from './payload'
22
22
 
23
23
  // Decoder for Prepare's `201` `data` payload.
24
24
  //
25
- // The `PreparedSocialLogin` body is loginRequestId, oidcNonce, oidcState,
26
- // expiresAt, and `prepareReceiptJws` when the service signs a receipt. Extra
27
- // response members are ignored.
28
- //
29
- // The receipt is carried through raw and not examined here: not its presence,
30
- // its type, or its shape. What to do with it is decided per environment by
31
- // `auth/prepareReceipt.ts`'s `checkPrepareReceipt`, which `prepareSocialLogin`
32
- // calls right after this decoder. With no trust anchor pinned the receipt is
33
- // ignored, so a receipt the SDK cannot use must not fail the response here;
34
- // with an anchor pinned, a missing or malformed receipt is
35
- // `prepare_receipt_invalid` there rather than a malformed response here.
25
+ // The `PreparedSocialLogin` body is loginRequestId, oidcNonce, oidcState, and
26
+ // expiresAt. Extra response members are ignored.
36
27
 
37
28
  const OIDC_VALUE_MIN = 32
38
29
  const OIDC_VALUE_MAX = 256
@@ -43,11 +34,6 @@ export type SocialLoginRequestPayload = Readonly<{
43
34
  oidcState: string
44
35
  /** ISO-8601, normalised when the service sends epoch milliseconds instead. */
45
36
  expiresAt: string
46
- /**
47
- * The raw `prepareReceiptJws` member, unvalidated: `null` when absent. Only
48
- * `checkPrepareReceipt` reads it.
49
- */
50
- prepareReceiptJws: unknown
51
37
  }>
52
38
 
53
39
  function requiredUuid(payload: Record<string, unknown>, name: string): string {
@@ -66,14 +52,6 @@ function requiredOidcValue(payload: Record<string, unknown>, name: string): stri
66
52
  return value
67
53
  }
68
54
 
69
- /** The raw receipt member: `null` when absent or JSON `null`, else whatever was sent. */
70
- function rawPrepareReceipt(payload: Record<string, unknown>): unknown {
71
- const value = Object.prototype.hasOwnProperty.call(payload, 'prepareReceiptJws')
72
- ? payload['prepareReceiptJws']
73
- : undefined
74
- return value === undefined ? null : value
75
- }
76
-
77
55
  /** Map a Prepare success payload onto the fields the SDK keeps. */
78
56
  export function toSocialLoginRequest(value: unknown): SocialLoginRequestPayload {
79
57
  const payload = asObject(value, 'payload')
@@ -84,6 +62,5 @@ export function toSocialLoginRequest(value: unknown): SocialLoginRequestPayload
84
62
  // The service sends this as epoch milliseconds; the decoder also accepts
85
63
  // ISO-8601. See `instant.ts`.
86
64
  expiresAt: requiredDateTime(payload, 'expiresAt'),
87
- prepareReceiptJws: rawPrepareReceipt(payload),
88
65
  }
89
66
  }
@@ -19,6 +19,7 @@
19
19
  import {
20
20
  Transaction,
21
21
  TransactionBlockchainLocation,
22
+ TransactionContractExecution,
22
23
  TransactionLocation,
23
24
  TransactionStatus,
24
25
  TransactionType,
@@ -27,6 +28,7 @@ import { requiredMoney } from './moneyPayload'
27
28
  import {
28
29
  asObject,
29
30
  MalformedPayloadError,
31
+ optionalObject,
30
32
  optionalString,
31
33
  requiredString,
32
34
  unrecognized,
@@ -36,8 +38,8 @@ import {
36
38
  * Wire payload -> public `Transaction` model, and the required/optional line.
37
39
  *
38
40
  * Generic object and string readers come from `wire/payload`; shared money
39
- * decoding comes from `wire/moneyPayload`. Transaction-specific enum and
40
- * location decoding remains here.
41
+ * decoding comes from `wire/moneyPayload`. Transaction-specific enum,
42
+ * location, and contract-execution summary decoding remains here.
41
43
  */
42
44
 
43
45
  const TRANSACTION_TYPES = new Map<string, TransactionType>([
@@ -47,6 +49,7 @@ const TRANSACTION_TYPES = new Map<string, TransactionType>([
47
49
  ['fx', Object.freeze({ kind: 'fx' })],
48
50
  ['fee', Object.freeze({ kind: 'fee' })],
49
51
  ['credit', Object.freeze({ kind: 'credit' })],
52
+ ['contract_execution', Object.freeze({ kind: 'contract_execution' })],
50
53
  ['other', Object.freeze({ kind: 'other' })],
51
54
  ])
52
55
 
@@ -111,14 +114,46 @@ function toOptionalTransactionLocation(value: unknown, field: string): Transacti
111
114
  return toTransactionLocation(value, field)
112
115
  }
113
116
 
117
+ /**
118
+ * Decode the optional `contractExecution` summary (OpenAPI
119
+ * `ContractExecutionActivityDetails`). Absent or JSON `null` is `null`.
120
+ *
121
+ * Once present, `id`, `blockchain`, and `contractAddress` are required and
122
+ * `functionSelector` is optional. `blockchain` is read as a string rather than
123
+ * checked against the service's current chain list, so a chain added later
124
+ * does not fail the row. The selector pattern is not re-checked either.
125
+ */
126
+ function toOptionalContractExecution(
127
+ payload: Record<string, unknown>,
128
+ ): TransactionContractExecution | null {
129
+ const summary = optionalObject(payload, 'contractExecution')
130
+ if (summary === null) {
131
+ return null
132
+ }
133
+ return new TransactionContractExecution({
134
+ id: requiredString(summary, 'id', 'contractExecution.id'),
135
+ blockchain: requiredString(summary, 'blockchain', 'contractExecution.blockchain'),
136
+ contractAddress: requiredString(
137
+ summary,
138
+ 'contractAddress',
139
+ 'contractExecution.contractAddress',
140
+ ),
141
+ functionSelector: optionalString(
142
+ summary,
143
+ 'functionSelector',
144
+ 'contractExecution.functionSelector',
145
+ ),
146
+ })
147
+ }
148
+
114
149
  /**
115
150
  * Map a decoded `Transaction` payload onto the public model.
116
151
  *
117
152
  * Unknown additional fields are ignored and an unfamiliar present closed-set
118
153
  * or location value degrades to `unrecognized`; a missing guaranteed field
119
154
  * throws. Guaranteed fields are `id`, `type`, `status`, `amount`, `createDate`,
120
- * and `updateDate` — see `models/Transaction.ts`. `source` and `destination`
121
- * decode to `null` when absent.
155
+ * and `updateDate` — see `models/Transaction.ts`. `source`, `destination`, and
156
+ * `contractExecution` decode to `null` when absent.
122
157
  *
123
158
  * @throws MalformedPayloadError
124
159
  */
@@ -132,6 +167,7 @@ export function toTransaction(value: unknown): Transaction {
132
167
  amount: requiredMoney(payload, 'amount'),
133
168
  source: toOptionalTransactionLocation(payload['source'], 'source'),
134
169
  destination: toOptionalTransactionLocation(payload['destination'], 'destination'),
170
+ contractExecution: toOptionalContractExecution(payload),
135
171
  createDate: requiredString(payload, 'createDate'),
136
172
  updateDate: requiredString(payload, 'updateDate'),
137
173
  })