@layerswap/wallet-imtbl-passport 1.7.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/constants.js +2 -0
- package/dist/esm/index.js +20 -18
- package/dist/esm/service/ImtblPassportService.js +85 -0
- package/dist/esm/service/imtblPassportStore.js +6 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/constants.d.ts +3 -0
- package/dist/types/constants.d.ts.map +1 -0
- package/dist/types/index.d.ts +11 -6
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/service/ImtblPassportService.d.ts +16 -0
- package/dist/types/service/ImtblPassportService.d.ts.map +1 -0
- package/dist/types/service/imtblPassportStore.d.ts +8 -0
- package/dist/types/service/imtblPassportStore.d.ts.map +1 -0
- package/package.json +8 -12
- package/dist/esm/ImtblPassportProvider.js +0 -43
- package/dist/types/ImtblPassportProvider.d.ts +0 -14
- package/dist/types/ImtblPassportProvider.d.ts.map +0 -1
package/dist/esm/index.js
CHANGED
|
@@ -1,26 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
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
|
|
8
|
-
|
|
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
|
|
12
|
-
|
|
13
|
+
id,
|
|
14
|
+
init,
|
|
13
15
|
};
|
|
14
16
|
}
|
|
15
|
-
export { ImtblRedirect } from "./ImtblPassportProvider";
|
|
16
17
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
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
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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();
|