@circle-fin/daa-react-native-sdk 0.0.0-stage → 0.4.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 +63 -0
- package/LICENSE +176 -0
- package/README.md +217 -2
- package/android/build.gradle +97 -0
- package/android/src/main/AndroidManifest.xml +1 -0
- package/android/src/main/java/com/circle/daa/reactnative/DaaRegistrationExceptions.kt +47 -0
- package/android/src/main/java/com/circle/daa/reactnative/DaaRegistrationModule.kt +121 -0
- package/android/src/main/java/com/circle/daa/reactnative/DaaScaExceptions.kt +60 -0
- package/android/src/main/java/com/circle/daa/reactnative/DaaScaModule.kt +179 -0
- package/android/src/main/java/com/circle/daa/reactnative/DaaSecureKeyExceptions.kt +73 -0
- package/android/src/main/java/com/circle/daa/reactnative/DaaSecureKeyModule.kt +185 -0
- package/android/src/main/java/com/circle/daa/reactnative/InstallationIdentityStore.kt +73 -0
- package/android/src/main/java/com/circle/daa/reactnative/UnpaddedBase64Url.kt +45 -0
- package/build/DaaClient.d.ts +57 -0
- package/build/DaaClient.js +158 -0
- package/build/api/AccountLimitsClient.d.ts +79 -0
- package/build/api/AccountLimitsClient.js +142 -0
- package/build/api/AccountsClient.d.ts +95 -0
- package/build/api/AccountsClient.js +276 -0
- package/build/api/AchFiatAccountsClient.d.ts +92 -0
- package/build/api/AchFiatAccountsClient.js +142 -0
- package/build/api/AuthClient.d.ts +120 -0
- package/build/api/AuthClient.js +257 -0
- package/build/api/DaaUserClient.d.ts +124 -0
- package/build/api/DaaUserClient.js +245 -0
- package/build/api/DepositAddressesClient.d.ts +112 -0
- package/build/api/DepositAddressesClient.js +222 -0
- package/build/api/DepositsClient.d.ts +106 -0
- package/build/api/DepositsClient.js +183 -0
- package/build/api/FiatAccountsClient.d.ts +43 -0
- package/build/api/FiatAccountsClient.js +46 -0
- package/build/api/TransactionsClient.d.ts +64 -0
- package/build/api/TransactionsClient.js +120 -0
- package/build/api/TransfersClient.d.ts +104 -0
- package/build/api/TransfersClient.js +321 -0
- package/build/api/WireFiatAccountsClient.d.ts +99 -0
- package/build/api/WireFiatAccountsClient.js +156 -0
- package/build/api/WithdrawalsClient.d.ts +94 -0
- package/build/api/WithdrawalsClient.js +305 -0
- package/build/api/loginFailure.d.ts +43 -0
- package/build/api/loginFailure.js +64 -0
- package/build/api/pagination.d.ts +29 -0
- package/build/api/pagination.js +50 -0
- package/build/api/queryParameters.d.ts +23 -0
- package/build/api/queryParameters.js +77 -0
- package/build/api/resourceRequest.d.ts +60 -0
- package/build/api/resourceRequest.js +79 -0
- package/build/api/signedResource.d.ts +77 -0
- package/build/api/signedResource.js +168 -0
- package/build/auth/RegistrationFlowKey.d.ts +93 -0
- package/build/auth/RegistrationFlowKey.js +203 -0
- package/build/auth/daaRegistrationNative.d.ts +80 -0
- package/build/auth/daaRegistrationNative.js +184 -0
- package/build/auth/deviceAuthorization.d.ts +73 -0
- package/build/auth/deviceAuthorization.js +243 -0
- package/build/auth/ownerGrant.d.ts +29 -0
- package/build/auth/ownerGrant.js +90 -0
- package/build/auth/prepareReceipt.d.ts +150 -0
- package/build/auth/prepareReceipt.js +357 -0
- package/build/auth/registrationAuthorization.d.ts +48 -0
- package/build/auth/registrationAuthorization.js +243 -0
- package/build/auth/registrationCodeVerifier.d.ts +31 -0
- package/build/auth/registrationCodeVerifier.js +75 -0
- package/build/config.d.ts +79 -0
- package/build/config.js +116 -0
- package/build/crypto/base64url.d.ts +26 -0
- package/build/crypto/base64url.js +87 -0
- package/build/crypto/bytes.d.ts +27 -0
- package/build/crypto/bytes.js +32 -0
- package/build/crypto/es256Jws.d.ts +56 -0
- package/build/crypto/es256Jws.js +196 -0
- package/build/crypto/jwkThumbprint.d.ts +32 -0
- package/build/crypto/jwkThumbprint.js +49 -0
- package/build/crypto/random.d.ts +35 -0
- package/build/crypto/random.js +106 -0
- package/build/crypto/serialization.d.ts +26 -0
- package/build/crypto/serialization.js +43 -0
- package/build/crypto/sha256.d.ts +18 -0
- package/build/crypto/sha256.js +100 -0
- package/build/errors.d.ts +190 -0
- package/build/errors.js +252 -0
- package/build/index.d.ts +61 -0
- package/build/index.js +67 -0
- package/build/models/Account.d.ts +201 -0
- package/build/models/Account.js +147 -0
- package/build/models/AccountLimitsUsage.d.ts +169 -0
- package/build/models/AccountLimitsUsage.js +86 -0
- package/build/models/Ach.d.ts +135 -0
- package/build/models/Ach.js +184 -0
- package/build/models/AddressObject.d.ts +128 -0
- package/build/models/AddressObject.js +141 -0
- package/build/models/BusinessPii.d.ts +84 -0
- package/build/models/BusinessPii.js +142 -0
- package/build/models/CreateAccountRequest.d.ts +74 -0
- package/build/models/CreateAccountRequest.js +19 -0
- package/build/models/CreateTransferRequest.d.ts +91 -0
- package/build/models/CreateTransferRequest.js +19 -0
- package/build/models/CreateWithdrawalRequest.d.ts +99 -0
- package/build/models/CreateWithdrawalRequest.js +19 -0
- package/build/models/DaaUserContext.d.ts +96 -0
- package/build/models/DaaUserContext.js +133 -0
- package/build/models/Deposit.d.ts +241 -0
- package/build/models/Deposit.js +149 -0
- package/build/models/DepositSummary.d.ts +91 -0
- package/build/models/DepositSummary.js +114 -0
- package/build/models/FiatAccount.d.ts +99 -0
- package/build/models/FiatAccount.js +151 -0
- package/build/models/GenerateAddressRequest.d.ts +67 -0
- package/build/models/GenerateAddressRequest.js +68 -0
- package/build/models/Money.d.ts +76 -0
- package/build/models/Money.js +99 -0
- package/build/models/Page.d.ts +33 -0
- package/build/models/Page.js +57 -0
- package/build/models/PendingDeviceRegistration.d.ts +90 -0
- package/build/models/PendingDeviceRegistration.js +180 -0
- package/build/models/PreparedSocialLogin.d.ts +155 -0
- package/build/models/PreparedSocialLogin.js +235 -0
- package/build/models/Transaction.d.ts +187 -0
- package/build/models/Transaction.js +134 -0
- package/build/models/Transfer.d.ts +335 -0
- package/build/models/Transfer.js +240 -0
- package/build/models/Wire.d.ts +213 -0
- package/build/models/Wire.js +264 -0
- package/build/models/Withdrawal.d.ts +201 -0
- package/build/models/Withdrawal.js +156 -0
- package/build/models/redaction.d.ts +18 -0
- package/build/models/redaction.js +23 -0
- package/build/sca/ScaClient.d.ts +105 -0
- package/build/sca/ScaClient.js +193 -0
- package/build/securekey/InMemorySecureKey.d.ts +23 -0
- package/build/securekey/InMemorySecureKey.js +127 -0
- package/build/securekey/NativeSecureKey.d.ts +24 -0
- package/build/securekey/NativeSecureKey.js +178 -0
- package/build/securekey/NativeSecureKeyBridge.d.ts +83 -0
- package/build/securekey/NativeSecureKeyBridge.js +19 -0
- package/build/securekey/SecureKey.d.ts +137 -0
- package/build/securekey/SecureKey.js +19 -0
- package/build/securekey/alias.d.ts +88 -0
- package/build/securekey/alias.js +175 -0
- package/build/securekey/ownerAuthKeyResolver.d.ts +56 -0
- package/build/securekey/ownerAuthKeyResolver.js +63 -0
- package/build/securekey/resolveNativeBridge.d.ts +30 -0
- package/build/securekey/resolveNativeBridge.js +120 -0
- package/build/testing.d.ts +18 -0
- package/build/testing.js +34 -0
- package/build/transport/DaaTransport.d.ts +35 -0
- package/build/transport/DaaTransport.js +19 -0
- package/build/transport/headers.d.ts +30 -0
- package/build/transport/headers.js +45 -0
- package/build/validation/requestBody.d.ts +27 -0
- package/build/validation/requestBody.js +56 -0
- package/build/validation/uuid.d.ts +25 -0
- package/build/validation/uuid.js +33 -0
- package/build/wire/accountLimitsUsagePayload.d.ts +30 -0
- package/build/wire/accountLimitsUsagePayload.js +130 -0
- package/build/wire/accountPayload.d.ts +36 -0
- package/build/wire/accountPayload.js +100 -0
- package/build/wire/achPayload.d.ts +24 -0
- package/build/wire/achPayload.js +82 -0
- package/build/wire/addressObjectPayload.d.ts +44 -0
- package/build/wire/addressObjectPayload.js +64 -0
- package/build/wire/daaStatusCodes.d.ts +64 -0
- package/build/wire/daaStatusCodes.js +69 -0
- package/build/wire/dataEnvelope.d.ts +40 -0
- package/build/wire/dataEnvelope.js +79 -0
- package/build/wire/depositPayload.d.ts +42 -0
- package/build/wire/depositPayload.js +159 -0
- package/build/wire/deviceContextPayload.d.ts +33 -0
- package/build/wire/deviceContextPayload.js +57 -0
- package/build/wire/deviceRegistrationResultPayload.d.ts +32 -0
- package/build/wire/deviceRegistrationResultPayload.js +128 -0
- package/build/wire/errorEnvelope.d.ts +43 -0
- package/build/wire/errorEnvelope.js +85 -0
- package/build/wire/fiatAccountPayload.d.ts +37 -0
- package/build/wire/fiatAccountPayload.js +93 -0
- package/build/wire/instant.d.ts +27 -0
- package/build/wire/instant.js +91 -0
- package/build/wire/jsonNumber.d.ts +30 -0
- package/build/wire/jsonNumber.js +34 -0
- package/build/wire/moneyPayload.d.ts +30 -0
- package/build/wire/moneyPayload.js +56 -0
- package/build/wire/paginationLinks.d.ts +28 -0
- package/build/wire/paginationLinks.js +167 -0
- package/build/wire/payload.d.ts +60 -0
- package/build/wire/payload.js +121 -0
- package/build/wire/pendingRegistrationPayload.d.ts +32 -0
- package/build/wire/pendingRegistrationPayload.js +54 -0
- package/build/wire/socialLoginRequestPayload.d.ts +31 -0
- package/build/wire/socialLoginRequestPayload.js +72 -0
- package/build/wire/transactionPayload.d.ts +31 -0
- package/build/wire/transactionPayload.js +126 -0
- package/build/wire/transferPayload.d.ts +43 -0
- package/build/wire/transferPayload.js +218 -0
- package/build/wire/wireEnums.d.ts +22 -0
- package/build/wire/wireEnums.js +69 -0
- package/build/wire/wirePayload.d.ts +29 -0
- package/build/wire/wirePayload.js +138 -0
- package/build/wire/withdrawalPayload.d.ts +27 -0
- package/build/wire/withdrawalPayload.js +179 -0
- package/expo-module.config.json +14 -0
- package/ios/DaaRegistrationExceptions.swift +58 -0
- package/ios/DaaRegistrationModule.swift +143 -0
- package/ios/DaaScaExceptions.swift +96 -0
- package/ios/DaaScaModule.swift +125 -0
- package/ios/DaaSecureKeyExceptions.swift +77 -0
- package/ios/DaaSecureKeyModule.swift +162 -0
- package/ios/InstallationIdentityStore.swift +126 -0
- package/ios/UnpaddedBase64URL.swift +42 -0
- package/package.json +114 -4
- package/src/DaaClient.ts +171 -0
- package/src/api/AccountLimitsClient.ts +146 -0
- package/src/api/AccountsClient.ts +305 -0
- package/src/api/AchFiatAccountsClient.ts +147 -0
- package/src/api/AuthClient.ts +331 -0
- package/src/api/DaaUserClient.ts +270 -0
- package/src/api/DepositAddressesClient.ts +239 -0
- package/src/api/DepositsClient.ts +208 -0
- package/src/api/FiatAccountsClient.ts +50 -0
- package/src/api/TransactionsClient.ts +137 -0
- package/src/api/TransfersClient.ts +361 -0
- package/src/api/WireFiatAccountsClient.ts +172 -0
- package/src/api/WithdrawalsClient.ts +339 -0
- package/src/api/loginFailure.ts +75 -0
- package/src/api/pagination.ts +63 -0
- package/src/api/queryParameters.ts +82 -0
- package/src/api/resourceRequest.ts +135 -0
- package/src/api/signedResource.ts +267 -0
- package/src/auth/RegistrationFlowKey.ts +274 -0
- package/src/auth/daaRegistrationNative.ts +236 -0
- package/src/auth/deviceAuthorization.ts +302 -0
- package/src/auth/ownerGrant.ts +102 -0
- package/src/auth/prepareReceipt.ts +522 -0
- package/src/auth/registrationAuthorization.ts +306 -0
- package/src/auth/registrationCodeVerifier.ts +76 -0
- package/src/config.ts +156 -0
- package/src/crypto/base64url.ts +88 -0
- package/src/crypto/bytes.ts +30 -0
- package/src/crypto/es256Jws.ts +240 -0
- package/src/crypto/jwkThumbprint.ts +65 -0
- package/src/crypto/random.ts +113 -0
- package/src/crypto/serialization.ts +42 -0
- package/src/crypto/sha256.ts +105 -0
- package/src/errors.ts +319 -0
- package/src/index.ts +223 -0
- package/src/models/Account.ts +264 -0
- package/src/models/AccountLimitsUsage.ts +192 -0
- package/src/models/Ach.ts +269 -0
- package/src/models/AddressObject.ts +169 -0
- package/src/models/BusinessPii.ts +154 -0
- package/src/models/CreateAccountRequest.ts +109 -0
- package/src/models/CreateTransferRequest.ts +114 -0
- package/src/models/CreateWithdrawalRequest.ts +120 -0
- package/src/models/DaaUserContext.ts +172 -0
- package/src/models/Deposit.ts +307 -0
- package/src/models/DepositSummary.ts +142 -0
- package/src/models/FiatAccount.ts +195 -0
- package/src/models/GenerateAddressRequest.ts +101 -0
- package/src/models/Money.ts +91 -0
- package/src/models/Page.ts +48 -0
- package/src/models/PendingDeviceRegistration.ts +197 -0
- package/src/models/PreparedSocialLogin.ts +287 -0
- package/src/models/Transaction.ts +247 -0
- package/src/models/Transfer.ts +459 -0
- package/src/models/Wire.ts +405 -0
- package/src/models/Withdrawal.ts +283 -0
- package/src/models/redaction.ts +22 -0
- package/src/sca/ScaClient.ts +289 -0
- package/src/securekey/InMemorySecureKey.ts +138 -0
- package/src/securekey/NativeSecureKey.ts +179 -0
- package/src/securekey/NativeSecureKeyBridge.ts +109 -0
- package/src/securekey/SecureKey.ts +149 -0
- package/src/securekey/alias.ts +204 -0
- package/src/securekey/ownerAuthKeyResolver.ts +96 -0
- package/src/securekey/resolveNativeBridge.ts +185 -0
- package/src/testing.ts +32 -0
- package/src/transport/DaaTransport.ts +42 -0
- package/src/transport/headers.ts +46 -0
- package/src/validation/requestBody.ts +60 -0
- package/src/validation/uuid.ts +31 -0
- package/src/wire/accountLimitsUsagePayload.ts +182 -0
- package/src/wire/accountPayload.ts +108 -0
- package/src/wire/achPayload.ts +98 -0
- package/src/wire/addressObjectPayload.ts +64 -0
- package/src/wire/daaStatusCodes.ts +69 -0
- package/src/wire/dataEnvelope.ts +80 -0
- package/src/wire/depositPayload.ts +186 -0
- package/src/wire/deviceContextPayload.ts +58 -0
- package/src/wire/deviceRegistrationResultPayload.ts +153 -0
- package/src/wire/errorEnvelope.ts +87 -0
- package/src/wire/fiatAccountPayload.ts +112 -0
- package/src/wire/instant.ts +93 -0
- package/src/wire/jsonNumber.ts +33 -0
- package/src/wire/moneyPayload.ts +55 -0
- package/src/wire/paginationLinks.ts +204 -0
- package/src/wire/payload.ts +146 -0
- package/src/wire/pendingRegistrationPayload.ts +75 -0
- package/src/wire/socialLoginRequestPayload.ts +89 -0
- package/src/wire/transactionPayload.ts +145 -0
- package/src/wire/transferPayload.ts +249 -0
- package/src/wire/wireEnums.ts +74 -0
- package/src/wire/wirePayload.ts +160 -0
- package/src/wire/withdrawalPayload.ts +219 -0
- package/testing.d.ts +19 -0
- package/testing.js +25 -0
|
@@ -0,0 +1,236 @@
|
|
|
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 { requireOptionalNativeModule } from 'expo-modules-core'
|
|
20
|
+
import { fromBase64Url, toBase64Url } from '../crypto/base64url'
|
|
21
|
+
import { contractViolation, isDaaError } from '../errors'
|
|
22
|
+
|
|
23
|
+
// JS shape of the Expo `DaaRegistration` native module. Separate from
|
|
24
|
+
// `DaaSecureKey`: this module bridges DaaSecureRandom, DaaRegistrationFlowKey,
|
|
25
|
+
// and platform verification of the Prepare receipt signature. The flow-key
|
|
26
|
+
// private key stays in the native cache keyed by the handle id; only Base64URL
|
|
27
|
+
// strings cross the bridge.
|
|
28
|
+
|
|
29
|
+
export const DAA_REGISTRATION_MODULE = 'DaaRegistration'
|
|
30
|
+
|
|
31
|
+
export interface DaaRegistrationNativeModule {
|
|
32
|
+
randomBytes(count: number): string
|
|
33
|
+
randomUuidV4(): string
|
|
34
|
+
generateRegistrationFlowKey(): string
|
|
35
|
+
registrationFlowPublicKeySpkiBase64Url(id: string): string
|
|
36
|
+
releaseRegistrationFlowKey(id: string): void
|
|
37
|
+
/**
|
|
38
|
+
* Typed Complete-only sign — not a generic `sign(bytes)`. `signingInputBase64Url`
|
|
39
|
+
* is the exact Device Registration Complete flow-JWS signing input; the native
|
|
40
|
+
* module validates those bytes and signs that same slice with the cached flow
|
|
41
|
+
* key, returning unpadded Base64URL of the 64-byte JOSE `R || S` signature.
|
|
42
|
+
*/
|
|
43
|
+
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
|
+
}
|
|
58
|
+
|
|
59
|
+
export function optionalDaaRegistrationNative(): DaaRegistrationNativeModule | null {
|
|
60
|
+
const native = requireOptionalNativeModule<DaaRegistrationNativeModule>(DAA_REGISTRATION_MODULE)
|
|
61
|
+
if (native == null) {
|
|
62
|
+
return null
|
|
63
|
+
}
|
|
64
|
+
return native
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function requireDaaRegistrationNative(): DaaRegistrationNativeModule {
|
|
68
|
+
const native = optionalDaaRegistrationNative()
|
|
69
|
+
if (native === null) {
|
|
70
|
+
throw contractViolation(
|
|
71
|
+
'native DaaRegistration module is not available to mint or verify login-attempt material; ' +
|
|
72
|
+
'include the DaaRegistration Expo module in the host app',
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
return native
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function nativeCall<T>(op: () => T): T {
|
|
79
|
+
try {
|
|
80
|
+
return op()
|
|
81
|
+
} catch (err) {
|
|
82
|
+
if (isDaaError(err)) {
|
|
83
|
+
throw err
|
|
84
|
+
}
|
|
85
|
+
throw contractViolation('native DaaRegistration operation failed')
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Verifier bytes from native `DaaSecureRandom`, decoded at the seam. */
|
|
90
|
+
export function nativeRandomBytes(native: DaaRegistrationNativeModule, count: number): Uint8Array {
|
|
91
|
+
if (typeof native.randomBytes !== 'function') {
|
|
92
|
+
throw contractViolation('native DaaRegistration.randomBytes is required')
|
|
93
|
+
}
|
|
94
|
+
const encoded = nativeCall(() => native.randomBytes(count))
|
|
95
|
+
if (typeof encoded !== 'string') {
|
|
96
|
+
throw contractViolation('native DaaSecureRandom did not return Base64URL')
|
|
97
|
+
}
|
|
98
|
+
let bytes: Uint8Array
|
|
99
|
+
try {
|
|
100
|
+
bytes = fromBase64Url(encoded)
|
|
101
|
+
} catch {
|
|
102
|
+
throw contractViolation('native DaaSecureRandom did not return valid Base64URL')
|
|
103
|
+
}
|
|
104
|
+
if (bytes.length !== count) {
|
|
105
|
+
throw contractViolation(
|
|
106
|
+
`native DaaSecureRandom must return ${count} bytes; got ${bytes.length}`,
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
return bytes
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function nativeRandomUuidV4(native: DaaRegistrationNativeModule): string {
|
|
113
|
+
if (typeof native.randomUuidV4 !== 'function') {
|
|
114
|
+
throw contractViolation('native DaaRegistration.randomUuidV4 is required')
|
|
115
|
+
}
|
|
116
|
+
const uuid = nativeCall(() => native.randomUuidV4())
|
|
117
|
+
if (typeof uuid !== 'string') {
|
|
118
|
+
throw contractViolation('native randomUuidV4 did not return a string')
|
|
119
|
+
}
|
|
120
|
+
return uuid
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function nativeGenerateRegistrationFlowKey(native: DaaRegistrationNativeModule): string {
|
|
124
|
+
if (typeof native.generateRegistrationFlowKey !== 'function') {
|
|
125
|
+
throw contractViolation('native DaaRegistration.generateRegistrationFlowKey is required')
|
|
126
|
+
}
|
|
127
|
+
const id = nativeCall(() => native.generateRegistrationFlowKey())
|
|
128
|
+
if (typeof id !== 'string' || id.length === 0) {
|
|
129
|
+
throw contractViolation('native generateRegistrationFlowKey did not return a handle id')
|
|
130
|
+
}
|
|
131
|
+
return id
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function nativeRegistrationFlowPublicKeySpkiBase64Url(
|
|
135
|
+
native: DaaRegistrationNativeModule,
|
|
136
|
+
id: string,
|
|
137
|
+
): string {
|
|
138
|
+
if (typeof native.registrationFlowPublicKeySpkiBase64Url !== 'function') {
|
|
139
|
+
throw contractViolation(
|
|
140
|
+
'native DaaRegistration.registrationFlowPublicKeySpkiBase64Url is required',
|
|
141
|
+
)
|
|
142
|
+
}
|
|
143
|
+
const spki = nativeCall(() => native.registrationFlowPublicKeySpkiBase64Url(id))
|
|
144
|
+
if (typeof spki !== 'string') {
|
|
145
|
+
throw contractViolation('native registration-flow public key did not return a string')
|
|
146
|
+
}
|
|
147
|
+
return spki
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Sign the exact Device Registration Complete flow-JWS signing input with the
|
|
152
|
+
* cached native flow key. Returns unpadded Base64URL of the 64-byte JOSE
|
|
153
|
+
* `R || S` signature. Not a generic sign: `id` names a cached
|
|
154
|
+
* `DaaRegistrationFlowKey`, and native validates the bytes as a registration
|
|
155
|
+
* authorization signing input before it ever touches the private key.
|
|
156
|
+
*/
|
|
157
|
+
export function nativeSignRegistrationAuthorization(
|
|
158
|
+
native: DaaRegistrationNativeModule,
|
|
159
|
+
id: string,
|
|
160
|
+
signingInputBase64Url: string,
|
|
161
|
+
): Uint8Array {
|
|
162
|
+
if (typeof native.signRegistrationAuthorization !== 'function') {
|
|
163
|
+
throw contractViolation('native DaaRegistration.signRegistrationAuthorization is required')
|
|
164
|
+
}
|
|
165
|
+
const signature = nativeCall(() =>
|
|
166
|
+
native.signRegistrationAuthorization(id, signingInputBase64Url),
|
|
167
|
+
)
|
|
168
|
+
if (typeof signature !== 'string') {
|
|
169
|
+
throw contractViolation('native registration-flow signature did not return a string')
|
|
170
|
+
}
|
|
171
|
+
let bytes: Uint8Array
|
|
172
|
+
try {
|
|
173
|
+
bytes = fromBase64Url(signature)
|
|
174
|
+
} catch {
|
|
175
|
+
throw contractViolation('native registration-flow signature is not valid Base64URL')
|
|
176
|
+
}
|
|
177
|
+
if (bytes.length !== 64) {
|
|
178
|
+
throw contractViolation(
|
|
179
|
+
`native registration-flow signature must be 64 bytes (JOSE R||S); got ${bytes.length}`,
|
|
180
|
+
)
|
|
181
|
+
}
|
|
182
|
+
return bytes
|
|
183
|
+
}
|
|
184
|
+
|
|
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
|
+
/**
|
|
217
|
+
* Drop a cached native flow key.
|
|
218
|
+
*
|
|
219
|
+
* Cleanup, so it is forgiving: an older host app whose module predates this
|
|
220
|
+
* function, or an id the native side no longer holds, is not an error. The
|
|
221
|
+
* alternative would make a `finally` block throw over tidying up and mask the
|
|
222
|
+
* failure that sent it there. What it must not do is fail silently in a way
|
|
223
|
+
* that leaves key material resident — hence the explicit function check rather
|
|
224
|
+
* than an optional-call shrug.
|
|
225
|
+
*/
|
|
226
|
+
export function nativeReleaseRegistrationFlowKey(
|
|
227
|
+
native: DaaRegistrationNativeModule,
|
|
228
|
+
id: string,
|
|
229
|
+
): void {
|
|
230
|
+
if (typeof native.releaseRegistrationFlowKey !== 'function') {
|
|
231
|
+
return
|
|
232
|
+
}
|
|
233
|
+
nativeCall(() => {
|
|
234
|
+
native.releaseRegistrationFlowKey(id)
|
|
235
|
+
})
|
|
236
|
+
}
|
|
@@ -0,0 +1,302 @@
|
|
|
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
|
+
// Registered-device authorization: a compact ES256 JWS in
|
|
20
|
+
// `X-Device-Authorization`, bound to the exact body bytes that will be sent.
|
|
21
|
+
//
|
|
22
|
+
// This is the post-activation header (`typ` in DEVICE_AUTHORIZATION_TYP).
|
|
23
|
+
// The SDK signs Device Registration Complete with its own two-JWS profile
|
|
24
|
+
// (`auth/registrationAuthorization.ts`), with its own `typ` values and header
|
|
25
|
+
// set. This module also maps the registry's `register_device_key` operation
|
|
26
|
+
// and accepts a not-yet-registered (`created`) AuthKey for that operation only;
|
|
27
|
+
// no SDK client signs it through this module. Not a public generic
|
|
28
|
+
// `sign(bytes)`.
|
|
29
|
+
// The only bytes the AuthKey signs are this JWS signing input.
|
|
30
|
+
//
|
|
31
|
+
// Operation identity is the digest-pinned Owner-authorization registry:
|
|
32
|
+
// `discriminator` + `schema_version` are the lookup key. The verifier resolves
|
|
33
|
+
// method and `canonical_path` from that entry; it does not infer the operation
|
|
34
|
+
// from the path. Callers name one of the six closed operations — they cannot
|
|
35
|
+
// pass a free-form `/v1` path.
|
|
36
|
+
//
|
|
37
|
+
// The header carries the published `typ` (`aw-device-authorization+jws`).
|
|
38
|
+
//
|
|
39
|
+
// Production `OwnerAuthKey.signExactBytes` reaches native
|
|
40
|
+
// `signDeviceAuthorization` via the Expo bridge. Tests use
|
|
41
|
+
// `InMemorySecureKeyStore`. The create-account, transfer, and withdrawal
|
|
42
|
+
// clients reach this through `api/signedResource.ts` rather than calling it
|
|
43
|
+
// directly.
|
|
44
|
+
|
|
45
|
+
import { toBase64Url } from '../crypto/base64url'
|
|
46
|
+
import { randomUuidV4 } from '../crypto/random'
|
|
47
|
+
import { sha256 } from '../crypto/sha256'
|
|
48
|
+
import { contractViolation, mapNativeError } from '../errors'
|
|
49
|
+
import type { OwnerAuthKey } from '../securekey/SecureKey'
|
|
50
|
+
import { isUuid } from '../validation/uuid'
|
|
51
|
+
|
|
52
|
+
export const DEVICE_AUTHORIZATION_HEADER = 'X-Device-Authorization'
|
|
53
|
+
|
|
54
|
+
const DEVICE_AUTHORIZATION_TYP = 'aw-device-authorization+jws'
|
|
55
|
+
const DEVICE_AUTHORIZATION_AUD = 'circle-digital-asset-accounts'
|
|
56
|
+
const JOSE_SIGNATURE_LENGTH = 64
|
|
57
|
+
/** Verifier ceiling: `exp - iat` must be at most 300 seconds. */
|
|
58
|
+
const AUTHORIZATION_LIFETIME_SECONDS = 300
|
|
59
|
+
const SCHEMA_VERSION = 1
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Closed Owner-authorization registry (v1). Wire `discriminator` values from
|
|
63
|
+
* the registry schema.
|
|
64
|
+
*/
|
|
65
|
+
export type DeviceAuthorizationOperation =
|
|
66
|
+
| 'create_account'
|
|
67
|
+
| 'transfer'
|
|
68
|
+
| 'withdrawal'
|
|
69
|
+
| 'complete_social_login'
|
|
70
|
+
| 'get_device_context'
|
|
71
|
+
| 'register_device_key'
|
|
72
|
+
|
|
73
|
+
export type DeviceAuthorizationProfile = Readonly<{
|
|
74
|
+
discriminator: DeviceAuthorizationOperation
|
|
75
|
+
schemaVersion: typeof SCHEMA_VERSION
|
|
76
|
+
method: 'GET' | 'POST'
|
|
77
|
+
canonicalPath: string
|
|
78
|
+
}>
|
|
79
|
+
|
|
80
|
+
export type DeviceAuthorizationRequest = Readonly<{
|
|
81
|
+
authKey: OwnerAuthKey
|
|
82
|
+
/** Proposed `AuthKey` id at Complete, or `DeviceContext.authKeyId` after ACTIVE. */
|
|
83
|
+
authKeyId: string
|
|
84
|
+
operation: DeviceAuthorizationOperation
|
|
85
|
+
/** Exact bytes that will be hashed and sent. Empty array for a bodyless call. */
|
|
86
|
+
bodyBytes: Uint8Array
|
|
87
|
+
}>
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Test-only clock. Production code always uses the system clock. This
|
|
91
|
+
* module is not on the public `.` entry, and `postSignedResource` does not
|
|
92
|
+
* take a clock. `authorizeExactRequest` accepts one so tests can pin `iat`/`jti`.
|
|
93
|
+
*/
|
|
94
|
+
export type DeviceAuthorizationClock = Readonly<{
|
|
95
|
+
nowEpochSeconds: () => number
|
|
96
|
+
jti: () => string
|
|
97
|
+
}>
|
|
98
|
+
|
|
99
|
+
const DEFAULT_CLOCK: DeviceAuthorizationClock = {
|
|
100
|
+
nowEpochSeconds: () => Math.floor(Date.now() / 1000),
|
|
101
|
+
jti: randomUuidV4,
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Registry profile for one closed operation. Method and path are not caller-chosen. */
|
|
105
|
+
export function deviceAuthorizationProfile(
|
|
106
|
+
operation: DeviceAuthorizationOperation,
|
|
107
|
+
): DeviceAuthorizationProfile {
|
|
108
|
+
switch (operation) {
|
|
109
|
+
case 'create_account':
|
|
110
|
+
return {
|
|
111
|
+
discriminator: 'create_account',
|
|
112
|
+
schemaVersion: SCHEMA_VERSION,
|
|
113
|
+
method: 'POST',
|
|
114
|
+
canonicalPath: '/v1/accounts',
|
|
115
|
+
}
|
|
116
|
+
case 'transfer':
|
|
117
|
+
return {
|
|
118
|
+
discriminator: 'transfer',
|
|
119
|
+
schemaVersion: SCHEMA_VERSION,
|
|
120
|
+
method: 'POST',
|
|
121
|
+
canonicalPath: '/v1/accounts/transfers',
|
|
122
|
+
}
|
|
123
|
+
case 'withdrawal':
|
|
124
|
+
return {
|
|
125
|
+
discriminator: 'withdrawal',
|
|
126
|
+
schemaVersion: SCHEMA_VERSION,
|
|
127
|
+
method: 'POST',
|
|
128
|
+
canonicalPath: '/v1/accounts/withdrawals',
|
|
129
|
+
}
|
|
130
|
+
case 'complete_social_login':
|
|
131
|
+
return {
|
|
132
|
+
discriminator: 'complete_social_login',
|
|
133
|
+
schemaVersion: SCHEMA_VERSION,
|
|
134
|
+
method: 'POST',
|
|
135
|
+
canonicalPath: '/v1/accounts/auth/sessions',
|
|
136
|
+
}
|
|
137
|
+
case 'get_device_context':
|
|
138
|
+
return {
|
|
139
|
+
discriminator: 'get_device_context',
|
|
140
|
+
schemaVersion: SCHEMA_VERSION,
|
|
141
|
+
method: 'GET',
|
|
142
|
+
canonicalPath: '/v1/accounts/auth/context',
|
|
143
|
+
}
|
|
144
|
+
case 'register_device_key':
|
|
145
|
+
return {
|
|
146
|
+
discriminator: 'register_device_key',
|
|
147
|
+
schemaVersion: SCHEMA_VERSION,
|
|
148
|
+
method: 'POST',
|
|
149
|
+
canonicalPath: '/v1/accounts/auth/deviceKeyRegistration',
|
|
150
|
+
}
|
|
151
|
+
default: {
|
|
152
|
+
const exhaustive: never = operation
|
|
153
|
+
void exhaustive
|
|
154
|
+
throw contractViolation('device authorization operation is not in the closed registry')
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Build a compact ES256 JWS over the exact request bytes.
|
|
161
|
+
*
|
|
162
|
+
* Serializes the protected header and payload with `JSON.stringify` (DAA
|
|
163
|
+
* verifies the transmitted signing input and parsed claim semantics, not a
|
|
164
|
+
* reconstructed JSON encoding). Signs the compact signing input
|
|
165
|
+
* `base64url(header) + "." + base64url(payload)` and appends a 64-byte JOSE
|
|
166
|
+
* `R || S` signature.
|
|
167
|
+
*/
|
|
168
|
+
export async function authorizeExactRequest(
|
|
169
|
+
request: DeviceAuthorizationRequest,
|
|
170
|
+
clock: DeviceAuthorizationClock = DEFAULT_CLOCK,
|
|
171
|
+
): Promise<string> {
|
|
172
|
+
// `register_device_key` accepts a `created` AuthKey. Other operations
|
|
173
|
+
// require a registered key. `typ` lives in DEVICE_AUTHORIZATION_TYP, not at
|
|
174
|
+
// each call site.
|
|
175
|
+
if (request.operation !== 'register_device_key' && request.authKey.resolution() !== 'existing') {
|
|
176
|
+
throw contractViolation('X-Device-Authorization requires a registered AuthKey')
|
|
177
|
+
}
|
|
178
|
+
if (!isUuid(request.authKeyId)) {
|
|
179
|
+
throw contractViolation('authKeyId must be a UUID')
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const profile = deviceAuthorizationProfile(request.operation)
|
|
183
|
+
const idempotencyKey = idempotencyKeyFromExactBody(request.bodyBytes)
|
|
184
|
+
const iat = requireEpochSeconds(clock.nowEpochSeconds())
|
|
185
|
+
const jti = clock.jti()
|
|
186
|
+
if (!isUuid(jti)) {
|
|
187
|
+
throw contractViolation('jti must be a UUID')
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const protectedHeader = {
|
|
191
|
+
alg: 'ES256',
|
|
192
|
+
kid: request.authKeyId,
|
|
193
|
+
typ: DEVICE_AUTHORIZATION_TYP,
|
|
194
|
+
}
|
|
195
|
+
const payload = payloadClaims({
|
|
196
|
+
bodyBytes: request.bodyBytes,
|
|
197
|
+
profile,
|
|
198
|
+
idempotencyKey,
|
|
199
|
+
jti,
|
|
200
|
+
iat,
|
|
201
|
+
})
|
|
202
|
+
|
|
203
|
+
const signingInput = toBase64Url(utf8Json(protectedHeader)) + '.' + toBase64Url(utf8Json(payload))
|
|
204
|
+
const signature = await signSigningInput(request.authKey, signingInput)
|
|
205
|
+
return signingInput + '.' + toBase64Url(signature)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function payloadClaims(args: {
|
|
209
|
+
bodyBytes: Uint8Array
|
|
210
|
+
profile: DeviceAuthorizationProfile
|
|
211
|
+
idempotencyKey: string | undefined
|
|
212
|
+
jti: string
|
|
213
|
+
iat: number
|
|
214
|
+
}): object {
|
|
215
|
+
const bodySha256 = toBase64Url(sha256(args.bodyBytes))
|
|
216
|
+
const exp = args.iat + AUTHORIZATION_LIFETIME_SECONDS
|
|
217
|
+
if (args.idempotencyKey === undefined) {
|
|
218
|
+
return {
|
|
219
|
+
body_sha256: bodySha256,
|
|
220
|
+
canonical_path: args.profile.canonicalPath,
|
|
221
|
+
method: args.profile.method,
|
|
222
|
+
aud: DEVICE_AUTHORIZATION_AUD,
|
|
223
|
+
discriminator: args.profile.discriminator,
|
|
224
|
+
schema_version: args.profile.schemaVersion,
|
|
225
|
+
jti: args.jti,
|
|
226
|
+
iat: args.iat,
|
|
227
|
+
exp,
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return {
|
|
231
|
+
body_sha256: bodySha256,
|
|
232
|
+
idempotency_key: args.idempotencyKey,
|
|
233
|
+
canonical_path: args.profile.canonicalPath,
|
|
234
|
+
method: args.profile.method,
|
|
235
|
+
aud: DEVICE_AUTHORIZATION_AUD,
|
|
236
|
+
discriminator: args.profile.discriminator,
|
|
237
|
+
schema_version: args.profile.schemaVersion,
|
|
238
|
+
jti: args.jti,
|
|
239
|
+
iat: args.iat,
|
|
240
|
+
exp,
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async function signSigningInput(authKey: OwnerAuthKey, signingInput: string): Promise<Uint8Array> {
|
|
245
|
+
let signature: Uint8Array
|
|
246
|
+
try {
|
|
247
|
+
signature = await authKey.signExactBytes(new TextEncoder().encode(signingInput))
|
|
248
|
+
} catch (err) {
|
|
249
|
+
throw mapNativeError(err)
|
|
250
|
+
}
|
|
251
|
+
if (signature.length !== JOSE_SIGNATURE_LENGTH) {
|
|
252
|
+
throw contractViolation('device authorization signature must be 64 bytes')
|
|
253
|
+
}
|
|
254
|
+
return signature
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function requireEpochSeconds(value: number): number {
|
|
258
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
259
|
+
throw contractViolation('device authorization clock must yield a positive Unix timestamp')
|
|
260
|
+
}
|
|
261
|
+
return value
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Read `idempotencyKey` from the already-serialized body. Parsing is observation
|
|
266
|
+
* only — the hashed and sent bytes are unchanged.
|
|
267
|
+
*
|
|
268
|
+
* Include `idempotency_key` iff the JSON body contains `idempotencyKey`,
|
|
269
|
+
* and omit it for a bodyless request.
|
|
270
|
+
*
|
|
271
|
+
* Exported so `api/signedResource.ts` can recover the same key when mapping a
|
|
272
|
+
* `409` `IDEMPOTENCY_KEY_REUSED` failure, rather than parsing the body a
|
|
273
|
+
* second, independent way.
|
|
274
|
+
*/
|
|
275
|
+
export function idempotencyKeyFromExactBody(bodyBytes: Uint8Array): string | undefined {
|
|
276
|
+
if (bodyBytes.length === 0) {
|
|
277
|
+
return undefined
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
let parsed: unknown
|
|
281
|
+
try {
|
|
282
|
+
parsed = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bodyBytes))
|
|
283
|
+
} catch {
|
|
284
|
+
throw contractViolation('signed request body is not UTF-8 JSON')
|
|
285
|
+
}
|
|
286
|
+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
|
|
287
|
+
throw contractViolation('signed request body must be a JSON object')
|
|
288
|
+
}
|
|
289
|
+
if (!Object.prototype.hasOwnProperty.call(parsed, 'idempotencyKey')) {
|
|
290
|
+
return undefined
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const value = (parsed as Record<string, unknown>).idempotencyKey
|
|
294
|
+
if (typeof value !== 'string' || !isUuid(value)) {
|
|
295
|
+
throw contractViolation('idempotencyKey must be a UUID when present on a signed body')
|
|
296
|
+
}
|
|
297
|
+
return value
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function utf8Json(value: object): Uint8Array {
|
|
301
|
+
return new TextEncoder().encode(JSON.stringify(value))
|
|
302
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
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
|
+
// Owner-grant claim check. The public Social Session contract does
|
|
20
|
+
// not return an Owner grant — Circle keeps that assertion internal. If a
|
|
21
|
+
// response nevertheless carries one, the SDK must not invent a grant shape and
|
|
22
|
+
// must not proceed without the flow-key binding Complete's dual headers rely on.
|
|
23
|
+
//
|
|
24
|
+
// Cryptographic verification of a grant is not done here. This
|
|
25
|
+
// only reads the compact-JWT payload and requires
|
|
26
|
+
// `registrationFlowKeyThumbprint`. The grant is never logged, stored, or
|
|
27
|
+
// forwarded to Device Registration Complete.
|
|
28
|
+
|
|
29
|
+
import { fromBase64Url } from '../crypto/base64url'
|
|
30
|
+
import { contractViolation } from '../errors'
|
|
31
|
+
|
|
32
|
+
/** RFC 7638 P-256 `jkt` is an unpadded Base64URL SHA-256 digest: 43 characters. */
|
|
33
|
+
const JWK_THUMBPRINT = /^[A-Za-z0-9_-]{43}$/
|
|
34
|
+
|
|
35
|
+
function isJsonObject(value: unknown): value is Record<string, unknown> {
|
|
36
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Require a compact Owner grant to carry `registrationFlowKeyThumbprint`.
|
|
41
|
+
*
|
|
42
|
+
* `correlationId` is the `X-Request-Id` of the response that carried the grant,
|
|
43
|
+
* so the failure can be traced server-side like any other error built from a
|
|
44
|
+
* response.
|
|
45
|
+
*
|
|
46
|
+
* @throws DaaError `contract_violation` when the grant is missing, unreadable,
|
|
47
|
+
* missing the thumbprint, or bound to a different flow key. The grant itself
|
|
48
|
+
* is never interpolated into the error.
|
|
49
|
+
*/
|
|
50
|
+
export function requireOwnerGrantFlowKeyThumbprint(
|
|
51
|
+
ownerGrant: string,
|
|
52
|
+
expectedThumbprint: string,
|
|
53
|
+
correlationId: string | null,
|
|
54
|
+
): void {
|
|
55
|
+
const claims = compactJwtPayload(ownerGrant, correlationId)
|
|
56
|
+
const thumbprint = claims.registrationFlowKeyThumbprint
|
|
57
|
+
if (typeof thumbprint !== 'string' || !JWK_THUMBPRINT.test(thumbprint)) {
|
|
58
|
+
throw contractViolation('owner grant is missing registrationFlowKeyThumbprint', correlationId)
|
|
59
|
+
}
|
|
60
|
+
if (thumbprint !== expectedThumbprint) {
|
|
61
|
+
throw contractViolation(
|
|
62
|
+
'owner grant registrationFlowKeyThumbprint does not match the registration-flow key',
|
|
63
|
+
correlationId,
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function compactJwtPayload(compact: string, correlationId: string | null): Record<string, unknown> {
|
|
69
|
+
const parts = compact.split('.')
|
|
70
|
+
const header = parts[0]
|
|
71
|
+
const payload = parts[1]
|
|
72
|
+
const signature = parts[2]
|
|
73
|
+
if (
|
|
74
|
+
parts.length !== 3 ||
|
|
75
|
+
header === undefined ||
|
|
76
|
+
payload === undefined ||
|
|
77
|
+
signature === undefined ||
|
|
78
|
+
header.length === 0 ||
|
|
79
|
+
payload.length === 0 ||
|
|
80
|
+
signature.length === 0
|
|
81
|
+
) {
|
|
82
|
+
throw contractViolation('owner grant must be a compact JWT', correlationId)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let bytes: Uint8Array
|
|
86
|
+
try {
|
|
87
|
+
bytes = fromBase64Url(payload)
|
|
88
|
+
} catch {
|
|
89
|
+
throw contractViolation('owner grant payload is not valid Base64URL', correlationId)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let parsed: unknown
|
|
93
|
+
try {
|
|
94
|
+
parsed = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes))
|
|
95
|
+
} catch {
|
|
96
|
+
throw contractViolation('owner grant payload is not JSON', correlationId)
|
|
97
|
+
}
|
|
98
|
+
if (!isJsonObject(parsed)) {
|
|
99
|
+
throw contractViolation('owner grant payload is not a JSON object', correlationId)
|
|
100
|
+
}
|
|
101
|
+
return parsed
|
|
102
|
+
}
|