@getpara/react-sdk-lite 2.0.0-alpha.67 → 2.0.0-alpha.68

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.
Files changed (29) hide show
  1. package/dist/modal/components/Account/AccountWalletSelect.js +15 -2
  2. package/dist/modal/components/ChainSwitch/ChainSwitch.js +8 -3
  3. package/dist/modal/components/ExternalWalletNetworkSelectStep/ExternalWalletNetworkSelectStep.js +2 -2
  4. package/dist/modal/components/ExternalWalletStep/ExternalWalletStep.d.ts +2 -1
  5. package/dist/modal/components/ExternalWalletStep/ExternalWalletStep.js +36 -29
  6. package/dist/modal/components/ExternalWallets/ExternalWallets.js +2 -2
  7. package/dist/modal/components/IFrameStep/IFrameStep.js +8 -6
  8. package/dist/modal/components/OAuth/FarcasterOAuthStep.js +6 -5
  9. package/dist/modal/components/OAuth/TelegramOAuthStep.js +6 -5
  10. package/dist/modal/components/WalletSelectOld/WalletSelectOld.js +17 -4
  11. package/dist/modal/hooks/useFarcasterLogin.js +8 -1
  12. package/dist/modal/hooks/useTelegramLogin.js +4 -0
  13. package/dist/modal/stores/modal/useModalStore.d.ts +1 -0
  14. package/dist/modal/stores/modal/useModalStore.js +2 -1
  15. package/dist/modal/utils/openPopup.d.ts +1 -1
  16. package/dist/modal/utils/openPopup.js +3 -1
  17. package/dist/modal/utils/stringFormatters.js +10 -1
  18. package/dist/modal/utils/validatePortalOrigin.d.ts +2 -0
  19. package/dist/modal/utils/validatePortalOrigin.js +14 -0
  20. package/dist/provider/hooks/mutations/useAddAuthMethod.d.ts +3 -3
  21. package/dist/provider/hooks/mutations/useLoginExternalWallet.d.ts +6 -0
  22. package/dist/provider/hooks/mutations/useVerifyExternalWallet.d.ts +16 -28
  23. package/dist/provider/hooks/utils/useEventListeners.js +2 -0
  24. package/dist/provider/providers/AuthProvider.js +73 -34
  25. package/dist/provider/providers/ExternalWalletProvider.d.ts +7 -1
  26. package/dist/provider/providers/ExternalWalletProvider.js +240 -27
  27. package/package.json +8 -8
  28. package/dist/modal/utils/routeMobileExternalWallet.d.ts +0 -1
  29. package/dist/modal/utils/routeMobileExternalWallet.js +0 -31
@@ -17,17 +17,13 @@ export declare const VERIFY_EXTERNAL_WALLET_KEY = "VERIFY_EXTERNAL_WALLET";
17
17
  export declare const useVerifyExternalWallet: () => {
18
18
  status: "idle" | "pending" | "error" | "success";
19
19
  error: Error | null;
20
- data: Compute<import("@getpara/web-sdk").AuthStateSignup | import("@getpara/web-sdk").AuthStateLogin> | undefined;
20
+ data: Compute<import("@getpara/web-sdk").AuthStateSignup | import("@getpara/web-sdk").AuthStateLogin | import("@getpara/core-sdk/dist/types/types/methods.js").AuthStateDone> | undefined;
21
21
  isSuccess: boolean;
22
- variables: {
23
- portalTheme?: import("@getpara/web-sdk").Theme | undefined;
24
- useShortUrls?: boolean | undefined;
25
- externalWallet: import("@getpara/web-sdk").ExternalWalletInfo;
26
- signedMessage: string;
27
- cosmosPublicKeyHex?: string | undefined;
28
- cosmosSigner?: string | undefined;
29
- sessionLookupId?: string | undefined;
30
- } | undefined;
22
+ variables: Compute<(import("@getpara/core-sdk/dist/types/types/methods.js").WithCustomTheme & import("@getpara/core-sdk/dist/types/types/methods.js").WithUseShortUrls & {
23
+ serverAuthState: import("@getpara/shared").ServerAuthStateSignup | import("@getpara/shared").ServerAuthStateLogin | import("@getpara/shared").ServerAuthStateDone;
24
+ }) | (import("@getpara/core-sdk/dist/types/types/methods.js").WithCustomTheme & import("@getpara/core-sdk/dist/types/types/methods.js").WithUseShortUrls & import("@getpara/web-sdk").VerifyExternalWalletParams & {
25
+ serverAuthState?: undefined;
26
+ })> | undefined;
31
27
  isError: boolean;
32
28
  isIdle: boolean;
33
29
  isPending: boolean;
@@ -37,22 +33,14 @@ export declare const useVerifyExternalWallet: () => {
37
33
  failureReason: Error | null;
38
34
  isPaused: boolean;
39
35
  submittedAt: number;
40
- verifyExternalWallet: import("@tanstack/react-query").UseMutateFunction<Compute<import("@getpara/web-sdk").AuthStateSignup | import("@getpara/web-sdk").AuthStateLogin>, Error, {
41
- portalTheme?: import("@getpara/web-sdk").Theme | undefined;
42
- useShortUrls?: boolean | undefined;
43
- externalWallet: import("@getpara/web-sdk").ExternalWalletInfo;
44
- signedMessage: string;
45
- cosmosPublicKeyHex?: string | undefined;
46
- cosmosSigner?: string | undefined;
47
- sessionLookupId?: string | undefined;
48
- }, unknown>;
49
- verifyExternalWalletAsync: import("@tanstack/react-query").UseMutateAsyncFunction<Compute<import("@getpara/web-sdk").AuthStateSignup | import("@getpara/web-sdk").AuthStateLogin>, Error, {
50
- portalTheme?: import("@getpara/web-sdk").Theme | undefined;
51
- useShortUrls?: boolean | undefined;
52
- externalWallet: import("@getpara/web-sdk").ExternalWalletInfo;
53
- signedMessage: string;
54
- cosmosPublicKeyHex?: string | undefined;
55
- cosmosSigner?: string | undefined;
56
- sessionLookupId?: string | undefined;
57
- }, unknown>;
36
+ verifyExternalWallet: import("@tanstack/react-query").UseMutateFunction<Compute<import("@getpara/web-sdk").AuthStateSignup | import("@getpara/web-sdk").AuthStateLogin | import("@getpara/core-sdk/dist/types/types/methods.js").AuthStateDone>, Error, Compute<(import("@getpara/core-sdk/dist/types/types/methods.js").WithCustomTheme & import("@getpara/core-sdk/dist/types/types/methods.js").WithUseShortUrls & {
37
+ serverAuthState: import("@getpara/shared").ServerAuthStateSignup | import("@getpara/shared").ServerAuthStateLogin | import("@getpara/shared").ServerAuthStateDone;
38
+ }) | (import("@getpara/core-sdk/dist/types/types/methods.js").WithCustomTheme & import("@getpara/core-sdk/dist/types/types/methods.js").WithUseShortUrls & import("@getpara/web-sdk").VerifyExternalWalletParams & {
39
+ serverAuthState?: undefined;
40
+ })>, unknown>;
41
+ verifyExternalWalletAsync: import("@tanstack/react-query").UseMutateAsyncFunction<Compute<import("@getpara/web-sdk").AuthStateSignup | import("@getpara/web-sdk").AuthStateLogin | import("@getpara/core-sdk/dist/types/types/methods.js").AuthStateDone>, Error, Compute<(import("@getpara/core-sdk/dist/types/types/methods.js").WithCustomTheme & import("@getpara/core-sdk/dist/types/types/methods.js").WithUseShortUrls & {
42
+ serverAuthState: import("@getpara/shared").ServerAuthStateSignup | import("@getpara/shared").ServerAuthStateLogin | import("@getpara/shared").ServerAuthStateDone;
43
+ }) | (import("@getpara/core-sdk/dist/types/types/methods.js").WithCustomTheme & import("@getpara/core-sdk/dist/types/types/methods.js").WithUseShortUrls & import("@getpara/web-sdk").VerifyExternalWalletParams & {
44
+ serverAuthState?: undefined;
45
+ })>, unknown>;
58
46
  };
@@ -123,6 +123,7 @@ const useEventListeners = ({
123
123
  });
124
124
  }, [queryClient, refs.balancesInvalidationTime]);
125
125
  useEffect(() => {
126
+ if (typeof window === "undefined") return;
126
127
  window.addEventListener(ParaEvent.LOGIN_EVENT, loginListener);
127
128
  window.addEventListener(ParaEvent.ACCOUNT_SETUP_EVENT, accountSetupListener);
128
129
  window.addEventListener(ParaEvent.ACCOUNT_CREATION_EVENT, accountCreationListener);
@@ -136,6 +137,7 @@ const useEventListeners = ({
136
137
  window.addEventListener(ParaEvent.GUEST_WALLETS_CREATED, guestWalletsCreatedListener);
137
138
  window.addEventListener(ParaEvent.ASSET_TRANSFERRED, assetTransferListener);
138
139
  return () => {
140
+ if (typeof window === "undefined") return;
139
141
  window.removeEventListener(ParaEvent.LOGIN_EVENT, loginListener);
140
142
  window.removeEventListener(ParaEvent.ACCOUNT_SETUP_EVENT, accountSetupListener);
141
143
  window.removeEventListener(ParaEvent.ACCOUNT_CREATION_EVENT, accountCreationListener);
@@ -5,8 +5,8 @@ import {
5
5
  __spreadValues
6
6
  } from "../../chunk-MMUBH76A.js";
7
7
  import { jsx } from "react/jsx-runtime";
8
- import { useUserAgent } from "@getpara/react-common";
9
- import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
8
+ import { openMobileUrl, useUserAgent } from "@getpara/react-common";
9
+ import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
10
10
  import { useModalStore } from "../../modal/stores/index.js";
11
11
  import { ModalStep } from "../../modal/utils/steps.js";
12
12
  import {
@@ -29,15 +29,15 @@ import { openPopup } from "../../modal/utils/openPopup.js";
29
29
  import {
30
30
  AuthMethod,
31
31
  entityToWallet,
32
- getPortalBaseURL
32
+ isMobile
33
33
  } from "@getpara/web-sdk";
34
34
  import { useInternalClient } from "../../provider/hooks/utils/useInternalClient.js";
35
35
  import { useGoBack } from "../../modal/hooks/useGoBack.js";
36
36
  import { isExternalWallet } from "@getpara/user-management-client";
37
- import { routeMobileExternalWallet } from "../../modal/utils/routeMobileExternalWallet.js";
38
37
  import { useStore } from "../stores/useStore.js";
39
38
  import { useFormattedBiometricHints } from "../hooks/utils/useFormattedBiometricHints.js";
40
39
  import { useQueryClient } from "@tanstack/react-query";
40
+ import { validatePortalOrigin } from "../../modal/utils/validatePortalOrigin.js";
41
41
  const AuthContext = createContext({
42
42
  signUpOrLogIn: () => {
43
43
  },
@@ -127,6 +127,7 @@ function AuthProvider({
127
127
  const { data: biometricHints } = useFormattedBiometricHints();
128
128
  const [switchWalletsUrl, setSwitchWalletsUrl] = useState(void 0);
129
129
  const [isSwitchWalletsPending, setIsSwitchWalletsPending] = useState(mutateIsSwitchWalletsPending);
130
+ const messageHandlerRef = useRef(null);
130
131
  const goBackIfPopupClosedOnSteps = (steps) => {
131
132
  var _a;
132
133
  if (((_a = refs.popupWindow.current) == null ? void 0 : _a.closed) && (!refs.currentStep.current || steps.includes(refs.currentStep.current))) {
@@ -139,11 +140,12 @@ function AuthProvider({
139
140
  return !!stepNow && !steps.includes(stepNow);
140
141
  };
141
142
  const setupListener = () => {
142
- window.addEventListener("message", function handleMessage(event) {
143
+ if (messageHandlerRef.current) {
144
+ window.removeEventListener("message", messageHandlerRef.current);
145
+ }
146
+ const handleMessage = (event) => {
143
147
  var _a;
144
- const portalBase = getPortalBaseURL(para.ctx);
145
- const portalLocalBase = getPortalBaseURL(para.ctx, true);
146
- if (!event.origin.startsWith(portalBase) && !event.origin.startsWith(portalLocalBase)) {
148
+ if (!validatePortalOrigin(event, para.ctx)) {
147
149
  return;
148
150
  }
149
151
  if (((_a = event.data) == null ? void 0 : _a.type) === "CLOSE_WINDOW") {
@@ -154,8 +156,11 @@ function AuthProvider({
154
156
  setStep(ModalStep.AWAITING_ACCOUNT);
155
157
  }
156
158
  window.removeEventListener("message", handleMessage);
159
+ messageHandlerRef.current = null;
157
160
  }
158
- });
161
+ };
162
+ messageHandlerRef.current = handleMessage;
163
+ window.addEventListener("message", handleMessage);
159
164
  };
160
165
  const pollSignup = () => {
161
166
  if (typeof window !== "undefined") {
@@ -215,12 +220,14 @@ function AuthProvider({
215
220
  if (refs.currentStep.current !== ModalStep.AWAITING_BIOMETRIC_CREATION) {
216
221
  setStep(ModalStep.AWAITING_BIOMETRIC_CREATION);
217
222
  }
218
- refs.popupWindow.current = openPopup({
219
- url: authState.passkeyUrl,
220
- target: "ParaPasskey",
221
- type: "CREATE_PASSKEY",
222
- current: refs.popupWindow.current
223
- });
223
+ if (typeof window !== "undefined") {
224
+ refs.popupWindow.current = openPopup({
225
+ url: authState.passkeyUrl,
226
+ target: "ParaPasskey",
227
+ type: "CREATE_PASSKEY",
228
+ current: refs.popupWindow.current
229
+ });
230
+ }
224
231
  break;
225
232
  case AuthMethod.PASSWORD:
226
233
  setupListener();
@@ -264,13 +271,14 @@ function AuthProvider({
264
271
  [isIFrameReady]
265
272
  );
266
273
  const login = (authState) => {
267
- if (authState.isWalletSelectionNeeded || authState.passkeyUrl) {
268
- setStep(ModalStep.BIOMETRIC_LOGIN);
269
- } else {
274
+ const hasPasskey = !!authState.passkeyUrl;
275
+ if (!hasPasskey) {
270
276
  setupListener();
271
277
  setIFrameUrl(authState.passwordUrl || authState.pinUrl);
272
278
  setIsIFrameReady(false);
273
279
  setStep(ModalStep.EMBEDDED_PASSWORD_LOGIN);
280
+ } else {
281
+ setStep(ModalStep.BIOMETRIC_LOGIN);
274
282
  }
275
283
  pollLogin();
276
284
  };
@@ -341,23 +349,49 @@ function AuthProvider({
341
349
  loginOverride();
342
350
  return;
343
351
  }
344
- refs.popupWindow.current = openPopup({
345
- url: isPIN ? authState.pinUrl : isPassword ? authState.passwordUrl : authState.passkeyUrl,
346
- target: isPIN ? "ParaPIN" : isPassword ? "ParaPassword" : "ParaPasskey",
347
- type: isPIN ? "LOGIN_PASSWORD" : isPassword ? "LOGIN_PASSWORD" : "LOGIN_PASSKEY",
348
- current: refs.popupWindow.current
349
- });
352
+ if (typeof window !== "undefined") {
353
+ refs.popupWindow.current = openPopup({
354
+ url: isPIN ? authState.pinUrl : isPassword ? authState.passwordUrl : authState.passkeyUrl,
355
+ target: isPIN ? "ParaPIN" : isPassword ? "ParaPassword" : "ParaPasskey",
356
+ type: isPIN ? "LOGIN_PASSWORD" : isPassword ? "LOGIN_PASSWORD" : "LOGIN_PASSKEY",
357
+ current: refs.popupWindow.current
358
+ });
359
+ }
350
360
  setStep(isPassword || isPIN ? ModalStep.AWAITING_PASSWORD_LOGIN : ModalStep.AWAITING_BIOMETRIC_LOGIN);
351
361
  },
352
362
  [loginState, biometricHints]
353
363
  );
354
364
  const onNewAuthState = (authState) => __async(this, null, function* () {
355
- refs.popupWindow.current = null;
365
+ var _a, _b, _c;
356
366
  setAuthState(authState);
357
367
  switch (authState.stage) {
358
368
  case "verify":
359
- if (isExternalWallet(authState.auth) && authState.signatureVerificationMessage) {
360
- setStep(ModalStep.EXTERNAL_WALLET_VERIFICATION);
369
+ if (isExternalWallet(authState.auth)) {
370
+ if (authState.loginUrl && ((_a = authState.externalWallet) == null ? void 0 : _a.withFullParaAuth)) {
371
+ let isBasicLogin = false;
372
+ if (authState.nextStage === "login") {
373
+ isBasicLogin = authState.loginAuthMethods.includes(AuthMethod.BASIC_LOGIN);
374
+ } else {
375
+ isBasicLogin = authState.signupAuthMethods.includes(AuthMethod.BASIC_LOGIN);
376
+ }
377
+ if (authState.nextStage === "login") {
378
+ setFlow("login");
379
+ isBasicLogin && pollLogin();
380
+ } else {
381
+ setFlow("signup");
382
+ isBasicLogin && pollSignup();
383
+ }
384
+ if (!isMobile() && refs.popupWindow.current) {
385
+ refs.popupWindow.current.location.href = authState.loginUrl;
386
+ setStep(ModalStep.AWAITING_ACCOUNT);
387
+ } else {
388
+ setIFrameUrl(authState.loginUrl);
389
+ setStep(ModalStep.OTP);
390
+ setupListener();
391
+ }
392
+ } else {
393
+ setStep(ModalStep.EXTERNAL_WALLET_VERIFICATION);
394
+ }
361
395
  } else {
362
396
  if (authState.nextStage === "login") {
363
397
  setFlow("login");
@@ -398,12 +432,14 @@ function AuthProvider({
398
432
  }
399
433
  break;
400
434
  case "done":
401
- if (authState.isNewUser) {
402
- pollSignup();
403
- setFlow("signup");
404
- } else {
405
- pollLogin();
406
- setFlow("login");
435
+ if (!((_b = authState.externalWallet) == null ? void 0 : _b.withFullParaAuth) && !((_c = authState.externalWallet) == null ? void 0 : _c.withVerification)) {
436
+ if (authState.isNewUser) {
437
+ pollSignup();
438
+ setFlow("signup");
439
+ } else {
440
+ pollLogin();
441
+ setFlow("login");
442
+ }
407
443
  }
408
444
  if (!authState.isWalletSelectionNeeded) {
409
445
  setStep(ModalStep.AWAITING_ACCOUNT);
@@ -463,7 +499,7 @@ function AuthProvider({
463
499
  isCanceled: () => refs.currentStep.current !== ModalStep.FARCASTER_OAUTH,
464
500
  onConnectUri: (connectUri) => {
465
501
  setFarcasterConnectUri(connectUri);
466
- routeMobileExternalWallet(connectUri);
502
+ openMobileUrl(connectUri);
467
503
  },
468
504
  useShortUrls: true,
469
505
  serverAuthState
@@ -722,6 +758,9 @@ function AuthProvider({
722
758
  return () => {
723
759
  var _a;
724
760
  window == null ? void 0 : window.clearTimeout((_a = refs.poll.current) == null ? void 0 : _a.timeout);
761
+ if (messageHandlerRef.current) {
762
+ window.removeEventListener("message", messageHandlerRef.current);
763
+ }
725
764
  };
726
765
  }, []);
727
766
  return /* @__PURE__ */ jsx(AuthContext.Provider, { value, children });
@@ -51,7 +51,13 @@ type Value = Omit<ExternalWalletContextType<CosmosSignResult>, 'disconnect' | 'd
51
51
  };
52
52
  username?: string;
53
53
  avatar?: string;
54
- connectExternalWallet: (wallet: CommonWallet, isMobile?: boolean, isManualWalletConnect?: boolean) => Promise<void>;
54
+ connectExternalWallet: (_: {
55
+ wallet: CommonWallet;
56
+ isMobile?: boolean;
57
+ isManualWalletConnect?: boolean;
58
+ isResetAfterManualWalletConnect?: boolean;
59
+ isRetryConnection?: boolean;
60
+ }) => Promise<void>;
55
61
  addAdditionalExternalWallet: (wallet: CommonWallet) => Promise<void>;
56
62
  disconnectExternalWallet: () => Promise<void>;
57
63
  setChainIdSwitchingTo: (chainId?: string) => void;
@@ -5,17 +5,22 @@ import {
5
5
  __spreadValues
6
6
  } from "../../chunk-MMUBH76A.js";
7
7
  import { jsx } from "react/jsx-runtime";
8
- import { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
8
+ import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState } from "react";
9
9
  import { isMobile, truncateAddress } from "@getpara/web-sdk";
10
10
  import { useInternalClient } from "../hooks/utils/useInternalClient.js";
11
11
  import { useStore } from "../stores/useStore.js";
12
- import { ModalStep } from "../../modal/index.js";
12
+ import { ModalStep, openPopup } from "../../modal/index.js";
13
13
  import { useModalStore } from "../../modal/stores/index.js";
14
14
  import { useAccount, useModal, useParaStatus, useVerifyExternalWallet, useWalletState } from "../hooks/index.js";
15
+ import {
16
+ openMobileUrl
17
+ } from "@getpara/react-common";
15
18
  import { ParaEvent, dispatchEvent } from "@getpara/web-sdk";
16
19
  import { useAuthActions } from "./AuthProvider.js";
17
20
  import { IS_FULLY_LOGGED_IN_BASE_KEY } from "../hooks/queries/useIsFullyLoggedIn.js";
18
21
  import { useQueryClient } from "@tanstack/react-query";
22
+ import { useGoBack } from "../../modal/hooks/useGoBack.js";
23
+ import { validatePortalOrigin } from "../../modal/utils/validatePortalOrigin.js";
19
24
  const useWalletDisplayHelpers = (wallet) => {
20
25
  const isUsingMobileConnector = useModalStore((state) => state.isUsingMobileConnector);
21
26
  return {
@@ -125,9 +130,11 @@ function ExternalWalletProvider({ children }) {
125
130
  const { onNewAuthState } = useAuthActions();
126
131
  const { verifyExternalWalletAsync } = useVerifyExternalWallet();
127
132
  const queryClient = useQueryClient();
133
+ const goBack = useGoBack();
128
134
  const [qrUri, setQrUri] = useState();
129
135
  const [chainIdSwitchingTo, setChainIdSwitchingTo] = useState();
130
136
  const [isSigningMessage, setIsSigningMessage] = useState(false);
137
+ const popupCloseIntervalRef = useRef(null);
131
138
  const allWallets = [...evmWallets, ...solanaWallets, ...cosmosWallets];
132
139
  let wallets = allWallets.filter(
133
140
  (w) => (w.internalId !== "FARCASTER" || (para == null ? void 0 : para.isFarcasterMiniApp)) && externalWallets.includes(w.internalId)
@@ -142,20 +149,25 @@ function ExternalWalletProvider({ children }) {
142
149
  () => wallets.find((w) => w.id === (selectedExternalWallet == null ? void 0 : selectedExternalWallet.id) && w.type === (selectedExternalWallet == null ? void 0 : selectedExternalWallet.type)),
143
150
  [wallets, selectedExternalWallet]
144
151
  );
145
- const updateQrUri = () => __async(this, null, function* () {
146
- var _a;
147
- const uri = yield (_a = wallet == null ? void 0 : wallet.getQrUri) == null ? void 0 : _a.call(wallet);
148
- setQrUri(uri);
149
- });
150
- useEffect(() => {
151
- if (wallet) {
152
- if (!qrUri) {
153
- updateQrUri();
154
- }
155
- } else if (qrUri) {
156
- setQrUri(void 0);
152
+ const [walletConnectCleanup, setWalletConnectCleanup] = useState(null);
153
+ const listenForWalletConnectUri = () => {
154
+ setQrUri(void 0);
155
+ if (walletConnectCleanup) {
156
+ walletConnectCleanup();
157
157
  }
158
- }, [wallet]);
158
+ const callback = (event) => {
159
+ openMobileUrl(event.detail);
160
+ setQrUri(event.detail);
161
+ cleanup();
162
+ setWalletConnectCleanup(null);
163
+ };
164
+ const cleanup = () => {
165
+ window.removeEventListener("PARA_WALLETCONNECT_URI_READY", callback);
166
+ };
167
+ window.addEventListener("PARA_WALLETCONNECT_URI_READY", callback);
168
+ setWalletConnectCleanup(() => cleanup);
169
+ return cleanup;
170
+ };
159
171
  const isWithFullAuth = (wallet2) => {
160
172
  if (connectionOnly) {
161
173
  return false;
@@ -351,7 +363,10 @@ function ExternalWalletProvider({ children }) {
351
363
  return verifyExternalWalletParams;
352
364
  }), [cosmosSignVerificationMessage, evmSignVerificationMessage, solanaSignVerificationMessage, wallet]);
353
365
  const signMessage = useCallback(
354
- (_0) => __async(this, [_0], function* ({ message, externalWallet: _externalWallet }) {
366
+ (_0) => __async(this, [_0], function* ({
367
+ message,
368
+ externalWallet: _externalWallet
369
+ }) {
355
370
  var _a;
356
371
  setExternalWalletError();
357
372
  setIsSigningMessage(true);
@@ -362,14 +377,16 @@ function ExternalWalletProvider({ children }) {
362
377
  switch (walletType) {
363
378
  case "COSMOS":
364
379
  {
365
- const { address, signature, error, cosmosPublicKeyHex, cosmosSigner } = yield cosmosSignMessage({
366
- message,
367
- externalWallet
368
- });
380
+ const { address, signature, error, cosmosPublicKeyHex, cosmosSigner, addressBech32 } = yield cosmosSignMessage(
381
+ {
382
+ message,
383
+ externalWallet
384
+ }
385
+ );
369
386
  if (error) {
370
387
  throw new Error(error);
371
388
  } else if (signature && address) {
372
- response = { address, signature, cosmosPublicKeyHex, cosmosSigner };
389
+ response = { address, signature, cosmosPublicKeyHex, cosmosSigner, addressBech32 };
373
390
  }
374
391
  }
375
392
  break;
@@ -407,6 +424,7 @@ function ExternalWalletProvider({ children }) {
407
424
  );
408
425
  const addAdditionalExternalWallet = useCallback(
409
426
  (wallet2) => __async(this, null, function* () {
427
+ setExternalWalletError();
410
428
  try {
411
429
  const walletInfo = yield requestInfo(wallet2.id, wallet2.type);
412
430
  const walletAddress = wallet2.type === "COSMOS" && walletInfo.addressBech32 ? walletInfo.addressBech32 : walletInfo.address;
@@ -470,8 +488,198 @@ function ExternalWalletProvider({ children }) {
470
488
  }),
471
489
  [para, connectionOnly, includeWalletVerification, setStep, setExternalWalletError]
472
490
  );
491
+ const setupExternalWalletVerificationStatusListener = (wallet2) => {
492
+ typeof window !== "undefined" && window.addEventListener("message", function handleMessage(event) {
493
+ return __async(this, null, function* () {
494
+ var _a, _b, _c, _d;
495
+ if (!validatePortalOrigin(event, para.ctx)) {
496
+ return;
497
+ }
498
+ if (((_a = event.data) == null ? void 0 : _a.type) === "EW_VERIFY_SUCCESS") {
499
+ clearPopupWindowCloseListener();
500
+ const serverAuthState = (_b = event.data) == null ? void 0 : _b.serverAuthState;
501
+ if (serverAuthState && ((_c = serverAuthState.externalWallet) == null ? void 0 : _c.withFullParaAuth)) {
502
+ const authState = yield para.verifyExternalWallet({ serverAuthState });
503
+ yield onNewAuthState(authState);
504
+ } else {
505
+ setStep(ModalStep.LOGIN_DONE);
506
+ }
507
+ window.removeEventListener("message", handleMessage);
508
+ }
509
+ if (((_d = event.data) == null ? void 0 : _d.type) === "EW_VERIFY_RETRY") {
510
+ yield handleTriggerSignMessage(wallet2, event.data.message);
511
+ window.removeEventListener("message", handleMessage);
512
+ }
513
+ });
514
+ });
515
+ };
516
+ const handlePostMessage = (message) => {
517
+ var _a;
518
+ if (refs.popupWindow.current) {
519
+ refs.popupWindow.current.postMessage(message, "*");
520
+ } else if (refs.iFrame.current) {
521
+ (_a = refs.iFrame.current.contentWindow) == null ? void 0 : _a.postMessage(message, "*");
522
+ }
523
+ };
524
+ const handleTriggerSignMessage = (wallet2, message) => __async(this, null, function* () {
525
+ setupExternalWalletVerificationStatusListener(wallet2);
526
+ try {
527
+ const { address, signature, cosmosPublicKeyHex, cosmosSigner, addressBech32 } = yield signMessage({
528
+ message,
529
+ externalWallet: wallet2
530
+ });
531
+ const paraWallet = Object.values(para.externalWallets)[0];
532
+ const walletType = paraWallet == null ? void 0 : paraWallet.type;
533
+ let verifyExternalWalletParams;
534
+ const withVerification = includeWalletVerification;
535
+ const isConnectionOnly = connectionOnly;
536
+ const withFullParaAuth = (paraWallet == null ? void 0 : paraWallet.name) ? isWithFullAuth(paraWallet) : false;
537
+ const defaultWalletInfo = {
538
+ withVerification,
539
+ isConnectionOnly,
540
+ withFullParaAuth,
541
+ provider: paraWallet.name,
542
+ providerId: paraWallet.externalProviderId,
543
+ isExternal: true
544
+ };
545
+ switch (walletType) {
546
+ case "COSMOS":
547
+ {
548
+ verifyExternalWalletParams = {
549
+ externalWallet: __spreadValues({
550
+ partnerId: para.partnerId,
551
+ type: "COSMOS",
552
+ address,
553
+ addressBech32
554
+ }, defaultWalletInfo),
555
+ signedMessage: signature,
556
+ cosmosPublicKeyHex,
557
+ cosmosSigner
558
+ };
559
+ }
560
+ break;
561
+ case "EVM":
562
+ {
563
+ verifyExternalWalletParams = {
564
+ externalWallet: __spreadValues({
565
+ partnerId: para.partnerId,
566
+ type: "EVM",
567
+ address
568
+ }, defaultWalletInfo),
569
+ signedMessage: signature
570
+ };
571
+ }
572
+ break;
573
+ case "SOLANA":
574
+ {
575
+ verifyExternalWalletParams = {
576
+ externalWallet: __spreadValues({
577
+ partnerId: para.partnerId,
578
+ type: "SOLANA",
579
+ address
580
+ }, defaultWalletInfo),
581
+ signedMessage: signature
582
+ };
583
+ }
584
+ break;
585
+ default:
586
+ break;
587
+ }
588
+ if (!(verifyExternalWalletParams == null ? void 0 : verifyExternalWalletParams.externalWallet) || !(verifyExternalWalletParams == null ? void 0 : verifyExternalWalletParams.signedMessage)) {
589
+ console.error("No signature or address found on the verifyWalletSignature response.");
590
+ handlePostMessage({ type: "EW_SIGN_MESSAGE_ERROR", error: "Signature verification failed." });
591
+ return;
592
+ }
593
+ handlePostMessage({ type: "EW_SIGN_MESSAGE_SUCCESS", verifyExternalWalletParams });
594
+ } catch (error) {
595
+ handlePostMessage({ type: "EW_SIGN_MESSAGE_ERROR", error: error.message || "Error signing message" });
596
+ }
597
+ });
598
+ const setupExternalWalletVerificationTriggerListener = (wallet2) => {
599
+ if (!wallet2) {
600
+ return;
601
+ }
602
+ typeof window !== "undefined" && window.addEventListener("message", function handleMessage(event) {
603
+ return __async(this, null, function* () {
604
+ var _a;
605
+ if (!validatePortalOrigin(event, para.ctx)) {
606
+ return;
607
+ }
608
+ if (((_a = event.data) == null ? void 0 : _a.type) === "EW_TRIGGER_SIGN_MESSAGE") {
609
+ yield handleTriggerSignMessage(wallet2, event.data.message);
610
+ window.removeEventListener("message", handleMessage);
611
+ }
612
+ });
613
+ });
614
+ };
615
+ const setupPopupWindowCloseListener = () => {
616
+ const popup = refs.popupWindow.current;
617
+ if (!popup) return;
618
+ if (popupCloseIntervalRef.current) {
619
+ clearInterval(popupCloseIntervalRef.current);
620
+ }
621
+ popupCloseIntervalRef.current = setInterval(() => {
622
+ if (popup.closed) {
623
+ if (popupCloseIntervalRef.current) {
624
+ clearInterval(popupCloseIntervalRef.current);
625
+ popupCloseIntervalRef.current = null;
626
+ }
627
+ goBack();
628
+ disconnectExternalWallet();
629
+ }
630
+ }, 500);
631
+ };
632
+ const clearPopupWindowCloseListener = () => {
633
+ if (popupCloseIntervalRef.current) {
634
+ clearInterval(popupCloseIntervalRef.current);
635
+ popupCloseIntervalRef.current = null;
636
+ }
637
+ };
638
+ const handlePostConnectRetry = () => {
639
+ handlePostMessage({ type: "EW_CONNECT_RETRY" });
640
+ };
641
+ const handlePostConnectError = (error) => {
642
+ handlePostMessage({ type: "EW_CONNECT_ERROR", error });
643
+ };
644
+ const handleConnectRetryMessage = (wallet2) => (event) => __async(this, null, function* () {
645
+ var _a;
646
+ if (!validatePortalOrigin(event, para.ctx)) {
647
+ return;
648
+ }
649
+ if (((_a = event.data) == null ? void 0 : _a.type) === "EW_CONNECT_RETRY") {
650
+ clearExternalWalletConnectionRetryListener(wallet2);
651
+ yield connectExternalWallet({ wallet: wallet2, isMobileConnect: wallet2.isMobile, isRetryConnection: true });
652
+ }
653
+ });
654
+ const setupExternalWalletConnectionRetryListener = (wallet2) => {
655
+ typeof window !== "undefined" && window.addEventListener("message", handleConnectRetryMessage(wallet2));
656
+ };
657
+ const clearExternalWalletConnectionRetryListener = (wallet2) => {
658
+ window.removeEventListener("message", handleConnectRetryMessage(wallet2));
659
+ };
473
660
  const connectExternalWallet = useCallback(
474
- (wallet2, isMobileConnect, isManualWalletConnect, isResetAfterManualWalletConnect) => __async(this, null, function* () {
661
+ (_0) => __async(this, [_0], function* ({
662
+ wallet: wallet2,
663
+ isManualWalletConnect,
664
+ isMobileConnect,
665
+ isResetAfterManualWalletConnect,
666
+ isRetryConnection
667
+ }) {
668
+ if (isRetryConnection) {
669
+ clearExternalWalletConnectionRetryListener(wallet2);
670
+ handlePostConnectRetry();
671
+ }
672
+ if (!isMobile() && isWithFullAuth(wallet2)) {
673
+ const popupUrl = yield para.constructPortalUrl("connectExternalWallet");
674
+ if (typeof window !== void 0) {
675
+ refs.popupWindow.current = openPopup({
676
+ url: popupUrl,
677
+ type: "LOGIN_EXTERNAL_WALLET",
678
+ target: "ParaExternalWallet"
679
+ });
680
+ }
681
+ setupPopupWindowCloseListener();
682
+ }
475
683
  if (isExternalWalletConnecting && isManualWalletConnect) {
476
684
  yield evmDisconnect();
477
685
  yield solanaDisconnect();
@@ -479,23 +687,27 @@ function ExternalWalletProvider({ children }) {
479
687
  setQrUri(void 0);
480
688
  setIsExternalWalletConnecting(false);
481
689
  }
482
- if (isResetAfterManualWalletConnect || isManualWalletConnect || !isExternalWalletConnecting) {
690
+ if (isResetAfterManualWalletConnect || isManualWalletConnect || isMobileConnect || !isExternalWalletConnecting) {
483
691
  setExternalWalletError();
484
692
  setIsExternalWalletConnecting(true);
485
693
  setIsUsingMobileConnector(isMobileConnect);
694
+ listenForWalletConnectUri();
486
695
  const { address, error, authState } = yield isMobileConnect ? wallet2.connectMobile(isManualWalletConnect, connectionOnly) : wallet2.connect(connectionOnly);
487
696
  if (error) {
488
697
  setExternalWalletError([error]);
489
698
  setIsUsingMobileConnector();
699
+ handlePostConnectError(error);
490
700
  if (isManualWalletConnect && error === "Connection request rejected") {
491
701
  setExternalWalletError();
492
- yield connectExternalWallet(wallet2, false, false, true);
493
- yield updateQrUri();
702
+ yield connectExternalWallet({ wallet: wallet2, isResetAfterManualWalletConnect: true });
494
703
  return;
495
704
  }
705
+ setupExternalWalletConnectionRetryListener(wallet2);
496
706
  } else if (address) {
497
707
  if (!!authState && (isWithFullAuth(wallet2) || includeWalletVerification)) {
708
+ clearExternalWalletConnectionRetryListener(wallet2);
498
709
  onNewAuthState(authState);
710
+ setupExternalWalletVerificationTriggerListener(authState.externalWallet);
499
711
  } else {
500
712
  setStep(ModalStep.LOGIN_DONE);
501
713
  }
@@ -521,7 +733,7 @@ function ExternalWalletProvider({ children }) {
521
733
  isConnectionOnly: true
522
734
  });
523
735
  } else {
524
- yield connectExternalWallet(evmWallet, false, true);
736
+ yield connectExternalWallet({ wallet: evmWallet, isManualWalletConnect: true });
525
737
  }
526
738
  }
527
739
  if (solanaWallet && (solanaFarcasterStatus == null ? void 0 : solanaFarcasterStatus.isPresent)) {
@@ -535,7 +747,7 @@ function ExternalWalletProvider({ children }) {
535
747
  isConnectionOnly: true
536
748
  });
537
749
  } else {
538
- yield connectExternalWallet(solanaWallet, false, true);
750
+ yield connectExternalWallet({ wallet: solanaWallet, isManualWalletConnect: true });
539
751
  }
540
752
  }
541
753
  if (loginWallets.length > 0) {
@@ -547,6 +759,7 @@ function ExternalWalletProvider({ children }) {
547
759
  }
548
760
  });
549
761
  const requestInfo = (providerId, type) => __async(this, null, function* () {
762
+ listenForWalletConnectUri();
550
763
  switch (type) {
551
764
  case "EVM": {
552
765
  const externalWallet = yield evmRequestInfo(providerId);