@paradex/sdk 0.9.0 → 0.11.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 +54 -4
- package/dist/builder-codes.d.ts +106 -0
- package/dist/builder-codes.js +101 -0
- package/dist/config.d.ts +2 -1
- package/dist/config.js +2 -1
- package/dist/index.d.ts +8 -3
- package/dist/index.js +5 -1
- package/dist/session-accounts.js +10 -0
- package/dist/session-auth.d.ts +27 -0
- package/dist/session-auth.js +58 -18
- package/dist/session-connector.d.ts +35 -1
- package/dist/session-connector.js +72 -1
- package/dist/session-typed-data.d.ts +144 -16
- package/dist/session-typed-data.js +229 -68
- package/package.json +1 -1
|
@@ -2,9 +2,11 @@ import { hashTypedData } from './eip712.js';
|
|
|
2
2
|
import { assertRecordVersion, memorySessionStorage, SessionKey, } from './session-key.js';
|
|
3
3
|
import { assertEoaSignature, } from './session-owner-signer.js';
|
|
4
4
|
import { defaultApiUrl, fetchTransport, SessionApiError, } from './session-transport.js';
|
|
5
|
-
import { assertGrantActive, buildSessionGrantEip712, canonicalFelt, createSessionGrant, grantAccounts, nowSeconds, SessionGrantError, } from './session-typed-data.js';
|
|
5
|
+
import { assertGrantActive, buildBuilderApprovalEip712, buildSessionGrantEip712, canonicalFelt, createBuilderApproval, createSessionGrant, grantAccounts, nowSeconds, SessionGrantError, } from './session-typed-data.js';
|
|
6
6
|
export const SESSIONS_PATH = '/v2/sessions';
|
|
7
7
|
export const EVM_SUBACCOUNTS_PATH = '/v2/onboarding/subaccounts';
|
|
8
|
+
export const BUILDER_APPROVALS_PATH = '/v2/builders/approvals';
|
|
9
|
+
export const SYSTEM_TIME_PATH = '/v1/system/time';
|
|
8
10
|
/** Whether `open()` finished registering this record. */
|
|
9
11
|
function isRegistered(record) {
|
|
10
12
|
return (record.grant != null &&
|
|
@@ -201,6 +203,75 @@ export class SessionConnector {
|
|
|
201
203
|
throw error;
|
|
202
204
|
}
|
|
203
205
|
}
|
|
206
|
+
/**
|
|
207
|
+
* Has the owner approve a builder's fee without opening a session (one
|
|
208
|
+
* wallet prompt) and registers the approval with
|
|
209
|
+
* `POST /v2/builders/approvals`, which needs no JWT. The approval covers
|
|
210
|
+
* the account and any `subaccounts` it lists until `builder.expiresAt`.
|
|
211
|
+
* The owner revokes it with `DELETE /v2/builders/approvals/{builder_account}`.
|
|
212
|
+
*
|
|
213
|
+
* `issuedAt` is the server's time in whole seconds, and the server refuses
|
|
214
|
+
* an approval issued in the same second as the builder's last revoke or
|
|
215
|
+
* approval (including one a session grant made) with
|
|
216
|
+
* `BUILDER_APPROVAL_STALE`. So after either, wait for the next server
|
|
217
|
+
* second, or the owner signs an approval that is refused.
|
|
218
|
+
*
|
|
219
|
+
* @experimental
|
|
220
|
+
*/
|
|
221
|
+
async approveBuilder(params) {
|
|
222
|
+
const approval = createBuilderApproval({
|
|
223
|
+
...params,
|
|
224
|
+
paradexChain: this.paradexChain,
|
|
225
|
+
environment: this.environment,
|
|
226
|
+
issuedAt: await this.serverTime(),
|
|
227
|
+
});
|
|
228
|
+
await this.assertSubaccountsOwned(approval);
|
|
229
|
+
const signatureChainId = await this.owner.getChainId();
|
|
230
|
+
const typedData = buildBuilderApprovalEip712(approval, signatureChainId);
|
|
231
|
+
const ownerSignature = assertEoaSignature(await this.owner.signTypedData({
|
|
232
|
+
typedData,
|
|
233
|
+
digest: hashTypedData(typedData),
|
|
234
|
+
}));
|
|
235
|
+
const response = await this.transport({
|
|
236
|
+
method: 'POST',
|
|
237
|
+
url: `${this.apiUrl}${BUILDER_APPROVALS_PATH}`,
|
|
238
|
+
headers: { 'Content-Type': 'application/json' },
|
|
239
|
+
body: JSON.stringify({
|
|
240
|
+
approval,
|
|
241
|
+
signature: ownerSignature,
|
|
242
|
+
signature_type: 'eip712',
|
|
243
|
+
signature_chain_id: signatureChainId,
|
|
244
|
+
}),
|
|
245
|
+
});
|
|
246
|
+
if (response.status < 200 || response.status >= 300) {
|
|
247
|
+
throw new SessionApiError(response.status, response.body);
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
approval,
|
|
251
|
+
ownerSignature,
|
|
252
|
+
signatureChainId,
|
|
253
|
+
response: response.body,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
/** The server's clock in whole seconds, from `GET /v1/system/time`. */
|
|
257
|
+
async serverTime() {
|
|
258
|
+
const response = await this.transport({
|
|
259
|
+
method: 'GET',
|
|
260
|
+
url: `${this.apiUrl}${SYSTEM_TIME_PATH}`,
|
|
261
|
+
headers: {},
|
|
262
|
+
});
|
|
263
|
+
if (response.status < 200 || response.status >= 300) {
|
|
264
|
+
throw new SessionApiError(response.status, response.body);
|
|
265
|
+
}
|
|
266
|
+
const ms = Number(response.body?.server_time);
|
|
267
|
+
if (!Number.isSafeInteger(ms) || ms <= 0) {
|
|
268
|
+
throw new SessionApiError(response.status, {
|
|
269
|
+
error: 'INVALID_RESPONSE',
|
|
270
|
+
message: `GET ${SYSTEM_TIME_PATH} returned no server_time`,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
return Math.floor(ms / 1000);
|
|
274
|
+
}
|
|
204
275
|
/**
|
|
205
276
|
* Restores registered sessions from storage for this environment. Expired
|
|
206
277
|
* sessions and sessions that never registered are deleted; records that
|
|
@@ -33,7 +33,9 @@ export declare const SESSION_TTL: {
|
|
|
33
33
|
export declare const MAX_PASSKEY_CONCURRENT_SESSIONS = 5;
|
|
34
34
|
/** A grant, mint or passkey ceiling lists at most this many subaccounts. */
|
|
35
35
|
export declare const MAX_GRANT_SUBACCOUNTS = 32;
|
|
36
|
-
export type SessionGrantErrorCode = 'INVALID_FIELD' | 'UNKNOWN_SCOPE' | 'RESERVED_SCOPE' | 'TTL_EXCEEDED'
|
|
36
|
+
export type SessionGrantErrorCode = 'INVALID_FIELD' | 'UNKNOWN_SCOPE' | 'RESERVED_SCOPE' | 'TTL_EXCEEDED'
|
|
37
|
+
/** @deprecated No longer thrown: builder grants follow the normal limit rules. */
|
|
38
|
+
| '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
39
|
export declare class SessionGrantError extends Error {
|
|
38
40
|
readonly code: SessionGrantErrorCode;
|
|
39
41
|
constructor(code: SessionGrantErrorCode, message: string);
|
|
@@ -141,25 +143,80 @@ export interface PasskeyGrant {
|
|
|
141
143
|
readonly issuedAt: number;
|
|
142
144
|
readonly expiresAt: number;
|
|
143
145
|
}
|
|
144
|
-
/**
|
|
145
|
-
|
|
146
|
+
/**
|
|
147
|
+
* An EVM owner's approval of a builder's fee, outside any session grant.
|
|
148
|
+
* Signed with EIP-712 only and registered with
|
|
149
|
+
* `POST /v2/builders/approvals`.
|
|
150
|
+
*
|
|
151
|
+
* @experimental
|
|
152
|
+
*/
|
|
153
|
+
export interface BuilderApproval {
|
|
146
154
|
readonly paradexChain: string;
|
|
147
155
|
readonly environment: SessionEnvironment;
|
|
148
156
|
readonly account: string;
|
|
149
|
-
|
|
157
|
+
/** `expiresAt` is required here: there is no session to follow. */
|
|
158
|
+
readonly builder: Builder;
|
|
159
|
+
readonly subaccounts: readonly string[];
|
|
160
|
+
readonly nonce: string;
|
|
161
|
+
/** Seconds, by the server's clock: the server refuses one in its future. */
|
|
162
|
+
readonly issuedAt: number;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* What a passkey signs to open one session; its SNIP-12 hash is the WebAuthn
|
|
166
|
+
* challenge ({@link sessionMintChallenge}).
|
|
167
|
+
*
|
|
168
|
+
* It names neither the account nor the passkey, so a client may let the user
|
|
169
|
+
* pick any passkey they hold (an empty `allowCredentials`). The server finds
|
|
170
|
+
* the passkey by the assertion's credential id, and its grant names the
|
|
171
|
+
* account.
|
|
172
|
+
*
|
|
173
|
+
* `limits` set to {@link CEILING_LIMITS} (every amount `""`, `markets: []`)
|
|
174
|
+
* takes the passkey's ceiling: the server applies its value for each limit it
|
|
175
|
+
* enforces and `"none"` for the rest. Stated limits must fit the ceiling.
|
|
176
|
+
* `expiresAt` {@link LONGEST_SESSION} (`0`) asks for the longest session the
|
|
177
|
+
* passkey allows: the ceiling's `maxSessionTtl` from the server's clock, or
|
|
178
|
+
* until the passkey expires if that is sooner. A stated `expiresAt` later
|
|
179
|
+
* than the passkey's expiry is accepted, and the session ends with the
|
|
180
|
+
* passkey.
|
|
181
|
+
*
|
|
182
|
+
* @experimental
|
|
183
|
+
*/
|
|
184
|
+
export interface SessionMint {
|
|
185
|
+
readonly paradexChain: string;
|
|
186
|
+
readonly environment: SessionEnvironment;
|
|
150
187
|
readonly sessionKey: string;
|
|
151
188
|
readonly scopes: readonly SessionScope[];
|
|
152
189
|
readonly limits: Limits;
|
|
153
190
|
readonly subaccounts: readonly string[];
|
|
191
|
+
/** From `POST /v2/passkeys/challenge`; the server refuses one it did not issue. */
|
|
154
192
|
readonly nonce: string;
|
|
155
193
|
readonly expiresAt: number;
|
|
156
194
|
}
|
|
195
|
+
/** The account a `SessionMint`'s SNIP-12 message hash is taken for: a mint names no account. */
|
|
196
|
+
export declare const MINT_HASH_ACCOUNT = "0x0";
|
|
197
|
+
/** A `SessionMint` `expiresAt` asking for the longest session the passkey allows. */
|
|
198
|
+
export declare const LONGEST_SESSION = 0;
|
|
199
|
+
/**
|
|
200
|
+
* The limits a `SessionMint` states to take its passkey's ceiling: every
|
|
201
|
+
* amount empty and no markets.
|
|
202
|
+
*/
|
|
203
|
+
export declare const CEILING_LIMITS: Limits;
|
|
204
|
+
/** Whether `limits` are {@link CEILING_LIMITS}: no limit stated at all. */
|
|
205
|
+
export declare function isCeilingLimits(limits: Limits): boolean;
|
|
157
206
|
export declare const SESSION_GRANT_EIP712_TYPES: Eip712Types;
|
|
207
|
+
export declare const BUILDER_APPROVAL_EIP712_TYPES: Eip712Types;
|
|
158
208
|
export declare const PASSKEY_GRANT_EIP712_TYPES: Eip712Types;
|
|
159
209
|
export declare const SESSION_MINT_EIP712_TYPES: Eip712Types;
|
|
160
210
|
export declare const SESSION_GRANT_SNIP12_TYPES: Starknet.TypedData['types'];
|
|
161
211
|
export declare const PASSKEY_GRANT_SNIP12_TYPES: Starknet.TypedData['types'];
|
|
162
212
|
export declare const SESSION_MINT_SNIP12_TYPES: Starknet.TypedData['types'];
|
|
213
|
+
/**
|
|
214
|
+
* A builder account in canonical `0x` form, refusing anything but a
|
|
215
|
+
* non-zero felt address. Orders and the Builder Codes calls share it.
|
|
216
|
+
*
|
|
217
|
+
* @internal
|
|
218
|
+
*/
|
|
219
|
+
export declare function builderAddress(value: string, name?: string): string;
|
|
163
220
|
/** Whether a ceiling market entry covers a mint's market entry. */
|
|
164
221
|
export declare function marketCovers(pattern: string, market: string): boolean;
|
|
165
222
|
/**
|
|
@@ -176,10 +233,11 @@ export declare function validateScopes(scopes: readonly string[], field?: string
|
|
|
176
233
|
* non-empty list, or `["*"]` alone. A limit the server does not enforce
|
|
177
234
|
* must be `"none"` (`markets`: `["*"]`).
|
|
178
235
|
*
|
|
179
|
-
*
|
|
180
|
-
*
|
|
236
|
+
* Builder grants and builder passkeys follow the same rules as any other grant.
|
|
237
|
+
*
|
|
238
|
+
* @param _builderGrant ignored; kept so existing callers still compile.
|
|
181
239
|
*/
|
|
182
|
-
export declare function validateLimits(limits: Limits,
|
|
240
|
+
export declare function validateLimits(limits: Limits, _builderGrant: boolean, field?: string, options?: LimitOptions): void;
|
|
183
241
|
export type SessionType = 'browser' | 'api';
|
|
184
242
|
/**
|
|
185
243
|
* Longest lifetime a grant with these scopes can request. The server measures
|
|
@@ -211,6 +269,18 @@ export interface ValidateSessionGrantOptions extends LimitOptions {
|
|
|
211
269
|
* before the owner is asked to sign.
|
|
212
270
|
*/
|
|
213
271
|
export declare function validateSessionGrant(grant: SessionGrant, options?: ValidateSessionGrantOptions): void;
|
|
272
|
+
/** Longest a builder approval may live after its `issuedAt`. */
|
|
273
|
+
export declare const BUILDER_APPROVAL_TTL_MAX: number;
|
|
274
|
+
/**
|
|
275
|
+
* Checks a builder approval against the server's rules: a builder other than
|
|
276
|
+
* the account, a `maxFeeRate` above 0 with at most 8 decimal places, and a
|
|
277
|
+
* required `expiresAt` at most
|
|
278
|
+
* {@link BUILDER_APPROVAL_TTL_MAX} after `issuedAt`. Throws a
|
|
279
|
+
* {@link SessionGrantError}.
|
|
280
|
+
*
|
|
281
|
+
* @experimental
|
|
282
|
+
*/
|
|
283
|
+
export declare function validateBuilderApproval(approval: BuilderApproval): void;
|
|
214
284
|
export interface ValidatePasskeyGrantOptions extends LimitOptions {
|
|
215
285
|
/**
|
|
216
286
|
* Whether the server accepts subaccounts in a passkey ceiling. Defaults to
|
|
@@ -226,6 +296,13 @@ export interface ValidatePasskeyGrantOptions extends LimitOptions {
|
|
|
226
296
|
readonly clockSkewSeconds?: number;
|
|
227
297
|
}
|
|
228
298
|
export declare function validatePasskeyGrant(grant: PasskeyGrant, options?: ValidatePasskeyGrantOptions): void;
|
|
299
|
+
/**
|
|
300
|
+
* Checks a `SessionMint`'s format. Its limits are either all stated (as for
|
|
301
|
+
* a grant) or {@link CEILING_LIMITS}; `expiresAt` may be
|
|
302
|
+
* {@link LONGEST_SESSION}. A mint names no account, so whether its
|
|
303
|
+
* subaccounts leave out the passkey's account is checked by
|
|
304
|
+
* {@link assertMintWithinCeiling}.
|
|
305
|
+
*/
|
|
229
306
|
export declare function validateSessionMint(mint: SessionMint, options?: LimitOptions): void;
|
|
230
307
|
export interface AssertMintOptions extends LimitOptions {
|
|
231
308
|
/**
|
|
@@ -237,14 +314,30 @@ export interface AssertMintOptions extends LimitOptions {
|
|
|
237
314
|
readonly clockSkewSeconds?: number;
|
|
238
315
|
}
|
|
239
316
|
/**
|
|
240
|
-
* Checks a `SessionMint` against the `PasskeyGrant`
|
|
241
|
-
* server does: same
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
* be
|
|
245
|
-
* `"none"`
|
|
246
|
-
*
|
|
247
|
-
*
|
|
317
|
+
* Checks a `SessionMint` against the `PasskeyGrant` of the passkey that signs
|
|
318
|
+
* it, as the server does: same chain and environment, and scopes within the
|
|
319
|
+
* ceiling. A mint names no account or passkey; the server finds the grant by
|
|
320
|
+
* the assertion's credential id. Only limits the server enforces are
|
|
321
|
+
* compared: each must be numeric and at or below a numeric ceiling (anything
|
|
322
|
+
* goes under a `"none"` ceiling), and markets must be within the ceiling's
|
|
323
|
+
* unless it allows `["*"]`. Unenforced limits are `"none"` on the mint and
|
|
324
|
+
* skipped, so passkeys registered with numeric limits keep working. A mint
|
|
325
|
+
* stating {@link CEILING_LIMITS} takes the ceiling's and fits by definition.
|
|
326
|
+
* A builder passkey's mints are checked like any other. Subaccounts must not
|
|
327
|
+
* list the passkey's account.
|
|
328
|
+
*
|
|
329
|
+
* The check holds only when `grant`'s passkey signs. With an empty
|
|
330
|
+
* `allowCredentials` the user may pick another passkey, on another account
|
|
331
|
+
* or with another ceiling, and the server checks the mint against that one.
|
|
332
|
+
* Pin `allowCredentials` to `grant.passkey.credentialId`, or compare the
|
|
333
|
+
* account in the server's response with `grant.account` and revoke the
|
|
334
|
+
* session if they differ.
|
|
335
|
+
*
|
|
336
|
+
* Expiry: the passkey must not have expired. `expiresAt`
|
|
337
|
+
* {@link LONGEST_SESSION} always fits. Otherwise it must be in the future
|
|
338
|
+
* and at most the ceiling's `maxSessionTtl` from `now` (less the clock-skew
|
|
339
|
+
* allowance). It may be later than the passkey's expiry: the server then
|
|
340
|
+
* ends the session when the passkey expires.
|
|
248
341
|
*
|
|
249
342
|
* @experimental
|
|
250
343
|
*/
|
|
@@ -294,6 +387,32 @@ export declare function nowSeconds(): number;
|
|
|
294
387
|
* @experimental
|
|
295
388
|
*/
|
|
296
389
|
export declare function createSessionGrant(params: CreateSessionGrantParams): SessionGrant;
|
|
390
|
+
export interface CreateBuilderApprovalParams {
|
|
391
|
+
readonly paradexChain: string;
|
|
392
|
+
readonly environment: SessionEnvironment;
|
|
393
|
+
readonly account: string;
|
|
394
|
+
/**
|
|
395
|
+
* The builder and its fee cap. `expiresAt` (seconds) defaults to
|
|
396
|
+
* `issuedAt + ttlSeconds`, as on a grant's builder.
|
|
397
|
+
*/
|
|
398
|
+
readonly builder: {
|
|
399
|
+
readonly account: string;
|
|
400
|
+
readonly maxFeeRate: string;
|
|
401
|
+
readonly expiresAt?: number;
|
|
402
|
+
};
|
|
403
|
+
/** Seconds, from the server's clock (`GET /v1/system/time`). */
|
|
404
|
+
readonly issuedAt: number;
|
|
405
|
+
/** Defaults to {@link BUILDER_APPROVAL_TTL_MAX}. */
|
|
406
|
+
readonly ttlSeconds?: number;
|
|
407
|
+
readonly subaccounts?: readonly string[];
|
|
408
|
+
readonly nonce?: string;
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Builds a canonical, validated {@link BuilderApproval}.
|
|
412
|
+
*
|
|
413
|
+
* @experimental
|
|
414
|
+
*/
|
|
415
|
+
export declare function createBuilderApproval(params: CreateBuilderApprovalParams): BuilderApproval;
|
|
297
416
|
/** Every account a grant reaches: the anchor first, then its sub-accounts. */
|
|
298
417
|
export declare function grantAccounts(grant: Pick<SessionGrant, 'account' | 'subaccounts'>): readonly string[];
|
|
299
418
|
/**
|
|
@@ -323,6 +442,14 @@ export declare function buildPasskeyGrantSnip12(grant: PasskeyGrant, options?: V
|
|
|
323
442
|
export declare function buildSessionMintEip712(mint: SessionMint, signatureChainId: number | string | bigint, options?: LimitOptions): Eip712TypedData;
|
|
324
443
|
/** @experimental */
|
|
325
444
|
export declare function buildSessionMintSnip12(mint: SessionMint, options?: LimitOptions): Starknet.TypedData;
|
|
445
|
+
/**
|
|
446
|
+
* EIP-712 `BuilderApproval`, signed by the account's EVM owner on the
|
|
447
|
+
* wallet's current chain. There is no SNIP-12 form: the server accepts
|
|
448
|
+
* builder approvals from EVM owners only.
|
|
449
|
+
*
|
|
450
|
+
* @experimental
|
|
451
|
+
*/
|
|
452
|
+
export declare function buildBuilderApprovalEip712(approval: BuilderApproval, signatureChainId: number | string | bigint): Eip712TypedData;
|
|
326
453
|
/** EIP-712 digest (`0x1901 ‖ domain ‖ struct`) as 0x-prefixed 32-byte hex. */
|
|
327
454
|
export declare function hashEip712(typedData: Eip712TypedData): Hex;
|
|
328
455
|
/**
|
|
@@ -332,7 +459,8 @@ export declare function hashEip712(typedData: Eip712TypedData): Hex;
|
|
|
332
459
|
export declare function hashSnip12(typedData: Starknet.TypedData, account: string): Hex;
|
|
333
460
|
/**
|
|
334
461
|
* The WebAuthn challenge for a `SessionMint`: the 32-byte SNIP-12 message
|
|
335
|
-
* hash
|
|
462
|
+
* hash, taken for {@link MINT_HASH_ACCOUNT} (`0x0`) since a mint names no
|
|
463
|
+
* account, base64url-encoded without padding.
|
|
336
464
|
*/
|
|
337
465
|
export declare function sessionMintChallenge(mint: SessionMint, options?: LimitOptions): string;
|
|
338
466
|
/** The wording shown to owners about what a session can lose. */
|