@b3dotfun/sdk 0.0.47-alpha.0 → 0.0.47-test.4

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 (90) hide show
  1. package/README.md +3 -3
  2. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.d.ts +3 -2
  3. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.js +15 -64
  4. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.native.d.ts +6 -2
  5. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.native.js +26 -21
  6. package/dist/cjs/global-account/react/components/B3Provider/types.d.ts +2 -0
  7. package/dist/cjs/global-account/react/components/B3Provider/types.js +2 -0
  8. package/dist/cjs/global-account/react/components/ManageAccount/BalanceContent.d.ts +1 -2
  9. package/dist/cjs/global-account/react/components/ManageAccount/BalanceContent.js +2 -2
  10. package/dist/cjs/global-account/react/components/ManageAccount/ManageAccount.js +1 -1
  11. package/dist/cjs/global-account/react/components/SignInWithB3/SignIn.js +2 -2
  12. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3.js +1 -1
  13. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.d.ts +1 -1
  14. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +6 -22
  15. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Privy.d.ts +1 -2
  16. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Privy.js +3 -2
  17. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStep.d.ts +1 -2
  18. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStep.js +10 -45
  19. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStepCustom.d.ts +1 -2
  20. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStepCustom.js +2 -1
  21. package/dist/cjs/global-account/react/components/custom/ManageAccountButton.js +2 -1
  22. package/dist/cjs/global-account/react/hooks/useAuthentication.d.ts +51 -2
  23. package/dist/cjs/global-account/react/hooks/useAuthentication.js +134 -84
  24. package/dist/cjs/global-account/react/hooks/useHandleConnectWithPrivy.d.ts +1 -1
  25. package/dist/cjs/global-account/react/hooks/useHandleConnectWithPrivy.js +2 -1
  26. package/dist/cjs/global-account/react/hooks/useUserQuery.d.ts +58 -0
  27. package/dist/cjs/global-account/react/hooks/useUserQuery.js +86 -0
  28. package/dist/cjs/global-account/react/hooks/useWagmiConfig.d.ts +13 -0
  29. package/dist/cjs/global-account/react/hooks/useWagmiConfig.js +42 -0
  30. package/dist/cjs/global-account/react/stores/useModalStore.d.ts +0 -2
  31. package/dist/esm/global-account/react/components/B3Provider/B3Provider.d.ts +3 -2
  32. package/dist/esm/global-account/react/components/B3Provider/B3Provider.js +19 -68
  33. package/dist/esm/global-account/react/components/B3Provider/B3Provider.native.d.ts +6 -2
  34. package/dist/esm/global-account/react/components/B3Provider/B3Provider.native.js +24 -19
  35. package/dist/esm/global-account/react/components/B3Provider/types.d.ts +2 -0
  36. package/dist/esm/global-account/react/components/B3Provider/types.js +2 -0
  37. package/dist/esm/global-account/react/components/ManageAccount/BalanceContent.d.ts +1 -2
  38. package/dist/esm/global-account/react/components/ManageAccount/BalanceContent.js +2 -2
  39. package/dist/esm/global-account/react/components/ManageAccount/ManageAccount.js +1 -1
  40. package/dist/esm/global-account/react/components/SignInWithB3/SignIn.js +2 -2
  41. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3.js +2 -2
  42. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.d.ts +1 -1
  43. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +7 -23
  44. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Privy.d.ts +1 -2
  45. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Privy.js +4 -3
  46. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStep.d.ts +1 -2
  47. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStep.js +11 -46
  48. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStepCustom.d.ts +1 -2
  49. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStepCustom.js +3 -2
  50. package/dist/esm/global-account/react/components/custom/ManageAccountButton.js +3 -2
  51. package/dist/esm/global-account/react/hooks/useAuthentication.d.ts +51 -2
  52. package/dist/esm/global-account/react/hooks/useAuthentication.js +137 -87
  53. package/dist/esm/global-account/react/hooks/useHandleConnectWithPrivy.d.ts +1 -1
  54. package/dist/esm/global-account/react/hooks/useHandleConnectWithPrivy.js +3 -2
  55. package/dist/esm/global-account/react/hooks/useUserQuery.d.ts +58 -0
  56. package/dist/esm/global-account/react/hooks/useUserQuery.js +83 -0
  57. package/dist/esm/global-account/react/hooks/useWagmiConfig.d.ts +13 -0
  58. package/dist/esm/global-account/react/hooks/useWagmiConfig.js +39 -0
  59. package/dist/esm/global-account/react/stores/useModalStore.d.ts +0 -2
  60. package/dist/types/global-account/react/components/B3Provider/B3Provider.d.ts +3 -2
  61. package/dist/types/global-account/react/components/B3Provider/B3Provider.native.d.ts +6 -2
  62. package/dist/types/global-account/react/components/B3Provider/types.d.ts +2 -0
  63. package/dist/types/global-account/react/components/ManageAccount/BalanceContent.d.ts +1 -2
  64. package/dist/types/global-account/react/components/SignInWithB3/SignInWithB3Flow.d.ts +1 -1
  65. package/dist/types/global-account/react/components/SignInWithB3/SignInWithB3Privy.d.ts +1 -2
  66. package/dist/types/global-account/react/components/SignInWithB3/steps/LoginStep.d.ts +1 -2
  67. package/dist/types/global-account/react/components/SignInWithB3/steps/LoginStepCustom.d.ts +1 -2
  68. package/dist/types/global-account/react/hooks/useAuthentication.d.ts +51 -2
  69. package/dist/types/global-account/react/hooks/useHandleConnectWithPrivy.d.ts +1 -1
  70. package/dist/types/global-account/react/hooks/useUserQuery.d.ts +58 -0
  71. package/dist/types/global-account/react/hooks/useWagmiConfig.d.ts +13 -0
  72. package/dist/types/global-account/react/stores/useModalStore.d.ts +0 -2
  73. package/package.json +3 -3
  74. package/src/global-account/react/components/B3Provider/B3Provider.native.tsx +51 -35
  75. package/src/global-account/react/components/B3Provider/B3Provider.tsx +28 -72
  76. package/src/global-account/react/components/B3Provider/types.ts +4 -0
  77. package/src/global-account/react/components/ManageAccount/BalanceContent.tsx +2 -3
  78. package/src/global-account/react/components/ManageAccount/ManageAccount.tsx +1 -1
  79. package/src/global-account/react/components/SignInWithB3/SignIn.tsx +2 -2
  80. package/src/global-account/react/components/SignInWithB3/SignInWithB3.tsx +2 -2
  81. package/src/global-account/react/components/SignInWithB3/SignInWithB3Flow.tsx +4 -23
  82. package/src/global-account/react/components/SignInWithB3/SignInWithB3Privy.tsx +4 -3
  83. package/src/global-account/react/components/SignInWithB3/steps/LoginStep.tsx +8 -46
  84. package/src/global-account/react/components/SignInWithB3/steps/LoginStepCustom.tsx +2 -2
  85. package/src/global-account/react/components/custom/ManageAccountButton.tsx +3 -2
  86. package/src/global-account/react/hooks/useAuthentication.ts +159 -90
  87. package/src/global-account/react/hooks/useHandleConnectWithPrivy.tsx +3 -2
  88. package/src/global-account/react/hooks/useUserQuery.ts +95 -0
  89. package/src/global-account/react/hooks/useWagmiConfig.tsx +44 -0
  90. package/src/global-account/react/stores/useModalStore.ts +0 -2
@@ -5,7 +5,6 @@ import {
5
5
  useB3,
6
6
  useGetAllTWSigners,
7
7
  useModalStore,
8
- useSiwe,
9
8
  } from "@b3dotfun/sdk/global-account/react";
10
9
  import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
11
10
  import { useCallback, useEffect, useState } from "react";
@@ -31,19 +30,16 @@ export function SignInWithB3Flow({
31
30
  sessionKeyAddress,
32
31
  partnerId,
33
32
  closeAfterLogin = false,
34
- loginWithSiwe = false,
35
33
  source = "signInWithB3Button",
36
34
  signersEnabled = false,
37
35
  }: SignInWithB3ModalProps) {
38
- const { setUser, automaticallySetFirstEoa } = useB3();
36
+ const { automaticallySetFirstEoa } = useB3();
39
37
  const [step, setStep] = useState<"login" | "permissions" | null>(source === "requestPermissions" ? null : "login");
40
38
  const [sessionKeyAdded, setSessionKeyAdded] = useState(source === "requestPermissions" ? true : false);
41
39
  const { setB3ModalContentType, setB3ModalOpen, isOpen } = useModalStore();
42
40
  const account = useActiveAccount();
43
- const setIsAuthenticating = useAuthStore(state => state.setIsAuthenticating);
44
41
  const isAuthenticating = useAuthStore(state => state.isAuthenticating);
45
42
  const isConnected = useAuthStore(state => state.isConnected);
46
- const setIsConnected = useAuthStore(state => state.setIsConnected);
47
43
  const [refetchCount, setRefetchCount] = useState(0);
48
44
  const [refetchError, setRefetchError] = useState<string | null>(null);
49
45
  const {
@@ -57,7 +53,6 @@ export function SignInWithB3Flow({
57
53
  enabled: signersEnabled,
58
54
  },
59
55
  });
60
- const { authenticate } = useSiwe();
61
56
  const [refetchQueued, setRefetchQueued] = useState(false);
62
57
 
63
58
  // Enhanced refetchSigners function that tracks number of attempts
@@ -175,13 +170,11 @@ export function SignInWithB3Flow({
175
170
  sessionKeyAddress,
176
171
  partnerId,
177
172
  closeAfterLogin,
178
- loginWithSiwe,
179
173
  source: "requestPermissions",
180
174
  });
181
175
  }, [
182
176
  chain,
183
177
  closeAfterLogin,
184
- loginWithSiwe,
185
178
  onError,
186
179
  onLoginSuccess,
187
180
  onSessionKeySuccess,
@@ -193,20 +186,9 @@ export function SignInWithB3Flow({
193
186
 
194
187
  const handleLoginSuccess = useCallback(
195
188
  async (account: Account) => {
196
- debug("Authenticating with B3 via SIWE");
197
- setIsConnected(true);
198
- if (loginWithSiwe) {
199
- debug("setIsAuthenticating:true:1");
200
- setIsAuthenticating(true);
201
- const userAuth = await authenticate(account, partnerId);
202
- setUser(userAuth.user);
203
- }
204
- debug("handleLoginSuccess:account", account);
205
189
  onLoginSuccess?.(account);
206
- debug("setIsAuthenticating:false:1");
207
- setIsAuthenticating(false);
208
190
  },
209
- [loginWithSiwe, onLoginSuccess, authenticate, partnerId, setUser, setIsConnected, setIsAuthenticating],
191
+ [onLoginSuccess],
210
192
  );
211
193
 
212
194
  useEffect(() => {
@@ -243,7 +225,7 @@ export function SignInWithB3Flow({
243
225
  if (step === "login") {
244
226
  // Custom strategy
245
227
  if (strategies?.[0] === "privy") {
246
- return <SignInWithB3Privy onSuccess={handleLoginSuccess} partnerId={partnerId} chain={chain} />;
228
+ return <SignInWithB3Privy onSuccess={handleLoginSuccess} chain={chain} />;
247
229
  }
248
230
 
249
231
  // Strategies are explicitly provided
@@ -251,7 +233,6 @@ export function SignInWithB3Flow({
251
233
  return (
252
234
  <LoginStepCustom
253
235
  strategies={strategies}
254
- partnerId={partnerId}
255
236
  chain={chain}
256
237
  onSuccess={handleLoginSuccess}
257
238
  onError={onError}
@@ -261,7 +242,7 @@ export function SignInWithB3Flow({
261
242
  }
262
243
 
263
244
  // Default to handle all strategies we support
264
- return <LoginStep partnerId={partnerId} chain={chain} onSuccess={handleLoginSuccess} onError={onError} />;
245
+ return <LoginStep chain={chain} onSuccess={handleLoginSuccess} onError={onError} />;
265
246
  }
266
247
 
267
248
  return null;
@@ -2,6 +2,7 @@ import {
2
2
  Loading,
3
3
  useAuthentication,
4
4
  useAuthStore,
5
+ useB3,
5
6
  useHandleConnectWithPrivy,
6
7
  } from "@b3dotfun/sdk/global-account/react";
7
8
  import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
@@ -14,12 +15,12 @@ interface SignInWithB3PrivyProps {
14
15
  onError?: (error: Error) => Promise<void>;
15
16
  onSuccess: (account: Account) => Promise<void>;
16
17
  accessToken?: string;
17
- partnerId: string;
18
18
  chain: Chain;
19
19
  }
20
20
 
21
- export function SignInWithB3Privy({ onSuccess, onError, partnerId, chain }: SignInWithB3PrivyProps) {
22
- const { isLoading, connectTw, fullToken } = useHandleConnectWithPrivy(partnerId, chain, onSuccess);
21
+ export function SignInWithB3Privy({ onSuccess, onError, chain }: SignInWithB3PrivyProps) {
22
+ const { partnerId } = useB3();
23
+ const { isLoading, connectTw, fullToken } = useHandleConnectWithPrivy(chain, onSuccess);
23
24
  const setIsAuthenticating = useAuthStore(state => state.setIsAuthenticating);
24
25
  const setIsAuthenticated = useAuthStore(state => state.setIsAuthenticated);
25
26
  const { logout } = useAuthentication(partnerId);
@@ -1,6 +1,5 @@
1
- import { useAuthentication, useAuthStore, useB3, useQueryB3 } from "@b3dotfun/sdk/global-account/react";
1
+ import { useAuthentication, useB3, useQueryB3 } from "@b3dotfun/sdk/global-account/react";
2
2
  import { ecosystemWalletId } from "@b3dotfun/sdk/shared/constants";
3
- import { debug } from "@b3dotfun/sdk/shared/utils/debug";
4
3
  import { client } from "@b3dotfun/sdk/shared/utils/thirdweb";
5
4
  import { Chain } from "thirdweb";
6
5
  import { ConnectEmbed, darkTheme, lightTheme } from "thirdweb/react";
@@ -14,7 +13,6 @@ interface LoginStepProps {
14
13
  /** Optional callback function called when an error occurs */
15
14
  onError?: (error: Error) => Promise<void>;
16
15
  /** Partner ID used for authentication */
17
- partnerId: string;
18
16
  /** Blockchain chain information */
19
17
  chain: Chain;
20
18
  /** Optional authentication strategy options */
@@ -56,15 +54,12 @@ export function LoginStepContainer({ children, partnerId }: LoginStepContainerPr
56
54
  );
57
55
  }
58
56
 
59
- export function LoginStep({ onSuccess, onError, partnerId, chain }: LoginStepProps) {
57
+ export function LoginStep({ onSuccess, chain }: LoginStepProps) {
58
+ const { partnerId, theme } = useB3();
60
59
  const wallet = ecosystemWallet(ecosystemWalletId, {
61
60
  partnerId: partnerId,
62
61
  });
63
-
64
- const { theme } = useB3();
65
- const setIsAuthenticating = useAuthStore(state => state.setIsAuthenticating);
66
- const setIsAuthenticated = useAuthStore(state => state.setIsAuthenticated);
67
- const { logout } = useAuthentication(partnerId);
62
+ const { onConnect } = useAuthentication(partnerId);
68
63
 
69
64
  return (
70
65
  <LoginStepContainer partnerId={partnerId}>
@@ -86,54 +81,21 @@ export function LoginStep({ onSuccess, onError, partnerId, chain }: LoginStepPro
86
81
  },
87
82
  })
88
83
  }
89
- autoConnect
90
84
  style={{
91
85
  width: "100%",
92
86
  height: "100%",
93
87
  border: 0,
94
88
  }}
95
- // TODO: Integrate with SIWE in useSIWE
96
- // auth={{
97
- // isLoggedIn: async (address) => {
98
- // console.log("checking if logged in!", { address });
99
- // return await isLoggedIn();
100
- // },
101
- // doLogin: async (params) => {
102
- // console.log("logging in!");
103
- // await login(params);
104
- // },
105
- // getLoginPayload: async ({ address }) =>
106
- // generatePayload({ address }),
107
- // doLogout: async () => {
108
- // console.log("logging out!");
109
- // await logout();
110
- // },
111
- // }}
112
89
  header={{
113
90
  title: "Sign in with B3",
114
91
  titleIcon: "https://cdn.b3.fun/b3_logo.svg",
115
92
  }}
116
93
  className="b3-login-step"
117
94
  onConnect={async wallet => {
118
- try {
119
- setIsAuthenticating(true);
120
- debug("setIsAuthenticating:true:6");
121
-
122
- const account = wallet.getAccount();
123
- if (!account) throw new Error("No account found");
124
-
125
- await onSuccess(account);
126
- setIsAuthenticated(true);
127
-
128
- console.log("connected!", wallet.id);
129
- } catch (error) {
130
- await onError?.(error as Error);
131
- await logout();
132
- setIsAuthenticated(false);
133
- } finally {
134
- debug("setIsAuthenticating:false:6");
135
- setIsAuthenticating(false);
136
- }
95
+ await onConnect(wallet);
96
+ const account = wallet.getAccount();
97
+ if (!account) throw new Error("No account found");
98
+ await onSuccess(account);
137
99
  }}
138
100
  />
139
101
  </LoginStepContainer>
@@ -7,6 +7,7 @@ import {
7
7
  LoginStepContainer,
8
8
  useAuthentication,
9
9
  useAuthStore,
10
+ useB3,
10
11
  useConnect,
11
12
  WalletRow,
12
13
  } from "@b3dotfun/sdk/global-account/react";
@@ -21,7 +22,6 @@ interface LoginStepCustomProps {
21
22
  automaticallySetFirstEoa: boolean;
22
23
  onSuccess: (account: Account) => Promise<void>;
23
24
  onError?: (error: Error) => Promise<void>;
24
- partnerId: string;
25
25
  chain: Chain;
26
26
  strategies: AllowedStrategy[];
27
27
  maxInitialWallets?: number;
@@ -32,12 +32,12 @@ const debug = debugB3React("LoginStepCustom");
32
32
  export function LoginStepCustom({
33
33
  onSuccess,
34
34
  onError,
35
- partnerId,
36
35
  chain,
37
36
  strategies,
38
37
  maxInitialWallets = 2,
39
38
  automaticallySetFirstEoa,
40
39
  }: LoginStepCustomProps) {
40
+ const { partnerId } = useB3();
41
41
  const [isLoading, setIsLoading] = useState(false);
42
42
  const [showAllWallets, setShowAllWallets] = useState(false);
43
43
  const { connect } = useConnect(partnerId, chain);
@@ -1,10 +1,11 @@
1
- import { Button, StyleRoot, useAuthentication, useModalStore } from "@b3dotfun/sdk/global-account/react";
1
+ import { Button, StyleRoot, useAuthentication, useB3, useModalStore } from "@b3dotfun/sdk/global-account/react";
2
2
  import { cn } from "@b3dotfun/sdk/shared/utils";
3
3
  import { SignInWithB3Props } from "../SignInWithB3/SignInWithB3";
4
4
 
5
5
  export function ManageAccountButton(props: SignInWithB3Props & { className?: string }) {
6
+ const { partnerId } = useB3();
6
7
  const { setB3ModalOpen, setB3ModalContentType } = useModalStore();
7
- const { isConnected } = useAuthentication(props.partnerId, props.loginWithSiwe);
8
+ const { isConnected } = useAuthentication(partnerId);
8
9
 
9
10
  const handleClickManageAccount = () => {
10
11
  setB3ModalContentType({
@@ -1,124 +1,173 @@
1
1
  import app from "@b3dotfun/sdk/global-account/app";
2
2
  import { authenticateWithB3JWT } from "@b3dotfun/sdk/global-account/bsmnt";
3
- import { useAuthStore, useB3 } from "@b3dotfun/sdk/global-account/react";
3
+ import { useAuthStore, useSiwe } from "@b3dotfun/sdk/global-account/react";
4
4
  import { ecosystemWalletId } from "@b3dotfun/sdk/shared/constants";
5
- import { b3MainnetThirdWeb } from "@b3dotfun/sdk/shared/constants/chains/supported";
6
5
  import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
7
6
  import { client } from "@b3dotfun/sdk/shared/utils/thirdweb";
8
- import { useEffect, useRef } from "react";
9
- import { useActiveWallet, useAutoConnect, useConnectedWallets, useDisconnect } from "thirdweb/react";
10
- import { ecosystemWallet } from "thirdweb/wallets";
7
+ import { ConnectionOptions } from "@thirdweb-dev/wagmi-adapter";
8
+ import { getConnectors } from "@wagmi/core";
9
+ import { useCallback, useEffect, useRef } from "react";
10
+ import {
11
+ useActiveWallet,
12
+ useAutoConnect,
13
+ useConnectedWallets,
14
+ useDisconnect,
15
+ useSetActiveWallet,
16
+ } from "thirdweb/react";
17
+ import { ecosystemWallet, Wallet } from "thirdweb/wallets";
11
18
  import { preAuthenticate } from "thirdweb/wallets/in-app";
12
- import { useConnect } from "./useConnect";
13
- import { useSiwe } from "./useSiwe";
19
+ import { useAccount, useConnect } from "wagmi";
20
+ import { useUserQuery } from "./useUserQuery";
21
+ import { useWagmiConfig } from "./useWagmiConfig";
14
22
 
15
23
  const debug = debugB3React("useAuthentication");
16
24
 
17
- export function useAuthentication(partnerId: string, loginWithSiwe?: boolean) {
25
+ export function useAuthentication(partnerId: string) {
18
26
  const { disconnect } = useDisconnect();
19
27
  const wallets = useConnectedWallets();
20
28
  const activeWallet = useActiveWallet();
21
- const { authenticate } = useSiwe();
22
- const { setUser } = useB3();
23
29
  const isAuthenticated = useAuthStore(state => state.isAuthenticated);
24
30
  const setIsAuthenticated = useAuthStore(state => state.setIsAuthenticated);
25
- const setIsConnecting = useAuthStore(state => state.setIsConnecting);
26
31
  const setIsConnected = useAuthStore(state => state.setIsConnected);
27
32
  const isConnecting = useAuthStore(state => state.isConnecting);
28
33
  const isConnected = useAuthStore(state => state.isConnected);
29
- const useAutoConnectLoadingPrevious = useRef(false);
30
- const setIsAuthenticating = useAuthStore(state => state.setIsAuthenticating);
31
34
  const isAuthenticating = useAuthStore(state => state.isAuthenticating);
32
- const hasStartedConnecting = useAuthStore(state => state.hasStartedConnecting);
35
+ const setIsAuthenticating = useAuthStore(state => state.setIsAuthenticating);
33
36
  const setHasStartedConnecting = useAuthStore(state => state.setHasStartedConnecting);
34
- const { connect } = useConnect(partnerId, b3MainnetThirdWeb);
37
+ const setActiveWallet = useSetActiveWallet();
38
+ const hasStartedConnecting = useAuthStore(state => state.hasStartedConnecting);
39
+ const { authenticate } = useSiwe();
40
+ const { user, setUser } = useUserQuery();
41
+ const useAutoConnectLoadingPrevious = useRef(false);
42
+ const wagmiConfig = useWagmiConfig(partnerId);
43
+ const { connect } = useConnect();
44
+ const activeWagmiAccount = useAccount();
45
+ debug("@@activeWagmiAccount", activeWagmiAccount);
35
46
 
36
47
  const wallet = ecosystemWallet(ecosystemWalletId, {
37
48
  partnerId: partnerId,
38
49
  });
39
50
 
40
- const { isLoading: useAutoConnectLoading } = useAutoConnect({
41
- client,
42
- wallets: [wallet],
43
- onConnect: async wallet => {
51
+ const syncWagmi = useCallback(async () => {
52
+ function syncWagmiFunc() {
53
+ const connectors = getConnectors(wagmiConfig);
54
+ debug("@@syncWagmi", {
55
+ connectors,
56
+ wallets,
57
+ });
58
+
59
+ // For each that matchs a TW wallet on wallets, connect to the wagmi connector
60
+ // or, since ecosystem wallets is separate, connect those via in-app-wallet from wagmi
61
+ connectors.forEach(async connector => {
62
+ const twWallet = wallets.find(wallet => wallet.id === connector.id || connector.id === "in-app-wallet");
63
+ if (
64
+ twWallet &&
65
+ // If it's not an in-app wallet or it is the ecosystem wallet, connect
66
+ (connector.id !== "in-app-wallet" || (connector.id === "in-app-wallet" && twWallet.id === ecosystemWalletId))
67
+ ) {
68
+ try {
69
+ const options = {
70
+ wallet: twWallet, // the connected wallet
71
+ } satisfies ConnectionOptions;
72
+ debug("@@syncWagmi:connecting", { twWallet, connector });
73
+ connect({
74
+ connector,
75
+ ...options,
76
+ });
77
+ } catch (error) {
78
+ console.error("@@syncWagmi:error", error);
79
+ }
80
+ } else {
81
+ debug("@@syncWagmi:not-connecting", connector);
82
+ }
83
+ });
84
+ }
85
+ syncWagmiFunc();
86
+ // wagmi config shouldn't change
87
+ // eslint-disable-next-line react-hooks/exhaustive-deps
88
+ }, [partnerId, wallets]);
89
+
90
+ useEffect(() => {
91
+ syncWagmi();
92
+ }, [wallets, syncWagmi]);
93
+
94
+ const authenticateUser = useCallback(
95
+ async (wallet?: Wallet) => {
44
96
  setHasStartedConnecting(true);
45
97
 
98
+ const account = wallet ? wallet.getAccount() : activeWallet?.getAccount();
99
+ if (!account) {
100
+ throw new Error("No account found during auto-connect");
101
+ }
102
+ if (!account) {
103
+ throw new Error("No account found during auto-connect");
104
+ }
105
+
106
+ // Try to re-authenticate first
46
107
  try {
47
- setIsConnected(true);
48
- if (!loginWithSiwe) {
49
- debug("Skipping SIWE login", { loginWithSiwe });
50
- setIsAuthenticated(true);
108
+ const userAuth = await app.reAuthenticate();
109
+ setUser(userAuth.user);
110
+ setIsAuthenticated(true);
111
+ setIsAuthenticating(false);
112
+ debug("Re-authenticated successfully", { userAuth });
113
+
114
+ // Authenticate on BSMNT with B3 JWT
115
+ const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
116
+ debug("@@b3Jwt", b3Jwt);
117
+ } catch (error) {
118
+ // If re-authentication fails, try fresh authentication
119
+ debug("Re-authentication failed, attempting fresh authentication");
120
+ const userAuth = await authenticate(account, partnerId);
121
+ setUser(userAuth.user);
122
+ setIsAuthenticated(true);
123
+ setIsAuthenticating(false);
124
+ debug("Fresh authentication successful", { userAuth });
125
+
126
+ // Authenticate on BSMNT with B3 JWT
127
+ const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
128
+ debug("@@b3Jwt", b3Jwt);
129
+ }
130
+ },
131
+ [activeWallet, partnerId, authenticate, setIsAuthenticated, setIsAuthenticating, setUser, setHasStartedConnecting],
132
+ );
51
133
 
52
- setIsAuthenticating(false);
53
- return;
54
- }
55
- debug("setIsAuthenticating:true:4");
56
- const account = await wallet.getAccount();
57
- if (!account) {
58
- throw new Error("No account found during auto-connect");
59
- }
134
+ const onConnect = useCallback(
135
+ async (wallet: Wallet) => {
136
+ debug("@@useAuthentication:onConnect", { wallet });
60
137
 
61
- // Try to re-authenticate first
62
- try {
63
- const userAuth = await app.reAuthenticate();
64
- setUser(userAuth.user);
65
- setIsAuthenticated(true);
66
- setIsAuthenticating(false);
67
- debug("Re-authenticated successfully", { userAuth });
68
-
69
- // Authenticate on BSMNT with B3 JWT
70
- const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
71
- console.log("@@b3Jwt", b3Jwt);
72
- } catch (error) {
73
- // If re-authentication fails, try fresh authentication
74
- debug("Re-authentication failed, attempting fresh authentication");
75
- const userAuth = await authenticate(account, partnerId);
76
- setUser(userAuth.user);
77
- setIsAuthenticated(true);
78
- setIsAuthenticating(false);
79
- debug("Fresh authentication successful", { userAuth });
80
-
81
- // Authenticate on BSMNT with B3 JWT
82
- const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
83
- console.log("@@b3Jwt", b3Jwt);
84
- }
138
+ try {
139
+ setHasStartedConnecting(true);
140
+ setIsConnected(true);
141
+ setIsAuthenticating(true);
142
+ await setActiveWallet(wallet);
143
+ await authenticateUser(wallet);
85
144
  } catch (error) {
86
- debug("Auto-connect authentication failed", { error });
145
+ debug("@@useAuthentication:onConnect:failed", { error });
87
146
  setIsAuthenticated(false);
88
- debug("setIsAuthenticating:false:4");
89
- setUser();
147
+ setUser(undefined);
148
+ } finally {
149
+ setIsAuthenticating(false);
90
150
  }
91
- setIsAuthenticating(false);
92
- },
93
- });
94
151
 
95
- /**
96
- * useAutoConnectLoading starts as false
97
- */
98
- useEffect(() => {
99
- if (!useAutoConnectLoading && useAutoConnectLoadingPrevious.current && !hasStartedConnecting) {
100
- setIsAuthenticating(false);
101
- }
102
- useAutoConnectLoadingPrevious.current = useAutoConnectLoading;
103
- }, [useAutoConnectLoading, hasStartedConnecting, setIsAuthenticating]);
104
-
105
- // Ensure isAuthenticating stays true until we're fully ready
106
- useEffect(() => {
107
- if (useAutoConnectLoading) {
108
- setIsConnecting(true);
109
- } else if (!isAuthenticated) {
110
- // Only set isAuthenticating to false if we're not authenticated
111
- // This prevents the flicker state where both isAuthenticating and isAuthenticated are false
112
- const timeout = setTimeout(() => {
113
- debug("setIsAuthenticating:false:5a");
114
- setIsConnecting(false);
115
- }, 100); // Add a small delay to prevent quick flickers
116
- return () => clearTimeout(timeout);
117
- } else {
118
- debug("setIsAuthenticating:false:5b");
119
- setIsConnecting(false);
120
- }
121
- }, [useAutoConnectLoading, isAuthenticated, setIsConnecting, setIsConnected]);
152
+ debug({
153
+ isAuthenticated,
154
+ isAuthenticating,
155
+ isConnected,
156
+ });
157
+ },
158
+ [
159
+ authenticateUser,
160
+ isAuthenticated,
161
+ isAuthenticating,
162
+ isConnected,
163
+ setActiveWallet,
164
+ setHasStartedConnecting,
165
+ setIsAuthenticated,
166
+ setIsAuthenticating,
167
+ setIsConnected,
168
+ setUser,
169
+ ],
170
+ );
122
171
 
123
172
  const logout = async (callback?: () => void) => {
124
173
  if (activeWallet) {
@@ -150,7 +199,23 @@ export function useAuthentication(partnerId: string, loginWithSiwe?: boolean) {
150
199
  callback?.();
151
200
  };
152
201
 
153
- const isReady = isAuthenticated && !useAutoConnectLoading && !isAuthenticating;
202
+ const { isLoading: useAutoConnectLoading } = useAutoConnect({
203
+ client,
204
+ wallets: [wallet],
205
+ onConnect: onConnect,
206
+ });
207
+
208
+ /**
209
+ * useAutoConnectLoading starts as false
210
+ */
211
+ useEffect(() => {
212
+ if (!useAutoConnectLoading && useAutoConnectLoadingPrevious.current && !hasStartedConnecting) {
213
+ setIsAuthenticating(false);
214
+ }
215
+ useAutoConnectLoadingPrevious.current = useAutoConnectLoading;
216
+ }, [useAutoConnectLoading, hasStartedConnecting, setIsAuthenticating]);
217
+
218
+ const isReady = isAuthenticated && !isAuthenticating;
154
219
 
155
220
  return {
156
221
  logout,
@@ -160,7 +225,11 @@ export function useAuthentication(partnerId: string, loginWithSiwe?: boolean) {
160
225
  isConnected,
161
226
  wallet,
162
227
  preAuthenticate,
163
- connect,
228
+ connect: onConnect,
164
229
  isAuthenticating,
230
+ onConnect,
231
+ user,
232
+ refetchUser: authenticateUser,
233
+ setUser,
165
234
  };
166
235
  }
@@ -1,4 +1,4 @@
1
- import { useConnect } from "@b3dotfun/sdk/global-account/react";
1
+ import { useB3, useConnect } from "@b3dotfun/sdk/global-account/react";
2
2
  import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
3
3
  import { useIdentityToken, usePrivy } from "@privy-io/react-auth";
4
4
  import { useCallback, useRef, useState } from "react";
@@ -10,7 +10,8 @@ const debug = debugB3React("@@b3:useHandleConnectWithPrivy");
10
10
  * This essentially wraps our useConnect hook to handle the Privy auth flow.
11
11
  * Currently, this is for the basement-privy strategy
12
12
  */
13
- export function useHandleConnectWithPrivy(partnerId: string, chain?: Chain, onSuccess?: (account: Account) => void) {
13
+ export function useHandleConnectWithPrivy(chain?: Chain, onSuccess?: (account: Account) => void) {
14
+ const { partnerId } = useB3();
14
15
  if (!chain) {
15
16
  throw new Error("Chain is required");
16
17
  }
@@ -0,0 +1,95 @@
1
+ import { Users } from "@b3dotfun/b3-api";
2
+ import { debugB3React } from "@b3dotfun/sdk/shared/utils/debug";
3
+ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
4
+
5
+ const debug = debugB3React("useUserQuery");
6
+
7
+ const USER_QUERY_KEY = ["b3-user"];
8
+
9
+ /**
10
+ * Retrieves the user from localStorage
11
+ */
12
+ function getUserFromStorage(): Users | null {
13
+ if (typeof window === "undefined") {
14
+ return null;
15
+ }
16
+
17
+ try {
18
+ const storedUser = localStorage.getItem("b3-user");
19
+ return storedUser ? JSON.parse(storedUser) : null;
20
+ } catch (error) {
21
+ console.warn("Failed to restore user from localStorage:", error);
22
+ return null;
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Saves user to localStorage
28
+ */
29
+ function saveUserToStorage(user: Users | null) {
30
+ if (typeof window === "undefined") {
31
+ return;
32
+ }
33
+
34
+ if (user) {
35
+ localStorage.setItem("b3-user", JSON.stringify(user));
36
+ } else {
37
+ localStorage.removeItem("b3-user");
38
+ }
39
+ }
40
+
41
+ /**
42
+ * NOTE: THIS IS ONLY MEANT FOR INTERNAL USE, from useOnConnect
43
+ *
44
+ * Custom hook to manage user state with react-query
45
+ * This allows for invalidation and refetching of user data
46
+ */
47
+ export function useUserQuery() {
48
+ const queryClient = useQueryClient();
49
+
50
+ // Query to get user data (primarily from cache/localStorage)
51
+ const { data: user } = useQuery<Users | null>({
52
+ queryKey: USER_QUERY_KEY,
53
+ queryFn: getUserFromStorage,
54
+ staleTime: Infinity, // User data doesn't go stale automatically
55
+ gcTime: Infinity, // Keep in cache indefinitely
56
+ initialData: getUserFromStorage,
57
+ });
58
+
59
+ // Mutation to update user
60
+ const setUserMutation = useMutation({
61
+ mutationFn: async (newUser: Users | undefined) => {
62
+ const userToSave = newUser ?? null;
63
+ saveUserToStorage(userToSave);
64
+ return userToSave;
65
+ },
66
+ onSuccess: data => {
67
+ queryClient.setQueryData(USER_QUERY_KEY, data);
68
+ debug("User updated", data);
69
+ },
70
+ });
71
+
72
+ // Helper function to set user (maintains backward compatibility)
73
+ const setUser = (newUser?: Users) => {
74
+ setUserMutation.mutate(newUser);
75
+ };
76
+
77
+ // Helper function to invalidate and refetch user
78
+ const refetchUser = async () => {
79
+ await queryClient.invalidateQueries({ queryKey: USER_QUERY_KEY });
80
+ return queryClient.refetchQueries({ queryKey: USER_QUERY_KEY });
81
+ };
82
+
83
+ // Helper function to clear user
84
+ const clearUser = () => {
85
+ setUser(undefined);
86
+ };
87
+
88
+ return {
89
+ user: user ?? undefined,
90
+ setUser,
91
+ refetchUser,
92
+ clearUser,
93
+ queryKey: USER_QUERY_KEY,
94
+ };
95
+ }