@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
@@ -1,150 +0,0 @@
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
- import type { DaaEnvironment } from '../config';
19
- import type { P256PublicJwk } from '../crypto/jwkThumbprint';
20
- /** Contract bounds on `SocialLoginRequest.prepareReceiptJws`. */
21
- export declare const PREPARE_RECEIPT_MIN_LENGTH = 64;
22
- export declare const PREPARE_RECEIPT_MAX_LENGTH = 8192;
23
- /** The JWS `typ` DAA signs the receipt with. */
24
- export declare const PREPARE_RECEIPT_TYP = "daa-prepare-receipt+jws";
25
- /** Widest clock skew an anchor may allow, so a configuration typo cannot disable expiry. */
26
- export declare const PREPARE_RECEIPT_MAX_CLOCK_SKEW_SECONDS = 300;
27
- /** One pinned receipt-signing key, addressed by the `kid` DAA puts in the header. */
28
- export type PrepareReceiptVerificationKey = Readonly<{
29
- kid: string;
30
- jwk: P256PublicJwk;
31
- }>;
32
- /**
33
- * Everything a trust anchor pins, in one value.
34
- *
35
- * `verificationKeys` is pinned by the build and never derived from a receipt.
36
- * More than one entry allows a rotation overlap; the receipt's `kid` selects
37
- * among them and nothing else.
38
- */
39
- export type PrepareReceiptTrustAnchor = Readonly<{
40
- /** Exact expected `iss`. */
41
- issuer: string;
42
- /** Exact expected `aud`, or one member of an `aud` array. */
43
- audience: string;
44
- verificationKeys: readonly PrepareReceiptVerificationKey[];
45
- /** Allowance on `exp` / `iat` / `nbf`, in seconds. */
46
- maxClockSkewSeconds: number;
47
- }>;
48
- /**
49
- * The build-time pinned anchor for an environment. **`null` for every
50
- * environment in this release.**
51
- *
52
- * A function rather than a `const`, so that its declared return type does not
53
- * narrow the rest of this module to unreachable code.
54
- */
55
- export declare function pinnedPrepareReceiptTrustAnchor(environment: DaaEnvironment): PrepareReceiptTrustAnchor | null;
56
- /**
57
- * The values this attempt's receipt must name. The first two come from the
58
- * Prepare response; the rest are the SDK's own.
59
- */
60
- export type PrepareReceiptExpectation = Readonly<{
61
- loginRequestId: string;
62
- /** The Prepare response's `oidcNonce`; the receipt's `nonce` claim. */
63
- nonce: string;
64
- provider: string;
65
- environment: DaaEnvironment;
66
- registrationCodeChallenge: string;
67
- registrationFlowKeyThumbprint: string;
68
- }>;
69
- /**
70
- * Test-only clock, following `auth/deviceAuthorization.ts`: production code
71
- * always uses the system clock, and no production caller passes one.
72
- */
73
- export type PrepareReceiptClock = Readonly<{
74
- nowEpochSeconds: () => number;
75
- }>;
76
- /** What a verified receipt attested. Only ever returned after every check passed. */
77
- export type VerifiedPrepareReceipt = Readonly<{
78
- /** The receipt exactly as received. */
79
- compactJws: string;
80
- loginRequestId: string;
81
- nonce: string;
82
- expiresAtEpochSeconds: number;
83
- /** Signed but not compared on the login path; see {@link requirePrepareReceiptTenantScope}. */
84
- tenantScope: Readonly<{
85
- customerAppId: unknown;
86
- parentEntityIdDigest: unknown;
87
- }>;
88
- }>;
89
- /**
90
- * Whether a value is shaped like the compact JWS the contract describes and sits
91
- * within its declared length bounds. Structural only: shape is not trust.
92
- */
93
- export declare function isPrepareReceiptShape(value: string): boolean;
94
- /**
95
- * What login does with the receipt, given the anchor for this environment.
96
- *
97
- * | Anchor | Receipt | Result |
98
- * | ------ | ------- | ----------------------------------------------------------- |
99
- * | unset | absent | `null`: nothing to verify, login proceeds |
100
- * | unset | present | `null`: the receipt is ignored, login proceeds as if absent |
101
- * | set | absent | `prepare_receipt_invalid`: the receipt is required |
102
- * | set | present | {@link verifyPrepareReceipt}; any failure throws |
103
- *
104
- * `prepareReceiptJws` is the raw response member: `undefined` or `null` is
105
- * absent, and anything else is present, whatever its type. In the first two
106
- * rows it is not read at all, and `expected` is a thunk so they compute
107
- * nothing: with no anchor, the login path does no receipt work.
108
- *
109
- * In the second row, an SDK without the anchor ignores the receipt: it could
110
- * not verify it, so ignoring gives up no protection. An ignored receipt is
111
- * never passed on as if it were checked: this returns `null` for it exactly as
112
- * for no receipt, so the login path hands the application no receipt at all.
113
- */
114
- export declare function checkPrepareReceipt(prepareReceiptJws: unknown, anchor: PrepareReceiptTrustAnchor | null, expected: () => PrepareReceiptExpectation, clock?: PrepareReceiptClock): Promise<VerifiedPrepareReceipt | null>;
115
- /**
116
- * Verify the receipt against a trust anchor and this attempt's expectation.
117
- *
118
- * Order: the anchor and the expectation first, because a fault in either is a
119
- * configuration error; then the receipt's shape; then its header and signature;
120
- * and only then the claims, because a claim from an unverified token is not
121
- * evidence of anything.
122
- *
123
- * A fault in the anchor, the expectation, or the clock is `contract_violation`;
124
- * every rejection of the receipt itself, including a platform verifier that is
125
- * missing or fails, is `prepare_receipt_invalid`.
126
- *
127
- * The receipt is never interpolated into an error: it is unverified
128
- * server-supplied text, and a consumer is likely to log the message verbatim.
129
- */
130
- export declare function verifyPrepareReceipt(prepareReceiptJws: unknown, anchor: PrepareReceiptTrustAnchor, expected: PrepareReceiptExpectation, clock?: PrepareReceiptClock): Promise<VerifiedPrepareReceipt>;
131
- /**
132
- * Domain separator for `parentEntityIdDigest`, byte-for-byte the one DAA
133
- * signs with. The trailing NUL is part of it.
134
- */
135
- export declare const PARENT_ENTITY_ID_DIGEST_DOMAIN = "daa-prepare-receipt-parent-entity-digest-v1\0";
136
- /**
137
- * `base64url_nopad(SHA-256(utf8(domain) || utf8(parentEntityId)))`, where the
138
- * id is the canonical lower-case hyphenated UUID — DAA's pinned formula.
139
- */
140
- export declare function parentEntityIdDigest(parentEntityId: string): string;
141
- /**
142
- * Tenant-scope comparison for a verified receipt. **Not called on the login
143
- * path:** the SDK has no defined way to derive the expected parent entity from
144
- * its configuration. A caller that has the expected values calls this after
145
- * {@link verifyPrepareReceipt}.
146
- */
147
- export declare function requirePrepareReceiptTenantScope(receipt: VerifiedPrepareReceipt, expected: Readonly<{
148
- customerAppId: string;
149
- parentEntityId: string;
150
- }>): void;
@@ -1,357 +0,0 @@
1
- "use strict";
2
- /**
3
- * Copyright (c) 2026, Circle Internet Group, Inc. All rights reserved.
4
- *
5
- * SPDX-License-Identifier: Apache-2.0
6
- *
7
- * Licensed under the Apache License, Version 2.0 (the "License");
8
- * you may not use this file except in compliance with the License.
9
- * You may obtain a copy of the License at
10
- *
11
- * http://www.apache.org/licenses/LICENSE-2.0
12
- *
13
- * Unless required by applicable law or agreed to in writing, software
14
- * distributed under the License is distributed on an "AS IS" BASIS,
15
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- * See the License for the specific language governing permissions and
17
- * limitations under the License.
18
- */
19
- Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.PARENT_ENTITY_ID_DIGEST_DOMAIN = exports.PREPARE_RECEIPT_MAX_CLOCK_SKEW_SECONDS = exports.PREPARE_RECEIPT_TYP = exports.PREPARE_RECEIPT_MAX_LENGTH = exports.PREPARE_RECEIPT_MIN_LENGTH = void 0;
21
- exports.pinnedPrepareReceiptTrustAnchor = pinnedPrepareReceiptTrustAnchor;
22
- exports.isPrepareReceiptShape = isPrepareReceiptShape;
23
- exports.checkPrepareReceipt = checkPrepareReceipt;
24
- exports.verifyPrepareReceipt = verifyPrepareReceipt;
25
- exports.parentEntityIdDigest = parentEntityIdDigest;
26
- exports.requirePrepareReceiptTenantScope = requirePrepareReceiptTenantScope;
27
- const base64url_1 = require("../crypto/base64url");
28
- const es256Jws_1 = require("../crypto/es256Jws");
29
- const sha256_1 = require("../crypto/sha256");
30
- const errors_1 = require("../errors");
31
- const uuid_1 = require("../validation/uuid");
32
- /** Contract bounds on `SocialLoginRequest.prepareReceiptJws`. */
33
- exports.PREPARE_RECEIPT_MIN_LENGTH = 64;
34
- exports.PREPARE_RECEIPT_MAX_LENGTH = 8192;
35
- /** The JWS `typ` DAA signs the receipt with. */
36
- exports.PREPARE_RECEIPT_TYP = 'daa-prepare-receipt+jws';
37
- /** Widest clock skew an anchor may allow, so a configuration typo cannot disable expiry. */
38
- exports.PREPARE_RECEIPT_MAX_CLOCK_SKEW_SECONDS = 300;
39
- /** Compact JWS serialization: three Base64URL segments separated by dots. */
40
- const COMPACT_JWS = /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$/;
41
- /**
42
- * The build-time pinned anchor for an environment. **`null` for every
43
- * environment in this release.**
44
- *
45
- * A function rather than a `const`, so that its declared return type does not
46
- * narrow the rest of this module to unreachable code.
47
- */
48
- function pinnedPrepareReceiptTrustAnchor(environment) {
49
- void environment;
50
- return null;
51
- }
52
- const SYSTEM_CLOCK = {
53
- nowEpochSeconds: () => Math.floor(Date.now() / 1000),
54
- };
55
- /**
56
- * Whether a value is shaped like the compact JWS the contract describes and sits
57
- * within its declared length bounds. Structural only: shape is not trust.
58
- */
59
- function isPrepareReceiptShape(value) {
60
- return (value.length >= exports.PREPARE_RECEIPT_MIN_LENGTH &&
61
- value.length <= exports.PREPARE_RECEIPT_MAX_LENGTH &&
62
- COMPACT_JWS.test(value));
63
- }
64
- /**
65
- * What login does with the receipt, given the anchor for this environment.
66
- *
67
- * | Anchor | Receipt | Result |
68
- * | ------ | ------- | ----------------------------------------------------------- |
69
- * | unset | absent | `null`: nothing to verify, login proceeds |
70
- * | unset | present | `null`: the receipt is ignored, login proceeds as if absent |
71
- * | set | absent | `prepare_receipt_invalid`: the receipt is required |
72
- * | set | present | {@link verifyPrepareReceipt}; any failure throws |
73
- *
74
- * `prepareReceiptJws` is the raw response member: `undefined` or `null` is
75
- * absent, and anything else is present, whatever its type. In the first two
76
- * rows it is not read at all, and `expected` is a thunk so they compute
77
- * nothing: with no anchor, the login path does no receipt work.
78
- *
79
- * In the second row, an SDK without the anchor ignores the receipt: it could
80
- * not verify it, so ignoring gives up no protection. An ignored receipt is
81
- * never passed on as if it were checked: this returns `null` for it exactly as
82
- * for no receipt, so the login path hands the application no receipt at all.
83
- */
84
- async function checkPrepareReceipt(prepareReceiptJws, anchor, expected, clock = SYSTEM_CLOCK) {
85
- if (anchor === null) {
86
- return null;
87
- }
88
- if (prepareReceiptJws === undefined || prepareReceiptJws === null) {
89
- throw (0, errors_1.prepareReceiptInvalid)('the Prepare response carries no receipt, and this environment requires one');
90
- }
91
- return verifyPrepareReceipt(prepareReceiptJws, anchor, expected(), clock);
92
- }
93
- /**
94
- * Verify the receipt against a trust anchor and this attempt's expectation.
95
- *
96
- * Order: the anchor and the expectation first, because a fault in either is a
97
- * configuration error; then the receipt's shape; then its header and signature;
98
- * and only then the claims, because a claim from an unverified token is not
99
- * evidence of anything.
100
- *
101
- * A fault in the anchor, the expectation, or the clock is `contract_violation`;
102
- * every rejection of the receipt itself, including a platform verifier that is
103
- * missing or fails, is `prepare_receipt_invalid`.
104
- *
105
- * The receipt is never interpolated into an error: it is unverified
106
- * server-supplied text, and a consumer is likely to log the message verbatim.
107
- */
108
- async function verifyPrepareReceipt(prepareReceiptJws, anchor, expected, clock = SYSTEM_CLOCK) {
109
- const keys = pinnedKeys(anchor);
110
- requireExpectation(expected);
111
- const now = clock.nowEpochSeconds();
112
- if (!Number.isInteger(now) || now <= 0) {
113
- throw (0, errors_1.contractViolation)('the Prepare receipt clock must yield a positive Unix timestamp');
114
- }
115
- if (typeof prepareReceiptJws !== 'string' || !isPrepareReceiptShape(prepareReceiptJws)) {
116
- throw (0, errors_1.prepareReceiptInvalid)('the Prepare receipt is not a compact JWS within the contract length bounds');
117
- }
118
- const result = await platformVerify(prepareReceiptJws, keys);
119
- if (!result.verified) {
120
- throw (0, errors_1.prepareReceiptInvalid)(`the Prepare receipt did not verify (${result.reason})`);
121
- }
122
- const claims = result.payload;
123
- requireClaimEquals(claims, 'iss', anchor.issuer);
124
- requireAudience(claims, anchor.audience);
125
- requireNonEmptyClaim(claims, 'jti');
126
- requireClaimEquals(claims, 'environment', expected.environment);
127
- requireClaimEquals(claims, 'provider', expected.provider);
128
- requireClaimEquals(claims, 'loginRequestId', expected.loginRequestId);
129
- requireClaimEquals(claims, 'nonce', expected.nonce);
130
- requireClaimEquals(claims, 'registrationCodeChallenge', expected.registrationCodeChallenge);
131
- requireClaimEquals(claims, 'registrationFlowKeyThumbprint', expected.registrationFlowKeyThumbprint);
132
- const expiresAtEpochSeconds = requireTimeClaims(claims, now, anchor.maxClockSkewSeconds);
133
- return {
134
- compactJws: prepareReceiptJws,
135
- loginRequestId: expected.loginRequestId,
136
- nonce: expected.nonce,
137
- expiresAtEpochSeconds,
138
- tenantScope: {
139
- customerAppId: (0, es256Jws_1.ownMember)(claims, 'customerAppId'),
140
- parentEntityIdDigest: (0, es256Jws_1.ownMember)(claims, 'parentEntityIdDigest'),
141
- },
142
- };
143
- }
144
- /**
145
- * The signature check, with a platform verifier that is missing or fails
146
- * reported as the receipt failing to verify.
147
- *
148
- * `pinnedKeys` has already validated everything {@link verifyCompactEs256}
149
- * would reject as a configuration error, so the only `DaaError` left to come
150
- * out of it is the platform's: no `DaaRegistration` module, one without the
151
- * verify function, or a native rejection. A native rejection also covers a
152
- * pinned point the platform does not accept as on the curve — the one anchor
153
- * fault only the platform can see, and indistinguishable here from the rest.
154
- * The SDK's own message and the original error (as `cause`) are kept.
155
- */
156
- async function platformVerify(jws, keys) {
157
- try {
158
- return await (0, es256Jws_1.verifyCompactEs256)(jws, { keys, expectedTyp: exports.PREPARE_RECEIPT_TYP });
159
- }
160
- catch (err) {
161
- if ((0, errors_1.isDaaError)(err) && err.detail.kind === 'contract_violation') {
162
- throw new errors_1.DaaError({ kind: 'prepare_receipt_invalid' }, err.message, err);
163
- }
164
- throw err;
165
- }
166
- }
167
- /**
168
- * Domain separator for `parentEntityIdDigest`, byte-for-byte the one DAA
169
- * signs with. The trailing NUL is part of it.
170
- */
171
- exports.PARENT_ENTITY_ID_DIGEST_DOMAIN = 'daa-prepare-receipt-parent-entity-digest-v1\0';
172
- /**
173
- * `base64url_nopad(SHA-256(utf8(domain) || utf8(parentEntityId)))`, where the
174
- * id is the canonical lower-case hyphenated UUID — DAA's pinned formula.
175
- */
176
- function parentEntityIdDigest(parentEntityId) {
177
- if (typeof parentEntityId !== 'string' ||
178
- !(0, uuid_1.isUuid)(parentEntityId) ||
179
- parentEntityId !== parentEntityId.toLowerCase()) {
180
- throw (0, errors_1.contractViolation)('parentEntityId must be a canonical lower-case UUID');
181
- }
182
- const encoder = new TextEncoder();
183
- const domain = encoder.encode(exports.PARENT_ENTITY_ID_DIGEST_DOMAIN);
184
- const id = encoder.encode(parentEntityId);
185
- const input = new Uint8Array(domain.length + id.length);
186
- input.set(domain);
187
- input.set(id, domain.length);
188
- return (0, base64url_1.toBase64Url)((0, sha256_1.sha256)(input));
189
- }
190
- /**
191
- * Tenant-scope comparison for a verified receipt. **Not called on the login
192
- * path:** the SDK has no defined way to derive the expected parent entity from
193
- * its configuration. A caller that has the expected values calls this after
194
- * {@link verifyPrepareReceipt}.
195
- */
196
- function requirePrepareReceiptTenantScope(receipt, expected) {
197
- const { customerAppId, parentEntityIdDigest: digest } = receipt.tenantScope;
198
- if (typeof customerAppId !== 'string' || customerAppId !== expected.customerAppId) {
199
- throw (0, errors_1.prepareReceiptInvalid)('the Prepare receipt does not bind the expected customerAppId');
200
- }
201
- if (typeof digest !== 'string' || digest !== parentEntityIdDigest(expected.parentEntityId)) {
202
- throw (0, errors_1.prepareReceiptInvalid)('the Prepare receipt does not bind the expected parentEntityIdDigest');
203
- }
204
- }
205
- /**
206
- * Validate the anchor and convert its keys to the points the platform verifies
207
- * against. A malformed anchor is a configuration error, not a rejected receipt.
208
- * Whether each point is on the curve is the platform's check.
209
- */
210
- function pinnedKeys(anchor) {
211
- if (typeof anchor !== 'object' || anchor === null) {
212
- throw (0, errors_1.contractViolation)('the Prepare receipt trust anchor must be an object');
213
- }
214
- if (typeof anchor.issuer !== 'string' || anchor.issuer.length === 0) {
215
- throw (0, errors_1.contractViolation)('the Prepare receipt trust anchor must name an issuer');
216
- }
217
- if (typeof anchor.audience !== 'string' || anchor.audience.length === 0) {
218
- throw (0, errors_1.contractViolation)('the Prepare receipt trust anchor must name an audience');
219
- }
220
- if (!Number.isInteger(anchor.maxClockSkewSeconds) ||
221
- anchor.maxClockSkewSeconds < 0 ||
222
- anchor.maxClockSkewSeconds > exports.PREPARE_RECEIPT_MAX_CLOCK_SKEW_SECONDS) {
223
- throw (0, errors_1.contractViolation)('the Prepare receipt trust anchor clock skew must be between 0 and ' +
224
- `${exports.PREPARE_RECEIPT_MAX_CLOCK_SKEW_SECONDS} seconds`);
225
- }
226
- if (!Array.isArray(anchor.verificationKeys) || anchor.verificationKeys.length === 0) {
227
- throw (0, errors_1.contractViolation)('the Prepare receipt trust anchor must pin at least one P-256 key');
228
- }
229
- const kids = new Set();
230
- for (const key of anchor.verificationKeys) {
231
- if (typeof key !== 'object' || key === null) {
232
- throw (0, errors_1.contractViolation)('every Prepare receipt verification key must be an object');
233
- }
234
- if (typeof key.kid !== 'string' || key.kid.length === 0) {
235
- throw (0, errors_1.contractViolation)('every Prepare receipt verification key must have a non-empty kid');
236
- }
237
- if (kids.has(key.kid)) {
238
- throw (0, errors_1.contractViolation)('Prepare receipt verification keys must have distinct kids');
239
- }
240
- kids.add(key.kid);
241
- }
242
- return anchor.verificationKeys.map((key) => ({
243
- kid: key.kid,
244
- publicKeyX963: x963FromJwk(key.jwk),
245
- }));
246
- }
247
- function x963FromJwk(jwk) {
248
- if (typeof jwk !== 'object' || jwk === null || jwk.kty !== 'EC' || jwk.crv !== 'P-256') {
249
- throw (0, errors_1.contractViolation)('a Prepare receipt verification key must be an EC P-256 JWK');
250
- }
251
- let x;
252
- let y;
253
- try {
254
- x = (0, base64url_1.fromBase64Url)(jwk.x);
255
- y = (0, base64url_1.fromBase64Url)(jwk.y);
256
- }
257
- catch {
258
- throw (0, errors_1.contractViolation)('a Prepare receipt verification key has a non-Base64URL coordinate');
259
- }
260
- if (x.length !== 32 || y.length !== 32) {
261
- throw (0, errors_1.contractViolation)('a Prepare receipt verification key must have 32-byte x and y coordinates');
262
- }
263
- const point = new Uint8Array(65);
264
- point[0] = 0x04;
265
- point.set(x, 1);
266
- point.set(y, 33);
267
- return point;
268
- }
269
- function requireExpectation(expected) {
270
- if (typeof expected !== 'object' || expected === null) {
271
- throw (0, errors_1.contractViolation)('verifying a Prepare receipt requires the expected values for this attempt');
272
- }
273
- for (const name of [
274
- 'loginRequestId',
275
- 'nonce',
276
- 'provider',
277
- 'environment',
278
- 'registrationCodeChallenge',
279
- 'registrationFlowKeyThumbprint',
280
- ]) {
281
- const value = expected[name];
282
- if (typeof value !== 'string' || value.length === 0) {
283
- throw (0, errors_1.contractViolation)(`verifying a Prepare receipt requires the expected ${name}`);
284
- }
285
- }
286
- }
287
- /**
288
- * Exact string equality on one claim. Not constant-time, and does not need to
289
- * be: every value compared is one the SDK sent, generated, or relayed to the
290
- * application; none is a secret. The claim name goes in the message — it is
291
- * one of this module's literals — and the value never does.
292
- */
293
- function requireClaimEquals(claims, name, expected) {
294
- const actual = (0, es256Jws_1.ownMember)(claims, name);
295
- if (typeof actual !== 'string' || actual !== expected) {
296
- throw (0, errors_1.prepareReceiptInvalid)(`the Prepare receipt does not bind the expected ${name}`);
297
- }
298
- }
299
- function requireNonEmptyClaim(claims, name) {
300
- const actual = (0, es256Jws_1.ownMember)(claims, name);
301
- if (typeof actual !== 'string' || actual.length === 0) {
302
- throw (0, errors_1.prepareReceiptInvalid)(`the Prepare receipt does not carry a ${name}`);
303
- }
304
- }
305
- /** RFC 7519 §4.1.3: `aud` is a string or an array of strings that must contain ours. */
306
- function requireAudience(claims, audience) {
307
- const actual = (0, es256Jws_1.ownMember)(claims, 'aud');
308
- if (typeof actual === 'string' && actual === audience) {
309
- return;
310
- }
311
- if (Array.isArray(actual) && actual.some((value) => value === audience)) {
312
- return;
313
- }
314
- throw (0, errors_1.prepareReceiptInvalid)('the Prepare receipt does not bind the expected aud');
315
- }
316
- /**
317
- * `exp` and `iat` are required (DAA always sets both); `nbf` is checked when
318
- * present. All are RFC 7519 NumericDates in seconds — deliberately not the
319
- * epoch-millis tolerance `wire/instant.ts` carries for the Prepare body, since
320
- * DAA's signer emits seconds. A millisecond value fails the upper bound below
321
- * instead of passing as an expiry thousands of years out.
322
- */
323
- function requireTimeClaims(claims, nowEpochSeconds, maxClockSkewSeconds) {
324
- const exp = requireNumericDate(claims, 'exp');
325
- const iat = requireNumericDate(claims, 'iat');
326
- if (exp + maxClockSkewSeconds < nowEpochSeconds) {
327
- throw (0, errors_1.prepareReceiptInvalid)('the Prepare receipt has expired');
328
- }
329
- if (iat - maxClockSkewSeconds > nowEpochSeconds) {
330
- throw (0, errors_1.prepareReceiptInvalid)('the Prepare receipt is not valid yet (iat is in the future)');
331
- }
332
- if (iat > exp) {
333
- throw (0, errors_1.prepareReceiptInvalid)('the Prepare receipt expires before it was issued');
334
- }
335
- if ((0, es256Jws_1.ownMember)(claims, 'nbf') !== undefined) {
336
- const nbf = requireNumericDate(claims, 'nbf');
337
- if (nbf - maxClockSkewSeconds > nowEpochSeconds) {
338
- throw (0, errors_1.prepareReceiptInvalid)('the Prepare receipt is not valid yet (nbf is in the future)');
339
- }
340
- }
341
- return exp;
342
- }
343
- /** Past year 9999 is not a clock; it is a unit mistake or a receipt that never expires. */
344
- const MAX_NUMERIC_DATE = 253402300799;
345
- function requireNumericDate(claims, name) {
346
- const value = (0, es256Jws_1.ownMember)(claims, name);
347
- if (value === undefined) {
348
- throw (0, errors_1.prepareReceiptInvalid)(`the Prepare receipt does not carry an ${name}`);
349
- }
350
- if (typeof value !== 'number' ||
351
- !Number.isInteger(value) ||
352
- value <= 0 ||
353
- value > MAX_NUMERIC_DATE) {
354
- throw (0, errors_1.prepareReceiptInvalid)(`the Prepare receipt carries a malformed ${name}`);
355
- }
356
- return value;
357
- }
@@ -1,56 +0,0 @@
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
- * The one algorithm this SDK verifies. Every additional entry is another
20
- * verification path, so the list holds only ES256.
21
- */
22
- export declare const SUPPORTED_JWS_ALGORITHMS: readonly string[];
23
- /** JOSE `R || S` for P-256: two 32-byte big-endian scalars. */
24
- export declare const ES256_SIGNATURE_LENGTH = 64;
25
- /** A verification key the caller pinned, addressed by its `kid`. */
26
- export type PinnedEs256Key = Readonly<{
27
- kid: string;
28
- /** Uncompressed P-256 point. The platform validates it is on the curve. */
29
- publicKeyX963: Uint8Array;
30
- }>;
31
- export type Es256JwsRejection = 'malformed_compact_serialization' | 'malformed_protected_header' | 'unsupported_algorithm' | 'unexpected_type' | 'header_sources_a_key' | 'unsupported_critical_header' | 'missing_key_id' | 'unknown_key_id' | 'malformed_signature' | 'signature_mismatch' | 'malformed_payload';
32
- export type Es256JwsResult = Readonly<{
33
- verified: true;
34
- protectedHeader: Readonly<Record<string, unknown>>;
35
- payload: Readonly<Record<string, unknown>>;
36
- }> | Readonly<{
37
- verified: false;
38
- reason: Es256JwsRejection;
39
- }>;
40
- /** Own-property read, so a name on `Object.prototype` never reads as a member. */
41
- export declare function ownMember(object: Readonly<Record<string, unknown>>, name: string): unknown;
42
- /**
43
- * Verify a compact JWS (`header.payload.signature`) against pinned P-256 keys.
44
- *
45
- * Length bounds are the caller's business: a caller with a contract bound (as
46
- * the Prepare receipt has) applies it first.
47
- *
48
- * @throws DaaError `contract_violation` for a malformed pinned key set, and when
49
- * the platform verifier is unavailable or fails — including a pinned key the
50
- * platform does not accept as a P-256 point. A token that is merely wrong is
51
- * a `{ verified: false }` result, not a throw.
52
- */
53
- export declare function verifyCompactEs256(jws: string, options: Readonly<{
54
- keys: readonly PinnedEs256Key[];
55
- expectedTyp: string;
56
- }>): Promise<Es256JwsResult>;