@b3dotfun/sdk 0.0.83-test.0 → 0.0.83

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 (68) hide show
  1. package/dist/cjs/anyspend/react/components/AnySpend.js +0 -3
  2. package/dist/cjs/anyspend/react/components/AnySpendCollectorClubPurchase.js +1 -1
  3. package/dist/cjs/anyspend/react/components/AnySpendCustom.js +0 -3
  4. package/dist/cjs/anyspend/react/components/common/CryptoPaymentMethod.js +47 -177
  5. package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.js +0 -3
  6. package/dist/cjs/anyspend/utils/chain.d.ts +38 -0
  7. package/dist/cjs/anyspend/utils/chain.js +22 -1
  8. package/dist/cjs/anyspend/utils/token.d.ts +3 -0
  9. package/dist/cjs/anyspend/utils/token.js +17 -1
  10. package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +12 -57
  11. package/dist/cjs/global-account/react/hooks/index.d.ts +0 -1
  12. package/dist/cjs/global-account/react/hooks/index.js +1 -3
  13. package/dist/cjs/global-account/react/hooks/useAuthentication.d.ts +2 -2
  14. package/dist/cjs/global-account/react/hooks/useAuthentication.js +63 -72
  15. package/dist/cjs/global-account/react/hooks/useTWAuth.d.ts +0 -3
  16. package/dist/cjs/global-account/react/hooks/useTWAuth.js +0 -8
  17. package/dist/cjs/global-account/react/hooks/useTurnkeyAuth.js +24 -54
  18. package/dist/cjs/global-account/react/hooks/useUserQuery.d.ts +1 -1
  19. package/dist/esm/anyspend/react/components/AnySpend.js +0 -3
  20. package/dist/esm/anyspend/react/components/AnySpendCollectorClubPurchase.js +1 -1
  21. package/dist/esm/anyspend/react/components/AnySpendCustom.js +0 -3
  22. package/dist/esm/anyspend/react/components/common/CryptoPaymentMethod.js +45 -175
  23. package/dist/esm/anyspend/react/hooks/useAnyspendFlow.js +0 -3
  24. package/dist/esm/anyspend/utils/chain.d.ts +38 -0
  25. package/dist/esm/anyspend/utils/chain.js +22 -1
  26. package/dist/esm/anyspend/utils/token.d.ts +3 -0
  27. package/dist/esm/anyspend/utils/token.js +15 -1
  28. package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +12 -57
  29. package/dist/esm/global-account/react/hooks/index.d.ts +0 -1
  30. package/dist/esm/global-account/react/hooks/index.js +0 -1
  31. package/dist/esm/global-account/react/hooks/useAuthentication.d.ts +2 -2
  32. package/dist/esm/global-account/react/hooks/useAuthentication.js +63 -72
  33. package/dist/esm/global-account/react/hooks/useTWAuth.d.ts +0 -3
  34. package/dist/esm/global-account/react/hooks/useTWAuth.js +0 -8
  35. package/dist/esm/global-account/react/hooks/useTurnkeyAuth.js +24 -54
  36. package/dist/esm/global-account/react/hooks/useUserQuery.d.ts +1 -1
  37. package/dist/styles/index.css +1 -1
  38. package/dist/types/anyspend/utils/chain.d.ts +38 -0
  39. package/dist/types/anyspend/utils/token.d.ts +3 -0
  40. package/dist/types/global-account/react/hooks/index.d.ts +0 -1
  41. package/dist/types/global-account/react/hooks/useAuthentication.d.ts +2 -2
  42. package/dist/types/global-account/react/hooks/useTWAuth.d.ts +0 -3
  43. package/dist/types/global-account/react/hooks/useUserQuery.d.ts +1 -1
  44. package/package.json +1 -1
  45. package/src/anyspend/react/components/AnySpend.tsx +0 -4
  46. package/src/anyspend/react/components/AnySpendCollectorClubPurchase.tsx +1 -1
  47. package/src/anyspend/react/components/AnySpendCustom.tsx +0 -4
  48. package/src/anyspend/react/components/common/CryptoPaymentMethod.tsx +22 -253
  49. package/src/anyspend/react/hooks/useAnyspendFlow.ts +0 -4
  50. package/src/anyspend/utils/chain.ts +31 -1
  51. package/src/anyspend/utils/token.ts +17 -1
  52. package/src/global-account/react/components/SignInWithB3/SignInWithB3Flow.tsx +22 -77
  53. package/src/global-account/react/hooks/index.ts +0 -1
  54. package/src/global-account/react/hooks/useAuthentication.ts +85 -88
  55. package/src/global-account/react/hooks/useTWAuth.tsx +0 -10
  56. package/src/global-account/react/hooks/useTurnkeyAuth.ts +26 -59
  57. package/dist/cjs/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +0 -10
  58. package/dist/cjs/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.js +0 -73
  59. package/dist/cjs/global-account/react/hooks/useAuth.d.ts +0 -76
  60. package/dist/cjs/global-account/react/hooks/useAuth.js +0 -338
  61. package/dist/esm/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +0 -10
  62. package/dist/esm/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.js +0 -70
  63. package/dist/esm/global-account/react/hooks/useAuth.d.ts +0 -76
  64. package/dist/esm/global-account/react/hooks/useAuth.js +0 -332
  65. package/dist/types/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +0 -10
  66. package/dist/types/global-account/react/hooks/useAuth.d.ts +0 -76
  67. package/src/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.ts +0 -80
  68. package/src/global-account/react/hooks/useAuth.ts +0 -380
@@ -12,7 +12,7 @@ import { preAuthenticate } from "thirdweb/wallets/in-app";
12
12
  import { useAccount, useConnect, useSwitchAccount } from "wagmi";
13
13
  import { LocalSDKContext } from "../components/B3Provider/LocalSDKProvider.js";
14
14
  import { createWagmiConfig } from "../utils/createWagmiConfig.js";
15
- import { useAuth } from "./useAuth.js";
15
+ import { useTWAuth } from "./useTWAuth.js";
16
16
  import { useUserQuery } from "./useUserQuery.js";
17
17
  const debug = debugB3React("useAuthentication");
18
18
  export function useAuthentication(partnerId) {
@@ -30,7 +30,7 @@ export function useAuthentication(partnerId) {
30
30
  const setHasStartedConnecting = useAuthStore(state => state.setHasStartedConnecting);
31
31
  const setActiveWallet = useSetActiveWallet();
32
32
  const hasStartedConnecting = useAuthStore(state => state.hasStartedConnecting);
33
- const { reAuthenticate } = useAuth();
33
+ const { authenticate } = useTWAuth();
34
34
  const { user, setUser } = useUserQuery();
35
35
  const useAutoConnectLoadingPrevious = useRef(false);
36
36
  const wagmiConfig = createWagmiConfig({ partnerId });
@@ -90,19 +90,18 @@ export function useAuthentication(partnerId) {
90
90
  useEffect(() => {
91
91
  syncWagmi();
92
92
  }, [wallets, syncWagmi]);
93
- /**
94
- * Authenticate user using Turnkey
95
- * Note: This no longer requires a wallet for authentication.
96
- * Wallets are still used for signing transactions, but authentication is done via Turnkey email OTP.
97
- *
98
- * For backward compatibility, this function still accepts a wallet parameter,
99
- * but it's not used for authentication anymore.
100
- */
101
- const authenticateUser = useCallback(async () => {
93
+ const authenticateUser = useCallback(async (wallet) => {
102
94
  setHasStartedConnecting(true);
95
+ if (!wallet) {
96
+ throw new Error("No wallet found during auto-connect");
97
+ }
98
+ const account = wallet ? wallet.getAccount() : activeWallet?.getAccount();
99
+ if (!account) {
100
+ throw new Error("No account found during auto-connect");
101
+ }
103
102
  // Try to re-authenticate first
104
103
  try {
105
- const userAuth = await reAuthenticate();
104
+ const userAuth = await app.reAuthenticate();
106
105
  setUser(userAuth.user);
107
106
  setIsAuthenticated(true);
108
107
  setIsAuthenticating(false);
@@ -113,50 +112,67 @@ export function useAuthentication(partnerId) {
113
112
  return userAuth;
114
113
  }
115
114
  catch (error) {
116
- // If re-authentication fails, user needs to authenticate via Turnkey
117
- // This should be handled by the Turnkey auth modal/flow
118
- debug("Re-authentication failed. User needs to authenticate via Turnkey.", error);
119
- setIsAuthenticated(false);
115
+ // If re-authentication fails, try fresh authentication
116
+ debug("Re-authentication failed, attempting fresh authentication");
117
+ const userAuth = await authenticate(wallet, partnerId);
118
+ setUser(userAuth.user);
119
+ setIsAuthenticated(true);
120
120
  setIsAuthenticating(false);
121
- throw new Error("Authentication required. Please authenticate via Turnkey.");
121
+ debug("Fresh authentication successful", { userAuth });
122
+ // Authenticate on BSMNT with B3 JWT
123
+ const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
124
+ debug("@@b3Jwt", b3Jwt);
125
+ return userAuth;
122
126
  }
123
- }, [reAuthenticate, setIsAuthenticated, setIsAuthenticating, setUser, setHasStartedConnecting]);
124
- /**
125
- * Handle wallet connection
126
- * Note: With Turnkey migration, wallet connection is primarily for signing transactions,
127
- * not for authentication. Authentication should be done separately via Turnkey email OTP.
128
- */
127
+ }, [activeWallet, partnerId, authenticate, setIsAuthenticated, setIsAuthenticating, setUser, setHasStartedConnecting]);
128
+ const logout = useCallback(async (callback) => {
129
+ if (activeWallet) {
130
+ debug("@@logout:activeWallet", activeWallet);
131
+ disconnect(activeWallet);
132
+ debug("@@logout:activeWallet", activeWallet);
133
+ }
134
+ // Log out of each wallet
135
+ wallets.forEach(wallet => {
136
+ console.log("@@logging out", wallet);
137
+ disconnect(wallet);
138
+ });
139
+ // Delete localStorage thirdweb:connected-wallet-ids
140
+ // https://npc-labs.slack.com/archives/C070E6HNG85/p1750185115273099
141
+ if (typeof localStorage !== "undefined") {
142
+ localStorage.removeItem("thirdweb:connected-wallet-ids");
143
+ localStorage.removeItem("wagmi.store");
144
+ localStorage.removeItem("lastAuthProvider");
145
+ localStorage.removeItem("b3-user");
146
+ }
147
+ app.logout();
148
+ debug("@@logout:loggedOut");
149
+ setIsAuthenticated(false);
150
+ setIsConnected(false);
151
+ setUser();
152
+ callback?.();
153
+ }, [activeWallet, disconnect, wallets, setIsAuthenticated, setUser, setIsConnected]);
129
154
  const onConnect = useCallback(async (_walleAutoConnectedWith, allConnectedWallets) => {
130
155
  debug("@@useAuthentication:onConnect", { _walleAutoConnectedWith, allConnectedWallets });
131
- const wallet = allConnectedWallets.find(wallet => wallet.id.startsWith("ecosystem."));
132
- if (!wallet) {
133
- throw new Error("No smart wallet found during auto-connect");
134
- }
135
- debug("@@useAuthentication:onConnect", { wallet });
136
156
  try {
157
+ const wallet = allConnectedWallets.find(wallet => wallet.id.startsWith("ecosystem."));
158
+ if (!wallet) {
159
+ throw new Error("No smart wallet found during auto-connect");
160
+ }
161
+ debug("@@useAuthentication:onConnect", { wallet });
137
162
  setHasStartedConnecting(true);
138
163
  setIsConnected(true);
139
164
  setIsAuthenticating(true);
140
165
  await setActiveWallet(wallet);
141
- // Try to authenticate user (will use re-authenticate if session exists)
142
- // If no session exists, authentication will need to happen via Turnkey flow
143
- try {
144
- const userAuth = await authenticateUser();
145
- if (userAuth && onConnectCallback) {
146
- await onConnectCallback(wallet, userAuth.accessToken);
147
- }
148
- }
149
- catch (authError) {
150
- // Authentication failed - this is expected if user hasn't authenticated via Turnkey yet
151
- // The Turnkey auth modal should handle this
152
- debug("@@useAuthentication:onConnect:authFailed", { authError });
153
- // Don't set isAuthenticated to false here - let the Turnkey flow handle it
166
+ const userAuth = await authenticateUser(wallet);
167
+ if (userAuth && onConnectCallback) {
168
+ await onConnectCallback(wallet, userAuth.accessToken);
154
169
  }
155
170
  }
156
171
  catch (error) {
157
172
  debug("@@useAuthentication:onConnect:failed", { error });
158
173
  setIsAuthenticated(false);
159
174
  setUser(undefined);
175
+ await logout();
160
176
  }
161
177
  finally {
162
178
  setIsAuthenticating(false);
@@ -167,44 +183,19 @@ export function useAuthentication(partnerId) {
167
183
  isConnected,
168
184
  });
169
185
  }, [
170
- onConnectCallback,
171
- authenticateUser,
172
186
  isAuthenticated,
173
187
  isAuthenticating,
174
188
  isConnected,
175
- setActiveWallet,
176
189
  setHasStartedConnecting,
177
- setIsAuthenticated,
178
- setIsAuthenticating,
179
190
  setIsConnected,
191
+ setIsAuthenticating,
192
+ setActiveWallet,
193
+ authenticateUser,
194
+ onConnectCallback,
195
+ setIsAuthenticated,
180
196
  setUser,
197
+ logout,
181
198
  ]);
182
- const logout = useCallback(async (callback) => {
183
- if (activeWallet) {
184
- debug("@@logout:activeWallet", activeWallet);
185
- disconnect(activeWallet);
186
- debug("@@logout:activeWallet", activeWallet);
187
- }
188
- // Log out of each wallet
189
- wallets.forEach(wallet => {
190
- console.log("@@logging out", wallet);
191
- disconnect(wallet);
192
- });
193
- // Delete localStorage thirdweb:connected-wallet-ids
194
- // https://npc-labs.slack.com/archives/C070E6HNG85/p1750185115273099
195
- if (typeof localStorage !== "undefined") {
196
- localStorage.removeItem("thirdweb:connected-wallet-ids");
197
- localStorage.removeItem("wagmi.store");
198
- localStorage.removeItem("lastAuthProvider");
199
- localStorage.removeItem("b3-user");
200
- }
201
- app.logout();
202
- debug("@@logout:loggedOut");
203
- setIsAuthenticated(false);
204
- setIsConnected(false);
205
- setUser();
206
- callback?.();
207
- }, [activeWallet, disconnect, wallets, setIsAuthenticated, setUser, setIsConnected]);
208
199
  const { isLoading: useAutoConnectLoading } = useAutoConnect({
209
200
  client,
210
201
  wallets: [wallet],
@@ -1,7 +1,4 @@
1
1
  import { Wallet } from "thirdweb/wallets";
2
- /**
3
- * @deprecated Use useAuth() with Turnkey authentication instead
4
- */
5
2
  export declare function useTWAuth(): {
6
3
  authenticate: (wallet: Wallet, partnerId: string) => Promise<import("@feathersjs/authentication").AuthenticationResult>;
7
4
  };
@@ -1,16 +1,8 @@
1
- /**
2
- * @deprecated This hook is deprecated. Use useAuth() with Turnkey authentication instead.
3
- * This file is kept for backward compatibility but should not be used in new code.
4
- */
5
1
  import app from "../../../global-account/app.js";
6
2
  import debug from "../../../shared/utils/debug.js";
7
3
  import { useCallback } from "react";
8
4
  import { useSearchParam } from "./useSearchParamsSSR.js";
9
- /**
10
- * @deprecated Use useAuth() with Turnkey authentication instead
11
- */
12
5
  export function useTWAuth() {
13
- console.warn("useTWAuth is deprecated. Please migrate to useAuth() with Turnkey authentication. See useTurnkeyAuth.ts for the new implementation.");
14
6
  const referralCode = useSearchParam("referralCode");
15
7
  const authenticate = useCallback(async (wallet, partnerId) => {
16
8
  if (!wallet || !wallet?.getAuthToken?.())
@@ -1,9 +1,8 @@
1
- import { debugB3React } from "../../../shared/utils/debug.js";
2
- import { useCallback, useState } from "react";
3
1
  import app from "../../app.js";
4
- import { useB3 } from "../components/B3Provider/useB3.js";
5
2
  import { useAuthStore } from "../stores/index.js";
6
- import { useAuth } from "./useAuth.js";
3
+ import { useCallback, useState } from "react";
4
+ import { useB3 } from "../components/B3Provider/useB3.js";
5
+ import { debugB3React } from "../../../shared/utils/debug.js";
7
6
  const debug = debugB3React("useTurnkeyAuth");
8
7
  /**
9
8
  * Hook for Turnkey email-based OTP authentication
@@ -18,55 +17,29 @@ export function useTurnkeyAuth() {
18
17
  const [error, setError] = useState(null);
19
18
  const setIsAuthenticating = useAuthStore(state => state.setIsAuthenticating);
20
19
  const setIsAuthenticated = useAuthStore(state => state.setIsAuthenticated);
21
- const { partnerId } = useB3();
22
- const { authenticate } = useAuth();
20
+ const { user } = useB3();
23
21
  /**
24
22
  * Step 1: Initiate login with email
25
- * - Calls backend turnkey-jwt strategy (init action) to create sub-org (if needed) and send OTP
23
+ * - Calls backend to create sub-org (if needed) and send OTP
26
24
  * - Returns otpId to use in verification step
27
- *
28
- * Note: Uses the turnkey-jwt authentication strategy, not the service directly.
29
- * The turnkey-jwt strategy handles both OTP flow (init/verify) and final authentication.
30
25
  */
31
26
  const initiateLogin = useCallback(async (email) => {
32
27
  setIsLoading(true);
33
28
  setError(null);
34
29
  setIsAuthenticating(true);
35
30
  try {
31
+ if (!user?.userId) {
32
+ throw new Error("User ID is required to initiate Turnkey login.");
33
+ }
36
34
  debug(`Initiating login for: ${email}`);
37
- // Use authentication service with turnkey-jwt strategy (init action)
38
- // userId is resolved from authentication context on the backend (params.user.userId)
39
- // Backend will get userId from _params.user?.userId if authenticated, or handle unauthenticated case
40
- // So we only need to send email
41
- debug(`Calling app.authenticate with turnkey-jwt strategy (init action)`, { email });
42
- const response = await app.authenticate({
43
- strategy: "turnkey-jwt",
44
- action: "init",
45
- email,
46
- });
47
- // The strategy returns the TurnkeyAuthInitResponse directly
48
- const data = response;
35
+ // Call FeathersJS service to initialize OTP
36
+ const data = await app.service("turnkey-auth").init({ email, userId: user.userId });
49
37
  debug(`OTP initialized successfully. OtpId: ${data.otpId}`);
50
38
  return data;
51
39
  }
52
40
  catch (err) {
53
41
  debug("Error initiating login:", err);
54
- // Provide more detailed error information
55
- let errorMessage = "Failed to send OTP email. Please try again.";
56
- if (err.message) {
57
- errorMessage = err.message;
58
- }
59
- else if (err.name === "TypeError" && err.message?.includes("fetch")) {
60
- errorMessage = "Network error: Unable to reach the server. Please check your connection and try again.";
61
- }
62
- else if (err.code === "ECONNREFUSED" || err.code === "ENOTFOUND") {
63
- errorMessage = "Connection error: Unable to reach the server. Please check your internet connection.";
64
- }
65
- else if (err.response) {
66
- // FeathersJS error response
67
- errorMessage = err.response.message || err.message || errorMessage;
68
- debug("FeathersJS error response:", err.response);
69
- }
42
+ const errorMessage = err.message || "Failed to send OTP email. Please try again.";
70
43
  setError(errorMessage);
71
44
  throw err;
72
45
  }
@@ -74,11 +47,11 @@ export function useTurnkeyAuth() {
74
47
  setIsLoading(false);
75
48
  setIsAuthenticating(false);
76
49
  }
77
- }, [setIsAuthenticating]);
50
+ }, [user, setIsAuthenticating]);
78
51
  /**
79
52
  * Step 2: Verify OTP and authenticate
80
- * - Verifies OTP with backend via turnkey-jwt strategy (verify action)
81
- * - Gets Turnkey session JWT from the verify response
53
+ * - Verifies OTP with backend
54
+ * - Gets Turnkey session JWT
82
55
  * - Authenticates with b3-api using "turnkey-jwt" strategy
83
56
  * - JWT automatically stored in cookies by SDK
84
57
  */
@@ -87,22 +60,19 @@ export function useTurnkeyAuth() {
87
60
  setError(null);
88
61
  setIsAuthenticating(true);
89
62
  try {
90
- debug(`Verifying OTP...`, { otpId });
91
- // Step 1: Verify OTP with backend using turnkey-jwt strategy (verify action)
92
- // This returns the Turnkey session JWT
93
- const response = await app.authenticate({
94
- strategy: "turnkey-jwt",
95
- action: "verify",
63
+ debug(`Verifying OTP...`, { userId: user?.userId });
64
+ // Step 1: Verify OTP and get Turnkey session JWT
65
+ const { turnkeySessionJwt } = await app.service("turnkey-auth").verify({
96
66
  otpId,
97
67
  otpCode,
98
68
  });
99
- // The strategy returns the TurnkeyAuthVerifyResponse directly
100
- const verifyResult = response;
101
- const { turnkeySessionJwt } = verifyResult;
102
- debug(`OTP verified! Got Turnkey session JWT. Authenticating with b3-api...`);
69
+ debug(`OTP verified! Authenticating with b3-api...`);
103
70
  // Step 2: Authenticate with b3-api using Turnkey JWT
104
- // Use the unified useAuth hook for authentication with "turnkey-jwt" strategy
105
- const authResult = await authenticate(turnkeySessionJwt, partnerId || "");
71
+ // The SDK will automatically store the b3-api JWT in cookies
72
+ const authResult = await app.authenticate({
73
+ strategy: "turnkey-jwt",
74
+ accessToken: turnkeySessionJwt,
75
+ });
106
76
  debug(`Successfully authenticated with b3-api!`, authResult);
107
77
  // Update auth store to reflect authenticated state
108
78
  setIsAuthenticated(true);
@@ -122,7 +92,7 @@ export function useTurnkeyAuth() {
122
92
  setIsLoading(false);
123
93
  setIsAuthenticating(false);
124
94
  }
125
- }, [partnerId, setIsAuthenticating, setIsAuthenticated, authenticate]);
95
+ }, [user, setIsAuthenticating, setIsAuthenticated]);
126
96
  const clearError = useCallback(() => {
127
97
  setError(null);
128
98
  }, []);
@@ -36,8 +36,8 @@ export declare function useUserQuery(): {
36
36
  name?: string | undefined;
37
37
  address?: string | undefined;
38
38
  email?: string | undefined;
39
- phone?: string | undefined;
40
39
  username?: string | undefined;
40
+ phone?: string | undefined;
41
41
  fid?: string | undefined;
42
42
  };
43
43
  }[] | undefined;