@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,117 +1,159 @@
|
|
|
1
1
|
import app from "../../../global-account/app.js";
|
|
2
2
|
import { authenticateWithB3JWT } from "../../../global-account/bsmnt.js";
|
|
3
|
-
import { useAuthStore,
|
|
3
|
+
import { useAuthStore, useSiwe } from "../../../global-account/react/index.js";
|
|
4
4
|
import { ecosystemWalletId } from "../../../shared/constants/index.js";
|
|
5
|
-
import { b3MainnetThirdWeb } from "../../../shared/constants/chains/supported.js";
|
|
6
5
|
import { debugB3React } from "../../../shared/utils/debug.js";
|
|
7
6
|
import { client } from "../../../shared/utils/thirdweb.js";
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
7
|
+
import { getConnectors } from "@wagmi/core";
|
|
8
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
9
|
+
import { useActiveWallet, useAutoConnect, useConnectedWallets, useDisconnect, useSetActiveWallet, } from "thirdweb/react";
|
|
10
10
|
import { ecosystemWallet } from "thirdweb/wallets";
|
|
11
11
|
import { preAuthenticate } from "thirdweb/wallets/in-app";
|
|
12
|
-
import { useConnect } from "
|
|
13
|
-
import {
|
|
12
|
+
import { useAccount, useConnect, useSwitchAccount } from "wagmi";
|
|
13
|
+
import { useUserQuery } from "./useUserQuery.js";
|
|
14
|
+
import { useWagmiConfig } from "./useWagmiConfig.js";
|
|
14
15
|
const debug = debugB3React("useAuthentication");
|
|
15
|
-
export function useAuthentication(partnerId
|
|
16
|
+
export function useAuthentication(partnerId) {
|
|
16
17
|
const { disconnect } = useDisconnect();
|
|
17
18
|
const wallets = useConnectedWallets();
|
|
18
19
|
const activeWallet = useActiveWallet();
|
|
19
|
-
const { authenticate } = useSiwe();
|
|
20
|
-
const { setUser } = useB3();
|
|
21
20
|
const isAuthenticated = useAuthStore(state => state.isAuthenticated);
|
|
22
21
|
const setIsAuthenticated = useAuthStore(state => state.setIsAuthenticated);
|
|
23
|
-
const setIsConnecting = useAuthStore(state => state.setIsConnecting);
|
|
24
22
|
const setIsConnected = useAuthStore(state => state.setIsConnected);
|
|
25
23
|
const isConnecting = useAuthStore(state => state.isConnecting);
|
|
26
24
|
const isConnected = useAuthStore(state => state.isConnected);
|
|
27
|
-
const useAutoConnectLoadingPrevious = useRef(false);
|
|
28
|
-
const setIsAuthenticating = useAuthStore(state => state.setIsAuthenticating);
|
|
29
25
|
const isAuthenticating = useAuthStore(state => state.isAuthenticating);
|
|
30
|
-
const
|
|
26
|
+
const setIsAuthenticating = useAuthStore(state => state.setIsAuthenticating);
|
|
31
27
|
const setHasStartedConnecting = useAuthStore(state => state.setHasStartedConnecting);
|
|
32
|
-
const
|
|
28
|
+
const setActiveWallet = useSetActiveWallet();
|
|
29
|
+
const hasStartedConnecting = useAuthStore(state => state.hasStartedConnecting);
|
|
30
|
+
const { authenticate } = useSiwe();
|
|
31
|
+
const { user, setUser } = useUserQuery();
|
|
32
|
+
const useAutoConnectLoadingPrevious = useRef(false);
|
|
33
|
+
const wagmiConfig = useWagmiConfig(partnerId);
|
|
34
|
+
const { connect } = useConnect();
|
|
35
|
+
const activeWagmiAccount = useAccount();
|
|
36
|
+
const { switchAccount } = useSwitchAccount();
|
|
37
|
+
debug("@@activeWagmiAccount", activeWagmiAccount);
|
|
33
38
|
const wallet = ecosystemWallet(ecosystemWalletId, {
|
|
34
39
|
partnerId: partnerId,
|
|
35
40
|
});
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
const syncWagmi = useCallback(async () => {
|
|
42
|
+
function syncWagmiFunc() {
|
|
43
|
+
const connectors = getConnectors(wagmiConfig);
|
|
44
|
+
debug("@@syncWagmi", {
|
|
45
|
+
connectors,
|
|
46
|
+
wallets,
|
|
47
|
+
});
|
|
48
|
+
// For each that matchs a TW wallet on wallets, connect to the wagmi connector
|
|
49
|
+
// or, since ecosystem wallets is separate, connect those via in-app-wallet from wagmi
|
|
50
|
+
connectors.forEach(async (connector) => {
|
|
51
|
+
const twWallet = wallets.find(wallet => wallet.id === connector.id || connector.id === "in-app-wallet");
|
|
52
|
+
// If no TW wallet, do not prompt the user to connect
|
|
53
|
+
if (!twWallet) {
|
|
47
54
|
return;
|
|
48
55
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
throw new Error("No account found during auto-connect");
|
|
56
|
+
// Metamask will prompt to connect, we can just switch accounts here.
|
|
57
|
+
if (connector.id === "io.metamask") {
|
|
58
|
+
return switchAccount({ connector });
|
|
53
59
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
if (
|
|
61
|
+
// If it's not an in-app wallet or it is the ecosystem wallet, connect
|
|
62
|
+
connector.id !== "in-app-wallet" ||
|
|
63
|
+
(connector.id === "in-app-wallet" && twWallet.id === ecosystemWalletId)) {
|
|
64
|
+
try {
|
|
65
|
+
const options = {
|
|
66
|
+
wallet: twWallet, // the connected wallet
|
|
67
|
+
};
|
|
68
|
+
debug("@@syncWagmi:connecting", { twWallet, connector });
|
|
69
|
+
connect({
|
|
70
|
+
connector,
|
|
71
|
+
...options,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
console.error("@@syncWagmi:error", error);
|
|
76
|
+
}
|
|
64
77
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
debug("Re-authentication failed, attempting fresh authentication");
|
|
68
|
-
const userAuth = await authenticate(account, partnerId);
|
|
69
|
-
setUser(userAuth.user);
|
|
70
|
-
setIsAuthenticated(true);
|
|
71
|
-
setIsAuthenticating(false);
|
|
72
|
-
debug("Fresh authentication successful", { userAuth });
|
|
73
|
-
// Authenticate on BSMNT with B3 JWT
|
|
74
|
-
const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
|
|
75
|
-
console.log("@@b3Jwt", b3Jwt);
|
|
78
|
+
else {
|
|
79
|
+
debug("@@syncWagmi:not-connecting", connector);
|
|
76
80
|
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
setIsAuthenticating(false);
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
/**
|
|
88
|
-
* useAutoConnectLoading starts as false
|
|
89
|
-
*/
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
syncWagmiFunc();
|
|
84
|
+
// wagmi config shouldn't change
|
|
85
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
86
|
+
}, [partnerId, wallets]);
|
|
90
87
|
useEffect(() => {
|
|
91
|
-
|
|
88
|
+
syncWagmi();
|
|
89
|
+
}, [wallets, syncWagmi]);
|
|
90
|
+
const authenticateUser = useCallback(async (wallet) => {
|
|
91
|
+
setHasStartedConnecting(true);
|
|
92
|
+
const account = wallet ? wallet.getAccount() : activeWallet?.getAccount();
|
|
93
|
+
if (!account) {
|
|
94
|
+
throw new Error("No account found during auto-connect");
|
|
95
|
+
}
|
|
96
|
+
if (!account) {
|
|
97
|
+
throw new Error("No account found during auto-connect");
|
|
98
|
+
}
|
|
99
|
+
// Try to re-authenticate first
|
|
100
|
+
try {
|
|
101
|
+
const userAuth = await app.reAuthenticate();
|
|
102
|
+
setUser(userAuth.user);
|
|
103
|
+
setIsAuthenticated(true);
|
|
92
104
|
setIsAuthenticating(false);
|
|
105
|
+
debug("Re-authenticated successfully", { userAuth });
|
|
106
|
+
// Authenticate on BSMNT with B3 JWT
|
|
107
|
+
const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
|
|
108
|
+
debug("@@b3Jwt", b3Jwt);
|
|
93
109
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
110
|
+
catch (error) {
|
|
111
|
+
// If re-authentication fails, try fresh authentication
|
|
112
|
+
debug("Re-authentication failed, attempting fresh authentication");
|
|
113
|
+
const userAuth = await authenticate(account, partnerId);
|
|
114
|
+
setUser(userAuth.user);
|
|
115
|
+
setIsAuthenticated(true);
|
|
116
|
+
setIsAuthenticating(false);
|
|
117
|
+
debug("Fresh authentication successful", { userAuth });
|
|
118
|
+
// Authenticate on BSMNT with B3 JWT
|
|
119
|
+
const b3Jwt = await authenticateWithB3JWT(userAuth.accessToken);
|
|
120
|
+
debug("@@b3Jwt", b3Jwt);
|
|
121
|
+
}
|
|
122
|
+
}, [activeWallet, partnerId, authenticate, setIsAuthenticated, setIsAuthenticating, setUser, setHasStartedConnecting]);
|
|
123
|
+
const onConnect = useCallback(async (wallet) => {
|
|
124
|
+
debug("@@useAuthentication:onConnect", { wallet });
|
|
125
|
+
try {
|
|
126
|
+
setHasStartedConnecting(true);
|
|
127
|
+
setIsConnected(true);
|
|
128
|
+
setIsAuthenticating(true);
|
|
129
|
+
await setActiveWallet(wallet);
|
|
130
|
+
await authenticateUser(wallet);
|
|
100
131
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
debug("setIsAuthenticating:false:5a");
|
|
106
|
-
setIsConnecting(false);
|
|
107
|
-
}, 100); // Add a small delay to prevent quick flickers
|
|
108
|
-
return () => clearTimeout(timeout);
|
|
132
|
+
catch (error) {
|
|
133
|
+
debug("@@useAuthentication:onConnect:failed", { error });
|
|
134
|
+
setIsAuthenticated(false);
|
|
135
|
+
setUser(undefined);
|
|
109
136
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
setIsConnecting(false);
|
|
137
|
+
finally {
|
|
138
|
+
setIsAuthenticating(false);
|
|
113
139
|
}
|
|
114
|
-
|
|
140
|
+
debug({
|
|
141
|
+
isAuthenticated,
|
|
142
|
+
isAuthenticating,
|
|
143
|
+
isConnected,
|
|
144
|
+
});
|
|
145
|
+
}, [
|
|
146
|
+
authenticateUser,
|
|
147
|
+
isAuthenticated,
|
|
148
|
+
isAuthenticating,
|
|
149
|
+
isConnected,
|
|
150
|
+
setActiveWallet,
|
|
151
|
+
setHasStartedConnecting,
|
|
152
|
+
setIsAuthenticated,
|
|
153
|
+
setIsAuthenticating,
|
|
154
|
+
setIsConnected,
|
|
155
|
+
setUser,
|
|
156
|
+
]);
|
|
115
157
|
const logout = async (callback) => {
|
|
116
158
|
if (activeWallet) {
|
|
117
159
|
debug("@@logout:activeWallet", activeWallet);
|
|
@@ -137,7 +179,21 @@ export function useAuthentication(partnerId, loginWithSiwe) {
|
|
|
137
179
|
setUser();
|
|
138
180
|
callback?.();
|
|
139
181
|
};
|
|
140
|
-
const
|
|
182
|
+
const { isLoading: useAutoConnectLoading } = useAutoConnect({
|
|
183
|
+
client,
|
|
184
|
+
wallets: [wallet],
|
|
185
|
+
onConnect: onConnect,
|
|
186
|
+
});
|
|
187
|
+
/**
|
|
188
|
+
* useAutoConnectLoading starts as false
|
|
189
|
+
*/
|
|
190
|
+
useEffect(() => {
|
|
191
|
+
if (!useAutoConnectLoading && useAutoConnectLoadingPrevious.current && !hasStartedConnecting) {
|
|
192
|
+
setIsAuthenticating(false);
|
|
193
|
+
}
|
|
194
|
+
useAutoConnectLoadingPrevious.current = useAutoConnectLoading;
|
|
195
|
+
}, [useAutoConnectLoading, hasStartedConnecting, setIsAuthenticating]);
|
|
196
|
+
const isReady = isAuthenticated && !isAuthenticating;
|
|
141
197
|
return {
|
|
142
198
|
logout,
|
|
143
199
|
isAuthenticated,
|
|
@@ -146,7 +202,11 @@ export function useAuthentication(partnerId, loginWithSiwe) {
|
|
|
146
202
|
isConnected,
|
|
147
203
|
wallet,
|
|
148
204
|
preAuthenticate,
|
|
149
|
-
connect,
|
|
205
|
+
connect: onConnect,
|
|
150
206
|
isAuthenticating,
|
|
207
|
+
onConnect,
|
|
208
|
+
user,
|
|
209
|
+
refetchUser: authenticateUser,
|
|
210
|
+
setUser,
|
|
151
211
|
};
|
|
152
212
|
}
|
|
@@ -4,7 +4,7 @@ import { Account } from "thirdweb/wallets";
|
|
|
4
4
|
* This essentially wraps our useConnect hook to handle the Privy auth flow.
|
|
5
5
|
* Currently, this is for the basement-privy strategy
|
|
6
6
|
*/
|
|
7
|
-
export declare function useHandleConnectWithPrivy(
|
|
7
|
+
export declare function useHandleConnectWithPrivy(chain?: Chain, onSuccess?: (account: Account) => void): {
|
|
8
8
|
connectTw: () => Promise<import("thirdweb/wallets").Wallet | null | undefined>;
|
|
9
9
|
isLoading: boolean;
|
|
10
10
|
fullToken: string | null;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useConnect } from "../../../global-account/react/index.js";
|
|
1
|
+
import { useB3, useConnect } from "../../../global-account/react/index.js";
|
|
2
2
|
import { debugB3React } from "../../../shared/utils/debug.js";
|
|
3
3
|
import { useIdentityToken, usePrivy } from "@privy-io/react-auth";
|
|
4
4
|
import { useCallback, useRef, useState } from "react";
|
|
@@ -7,7 +7,8 @@ const debug = debugB3React("@@b3:useHandleConnectWithPrivy");
|
|
|
7
7
|
* This essentially wraps our useConnect hook to handle the Privy auth flow.
|
|
8
8
|
* Currently, this is for the basement-privy strategy
|
|
9
9
|
*/
|
|
10
|
-
export function useHandleConnectWithPrivy(
|
|
10
|
+
export function useHandleConnectWithPrivy(chain, onSuccess) {
|
|
11
|
+
const { partnerId } = useB3();
|
|
11
12
|
if (!chain) {
|
|
12
13
|
throw new Error("Chain is required");
|
|
13
14
|
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Users } from "@b3dotfun/b3-api";
|
|
2
|
+
/**
|
|
3
|
+
* NOTE: THIS IS ONLY MEANT FOR INTERNAL USE, from useOnConnect
|
|
4
|
+
*
|
|
5
|
+
* Custom hook to manage user state with react-query
|
|
6
|
+
* This allows for invalidation and refetching of user data
|
|
7
|
+
*/
|
|
8
|
+
export declare function useUserQuery(): {
|
|
9
|
+
user: {
|
|
10
|
+
email?: string | undefined;
|
|
11
|
+
username?: string | undefined;
|
|
12
|
+
telNumber?: string | undefined;
|
|
13
|
+
ens?: string | undefined;
|
|
14
|
+
avatar?: string | undefined;
|
|
15
|
+
preferences?: {} | undefined;
|
|
16
|
+
referredBy?: string | {} | undefined;
|
|
17
|
+
sourceApp?: string | undefined;
|
|
18
|
+
referralCode?: string | undefined;
|
|
19
|
+
userGroups?: number[] | undefined;
|
|
20
|
+
isMigratedFromBSMNT?: boolean | undefined;
|
|
21
|
+
privyLinkedAccounts?: {
|
|
22
|
+
name?: string | undefined;
|
|
23
|
+
address?: string | undefined;
|
|
24
|
+
email?: string | undefined;
|
|
25
|
+
chain_type?: string | undefined;
|
|
26
|
+
lv?: number | undefined;
|
|
27
|
+
wallet_client_type?: string | undefined;
|
|
28
|
+
smart_wallet_type?: string | undefined;
|
|
29
|
+
subject?: string | undefined;
|
|
30
|
+
type: string;
|
|
31
|
+
}[] | undefined;
|
|
32
|
+
twProfiles?: {
|
|
33
|
+
type: string;
|
|
34
|
+
details: {
|
|
35
|
+
id?: string | undefined;
|
|
36
|
+
name?: string | undefined;
|
|
37
|
+
address?: string | undefined;
|
|
38
|
+
email?: string | undefined;
|
|
39
|
+
phone?: string | undefined;
|
|
40
|
+
username?: string | undefined;
|
|
41
|
+
fid?: string | undefined;
|
|
42
|
+
};
|
|
43
|
+
}[] | undefined;
|
|
44
|
+
_id: string | {};
|
|
45
|
+
userId: string;
|
|
46
|
+
smartAccountAddress: string;
|
|
47
|
+
createdAt: number;
|
|
48
|
+
updatedAt: number;
|
|
49
|
+
partnerIds: {
|
|
50
|
+
privyId?: string | undefined;
|
|
51
|
+
thirdwebId?: string | undefined;
|
|
52
|
+
};
|
|
53
|
+
} | undefined;
|
|
54
|
+
setUser: (newUser?: Users) => void;
|
|
55
|
+
refetchUser: () => Promise<void>;
|
|
56
|
+
clearUser: () => void;
|
|
57
|
+
queryKey: string[];
|
|
58
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { debugB3React } from "../../../shared/utils/debug.js";
|
|
2
|
+
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
3
|
+
const debug = debugB3React("useUserQuery");
|
|
4
|
+
const USER_QUERY_KEY = ["b3-user"];
|
|
5
|
+
/**
|
|
6
|
+
* Retrieves the user from localStorage
|
|
7
|
+
*/
|
|
8
|
+
function getUserFromStorage() {
|
|
9
|
+
if (typeof window === "undefined") {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
try {
|
|
13
|
+
const storedUser = localStorage.getItem("b3-user");
|
|
14
|
+
return storedUser ? JSON.parse(storedUser) : null;
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
console.warn("Failed to restore user from localStorage:", error);
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Saves user to localStorage
|
|
23
|
+
*/
|
|
24
|
+
function saveUserToStorage(user) {
|
|
25
|
+
if (typeof window === "undefined") {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (user) {
|
|
29
|
+
localStorage.setItem("b3-user", JSON.stringify(user));
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
localStorage.removeItem("b3-user");
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* NOTE: THIS IS ONLY MEANT FOR INTERNAL USE, from useOnConnect
|
|
37
|
+
*
|
|
38
|
+
* Custom hook to manage user state with react-query
|
|
39
|
+
* This allows for invalidation and refetching of user data
|
|
40
|
+
*/
|
|
41
|
+
export function useUserQuery() {
|
|
42
|
+
const queryClient = useQueryClient();
|
|
43
|
+
// Query to get user data (primarily from cache/localStorage)
|
|
44
|
+
const { data: user } = useQuery({
|
|
45
|
+
queryKey: USER_QUERY_KEY,
|
|
46
|
+
queryFn: getUserFromStorage,
|
|
47
|
+
staleTime: Infinity, // User data doesn't go stale automatically
|
|
48
|
+
gcTime: Infinity, // Keep in cache indefinitely
|
|
49
|
+
initialData: getUserFromStorage,
|
|
50
|
+
});
|
|
51
|
+
// Mutation to update user
|
|
52
|
+
const setUserMutation = useMutation({
|
|
53
|
+
mutationFn: async (newUser) => {
|
|
54
|
+
const userToSave = newUser ?? null;
|
|
55
|
+
saveUserToStorage(userToSave);
|
|
56
|
+
return userToSave;
|
|
57
|
+
},
|
|
58
|
+
onSuccess: data => {
|
|
59
|
+
queryClient.setQueryData(USER_QUERY_KEY, data);
|
|
60
|
+
debug("User updated", data);
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
// Helper function to set user (maintains backward compatibility)
|
|
64
|
+
const setUser = (newUser) => {
|
|
65
|
+
setUserMutation.mutate(newUser);
|
|
66
|
+
};
|
|
67
|
+
// Helper function to invalidate and refetch user
|
|
68
|
+
const refetchUser = async () => {
|
|
69
|
+
await queryClient.invalidateQueries({ queryKey: USER_QUERY_KEY });
|
|
70
|
+
return queryClient.refetchQueries({ queryKey: USER_QUERY_KEY });
|
|
71
|
+
};
|
|
72
|
+
// Helper function to clear user
|
|
73
|
+
const clearUser = () => {
|
|
74
|
+
setUser(undefined);
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
user: user ?? undefined,
|
|
78
|
+
setUser,
|
|
79
|
+
refetchUser,
|
|
80
|
+
clearUser,
|
|
81
|
+
queryKey: USER_QUERY_KEY,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare function useWagmiConfig(partnerId: string, rpcUrls?: Record<number, string>): import("wagmi").Config<readonly [import("viem").Chain, ...import("viem").Chain[]], {
|
|
2
|
+
[k: string]: import("viem").HttpTransport<undefined, false>;
|
|
3
|
+
}, readonly [import("wagmi").CreateConnectorFn<import("thirdweb/dist/types/adapters/eip1193").EIP1193Provider | undefined, {
|
|
4
|
+
connect<withCapabilities extends boolean = false>(parameters?: import("@thirdweb-dev/wagmi-adapter").ConnectionOptions<withCapabilities> | undefined): Promise<{
|
|
5
|
+
accounts: withCapabilities extends true ? readonly {
|
|
6
|
+
address: `0x${string}`;
|
|
7
|
+
capabilities: Record<string, unknown>;
|
|
8
|
+
}[] : readonly `0x${string}`[];
|
|
9
|
+
chainId: number;
|
|
10
|
+
}>;
|
|
11
|
+
}, {
|
|
12
|
+
"thirdweb:lastChainId": number;
|
|
13
|
+
}>]>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { ecosystemWalletId } from "../../../shared/constants/index.js";
|
|
2
|
+
import { supportedChains } from "../../../shared/constants/chains/supported.js";
|
|
3
|
+
import { client } from "../../../shared/utils/thirdweb.js";
|
|
4
|
+
import { inAppWalletConnector } from "@thirdweb-dev/wagmi-adapter";
|
|
5
|
+
import { useMemo } from "react";
|
|
6
|
+
import { http } from "viem";
|
|
7
|
+
import { createConfig } from "wagmi";
|
|
8
|
+
export function useWagmiConfig(partnerId, rpcUrls) {
|
|
9
|
+
// Stringify rpcUrls for stable comparison to prevent wagmiConfig recreation
|
|
10
|
+
// when parent component passes new object references with same content
|
|
11
|
+
const rpcUrlsString = useMemo(() => (rpcUrls ? JSON.stringify(rpcUrls) : undefined), [rpcUrls]);
|
|
12
|
+
const ecocystemConfig = useMemo(() => {
|
|
13
|
+
return {
|
|
14
|
+
ecosystemId: ecosystemWalletId,
|
|
15
|
+
partnerId: partnerId,
|
|
16
|
+
client,
|
|
17
|
+
};
|
|
18
|
+
}, [partnerId]);
|
|
19
|
+
/**
|
|
20
|
+
* Creates wagmi config with optional custom RPC URLs
|
|
21
|
+
* @param rpcUrls - Optional mapping of chain IDs to RPC URLs
|
|
22
|
+
*/
|
|
23
|
+
const wagmiConfig = useMemo(() => {
|
|
24
|
+
const parsedRpcUrls = rpcUrlsString ? JSON.parse(rpcUrlsString) : undefined;
|
|
25
|
+
return createConfig({
|
|
26
|
+
chains: [supportedChains[0], ...supportedChains.slice(1)],
|
|
27
|
+
transports: Object.fromEntries(supportedChains.map(chain => [chain.id, http(parsedRpcUrls?.[chain.id])])),
|
|
28
|
+
connectors: [
|
|
29
|
+
inAppWalletConnector({
|
|
30
|
+
...ecocystemConfig,
|
|
31
|
+
client,
|
|
32
|
+
}),
|
|
33
|
+
// injected(),
|
|
34
|
+
// coinbaseWallet({ appName: "HypeDuel" }),
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
}, [rpcUrlsString, ecocystemConfig]);
|
|
38
|
+
return wagmiConfig;
|
|
39
|
+
}
|
|
@@ -32,8 +32,6 @@ export interface SignInWithB3ModalProps extends BaseModalProps {
|
|
|
32
32
|
sessionKeyAddress?: Address;
|
|
33
33
|
/** Unique identifier for the partner application */
|
|
34
34
|
partnerId: string;
|
|
35
|
-
/** Whether to authenticate with Sign In With Ethereum */
|
|
36
|
-
loginWithSiwe?: boolean;
|
|
37
35
|
/** Whether to close the modal after successful login */
|
|
38
36
|
closeAfterLogin?: boolean;
|
|
39
37
|
/** Source of the sign-in request */
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Export types
|
|
2
|
+
export * from "./types/index.js";
|
|
3
|
+
// Export services
|
|
4
|
+
export * from "./services/index.js";
|
|
5
|
+
// Re-export auth token getter from shared for convenience
|
|
6
|
+
// Note: Auth token is managed by B3 Global Account authentication
|
|
7
|
+
export { getAuthToken } from "../shared/utils/auth-token.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useNotifications";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./useNotifications.js";
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { UserData } from "../../types";
|
|
2
|
+
/**
|
|
3
|
+
* React hook for managing B3 notifications
|
|
4
|
+
* Automatically uses the authenticated user's ID from JWT
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```tsx
|
|
8
|
+
* import { useNotifications } from '../../../notifications/react';
|
|
9
|
+
*
|
|
10
|
+
* function NotificationSettings() {
|
|
11
|
+
* const { user, loading, connectEmail, connectTelegram, isEmailConnected } = useNotifications();
|
|
12
|
+
*
|
|
13
|
+
* if (loading) return <div>Loading...</div>;
|
|
14
|
+
*
|
|
15
|
+
* return (
|
|
16
|
+
* <div>
|
|
17
|
+
* {!isEmailConnected && (
|
|
18
|
+
* <button onClick={() => connectEmail('user@example.com')}>
|
|
19
|
+
* Connect Email
|
|
20
|
+
* </button>
|
|
21
|
+
* )}
|
|
22
|
+
* <button onClick={connectTelegram}>Connect Telegram</button>
|
|
23
|
+
* </div>
|
|
24
|
+
* );
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function useNotifications(): {
|
|
29
|
+
user: UserData | null;
|
|
30
|
+
loading: boolean;
|
|
31
|
+
error: Error | null;
|
|
32
|
+
refresh: () => Promise<void>;
|
|
33
|
+
connectEmail: (email: string) => Promise<void>;
|
|
34
|
+
connectTelegram: () => Promise<void>;
|
|
35
|
+
updateChannel: (channelId: string, updates: {
|
|
36
|
+
enabled?: boolean;
|
|
37
|
+
}) => Promise<void>;
|
|
38
|
+
deleteChannel: (channelId: string) => Promise<void>;
|
|
39
|
+
isEmailConnected: boolean;
|
|
40
|
+
isTelegramConnected: boolean;
|
|
41
|
+
isDiscordConnected: boolean;
|
|
42
|
+
};
|