@paradex/sdk 0.9.0 → 0.10.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/README.md +8 -3
- package/dist/config.d.ts +2 -1
- package/dist/config.js +2 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/session-typed-data.d.ts +66 -12
- package/dist/session-typed-data.js +104 -53
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ import * as Paradex from '@paradex/sdk';
|
|
|
25
25
|
import { ethers } from 'ethers';
|
|
26
26
|
|
|
27
27
|
// 1. Fetch configuration for your environment
|
|
28
|
-
const config = await Paradex.Config.fetch('testnet'); // or '
|
|
28
|
+
const config = await Paradex.Config.fetch('testnet'); // 'prod', 'testnet' or 'nightly'
|
|
29
29
|
|
|
30
30
|
// 2. Connect to user's wallet
|
|
31
31
|
const provider = new ethers.BrowserProvider(window.ethereum);
|
|
@@ -169,7 +169,7 @@ try {
|
|
|
169
169
|
Fetch environment-specific configuration:
|
|
170
170
|
|
|
171
171
|
```typescript
|
|
172
|
-
const config = await Paradex.Config.fetch('testnet'); //
|
|
172
|
+
const config = await Paradex.Config.fetch('testnet'); // 'prod', 'testnet' or 'nightly'
|
|
173
173
|
```
|
|
174
174
|
|
|
175
175
|
## Common Operations
|
|
@@ -332,7 +332,11 @@ Grants are checked before signing. These throw a `SessionGrantError` with a `cod
|
|
|
332
332
|
|
|
333
333
|
`POST /v2/sessions` caps every grant at 30 days (7 with `trade:block` or `account:settings`). The `api` session type's 180-day cap is for IP-pinned API sessions, which that endpoint does not open; `SessionConnector.open()` always applies the 30-day cap.
|
|
334
334
|
|
|
335
|
-
|
|
335
|
+
A passkey opens a session by signing a `SessionMint`, whose WebAuthn challenge is `SessionTypedData.mintChallenge(mint)`. A mint names neither the account nor the passkey: the server finds the passkey by the assertion's credential id, so a page may pass an empty `allowCredentials` and let the user pick any passkey they hold. Its SNIP-12 hash is taken for account `0x0` (`MINT_HASH_ACCOUNT`). A mint may state `CEILING_LIMITS` (every amount `""` and `markets: []`) to take its passkey's ceiling, and `expiresAt: LONGEST_SESSION` (`0`) for the longest session the passkey allows. A stated `expiresAt` later than the passkey's own expiry is accepted, and the session ends when the passkey does; one past the ceiling's `maxSessionTtl` from now is refused.
|
|
336
|
+
|
|
337
|
+
`assertMintWithinCeiling` checks a mint against one `PasskeyGrant`, so it holds only when that grant's passkey signs. With an empty `allowCredentials` the user may pick another passkey, on another account or with another ceiling, and the server checks the mint against that one. A page that pre-checks against a grant, or shows the user limits taken from one, should pin `allowCredentials: [{ type: 'public-key', id: grant.passkey.credentialId }]` (the id decoded from base64url). A page that lets the user pick any passkey must compare the account in the server's response with the one it expected, and revoke the session it just opened if they differ.
|
|
338
|
+
|
|
339
|
+
`assertMintWithinCeiling` keeps a stated `expiresAt` the same 5 minutes inside the ceiling's `maxSessionTtl`; pass `clockSkewSeconds: 0` when `now` comes from the server's clock. For the same reason `validatePasskeyGrant` refuses a ceiling whose `maxSessionTtl` is 300 seconds or less, which would leave no time to mint; it takes the same `clockSkewSeconds` option.
|
|
336
340
|
|
|
337
341
|
Pass `limitCaps` to refuse limits above your own ceilings before the wallet is asked. Caps apply only to enforced limits; a cap on a limit the server doesn't enforce refuses every grant.
|
|
338
342
|
|
|
@@ -557,6 +561,7 @@ For a full React application example, see: [paradex-react-example](https://githu
|
|
|
557
561
|
| `buildGrantSnip12(grant)` | SNIP-12 rev 1 typed data for a Stark owner | `TypedData` |
|
|
558
562
|
| `buildPasskeyGrantEip712/Snip12(...)` | Typed data for `PasskeyGrant` | typed data |
|
|
559
563
|
| `buildMintEip712/Snip12(...)` | Typed data for `SessionMint` | typed data |
|
|
564
|
+
| `mintChallenge(mint)` | WebAuthn challenge for a `SessionMint` | `string` |
|
|
560
565
|
| `hashEip712(typedData)` | EIP-712 digest | `Hex` |
|
|
561
566
|
| `hashSnip12(typedData, account)` | SNIP-12 message hash | `Hex` |
|
|
562
567
|
| `validateGrant(grant)` | Check format rules and lifetime caps | `void` (throws) |
|
package/dist/config.d.ts
CHANGED
|
@@ -48,7 +48,8 @@ export interface ParadexConfig {
|
|
|
48
48
|
*/
|
|
49
49
|
readonly paradexApiUrl?: string;
|
|
50
50
|
}
|
|
51
|
-
|
|
51
|
+
declare const PARADEX_ENVIRONMENTS: readonly ["prod", "testnet", "nightly"];
|
|
52
|
+
type ParadexEnvironment = (typeof PARADEX_ENVIRONMENTS)[number];
|
|
52
53
|
/**
|
|
53
54
|
* Fetches the Paradex config for the given environment.
|
|
54
55
|
* This is required for account derivation.
|
package/dist/config.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ETHEREUM_MAINNET_CHAIN_ID, ETHEREUM_TESTNET_CHAIN_ID, STARKNET_MAINNET_CHAIN_ID, STARKNET_TESTNET_CHAIN_ID, } from './constants.js';
|
|
2
|
+
const PARADEX_ENVIRONMENTS = ['prod', 'testnet', 'nightly'];
|
|
2
3
|
/**
|
|
3
4
|
* Fetches the Paradex config for the given environment.
|
|
4
5
|
* This is required for account derivation.
|
|
@@ -15,7 +16,7 @@ export async function fetchConfig(environment) {
|
|
|
15
16
|
return buildConfig(config, apiUrl);
|
|
16
17
|
}
|
|
17
18
|
function assertParadexEnvironment(environment) {
|
|
18
|
-
if (environment
|
|
19
|
+
if (!PARADEX_ENVIRONMENTS.includes(environment)) {
|
|
19
20
|
throw new Error(`Invalid Paradex environment: ${environment}`);
|
|
20
21
|
}
|
|
21
22
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -56,7 +56,7 @@ export declare const Session: {
|
|
|
56
56
|
memoryStorage: typeof _SessionKey.memorySessionStorage;
|
|
57
57
|
indexedDbStorage: typeof _SessionKey.indexedDbSessionStorage;
|
|
58
58
|
};
|
|
59
|
-
export { ALL_LIMITS, DEFAULT_ENFORCED_LIMITS, DEFAULT_LIMIT_CAPS, MAX_GRANT_SUBACCOUNTS, SessionGrantError, UNCAPPED_LIMITS, } from './session-typed-data.js';
|
|
59
|
+
export { ALL_LIMITS, CEILING_LIMITS, DEFAULT_ENFORCED_LIMITS, DEFAULT_LIMIT_CAPS, LONGEST_SESSION, MAX_GRANT_SUBACCOUNTS, MINT_HASH_ACCOUNT, SessionGrantError, UNCAPPED_LIMITS, } from './session-typed-data.js';
|
|
60
60
|
export { SessionConnector } from './session-connector.js';
|
|
61
61
|
export { DeterminismGuardError, isContractCode } from './determinism-guard.js';
|
|
62
62
|
export { SessionAccounts } from './session-accounts.js';
|
package/dist/index.js
CHANGED
|
@@ -57,7 +57,7 @@ export const Session = {
|
|
|
57
57
|
memoryStorage: _SessionKey.memorySessionStorage,
|
|
58
58
|
indexedDbStorage: _SessionKey.indexedDbSessionStorage,
|
|
59
59
|
};
|
|
60
|
-
export { ALL_LIMITS, DEFAULT_ENFORCED_LIMITS, DEFAULT_LIMIT_CAPS, MAX_GRANT_SUBACCOUNTS, SessionGrantError, UNCAPPED_LIMITS, } from './session-typed-data.js';
|
|
60
|
+
export { ALL_LIMITS, CEILING_LIMITS, DEFAULT_ENFORCED_LIMITS, DEFAULT_LIMIT_CAPS, LONGEST_SESSION, MAX_GRANT_SUBACCOUNTS, MINT_HASH_ACCOUNT, SessionGrantError, UNCAPPED_LIMITS, } from './session-typed-data.js';
|
|
61
61
|
export { SessionConnector } from './session-connector.js';
|
|
62
62
|
export { DeterminismGuardError, isContractCode } from './determinism-guard.js';
|
|
63
63
|
export { SessionAccounts } from './session-accounts.js';
|
|
@@ -141,19 +141,48 @@ export interface PasskeyGrant {
|
|
|
141
141
|
readonly issuedAt: number;
|
|
142
142
|
readonly expiresAt: number;
|
|
143
143
|
}
|
|
144
|
-
/**
|
|
144
|
+
/**
|
|
145
|
+
* What a passkey signs to open one session; its SNIP-12 hash is the WebAuthn
|
|
146
|
+
* challenge ({@link sessionMintChallenge}).
|
|
147
|
+
*
|
|
148
|
+
* It names neither the account nor the passkey, so a client may let the user
|
|
149
|
+
* pick any passkey they hold (an empty `allowCredentials`). The server finds
|
|
150
|
+
* the passkey by the assertion's credential id, and its grant names the
|
|
151
|
+
* account.
|
|
152
|
+
*
|
|
153
|
+
* `limits` set to {@link CEILING_LIMITS} (every amount `""`, `markets: []`)
|
|
154
|
+
* takes the passkey's ceiling: the server applies its value for each limit it
|
|
155
|
+
* enforces and `"none"` for the rest. Stated limits must fit the ceiling.
|
|
156
|
+
* `expiresAt` {@link LONGEST_SESSION} (`0`) asks for the longest session the
|
|
157
|
+
* passkey allows: the ceiling's `maxSessionTtl` from the server's clock, or
|
|
158
|
+
* until the passkey expires if that is sooner. A stated `expiresAt` later
|
|
159
|
+
* than the passkey's expiry is accepted, and the session ends with the
|
|
160
|
+
* passkey.
|
|
161
|
+
*
|
|
162
|
+
* @experimental
|
|
163
|
+
*/
|
|
145
164
|
export interface SessionMint {
|
|
146
165
|
readonly paradexChain: string;
|
|
147
166
|
readonly environment: SessionEnvironment;
|
|
148
|
-
readonly account: string;
|
|
149
|
-
readonly credentialId: string;
|
|
150
167
|
readonly sessionKey: string;
|
|
151
168
|
readonly scopes: readonly SessionScope[];
|
|
152
169
|
readonly limits: Limits;
|
|
153
170
|
readonly subaccounts: readonly string[];
|
|
171
|
+
/** From `POST /v2/passkeys/challenge`; the server refuses one it did not issue. */
|
|
154
172
|
readonly nonce: string;
|
|
155
173
|
readonly expiresAt: number;
|
|
156
174
|
}
|
|
175
|
+
/** The account a `SessionMint`'s SNIP-12 message hash is taken for: a mint names no account. */
|
|
176
|
+
export declare const MINT_HASH_ACCOUNT = "0x0";
|
|
177
|
+
/** A `SessionMint` `expiresAt` asking for the longest session the passkey allows. */
|
|
178
|
+
export declare const LONGEST_SESSION = 0;
|
|
179
|
+
/**
|
|
180
|
+
* The limits a `SessionMint` states to take its passkey's ceiling: every
|
|
181
|
+
* amount empty and no markets.
|
|
182
|
+
*/
|
|
183
|
+
export declare const CEILING_LIMITS: Limits;
|
|
184
|
+
/** Whether `limits` are {@link CEILING_LIMITS}: no limit stated at all. */
|
|
185
|
+
export declare function isCeilingLimits(limits: Limits): boolean;
|
|
157
186
|
export declare const SESSION_GRANT_EIP712_TYPES: Eip712Types;
|
|
158
187
|
export declare const PASSKEY_GRANT_EIP712_TYPES: Eip712Types;
|
|
159
188
|
export declare const SESSION_MINT_EIP712_TYPES: Eip712Types;
|
|
@@ -226,6 +255,13 @@ export interface ValidatePasskeyGrantOptions extends LimitOptions {
|
|
|
226
255
|
readonly clockSkewSeconds?: number;
|
|
227
256
|
}
|
|
228
257
|
export declare function validatePasskeyGrant(grant: PasskeyGrant, options?: ValidatePasskeyGrantOptions): void;
|
|
258
|
+
/**
|
|
259
|
+
* Checks a `SessionMint`'s format. Its limits are either all stated (as for
|
|
260
|
+
* a grant) or {@link CEILING_LIMITS}; `expiresAt` may be
|
|
261
|
+
* {@link LONGEST_SESSION}. A mint names no account, so whether its
|
|
262
|
+
* subaccounts leave out the passkey's account is checked by
|
|
263
|
+
* {@link assertMintWithinCeiling}.
|
|
264
|
+
*/
|
|
229
265
|
export declare function validateSessionMint(mint: SessionMint, options?: LimitOptions): void;
|
|
230
266
|
export interface AssertMintOptions extends LimitOptions {
|
|
231
267
|
/**
|
|
@@ -237,14 +273,31 @@ export interface AssertMintOptions extends LimitOptions {
|
|
|
237
273
|
readonly clockSkewSeconds?: number;
|
|
238
274
|
}
|
|
239
275
|
/**
|
|
240
|
-
* Checks a `SessionMint` against the `PasskeyGrant`
|
|
241
|
-
* server does: same
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
* be
|
|
245
|
-
* `"none"`
|
|
246
|
-
*
|
|
247
|
-
*
|
|
276
|
+
* Checks a `SessionMint` against the `PasskeyGrant` of the passkey that signs
|
|
277
|
+
* it, as the server does: same chain and environment, and scopes within the
|
|
278
|
+
* ceiling. A mint names no account or passkey; the server finds the grant by
|
|
279
|
+
* the assertion's credential id. Only limits the server enforces are
|
|
280
|
+
* compared: each must be numeric and at or below a numeric ceiling (anything
|
|
281
|
+
* goes under a `"none"` ceiling), and markets must be within the ceiling's
|
|
282
|
+
* unless it allows `["*"]`. Unenforced limits are `"none"` on the mint and
|
|
283
|
+
* skipped, so passkeys registered with numeric limits keep working. A mint
|
|
284
|
+
* stating {@link CEILING_LIMITS} takes the ceiling's and fits by definition.
|
|
285
|
+
* A builder passkey's mints are checked like any other: the server checks
|
|
286
|
+
* `maxLoss` when the passkey registers, not on its mints. Subaccounts must
|
|
287
|
+
* not list the passkey's account.
|
|
288
|
+
*
|
|
289
|
+
* The check holds only when `grant`'s passkey signs. With an empty
|
|
290
|
+
* `allowCredentials` the user may pick another passkey, on another account
|
|
291
|
+
* or with another ceiling, and the server checks the mint against that one.
|
|
292
|
+
* Pin `allowCredentials` to `grant.passkey.credentialId`, or compare the
|
|
293
|
+
* account in the server's response with `grant.account` and revoke the
|
|
294
|
+
* session if they differ.
|
|
295
|
+
*
|
|
296
|
+
* Expiry: the passkey must not have expired. `expiresAt`
|
|
297
|
+
* {@link LONGEST_SESSION} always fits. Otherwise it must be in the future
|
|
298
|
+
* and at most the ceiling's `maxSessionTtl` from `now` (less the clock-skew
|
|
299
|
+
* allowance). It may be later than the passkey's expiry: the server then
|
|
300
|
+
* ends the session when the passkey expires.
|
|
248
301
|
*
|
|
249
302
|
* @experimental
|
|
250
303
|
*/
|
|
@@ -332,7 +385,8 @@ export declare function hashEip712(typedData: Eip712TypedData): Hex;
|
|
|
332
385
|
export declare function hashSnip12(typedData: Starknet.TypedData, account: string): Hex;
|
|
333
386
|
/**
|
|
334
387
|
* The WebAuthn challenge for a `SessionMint`: the 32-byte SNIP-12 message
|
|
335
|
-
* hash
|
|
388
|
+
* hash, taken for {@link MINT_HASH_ACCOUNT} (`0x0`) since a mint names no
|
|
389
|
+
* account, base64url-encoded without padding.
|
|
336
390
|
*/
|
|
337
391
|
export declare function sessionMintChallenge(mint: SessionMint, options?: LimitOptions): string;
|
|
338
392
|
/** The wording shown to owners about what a session can lose. */
|
|
@@ -97,6 +97,30 @@ export const UNCAPPED_LIMITS = {
|
|
|
97
97
|
maxLeverage: NO_CAP,
|
|
98
98
|
markets: ALL_MARKETS,
|
|
99
99
|
};
|
|
100
|
+
/** The account a `SessionMint`'s SNIP-12 message hash is taken for: a mint names no account. */
|
|
101
|
+
export const MINT_HASH_ACCOUNT = '0x0';
|
|
102
|
+
/** A `SessionMint` `expiresAt` asking for the longest session the passkey allows. */
|
|
103
|
+
export const LONGEST_SESSION = 0;
|
|
104
|
+
/**
|
|
105
|
+
* The limits a `SessionMint` states to take its passkey's ceiling: every
|
|
106
|
+
* amount empty and no markets.
|
|
107
|
+
*/
|
|
108
|
+
export const CEILING_LIMITS = {
|
|
109
|
+
maxOrderNotional: '',
|
|
110
|
+
maxOpenNotional: '',
|
|
111
|
+
dailyNotional: '',
|
|
112
|
+
maxLoss: '',
|
|
113
|
+
maxLeverage: '',
|
|
114
|
+
markets: [],
|
|
115
|
+
};
|
|
116
|
+
/** Whether `limits` are {@link CEILING_LIMITS}: no limit stated at all. */
|
|
117
|
+
export function isCeilingLimits(limits) {
|
|
118
|
+
return (limits != null &&
|
|
119
|
+
typeof limits === 'object' &&
|
|
120
|
+
LIMIT_AMOUNTS.every((key) => limits[key] === '') &&
|
|
121
|
+
Array.isArray(limits.markets) &&
|
|
122
|
+
limits.markets.length === 0);
|
|
123
|
+
}
|
|
100
124
|
// ---------------------------------------------------------------------------
|
|
101
125
|
// Type definitions
|
|
102
126
|
// ---------------------------------------------------------------------------
|
|
@@ -163,8 +187,6 @@ export const SESSION_MINT_EIP712_TYPES = {
|
|
|
163
187
|
SessionMint: [
|
|
164
188
|
{ name: 'paradexChain', type: 'string' },
|
|
165
189
|
{ name: 'environment', type: 'string' },
|
|
166
|
-
{ name: 'account', type: 'string' },
|
|
167
|
-
{ name: 'credentialId', type: 'string' },
|
|
168
190
|
{ name: 'sessionKey', type: 'string' },
|
|
169
191
|
{ name: 'scopes', type: 'string[]' },
|
|
170
192
|
{ name: 'limits', type: 'Limits' },
|
|
@@ -246,8 +268,6 @@ export const SESSION_MINT_SNIP12_TYPES = {
|
|
|
246
268
|
SessionMint: [
|
|
247
269
|
{ name: 'paradexChain', type: 'shortstring' },
|
|
248
270
|
{ name: 'environment', type: 'shortstring' },
|
|
249
|
-
{ name: 'account', type: 'ContractAddress' },
|
|
250
|
-
{ name: 'credentialId', type: 'string' },
|
|
251
271
|
{ name: 'sessionKey', type: 'felt' },
|
|
252
272
|
{ name: 'scopes', type: 'shortstring*' },
|
|
253
273
|
{ name: 'limits', type: 'Limits' },
|
|
@@ -393,8 +413,9 @@ function requireUniqueAddresses(field, values, anchor) {
|
|
|
393
413
|
const seen = new Set();
|
|
394
414
|
values.forEach((v, i) => {
|
|
395
415
|
requireAddress(`${field}[${i}]`, v);
|
|
396
|
-
if (v === anchor)
|
|
416
|
+
if (anchor != null && v === anchor) {
|
|
397
417
|
throw invalid(field, 'must not include the account');
|
|
418
|
+
}
|
|
398
419
|
if (seen.has(v))
|
|
399
420
|
throw invalid(field, 'has duplicates');
|
|
400
421
|
seen.add(v);
|
|
@@ -490,11 +511,14 @@ export function validateLimits(limits, builderGrant, field = 'limits', options =
|
|
|
490
511
|
seen.add(market);
|
|
491
512
|
});
|
|
492
513
|
}
|
|
493
|
-
function validateBinding(value
|
|
514
|
+
function validateBinding(value) {
|
|
494
515
|
requireShortString('paradexChain', value.paradexChain);
|
|
495
516
|
requireEnvironment(value.environment);
|
|
517
|
+
requireNonce('nonce', value.nonce);
|
|
518
|
+
}
|
|
519
|
+
function validateAccountBinding(value) {
|
|
520
|
+
validateBinding(value);
|
|
496
521
|
requireAddress('account', value.account);
|
|
497
|
-
requireNonce('nonce', noncePresent.nonce);
|
|
498
522
|
}
|
|
499
523
|
function requireLabel(field, value) {
|
|
500
524
|
if (typeof value !== 'string')
|
|
@@ -526,7 +550,7 @@ export function maxSessionTtl(scopes, sessionType = 'browser', clockSkewSeconds
|
|
|
526
550
|
* before the owner is asked to sign.
|
|
527
551
|
*/
|
|
528
552
|
export function validateSessionGrant(grant, options = {}) {
|
|
529
|
-
|
|
553
|
+
validateAccountBinding(grant);
|
|
530
554
|
requireCanonicalHex('sessionKey', grant.sessionKey, FELT_PRIME);
|
|
531
555
|
if (grant.sessionKey === '0x0')
|
|
532
556
|
throw invalid('sessionKey', 'is zero');
|
|
@@ -569,7 +593,7 @@ export function validateSessionGrant(grant, options = {}) {
|
|
|
569
593
|
validateLimits(grant.limits, hasBuilder, 'limits', options);
|
|
570
594
|
}
|
|
571
595
|
export function validatePasskeyGrant(grant, options = {}) {
|
|
572
|
-
|
|
596
|
+
validateAccountBinding(grant);
|
|
573
597
|
requireAddress('builderAccount', grant.builderAccount, false);
|
|
574
598
|
requireTimestamp('issuedAt', grant.issuedAt);
|
|
575
599
|
requireTimestamp('expiresAt', grant.expiresAt);
|
|
@@ -632,31 +656,54 @@ export function validatePasskeyGrant(grant, options = {}) {
|
|
|
632
656
|
throw invalid('ceiling.maxConcurrentSessions', `must be 1-${MAX_PASSKEY_CONCURRENT_SESSIONS}`);
|
|
633
657
|
}
|
|
634
658
|
}
|
|
659
|
+
/**
|
|
660
|
+
* Checks a `SessionMint`'s format. Its limits are either all stated (as for
|
|
661
|
+
* a grant) or {@link CEILING_LIMITS}; `expiresAt` may be
|
|
662
|
+
* {@link LONGEST_SESSION}. A mint names no account, so whether its
|
|
663
|
+
* subaccounts leave out the passkey's account is checked by
|
|
664
|
+
* {@link assertMintWithinCeiling}.
|
|
665
|
+
*/
|
|
635
666
|
export function validateSessionMint(mint, options = {}) {
|
|
636
|
-
validateBinding(mint
|
|
637
|
-
requireUniqueAddresses('subaccounts', mint.subaccounts
|
|
638
|
-
if (!BASE64URL_RE.test(mint.credentialId)) {
|
|
639
|
-
throw invalid('credentialId', 'must be base64url');
|
|
640
|
-
}
|
|
667
|
+
validateBinding(mint);
|
|
668
|
+
requireUniqueAddresses('subaccounts', mint.subaccounts);
|
|
641
669
|
requireCanonicalHex('sessionKey', mint.sessionKey, FELT_PRIME);
|
|
642
670
|
if (mint.sessionKey === '0x0')
|
|
643
671
|
throw invalid('sessionKey', 'is zero');
|
|
644
672
|
validateScopes(mint.scopes);
|
|
645
|
-
|
|
673
|
+
if (!isCeilingLimits(mint.limits)) {
|
|
674
|
+
validateLimits(mint.limits, false, 'limits', options);
|
|
675
|
+
}
|
|
646
676
|
requireTimestamp('expiresAt', mint.expiresAt);
|
|
647
677
|
}
|
|
648
678
|
function exceeds(message) {
|
|
649
679
|
return new SessionGrantError('EXCEEDS_CEILING', message);
|
|
650
680
|
}
|
|
651
681
|
/**
|
|
652
|
-
* Checks a `SessionMint` against the `PasskeyGrant`
|
|
653
|
-
* server does: same
|
|
654
|
-
*
|
|
655
|
-
*
|
|
656
|
-
* be
|
|
657
|
-
* `"none"`
|
|
658
|
-
*
|
|
659
|
-
*
|
|
682
|
+
* Checks a `SessionMint` against the `PasskeyGrant` of the passkey that signs
|
|
683
|
+
* it, as the server does: same chain and environment, and scopes within the
|
|
684
|
+
* ceiling. A mint names no account or passkey; the server finds the grant by
|
|
685
|
+
* the assertion's credential id. Only limits the server enforces are
|
|
686
|
+
* compared: each must be numeric and at or below a numeric ceiling (anything
|
|
687
|
+
* goes under a `"none"` ceiling), and markets must be within the ceiling's
|
|
688
|
+
* unless it allows `["*"]`. Unenforced limits are `"none"` on the mint and
|
|
689
|
+
* skipped, so passkeys registered with numeric limits keep working. A mint
|
|
690
|
+
* stating {@link CEILING_LIMITS} takes the ceiling's and fits by definition.
|
|
691
|
+
* A builder passkey's mints are checked like any other: the server checks
|
|
692
|
+
* `maxLoss` when the passkey registers, not on its mints. Subaccounts must
|
|
693
|
+
* not list the passkey's account.
|
|
694
|
+
*
|
|
695
|
+
* The check holds only when `grant`'s passkey signs. With an empty
|
|
696
|
+
* `allowCredentials` the user may pick another passkey, on another account
|
|
697
|
+
* or with another ceiling, and the server checks the mint against that one.
|
|
698
|
+
* Pin `allowCredentials` to `grant.passkey.credentialId`, or compare the
|
|
699
|
+
* account in the server's response with `grant.account` and revoke the
|
|
700
|
+
* session if they differ.
|
|
701
|
+
*
|
|
702
|
+
* Expiry: the passkey must not have expired. `expiresAt`
|
|
703
|
+
* {@link LONGEST_SESSION} always fits. Otherwise it must be in the future
|
|
704
|
+
* and at most the ceiling's `maxSessionTtl` from `now` (less the clock-skew
|
|
705
|
+
* allowance). It may be later than the passkey's expiry: the server then
|
|
706
|
+
* ends the session when the passkey expires.
|
|
660
707
|
*
|
|
661
708
|
* @experimental
|
|
662
709
|
*/
|
|
@@ -674,12 +721,6 @@ export function assertMintWithinCeiling(mint, grant, now = nowSeconds(), options
|
|
|
674
721
|
mint.environment !== grant.environment) {
|
|
675
722
|
throw new SessionGrantError('ENVIRONMENT_MISMATCH', 'mint and passkey grant are for different environments');
|
|
676
723
|
}
|
|
677
|
-
if (mint.account !== grant.account) {
|
|
678
|
-
throw exceeds('account: the mint names a different account');
|
|
679
|
-
}
|
|
680
|
-
if (mint.credentialId !== grant.passkey.credentialId) {
|
|
681
|
-
throw exceeds('credentialId: the mint names a different passkey');
|
|
682
|
-
}
|
|
683
724
|
const { ceiling } = grant;
|
|
684
725
|
for (const scope of mint.scopes) {
|
|
685
726
|
if (!ceiling.scopes.includes(scope)) {
|
|
@@ -687,30 +728,33 @@ export function assertMintWithinCeiling(mint, grant, now = nowSeconds(), options
|
|
|
687
728
|
}
|
|
688
729
|
}
|
|
689
730
|
const enforced = options.enforcedLimits ?? DEFAULT_ENFORCED_LIMITS;
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
731
|
+
// The ceiling's own limits fit by definition. Limits the server does not
|
|
732
|
+
// enforce are "none" on the mint and are not compared, so passkeys
|
|
733
|
+
// registered with numbers keep working.
|
|
734
|
+
if (!isCeilingLimits(mint.limits)) {
|
|
735
|
+
for (const key of LIMIT_AMOUNTS) {
|
|
736
|
+
const cap = ceiling.limits[key];
|
|
737
|
+
const value = mint.limits[key];
|
|
738
|
+
if (!enforced.includes(key) || isNoCap(cap))
|
|
739
|
+
continue;
|
|
740
|
+
if (isNoCap(value)) {
|
|
741
|
+
throw exceeds(`limits.${key}: the ceiling caps it at ${cap}`);
|
|
742
|
+
}
|
|
743
|
+
if (new BigNumber(value).isGreaterThan(cap)) {
|
|
744
|
+
throw exceeds(`limits.${key}: ${value} is above the ceiling ${cap}`);
|
|
745
|
+
}
|
|
702
746
|
}
|
|
703
|
-
|
|
704
|
-
|
|
747
|
+
const allowedMarkets = enforced.includes('markets')
|
|
748
|
+
? ceiling.limits.markets
|
|
749
|
+
: ALL_MARKETS;
|
|
750
|
+
for (const market of mint.limits.markets) {
|
|
751
|
+
if (!allowedMarkets.some((pattern) => marketCovers(pattern, market))) {
|
|
752
|
+
throw exceeds(`limits.markets: "${market}" is not in the ceiling`);
|
|
753
|
+
}
|
|
705
754
|
}
|
|
706
755
|
}
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
: ALL_MARKETS;
|
|
710
|
-
for (const market of mint.limits.markets) {
|
|
711
|
-
if (!allowedMarkets.some((pattern) => marketCovers(pattern, market))) {
|
|
712
|
-
throw exceeds(`limits.markets: "${market}" is not in the ceiling`);
|
|
713
|
-
}
|
|
756
|
+
if (mint.subaccounts.includes(grant.account)) {
|
|
757
|
+
throw invalid('subaccounts', "must not include the passkey's account");
|
|
714
758
|
}
|
|
715
759
|
if (!ceiling.includeFutureSubaccounts) {
|
|
716
760
|
for (const account of mint.subaccounts) {
|
|
@@ -722,6 +766,10 @@ export function assertMintWithinCeiling(mint, grant, now = nowSeconds(), options
|
|
|
722
766
|
if (grant.expiresAt <= now) {
|
|
723
767
|
throw new SessionGrantError('GRANT_EXPIRED', `passkey grant expired at ${grant.expiresAt}`);
|
|
724
768
|
}
|
|
769
|
+
// The server picks the expiry: the ceiling's maxSessionTtl from its own
|
|
770
|
+
// clock, or the passkey's expiry if that is sooner.
|
|
771
|
+
if (mint.expiresAt === LONGEST_SESSION)
|
|
772
|
+
return;
|
|
725
773
|
if (mint.expiresAt <= now) {
|
|
726
774
|
throw new SessionGrantError('GRANT_EXPIRED', `mint expired at ${mint.expiresAt}`);
|
|
727
775
|
}
|
|
@@ -729,7 +777,9 @@ export function assertMintWithinCeiling(mint, grant, now = nowSeconds(), options
|
|
|
729
777
|
if (ceiling.maxSessionTtl <= skew) {
|
|
730
778
|
throw new SessionGrantError('TTL_EXCEEDED', `ceiling.maxSessionTtl: ${ceiling.maxSessionTtl}s leaves no time after ${skew}s of clock skew`);
|
|
731
779
|
}
|
|
732
|
-
|
|
780
|
+
// A mint may outlive its passkey: the server ends the session when the
|
|
781
|
+
// passkey expires.
|
|
782
|
+
const latest = now + ceiling.maxSessionTtl - skew;
|
|
733
783
|
if (mint.expiresAt > latest) {
|
|
734
784
|
throw exceeds(`expiresAt: must be at or before ${latest}`);
|
|
735
785
|
}
|
|
@@ -935,10 +985,11 @@ export function hashSnip12(typedData, account) {
|
|
|
935
985
|
}
|
|
936
986
|
/**
|
|
937
987
|
* The WebAuthn challenge for a `SessionMint`: the 32-byte SNIP-12 message
|
|
938
|
-
* hash
|
|
988
|
+
* hash, taken for {@link MINT_HASH_ACCOUNT} (`0x0`) since a mint names no
|
|
989
|
+
* account, base64url-encoded without padding.
|
|
939
990
|
*/
|
|
940
991
|
export function sessionMintChallenge(mint, options = {}) {
|
|
941
|
-
const hash = hashSnip12(buildSessionMintSnip12(mint, options),
|
|
992
|
+
const hash = hashSnip12(buildSessionMintSnip12(mint, options), MINT_HASH_ACCOUNT);
|
|
942
993
|
const bytes = hexToBytes(hash.slice(2));
|
|
943
994
|
let binary = '';
|
|
944
995
|
for (const b of bytes)
|