odin-connect 2.0.0 → 2.0.1
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/MIGRATION-2.0.md +327 -0
- package/dist/index.d.ts +393 -63
- package/dist/index.js +3 -2
- package/package.json +4 -2
- package/readme.md +454 -103
package/dist/index.d.ts
CHANGED
|
@@ -152,6 +152,29 @@ type PaginatedResponse<T> = {
|
|
|
152
152
|
page: number;
|
|
153
153
|
limit: number;
|
|
154
154
|
};
|
|
155
|
+
/** Identity proof signed by the Odin authorize page (see `/connect/verify`). */
|
|
156
|
+
type ConnectProof = {
|
|
157
|
+
payload: string;
|
|
158
|
+
signature: string;
|
|
159
|
+
delegation: string | null;
|
|
160
|
+
publicKey: string | null;
|
|
161
|
+
};
|
|
162
|
+
type VerifyConnectRequest = ConnectProof & {
|
|
163
|
+
audience: string;
|
|
164
|
+
nonce: string;
|
|
165
|
+
issue_jwt: boolean;
|
|
166
|
+
/**
|
|
167
|
+
* base64 of the session key's signature over
|
|
168
|
+
* UTF-8("odin-connect-verify:v1\n" + payload): proves the caller holds the
|
|
169
|
+
* key the proof is bound to (`sk`).
|
|
170
|
+
*/
|
|
171
|
+
client_signature: string;
|
|
172
|
+
};
|
|
173
|
+
type VerifyConnectResponse = {
|
|
174
|
+
principal: string;
|
|
175
|
+
username: string | null;
|
|
176
|
+
jwt: string | null;
|
|
177
|
+
};
|
|
155
178
|
declare class OdinApiClient {
|
|
156
179
|
private _apiKey;
|
|
157
180
|
private _httpClient;
|
|
@@ -196,6 +219,12 @@ declare class OdinApiClient {
|
|
|
196
219
|
followers: number;
|
|
197
220
|
following: number;
|
|
198
221
|
}>;
|
|
222
|
+
/**
|
|
223
|
+
* Verify a connect identity proof with odin-api. Resolves with the verified
|
|
224
|
+
* principal (and a JWT when `issue_jwt`); rejects with the API's message on
|
|
225
|
+
* any non-2xx response.
|
|
226
|
+
*/
|
|
227
|
+
verifyConnect(body: VerifyConnectRequest): Promise<VerifyConnectResponse>;
|
|
199
228
|
set apiKey(key: string | null);
|
|
200
229
|
get apiKey(): string | null;
|
|
201
230
|
}
|
|
@@ -216,34 +245,46 @@ declare class WindowClient {
|
|
|
216
245
|
|
|
217
246
|
/**
|
|
218
247
|
* How the SDK reaches the Odin authorize pages, for `connect()` and actions.
|
|
219
|
-
* - `"
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
*
|
|
225
|
-
*
|
|
248
|
+
* - `"auto"` (default): `"redirect"` inside a wallet in-app browser or app
|
|
249
|
+
* webview (see `isInAppBrowser()`), else `"popup"`.
|
|
250
|
+
* - `"popup"`: always `window.open` + `postMessage`.
|
|
251
|
+
* - `"redirect"`: always navigate this tab to Odin and back. For wallet
|
|
252
|
+
* in-app browsers (OKX) that open popups without `window.opener`. The
|
|
253
|
+
* returned promise never settles because the page unloads; on the next
|
|
254
|
+
* load `ready()` verifies and applies the result to `OdinConnect.state`.
|
|
226
255
|
*/
|
|
227
256
|
type ConnectMode = "popup" | "redirect" | "auto";
|
|
228
257
|
interface PendingRedirect {
|
|
229
258
|
state: string;
|
|
230
259
|
/** Authorize path, e.g. `/authorize/connect` or `/authorize/buy`. */
|
|
231
260
|
path: string;
|
|
232
|
-
/**
|
|
233
|
-
successMessage?: string;
|
|
234
|
-
/** connect only */
|
|
261
|
+
/** connect only: the session key's JSON (never leaves this browser). */
|
|
235
262
|
sessionKey?: string;
|
|
236
263
|
/** connect only */
|
|
237
264
|
requires_delegation?: boolean;
|
|
238
|
-
/**
|
|
265
|
+
/** connect only */
|
|
266
|
+
requires_api?: boolean;
|
|
267
|
+
/** connect only: canister targets the delegation may be scoped to. */
|
|
268
|
+
targets?: string[];
|
|
269
|
+
/** The request's `input` (no secrets; bigints kept), for `state.request`. */
|
|
270
|
+
input?: unknown;
|
|
271
|
+
/** App data handed back as `state.request.returnState`. */
|
|
239
272
|
returnState?: unknown;
|
|
273
|
+
/** `Date.now()` when the redirect started; abandoned entries expire. */
|
|
274
|
+
createdAt?: number;
|
|
275
|
+
/**
|
|
276
|
+
* The page URL that started the request, with its query, without its
|
|
277
|
+
* fragment. Its query is restored once the result is consumed. Missing in
|
|
278
|
+
* entries saved by older SDK versions.
|
|
279
|
+
*/
|
|
280
|
+
returnHref?: string;
|
|
240
281
|
}
|
|
241
282
|
/**
|
|
242
|
-
* Per-call option shared by `connect()` and every action.
|
|
243
|
-
*
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
283
|
+
* Per-call option shared by `connect()` and every action. Comes back as
|
|
284
|
+
* `state.request.returnState` in both modes; in redirect mode the page
|
|
285
|
+
* unloads, so anything the app needs to resume (current step, token,
|
|
286
|
+
* amounts) can ride along here. Must be JSON-serializable in redirect mode;
|
|
287
|
+
* bigints are preserved. Never sent to Odin.
|
|
247
288
|
*/
|
|
248
289
|
interface RedirectCallOptions {
|
|
249
290
|
returnState?: unknown;
|
|
@@ -251,6 +292,8 @@ interface RedirectCallOptions {
|
|
|
251
292
|
interface RedirectResult {
|
|
252
293
|
path: string;
|
|
253
294
|
message: unknown;
|
|
295
|
+
/** Extra result data (e.g. `{ block_index }` for icrc_approve). */
|
|
296
|
+
detail?: unknown;
|
|
254
297
|
state: string;
|
|
255
298
|
}
|
|
256
299
|
declare class PendingRedirectStorage {
|
|
@@ -265,6 +308,8 @@ declare class PendingRedirectStorage {
|
|
|
265
308
|
peek(): PendingRedirect | null;
|
|
266
309
|
/** Load and delete (single-use). */
|
|
267
310
|
take(): PendingRedirect | null;
|
|
311
|
+
/** Delete the pending request if it is older than `maxAgeMs` (or undated). */
|
|
312
|
+
dropIfOlderThan(maxAgeMs: number): void;
|
|
268
313
|
}
|
|
269
314
|
/**
|
|
270
315
|
* Owns the mode and the pending request for one `OdinConnect` instance, so
|
|
@@ -279,17 +324,25 @@ declare class RedirectClient {
|
|
|
279
324
|
get useRedirect(): boolean;
|
|
280
325
|
/**
|
|
281
326
|
* Save the pending request, then navigate this tab to `url` with
|
|
282
|
-
* `return_url` + `state`.
|
|
283
|
-
*
|
|
284
|
-
*
|
|
327
|
+
* `return_url` + `state`. `state` is the request's `request_id` (already on
|
|
328
|
+
* `url`), so Odin can sign it as the nonce. The returned promise never
|
|
329
|
+
* settles (the page unloads) unless sessionStorage is unusable, in which
|
|
330
|
+
* case it rejects before navigating.
|
|
331
|
+
*
|
|
332
|
+
* If the user presses Back on Odin, browsers often restore this page from
|
|
333
|
+
* the back-forward cache instead of reloading it, so nothing would ever
|
|
334
|
+
* settle the pending request. `onBack` is called once in that case (a
|
|
335
|
+
* persisted `pageshow` with no redirect result in the URL), after the
|
|
336
|
+
* pending entry (and its session secret) has been dropped.
|
|
285
337
|
*/
|
|
286
|
-
start<T>(url: URL, pending: Omit<PendingRedirect, "state"
|
|
287
|
-
/** Path of the pending request when the URL carries its result. */
|
|
288
|
-
pendingPath(): string | null;
|
|
338
|
+
start<T>(url: URL, pending: Omit<PendingRedirect, "state" | "createdAt" | "returnHref">, state: string, onBack?: () => void): Promise<T>;
|
|
289
339
|
/**
|
|
290
|
-
* Consume the URL's redirect result and its pending request.
|
|
291
|
-
*
|
|
292
|
-
* the
|
|
340
|
+
* Consume the URL's redirect result and its pending request. The fragment
|
|
341
|
+
* is removed from the address bar (and the history entry), and when the
|
|
342
|
+
* result matches, the page's original query string is restored. Returns
|
|
343
|
+
* null when the URL carries no result (and deletes a pending request
|
|
344
|
+
* abandoned for more than `PENDING_REDIRECT_MAX_AGE_MS`); throws when the
|
|
345
|
+
* result does not match the pending request (stale, foreign or replayed).
|
|
293
346
|
*/
|
|
294
347
|
consume(): {
|
|
295
348
|
result: RedirectResult;
|
|
@@ -297,6 +350,185 @@ declare class RedirectClient {
|
|
|
297
350
|
} | null;
|
|
298
351
|
}
|
|
299
352
|
|
|
353
|
+
/**
|
|
354
|
+
* The one place results land. Popup messages, redirect results (read by
|
|
355
|
+
* `ready()` on the next page load) and the stored session all go through
|
|
356
|
+
* `reduce()`; the app reads `OdinConnect.state` and `subscribe()`s to it.
|
|
357
|
+
* Every change produces a new frozen snapshot, so `getState()` returns the
|
|
358
|
+
* same object until something changes (what `useSyncExternalStore` needs).
|
|
359
|
+
*/
|
|
360
|
+
|
|
361
|
+
/** Authorize actions started from `ConnectedUser` (or `OdinConnect.odin`). */
|
|
362
|
+
type OdinAction = "buy" | "sell" | "transfer" | "swap" | "add_liquidity" | "remove_liquidity" | "icrc_approve" | "create_token";
|
|
363
|
+
/**
|
|
364
|
+
* Extra data Odin returns with an action result. `icrc_approve` carries
|
|
365
|
+
* `block_index`, the ledger block of the approval as a decimal string. Odin
|
|
366
|
+
* sends no ICRC-2 memo (the Odin canister rejects approvals that carry one).
|
|
367
|
+
* Other actions carry nothing today.
|
|
368
|
+
*/
|
|
369
|
+
type OdinActionDetail = {
|
|
370
|
+
/** `icrc_approve`: ledger block index of the approval (decimal string). */
|
|
371
|
+
block_index?: string;
|
|
372
|
+
[key: string]: unknown;
|
|
373
|
+
};
|
|
374
|
+
/**
|
|
375
|
+
* - `"pending"`: started; the popup is open or the tab is on its way to Odin.
|
|
376
|
+
* - `"success"`: Odin confirmed it (a connect also passed every verification).
|
|
377
|
+
* - `"rejected"`: the user declined in the Odin page, went back to the app
|
|
378
|
+
* from an Odin error screen, or closed the popup without answering (then
|
|
379
|
+
* `error` holds an `OdinRejectReason`).
|
|
380
|
+
* - `"failed"`: anything else (popup blocked, Odin reported an error, ...).
|
|
381
|
+
* - `"unverified"`: connect only; Odin's answer could not be verified, the
|
|
382
|
+
* user is not connected and nothing was stored.
|
|
383
|
+
*/
|
|
384
|
+
type OdinRequestStatus = "pending" | "success" | "rejected" | "failed" | "unverified";
|
|
385
|
+
/**
|
|
386
|
+
* What the app asked for, per action (the arguments of `connect()` /
|
|
387
|
+
* `user.<action>()` without `returnState`). Amounts stay `bigint`, also
|
|
388
|
+
* across a redirect. `create_token` holds the uploaded image URL (set once
|
|
389
|
+
* the upload finished), never the `File`.
|
|
390
|
+
*/
|
|
391
|
+
type OdinRequestInput = {
|
|
392
|
+
connect: {
|
|
393
|
+
requires_api: boolean;
|
|
394
|
+
requires_delegation: boolean;
|
|
395
|
+
targets: string[];
|
|
396
|
+
};
|
|
397
|
+
buy: {
|
|
398
|
+
token: string;
|
|
399
|
+
btcAmount: bigint;
|
|
400
|
+
};
|
|
401
|
+
sell: {
|
|
402
|
+
token: string;
|
|
403
|
+
tokenAmount: bigint;
|
|
404
|
+
};
|
|
405
|
+
transfer: {
|
|
406
|
+
token: string;
|
|
407
|
+
amount: bigint;
|
|
408
|
+
destination: string;
|
|
409
|
+
};
|
|
410
|
+
swap: {
|
|
411
|
+
fromToken: string;
|
|
412
|
+
toToken: string;
|
|
413
|
+
fromAmount: bigint;
|
|
414
|
+
};
|
|
415
|
+
add_liquidity: {
|
|
416
|
+
token: string;
|
|
417
|
+
btcAmount: bigint;
|
|
418
|
+
};
|
|
419
|
+
remove_liquidity: {
|
|
420
|
+
token: string;
|
|
421
|
+
lpAmount: bigint;
|
|
422
|
+
};
|
|
423
|
+
icrc_approve: {
|
|
424
|
+
token: string;
|
|
425
|
+
spender: string;
|
|
426
|
+
amount: bigint;
|
|
427
|
+
};
|
|
428
|
+
create_token: {
|
|
429
|
+
name: string;
|
|
430
|
+
ticker: string;
|
|
431
|
+
image?: string;
|
|
432
|
+
vanity_ticker?: string;
|
|
433
|
+
description?: string;
|
|
434
|
+
website?: string;
|
|
435
|
+
twitter?: string;
|
|
436
|
+
telegram?: string;
|
|
437
|
+
buy?: bigint;
|
|
438
|
+
discount?: string;
|
|
439
|
+
};
|
|
440
|
+
};
|
|
441
|
+
/** The latest request, discriminated by `action` (narrows `input`). */
|
|
442
|
+
type OdinRequestState = {
|
|
443
|
+
[A in keyof OdinRequestInput]: {
|
|
444
|
+
/** The request's `request_id`. */
|
|
445
|
+
id: string;
|
|
446
|
+
action: A;
|
|
447
|
+
status: OdinRequestStatus;
|
|
448
|
+
input: OdinRequestInput[A];
|
|
449
|
+
/** Set on a successful action that returned data (e.g. icrc_approve). */
|
|
450
|
+
detail?: OdinActionDetail;
|
|
451
|
+
/** Whatever was passed as `returnState` to the call, in both modes. */
|
|
452
|
+
returnState?: unknown;
|
|
453
|
+
/**
|
|
454
|
+
* Why it is `"failed"` or `"unverified"`. On `"rejected"`: the reason
|
|
455
|
+
* code (`OdinRejectReason`) when the user went back to the app from an
|
|
456
|
+
* Odin error screen or closed the popup; absent when the user simply
|
|
457
|
+
* declined.
|
|
458
|
+
*/
|
|
459
|
+
error?: string;
|
|
460
|
+
};
|
|
461
|
+
}[keyof OdinRequestInput];
|
|
462
|
+
/**
|
|
463
|
+
* Codes Odin sends (v=2) when the user goes back to the app from an
|
|
464
|
+
* authorize error screen. They land in `state.request.error` with status
|
|
465
|
+
* `"rejected"`. Odin may add codes; treat unknown ones as a plain rejection.
|
|
466
|
+
* - `untrusted_origin`: a delegation target canister does not list the app's
|
|
467
|
+
* origin in its ICRC-28 trusted origins.
|
|
468
|
+
* - `invalid_targets`: a `targets` canister id is invalid or not allowed.
|
|
469
|
+
* - `no_targets`: a delegation was requested without target canisters.
|
|
470
|
+
* - `invalid_session_key`: the session public key is missing or malformed.
|
|
471
|
+
* - `missing_request_id`: no request id, or no identity, to bind the proof.
|
|
472
|
+
* - `unsupported_identity`: the user's wallet cannot sign the identity proof.
|
|
473
|
+
* - `no_action`: Odin has no authorize page for this action.
|
|
474
|
+
*
|
|
475
|
+
* Set by the SDK itself:
|
|
476
|
+
* - `popup_closed` (popup mode): the popup was closed without an answer.
|
|
477
|
+
* - `navigated_back` (redirect mode): the user pressed Back on Odin and the
|
|
478
|
+
* browser restored this page from its back-forward cache, with no result.
|
|
479
|
+
*/
|
|
480
|
+
type OdinRejectReason = "untrusted_origin" | "invalid_targets" | "no_targets" | "invalid_session_key" | "missing_request_id" | "unsupported_identity" | "no_action" | "popup_closed" | "navigated_back";
|
|
481
|
+
type OdinState = {
|
|
482
|
+
/** `"initializing"` until `ready()` has restored the session. */
|
|
483
|
+
status: "initializing" | "ready";
|
|
484
|
+
user: ConnectedUser | null;
|
|
485
|
+
/** The latest request; a new request replaces it. */
|
|
486
|
+
request: OdinRequestState | null;
|
|
487
|
+
};
|
|
488
|
+
type OdinStateListener = (state: OdinState) => void;
|
|
489
|
+
type StateEvent =
|
|
490
|
+
/** `ready()` finished: the restored user and a redirect outcome, if any. */
|
|
491
|
+
{
|
|
492
|
+
type: "ready";
|
|
493
|
+
user: ConnectedUser | null;
|
|
494
|
+
request: OdinRequestState | null;
|
|
495
|
+
}
|
|
496
|
+
/** A request started (or was updated before settling, same id). */
|
|
497
|
+
| {
|
|
498
|
+
type: "request";
|
|
499
|
+
request: OdinRequestState;
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* A request settled; ignored when a newer request replaced it (or
|
|
503
|
+
* `disconnect()` cleared it): neither `request` nor `user` changes then.
|
|
504
|
+
*/
|
|
505
|
+
| {
|
|
506
|
+
type: "settle";
|
|
507
|
+
id: string;
|
|
508
|
+
status: Exclude<OdinRequestStatus, "pending">;
|
|
509
|
+
detail?: OdinActionDetail;
|
|
510
|
+
error?: string;
|
|
511
|
+
/** connect success: the verified user. */
|
|
512
|
+
user?: ConnectedUser;
|
|
513
|
+
} | {
|
|
514
|
+
type: "disconnect";
|
|
515
|
+
};
|
|
516
|
+
/**
|
|
517
|
+
* The state before `ready()` restored anything, and always on a server. A
|
|
518
|
+
* frozen constant: pass it as `useSyncExternalStore`'s `getServerSnapshot`
|
|
519
|
+
* (`() => INITIAL_ODIN_STATE`, or `odin.getServerState`) so hydration
|
|
520
|
+
* renders the same "initializing" state the server did.
|
|
521
|
+
*/
|
|
522
|
+
declare const INITIAL_ODIN_STATE: OdinState;
|
|
523
|
+
/** Holds one `OdinConnect`'s state and its listeners. */
|
|
524
|
+
declare class StateStore {
|
|
525
|
+
private _state;
|
|
526
|
+
private _listeners;
|
|
527
|
+
get state(): OdinState;
|
|
528
|
+
dispatch(event: StateEvent): void;
|
|
529
|
+
subscribe(listener: OdinStateListener): () => void;
|
|
530
|
+
}
|
|
531
|
+
|
|
300
532
|
interface SellOptions extends RedirectCallOptions {
|
|
301
533
|
principal: string;
|
|
302
534
|
token: string;
|
|
@@ -353,19 +585,34 @@ declare class OdinCanisterClient {
|
|
|
353
585
|
private _appInfo;
|
|
354
586
|
private _api;
|
|
355
587
|
private _redirect;
|
|
588
|
+
private _store;
|
|
356
589
|
origin: string;
|
|
357
|
-
constructor(windowClient: WindowClient, apiClient: OdinApiClient, appInfo: AppInitOptions, origin: string, redirectClient?: RedirectClient | null);
|
|
590
|
+
constructor(windowClient: WindowClient, apiClient: OdinApiClient, appInfo: AppInitOptions, origin: string, redirectClient?: RedirectClient | null, store?: StateStore | null);
|
|
358
591
|
get appInfo(): AppInitOptions;
|
|
592
|
+
/** Authorize URL with the protocol flag and a fresh `request_id`. */
|
|
359
593
|
private createUrl;
|
|
594
|
+
private startRequest;
|
|
595
|
+
/** Mark the request failed and return the error to reject with. */
|
|
596
|
+
private fail;
|
|
597
|
+
/**
|
|
598
|
+
* Every action: record the request as pending, then either navigate the
|
|
599
|
+
* tab (redirect mode: `ready()` applies the outcome on the next load and
|
|
600
|
+
* the promise never settles) or open a popup and settle on its message.
|
|
601
|
+
*/
|
|
360
602
|
private baseAction;
|
|
361
603
|
sell({ token, tokenAmount, principal, returnState }: SellOptions): Promise<boolean>;
|
|
362
604
|
buy({ principal, token, btcAmount, returnState }: BuyOptions): Promise<boolean>;
|
|
363
605
|
transfer({ principal, token, amount, destination, returnState, }: TransferOptions): Promise<boolean>;
|
|
364
606
|
addLiquidity({ principal, btcAmount, token, returnState, }: AddLiquidityOptions): Promise<boolean>;
|
|
365
607
|
removeLiquidity({ principal, lpAmount, token, returnState, }: RemoveLiquidityOptions): Promise<boolean>;
|
|
366
|
-
swap({ principal, fromToken
|
|
608
|
+
swap({ principal, fromToken, toToken, fromAmount, returnState, }: SwapOptions): Promise<boolean>;
|
|
367
609
|
icrcApprove({ principal, token, spender, amount, returnState, }: IcrcApproveOptions): Promise<boolean>;
|
|
368
|
-
|
|
610
|
+
/**
|
|
611
|
+
* Validates, uploads the image, then authorizes like any action. The
|
|
612
|
+
* request is pending from the start (`input.image` is set once the upload
|
|
613
|
+
* finished); a validation or upload error marks it failed.
|
|
614
|
+
*/
|
|
615
|
+
createToken({ image, returnState, principal, ...params }: CreateTokenParams): Promise<boolean>;
|
|
369
616
|
}
|
|
370
617
|
|
|
371
618
|
declare class ConnectedUser {
|
|
@@ -457,31 +704,12 @@ interface AppInitOptions {
|
|
|
457
704
|
slug?: string;
|
|
458
705
|
lang?: OdinLang;
|
|
459
706
|
/**
|
|
460
|
-
* How `connect()` and every action reach Odin: `"
|
|
461
|
-
*
|
|
707
|
+
* How `connect()` and every action reach Odin: `"auto"` (default:
|
|
708
|
+
* redirect in wallet in-app browsers, popup elsewhere), `"popup"` or
|
|
709
|
+
* `"redirect"`. See `ConnectMode`.
|
|
462
710
|
*/
|
|
463
711
|
mode?: ConnectMode;
|
|
464
712
|
}
|
|
465
|
-
/** Authorize actions that can come back through `handleRedirectResult()`. */
|
|
466
|
-
type OdinAction = "buy" | "sell" | "transfer" | "swap" | "add_liquidity" | "remove_liquidity" | "icrc_approve" | "create_token";
|
|
467
|
-
/**
|
|
468
|
-
* What `handleRedirectResult()` found in the URL after a redirect.
|
|
469
|
-
* `returnState` is whatever was passed as `returnState` to the call that
|
|
470
|
-
* redirected (undefined if none).
|
|
471
|
-
*/
|
|
472
|
-
type OdinRedirectResult<ReturnState = unknown> = ({
|
|
473
|
-
action: "connect";
|
|
474
|
-
status: "connected";
|
|
475
|
-
user: ConnectedUser;
|
|
476
|
-
} | {
|
|
477
|
-
action: "connect";
|
|
478
|
-
status: "rejected";
|
|
479
|
-
} | {
|
|
480
|
-
action: OdinAction;
|
|
481
|
-
status: "success" | "failed";
|
|
482
|
-
}) & {
|
|
483
|
-
returnState: ReturnState | undefined;
|
|
484
|
-
};
|
|
485
713
|
interface BaseConnectOptions extends RedirectCallOptions {
|
|
486
714
|
open?: WindowClientSettings;
|
|
487
715
|
requires_api?: boolean;
|
|
@@ -493,16 +721,22 @@ interface ConnectOptionsWithDelegation extends BaseConnectOptions {
|
|
|
493
721
|
interface ConnectOptionsWithoutDelegation extends BaseConnectOptions {
|
|
494
722
|
requires_delegation?: false;
|
|
495
723
|
targets?: never;
|
|
496
|
-
session_key?: never;
|
|
497
724
|
}
|
|
498
725
|
type ConnectOptions = ConnectOptionsWithDelegation | ConnectOptionsWithoutDelegation;
|
|
499
726
|
declare class Connect {
|
|
727
|
+
/**
|
|
728
|
+
* The state on a server and before `ready()` restored anything; the same
|
|
729
|
+
* frozen object every time (`INITIAL_ODIN_STATE`).
|
|
730
|
+
*/
|
|
731
|
+
static readonly serverState: OdinState;
|
|
500
732
|
private _appInfo;
|
|
501
733
|
private _api;
|
|
502
734
|
private _window;
|
|
503
735
|
private _odin;
|
|
504
736
|
private _storage;
|
|
505
737
|
private _redirect;
|
|
738
|
+
private _store;
|
|
739
|
+
private _ready;
|
|
506
740
|
constructor(appInfo?: Partial<AppInitOptions>);
|
|
507
741
|
private createUrl;
|
|
508
742
|
get origin(): string;
|
|
@@ -513,30 +747,126 @@ declare class Connect {
|
|
|
513
747
|
set mode(value: ConnectMode);
|
|
514
748
|
get appInfo(): AppInitOptions | null;
|
|
515
749
|
get slug(): string;
|
|
750
|
+
/**
|
|
751
|
+
* Restore the stored session and, when this page load returned from a
|
|
752
|
+
* redirect-mode `connect()` or action, consume and verify that result and
|
|
753
|
+
* apply it as `state.request`. Resolves with the current state once that
|
|
754
|
+
* is done. Idempotent: started by the constructor, every call shares the
|
|
755
|
+
* same work (also across `OdinConnect` instances with the same slug and
|
|
756
|
+
* env, e.g. React StrictMode), and it never rejects. A stale or foreign
|
|
757
|
+
* result in the URL is dropped (`request: null`, fragment removed).
|
|
758
|
+
*
|
|
759
|
+
* Odin returns to the page path without its query string; the original
|
|
760
|
+
* query is put back here, so read URL query state after `ready()`.
|
|
761
|
+
*
|
|
762
|
+
* On a server (no `window`) nothing is restored and the state stays
|
|
763
|
+
* `"initializing"`.
|
|
764
|
+
*/
|
|
765
|
+
ready(): Promise<OdinState>;
|
|
766
|
+
/** Current state snapshot; a new object after every change. */
|
|
767
|
+
get state(): OdinState;
|
|
768
|
+
/**
|
|
769
|
+
* Same as `state`, as a bound function for
|
|
770
|
+
* `useSyncExternalStore(odin.subscribe, odin.getState)`.
|
|
771
|
+
*/
|
|
772
|
+
getState: () => OdinState;
|
|
773
|
+
/**
|
|
774
|
+
* `Connect.serverState`, as a bound function: the third argument of
|
|
775
|
+
* `useSyncExternalStore(odin.subscribe, odin.getState, odin.getServerState)`
|
|
776
|
+
* so hydration matches the server render ("initializing").
|
|
777
|
+
*/
|
|
778
|
+
getServerState: () => OdinState;
|
|
779
|
+
/**
|
|
780
|
+
* Call `listener` with the new state after every change (not on
|
|
781
|
+
* subscribe: read `state` after `await ready()`). Returns the unsubscribe
|
|
782
|
+
* function. Bound, so it can be passed around as is.
|
|
783
|
+
*/
|
|
784
|
+
subscribe: (listener: OdinStateListener) => (() => void);
|
|
785
|
+
/** The connected user, or null (`state.user`). */
|
|
786
|
+
get user(): ConnectedUser | null;
|
|
787
|
+
/** Synchronous up to the redirect read, so the URL is read right away. */
|
|
788
|
+
private initialize;
|
|
789
|
+
private finishReady;
|
|
790
|
+
/**
|
|
791
|
+
* Start a connect. `state.request` becomes a pending `"connect"` request
|
|
792
|
+
* right away and settles as `"success"` (and `state.user` is set),
|
|
793
|
+
* `"rejected"`, `"failed"` (popup blocked) or `"unverified"`.
|
|
794
|
+
*
|
|
795
|
+
* Only this request's popup is listened to (`event.source`). If a newer
|
|
796
|
+
* `connect()` / action replaced it as `state.request`, or `disconnect()`
|
|
797
|
+
* ran, its success leaves `state.user` and the stored session alone.
|
|
798
|
+
*
|
|
799
|
+
* The returned promise is kept for 1.6.0 code in popup mode: it resolves
|
|
800
|
+
* with the user or rejects. In redirect mode the tab navigates away and it
|
|
801
|
+
* never settles; render from `subscribe()` / `state` to support both.
|
|
802
|
+
* Ignoring it never causes an unhandled rejection.
|
|
803
|
+
*/
|
|
516
804
|
connect({ open, requires_api, requires_delegation, targets, returnState, }?: ConnectOptions | undefined): Promise<ConnectedUser>;
|
|
805
|
+
/** Settle a request as failed/unverified; returns the error to reject with. */
|
|
806
|
+
private settleError;
|
|
517
807
|
private createConnectUrl;
|
|
518
|
-
private connectByRedirect;
|
|
519
808
|
/**
|
|
520
|
-
*
|
|
521
|
-
* the
|
|
522
|
-
* no
|
|
523
|
-
*
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
809
|
+
* This page load's redirect outcome, shared per `slug:env`: reads the URL
|
|
810
|
+
* the first time a fragment is seen, else reuses the cached read. Null when
|
|
811
|
+
* there is no result in the URL and none was read recently. Synchronous up
|
|
812
|
+
* to the returned promise, so concurrent callers share one read.
|
|
813
|
+
*/
|
|
814
|
+
private redirectOutcome;
|
|
815
|
+
/** Bind a shared verified connection to this instance (its API key). */
|
|
816
|
+
private bindConnection;
|
|
817
|
+
private get redirectCacheKey();
|
|
818
|
+
/**
|
|
819
|
+
* Consume the URL's result once and turn it into the settled request; a
|
|
820
|
+
* connect result is verified and persisted first. Throws for a stale or
|
|
821
|
+
* foreign result.
|
|
822
|
+
*/
|
|
823
|
+
private readRedirectResult;
|
|
824
|
+
/**
|
|
825
|
+
* A superseded verified connection, for the 1.6.0 promise only: its own API
|
|
826
|
+
* client, so its JWT never becomes this instance's API key.
|
|
827
|
+
*/
|
|
828
|
+
private detachedUser;
|
|
829
|
+
private get apiEnv();
|
|
830
|
+
private get disconnectGeneration();
|
|
831
|
+
/** Every local and odin-api check; no side effects. */
|
|
832
|
+
private verifyConnection;
|
|
833
|
+
/**
|
|
834
|
+
* Replace whatever session was stored before (another user's JWT or
|
|
835
|
+
* delegation must not survive a re-connect), then store this one.
|
|
528
836
|
*/
|
|
529
|
-
|
|
530
|
-
private completeConnection;
|
|
837
|
+
private persistConnection;
|
|
531
838
|
get api(): OdinApiClient;
|
|
532
839
|
get odin(): OdinCanisterClient;
|
|
533
840
|
get currentEnv(): "legacy" | "dev" | "prod" | "local";
|
|
841
|
+
/**
|
|
842
|
+
* @deprecated Since 2.0.0: use `(await odin.ready()).user`, or `state` /
|
|
843
|
+
* `subscribe()`. Kept with its 1.6.0 behavior: synchronous, returns the
|
|
844
|
+
* stored session's user (`state.user` once `ready()` finished). It does not
|
|
845
|
+
* wait for or apply a redirect result; `ready()` does.
|
|
846
|
+
*/
|
|
534
847
|
restoreSession(): ConnectedUser | null;
|
|
848
|
+
private loadStoredSession;
|
|
849
|
+
/** Forget the stored session; `state.user` and `state.request` become null. */
|
|
535
850
|
disconnect(): void;
|
|
536
851
|
isSessionValid(): boolean;
|
|
537
852
|
hello(): void;
|
|
538
853
|
}
|
|
539
854
|
|
|
855
|
+
/**
|
|
856
|
+
* Local checks on a connect result before the SDK trusts it. The identity
|
|
857
|
+
* proof's signature is verified by odin-api (`POST /connect/verify`), which
|
|
858
|
+
* also handles canister-signature roots (Internet Identity); these checks
|
|
859
|
+
* bind the result to this request, this origin and this session key. The
|
|
860
|
+
* proof names the session public key (`sk`) and odin-api only redeems it
|
|
861
|
+
* with a `client_signature` from that key, so a proof read from a return
|
|
862
|
+
* URL is useless without the secret, which never leaves the SDK.
|
|
863
|
+
*/
|
|
864
|
+
|
|
865
|
+
/** Thrown for any connect result the SDK will not accept. */
|
|
866
|
+
declare class ConnectVerificationError extends Error {
|
|
867
|
+
constructor(reason: string);
|
|
868
|
+
}
|
|
869
|
+
|
|
540
870
|
/**
|
|
541
871
|
* Best-effort detection of wallet in-app browsers and app webviews, where
|
|
542
872
|
* `window.open` targets load as a detached page with no `window.opener`, so a
|
|
@@ -595,4 +925,4 @@ interface SessionData {
|
|
|
595
925
|
jwt: string | null;
|
|
596
926
|
}
|
|
597
927
|
|
|
598
|
-
export { type Achievement as OdinAchievement, type AchievementCategory as OdinAchievementCategory, type OdinAction, type Activity as OdinActivity, type Balance as OdinBalance, type BaseToken as OdinBaseToken, Connect as OdinConnect, type ConnectMode as OdinConnectMode, ConnectedUser as OdinConnectedUser, type OdinLang, type RedirectCallOptions as OdinRedirectCallOptions, type
|
|
928
|
+
export { INITIAL_ODIN_STATE, type Achievement as OdinAchievement, type AchievementCategory as OdinAchievementCategory, type OdinAction, type OdinActionDetail, type Activity as OdinActivity, type Balance as OdinBalance, type BaseToken as OdinBaseToken, Connect as OdinConnect, type ConnectMode as OdinConnectMode, ConnectVerificationError as OdinConnectVerificationError, ConnectedUser as OdinConnectedUser, type OdinLang, type RedirectCallOptions as OdinRedirectCallOptions, type OdinRejectReason, type OdinRequestInput, type OdinRequestState, type OdinRequestStatus, type OdinState, type OdinStateListener, type Token as OdinToken, type TokenWithBalance as OdinTokenWithBalance, type Transaction as OdinTransaction, type User as OdinUser, index as OdinUtils, type SessionData, isInAppBrowser };
|