@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.
- package/DaaReactNativeSdk.podspec +14 -8
- package/README.md +498 -24
- package/android/build.gradle +12 -7
- package/android/src/main/java/com/circle/daa/reactnative/DaaRegistrationModule.kt +1 -18
- package/android/src/main/java/com/circle/daa/reactnative/DaaSecureKeyExceptions.kt +38 -4
- package/android/src/main/java/com/circle/daa/reactnative/DaaSecureKeyModule.kt +139 -6
- package/android/src/main/java/com/circle/daa/reactnative/InstallationIdentityStore.kt +50 -1
- package/build/DaaClient.d.ts +183 -3
- package/build/DaaClient.js +296 -12
- package/build/api/AccountsClient.d.ts +3 -0
- package/build/api/AccountsClient.js +3 -0
- package/build/api/AuthClient.d.ts +17 -42
- package/build/api/AuthClient.js +26 -62
- package/build/api/ContractExecutionsClient.d.ts +114 -0
- package/build/api/ContractExecutionsClient.js +462 -0
- package/build/api/DaaUserClient.d.ts +74 -11
- package/build/api/DaaUserClient.js +84 -17
- package/build/api/TransactionsClient.d.ts +1 -1
- package/build/api/TransactionsClient.js +1 -0
- package/build/api/TransfersClient.d.ts +3 -0
- package/build/api/TransfersClient.js +3 -0
- package/build/api/WithdrawalsClient.d.ts +3 -0
- package/build/api/WithdrawalsClient.js +3 -0
- package/build/api/deviceApproval.d.ts +48 -0
- package/build/api/deviceApproval.js +247 -0
- package/build/api/deviceStatus.d.ts +61 -0
- package/build/api/deviceStatus.js +216 -0
- package/build/api/loginFailure.d.ts +10 -2
- package/build/api/loginFailure.js +33 -2
- package/build/api/signedResource.d.ts +49 -2
- package/build/api/signedResource.js +135 -36
- package/build/auth/RegistrationFlowKey.js +1 -1
- package/build/auth/daaRegistrationNative.d.ts +0 -22
- package/build/auth/daaRegistrationNative.js +3 -29
- package/build/auth/deviceAuthorization.d.ts +17 -3
- package/build/auth/deviceAuthorization.js +60 -8
- package/build/auth/registrationAuthorization.d.ts +2 -2
- package/build/auth/registrationAuthorization.js +13 -10
- package/build/config.d.ts +27 -0
- package/build/config.js +15 -0
- package/build/crypto/matchingCode.d.ts +47 -0
- package/build/crypto/matchingCode.js +93 -0
- package/build/errors.d.ts +150 -33
- package/build/errors.js +208 -47
- package/build/index.d.ts +11 -3
- package/build/models/ContractExecution.d.ts +197 -0
- package/build/models/ContractExecution.js +130 -0
- package/build/models/CreateContractExecutionRequest.d.ts +81 -0
- package/build/models/CreateContractExecutionRequest.js +19 -0
- package/build/models/DaaUserContext.d.ts +53 -3
- package/build/models/DaaUserContext.js +33 -2
- package/build/models/DeviceStatus.d.ts +69 -0
- package/build/models/DeviceStatus.js +19 -0
- package/build/models/PendingDeviceApproval.d.ts +121 -0
- package/build/models/PendingDeviceApproval.js +208 -0
- package/build/models/PendingDeviceRegistration.d.ts +162 -23
- package/build/models/PendingDeviceRegistration.js +525 -69
- package/build/models/PreparedContractExecution.d.ts +70 -0
- package/build/models/PreparedContractExecution.js +77 -0
- package/build/models/PreparedSocialLogin.d.ts +19 -26
- package/build/models/PreparedSocialLogin.js +32 -15
- package/build/models/Transaction.d.ts +48 -2
- package/build/models/Transaction.js +52 -4
- package/build/models/codeSecrets.d.ts +100 -0
- package/build/models/codeSecrets.js +321 -0
- package/build/securekey/InMemorySecureKey.d.ts +3 -1
- package/build/securekey/InMemorySecureKey.js +47 -6
- package/build/securekey/NativeSecureKey.d.ts +3 -1
- package/build/securekey/NativeSecureKey.js +204 -27
- package/build/securekey/NativeSecureKeyBridge.d.ts +72 -4
- package/build/securekey/SecureKey.d.ts +128 -8
- package/build/securekey/alias.d.ts +9 -0
- package/build/securekey/alias.js +11 -2
- package/build/securekey/ownerAuthKeyResolver.d.ts +26 -9
- package/build/securekey/ownerAuthKeyResolver.js +35 -20
- package/build/securekey/registration.d.ts +19 -0
- package/build/securekey/registration.js +38 -0
- package/build/securekey/resolveNativeBridge.d.ts +3 -0
- package/build/securekey/resolveNativeBridge.js +88 -7
- package/build/transport/headers.d.ts +11 -0
- package/build/transport/headers.js +36 -0
- package/build/wire/contractExecutionPayload.d.ts +24 -0
- package/build/wire/contractExecutionPayload.js +130 -0
- package/build/wire/daaStatusCodes.d.ts +20 -3
- package/build/wire/daaStatusCodes.js +20 -3
- package/build/wire/deviceContextPayload.d.ts +9 -7
- package/build/wire/deviceContextPayload.js +9 -7
- package/build/wire/deviceRegistrationResultPayload.d.ts +55 -10
- package/build/wire/deviceRegistrationResultPayload.js +116 -82
- package/build/wire/deviceStatusPayload.d.ts +20 -0
- package/build/wire/deviceStatusPayload.js +66 -0
- package/build/wire/instant.js +4 -1
- package/build/wire/pendingRegistrationPayload.d.ts +16 -0
- package/build/wire/pendingRegistrationPayload.js +18 -0
- package/build/wire/socialLoginRequestPayload.d.ts +0 -5
- package/build/wire/socialLoginRequestPayload.js +2 -19
- package/build/wire/transactionPayload.d.ts +2 -2
- package/build/wire/transactionPayload.js +27 -4
- package/ios/DaaRegistrationModule.swift +1 -19
- package/ios/DaaSecureKeyExceptions.swift +53 -5
- package/ios/DaaSecureKeyModule.swift +149 -5
- package/ios/InstallationIdentityStore.swift +43 -7
- package/package.json +1 -1
- package/src/DaaClient.ts +353 -15
- package/src/api/AccountsClient.ts +3 -0
- package/src/api/AuthClient.ts +33 -98
- package/src/api/ContractExecutionsClient.ts +527 -0
- package/src/api/DaaUserClient.ts +92 -17
- package/src/api/TransactionsClient.ts +1 -0
- package/src/api/TransfersClient.ts +3 -0
- package/src/api/WithdrawalsClient.ts +3 -0
- package/src/api/deviceApproval.ts +306 -0
- package/src/api/deviceStatus.ts +238 -0
- package/src/api/loginFailure.ts +39 -2
- package/src/api/signedResource.ts +206 -46
- package/src/auth/RegistrationFlowKey.ts +1 -1
- package/src/auth/daaRegistrationNative.ts +4 -49
- package/src/auth/deviceAuthorization.ts +74 -11
- package/src/auth/registrationAuthorization.ts +12 -9
- package/src/config.ts +36 -0
- package/src/crypto/matchingCode.ts +100 -0
- package/src/errors.ts +309 -52
- package/src/index.ts +36 -1
- package/src/models/ContractExecution.ts +250 -0
- package/src/models/CreateContractExecutionRequest.ts +100 -0
- package/src/models/DaaUserContext.ts +59 -3
- package/src/models/DeviceStatus.ts +50 -0
- package/src/models/PendingDeviceApproval.ts +229 -0
- package/src/models/PendingDeviceRegistration.ts +673 -79
- package/src/models/PreparedContractExecution.ts +128 -0
- package/src/models/PreparedSocialLogin.ts +41 -31
- package/src/models/Transaction.ts +77 -2
- package/src/models/codeSecrets.ts +360 -0
- package/src/securekey/InMemorySecureKey.ts +61 -7
- package/src/securekey/NativeSecureKey.ts +234 -25
- package/src/securekey/NativeSecureKeyBridge.ts +84 -4
- package/src/securekey/SecureKey.ts +138 -8
- package/src/securekey/alias.ts +11 -3
- package/src/securekey/ownerAuthKeyResolver.ts +63 -24
- package/src/securekey/registration.ts +40 -0
- package/src/securekey/resolveNativeBridge.ts +167 -9
- package/src/transport/headers.ts +47 -0
- package/src/wire/contractExecutionPayload.ts +152 -0
- package/src/wire/daaStatusCodes.ts +20 -3
- package/src/wire/deviceContextPayload.ts +9 -7
- package/src/wire/deviceRegistrationResultPayload.ts +156 -98
- package/src/wire/deviceStatusPayload.ts +68 -0
- package/src/wire/instant.ts +4 -1
- package/src/wire/pendingRegistrationPayload.ts +44 -1
- package/src/wire/socialLoginRequestPayload.ts +2 -25
- package/src/wire/transactionPayload.ts +40 -4
- package/build/auth/prepareReceipt.d.ts +0 -150
- package/build/auth/prepareReceipt.js +0 -357
- package/build/crypto/es256Jws.d.ts +0 -56
- package/build/crypto/es256Jws.js +0 -196
- package/src/auth/prepareReceipt.ts +0 -522
- package/src/crypto/es256Jws.ts +0 -240
|
@@ -17,14 +17,13 @@
|
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
19
|
import { requireOptionalNativeModule } from 'expo-modules-core'
|
|
20
|
-
import { fromBase64Url
|
|
20
|
+
import { fromBase64Url } from '../crypto/base64url'
|
|
21
21
|
import { contractViolation, isDaaError } from '../errors'
|
|
22
22
|
|
|
23
23
|
// JS shape of the Expo `DaaRegistration` native module. Separate from
|
|
24
|
-
// `DaaSecureKey`: this module bridges DaaSecureRandom
|
|
25
|
-
//
|
|
26
|
-
//
|
|
27
|
-
// strings cross the bridge.
|
|
24
|
+
// `DaaSecureKey`: this module bridges DaaSecureRandom and
|
|
25
|
+
// DaaRegistrationFlowKey. The flow-key private key stays in the native cache
|
|
26
|
+
// keyed by the handle id; only Base64URL strings cross the bridge.
|
|
28
27
|
|
|
29
28
|
export const DAA_REGISTRATION_MODULE = 'DaaRegistration'
|
|
30
29
|
|
|
@@ -41,19 +40,6 @@ export interface DaaRegistrationNativeModule {
|
|
|
41
40
|
* key, returning unpadded Base64URL of the 64-byte JOSE `R || S` signature.
|
|
42
41
|
*/
|
|
43
42
|
signRegistrationAuthorization(id: string, signingInputBase64Url: string): string
|
|
44
|
-
/**
|
|
45
|
-
* Native ES256 verification of a Prepare receipt signature, delegated to the
|
|
46
|
-
* native SDKs' `DaaPrepareReceiptSignature` (CryptoKit on iOS,
|
|
47
|
-
* `java.security` on Android). All three arguments are unpadded
|
|
48
|
-
* Base64URL — the uncompressed P-256 point of a pinned key, the exact JWS
|
|
49
|
-
* signing input, and the 64-byte JOSE `R || S` signature. Resolves `true`
|
|
50
|
-
* only for a valid signature; rejects when the key is not a P-256 point.
|
|
51
|
-
*/
|
|
52
|
-
verifyPrepareReceiptSignature(
|
|
53
|
-
publicKeyX963Base64Url: string,
|
|
54
|
-
signingInputBase64Url: string,
|
|
55
|
-
signatureBase64Url: string,
|
|
56
|
-
): Promise<boolean>
|
|
57
43
|
}
|
|
58
44
|
|
|
59
45
|
export function optionalDaaRegistrationNative(): DaaRegistrationNativeModule | null {
|
|
@@ -182,37 +168,6 @@ export function nativeSignRegistrationAuthorization(
|
|
|
182
168
|
return bytes
|
|
183
169
|
}
|
|
184
170
|
|
|
185
|
-
/**
|
|
186
|
-
* Verify one ES256 signature with platform crypto.
|
|
187
|
-
*
|
|
188
|
-
* Only a literal `true` from native counts as verified. Anything else — `false`,
|
|
189
|
-
* a non-boolean, a rejection, a missing function on an older host build — is
|
|
190
|
-
* not a pass: a rejection or a missing function is a `contract_violation`, and
|
|
191
|
-
* any other value is `false`.
|
|
192
|
-
*/
|
|
193
|
-
export async function nativeVerifyPrepareReceiptSignature(
|
|
194
|
-
native: DaaRegistrationNativeModule,
|
|
195
|
-
args: Readonly<{ publicKeyX963: Uint8Array; signingInput: Uint8Array; signature: Uint8Array }>,
|
|
196
|
-
): Promise<boolean> {
|
|
197
|
-
if (typeof native.verifyPrepareReceiptSignature !== 'function') {
|
|
198
|
-
throw contractViolation('native DaaRegistration.verifyPrepareReceiptSignature is required')
|
|
199
|
-
}
|
|
200
|
-
let result: unknown
|
|
201
|
-
try {
|
|
202
|
-
result = await native.verifyPrepareReceiptSignature(
|
|
203
|
-
toBase64Url(args.publicKeyX963),
|
|
204
|
-
toBase64Url(args.signingInput),
|
|
205
|
-
toBase64Url(args.signature),
|
|
206
|
-
)
|
|
207
|
-
} catch (err) {
|
|
208
|
-
if (isDaaError(err)) {
|
|
209
|
-
throw err
|
|
210
|
-
}
|
|
211
|
-
throw contractViolation('native Prepare receipt signature verification failed')
|
|
212
|
-
}
|
|
213
|
-
return result === true
|
|
214
|
-
}
|
|
215
|
-
|
|
216
171
|
/**
|
|
217
172
|
* Drop a cached native flow key.
|
|
218
173
|
*
|
|
@@ -31,22 +31,22 @@
|
|
|
31
31
|
// Operation identity is the digest-pinned Owner-authorization registry:
|
|
32
32
|
// `discriminator` + `schema_version` are the lookup key. The verifier resolves
|
|
33
33
|
// method and `canonical_path` from that entry; it does not infer the operation
|
|
34
|
-
// from the path. Callers name one of the
|
|
34
|
+
// from the path. Callers name one of the eight closed operations — they cannot
|
|
35
35
|
// pass a free-form `/v1` path.
|
|
36
36
|
//
|
|
37
37
|
// The header carries the published `typ` (`aw-device-authorization+jws`).
|
|
38
38
|
//
|
|
39
39
|
// Production `OwnerAuthKey.signExactBytes` reaches native
|
|
40
40
|
// `signDeviceAuthorization` via the Expo bridge. Tests use
|
|
41
|
-
// `InMemorySecureKeyStore`. The create-account, transfer, and
|
|
42
|
-
// clients reach this through `api/signedResource.ts` rather
|
|
43
|
-
// directly.
|
|
41
|
+
// `InMemorySecureKeyStore`. The create-account, transfer, withdrawal, and
|
|
42
|
+
// contract-execution clients reach this through `api/signedResource.ts` rather
|
|
43
|
+
// than calling it directly.
|
|
44
44
|
|
|
45
|
-
import { toBase64Url } from '../crypto/base64url'
|
|
45
|
+
import { fromBase64Url, toBase64Url } from '../crypto/base64url'
|
|
46
46
|
import { randomUuidV4 } from '../crypto/random'
|
|
47
47
|
import { sha256 } from '../crypto/sha256'
|
|
48
48
|
import { contractViolation, mapNativeError } from '../errors'
|
|
49
|
-
import type { OwnerAuthKey } from '../securekey/SecureKey'
|
|
49
|
+
import type { DeviceApprovalCheck, OwnerAuthKey } from '../securekey/SecureKey'
|
|
50
50
|
import { isUuid } from '../validation/uuid'
|
|
51
51
|
|
|
52
52
|
export const DEVICE_AUTHORIZATION_HEADER = 'X-Device-Authorization'
|
|
@@ -57,6 +57,8 @@ const JOSE_SIGNATURE_LENGTH = 64
|
|
|
57
57
|
/** Verifier ceiling: `exp - iat` must be at most 300 seconds. */
|
|
58
58
|
const AUTHORIZATION_LIFETIME_SECONDS = 300
|
|
59
59
|
const SCHEMA_VERSION = 1
|
|
60
|
+
/** `register_device_key` is version 3: the device key operation on `/v1/accounts/auth/keys`. */
|
|
61
|
+
const REGISTER_DEVICE_KEY_SCHEMA_VERSION = 3
|
|
60
62
|
|
|
61
63
|
/**
|
|
62
64
|
* Closed Owner-authorization registry (v1). Wire `discriminator` values from
|
|
@@ -66,13 +68,15 @@ export type DeviceAuthorizationOperation =
|
|
|
66
68
|
| 'create_account'
|
|
67
69
|
| 'transfer'
|
|
68
70
|
| 'withdrawal'
|
|
71
|
+
| 'contract_execution'
|
|
69
72
|
| 'complete_social_login'
|
|
70
73
|
| 'get_device_context'
|
|
71
74
|
| 'register_device_key'
|
|
75
|
+
| 'approve_device'
|
|
72
76
|
|
|
73
77
|
export type DeviceAuthorizationProfile = Readonly<{
|
|
74
78
|
discriminator: DeviceAuthorizationOperation
|
|
75
|
-
schemaVersion: typeof SCHEMA_VERSION
|
|
79
|
+
schemaVersion: typeof SCHEMA_VERSION | typeof REGISTER_DEVICE_KEY_SCHEMA_VERSION
|
|
76
80
|
method: 'GET' | 'POST'
|
|
77
81
|
canonicalPath: string
|
|
78
82
|
}>
|
|
@@ -84,6 +88,12 @@ export type DeviceAuthorizationRequest = Readonly<{
|
|
|
84
88
|
operation: DeviceAuthorizationOperation
|
|
85
89
|
/** Exact bytes that will be hashed and sent. Empty array for a bodyless call. */
|
|
86
90
|
bodyBytes: Uint8Array
|
|
91
|
+
/**
|
|
92
|
+
* Required for `approve_device`, and refused for every other operation:
|
|
93
|
+
* the code secret and the code the user typed, which native checks against
|
|
94
|
+
* `bodyBytes` before it shows the biometric prompt.
|
|
95
|
+
*/
|
|
96
|
+
approval?: DeviceApprovalCheck
|
|
87
97
|
}>
|
|
88
98
|
|
|
89
99
|
/**
|
|
@@ -127,6 +137,13 @@ export function deviceAuthorizationProfile(
|
|
|
127
137
|
method: 'POST',
|
|
128
138
|
canonicalPath: '/v1/accounts/withdrawals',
|
|
129
139
|
}
|
|
140
|
+
case 'contract_execution':
|
|
141
|
+
return {
|
|
142
|
+
discriminator: 'contract_execution',
|
|
143
|
+
schemaVersion: SCHEMA_VERSION,
|
|
144
|
+
method: 'POST',
|
|
145
|
+
canonicalPath: '/v1/accounts/contractExecutions',
|
|
146
|
+
}
|
|
130
147
|
case 'complete_social_login':
|
|
131
148
|
return {
|
|
132
149
|
discriminator: 'complete_social_login',
|
|
@@ -144,9 +161,16 @@ export function deviceAuthorizationProfile(
|
|
|
144
161
|
case 'register_device_key':
|
|
145
162
|
return {
|
|
146
163
|
discriminator: 'register_device_key',
|
|
164
|
+
schemaVersion: REGISTER_DEVICE_KEY_SCHEMA_VERSION,
|
|
165
|
+
method: 'POST',
|
|
166
|
+
canonicalPath: '/v1/accounts/auth/keys',
|
|
167
|
+
}
|
|
168
|
+
case 'approve_device':
|
|
169
|
+
return {
|
|
170
|
+
discriminator: 'approve_device',
|
|
147
171
|
schemaVersion: SCHEMA_VERSION,
|
|
148
172
|
method: 'POST',
|
|
149
|
-
canonicalPath: '/v1/accounts/auth/
|
|
173
|
+
canonicalPath: '/v1/accounts/auth/deviceApprovals',
|
|
150
174
|
}
|
|
151
175
|
default: {
|
|
152
176
|
const exhaustive: never = operation
|
|
@@ -178,6 +202,9 @@ export async function authorizeExactRequest(
|
|
|
178
202
|
if (!isUuid(request.authKeyId)) {
|
|
179
203
|
throw contractViolation('authKeyId must be a UUID')
|
|
180
204
|
}
|
|
205
|
+
if ((request.operation === 'approve_device') !== (request.approval !== undefined)) {
|
|
206
|
+
throw contractViolation('approve_device, and only approve_device, is signed with an approval')
|
|
207
|
+
}
|
|
181
208
|
|
|
182
209
|
const profile = deviceAuthorizationProfile(request.operation)
|
|
183
210
|
const idempotencyKey = idempotencyKeyFromExactBody(request.bodyBytes)
|
|
@@ -201,7 +228,7 @@ export async function authorizeExactRequest(
|
|
|
201
228
|
})
|
|
202
229
|
|
|
203
230
|
const signingInput = toBase64Url(utf8Json(protectedHeader)) + '.' + toBase64Url(utf8Json(payload))
|
|
204
|
-
const signature = await signSigningInput(request
|
|
231
|
+
const signature = await signSigningInput(request, signingInput)
|
|
205
232
|
return signingInput + '.' + toBase64Url(signature)
|
|
206
233
|
}
|
|
207
234
|
|
|
@@ -241,10 +268,24 @@ function payloadClaims(args: {
|
|
|
241
268
|
}
|
|
242
269
|
}
|
|
243
270
|
|
|
244
|
-
async function signSigningInput(
|
|
271
|
+
async function signSigningInput(
|
|
272
|
+
request: DeviceAuthorizationRequest,
|
|
273
|
+
signingInput: string,
|
|
274
|
+
): Promise<Uint8Array> {
|
|
275
|
+
const { authKey, approval, bodyBytes } = request
|
|
276
|
+
const message = new TextEncoder().encode(signingInput)
|
|
245
277
|
let signature: Uint8Array
|
|
246
278
|
try {
|
|
247
|
-
|
|
279
|
+
if (approval === undefined) {
|
|
280
|
+
signature = await authKey.signExactBytes(message)
|
|
281
|
+
} else {
|
|
282
|
+
// A separate native call: it checks the typed code against the body
|
|
283
|
+
// and code secret, and shows that code in the biometric prompt.
|
|
284
|
+
if (authKey.signDeviceApproval === undefined) {
|
|
285
|
+
throw contractViolation('this SecureKeyStore cannot sign a device approval')
|
|
286
|
+
}
|
|
287
|
+
signature = await authKey.signDeviceApproval(message, { ...approval, requestBody: bodyBytes })
|
|
288
|
+
}
|
|
248
289
|
} catch (err) {
|
|
249
290
|
throw mapNativeError(err)
|
|
250
291
|
}
|
|
@@ -300,3 +341,25 @@ export function idempotencyKeyFromExactBody(bodyBytes: Uint8Array): string | und
|
|
|
300
341
|
function utf8Json(value: object): Uint8Array {
|
|
301
342
|
return new TextEncoder().encode(JSON.stringify(value))
|
|
302
343
|
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Decode the protected header (`0`) or payload (`1`) of a compact JWS this SDK
|
|
347
|
+
* signed, or `null` if it is not readable JSON. Reading, not verification: the
|
|
348
|
+
* signature is DAA's to check.
|
|
349
|
+
*/
|
|
350
|
+
export function jwsSegmentJson(compact: string, index: 0 | 1): Record<string, unknown> | null {
|
|
351
|
+
const segments = compact.split('.')
|
|
352
|
+
if (segments.length !== 3) {
|
|
353
|
+
return null
|
|
354
|
+
}
|
|
355
|
+
try {
|
|
356
|
+
const value: unknown = JSON.parse(
|
|
357
|
+
new TextDecoder('utf-8', { fatal: true }).decode(fromBase64Url(segments[index] ?? '')),
|
|
358
|
+
)
|
|
359
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
360
|
+
? (value as Record<string, unknown>)
|
|
361
|
+
: null
|
|
362
|
+
} catch {
|
|
363
|
+
return null
|
|
364
|
+
}
|
|
365
|
+
}
|
|
@@ -18,6 +18,8 @@
|
|
|
18
18
|
|
|
19
19
|
// Device Registration Complete dual-header proofs.
|
|
20
20
|
//
|
|
21
|
+
// Complete is `POST /v1/accounts/auth/keys` with `"type": "deviceKey"`, and
|
|
22
|
+
// both proofs name the registry operation `register_device_key` version 3.
|
|
21
23
|
// Complete sends two JWSs, in two headers:
|
|
22
24
|
//
|
|
23
25
|
// X-Registration-Flow-Authorization typ=daa-registration-flow-authorization+jws
|
|
@@ -38,9 +40,10 @@
|
|
|
38
40
|
// strictly and rejects any field outside that set, so adding a binding claim
|
|
39
41
|
// to the device proof makes Complete fail. Keep the two builders separate.
|
|
40
42
|
// Native's registration validator (`DaaRegistrationAuthorizationSigningInput`)
|
|
41
|
-
// must accept this request-claims-only device payload
|
|
42
|
-
//
|
|
43
|
-
//
|
|
43
|
+
// must accept this request-claims-only device payload, and must allow
|
|
44
|
+
// `register_device_key` version 3 on `/v1/accounts/auth/keys`: a native SDK
|
|
45
|
+
// that requires the binding claims, or allows only another operation, refuses
|
|
46
|
+
// to sign it, and Complete fails on device before any request is sent.
|
|
44
47
|
//
|
|
45
48
|
// The only bytes each key signs are its own JWS signing input. This module is
|
|
46
49
|
// not exported from `index.ts`.
|
|
@@ -70,8 +73,8 @@ import { registrationCodeChallengeS256 } from './registrationCodeVerifier'
|
|
|
70
73
|
export const REGISTRATION_FLOW_AUTHORIZATION_HEADER = 'X-Registration-Flow-Authorization'
|
|
71
74
|
export const REGISTRATION_DEVICE_AUTHORIZATION_HEADER = 'X-Device-Authorization'
|
|
72
75
|
|
|
73
|
-
/** Public path of
|
|
74
|
-
export const
|
|
76
|
+
/** Public path of Device Registration Complete (OpenAPI + signed `canonical_path`). */
|
|
77
|
+
export const KEY_REGISTRATION_PATH = '/v1/accounts/auth/keys'
|
|
75
78
|
|
|
76
79
|
const FLOW_TYP = 'daa-registration-flow-authorization+jws'
|
|
77
80
|
const DEVICE_TYP = 'daa-device-authorization+jws'
|
|
@@ -79,9 +82,9 @@ const AUDIENCE = 'circle-digital-asset-accounts'
|
|
|
79
82
|
const JOSE_SIGNATURE_LENGTH = 64
|
|
80
83
|
/** Verifier ceiling: `exp - iat` must be at most 300 seconds. */
|
|
81
84
|
const AUTHORIZATION_LIFETIME_SECONDS = 300
|
|
82
|
-
/** The
|
|
83
|
-
const DISCRIMINATOR = '
|
|
84
|
-
const SCHEMA_VERSION =
|
|
85
|
+
/** The registry operation for a device key on `/v1/accounts/auth/keys`. */
|
|
86
|
+
const DISCRIMINATOR = 'register_device_key'
|
|
87
|
+
const SCHEMA_VERSION = 3
|
|
85
88
|
|
|
86
89
|
export type RegistrationAuthorizationClock = Readonly<{
|
|
87
90
|
nowEpochSeconds: () => number
|
|
@@ -214,7 +217,7 @@ function requestTargetClaims(args: RequestClaimInputs): object {
|
|
|
214
217
|
schema_version: SCHEMA_VERSION,
|
|
215
218
|
body_sha256: toBase64Url(sha256(args.bodyBytes)),
|
|
216
219
|
idempotency_key: args.idempotencyKey,
|
|
217
|
-
canonical_path:
|
|
220
|
+
canonical_path: KEY_REGISTRATION_PATH,
|
|
218
221
|
method: 'POST',
|
|
219
222
|
aud: AUDIENCE,
|
|
220
223
|
}
|
package/src/config.ts
CHANGED
|
@@ -100,8 +100,44 @@ export type DaaSdkConfiguration = Readonly<{
|
|
|
100
100
|
* anchor.
|
|
101
101
|
*/
|
|
102
102
|
publicApiOrigin?: string
|
|
103
|
+
/**
|
|
104
|
+
* Turns on adding a device with approval from an existing device. Off by
|
|
105
|
+
* default. When on, Device Registration Complete signs `codeSeal` into every
|
|
106
|
+
* body, and a new device on an account that already has one comes back
|
|
107
|
+
* `PENDING_APPROVAL` with a matching code. Off, the body has no `codeSeal`.
|
|
108
|
+
*
|
|
109
|
+
* Turn it on after DAA runs enclave 0.1.93 or later (older enclaves refuse
|
|
110
|
+
* every registration that carries `codeSeal`, first devices included) and
|
|
111
|
+
* before DAA turns on device approval in that environment (it then refuses a
|
|
112
|
+
* new device on an existing account without `codeSeal`, so with the switch
|
|
113
|
+
* off a second device can't be added).
|
|
114
|
+
*
|
|
115
|
+
* Keep it off in production for now: after an app restart, signing in again
|
|
116
|
+
* while a request is waiting returns that request, sealed with a code secret
|
|
117
|
+
* the app no longer has. This SDK reports its code as unavailable
|
|
118
|
+
* (`matching_code_unavailable`) only when DAA returns the request's
|
|
119
|
+
* `codeSeal`; without it, the app would show a code that can't match.
|
|
120
|
+
* Temporary: it becomes the default once every DAA environment runs 0.1.93
|
|
121
|
+
* or later and returns `codeSeal`.
|
|
122
|
+
*/
|
|
123
|
+
addDeviceApproval?: boolean
|
|
103
124
|
}>
|
|
104
125
|
|
|
126
|
+
/**
|
|
127
|
+
* Runtime check for `addDeviceApproval`: `true`, `false`, or unset (off). A
|
|
128
|
+
* JS caller passing anything else (say, the string `'false'`) fails at
|
|
129
|
+
* construction instead of being read as on or off.
|
|
130
|
+
*/
|
|
131
|
+
export function assertAddDeviceApproval(value: unknown): boolean {
|
|
132
|
+
if (value === undefined) {
|
|
133
|
+
return false
|
|
134
|
+
}
|
|
135
|
+
if (typeof value !== 'boolean') {
|
|
136
|
+
throw new Error('DAA: addDeviceApproval must be a boolean')
|
|
137
|
+
}
|
|
138
|
+
return value
|
|
139
|
+
}
|
|
140
|
+
|
|
105
141
|
/** SDK-owned trust anchor used to authenticate absolute pagination links. */
|
|
106
142
|
export function publicApiOriginFor(environment: DaaEnvironment): string {
|
|
107
143
|
return PUBLIC_API_ORIGINS[environment]
|
|
@@ -0,0 +1,100 @@
|
|
|
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 { contractViolation } from '../errors'
|
|
20
|
+
import { fromBase64Url } from './base64url'
|
|
21
|
+
import { sha256 } from './sha256'
|
|
22
|
+
|
|
23
|
+
const SEAL_DOMAIN = new TextEncoder().encode('daa-device-code-seal-v1')
|
|
24
|
+
const CODE_DOMAIN = new TextEncoder().encode('daa-device-approval-v1')
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The add-a-device matching code, shared with `DeviceApprovalMatchingCode` in
|
|
28
|
+
* daa-ios-sdk and its daa-android-sdk counterpart. The new device and the
|
|
29
|
+
* approving device must produce the same six digits from the same inputs; the
|
|
30
|
+
* tests pin the native SDKs' known-answer vector.
|
|
31
|
+
*
|
|
32
|
+
* All three inputs are 32 bytes: `thumbprint` is the new AuthKey's RFC 7638
|
|
33
|
+
* JWK thumbprint, `codeNonce` is the enclave's, and `codeSecret` is the new
|
|
34
|
+
* device's own random value.
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* `SHA-256("daa-device-code-seal-v1" || thumbprint || codeSecret)`: the
|
|
39
|
+
* commitment the new device signs into its registration as `codeSeal`, before
|
|
40
|
+
* the enclave picks `codeNonce`.
|
|
41
|
+
*/
|
|
42
|
+
export function codeSeal(thumbprint: Uint8Array, codeSecret: Uint8Array): Uint8Array {
|
|
43
|
+
requireBytes32(thumbprint, 'thumbprint')
|
|
44
|
+
requireBytes32(codeSecret, 'codeSecret')
|
|
45
|
+
return sha256(concat(SEAL_DOMAIN, thumbprint, codeSecret))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Six ASCII digits: the first four bytes of
|
|
50
|
+
* `SHA-256("daa-device-approval-v1" || thumbprint || codeNonce || codeSecret)`
|
|
51
|
+
* as a big-endian integer, modulo 1,000,000, zero-padded.
|
|
52
|
+
*/
|
|
53
|
+
export function matchingCode(
|
|
54
|
+
thumbprint: Uint8Array,
|
|
55
|
+
codeNonce: Uint8Array,
|
|
56
|
+
codeSecret: Uint8Array,
|
|
57
|
+
): string {
|
|
58
|
+
requireBytes32(thumbprint, 'thumbprint')
|
|
59
|
+
requireBytes32(codeNonce, 'codeNonce')
|
|
60
|
+
requireBytes32(codeSecret, 'codeSecret')
|
|
61
|
+
const digest = sha256(concat(CODE_DOMAIN, thumbprint, codeNonce, codeSecret))
|
|
62
|
+
const value = new DataView(digest.buffer, digest.byteOffset, 4).getUint32(0)
|
|
63
|
+
return String(value % 1_000_000).padStart(6, '0')
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The 32 bytes a 43-character unpadded Base64URL value encodes, or `null` when
|
|
68
|
+
* it is any other length, uses another alphabet, or is not the canonical
|
|
69
|
+
* encoding (non-zero trailing bits). For the wire and native forms of the
|
|
70
|
+
* thumbprint and `codeNonce`, so a malformed value fails as the caller's typed
|
|
71
|
+
* error rather than as `fromBase64Url`'s plain `Error`.
|
|
72
|
+
*/
|
|
73
|
+
export function canonicalBytes32(value: string): Uint8Array | null {
|
|
74
|
+
if (!BYTES32_BASE64URL.test(value)) {
|
|
75
|
+
return null
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
return fromBase64Url(value)
|
|
79
|
+
} catch {
|
|
80
|
+
return null
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const BYTES32_BASE64URL = /^[A-Za-z0-9_-]{43}$/
|
|
85
|
+
|
|
86
|
+
function requireBytes32(value: Uint8Array, name: string): void {
|
|
87
|
+
if (value.length !== 32) {
|
|
88
|
+
throw contractViolation(`matching code input ${name} must be 32 bytes`)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function concat(...parts: readonly Uint8Array[]): Uint8Array {
|
|
93
|
+
const out = new Uint8Array(parts.reduce((total, part) => total + part.length, 0))
|
|
94
|
+
let offset = 0
|
|
95
|
+
for (const part of parts) {
|
|
96
|
+
out.set(part, offset)
|
|
97
|
+
offset += part.length
|
|
98
|
+
}
|
|
99
|
+
return out
|
|
100
|
+
}
|