@b3dotfun/sdk 0.0.47 → 0.0.48-alpha.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.
Files changed (151) hide show
  1. package/README.md +225 -6
  2. package/dist/cjs/anyspend/react/components/AnySpendCustom.js +5 -3
  3. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.d.ts +3 -2
  4. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.js +15 -64
  5. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.native.d.ts +6 -2
  6. package/dist/cjs/global-account/react/components/B3Provider/B3Provider.native.js +26 -21
  7. package/dist/cjs/global-account/react/components/B3Provider/types.d.ts +2 -0
  8. package/dist/cjs/global-account/react/components/B3Provider/types.js +2 -0
  9. package/dist/cjs/global-account/react/components/ManageAccount/BalanceContent.d.ts +1 -2
  10. package/dist/cjs/global-account/react/components/ManageAccount/BalanceContent.js +2 -2
  11. package/dist/cjs/global-account/react/components/ManageAccount/ManageAccount.js +1 -1
  12. package/dist/cjs/global-account/react/components/SignInWithB3/SignIn.js +2 -2
  13. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3.js +1 -1
  14. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.d.ts +1 -1
  15. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +6 -22
  16. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Privy.d.ts +1 -2
  17. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Privy.js +3 -2
  18. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStep.d.ts +1 -2
  19. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStep.js +10 -45
  20. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStepCustom.d.ts +1 -2
  21. package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStepCustom.js +2 -1
  22. package/dist/cjs/global-account/react/components/custom/ManageAccountButton.js +2 -1
  23. package/dist/cjs/global-account/react/hooks/useAuthentication.d.ts +51 -2
  24. package/dist/cjs/global-account/react/hooks/useAuthentication.js +141 -81
  25. package/dist/cjs/global-account/react/hooks/useHandleConnectWithPrivy.d.ts +1 -1
  26. package/dist/cjs/global-account/react/hooks/useHandleConnectWithPrivy.js +2 -1
  27. package/dist/cjs/global-account/react/hooks/useUserQuery.d.ts +58 -0
  28. package/dist/cjs/global-account/react/hooks/useUserQuery.js +86 -0
  29. package/dist/cjs/global-account/react/hooks/useWagmiConfig.d.ts +13 -0
  30. package/dist/cjs/global-account/react/hooks/useWagmiConfig.js +42 -0
  31. package/dist/cjs/global-account/react/stores/useModalStore.d.ts +0 -2
  32. package/dist/cjs/notifications/index.d.ts +3 -0
  33. package/dist/cjs/notifications/index.js +25 -0
  34. package/dist/cjs/notifications/react/hooks/index.d.ts +1 -0
  35. package/dist/cjs/notifications/react/hooks/index.js +17 -0
  36. package/dist/cjs/notifications/react/hooks/useNotifications.d.ts +42 -0
  37. package/dist/cjs/notifications/react/hooks/useNotifications.js +148 -0
  38. package/dist/cjs/notifications/react/index.d.ts +1 -0
  39. package/dist/cjs/notifications/react/index.js +17 -0
  40. package/dist/cjs/notifications/services/api.d.ts +67 -0
  41. package/dist/cjs/notifications/services/api.js +184 -0
  42. package/dist/cjs/notifications/services/index.d.ts +1 -0
  43. package/dist/cjs/notifications/services/index.js +17 -0
  44. package/dist/cjs/notifications/types/index.d.ts +51 -0
  45. package/dist/cjs/notifications/types/index.js +2 -0
  46. package/dist/cjs/shared/utils/auth-token.d.ts +7 -0
  47. package/dist/cjs/shared/utils/auth-token.js +17 -0
  48. package/dist/cjs/shared/utils/index.d.ts +1 -0
  49. package/dist/cjs/shared/utils/index.js +1 -0
  50. package/dist/esm/anyspend/react/components/AnySpendCustom.js +5 -3
  51. package/dist/esm/global-account/react/components/B3Provider/B3Provider.d.ts +3 -2
  52. package/dist/esm/global-account/react/components/B3Provider/B3Provider.js +19 -68
  53. package/dist/esm/global-account/react/components/B3Provider/B3Provider.native.d.ts +6 -2
  54. package/dist/esm/global-account/react/components/B3Provider/B3Provider.native.js +24 -19
  55. package/dist/esm/global-account/react/components/B3Provider/types.d.ts +2 -0
  56. package/dist/esm/global-account/react/components/B3Provider/types.js +2 -0
  57. package/dist/esm/global-account/react/components/ManageAccount/BalanceContent.d.ts +1 -2
  58. package/dist/esm/global-account/react/components/ManageAccount/BalanceContent.js +2 -2
  59. package/dist/esm/global-account/react/components/ManageAccount/ManageAccount.js +1 -1
  60. package/dist/esm/global-account/react/components/SignInWithB3/SignIn.js +2 -2
  61. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3.js +2 -2
  62. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.d.ts +1 -1
  63. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +7 -23
  64. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Privy.d.ts +1 -2
  65. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Privy.js +4 -3
  66. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStep.d.ts +1 -2
  67. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStep.js +11 -46
  68. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStepCustom.d.ts +1 -2
  69. package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStepCustom.js +3 -2
  70. package/dist/esm/global-account/react/components/custom/ManageAccountButton.js +3 -2
  71. package/dist/esm/global-account/react/hooks/useAuthentication.d.ts +51 -2
  72. package/dist/esm/global-account/react/hooks/useAuthentication.js +144 -84
  73. package/dist/esm/global-account/react/hooks/useHandleConnectWithPrivy.d.ts +1 -1
  74. package/dist/esm/global-account/react/hooks/useHandleConnectWithPrivy.js +3 -2
  75. package/dist/esm/global-account/react/hooks/useUserQuery.d.ts +58 -0
  76. package/dist/esm/global-account/react/hooks/useUserQuery.js +83 -0
  77. package/dist/esm/global-account/react/hooks/useWagmiConfig.d.ts +13 -0
  78. package/dist/esm/global-account/react/hooks/useWagmiConfig.js +39 -0
  79. package/dist/esm/global-account/react/stores/useModalStore.d.ts +0 -2
  80. package/dist/esm/notifications/index.d.ts +3 -0
  81. package/dist/esm/notifications/index.js +7 -0
  82. package/dist/esm/notifications/react/hooks/index.d.ts +1 -0
  83. package/dist/esm/notifications/react/hooks/index.js +1 -0
  84. package/dist/esm/notifications/react/hooks/useNotifications.d.ts +42 -0
  85. package/dist/esm/notifications/react/hooks/useNotifications.js +145 -0
  86. package/dist/esm/notifications/react/index.d.ts +1 -0
  87. package/dist/esm/notifications/react/index.js +1 -0
  88. package/dist/esm/notifications/services/api.d.ts +67 -0
  89. package/dist/esm/notifications/services/api.js +179 -0
  90. package/dist/esm/notifications/services/index.d.ts +1 -0
  91. package/dist/esm/notifications/services/index.js +1 -0
  92. package/dist/esm/notifications/types/index.d.ts +51 -0
  93. package/dist/esm/shared/utils/auth-token.d.ts +7 -0
  94. package/dist/esm/shared/utils/auth-token.js +11 -0
  95. package/dist/esm/shared/utils/index.d.ts +1 -0
  96. package/dist/esm/shared/utils/index.js +1 -0
  97. package/dist/types/global-account/react/components/B3Provider/B3Provider.d.ts +3 -2
  98. package/dist/types/global-account/react/components/B3Provider/B3Provider.native.d.ts +6 -2
  99. package/dist/types/global-account/react/components/B3Provider/types.d.ts +2 -0
  100. package/dist/types/global-account/react/components/ManageAccount/BalanceContent.d.ts +1 -2
  101. package/dist/types/global-account/react/components/SignInWithB3/SignInWithB3Flow.d.ts +1 -1
  102. package/dist/types/global-account/react/components/SignInWithB3/SignInWithB3Privy.d.ts +1 -2
  103. package/dist/types/global-account/react/components/SignInWithB3/steps/LoginStep.d.ts +1 -2
  104. package/dist/types/global-account/react/components/SignInWithB3/steps/LoginStepCustom.d.ts +1 -2
  105. package/dist/types/global-account/react/hooks/useAuthentication.d.ts +51 -2
  106. package/dist/types/global-account/react/hooks/useHandleConnectWithPrivy.d.ts +1 -1
  107. package/dist/types/global-account/react/hooks/useUserQuery.d.ts +58 -0
  108. package/dist/types/global-account/react/hooks/useWagmiConfig.d.ts +13 -0
  109. package/dist/types/global-account/react/stores/useModalStore.d.ts +0 -2
  110. package/dist/types/notifications/index.d.ts +3 -0
  111. package/dist/types/notifications/react/hooks/index.d.ts +1 -0
  112. package/dist/types/notifications/react/hooks/useNotifications.d.ts +42 -0
  113. package/dist/types/notifications/react/index.d.ts +1 -0
  114. package/dist/types/notifications/services/api.d.ts +67 -0
  115. package/dist/types/notifications/services/index.d.ts +1 -0
  116. package/dist/types/notifications/types/index.d.ts +51 -0
  117. package/dist/types/shared/utils/auth-token.d.ts +7 -0
  118. package/dist/types/shared/utils/index.d.ts +1 -0
  119. package/package.json +23 -3
  120. package/src/anyspend/react/components/AnySpendCustom.tsx +5 -3
  121. package/src/global-account/react/components/B3Provider/B3Provider.native.tsx +51 -35
  122. package/src/global-account/react/components/B3Provider/B3Provider.tsx +28 -72
  123. package/src/global-account/react/components/B3Provider/types.ts +4 -0
  124. package/src/global-account/react/components/ManageAccount/BalanceContent.tsx +2 -3
  125. package/src/global-account/react/components/ManageAccount/ManageAccount.tsx +1 -1
  126. package/src/global-account/react/components/SignInWithB3/SignIn.tsx +2 -2
  127. package/src/global-account/react/components/SignInWithB3/SignInWithB3.tsx +2 -2
  128. package/src/global-account/react/components/SignInWithB3/SignInWithB3Flow.tsx +4 -23
  129. package/src/global-account/react/components/SignInWithB3/SignInWithB3Privy.tsx +4 -3
  130. package/src/global-account/react/components/SignInWithB3/steps/LoginStep.tsx +8 -46
  131. package/src/global-account/react/components/SignInWithB3/steps/LoginStepCustom.tsx +2 -2
  132. package/src/global-account/react/components/custom/ManageAccountButton.tsx +3 -2
  133. package/src/global-account/react/hooks/useAuthentication.ts +170 -89
  134. package/src/global-account/react/hooks/useHandleConnectWithPrivy.tsx +3 -2
  135. package/src/global-account/react/hooks/useUserQuery.ts +95 -0
  136. package/src/global-account/react/hooks/useWagmiConfig.tsx +44 -0
  137. package/src/global-account/react/stores/useModalStore.ts +0 -2
  138. package/src/notifications/index.ts +9 -0
  139. package/src/notifications/react/hooks/index.ts +1 -0
  140. package/src/notifications/react/hooks/useNotifications.ts +153 -0
  141. package/src/notifications/react/index.ts +1 -0
  142. package/src/notifications/services/api.ts +217 -0
  143. package/src/notifications/services/index.ts +1 -0
  144. package/src/notifications/types/index.ts +58 -0
  145. package/src/shared/utils/auth-token.ts +13 -0
  146. package/src/shared/utils/index.ts +1 -0
  147. package/dist/cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.js +0 -245
  148. package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +0 -1
  149. package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.js +0 -243
  150. package/dist/types/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +0 -1
  151. /package/dist/{cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts → esm/notifications/types/index.js} +0 -0
@@ -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,185 @@
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, useSwitchAccount } 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
+ const { switchAccount } = useSwitchAccount();
46
+ debug("@@activeWagmiAccount", activeWagmiAccount);
35
47
 
36
48
  const wallet = ecosystemWallet(ecosystemWalletId, {
37
49
  partnerId: partnerId,
38
50
  });
39
51
 
40
- const { isLoading: useAutoConnectLoading } = useAutoConnect({
41
- client,
42
- wallets: [wallet],
43
- onConnect: async wallet => {
44
- setHasStartedConnecting(true);
45
-
46
- try {
47
- setIsConnected(true);
48
- if (!loginWithSiwe) {
49
- debug("Skipping SIWE login", { loginWithSiwe });
50
- setIsAuthenticated(true);
51
-
52
- setIsAuthenticating(false);
52
+ const syncWagmi = useCallback(async () => {
53
+ function syncWagmiFunc() {
54
+ const connectors = getConnectors(wagmiConfig);
55
+ debug("@@syncWagmi", {
56
+ connectors,
57
+ wallets,
58
+ });
59
+
60
+ // For each that matchs a TW wallet on wallets, connect to the wagmi connector
61
+ // or, since ecosystem wallets is separate, connect those via in-app-wallet from wagmi
62
+ connectors.forEach(async connector => {
63
+ const twWallet = wallets.find(wallet => wallet.id === connector.id || connector.id === "in-app-wallet");
64
+
65
+ // If no TW wallet, do not prompt the user to connect
66
+ if (!twWallet) {
53
67
  return;
54
68
  }
55
- debug("setIsAuthenticating:true:4");
56
- const account = await wallet.getAccount();
57
- if (!account) {
58
- throw new Error("No account found during auto-connect");
69
+
70
+ // Metamask will prompt to connect, we can just switch accounts here.
71
+ if (connector.id === "io.metamask") {
72
+ return switchAccount({ connector });
59
73
  }
60
74
 
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);
75
+ if (
76
+ // If it's not an in-app wallet or it is the ecosystem wallet, connect
77
+ connector.id !== "in-app-wallet" ||
78
+ (connector.id === "in-app-wallet" && twWallet.id === ecosystemWalletId)
79
+ ) {
80
+ try {
81
+ const options = {
82
+ wallet: twWallet, // the connected wallet
83
+ } satisfies ConnectionOptions;
84
+ debug("@@syncWagmi:connecting", { twWallet, connector });
85
+ connect({
86
+ connector,
87
+ ...options,
88
+ });
89
+ } catch (error) {
90
+ console.error("@@syncWagmi:error", error);
91
+ }
92
+ } else {
93
+ debug("@@syncWagmi:not-connecting", connector);
84
94
  }
95
+ });
96
+ }
97
+ syncWagmiFunc();
98
+ // wagmi config shouldn't change
99
+ // eslint-disable-next-line react-hooks/exhaustive-deps
100
+ }, [partnerId, wallets]);
101
+
102
+ useEffect(() => {
103
+ syncWagmi();
104
+ }, [wallets, syncWagmi]);
105
+
106
+ const authenticateUser = useCallback(
107
+ async (wallet?: Wallet) => {
108
+ setHasStartedConnecting(true);
109
+
110
+ const account = wallet ? wallet.getAccount() : activeWallet?.getAccount();
111
+ if (!account) {
112
+ throw new Error("No account found during auto-connect");
113
+ }
114
+ if (!account) {
115
+ throw new Error("No account found during auto-connect");
116
+ }
117
+
118
+ // Try to re-authenticate first
119
+ try {
120
+ const userAuth = await app.reAuthenticate();
121
+ setUser(userAuth.user);
122
+ setIsAuthenticated(true);
123
+ setIsAuthenticating(false);
124
+ debug("Re-authenticated successfully", { userAuth });
125
+
126
+ // Authenticate on BSMNT with B3 JWT
127
+ const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
128
+ debug("@@b3Jwt", b3Jwt);
85
129
  } catch (error) {
86
- debug("Auto-connect authentication failed", { error });
87
- setIsAuthenticated(false);
88
- debug("setIsAuthenticating:false:4");
89
- setUser();
130
+ // If re-authentication fails, try fresh authentication
131
+ debug("Re-authentication failed, attempting fresh authentication");
132
+ const userAuth = await authenticate(account, partnerId);
133
+ setUser(userAuth.user);
134
+ setIsAuthenticated(true);
135
+ setIsAuthenticating(false);
136
+ debug("Fresh authentication successful", { userAuth });
137
+
138
+ // Authenticate on BSMNT with B3 JWT
139
+ const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
140
+ debug("@@b3Jwt", b3Jwt);
90
141
  }
91
- setIsAuthenticating(false);
92
142
  },
93
- });
143
+ [activeWallet, partnerId, authenticate, setIsAuthenticated, setIsAuthenticating, setUser, setHasStartedConnecting],
144
+ );
94
145
 
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]);
146
+ const onConnect = useCallback(
147
+ async (wallet: Wallet) => {
148
+ debug("@@useAuthentication:onConnect", { wallet });
104
149
 
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]);
150
+ try {
151
+ setHasStartedConnecting(true);
152
+ setIsConnected(true);
153
+ setIsAuthenticating(true);
154
+ await setActiveWallet(wallet);
155
+ await authenticateUser(wallet);
156
+ } catch (error) {
157
+ debug("@@useAuthentication:onConnect:failed", { error });
158
+ setIsAuthenticated(false);
159
+ setUser(undefined);
160
+ } finally {
161
+ setIsAuthenticating(false);
162
+ }
163
+
164
+ debug({
165
+ isAuthenticated,
166
+ isAuthenticating,
167
+ isConnected,
168
+ });
169
+ },
170
+ [
171
+ authenticateUser,
172
+ isAuthenticated,
173
+ isAuthenticating,
174
+ isConnected,
175
+ setActiveWallet,
176
+ setHasStartedConnecting,
177
+ setIsAuthenticated,
178
+ setIsAuthenticating,
179
+ setIsConnected,
180
+ setUser,
181
+ ],
182
+ );
122
183
 
123
184
  const logout = async (callback?: () => void) => {
124
185
  if (activeWallet) {
@@ -150,7 +211,23 @@ export function useAuthentication(partnerId: string, loginWithSiwe?: boolean) {
150
211
  callback?.();
151
212
  };
152
213
 
153
- const isReady = isAuthenticated && !useAutoConnectLoading && !isAuthenticating;
214
+ const { isLoading: useAutoConnectLoading } = useAutoConnect({
215
+ client,
216
+ wallets: [wallet],
217
+ onConnect: onConnect,
218
+ });
219
+
220
+ /**
221
+ * useAutoConnectLoading starts as false
222
+ */
223
+ useEffect(() => {
224
+ if (!useAutoConnectLoading && useAutoConnectLoadingPrevious.current && !hasStartedConnecting) {
225
+ setIsAuthenticating(false);
226
+ }
227
+ useAutoConnectLoadingPrevious.current = useAutoConnectLoading;
228
+ }, [useAutoConnectLoading, hasStartedConnecting, setIsAuthenticating]);
229
+
230
+ const isReady = isAuthenticated && !isAuthenticating;
154
231
 
155
232
  return {
156
233
  logout,
@@ -160,7 +237,11 @@ export function useAuthentication(partnerId: string, loginWithSiwe?: boolean) {
160
237
  isConnected,
161
238
  wallet,
162
239
  preAuthenticate,
163
- connect,
240
+ connect: onConnect,
164
241
  isAuthenticating,
242
+ onConnect,
243
+ user,
244
+ refetchUser: authenticateUser,
245
+ setUser,
165
246
  };
166
247
  }
@@ -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
  }