@paradex/sdk 0.8.1 → 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.
@@ -0,0 +1,400 @@
1
+ import * as Starknet from 'starknet';
2
+ import { type Eip712TypedData, type Eip712Types } from './eip712.js';
3
+ import type { Hex } from './types.js';
4
+ export declare const SESSION_DOMAIN_NAME = "Paradex Session";
5
+ export declare const SESSION_DOMAIN_VERSION = "1";
6
+ /** The "none" builder / first-party passkey sentinel. */
7
+ export declare const NO_BUILDER = "0x0";
8
+ /**
9
+ * The Paradex environment the grant is for, e.g. `"mainnet"` or `"testnet"`.
10
+ * It is part of the signed message so a grant cannot be replayed on another
11
+ * environment; the server checks it.
12
+ */
13
+ export type SessionEnvironment = 'mainnet' | 'testnet' | (string & {});
14
+ /** Scopes a grant may request. `read` is always implied and never listed. */
15
+ export type SessionScope = 'trade' | 'trade:rfq' | 'trade:block' | 'account:settings';
16
+ export declare const GRANTABLE_SCOPES: readonly SessionScope[];
17
+ /** Scopes that exist in the format but are refused. */
18
+ export declare const RESERVED_SCOPES: readonly string[];
19
+ /** Server caps on session lifetime, in seconds. */
20
+ export declare const SESSION_TTL: {
21
+ readonly browserDefault: number;
22
+ readonly browserMax: number;
23
+ /**
24
+ * Cap for IP-pinned API sessions. `POST /v2/sessions` applies
25
+ * `browserMax` to every grant, so `SessionConnector` never uses this.
26
+ */
27
+ readonly apiMax: number;
28
+ /** Cap for grants that include `trade:block` or `account:settings`. */
29
+ readonly sensitiveScopeMax: number;
30
+ readonly passkeyMax: number;
31
+ readonly passkeySessionMax: number;
32
+ };
33
+ export declare const MAX_PASSKEY_CONCURRENT_SESSIONS = 5;
34
+ /** A grant, mint or passkey ceiling lists at most this many subaccounts. */
35
+ export declare const MAX_GRANT_SUBACCOUNTS = 32;
36
+ export type SessionGrantErrorCode = 'INVALID_FIELD' | 'UNKNOWN_SCOPE' | 'RESERVED_SCOPE' | 'TTL_EXCEEDED' | 'BUILDER_LIMITS_REQUIRED' | 'BUILDER_REQUIRES_EIP712' | 'GRANT_EXPIRED' | 'GRANT_REVOKED' | 'ENVIRONMENT_MISMATCH' | 'ACCOUNT_NOT_IN_GRANT' | 'ACCOUNT_NOT_ELIGIBLE' | 'EXCEEDS_CEILING' | 'SCOPE_NOT_ALLOWED' | 'LIMIT_INVALID';
37
+ export declare class SessionGrantError extends Error {
38
+ readonly code: SessionGrantErrorCode;
39
+ constructor(code: SessionGrantErrorCode, message: string);
40
+ }
41
+ /**
42
+ * Every limit is stated: a positive decimal string (USD, or a multiple for
43
+ * `maxLeverage`) such as `"25000"`, or {@link NO_CAP} for no cap.
44
+ */
45
+ export interface Limits {
46
+ readonly maxOrderNotional: string;
47
+ readonly maxOpenNotional: string;
48
+ readonly dailyNotional: string;
49
+ readonly maxLoss: string;
50
+ readonly maxLeverage: string;
51
+ /**
52
+ * Symbols or patterns (`"BTC-USD-PERP"`, `"*-USD-PERP"`), or {@link ALL_MARKETS}
53
+ * (`["*"]`) for every market the session may trade. Never empty.
54
+ */
55
+ readonly markets: readonly string[];
56
+ }
57
+ /** A limit value meaning "not capped by this grant". */
58
+ export declare const NO_CAP = "none";
59
+ /** `markets` value meaning every permitted market. */
60
+ export declare const ALL_MARKETS: readonly string[];
61
+ export type LimitAmount = Exclude<keyof Limits, 'markets'>;
62
+ /**
63
+ * Highest value accepted for each limit, as decimal strings. The server
64
+ * applies its own configured caps; these are checked before signing so an
65
+ * over-cap grant never reaches the wallet. Unset means uncapped.
66
+ */
67
+ export type LimitCaps = Partial<Record<LimitAmount, string>>;
68
+ /** No client-side caps by default; the server applies its own. */
69
+ export declare const DEFAULT_LIMIT_CAPS: LimitCaps;
70
+ /** A limit the server can enforce: one of the amounts, or `markets`. */
71
+ export type LimitName = keyof Limits;
72
+ /**
73
+ * Limits the server enforces. Defaults to none: until the server enforces a
74
+ * limit, a grant may not set it, because nothing would hold orders to it.
75
+ */
76
+ export declare const DEFAULT_ENFORCED_LIMITS: readonly LimitName[];
77
+ /** Every limit set, for limits a client knows the server enforces. */
78
+ export declare const ALL_LIMITS: readonly LimitName[];
79
+ /** Every limit uncapped: what a grant must state while none is enforced. */
80
+ export declare const UNCAPPED_LIMITS: Limits;
81
+ export interface LimitOptions {
82
+ /** Defaults to {@link DEFAULT_LIMIT_CAPS}. */
83
+ readonly limitCaps?: LimitCaps;
84
+ /**
85
+ * Limits the server enforces; defaults to {@link DEFAULT_ENFORCED_LIMITS}
86
+ * (none). A limit not listed must be `"none"` (`markets`: `["*"]`).
87
+ */
88
+ readonly enforcedLimits?: readonly LimitName[];
89
+ }
90
+ export interface Builder {
91
+ /** Builder's Paradex account; `"0x0"` = none. */
92
+ readonly account: string;
93
+ /** Fee ceiling as a decimal fraction: `"0.0005"` = 5 bps. */
94
+ readonly maxFeeRate: string;
95
+ /** Seconds; `0` = follows the session. */
96
+ readonly expiresAt: number;
97
+ }
98
+ /** @experimental */
99
+ export interface SessionGrant {
100
+ readonly paradexChain: string;
101
+ readonly environment: SessionEnvironment;
102
+ readonly account: string;
103
+ readonly sessionKey: string;
104
+ readonly label: string;
105
+ readonly scopes: readonly SessionScope[];
106
+ readonly limits: Limits;
107
+ readonly builder: Builder;
108
+ readonly subaccounts: readonly string[];
109
+ readonly nonce: string;
110
+ readonly issuedAt: number;
111
+ readonly expiresAt: number;
112
+ }
113
+ export interface Passkey {
114
+ /** base64url credential id. */
115
+ readonly credentialId: string;
116
+ /** P-256 public key coordinates, 0x-prefixed 32-byte hex. */
117
+ readonly pubkeyX: string;
118
+ readonly pubkeyY: string;
119
+ readonly rpId: string;
120
+ readonly label: string;
121
+ }
122
+ export interface Ceiling {
123
+ readonly scopes: readonly SessionScope[];
124
+ readonly limits: Limits;
125
+ readonly subaccounts: readonly string[];
126
+ /** Allowed only for first-party passkeys (`builderAccount = "0x0"`). */
127
+ readonly includeFutureSubaccounts: boolean;
128
+ readonly maxSessionTtl: number;
129
+ readonly maxConcurrentSessions: number;
130
+ }
131
+ /** @experimental */
132
+ export interface PasskeyGrant {
133
+ readonly paradexChain: string;
134
+ readonly environment: SessionEnvironment;
135
+ readonly account: string;
136
+ readonly passkey: Passkey;
137
+ readonly ceiling: Ceiling;
138
+ /** `"0x0"` = Paradex app; otherwise binds the passkey to one builder. */
139
+ readonly builderAccount: string;
140
+ readonly nonce: string;
141
+ readonly issuedAt: number;
142
+ readonly expiresAt: number;
143
+ }
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
+ */
164
+ export interface SessionMint {
165
+ readonly paradexChain: string;
166
+ readonly environment: SessionEnvironment;
167
+ readonly sessionKey: string;
168
+ readonly scopes: readonly SessionScope[];
169
+ readonly limits: Limits;
170
+ readonly subaccounts: readonly string[];
171
+ /** From `POST /v2/passkeys/challenge`; the server refuses one it did not issue. */
172
+ readonly nonce: string;
173
+ readonly expiresAt: number;
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;
186
+ export declare const SESSION_GRANT_EIP712_TYPES: Eip712Types;
187
+ export declare const PASSKEY_GRANT_EIP712_TYPES: Eip712Types;
188
+ export declare const SESSION_MINT_EIP712_TYPES: Eip712Types;
189
+ export declare const SESSION_GRANT_SNIP12_TYPES: Starknet.TypedData['types'];
190
+ export declare const PASSKEY_GRANT_SNIP12_TYPES: Starknet.TypedData['types'];
191
+ export declare const SESSION_MINT_SNIP12_TYPES: Starknet.TypedData['types'];
192
+ /** Whether a ceiling market entry covers a mint's market entry. */
193
+ export declare function marketCovers(pattern: string, market: string): boolean;
194
+ /**
195
+ * Canonical hex for felts and addresses: lowercase, `0x`-prefixed, no leading
196
+ * zeros (`0x0` for zero). EIP-712 hashes these as strings, so the form is part
197
+ * of the signed bytes; every builder normalizes to it and validation rejects
198
+ * anything else.
199
+ */
200
+ export declare function canonicalFelt(value: string | bigint): string;
201
+ /** Rejects unknown and reserved scopes, duplicates and an empty list. */
202
+ export declare function validateScopes(scopes: readonly string[], field?: string): asserts scopes is readonly SessionScope[];
203
+ /**
204
+ * Every amount must be a positive decimal or `"none"`; `markets` must be a
205
+ * non-empty list, or `["*"]` alone. A limit the server does not enforce
206
+ * must be `"none"` (`markets`: `["*"]`).
207
+ *
208
+ * @param builderGrant builder grants must cap `maxLoss` with a number, so
209
+ * they are refused while the server does not enforce `maxLoss`.
210
+ */
211
+ export declare function validateLimits(limits: Limits, builderGrant: boolean, field?: string, options?: LimitOptions): void;
212
+ export type SessionType = 'browser' | 'api';
213
+ /**
214
+ * Longest lifetime a grant with these scopes can request. The server measures
215
+ * the lifetime from the earlier of `issuedAt` and its own clock, and accepts
216
+ * an `issuedAt` up to 5 minutes ahead of it, so this is the server's cap less
217
+ * that allowance: a grant at this length still passes when the client's clock
218
+ * runs ahead.
219
+ */
220
+ export declare function maxSessionTtl(scopes: readonly SessionScope[], sessionType?: SessionType, clockSkewSeconds?: number): number;
221
+ export interface ValidateSessionGrantOptions extends LimitOptions {
222
+ /**
223
+ * Which lifetime cap applies. Defaults to `browser` (30 days), the cap
224
+ * `POST /v2/sessions` applies to every grant. `api` (180 days) is the cap
225
+ * for IP-pinned API sessions, which that endpoint does not open; a grant
226
+ * longer than 30 days is refused there.
227
+ */
228
+ readonly sessionType?: SessionType;
229
+ /**
230
+ * How far ahead of the server the client's clock may run, in seconds.
231
+ * Lifetimes are kept this far under the server cap. Defaults to 300, the
232
+ * server's `issuedAt` allowance; pass 0 when `issuedAt` comes from the
233
+ * server's clock.
234
+ */
235
+ readonly clockSkewSeconds?: number;
236
+ }
237
+ /**
238
+ * Checks a grant against the format rules and server caps. Throws a
239
+ * {@link SessionGrantError}. The server repeats every check; this one fails
240
+ * before the owner is asked to sign.
241
+ */
242
+ export declare function validateSessionGrant(grant: SessionGrant, options?: ValidateSessionGrantOptions): void;
243
+ export interface ValidatePasskeyGrantOptions extends LimitOptions {
244
+ /**
245
+ * Whether the server accepts subaccounts in a passkey ceiling. Defaults to
246
+ * false, as the server refuses `ceiling.subaccounts` and
247
+ * `includeFutureSubaccounts` until it enables them.
248
+ */
249
+ readonly passkeySubaccounts?: boolean;
250
+ /**
251
+ * See {@link AssertMintOptions.clockSkewSeconds}. A `ceiling.maxSessionTtl`
252
+ * at or under it leaves no time to mint a session, so it is refused.
253
+ * Defaults to 300.
254
+ */
255
+ readonly clockSkewSeconds?: number;
256
+ }
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
+ */
265
+ export declare function validateSessionMint(mint: SessionMint, options?: LimitOptions): void;
266
+ export interface AssertMintOptions extends LimitOptions {
267
+ /**
268
+ * How far ahead of the server the client's clock may run, in seconds. The
269
+ * server bounds a mint's `expiresAt` by its own clock plus the ceiling's
270
+ * `maxSessionTtl`, so the bound here is kept this far under it. Defaults
271
+ * to 300; pass 0 when `now` comes from the server's clock.
272
+ */
273
+ readonly clockSkewSeconds?: number;
274
+ }
275
+ /**
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.
301
+ *
302
+ * @experimental
303
+ */
304
+ export declare function assertMintWithinCeiling(mint: SessionMint, grant: PasskeyGrant, now?: number, options?: AssertMintOptions): void;
305
+ /** A fresh 128-bit random nonce in canonical hex. */
306
+ export declare function generateSessionNonce(): string;
307
+ export interface CreateSessionGrantParams {
308
+ readonly paradexChain: string;
309
+ readonly environment: SessionEnvironment;
310
+ readonly account: string;
311
+ readonly sessionKey: string;
312
+ readonly scopes?: readonly SessionScope[];
313
+ readonly label?: string;
314
+ /**
315
+ * Every limit, stated as a positive decimal or `"none"`. Defaults to
316
+ * {@link UNCAPPED_LIMITS}, the only limits accepted while the server
317
+ * enforces none.
318
+ */
319
+ readonly limits?: Limits;
320
+ readonly builder?: {
321
+ readonly account: string;
322
+ readonly maxFeeRate: string;
323
+ readonly expiresAt?: number;
324
+ };
325
+ readonly subaccounts?: readonly string[];
326
+ readonly nonce?: string;
327
+ /** Seconds. Defaults to now. */
328
+ readonly issuedAt?: number;
329
+ /** Seconds. Defaults to `issuedAt + ttlSeconds`. */
330
+ readonly expiresAt?: number;
331
+ /** Defaults to 7 days, the browser-session default. */
332
+ readonly ttlSeconds?: number;
333
+ /** See {@link ValidateSessionGrantOptions.sessionType}. */
334
+ readonly sessionType?: SessionType;
335
+ /** See {@link ValidateSessionGrantOptions.clockSkewSeconds}. */
336
+ readonly clockSkewSeconds?: number;
337
+ /** Defaults to {@link DEFAULT_LIMIT_CAPS}. */
338
+ readonly limitCaps?: LimitCaps;
339
+ /** Limits the server enforces; defaults to none (see {@link LimitOptions}). */
340
+ readonly enforcedLimits?: readonly LimitName[];
341
+ }
342
+ export declare function nowSeconds(): number;
343
+ /**
344
+ * Builds a canonical, validated `SessionGrant`. Addresses and keys are
345
+ * normalized to canonical hex; everything else is checked as given.
346
+ *
347
+ * @experimental
348
+ */
349
+ export declare function createSessionGrant(params: CreateSessionGrantParams): SessionGrant;
350
+ /** Every account a grant reaches: the anchor first, then its sub-accounts. */
351
+ export declare function grantAccounts(grant: Pick<SessionGrant, 'account' | 'subaccounts'>): readonly string[];
352
+ /**
353
+ * Throws `GRANT_EXPIRED` once the grant (or a time just ahead of `now`) has
354
+ * passed its expiry, and `INVALID_FIELD` if `issuedAt` is too far ahead.
355
+ */
356
+ export declare function assertGrantActive(grant: Pick<SessionGrant, 'issuedAt' | 'expiresAt'>, now?: number): void;
357
+ /**
358
+ * EIP-712 `SessionGrant`. `signatureChainId` is the wallet's *current* EVM
359
+ * chain, so no network switch is needed; send it along with the signature.
360
+ *
361
+ * @experimental
362
+ */
363
+ export declare function buildSessionGrantEip712(grant: SessionGrant, signatureChainId: number | string | bigint, options?: ValidateSessionGrantOptions): Eip712TypedData;
364
+ /**
365
+ * SNIP-12 revision 1 `SessionGrant`, signed by a Stark owner key. Builder
366
+ * grants are EIP-712 only, so a grant naming a builder is refused here.
367
+ *
368
+ * @experimental
369
+ */
370
+ export declare function buildSessionGrantSnip12(grant: SessionGrant, options?: ValidateSessionGrantOptions): Starknet.TypedData;
371
+ /** @experimental */
372
+ export declare function buildPasskeyGrantEip712(grant: PasskeyGrant, signatureChainId: number | string | bigint, options?: ValidatePasskeyGrantOptions): Eip712TypedData;
373
+ /** @experimental */
374
+ export declare function buildPasskeyGrantSnip12(grant: PasskeyGrant, options?: ValidatePasskeyGrantOptions): Starknet.TypedData;
375
+ /** @experimental */
376
+ export declare function buildSessionMintEip712(mint: SessionMint, signatureChainId: number | string | bigint, options?: LimitOptions): Eip712TypedData;
377
+ /** @experimental */
378
+ export declare function buildSessionMintSnip12(mint: SessionMint, options?: LimitOptions): Starknet.TypedData;
379
+ /** EIP-712 digest (`0x1901 ‖ domain ‖ struct`) as 0x-prefixed 32-byte hex. */
380
+ export declare function hashEip712(typedData: Eip712TypedData): Hex;
381
+ /**
382
+ * SNIP-12 message hash for `account` (the signer's address), as 0x-prefixed
383
+ * 32-byte hex.
384
+ */
385
+ export declare function hashSnip12(typedData: Starknet.TypedData, account: string): Hex;
386
+ /**
387
+ * The WebAuthn challenge for a `SessionMint`: the 32-byte SNIP-12 message
388
+ * hash, taken for {@link MINT_HASH_ACCOUNT} (`0x0`) since a mint names no
389
+ * account, base64url-encoded without padding.
390
+ */
391
+ export declare function sessionMintChallenge(mint: SessionMint, options?: LimitOptions): string;
392
+ /** The wording shown to owners about what a session can lose. */
393
+ export declare function lossSummary(limits: Pick<Limits, 'maxLoss'>): string;
394
+ /**
395
+ * A readable summary of a grant for the approval screen, one line each.
396
+ * Show the builder by its registered name, not the grant's free-text label.
397
+ *
398
+ * @experimental
399
+ */
400
+ export declare function summarizeGrant(grant: SessionGrant): string[];