@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.
- package/README.md +3 -3
- package/dist/cjs/global-account/react/components/B3Provider/B3Provider.d.ts +3 -2
- package/dist/cjs/global-account/react/components/B3Provider/B3Provider.js +15 -64
- package/dist/cjs/global-account/react/components/B3Provider/B3Provider.native.d.ts +6 -2
- package/dist/cjs/global-account/react/components/B3Provider/B3Provider.native.js +26 -21
- package/dist/cjs/global-account/react/components/B3Provider/types.d.ts +2 -0
- package/dist/cjs/global-account/react/components/B3Provider/types.js +2 -0
- package/dist/cjs/global-account/react/components/ManageAccount/BalanceContent.d.ts +1 -2
- package/dist/cjs/global-account/react/components/ManageAccount/BalanceContent.js +2 -2
- package/dist/cjs/global-account/react/components/ManageAccount/ManageAccount.js +1 -1
- package/dist/cjs/global-account/react/components/SignInWithB3/SignIn.js +2 -2
- package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3.js +1 -1
- package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.d.ts +1 -1
- package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +6 -22
- package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Privy.d.ts +1 -2
- package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Privy.js +3 -2
- package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStep.d.ts +1 -2
- package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStep.js +10 -45
- package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStepCustom.d.ts +1 -2
- package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStepCustom.js +2 -1
- package/dist/cjs/global-account/react/components/custom/ManageAccountButton.js +2 -1
- package/dist/cjs/global-account/react/hooks/useAuthentication.d.ts +51 -2
- package/dist/cjs/global-account/react/hooks/useAuthentication.js +134 -84
- package/dist/cjs/global-account/react/hooks/useHandleConnectWithPrivy.d.ts +1 -1
- package/dist/cjs/global-account/react/hooks/useHandleConnectWithPrivy.js +2 -1
- package/dist/cjs/global-account/react/hooks/useUserQuery.d.ts +58 -0
- package/dist/cjs/global-account/react/hooks/useUserQuery.js +86 -0
- package/dist/cjs/global-account/react/hooks/useWagmiConfig.d.ts +13 -0
- package/dist/cjs/global-account/react/hooks/useWagmiConfig.js +42 -0
- package/dist/cjs/global-account/react/stores/useModalStore.d.ts +0 -2
- package/dist/esm/global-account/react/components/B3Provider/B3Provider.d.ts +3 -2
- package/dist/esm/global-account/react/components/B3Provider/B3Provider.js +19 -68
- package/dist/esm/global-account/react/components/B3Provider/B3Provider.native.d.ts +6 -2
- package/dist/esm/global-account/react/components/B3Provider/B3Provider.native.js +24 -19
- package/dist/esm/global-account/react/components/B3Provider/types.d.ts +2 -0
- package/dist/esm/global-account/react/components/B3Provider/types.js +2 -0
- package/dist/esm/global-account/react/components/ManageAccount/BalanceContent.d.ts +1 -2
- package/dist/esm/global-account/react/components/ManageAccount/BalanceContent.js +2 -2
- package/dist/esm/global-account/react/components/ManageAccount/ManageAccount.js +1 -1
- package/dist/esm/global-account/react/components/SignInWithB3/SignIn.js +2 -2
- package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3.js +2 -2
- package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.d.ts +1 -1
- package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +7 -23
- package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Privy.d.ts +1 -2
- package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Privy.js +4 -3
- package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStep.d.ts +1 -2
- package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStep.js +11 -46
- package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStepCustom.d.ts +1 -2
- package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStepCustom.js +3 -2
- package/dist/esm/global-account/react/components/custom/ManageAccountButton.js +3 -2
- package/dist/esm/global-account/react/hooks/useAuthentication.d.ts +51 -2
- package/dist/esm/global-account/react/hooks/useAuthentication.js +137 -87
- package/dist/esm/global-account/react/hooks/useHandleConnectWithPrivy.d.ts +1 -1
- package/dist/esm/global-account/react/hooks/useHandleConnectWithPrivy.js +3 -2
- package/dist/esm/global-account/react/hooks/useUserQuery.d.ts +58 -0
- package/dist/esm/global-account/react/hooks/useUserQuery.js +83 -0
- package/dist/esm/global-account/react/hooks/useWagmiConfig.d.ts +13 -0
- package/dist/esm/global-account/react/hooks/useWagmiConfig.js +39 -0
- package/dist/esm/global-account/react/stores/useModalStore.d.ts +0 -2
- package/dist/types/global-account/react/components/B3Provider/B3Provider.d.ts +3 -2
- package/dist/types/global-account/react/components/B3Provider/B3Provider.native.d.ts +6 -2
- package/dist/types/global-account/react/components/B3Provider/types.d.ts +2 -0
- package/dist/types/global-account/react/components/ManageAccount/BalanceContent.d.ts +1 -2
- package/dist/types/global-account/react/components/SignInWithB3/SignInWithB3Flow.d.ts +1 -1
- package/dist/types/global-account/react/components/SignInWithB3/SignInWithB3Privy.d.ts +1 -2
- package/dist/types/global-account/react/components/SignInWithB3/steps/LoginStep.d.ts +1 -2
- package/dist/types/global-account/react/components/SignInWithB3/steps/LoginStepCustom.d.ts +1 -2
- package/dist/types/global-account/react/hooks/useAuthentication.d.ts +51 -2
- package/dist/types/global-account/react/hooks/useHandleConnectWithPrivy.d.ts +1 -1
- package/dist/types/global-account/react/hooks/useUserQuery.d.ts +58 -0
- package/dist/types/global-account/react/hooks/useWagmiConfig.d.ts +13 -0
- package/dist/types/global-account/react/stores/useModalStore.d.ts +0 -2
- package/package.json +3 -3
- package/src/global-account/react/components/B3Provider/B3Provider.native.tsx +51 -35
- package/src/global-account/react/components/B3Provider/B3Provider.tsx +28 -72
- package/src/global-account/react/components/B3Provider/types.ts +4 -0
- package/src/global-account/react/components/ManageAccount/BalanceContent.tsx +2 -3
- package/src/global-account/react/components/ManageAccount/ManageAccount.tsx +1 -1
- package/src/global-account/react/components/SignInWithB3/SignIn.tsx +2 -2
- package/src/global-account/react/components/SignInWithB3/SignInWithB3.tsx +2 -2
- package/src/global-account/react/components/SignInWithB3/SignInWithB3Flow.tsx +4 -23
- package/src/global-account/react/components/SignInWithB3/SignInWithB3Privy.tsx +4 -3
- package/src/global-account/react/components/SignInWithB3/steps/LoginStep.tsx +8 -46
- package/src/global-account/react/components/SignInWithB3/steps/LoginStepCustom.tsx +2 -2
- package/src/global-account/react/components/custom/ManageAccountButton.tsx +3 -2
- package/src/global-account/react/hooks/useAuthentication.ts +159 -90
- package/src/global-account/react/hooks/useHandleConnectWithPrivy.tsx +3 -2
- package/src/global-account/react/hooks/useUserQuery.ts +95 -0
- package/src/global-account/react/hooks/useWagmiConfig.tsx +44 -0
- 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 {
|
|
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
|
-
[
|
|
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}
|
|
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
|
|
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,
|
|
22
|
-
const {
|
|
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,
|
|
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,
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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(
|
|
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,
|
|
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 {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
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 "
|
|
13
|
-
import {
|
|
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
|
|
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
|
|
35
|
+
const setIsAuthenticating = useAuthStore(state => state.setIsAuthenticating);
|
|
33
36
|
const setHasStartedConnecting = useAuthStore(state => state.setHasStartedConnecting);
|
|
34
|
-
const
|
|
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
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
53
|
-
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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("
|
|
145
|
+
debug("@@useAuthentication:onConnect:failed", { error });
|
|
87
146
|
setIsAuthenticated(false);
|
|
88
|
-
|
|
89
|
-
|
|
147
|
+
setUser(undefined);
|
|
148
|
+
} finally {
|
|
149
|
+
setIsAuthenticating(false);
|
|
90
150
|
}
|
|
91
|
-
setIsAuthenticating(false);
|
|
92
|
-
},
|
|
93
|
-
});
|
|
94
151
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
|
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(
|
|
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
|
+
}
|