odin-connect 1.7.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 +419 -68
- package/dist/index.js +3 -2
- package/package.json +4 -2
- package/readme.md +464 -83
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,40 +245,71 @@ 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;
|
|
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`. */
|
|
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;
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
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.
|
|
288
|
+
*/
|
|
289
|
+
interface RedirectCallOptions {
|
|
290
|
+
returnState?: unknown;
|
|
238
291
|
}
|
|
239
292
|
interface RedirectResult {
|
|
240
293
|
path: string;
|
|
241
294
|
message: unknown;
|
|
295
|
+
/** Extra result data (e.g. `{ block_index }` for icrc_approve). */
|
|
296
|
+
detail?: unknown;
|
|
242
297
|
state: string;
|
|
243
298
|
}
|
|
244
299
|
declare class PendingRedirectStorage {
|
|
245
300
|
private _key;
|
|
246
301
|
constructor(slug: string, env: string);
|
|
247
|
-
/**
|
|
302
|
+
/**
|
|
303
|
+
* Save and read back; false when sessionStorage is unusable. Throws when
|
|
304
|
+
* `returnState` is not serializable (e.g. circular references).
|
|
305
|
+
*/
|
|
248
306
|
save(pending: PendingRedirect): boolean;
|
|
249
307
|
/** Load without deleting. */
|
|
250
308
|
peek(): PendingRedirect | null;
|
|
251
309
|
/** Load and delete (single-use). */
|
|
252
310
|
take(): PendingRedirect | null;
|
|
311
|
+
/** Delete the pending request if it is older than `maxAgeMs` (or undated). */
|
|
312
|
+
dropIfOlderThan(maxAgeMs: number): void;
|
|
253
313
|
}
|
|
254
314
|
/**
|
|
255
315
|
* Owns the mode and the pending request for one `OdinConnect` instance, so
|
|
@@ -264,17 +324,25 @@ declare class RedirectClient {
|
|
|
264
324
|
get useRedirect(): boolean;
|
|
265
325
|
/**
|
|
266
326
|
* Save the pending request, then navigate this tab to `url` with
|
|
267
|
-
* `return_url` + `state`.
|
|
268
|
-
*
|
|
269
|
-
*
|
|
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.
|
|
270
337
|
*/
|
|
271
|
-
start<T>(url: URL, pending: Omit<PendingRedirect, "state"
|
|
272
|
-
/** Path of the pending request when the URL carries its result. */
|
|
273
|
-
pendingPath(): string | null;
|
|
338
|
+
start<T>(url: URL, pending: Omit<PendingRedirect, "state" | "createdAt" | "returnHref">, state: string, onBack?: () => void): Promise<T>;
|
|
274
339
|
/**
|
|
275
|
-
* Consume the URL's redirect result and its pending request.
|
|
276
|
-
*
|
|
277
|
-
* 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).
|
|
278
346
|
*/
|
|
279
347
|
consume(): {
|
|
280
348
|
result: RedirectResult;
|
|
@@ -282,45 +350,224 @@ declare class RedirectClient {
|
|
|
282
350
|
} | null;
|
|
283
351
|
}
|
|
284
352
|
|
|
285
|
-
|
|
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
|
+
|
|
532
|
+
interface SellOptions extends RedirectCallOptions {
|
|
286
533
|
principal: string;
|
|
287
534
|
token: string;
|
|
288
535
|
tokenAmount: bigint;
|
|
289
536
|
}
|
|
290
|
-
interface BuyOptions {
|
|
537
|
+
interface BuyOptions extends RedirectCallOptions {
|
|
291
538
|
principal: string;
|
|
292
539
|
token: string;
|
|
293
540
|
btcAmount: bigint;
|
|
294
541
|
}
|
|
295
|
-
interface TransferOptions {
|
|
542
|
+
interface TransferOptions extends RedirectCallOptions {
|
|
296
543
|
principal: string;
|
|
297
544
|
token: string;
|
|
298
545
|
amount: bigint;
|
|
299
546
|
destination: string;
|
|
300
547
|
}
|
|
301
|
-
interface AddLiquidityOptions {
|
|
548
|
+
interface AddLiquidityOptions extends RedirectCallOptions {
|
|
302
549
|
principal: string;
|
|
303
550
|
btcAmount: bigint;
|
|
304
551
|
token: string;
|
|
305
552
|
}
|
|
306
|
-
interface RemoveLiquidityOptions {
|
|
553
|
+
interface RemoveLiquidityOptions extends RedirectCallOptions {
|
|
307
554
|
principal: string;
|
|
308
555
|
lpAmount: bigint;
|
|
309
556
|
token: string;
|
|
310
557
|
}
|
|
311
|
-
interface SwapOptions {
|
|
558
|
+
interface SwapOptions extends RedirectCallOptions {
|
|
312
559
|
principal: string;
|
|
313
560
|
fromToken: string;
|
|
314
561
|
toToken: string;
|
|
315
562
|
fromAmount: bigint;
|
|
316
563
|
}
|
|
317
|
-
interface IcrcApproveOptions {
|
|
564
|
+
interface IcrcApproveOptions extends RedirectCallOptions {
|
|
318
565
|
principal: string;
|
|
319
566
|
token: string;
|
|
320
567
|
spender: string;
|
|
321
568
|
amount: bigint;
|
|
322
569
|
}
|
|
323
|
-
interface CreateTokenParams {
|
|
570
|
+
interface CreateTokenParams extends RedirectCallOptions {
|
|
324
571
|
principal: string;
|
|
325
572
|
name: string;
|
|
326
573
|
ticker: string;
|
|
@@ -338,19 +585,34 @@ declare class OdinCanisterClient {
|
|
|
338
585
|
private _appInfo;
|
|
339
586
|
private _api;
|
|
340
587
|
private _redirect;
|
|
588
|
+
private _store;
|
|
341
589
|
origin: string;
|
|
342
|
-
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);
|
|
343
591
|
get appInfo(): AppInitOptions;
|
|
592
|
+
/** Authorize URL with the protocol flag and a fresh `request_id`. */
|
|
344
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
|
+
*/
|
|
345
602
|
private baseAction;
|
|
346
|
-
sell({ token, tokenAmount, principal }: SellOptions): Promise<boolean>;
|
|
347
|
-
buy({ principal, token, btcAmount }: BuyOptions): Promise<boolean>;
|
|
348
|
-
transfer({ principal, token, amount, destination }: TransferOptions): Promise<boolean>;
|
|
349
|
-
addLiquidity({ principal, btcAmount, token }: AddLiquidityOptions): Promise<boolean>;
|
|
350
|
-
removeLiquidity({ principal, lpAmount, token }: RemoveLiquidityOptions): Promise<boolean>;
|
|
351
|
-
swap({ principal, fromToken
|
|
352
|
-
icrcApprove({ principal, token, spender, amount }: IcrcApproveOptions): Promise<boolean>;
|
|
353
|
-
|
|
603
|
+
sell({ token, tokenAmount, principal, returnState }: SellOptions): Promise<boolean>;
|
|
604
|
+
buy({ principal, token, btcAmount, returnState }: BuyOptions): Promise<boolean>;
|
|
605
|
+
transfer({ principal, token, amount, destination, returnState, }: TransferOptions): Promise<boolean>;
|
|
606
|
+
addLiquidity({ principal, btcAmount, token, returnState, }: AddLiquidityOptions): Promise<boolean>;
|
|
607
|
+
removeLiquidity({ principal, lpAmount, token, returnState, }: RemoveLiquidityOptions): Promise<boolean>;
|
|
608
|
+
swap({ principal, fromToken, toToken, fromAmount, returnState, }: SwapOptions): Promise<boolean>;
|
|
609
|
+
icrcApprove({ principal, token, spender, amount, returnState, }: IcrcApproveOptions): Promise<boolean>;
|
|
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>;
|
|
354
616
|
}
|
|
355
617
|
|
|
356
618
|
declare class ConnectedUser {
|
|
@@ -442,26 +704,13 @@ interface AppInitOptions {
|
|
|
442
704
|
slug?: string;
|
|
443
705
|
lang?: OdinLang;
|
|
444
706
|
/**
|
|
445
|
-
* How `connect()` and every action reach Odin: `"
|
|
446
|
-
*
|
|
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`.
|
|
447
710
|
*/
|
|
448
711
|
mode?: ConnectMode;
|
|
449
712
|
}
|
|
450
|
-
|
|
451
|
-
type OdinAction = "buy" | "sell" | "transfer" | "swap" | "add_liquidity" | "remove_liquidity" | "icrc_approve" | "create_token";
|
|
452
|
-
/** What `handleRedirectResult()` found in the URL after a redirect. */
|
|
453
|
-
type OdinRedirectResult = {
|
|
454
|
-
action: "connect";
|
|
455
|
-
status: "connected";
|
|
456
|
-
user: ConnectedUser;
|
|
457
|
-
} | {
|
|
458
|
-
action: "connect";
|
|
459
|
-
status: "rejected";
|
|
460
|
-
} | {
|
|
461
|
-
action: OdinAction;
|
|
462
|
-
status: "success" | "failed";
|
|
463
|
-
};
|
|
464
|
-
interface BaseConnectOptions {
|
|
713
|
+
interface BaseConnectOptions extends RedirectCallOptions {
|
|
465
714
|
open?: WindowClientSettings;
|
|
466
715
|
requires_api?: boolean;
|
|
467
716
|
}
|
|
@@ -472,16 +721,22 @@ interface ConnectOptionsWithDelegation extends BaseConnectOptions {
|
|
|
472
721
|
interface ConnectOptionsWithoutDelegation extends BaseConnectOptions {
|
|
473
722
|
requires_delegation?: false;
|
|
474
723
|
targets?: never;
|
|
475
|
-
session_key?: never;
|
|
476
724
|
}
|
|
477
725
|
type ConnectOptions = ConnectOptionsWithDelegation | ConnectOptionsWithoutDelegation;
|
|
478
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;
|
|
479
732
|
private _appInfo;
|
|
480
733
|
private _api;
|
|
481
734
|
private _window;
|
|
482
735
|
private _odin;
|
|
483
736
|
private _storage;
|
|
484
737
|
private _redirect;
|
|
738
|
+
private _store;
|
|
739
|
+
private _ready;
|
|
485
740
|
constructor(appInfo?: Partial<AppInitOptions>);
|
|
486
741
|
private createUrl;
|
|
487
742
|
get origin(): string;
|
|
@@ -492,30 +747,126 @@ declare class Connect {
|
|
|
492
747
|
set mode(value: ConnectMode);
|
|
493
748
|
get appInfo(): AppInitOptions | null;
|
|
494
749
|
get slug(): string;
|
|
495
|
-
connect({ open, requires_api, requires_delegation, targets, }?: ConnectOptions | undefined): Promise<ConnectedUser>;
|
|
496
|
-
private createConnectUrl;
|
|
497
|
-
private connectByRedirect;
|
|
498
750
|
/**
|
|
499
|
-
*
|
|
500
|
-
*
|
|
501
|
-
*
|
|
502
|
-
*
|
|
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.
|
|
503
798
|
*
|
|
504
|
-
*
|
|
505
|
-
*
|
|
506
|
-
*
|
|
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.
|
|
507
803
|
*/
|
|
508
|
-
|
|
509
|
-
|
|
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;
|
|
807
|
+
private createConnectUrl;
|
|
808
|
+
/**
|
|
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.
|
|
836
|
+
*/
|
|
837
|
+
private persistConnection;
|
|
510
838
|
get api(): OdinApiClient;
|
|
511
839
|
get odin(): OdinCanisterClient;
|
|
512
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
|
+
*/
|
|
513
847
|
restoreSession(): ConnectedUser | null;
|
|
848
|
+
private loadStoredSession;
|
|
849
|
+
/** Forget the stored session; `state.user` and `state.request` become null. */
|
|
514
850
|
disconnect(): void;
|
|
515
851
|
isSessionValid(): boolean;
|
|
516
852
|
hello(): void;
|
|
517
853
|
}
|
|
518
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
|
+
|
|
519
870
|
/**
|
|
520
871
|
* Best-effort detection of wallet in-app browsers and app webviews, where
|
|
521
872
|
* `window.open` targets load as a detached page with no `window.opener`, so a
|
|
@@ -574,4 +925,4 @@ interface SessionData {
|
|
|
574
925
|
jwt: string | null;
|
|
575
926
|
}
|
|
576
927
|
|
|
577
|
-
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
|
|
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 };
|