@b3dotfun/sdk 0.0.82-alpha.3 → 0.0.83-test.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/anyspend/react/components/AnySpend.js +3 -0
- package/dist/cjs/anyspend/react/components/AnySpendCustom.js +3 -0
- package/dist/cjs/anyspend/react/components/common/CryptoPaymentMethod.js +177 -47
- package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.js +3 -0
- package/dist/cjs/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +10 -0
- package/dist/cjs/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.js +73 -0
- package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +57 -12
- package/dist/cjs/global-account/react/hooks/index.d.ts +1 -0
- package/dist/cjs/global-account/react/hooks/index.js +3 -1
- package/dist/cjs/global-account/react/hooks/useAuth.d.ts +76 -0
- package/dist/cjs/global-account/react/hooks/useAuth.js +338 -0
- package/dist/cjs/global-account/react/hooks/useAuthentication.d.ts +2 -2
- package/dist/cjs/global-account/react/hooks/useAuthentication.js +72 -63
- package/dist/cjs/global-account/react/hooks/useTWAuth.d.ts +3 -0
- package/dist/cjs/global-account/react/hooks/useTWAuth.js +8 -0
- package/dist/cjs/global-account/react/hooks/useTurnkeyAuth.js +54 -24
- package/dist/cjs/global-account/react/hooks/useUserQuery.d.ts +1 -1
- package/dist/esm/anyspend/react/components/AnySpend.js +3 -0
- package/dist/esm/anyspend/react/components/AnySpendCustom.js +3 -0
- package/dist/esm/anyspend/react/components/common/CryptoPaymentMethod.js +175 -45
- package/dist/esm/anyspend/react/hooks/useAnyspendFlow.js +3 -0
- package/dist/esm/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +10 -0
- package/dist/esm/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.js +70 -0
- package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +57 -12
- package/dist/esm/global-account/react/hooks/index.d.ts +1 -0
- package/dist/esm/global-account/react/hooks/index.js +1 -0
- package/dist/esm/global-account/react/hooks/useAuth.d.ts +76 -0
- package/dist/esm/global-account/react/hooks/useAuth.js +332 -0
- package/dist/esm/global-account/react/hooks/useAuthentication.d.ts +2 -2
- package/dist/esm/global-account/react/hooks/useAuthentication.js +72 -63
- package/dist/esm/global-account/react/hooks/useTWAuth.d.ts +3 -0
- package/dist/esm/global-account/react/hooks/useTWAuth.js +8 -0
- package/dist/esm/global-account/react/hooks/useTurnkeyAuth.js +54 -24
- package/dist/esm/global-account/react/hooks/useUserQuery.d.ts +1 -1
- package/dist/styles/index.css +1 -1
- package/dist/types/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +10 -0
- package/dist/types/global-account/react/hooks/index.d.ts +1 -0
- package/dist/types/global-account/react/hooks/useAuth.d.ts +76 -0
- package/dist/types/global-account/react/hooks/useAuthentication.d.ts +2 -2
- package/dist/types/global-account/react/hooks/useTWAuth.d.ts +3 -0
- package/dist/types/global-account/react/hooks/useUserQuery.d.ts +1 -1
- package/package.json +1 -1
- package/src/anyspend/react/components/AnySpend.tsx +4 -0
- package/src/anyspend/react/components/AnySpendCustom.tsx +4 -0
- package/src/anyspend/react/components/common/CryptoPaymentMethod.tsx +253 -22
- package/src/anyspend/react/hooks/useAnyspendFlow.ts +4 -0
- package/src/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.ts +80 -0
- package/src/global-account/react/components/SignInWithB3/SignInWithB3Flow.tsx +77 -22
- package/src/global-account/react/hooks/index.ts +1 -0
- package/src/global-account/react/hooks/useAuth.ts +380 -0
- package/src/global-account/react/hooks/useAuthentication.ts +88 -85
- package/src/global-account/react/hooks/useTWAuth.tsx +10 -0
- package/src/global-account/react/hooks/useTurnkeyAuth.ts +59 -26
|
@@ -4,11 +4,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.useTurnkeyAuth = useTurnkeyAuth;
|
|
7
|
-
const
|
|
8
|
-
const stores_1 = require("../stores");
|
|
7
|
+
const debug_1 = require("../../../shared/utils/debug");
|
|
9
8
|
const react_1 = require("react");
|
|
9
|
+
const app_1 = __importDefault(require("../../app"));
|
|
10
10
|
const useB3_1 = require("../components/B3Provider/useB3");
|
|
11
|
-
const
|
|
11
|
+
const stores_1 = require("../stores");
|
|
12
|
+
const useAuth_1 = require("./useAuth");
|
|
12
13
|
const debug = (0, debug_1.debugB3React)("useTurnkeyAuth");
|
|
13
14
|
/**
|
|
14
15
|
* Hook for Turnkey email-based OTP authentication
|
|
@@ -23,29 +24,55 @@ function useTurnkeyAuth() {
|
|
|
23
24
|
const [error, setError] = (0, react_1.useState)(null);
|
|
24
25
|
const setIsAuthenticating = (0, stores_1.useAuthStore)(state => state.setIsAuthenticating);
|
|
25
26
|
const setIsAuthenticated = (0, stores_1.useAuthStore)(state => state.setIsAuthenticated);
|
|
26
|
-
const {
|
|
27
|
+
const { partnerId } = (0, useB3_1.useB3)();
|
|
28
|
+
const { authenticate } = (0, useAuth_1.useAuth)();
|
|
27
29
|
/**
|
|
28
30
|
* Step 1: Initiate login with email
|
|
29
|
-
* - Calls backend to create sub-org (if needed) and send OTP
|
|
31
|
+
* - Calls backend turnkey-jwt strategy (init action) to create sub-org (if needed) and send OTP
|
|
30
32
|
* - Returns otpId to use in verification step
|
|
33
|
+
*
|
|
34
|
+
* Note: Uses the turnkey-jwt authentication strategy, not the service directly.
|
|
35
|
+
* The turnkey-jwt strategy handles both OTP flow (init/verify) and final authentication.
|
|
31
36
|
*/
|
|
32
37
|
const initiateLogin = (0, react_1.useCallback)(async (email) => {
|
|
33
38
|
setIsLoading(true);
|
|
34
39
|
setError(null);
|
|
35
40
|
setIsAuthenticating(true);
|
|
36
41
|
try {
|
|
37
|
-
if (!user?.userId) {
|
|
38
|
-
throw new Error("User ID is required to initiate Turnkey login.");
|
|
39
|
-
}
|
|
40
42
|
debug(`Initiating login for: ${email}`);
|
|
41
|
-
//
|
|
42
|
-
|
|
43
|
+
// Use authentication service with turnkey-jwt strategy (init action)
|
|
44
|
+
// userId is resolved from authentication context on the backend (params.user.userId)
|
|
45
|
+
// Backend will get userId from _params.user?.userId if authenticated, or handle unauthenticated case
|
|
46
|
+
// So we only need to send email
|
|
47
|
+
debug(`Calling app.authenticate with turnkey-jwt strategy (init action)`, { email });
|
|
48
|
+
const response = await app_1.default.authenticate({
|
|
49
|
+
strategy: "turnkey-jwt",
|
|
50
|
+
action: "init",
|
|
51
|
+
email,
|
|
52
|
+
});
|
|
53
|
+
// The strategy returns the TurnkeyAuthInitResponse directly
|
|
54
|
+
const data = response;
|
|
43
55
|
debug(`OTP initialized successfully. OtpId: ${data.otpId}`);
|
|
44
56
|
return data;
|
|
45
57
|
}
|
|
46
58
|
catch (err) {
|
|
47
59
|
debug("Error initiating login:", err);
|
|
48
|
-
|
|
60
|
+
// Provide more detailed error information
|
|
61
|
+
let errorMessage = "Failed to send OTP email. Please try again.";
|
|
62
|
+
if (err.message) {
|
|
63
|
+
errorMessage = err.message;
|
|
64
|
+
}
|
|
65
|
+
else if (err.name === "TypeError" && err.message?.includes("fetch")) {
|
|
66
|
+
errorMessage = "Network error: Unable to reach the server. Please check your connection and try again.";
|
|
67
|
+
}
|
|
68
|
+
else if (err.code === "ECONNREFUSED" || err.code === "ENOTFOUND") {
|
|
69
|
+
errorMessage = "Connection error: Unable to reach the server. Please check your internet connection.";
|
|
70
|
+
}
|
|
71
|
+
else if (err.response) {
|
|
72
|
+
// FeathersJS error response
|
|
73
|
+
errorMessage = err.response.message || err.message || errorMessage;
|
|
74
|
+
debug("FeathersJS error response:", err.response);
|
|
75
|
+
}
|
|
49
76
|
setError(errorMessage);
|
|
50
77
|
throw err;
|
|
51
78
|
}
|
|
@@ -53,11 +80,11 @@ function useTurnkeyAuth() {
|
|
|
53
80
|
setIsLoading(false);
|
|
54
81
|
setIsAuthenticating(false);
|
|
55
82
|
}
|
|
56
|
-
}, [
|
|
83
|
+
}, [setIsAuthenticating]);
|
|
57
84
|
/**
|
|
58
85
|
* Step 2: Verify OTP and authenticate
|
|
59
|
-
* - Verifies OTP with backend
|
|
60
|
-
* - Gets Turnkey session JWT
|
|
86
|
+
* - Verifies OTP with backend via turnkey-jwt strategy (verify action)
|
|
87
|
+
* - Gets Turnkey session JWT from the verify response
|
|
61
88
|
* - Authenticates with b3-api using "turnkey-jwt" strategy
|
|
62
89
|
* - JWT automatically stored in cookies by SDK
|
|
63
90
|
*/
|
|
@@ -66,19 +93,22 @@ function useTurnkeyAuth() {
|
|
|
66
93
|
setError(null);
|
|
67
94
|
setIsAuthenticating(true);
|
|
68
95
|
try {
|
|
69
|
-
debug(`Verifying OTP...`, {
|
|
70
|
-
// Step 1: Verify OTP
|
|
71
|
-
|
|
96
|
+
debug(`Verifying OTP...`, { otpId });
|
|
97
|
+
// Step 1: Verify OTP with backend using turnkey-jwt strategy (verify action)
|
|
98
|
+
// This returns the Turnkey session JWT
|
|
99
|
+
const response = await app_1.default.authenticate({
|
|
100
|
+
strategy: "turnkey-jwt",
|
|
101
|
+
action: "verify",
|
|
72
102
|
otpId,
|
|
73
103
|
otpCode,
|
|
74
104
|
});
|
|
75
|
-
|
|
105
|
+
// The strategy returns the TurnkeyAuthVerifyResponse directly
|
|
106
|
+
const verifyResult = response;
|
|
107
|
+
const { turnkeySessionJwt } = verifyResult;
|
|
108
|
+
debug(`OTP verified! Got Turnkey session JWT. Authenticating with b3-api...`);
|
|
76
109
|
// Step 2: Authenticate with b3-api using Turnkey JWT
|
|
77
|
-
//
|
|
78
|
-
const authResult = await
|
|
79
|
-
strategy: "turnkey-jwt",
|
|
80
|
-
accessToken: turnkeySessionJwt,
|
|
81
|
-
});
|
|
110
|
+
// Use the unified useAuth hook for authentication with "turnkey-jwt" strategy
|
|
111
|
+
const authResult = await authenticate(turnkeySessionJwt, partnerId || "");
|
|
82
112
|
debug(`Successfully authenticated with b3-api!`, authResult);
|
|
83
113
|
// Update auth store to reflect authenticated state
|
|
84
114
|
setIsAuthenticated(true);
|
|
@@ -98,7 +128,7 @@ function useTurnkeyAuth() {
|
|
|
98
128
|
setIsLoading(false);
|
|
99
129
|
setIsAuthenticating(false);
|
|
100
130
|
}
|
|
101
|
-
}, [
|
|
131
|
+
}, [partnerId, setIsAuthenticating, setIsAuthenticated, authenticate]);
|
|
102
132
|
const clearError = (0, react_1.useCallback)(() => {
|
|
103
133
|
setError(null);
|
|
104
134
|
}, []);
|
|
@@ -36,8 +36,8 @@ export declare function useUserQuery(): {
|
|
|
36
36
|
name?: string | undefined;
|
|
37
37
|
address?: string | undefined;
|
|
38
38
|
email?: string | undefined;
|
|
39
|
-
username?: string | undefined;
|
|
40
39
|
phone?: string | undefined;
|
|
40
|
+
username?: string | undefined;
|
|
41
41
|
fid?: string | undefined;
|
|
42
42
|
};
|
|
43
43
|
}[] | undefined;
|
|
@@ -15,6 +15,7 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
15
15
|
import { parseUnits } from "viem";
|
|
16
16
|
import { base, mainnet } from "viem/chains";
|
|
17
17
|
import { useAutoSelectCryptoPaymentMethod } from "../hooks/useAutoSelectCryptoPaymentMethod.js";
|
|
18
|
+
import { useAutoSetActiveWalletFromWagmi } from "../hooks/useAutoSetActiveWalletFromWagmi.js";
|
|
18
19
|
import { useConnectedWalletDisplay } from "../hooks/useConnectedWalletDisplay.js";
|
|
19
20
|
import { useCryptoPaymentMethodState } from "../hooks/useCryptoPaymentMethodState.js";
|
|
20
21
|
import { useRecipientAddressState } from "../hooks/useRecipientAddressState.js";
|
|
@@ -312,6 +313,8 @@ function AnySpendInner({ destinationTokenAddress, destinationTokenChainId, mode
|
|
|
312
313
|
// );
|
|
313
314
|
const { address: globalAddress, wallet: globalWallet, connectedEOAWallet } = useAccountWallet();
|
|
314
315
|
const globalWalletImage = useAccountWalletImage();
|
|
316
|
+
// Auto-set active wallet from wagmi
|
|
317
|
+
useAutoSetActiveWalletFromWagmi();
|
|
315
318
|
// Get wallet address based on selected payment method
|
|
316
319
|
const { walletAddress } = useConnectedWalletDisplay(effectiveCryptoPaymentMethod);
|
|
317
320
|
// Recipient address state with dual-state system (auto + explicit user selection)
|
|
@@ -13,6 +13,7 @@ import { ChevronRight, ChevronRightCircle, Info, Loader2 } from "lucide-react";
|
|
|
13
13
|
import { motion } from "motion/react";
|
|
14
14
|
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
|
15
15
|
import { base } from "viem/chains";
|
|
16
|
+
import { useAutoSetActiveWalletFromWagmi } from "../hooks/useAutoSetActiveWalletFromWagmi.js";
|
|
16
17
|
import { useCryptoPaymentMethodState } from "../hooks/useCryptoPaymentMethodState.js";
|
|
17
18
|
import { useRecipientAddressState } from "../hooks/useRecipientAddressState.js";
|
|
18
19
|
import { AnySpendFingerprintWrapper, getFingerprintConfig } from "./AnySpendFingerprintWrapper.js";
|
|
@@ -110,6 +111,8 @@ function AnySpendCustomInner({ loadOrder, mode = "modal", activeTab: activeTabPr
|
|
|
110
111
|
const hasMounted = useHasMounted();
|
|
111
112
|
const searchParams = useSearchParamsSSR();
|
|
112
113
|
const router = useRouter();
|
|
114
|
+
// Auto-set active wallet from wagmi
|
|
115
|
+
useAutoSetActiveWalletFromWagmi();
|
|
113
116
|
const [activePanel, setActivePanel] = useState(loadOrder ? PanelView.ORDER_DETAILS : PanelView.CONFIRM_ORDER);
|
|
114
117
|
const [activeTab, setActiveTab] = useState(activeTabProps);
|
|
115
118
|
// Payment method state with dual-state system (auto + explicit user selection)
|
|
@@ -4,8 +4,13 @@ import { toast, useAccountWallet, WalletImage } from "../../../../global-account
|
|
|
4
4
|
import { cn } from "../../../../shared/utils/cn.js";
|
|
5
5
|
import { shortenAddress } from "../../../../shared/utils/formatAddress.js";
|
|
6
6
|
import { client } from "../../../../shared/utils/thirdweb.js";
|
|
7
|
+
import { WalletCoinbase, WalletMetamask, WalletRainbow, WalletWalletConnect } from "@web3icons/react";
|
|
7
8
|
import { ChevronLeft, ChevronRightCircle, Wallet, X, ZapIcon } from "lucide-react";
|
|
8
|
-
import {
|
|
9
|
+
import { useState } from "react";
|
|
10
|
+
import { createPortal } from "react-dom";
|
|
11
|
+
import { useSetActiveWallet, useWalletInfo } from "thirdweb/react";
|
|
12
|
+
import { createWallet } from "thirdweb/wallets";
|
|
13
|
+
import { useAccount, useConnect, useDisconnect, useWalletClient } from "wagmi";
|
|
9
14
|
import { useConnectedWalletDisplay } from "../../hooks/useConnectedWalletDisplay.js";
|
|
10
15
|
export var CryptoPaymentMethodType;
|
|
11
16
|
(function (CryptoPaymentMethodType) {
|
|
@@ -15,70 +20,195 @@ export var CryptoPaymentMethodType;
|
|
|
15
20
|
CryptoPaymentMethodType["TRANSFER_CRYPTO"] = "transfer_crypto";
|
|
16
21
|
})(CryptoPaymentMethodType || (CryptoPaymentMethodType = {}));
|
|
17
22
|
export function CryptoPaymentMethod({ selectedPaymentMethod, setSelectedPaymentMethod, isCreatingOrder, onBack, onSelectPaymentMethod, }) {
|
|
18
|
-
const { connectedEOAWallet, connectedSmartWallet } = useAccountWallet();
|
|
23
|
+
const { connectedEOAWallet: connectedEOAWallet, connectedSmartWallet: connectedSmartWallet } = useAccountWallet();
|
|
24
|
+
const { connector, address } = useAccount();
|
|
25
|
+
const { connect, connectors, isPending } = useConnect();
|
|
19
26
|
const { disconnect } = useDisconnect();
|
|
20
|
-
const {
|
|
27
|
+
const { data: walletClient } = useWalletClient();
|
|
28
|
+
const [showWalletModal, setShowWalletModal] = useState(false);
|
|
29
|
+
const setActiveWallet = useSetActiveWallet();
|
|
21
30
|
const { data: eoaWalletInfo } = useWalletInfo(connectedEOAWallet?.id);
|
|
31
|
+
const isConnected = !!connectedEOAWallet;
|
|
22
32
|
const globalAddress = connectedSmartWallet?.getAccount()?.address;
|
|
23
33
|
// Use custom hook to determine wallet display logic
|
|
24
|
-
const { shouldShowConnectedEOA } = useConnectedWalletDisplay(selectedPaymentMethod);
|
|
25
|
-
|
|
26
|
-
|
|
34
|
+
const { shouldShowConnectedEOA, shouldShowWagmiWallet } = useConnectedWalletDisplay(selectedPaymentMethod);
|
|
35
|
+
console.log("shouldShowWagmiWallet :", shouldShowWagmiWallet);
|
|
36
|
+
// Map wagmi connector names to thirdweb wallet IDs
|
|
37
|
+
const getThirdwebWalletId = (connectorName) => {
|
|
38
|
+
const walletMap = {
|
|
39
|
+
MetaMask: "io.metamask",
|
|
40
|
+
"Coinbase Wallet": "com.coinbase.wallet",
|
|
41
|
+
Rainbow: "me.rainbow",
|
|
42
|
+
WalletConnect: "walletConnect",
|
|
43
|
+
Phantom: "app.phantom",
|
|
44
|
+
};
|
|
45
|
+
return walletMap[connectorName] || null;
|
|
46
|
+
};
|
|
47
|
+
// Create thirdweb wallet from wagmi connector
|
|
48
|
+
const createThirdwebWalletFromConnector = async (connectorName) => {
|
|
49
|
+
const walletId = getThirdwebWalletId(connectorName);
|
|
50
|
+
if (!walletId) {
|
|
51
|
+
console.warn(`No thirdweb wallet ID found for connector: ${connectorName}`);
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const thirdwebWallet = createWallet(walletId);
|
|
56
|
+
// Connect the wallet to sync with the existing wagmi connection
|
|
57
|
+
await thirdwebWallet.connect({ client });
|
|
58
|
+
return thirdwebWallet;
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
console.error(`Failed to create thirdweb wallet for ${connectorName}:`, error);
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
// Define available wallet connectors
|
|
66
|
+
const availableConnectors = connectors.filter(connector => ["MetaMask", "WalletConnect", "Coinbase Wallet", "Rainbow", "Phantom"].includes(connector.name));
|
|
67
|
+
// Define wallet options with icons and info
|
|
68
|
+
const walletOptions = [
|
|
69
|
+
{
|
|
70
|
+
id: "metamask",
|
|
71
|
+
name: "MetaMask",
|
|
72
|
+
icon: _jsx(WalletMetamask, { size: 48 }),
|
|
73
|
+
description: "Connect using MetaMask browser extension",
|
|
74
|
+
connector: availableConnectors.find(c => c.name === "MetaMask"),
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
id: "coinbase",
|
|
78
|
+
name: "Coinbase Wallet",
|
|
79
|
+
icon: _jsx(WalletCoinbase, { size: 48 }),
|
|
80
|
+
description: "Connect using Coinbase Wallet",
|
|
81
|
+
connector: availableConnectors.find(c => c.name === "Coinbase Wallet"),
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
id: "rainbow",
|
|
85
|
+
name: "Rainbow",
|
|
86
|
+
icon: _jsx(WalletRainbow, { size: 48 }),
|
|
87
|
+
description: "Connect using Rainbow wallet",
|
|
88
|
+
connector: availableConnectors.find(c => c.name === "Rainbow"),
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
id: "walletconnect",
|
|
92
|
+
name: "WalletConnect",
|
|
93
|
+
icon: _jsx(WalletWalletConnect, { size: 48 }),
|
|
94
|
+
description: "Connect using WalletConnect protocol",
|
|
95
|
+
connector: availableConnectors.find(c => c.name === "WalletConnect"),
|
|
96
|
+
},
|
|
97
|
+
].filter(wallet => wallet.connector); // Only show wallets that have available connectors
|
|
98
|
+
// Reset modal state when closing
|
|
99
|
+
const handleCloseModal = () => {
|
|
100
|
+
setShowWalletModal(false);
|
|
101
|
+
};
|
|
102
|
+
// Function to request wallet permissions for specific wallet
|
|
103
|
+
const requestWalletPermissions = async (walletConnector) => {
|
|
27
104
|
try {
|
|
28
|
-
//
|
|
29
|
-
if (
|
|
30
|
-
|
|
105
|
+
// If a specific wallet connector is provided and it's different from current
|
|
106
|
+
if (walletConnector && connector?.name !== walletConnector.name) {
|
|
107
|
+
// Disconnect current and connect to the selected wallet
|
|
108
|
+
// if (isConnected) {
|
|
109
|
+
// disconnect();
|
|
110
|
+
// // Small delay to ensure disconnection
|
|
111
|
+
// await new Promise(resolve => setTimeout(resolve, 100));
|
|
112
|
+
// }
|
|
113
|
+
await connect({ connector: walletConnector });
|
|
114
|
+
setSelectedPaymentMethod(CryptoPaymentMethodType.CONNECT_WALLET);
|
|
115
|
+
onSelectPaymentMethod(CryptoPaymentMethodType.CONNECT_WALLET);
|
|
116
|
+
toast.success(`Connected to ${walletConnector.name}`);
|
|
117
|
+
return;
|
|
31
118
|
}
|
|
32
|
-
|
|
33
|
-
if (
|
|
34
|
-
|
|
119
|
+
// If same wallet or no specific wallet, request permissions for account switching
|
|
120
|
+
if (walletClient && "request" in walletClient) {
|
|
121
|
+
await walletClient.request({
|
|
122
|
+
method: "wallet_requestPermissions",
|
|
123
|
+
params: [{ eth_accounts: {} }],
|
|
124
|
+
});
|
|
125
|
+
toast.success("Account selection completed");
|
|
35
126
|
setSelectedPaymentMethod(CryptoPaymentMethodType.CONNECT_WALLET);
|
|
36
127
|
onSelectPaymentMethod(CryptoPaymentMethodType.CONNECT_WALLET);
|
|
37
|
-
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
// Fallback: show modal for manual wallet selection
|
|
131
|
+
setShowWalletModal(true);
|
|
38
132
|
}
|
|
39
133
|
}
|
|
40
134
|
catch (error) {
|
|
41
|
-
console.error("Failed to
|
|
135
|
+
console.error("Failed to request wallet permissions:", error);
|
|
42
136
|
if (error && typeof error === "object" && "message" in error) {
|
|
43
137
|
const errorMessage = error.message.toLowerCase();
|
|
44
138
|
if (errorMessage.includes("rejected") ||
|
|
45
139
|
errorMessage.includes("denied") ||
|
|
46
140
|
errorMessage.includes("cancelled")) {
|
|
47
|
-
|
|
141
|
+
toast.error("Account selection cancelled");
|
|
48
142
|
}
|
|
49
143
|
else {
|
|
50
|
-
toast.error("Failed to
|
|
144
|
+
toast.error("Failed to open account selection");
|
|
51
145
|
}
|
|
52
146
|
}
|
|
147
|
+
else {
|
|
148
|
+
toast.error("Failed to open account selection");
|
|
149
|
+
}
|
|
53
150
|
}
|
|
54
151
|
};
|
|
55
|
-
return (
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
152
|
+
return (_jsxs("div", { className: "crypto-payment-method mx-auto h-fit w-[460px] max-w-full px-5 pb-5 pt-5 sm:px-0 sm:pt-5", children: [_jsxs("div", { className: cn("relative flex flex-col gap-10"), children: [_jsx("button", { onClick: onBack, className: "text-as-quaternary hover:text-as-primary absolute flex h-8 w-8 items-center justify-center rounded-lg transition-colors", children: _jsx(ChevronLeft, { className: "h-6 w-6" }) }), _jsx("div", { className: "flex items-center justify-around gap-4", children: _jsx("div", { className: "flex-1 text-center", children: _jsx("h2", { className: "text-as-primary text-lg font-semibold", children: "Select a payment method" }) }) }), process.env.NODE_ENV === "development" && (_jsxs("div", { className: "rounded-lg border border-yellow-500/50 bg-yellow-50 p-3 dark:bg-yellow-950/20", children: [_jsx("p", { className: "mb-2 text-xs font-semibold text-yellow-800 dark:text-yellow-300", children: "\uD83E\uDDEA Toast Test (Dev Only)" }), _jsxs("div", { className: "flex flex-wrap gap-2", children: [_jsx("button", { onClick: () => toast.success("Success! Transaction completed"), className: "rounded bg-green-600 px-2 py-1 text-xs font-medium text-white hover:bg-green-700", children: "Success" }), _jsx("button", { onClick: () => toast.error("Error! Transaction failed"), className: "rounded bg-red-600 px-2 py-1 text-xs font-medium text-white hover:bg-red-700", children: "Error" }), _jsx("button", { onClick: () => toast.info("Info: Processing your request..."), className: "rounded bg-blue-600 px-2 py-1 text-xs font-medium text-white hover:bg-blue-700", children: "Info" }), _jsx("button", { onClick: () => toast.warning("Warning: Low balance detected"), className: "rounded bg-yellow-600 px-2 py-1 text-xs font-medium text-white hover:bg-yellow-700", children: "Warning" }), _jsx("button", { onClick: () => {
|
|
153
|
+
toast.success("Multiple test 1");
|
|
154
|
+
setTimeout(() => toast.info("Multiple test 2"), 200);
|
|
155
|
+
setTimeout(() => toast.warning("Multiple test 3"), 400);
|
|
156
|
+
}, className: "rounded bg-purple-600 px-2 py-1 text-xs font-medium text-white hover:bg-purple-700", children: "Multiple" })] })] })), _jsxs("div", { className: "crypto-payment-methods flex flex-col gap-4", children: [(shouldShowConnectedEOA || shouldShowWagmiWallet || globalAddress) && (_jsxs("div", { className: "installed-wallets", children: [_jsx("h3", { className: "text-as-primary/80 mb-3 text-sm font-medium", children: "Connected wallets" }), _jsxs("div", { className: "space-y-2", children: [shouldShowConnectedEOA && (_jsx("button", { onClick: () => {
|
|
157
|
+
setSelectedPaymentMethod(CryptoPaymentMethodType.CONNECT_WALLET);
|
|
158
|
+
onSelectPaymentMethod(CryptoPaymentMethodType.CONNECT_WALLET);
|
|
159
|
+
if (connectedEOAWallet) {
|
|
160
|
+
setActiveWallet(connectedEOAWallet);
|
|
161
|
+
}
|
|
162
|
+
toast.success(`Selected ${eoaWalletInfo?.name || connector?.name || "wallet"}`);
|
|
163
|
+
}, className: cn("crypto-payment-method-connect-wallet eoa-wallet w-full rounded-xl border p-4 text-left transition-all hover:shadow-md", selectedPaymentMethod === CryptoPaymentMethodType.CONNECT_WALLET
|
|
164
|
+
? "connected-wallet border-as-brand bg-as-brand/5"
|
|
165
|
+
: "border-as-border-secondary bg-as-surface-primary hover:border-as-secondary/80"), children: _jsxs("div", { className: "flex items-center justify-between", children: [_jsxs("div", { className: "flex items-center gap-3", children: [_jsx("div", { className: "wallet-icon flex h-10 w-10 items-center justify-center rounded-full bg-blue-100", children: _jsx(Wallet, { className: "h-5 w-5 text-blue-600" }) }), _jsxs("div", { className: "flex flex-col", children: [_jsx("span", { className: "text-as-primary font-semibold", children: eoaWalletInfo?.name || connector?.name || "Connected Wallet" }), _jsx("span", { className: "text-as-primary/60 text-sm", children: shortenAddress(connectedEOAWallet?.getAccount()?.address || "") })] })] }), _jsxs("div", { className: "flex items-center gap-2", children: [selectedPaymentMethod === CryptoPaymentMethodType.CONNECT_WALLET && (_jsx("div", { className: "h-2 w-2 rounded-full bg-green-500" })), _jsx("button", { onClick: e => {
|
|
166
|
+
e.stopPropagation();
|
|
167
|
+
disconnect();
|
|
168
|
+
toast.success("Wallet disconnected");
|
|
169
|
+
if (selectedPaymentMethod === CryptoPaymentMethodType.CONNECT_WALLET) {
|
|
170
|
+
setSelectedPaymentMethod(CryptoPaymentMethodType.NONE);
|
|
171
|
+
}
|
|
172
|
+
}, className: "text-as-primary/60 hover:text-as-primary/80 rounded-lg p-1.5 transition-colors", children: _jsx(X, { className: "h-4 w-4" }) })] })] }) })), shouldShowWagmiWallet && (_jsx("button", { onClick: async () => {
|
|
173
|
+
setSelectedPaymentMethod(CryptoPaymentMethodType.CONNECT_WALLET);
|
|
174
|
+
onSelectPaymentMethod(CryptoPaymentMethodType.CONNECT_WALLET);
|
|
175
|
+
// Create thirdweb wallet from wagmi connector
|
|
176
|
+
if (connector?.name) {
|
|
177
|
+
const thirdwebWallet = await createThirdwebWalletFromConnector(connector.name);
|
|
178
|
+
if (thirdwebWallet) {
|
|
179
|
+
setActiveWallet(thirdwebWallet);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
toast.success(`Selected ${connector?.name || "wallet"}`);
|
|
183
|
+
}, className: cn("crypto-payment-method-connect-wallet wagmi-wallet w-full rounded-xl border p-4 text-left transition-all hover:shadow-md", selectedPaymentMethod === CryptoPaymentMethodType.CONNECT_WALLET
|
|
184
|
+
? "connected-wallet border-as-brand bg-as-brand/5"
|
|
185
|
+
: "border-as-border-secondary bg-as-surface-primary hover:border-as-secondary/80"), children: _jsxs("div", { className: "flex items-center justify-between", children: [_jsxs("div", { className: "flex items-center gap-3", children: [_jsx("div", { className: "wallet-icon flex h-10 w-10 items-center justify-center rounded-full bg-blue-100", children: _jsx(Wallet, { className: "h-5 w-5 text-blue-600" }) }), _jsxs("div", { className: "flex flex-col", children: [_jsx("span", { className: "text-as-primary font-semibold", children: connector?.name || "Connected Wallet" }), _jsx("span", { className: "text-as-primary/60 text-sm", children: shortenAddress(address || "") })] })] }), _jsxs("div", { className: "flex items-center gap-2", children: [selectedPaymentMethod === CryptoPaymentMethodType.CONNECT_WALLET && (_jsx("div", { className: "h-2 w-2 rounded-full bg-green-500" })), _jsx("button", { onClick: e => {
|
|
186
|
+
e.stopPropagation();
|
|
187
|
+
disconnect();
|
|
188
|
+
toast.success("Wallet disconnected");
|
|
189
|
+
if (selectedPaymentMethod === CryptoPaymentMethodType.CONNECT_WALLET) {
|
|
190
|
+
setSelectedPaymentMethod(CryptoPaymentMethodType.NONE);
|
|
191
|
+
}
|
|
192
|
+
}, className: "text-as-primary/60 hover:text-as-primary/80 rounded-lg p-1.5 transition-colors", children: _jsx(X, { className: "h-4 w-4" }) })] })] }) })), globalAddress && (_jsx("button", { onClick: () => {
|
|
193
|
+
setSelectedPaymentMethod(CryptoPaymentMethodType.GLOBAL_WALLET);
|
|
194
|
+
onSelectPaymentMethod(CryptoPaymentMethodType.GLOBAL_WALLET);
|
|
195
|
+
toast.success("Selected B3 Account");
|
|
196
|
+
}, className: cn("crypto-payment-method-global-wallet w-full rounded-xl border p-4 text-left transition-all hover:shadow-md", selectedPaymentMethod === CryptoPaymentMethodType.GLOBAL_WALLET
|
|
197
|
+
? "connected-wallet border-as-brand bg-as-brand/5"
|
|
198
|
+
: "border-as-border-secondary bg-as-surface-primary hover:border-as-secondary/80"), children: _jsxs("div", { className: "flex items-center justify-between", children: [_jsxs("div", { className: "flex items-center gap-3", children: [_jsx(WalletImage, { fallback: _jsx("div", { className: "wallet-icon flex h-10 w-10 items-center justify-center rounded-full bg-purple-100", children: _jsx(Wallet, { className: "h-5 w-5 text-purple-600" }) }) }), _jsxs("div", { className: "flex flex-col", children: [_jsx("span", { className: "text-as-primary font-semibold", children: "Global Account" }), _jsx("span", { className: "text-as-primary/60 text-sm", children: shortenAddress(globalAddress || "") })] })] }), _jsx("div", { className: "flex items-center gap-2", children: selectedPaymentMethod === CryptoPaymentMethodType.GLOBAL_WALLET && (_jsx("div", { className: "h-2 w-2 rounded-full bg-green-500" })) })] }) }))] })] })), _jsxs("div", { className: "other-payment-methods", children: [_jsx("h3", { className: "text-as-primary/80 mb-3 text-sm font-medium", children: "Payment methods" }), _jsxs("div", { className: "space-y-3", children: [_jsxs("button", { onClick: () => {
|
|
199
|
+
// Always show wallet selection modal first
|
|
200
|
+
setShowWalletModal(true);
|
|
201
|
+
}, className: "crypto-payment-method-connect-wallet bg-as-surface-primary border-as-border-secondary hover:border-as-secondary/80 group flex w-full items-center justify-between gap-4 rounded-xl border px-4 py-3.5 transition-all duration-200 hover:shadow-md", children: [_jsxs("div", { className: "flex items-center gap-3", children: [_jsx("div", { className: "wallet-icon flex h-8 w-8 items-center justify-center rounded-full bg-blue-100", children: _jsx(Wallet, { className: "h-4 w-4 text-blue-600" }) }), _jsx("div", { className: "flex flex-col items-start text-left", children: _jsx("h4", { className: "text-as-primary font-semibold", children: "Connect wallet" }) })] }), _jsx(ChevronRightCircle, { className: "text-as-primary/40 group-hover:text-as-primary/60 h-5 w-5 transition-colors" })] }), _jsxs("button", { onClick: () => {
|
|
202
|
+
setSelectedPaymentMethod(CryptoPaymentMethodType.TRANSFER_CRYPTO);
|
|
203
|
+
onSelectPaymentMethod(CryptoPaymentMethodType.TRANSFER_CRYPTO);
|
|
204
|
+
}, disabled: isCreatingOrder, className: "crypto-payment-method-transfer bg-as-surface-primary border-as-border-secondary hover:border-as-secondary/80 group flex w-full items-center justify-between gap-4 rounded-xl border px-4 py-3.5 transition-all duration-200 hover:shadow-md", children: [_jsxs("div", { className: "flex items-center gap-3", children: [_jsx("div", { className: "wallet-icon flex h-8 w-8 items-center justify-center rounded-full bg-orange-100", children: _jsx(ZapIcon, { className: "h-4 w-4" }) }), _jsx("div", { className: "flex flex-col items-start text-left", children: _jsx("h4", { className: "text-as-primary font-semibold", children: "Transfer crypto" }) })] }), _jsx(ChevronRightCircle, { className: "text-as-primary/40 group-hover:text-as-primary/60 h-5 w-5 transition-colors" })] })] })] })] })] }), showWalletModal &&
|
|
205
|
+
createPortal(_jsx("div", { className: "wallet-connection-modal pointer-events-auto fixed inset-0 z-[9999] flex items-center justify-center bg-black/50", children: _jsxs("div", { className: "max-h-[80vh] w-[400px] max-w-[90vw] overflow-auto rounded-xl bg-white p-6 dark:bg-gray-900", children: [_jsxs("div", { className: "mb-4 flex items-center justify-between", children: [_jsx("h3", { className: "text-lg font-semibold text-gray-900 dark:text-white", children: isConnected ? "Switch wallet or account" : "Choose wallet to connect" }), _jsx("button", { onClick: handleCloseModal, className: "text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200", children: _jsx(X, { className: "h-5 w-5" }) })] }), _jsx("div", { className: "space-y-4", children: _jsx("div", { className: "space-y-3", children: walletOptions.map(walletOption => {
|
|
206
|
+
const isCurrentWallet = isConnected && connector?.name === walletOption.connector?.name;
|
|
207
|
+
return (_jsx("button", { onClick: async () => {
|
|
208
|
+
handleCloseModal();
|
|
209
|
+
await requestWalletPermissions(walletOption.connector);
|
|
210
|
+
}, disabled: isPending, className: `wallet-option w-full rounded-xl border p-4 text-left transition-all hover:shadow-md disabled:opacity-50 ${isCurrentWallet
|
|
211
|
+
? "wallet-option--active border-blue-500 bg-blue-50 dark:bg-blue-900/20"
|
|
212
|
+
: "border-gray-200 bg-white hover:border-gray-300 dark:border-gray-600 dark:bg-gray-800 dark:hover:border-gray-500"}`, children: _jsxs("div", { className: "flex items-center justify-between", children: [_jsxs("div", { className: "flex items-center gap-3", children: [walletOption.icon, _jsxs("div", { children: [_jsxs("div", { className: "wallet-option-name flex items-center gap-2", children: [_jsx("div", { className: "text-sm font-semibold text-gray-900 dark:text-white", children: walletOption.name }), isCurrentWallet && (_jsx("span", { className: "rounded-full bg-blue-100 px-2 py-0.5 text-xs font-medium text-blue-700 dark:bg-blue-800 dark:text-blue-200", children: "Connected" }))] }), _jsx("div", { className: "text-xs text-gray-500 dark:text-gray-400", children: isCurrentWallet ? "Switch account or reconnect" : walletOption.description })] })] }), _jsx(ChevronRightCircle, { className: "h-5 w-5 text-gray-400" })] }) }, walletOption.id));
|
|
213
|
+
}) }) })] }) }), typeof window !== "undefined" ? document.getElementById("b3-root") || document.body : document.body)] }));
|
|
84
214
|
}
|
|
@@ -9,6 +9,7 @@ import { base, mainnet } from "viem/chains";
|
|
|
9
9
|
import { CryptoPaymentMethodType } from "../components/common/CryptoPaymentMethod.js";
|
|
10
10
|
import { FiatPaymentMethod } from "../components/common/FiatPaymentMethod.js";
|
|
11
11
|
import { useAutoSelectCryptoPaymentMethod } from "./useAutoSelectCryptoPaymentMethod.js";
|
|
12
|
+
import { useAutoSetActiveWalletFromWagmi } from "./useAutoSetActiveWalletFromWagmi.js";
|
|
12
13
|
import { useConnectedWalletDisplay } from "./useConnectedWalletDisplay.js";
|
|
13
14
|
import { useCryptoPaymentMethodState } from "./useCryptoPaymentMethodState.js";
|
|
14
15
|
import { useRecipientAddressState } from "./useRecipientAddressState.js";
|
|
@@ -48,6 +49,8 @@ export function useAnyspendFlow({ paymentType = "crypto", recipientAddress, load
|
|
|
48
49
|
// Recipient state with dual-state system (auto + explicit user selection)
|
|
49
50
|
const { address: globalAddress } = useAccountWallet();
|
|
50
51
|
const { walletAddress } = useConnectedWalletDisplay(effectiveCryptoPaymentMethod);
|
|
52
|
+
// Auto-set active wallet from wagmi
|
|
53
|
+
useAutoSetActiveWalletFromWagmi();
|
|
51
54
|
// Recipient address state - hook automatically manages priority: props > user selection > wallet/global
|
|
52
55
|
const { setSelectedRecipientAddress, effectiveRecipientAddress } = useRecipientAddressState({
|
|
53
56
|
recipientAddressFromProps: recipientAddress,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook that automatically sets the active thirdweb wallet when a wagmi wallet connects.
|
|
3
|
+
*
|
|
4
|
+
* This is useful for syncing wagmi wallet connections with thirdweb's wallet system,
|
|
5
|
+
* ensuring that when users connect via wagmi, the active wallet is properly set.
|
|
6
|
+
*
|
|
7
|
+
* Place this hook in components that stay mounted throughout the user flow
|
|
8
|
+
* (not in components that unmount during navigation).
|
|
9
|
+
*/
|
|
10
|
+
export declare function useAutoSetActiveWalletFromWagmi(): void;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { client } from "../../../shared/utils/thirdweb.js";
|
|
2
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
3
|
+
import { useSetActiveWallet } from "thirdweb/react";
|
|
4
|
+
import { createWallet } from "thirdweb/wallets";
|
|
5
|
+
import { useAccount } from "wagmi";
|
|
6
|
+
/**
|
|
7
|
+
* Hook that automatically sets the active thirdweb wallet when a wagmi wallet connects.
|
|
8
|
+
*
|
|
9
|
+
* This is useful for syncing wagmi wallet connections with thirdweb's wallet system,
|
|
10
|
+
* ensuring that when users connect via wagmi, the active wallet is properly set.
|
|
11
|
+
*
|
|
12
|
+
* Place this hook in components that stay mounted throughout the user flow
|
|
13
|
+
* (not in components that unmount during navigation).
|
|
14
|
+
*/
|
|
15
|
+
export function useAutoSetActiveWalletFromWagmi() {
|
|
16
|
+
const { address: wagmiAddress, connector: wagmiConnector } = useAccount();
|
|
17
|
+
const setActiveWallet = useSetActiveWallet();
|
|
18
|
+
const prevWagmiAddress = useRef(undefined);
|
|
19
|
+
// Map wagmi connector names to thirdweb wallet IDs
|
|
20
|
+
const getThirdwebWalletId = useCallback((connectorName) => {
|
|
21
|
+
const walletMap = {
|
|
22
|
+
MetaMask: "io.metamask",
|
|
23
|
+
"Coinbase Wallet": "com.coinbase.wallet",
|
|
24
|
+
Rainbow: "me.rainbow",
|
|
25
|
+
WalletConnect: "walletConnect",
|
|
26
|
+
Phantom: "app.phantom",
|
|
27
|
+
};
|
|
28
|
+
return walletMap[connectorName] || null;
|
|
29
|
+
}, []);
|
|
30
|
+
// Create thirdweb wallet from wagmi connector
|
|
31
|
+
const createThirdwebWalletFromConnector = useCallback(async (connectorName) => {
|
|
32
|
+
const walletId = getThirdwebWalletId(connectorName);
|
|
33
|
+
if (!walletId) {
|
|
34
|
+
console.warn(`No thirdweb wallet ID found for connector: ${connectorName}`);
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
const thirdwebWallet = createWallet(walletId);
|
|
39
|
+
await thirdwebWallet.connect({ client });
|
|
40
|
+
return thirdwebWallet;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error(`Failed to create thirdweb wallet for ${connectorName}:`, error);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}, [getThirdwebWalletId]);
|
|
47
|
+
// Listen for wagmi wallet connections and automatically set active wallet
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
const isNewConnection = wagmiAddress && wagmiAddress !== prevWagmiAddress.current;
|
|
50
|
+
if (isNewConnection && wagmiConnector?.name) {
|
|
51
|
+
prevWagmiAddress.current = wagmiAddress;
|
|
52
|
+
const setupThirdwebWallet = async () => {
|
|
53
|
+
try {
|
|
54
|
+
const thirdwebWallet = await createThirdwebWalletFromConnector(wagmiConnector.name);
|
|
55
|
+
if (thirdwebWallet) {
|
|
56
|
+
setActiveWallet(thirdwebWallet);
|
|
57
|
+
console.log(`Auto-set active wallet for ${wagmiConnector.name}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
console.error("Failed to auto-set active wallet:", error);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
setupThirdwebWallet();
|
|
65
|
+
}
|
|
66
|
+
if (!wagmiAddress) {
|
|
67
|
+
prevWagmiAddress.current = undefined;
|
|
68
|
+
}
|
|
69
|
+
}, [wagmiAddress, wagmiConnector?.name, setActiveWallet, createThirdwebWalletFromConnector]);
|
|
70
|
+
}
|