@layerswap/wallet-imtbl-passport 1.7.0 → 2.0.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ export const name = 'Immutable Passport';
2
+ export const id = 'imtblPassport';
package/dist/esm/index.js CHANGED
@@ -1,26 +1,28 @@
1
- 'use client';
2
- import { jsx as _jsx } from "react/jsx-runtime";
3
- import { ImtblPassportProviderWrapper } from "./ImtblPassportProvider";
4
- import { AppSettings } from "@layerswap/widget/internal";
1
+ import { imtblPassportService } from "./service/ImtblPassportService";
2
+ import { id } from "./constants";
3
+ // The literal id in the return type lets `defineWalletDescriptor` in
4
+ // `@layerswap/wallets` verify it matches the descriptor id at compile time.
5
5
  export function createImmutablePassportProvider(config = {}) {
6
6
  const { imtblPassportConfig } = config;
7
- const WrapperComponent = ({ children }) => {
8
- return (_jsx(ImtblPassportProviderWrapper, { imtblPassportConfig: imtblPassportConfig, children: children }));
7
+ const init = (_ctx) => {
8
+ imtblPassportService.setConfig(imtblPassportConfig);
9
+ imtblPassportService.ensureEvmConnected().catch(() => { });
10
+ // No-op disposer; init is idempotent across remounts.
9
11
  };
10
12
  return {
11
- id: "imtblPassport",
12
- wrapper: WrapperComponent,
13
+ id,
14
+ init,
13
15
  };
14
16
  }
15
- export { ImtblRedirect } from "./ImtblPassportProvider";
16
17
  /**
17
- * @deprecated Use createImmutablePassportProvider() instead. This export will be removed in a future version.
18
- * Note: This uses undefined config which requires configuration to be provided elsewhere.
18
+ * Run the Passport OAuth redirect handler. Call this from your redirect page
19
+ * once the route has loaded (e.g. inside a `useEffect`). Replaces the old
20
+ * `<ImtblRedirect />` React component.
19
21
  */
20
- export const ImtblPassportProvider = {
21
- id: "imtblPassport",
22
- wrapper: ({ children }) => {
23
- const imtblPassportConfig = AppSettings.ImtblPassportConfig;
24
- return (_jsx(ImtblPassportProviderWrapper, { imtblPassportConfig: imtblPassportConfig, children: children }));
25
- }
26
- };
22
+ export async function imtblPassportLoginCallback(config) {
23
+ if (config)
24
+ imtblPassportService.setConfig(config);
25
+ await imtblPassportService.loginCallback();
26
+ }
27
+ export { imtblPassportService } from "./service/ImtblPassportService";
28
+ export { useImtblPassportStore } from "./service/imtblPassportStore";
@@ -0,0 +1,85 @@
1
+ import { useImtblPassportStore } from './imtblPassportStore';
2
+ export class ImtblPassportService {
3
+ constructor() {
4
+ this._evmConnected = false;
5
+ }
6
+ setConfig(config) {
7
+ this._config = config;
8
+ }
9
+ hasInstance() {
10
+ return useImtblPassportStore.getState().instance !== undefined;
11
+ }
12
+ getInstance() {
13
+ const instance = useImtblPassportStore.getState().instance;
14
+ if (!instance) {
15
+ throw new Error('Passport auth instance requested before initialization');
16
+ }
17
+ return instance;
18
+ }
19
+ async initialize(config) {
20
+ const cfg = config ?? this._config;
21
+ if (this._initializing)
22
+ return this._initializing;
23
+ if (this.hasInstance())
24
+ return;
25
+ this._initializing = this._doInitialize(cfg).finally(() => {
26
+ this._initializing = undefined;
27
+ });
28
+ return this._initializing;
29
+ }
30
+ async _doInitialize(config) {
31
+ const { publishableKey, clientId, redirectUri, logoutRedirectUri } = config || {};
32
+ // publishableKey is not required by the Auth SDK — it is only sniffed
33
+ // below to pick the sandbox vs production Passport domain.
34
+ if (!clientId || !redirectUri || !logoutRedirectUri)
35
+ return;
36
+ const { Auth } = await import('@imtbl/auth');
37
+ const isSandbox = publishableKey?.includes('pk_imapik-test') === true;
38
+ const passportDomain = isSandbox
39
+ ? 'https://passport.sandbox.immutable.com'
40
+ : 'https://passport.immutable.com';
41
+ const instance = new Auth({
42
+ clientId,
43
+ redirectUri,
44
+ popupRedirectUri: redirectUri,
45
+ logoutRedirectUri,
46
+ logoutMode: 'silent',
47
+ audience: 'platform_api',
48
+ scope: 'openid offline_access email transact',
49
+ authenticationDomain: 'https://auth.immutable.com',
50
+ passportDomain,
51
+ });
52
+ useImtblPassportStore.getState()._setInstance(instance);
53
+ }
54
+ async ensureEvmConnected() {
55
+ const clientId = this._config?.clientId;
56
+ if (this._evmConnected || !clientId)
57
+ return;
58
+ this._evmConnected = true;
59
+ if (!this.hasInstance())
60
+ await this.initialize();
61
+ const auth = this.getInstance();
62
+ const { connectWallet, IMMUTABLE_ZKEVM_MAINNET_CHAIN, IMMUTABLE_ZKEVM_TESTNET_CHAIN } = await import('@imtbl/wallet');
63
+ const isSandbox = this._config?.publishableKey?.includes('pk_imapik-test') === true;
64
+ const selectedChain = isSandbox ? IMMUTABLE_ZKEVM_TESTNET_CHAIN : IMMUTABLE_ZKEVM_MAINNET_CHAIN;
65
+ await connectWallet({
66
+ clientId,
67
+ chains: [selectedChain],
68
+ initialChainId: selectedChain.chainId,
69
+ getUser: async (forceRefresh, options) => {
70
+ if (forceRefresh)
71
+ return auth.forceUserRefresh();
72
+ if (options?.silent)
73
+ return auth.getUser();
74
+ return auth.getUserOrLogin();
75
+ },
76
+ announceProvider: true, // EIP-6963
77
+ });
78
+ }
79
+ async loginCallback() {
80
+ if (!this.hasInstance())
81
+ await this.initialize();
82
+ await this.getInstance().loginCallback();
83
+ }
84
+ }
85
+ export const imtblPassportService = new ImtblPassportService();
@@ -0,0 +1,6 @@
1
+ import { create } from 'zustand';
2
+ export const useImtblPassportStore = create()((set) => ({
3
+ ready: false,
4
+ instance: undefined,
5
+ _setInstance: (instance) => set({ instance, ready: !!instance }),
6
+ }));