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