@b3dotfun/sdk 0.0.47 → 0.0.48-alpha.1
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 +225 -6
- package/dist/cjs/anyspend/react/components/AnySpendCustom.js +5 -3
- package/dist/cjs/anyspend/types/chain.d.ts +2 -1
- package/dist/cjs/anyspend/utils/chain.js +4 -0
- 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 +141 -81
- 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/cjs/notifications/index.d.ts +3 -0
- package/dist/cjs/notifications/index.js +25 -0
- package/dist/cjs/notifications/react/hooks/index.d.ts +1 -0
- package/dist/cjs/notifications/react/hooks/index.js +17 -0
- package/dist/cjs/notifications/react/hooks/useNotifications.d.ts +42 -0
- package/dist/cjs/notifications/react/hooks/useNotifications.js +148 -0
- package/dist/cjs/notifications/react/index.d.ts +1 -0
- package/dist/cjs/notifications/react/index.js +17 -0
- package/dist/cjs/notifications/services/api.d.ts +67 -0
- package/dist/cjs/notifications/services/api.js +184 -0
- package/dist/cjs/notifications/services/index.d.ts +1 -0
- package/dist/cjs/notifications/services/index.js +17 -0
- package/dist/cjs/notifications/types/index.d.ts +51 -0
- package/dist/cjs/notifications/types/index.js +2 -0
- package/dist/cjs/shared/utils/auth-token.d.ts +7 -0
- package/dist/cjs/shared/utils/auth-token.js +17 -0
- package/dist/cjs/shared/utils/index.d.ts +1 -0
- package/dist/cjs/shared/utils/index.js +1 -0
- package/dist/esm/anyspend/react/components/AnySpendCustom.js +5 -3
- package/dist/esm/anyspend/types/chain.d.ts +2 -1
- package/dist/esm/anyspend/utils/chain.js +4 -0
- 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 +144 -84
- 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/esm/notifications/index.d.ts +3 -0
- package/dist/esm/notifications/index.js +7 -0
- package/dist/esm/notifications/react/hooks/index.d.ts +1 -0
- package/dist/esm/notifications/react/hooks/index.js +1 -0
- package/dist/esm/notifications/react/hooks/useNotifications.d.ts +42 -0
- package/dist/esm/notifications/react/hooks/useNotifications.js +145 -0
- package/dist/esm/notifications/react/index.d.ts +1 -0
- package/dist/esm/notifications/react/index.js +1 -0
- package/dist/esm/notifications/services/api.d.ts +67 -0
- package/dist/esm/notifications/services/api.js +179 -0
- package/dist/esm/notifications/services/index.d.ts +1 -0
- package/dist/esm/notifications/services/index.js +1 -0
- package/dist/esm/notifications/types/index.d.ts +51 -0
- package/dist/esm/shared/utils/auth-token.d.ts +7 -0
- package/dist/esm/shared/utils/auth-token.js +11 -0
- package/dist/esm/shared/utils/index.d.ts +1 -0
- package/dist/esm/shared/utils/index.js +1 -0
- package/dist/types/anyspend/types/chain.d.ts +2 -1
- 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/dist/types/notifications/index.d.ts +3 -0
- package/dist/types/notifications/react/hooks/index.d.ts +1 -0
- package/dist/types/notifications/react/hooks/useNotifications.d.ts +42 -0
- package/dist/types/notifications/react/index.d.ts +1 -0
- package/dist/types/notifications/services/api.d.ts +67 -0
- package/dist/types/notifications/services/index.d.ts +1 -0
- package/dist/types/notifications/types/index.d.ts +51 -0
- package/dist/types/shared/utils/auth-token.d.ts +7 -0
- package/dist/types/shared/utils/index.d.ts +1 -0
- package/package.json +23 -3
- package/src/anyspend/react/components/AnySpendCustom.tsx +5 -3
- package/src/anyspend/types/chain.ts +2 -1
- package/src/anyspend/utils/chain.ts +4 -0
- 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 +170 -89
- 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
- package/src/notifications/index.ts +9 -0
- package/src/notifications/react/hooks/index.ts +1 -0
- package/src/notifications/react/hooks/useNotifications.ts +153 -0
- package/src/notifications/react/index.ts +1 -0
- package/src/notifications/services/api.ts +217 -0
- package/src/notifications/services/index.ts +1 -0
- package/src/notifications/types/index.ts +58 -0
- package/src/shared/utils/auth-token.ts +13 -0
- package/src/shared/utils/index.ts +1 -0
- package/dist/cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.js +0 -245
- package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +0 -1
- package/dist/esm/shared/react/hooks/__tests__/useCurrencyConversion.test.js +0 -243
- package/dist/types/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts +0 -1
- /package/dist/{cjs/shared/react/hooks/__tests__/useCurrencyConversion.test.d.ts → esm/notifications/types/index.js} +0 -0
|
@@ -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,
|
|
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, 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
|
|
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
|
+
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
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
if (
|
|
58
|
-
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
debug("
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
|
|
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
|
-
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
|
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(
|
|
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
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ecosystemWalletId } from "@b3dotfun/sdk/shared/constants";
|
|
2
|
+
import { supportedChains } from "@b3dotfun/sdk/shared/constants/chains/supported";
|
|
3
|
+
import { client } from "@b3dotfun/sdk/shared/utils/thirdweb";
|
|
4
|
+
import { inAppWalletConnector } from "@thirdweb-dev/wagmi-adapter";
|
|
5
|
+
import { useMemo } from "react";
|
|
6
|
+
import { http } from "viem";
|
|
7
|
+
import { createConfig } from "wagmi";
|
|
8
|
+
|
|
9
|
+
export function useWagmiConfig(partnerId: string, rpcUrls?: Record<number, string>) {
|
|
10
|
+
// Stringify rpcUrls for stable comparison to prevent wagmiConfig recreation
|
|
11
|
+
// when parent component passes new object references with same content
|
|
12
|
+
const rpcUrlsString = useMemo(() => (rpcUrls ? JSON.stringify(rpcUrls) : undefined), [rpcUrls]);
|
|
13
|
+
|
|
14
|
+
const ecocystemConfig = useMemo(() => {
|
|
15
|
+
return {
|
|
16
|
+
ecosystemId: ecosystemWalletId,
|
|
17
|
+
partnerId: partnerId,
|
|
18
|
+
client,
|
|
19
|
+
};
|
|
20
|
+
}, [partnerId]);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Creates wagmi config with optional custom RPC URLs
|
|
24
|
+
* @param rpcUrls - Optional mapping of chain IDs to RPC URLs
|
|
25
|
+
*/
|
|
26
|
+
const wagmiConfig = useMemo(() => {
|
|
27
|
+
const parsedRpcUrls = rpcUrlsString ? JSON.parse(rpcUrlsString) : undefined;
|
|
28
|
+
|
|
29
|
+
return createConfig({
|
|
30
|
+
chains: [supportedChains[0], ...supportedChains.slice(1)],
|
|
31
|
+
transports: Object.fromEntries(supportedChains.map(chain => [chain.id, http(parsedRpcUrls?.[chain.id])])),
|
|
32
|
+
connectors: [
|
|
33
|
+
inAppWalletConnector({
|
|
34
|
+
...ecocystemConfig,
|
|
35
|
+
client,
|
|
36
|
+
}),
|
|
37
|
+
// injected(),
|
|
38
|
+
// coinbaseWallet({ appName: "HypeDuel" }),
|
|
39
|
+
],
|
|
40
|
+
});
|
|
41
|
+
}, [rpcUrlsString, ecocystemConfig]);
|
|
42
|
+
|
|
43
|
+
return wagmiConfig;
|
|
44
|
+
}
|
|
@@ -35,8 +35,6 @@ export interface SignInWithB3ModalProps extends BaseModalProps {
|
|
|
35
35
|
sessionKeyAddress?: Address;
|
|
36
36
|
/** Unique identifier for the partner application */
|
|
37
37
|
partnerId: string;
|
|
38
|
-
/** Whether to authenticate with Sign In With Ethereum */
|
|
39
|
-
loginWithSiwe?: boolean;
|
|
40
38
|
/** Whether to close the modal after successful login */
|
|
41
39
|
closeAfterLogin?: boolean;
|
|
42
40
|
/** Source of the sign-in request */
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Export types
|
|
2
|
+
export * from "./types";
|
|
3
|
+
|
|
4
|
+
// Export services
|
|
5
|
+
export * from "./services";
|
|
6
|
+
|
|
7
|
+
// Re-export auth token getter from shared for convenience
|
|
8
|
+
// Note: Auth token is managed by B3 Global Account authentication
|
|
9
|
+
export { getAuthToken } from "../shared/utils/auth-token";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useNotifications";
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
|
+
import { notificationsAPI } from "../../services/api";
|
|
3
|
+
import type { UserData } from "../../types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* React hook for managing B3 notifications
|
|
7
|
+
* Automatically uses the authenticated user's ID from JWT
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```tsx
|
|
11
|
+
* import { useNotifications } from '@b3dotfun/sdk/notifications/react';
|
|
12
|
+
*
|
|
13
|
+
* function NotificationSettings() {
|
|
14
|
+
* const { user, loading, connectEmail, connectTelegram, isEmailConnected } = useNotifications();
|
|
15
|
+
*
|
|
16
|
+
* if (loading) return <div>Loading...</div>;
|
|
17
|
+
*
|
|
18
|
+
* return (
|
|
19
|
+
* <div>
|
|
20
|
+
* {!isEmailConnected && (
|
|
21
|
+
* <button onClick={() => connectEmail('user@example.com')}>
|
|
22
|
+
* Connect Email
|
|
23
|
+
* </button>
|
|
24
|
+
* )}
|
|
25
|
+
* <button onClick={connectTelegram}>Connect Telegram</button>
|
|
26
|
+
* </div>
|
|
27
|
+
* );
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export function useNotifications() {
|
|
32
|
+
const [user, setUser] = useState<UserData | null>(null);
|
|
33
|
+
const [loading, setLoading] = useState(true);
|
|
34
|
+
const [error, setError] = useState<Error | null>(null);
|
|
35
|
+
|
|
36
|
+
// Refs to track polling timers for cleanup
|
|
37
|
+
const telegramPollIntervalRef = useRef<NodeJS.Timeout | null>(null);
|
|
38
|
+
const telegramPollTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
|
39
|
+
|
|
40
|
+
// Cleanup function for Telegram polling
|
|
41
|
+
const cleanupTelegramPolling = () => {
|
|
42
|
+
if (telegramPollIntervalRef.current) {
|
|
43
|
+
clearInterval(telegramPollIntervalRef.current);
|
|
44
|
+
telegramPollIntervalRef.current = null;
|
|
45
|
+
}
|
|
46
|
+
if (telegramPollTimeoutRef.current) {
|
|
47
|
+
clearTimeout(telegramPollTimeoutRef.current);
|
|
48
|
+
telegramPollTimeoutRef.current = null;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// Load user data on mount
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
loadUser();
|
|
55
|
+
}, []);
|
|
56
|
+
|
|
57
|
+
// Cleanup polling on unmount
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
return () => {
|
|
60
|
+
cleanupTelegramPolling();
|
|
61
|
+
};
|
|
62
|
+
}, []);
|
|
63
|
+
|
|
64
|
+
const loadUser = async () => {
|
|
65
|
+
try {
|
|
66
|
+
setLoading(true);
|
|
67
|
+
setError(null);
|
|
68
|
+
const userData = await notificationsAPI.getUser();
|
|
69
|
+
setUser(userData);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
setError(err instanceof Error ? err : new Error("Failed to load user"));
|
|
72
|
+
console.error("Failed to load user:", err);
|
|
73
|
+
} finally {
|
|
74
|
+
setLoading(false);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const connectEmail = async (email: string) => {
|
|
79
|
+
try {
|
|
80
|
+
await notificationsAPI.connectEmail(email);
|
|
81
|
+
await loadUser(); // Refresh user data
|
|
82
|
+
} catch (err) {
|
|
83
|
+
console.error("Failed to connect email:", err);
|
|
84
|
+
throw err;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const connectTelegram = async () => {
|
|
89
|
+
try {
|
|
90
|
+
// Clear any existing polling before starting new one
|
|
91
|
+
cleanupTelegramPolling();
|
|
92
|
+
|
|
93
|
+
const { deepLink } = await notificationsAPI.getTelegramLink();
|
|
94
|
+
window.open(deepLink, "_blank");
|
|
95
|
+
|
|
96
|
+
// Poll for connection status
|
|
97
|
+
telegramPollIntervalRef.current = setInterval(async () => {
|
|
98
|
+
try {
|
|
99
|
+
const { connected } = await notificationsAPI.checkTelegramStatus();
|
|
100
|
+
if (connected) {
|
|
101
|
+
cleanupTelegramPolling();
|
|
102
|
+
await loadUser(); // Refresh user data
|
|
103
|
+
}
|
|
104
|
+
} catch (err) {
|
|
105
|
+
console.error("Failed to check Telegram status:", err);
|
|
106
|
+
}
|
|
107
|
+
}, 2000);
|
|
108
|
+
|
|
109
|
+
// Stop polling after 2 minutes
|
|
110
|
+
telegramPollTimeoutRef.current = setTimeout(() => {
|
|
111
|
+
cleanupTelegramPolling();
|
|
112
|
+
}, 120000);
|
|
113
|
+
} catch (err) {
|
|
114
|
+
console.error("Failed to connect Telegram:", err);
|
|
115
|
+
throw err;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const updateChannel = async (channelId: string, updates: { enabled?: boolean }) => {
|
|
120
|
+
try {
|
|
121
|
+
await notificationsAPI.updateChannel(channelId, updates);
|
|
122
|
+
await loadUser(); // Refresh user data
|
|
123
|
+
} catch (err) {
|
|
124
|
+
console.error("Failed to update channel:", err);
|
|
125
|
+
throw err;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const deleteChannel = async (channelId: string) => {
|
|
130
|
+
try {
|
|
131
|
+
await notificationsAPI.deleteChannel(channelId);
|
|
132
|
+
await loadUser(); // Refresh user data
|
|
133
|
+
} catch (err) {
|
|
134
|
+
console.error("Failed to delete channel:", err);
|
|
135
|
+
throw err;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
return {
|
|
140
|
+
user,
|
|
141
|
+
loading,
|
|
142
|
+
error,
|
|
143
|
+
refresh: loadUser,
|
|
144
|
+
connectEmail,
|
|
145
|
+
connectTelegram,
|
|
146
|
+
updateChannel,
|
|
147
|
+
deleteChannel,
|
|
148
|
+
// Convenience helpers
|
|
149
|
+
isEmailConnected: user?.channels?.find(c => c.channel_type === "email")?.enabled === 1,
|
|
150
|
+
isTelegramConnected: user?.channels?.find(c => c.channel_type === "telegram")?.enabled === 1,
|
|
151
|
+
isDiscordConnected: user?.channels?.find(c => c.channel_type === "discord")?.enabled === 1,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./hooks";
|