@phantom/react-sdk 1.0.0-beta.9 → 1.0.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/README.md +609 -58
- package/dist/index.d.ts +75 -22
- package/dist/index.js +969 -175
- package/dist/index.mjs +975 -172
- package/package.json +13 -6
package/dist/index.mjs
CHANGED
|
@@ -1,46 +1,798 @@
|
|
|
1
1
|
// src/PhantomProvider.tsx
|
|
2
|
-
import {
|
|
2
|
+
import { useState as useState8, useEffect as useEffect5, useMemo as useMemo3, useCallback as useCallback6 } from "react";
|
|
3
3
|
import { BrowserSDK } from "@phantom/browser-sdk";
|
|
4
|
-
import {
|
|
4
|
+
import { mergeTheme, darkTheme, ThemeProvider } from "@phantom/wallet-sdk-ui";
|
|
5
|
+
|
|
6
|
+
// src/PhantomContext.ts
|
|
7
|
+
import { createContext, useContext } from "react";
|
|
5
8
|
var PhantomContext = createContext(void 0);
|
|
6
|
-
function
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
function usePhantom() {
|
|
10
|
+
const context = useContext(PhantomContext);
|
|
11
|
+
if (!context) {
|
|
12
|
+
throw new Error("usePhantom must be used within a PhantomProvider");
|
|
13
|
+
}
|
|
14
|
+
return context;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// src/ModalProvider.tsx
|
|
18
|
+
import { useState as useState7, useCallback as useCallback5, useMemo as useMemo2 } from "react";
|
|
19
|
+
|
|
20
|
+
// src/ModalContext.ts
|
|
21
|
+
import { createContext as createContext2, useContext as useContext2 } from "react";
|
|
22
|
+
var ModalContext = createContext2(void 0);
|
|
23
|
+
function useModal() {
|
|
24
|
+
const context = useContext2(ModalContext);
|
|
25
|
+
if (!context) {
|
|
26
|
+
throw new Error("useModal must be used within a ModalProvider");
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
open: context.openModal,
|
|
30
|
+
close: context.closeModal,
|
|
31
|
+
isOpened: context.isModalOpen
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/ModalProvider.tsx
|
|
36
|
+
import { isMobileDevice as isMobileDevice2 } from "@phantom/browser-sdk";
|
|
37
|
+
import { Modal } from "@phantom/wallet-sdk-ui";
|
|
38
|
+
|
|
39
|
+
// src/components/ConnectModalContent.tsx
|
|
40
|
+
import { useState as useState4, useCallback as useCallback3, useMemo } from "react";
|
|
41
|
+
import { isMobileDevice } from "@phantom/browser-sdk";
|
|
42
|
+
import {
|
|
43
|
+
Button,
|
|
44
|
+
LoginWithPhantomButton,
|
|
45
|
+
Icon as Icon2,
|
|
46
|
+
BoundedIcon,
|
|
47
|
+
Text,
|
|
48
|
+
hexToRgba,
|
|
49
|
+
useTheme as useTheme2,
|
|
50
|
+
ModalHeader
|
|
51
|
+
} from "@phantom/wallet-sdk-ui";
|
|
52
|
+
import { getProviderName } from "@phantom/constants";
|
|
53
|
+
|
|
54
|
+
// src/hooks/useIsExtensionInstalled.ts
|
|
55
|
+
import * as React from "react";
|
|
56
|
+
import { waitForPhantomExtension } from "@phantom/browser-sdk";
|
|
57
|
+
function useIsExtensionInstalled() {
|
|
58
|
+
const [isLoading, setIsLoading] = React.useState(true);
|
|
59
|
+
const [isInstalled, setIsInstalled] = React.useState(false);
|
|
60
|
+
React.useEffect(() => {
|
|
61
|
+
let isMounted = true;
|
|
62
|
+
const checkExtension = async () => {
|
|
63
|
+
try {
|
|
64
|
+
setIsLoading(true);
|
|
65
|
+
const result = await waitForPhantomExtension(3e3);
|
|
66
|
+
if (isMounted) {
|
|
67
|
+
setIsInstalled(result);
|
|
68
|
+
}
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (isMounted) {
|
|
71
|
+
setIsInstalled(false);
|
|
72
|
+
}
|
|
73
|
+
} finally {
|
|
74
|
+
if (isMounted) {
|
|
75
|
+
setIsLoading(false);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
checkExtension();
|
|
80
|
+
return () => {
|
|
81
|
+
isMounted = false;
|
|
82
|
+
};
|
|
83
|
+
}, []);
|
|
84
|
+
return { isLoading, isInstalled };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/hooks/useIsPhantomLoginAvailable.ts
|
|
88
|
+
import * as React2 from "react";
|
|
89
|
+
import { isPhantomLoginAvailable } from "@phantom/browser-sdk";
|
|
90
|
+
function useIsPhantomLoginAvailable() {
|
|
91
|
+
const [isLoading, setIsLoading] = React2.useState(true);
|
|
92
|
+
const [isAvailable, setIsAvailable] = React2.useState(false);
|
|
93
|
+
React2.useEffect(() => {
|
|
94
|
+
let isMounted = true;
|
|
95
|
+
const checkPhantomLogin = async () => {
|
|
96
|
+
try {
|
|
97
|
+
setIsLoading(true);
|
|
98
|
+
const result = await isPhantomLoginAvailable(3e3);
|
|
99
|
+
if (isMounted) {
|
|
100
|
+
setIsAvailable(result);
|
|
101
|
+
}
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (isMounted) {
|
|
104
|
+
setIsAvailable(false);
|
|
105
|
+
}
|
|
106
|
+
} finally {
|
|
107
|
+
if (isMounted) {
|
|
108
|
+
setIsLoading(false);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
checkPhantomLogin();
|
|
113
|
+
return () => {
|
|
114
|
+
isMounted = false;
|
|
115
|
+
};
|
|
116
|
+
}, []);
|
|
117
|
+
return { isLoading, isAvailable };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/hooks/useConnect.ts
|
|
121
|
+
import { useCallback } from "react";
|
|
122
|
+
function useConnect() {
|
|
123
|
+
const { sdk, isConnecting, isLoading, errors } = usePhantom();
|
|
124
|
+
const connect = useCallback(
|
|
125
|
+
async (options) => {
|
|
126
|
+
if (!sdk) {
|
|
127
|
+
throw new Error("SDK not initialized");
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
const result = await sdk.connect(options);
|
|
131
|
+
return result;
|
|
132
|
+
} catch (err) {
|
|
133
|
+
console.error("Error connecting to Phantom:", err);
|
|
134
|
+
throw err;
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
[sdk]
|
|
138
|
+
);
|
|
139
|
+
return {
|
|
140
|
+
connect,
|
|
141
|
+
isConnecting,
|
|
142
|
+
isLoading,
|
|
143
|
+
error: errors.connect
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// src/hooks/useDiscoveredWallets.ts
|
|
148
|
+
import { useCallback as useCallback2, useState as useState3, useEffect as useEffect3 } from "react";
|
|
149
|
+
function useDiscoveredWallets() {
|
|
150
|
+
const { sdk } = usePhantom();
|
|
151
|
+
const [wallets, setWallets] = useState3([]);
|
|
152
|
+
const [isLoading, setIsLoading] = useState3(true);
|
|
153
|
+
const [error, setError] = useState3(null);
|
|
154
|
+
const refetch = useCallback2(async () => {
|
|
155
|
+
if (!sdk) {
|
|
156
|
+
setWallets([]);
|
|
157
|
+
setError(null);
|
|
158
|
+
setIsLoading(false);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
setIsLoading(true);
|
|
163
|
+
setError(null);
|
|
164
|
+
const initialWallets = sdk.getDiscoveredWallets();
|
|
165
|
+
if (initialWallets.length > 0) {
|
|
166
|
+
setWallets(initialWallets);
|
|
167
|
+
setIsLoading(false);
|
|
168
|
+
} else {
|
|
169
|
+
await sdk.discoverWallets();
|
|
170
|
+
const discoveredWallets = sdk.getDiscoveredWallets();
|
|
171
|
+
setWallets(discoveredWallets);
|
|
172
|
+
setIsLoading(false);
|
|
173
|
+
}
|
|
174
|
+
} catch (err) {
|
|
175
|
+
const error2 = err instanceof Error ? err : new Error("Failed to fetch discovered wallets");
|
|
176
|
+
setError(error2);
|
|
177
|
+
setWallets([]);
|
|
178
|
+
setIsLoading(false);
|
|
179
|
+
}
|
|
180
|
+
}, [sdk]);
|
|
181
|
+
useEffect3(() => {
|
|
182
|
+
refetch();
|
|
183
|
+
}, [refetch]);
|
|
184
|
+
return {
|
|
185
|
+
wallets,
|
|
186
|
+
isLoading,
|
|
187
|
+
error,
|
|
188
|
+
refetch
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// src/components/ChainIcon.tsx
|
|
193
|
+
import { Icon, useTheme } from "@phantom/wallet-sdk-ui";
|
|
194
|
+
import { jsx } from "react/jsx-runtime";
|
|
195
|
+
var IconWrapper = ({ children }) => {
|
|
196
|
+
const theme = useTheme();
|
|
197
|
+
return /* @__PURE__ */ jsx(
|
|
198
|
+
"span",
|
|
199
|
+
{
|
|
200
|
+
style: {
|
|
201
|
+
display: "inline-flex",
|
|
202
|
+
alignItems: "center",
|
|
203
|
+
justifyContent: "center",
|
|
204
|
+
borderRadius: "4px",
|
|
205
|
+
backgroundColor: theme.aux,
|
|
206
|
+
color: theme.text,
|
|
207
|
+
padding: "2px"
|
|
208
|
+
},
|
|
209
|
+
children
|
|
210
|
+
}
|
|
211
|
+
);
|
|
212
|
+
};
|
|
213
|
+
function ChainIcon({ addressType, size = 8 }) {
|
|
214
|
+
const theme = useTheme();
|
|
215
|
+
const type = addressType.toLowerCase();
|
|
216
|
+
if (type.includes("solana")) {
|
|
217
|
+
return /* @__PURE__ */ jsx(IconWrapper, { children: /* @__PURE__ */ jsx(Icon, { type: "solana", size, color: theme.text }) });
|
|
218
|
+
}
|
|
219
|
+
if (type.includes("ethereum") || type.includes("evm")) {
|
|
220
|
+
return /* @__PURE__ */ jsx(IconWrapper, { children: /* @__PURE__ */ jsx(Icon, { type: "ethereum", size, color: theme.text }) });
|
|
221
|
+
}
|
|
222
|
+
if (type.includes("bitcoin")) {
|
|
223
|
+
return /* @__PURE__ */ jsx(IconWrapper, { children: /* @__PURE__ */ jsx(Icon, { type: "bitcoin", size, color: theme.text }) });
|
|
224
|
+
}
|
|
225
|
+
if (type.includes("sui")) {
|
|
226
|
+
return /* @__PURE__ */ jsx(IconWrapper, { children: /* @__PURE__ */ jsx(Icon, { type: "sui", size, color: theme.text }) });
|
|
227
|
+
}
|
|
228
|
+
return /* @__PURE__ */ jsx(
|
|
229
|
+
"span",
|
|
230
|
+
{
|
|
231
|
+
style: {
|
|
232
|
+
display: "inline-flex",
|
|
233
|
+
alignItems: "center",
|
|
234
|
+
justifyContent: "center",
|
|
235
|
+
borderRadius: "4px",
|
|
236
|
+
backgroundColor: theme.aux,
|
|
237
|
+
color: theme.text,
|
|
238
|
+
fontSize: "6px",
|
|
239
|
+
fontWeight: "bold",
|
|
240
|
+
lineHeight: "1",
|
|
241
|
+
padding: "2px"
|
|
242
|
+
},
|
|
243
|
+
title: addressType,
|
|
244
|
+
children: addressType.charAt(0).toUpperCase()
|
|
245
|
+
}
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// src/components/ConnectModalContent.tsx
|
|
250
|
+
import { Fragment, jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
251
|
+
function ConnectModalContent({
|
|
252
|
+
appIcon,
|
|
253
|
+
appName = "App Name",
|
|
254
|
+
onClose,
|
|
255
|
+
hideCloseButton = false
|
|
256
|
+
}) {
|
|
257
|
+
const theme = useTheme2();
|
|
258
|
+
const { isLoading, allowedProviders } = usePhantom();
|
|
259
|
+
const baseConnect = useConnect();
|
|
260
|
+
const isExtensionInstalled = useIsExtensionInstalled();
|
|
261
|
+
const isPhantomLoginAvailable2 = useIsPhantomLoginAvailable();
|
|
262
|
+
const isMobile = useMemo(() => isMobileDevice(), []);
|
|
263
|
+
const { wallets: discoveredWallets } = useDiscoveredWallets();
|
|
264
|
+
const [isConnecting, setIsConnecting] = useState4(false);
|
|
265
|
+
const [error, setError] = useState4(null);
|
|
266
|
+
const [providerType, setProviderType] = useState4(null);
|
|
267
|
+
const [showOtherWallets, setShowOtherWallets] = useState4(false);
|
|
268
|
+
const [selectedWalletId, setSelectedWalletId] = useState4(null);
|
|
269
|
+
const isConnectingState = baseConnect.isConnecting || isConnecting;
|
|
270
|
+
const errorState = baseConnect.error ? baseConnect.error.message : error;
|
|
271
|
+
const showDivider = !(allowedProviders.length === 1 && allowedProviders.includes("injected"));
|
|
272
|
+
const shouldShowOtherWalletsButton = discoveredWallets.length > 2;
|
|
273
|
+
const walletsToShowInline = shouldShowOtherWalletsButton ? [] : discoveredWallets;
|
|
274
|
+
const connectWithAuthProvider = useCallback3(
|
|
275
|
+
async (provider, walletId) => {
|
|
276
|
+
try {
|
|
277
|
+
setIsConnecting(true);
|
|
278
|
+
setError(null);
|
|
279
|
+
setProviderType(provider);
|
|
280
|
+
setSelectedWalletId(walletId || null);
|
|
281
|
+
await baseConnect.connect({ provider, walletId });
|
|
282
|
+
onClose();
|
|
283
|
+
} catch {
|
|
284
|
+
const wallet = discoveredWallets.find((w) => w.id === walletId);
|
|
285
|
+
const providerName = wallet?.name || getProviderName(provider);
|
|
286
|
+
setError(`Failed to connect to ${providerName}`);
|
|
287
|
+
} finally {
|
|
288
|
+
setIsConnecting(false);
|
|
289
|
+
setProviderType(null);
|
|
290
|
+
setSelectedWalletId(null);
|
|
291
|
+
}
|
|
292
|
+
},
|
|
293
|
+
[baseConnect, discoveredWallets, onClose]
|
|
17
294
|
);
|
|
18
|
-
const
|
|
19
|
-
|
|
295
|
+
const connectWithWallet = useCallback3(
|
|
296
|
+
async (wallet) => {
|
|
297
|
+
await connectWithAuthProvider("injected", wallet.id);
|
|
298
|
+
},
|
|
299
|
+
[connectWithAuthProvider]
|
|
300
|
+
);
|
|
301
|
+
const connectWithDeeplink = useCallback3(async () => {
|
|
302
|
+
try {
|
|
303
|
+
setIsConnecting(true);
|
|
304
|
+
setError(null);
|
|
305
|
+
setProviderType("deeplink");
|
|
306
|
+
await baseConnect.connect({ provider: "deeplink" });
|
|
307
|
+
onClose();
|
|
308
|
+
} catch (error2) {
|
|
309
|
+
const errorMessage = error2 instanceof Error ? error2.message : "Failed to open deeplink";
|
|
310
|
+
setError(errorMessage);
|
|
311
|
+
} finally {
|
|
312
|
+
setIsConnecting(false);
|
|
313
|
+
setProviderType(null);
|
|
314
|
+
}
|
|
315
|
+
}, [baseConnect, onClose]);
|
|
316
|
+
const appIconStyle = {
|
|
317
|
+
width: "56px",
|
|
318
|
+
height: "56px",
|
|
319
|
+
borderRadius: "50%",
|
|
320
|
+
display: "block",
|
|
321
|
+
objectFit: "cover",
|
|
322
|
+
marginBottom: "12px"
|
|
323
|
+
};
|
|
324
|
+
const connectContentContainerStyle = {
|
|
325
|
+
transition: "opacity 0.15s ease-in-out, transform 0.15s ease-in-out",
|
|
326
|
+
display: "flex",
|
|
327
|
+
flexDirection: "column",
|
|
328
|
+
alignItems: "center",
|
|
329
|
+
gap: "12px",
|
|
330
|
+
padding: "0 32px"
|
|
331
|
+
};
|
|
332
|
+
const otherWalletsContainerStyle = {
|
|
333
|
+
display: "flex",
|
|
334
|
+
flexDirection: "column",
|
|
335
|
+
alignItems: "center",
|
|
336
|
+
gap: "12px",
|
|
337
|
+
maxHeight: "480px",
|
|
338
|
+
overflowY: "auto",
|
|
339
|
+
padding: "0 32px 32px 32px",
|
|
340
|
+
transition: "opacity 0.15s ease-in-out, transform 0.15s ease-in-out"
|
|
341
|
+
};
|
|
342
|
+
const dividerStyle = {
|
|
343
|
+
display: "flex",
|
|
344
|
+
alignItems: "center",
|
|
345
|
+
width: "100%",
|
|
346
|
+
margin: "12px 0",
|
|
347
|
+
...theme.typography.caption,
|
|
348
|
+
color: theme.secondary,
|
|
349
|
+
textTransform: "uppercase"
|
|
350
|
+
};
|
|
351
|
+
const dividerLineStyle = {
|
|
352
|
+
flex: 1,
|
|
353
|
+
height: "1px",
|
|
354
|
+
backgroundColor: hexToRgba(theme.secondary, 0.1)
|
|
355
|
+
};
|
|
356
|
+
const dividerTextStyle = {
|
|
357
|
+
padding: "0 12px"
|
|
358
|
+
};
|
|
359
|
+
const errorStyle = {
|
|
360
|
+
backgroundColor: "rgba(220, 53, 69, 0.1)",
|
|
361
|
+
color: "#ff6b6b",
|
|
362
|
+
border: "1px solid rgba(220, 53, 69, 0.3)",
|
|
363
|
+
borderRadius: theme.borderRadius,
|
|
364
|
+
boxSizing: "border-box",
|
|
365
|
+
padding: "12px",
|
|
366
|
+
width: "100%",
|
|
367
|
+
fontSize: "14px"
|
|
368
|
+
};
|
|
369
|
+
const loadingContainerStyle = {
|
|
370
|
+
display: "flex",
|
|
371
|
+
flexDirection: "column",
|
|
372
|
+
alignItems: "center",
|
|
373
|
+
justifyContent: "center",
|
|
374
|
+
padding: "24px",
|
|
375
|
+
gap: "12px"
|
|
376
|
+
};
|
|
377
|
+
const spinnerStyle = {
|
|
378
|
+
width: "40px",
|
|
379
|
+
height: "40px",
|
|
380
|
+
border: `3px solid ${theme.secondary}`,
|
|
381
|
+
borderTop: `3px solid ${theme.brand}`,
|
|
382
|
+
borderRadius: "50%",
|
|
383
|
+
animation: "spin 1s linear infinite"
|
|
384
|
+
};
|
|
385
|
+
const walletIconStyle = {
|
|
386
|
+
width: "32px",
|
|
387
|
+
height: "32px",
|
|
388
|
+
borderRadius: "8px",
|
|
389
|
+
objectFit: "cover"
|
|
390
|
+
};
|
|
391
|
+
const walletButtonContentStyle = {
|
|
392
|
+
display: "flex",
|
|
393
|
+
alignItems: "center",
|
|
394
|
+
justifyContent: "space-between",
|
|
395
|
+
gap: "8px",
|
|
396
|
+
width: "100%"
|
|
397
|
+
};
|
|
398
|
+
const walletButtonLeftStyle = {
|
|
399
|
+
display: "flex",
|
|
400
|
+
alignItems: "center",
|
|
401
|
+
gap: "8px",
|
|
402
|
+
flex: 1
|
|
403
|
+
};
|
|
404
|
+
const walletNameContainerStyle = {
|
|
405
|
+
display: "flex",
|
|
406
|
+
flexDirection: "column",
|
|
407
|
+
gap: "4px",
|
|
408
|
+
alignItems: "flex-start"
|
|
409
|
+
};
|
|
410
|
+
const chainIndicatorsStyle = {
|
|
411
|
+
display: "flex",
|
|
412
|
+
alignItems: "center",
|
|
413
|
+
gap: "4px"
|
|
414
|
+
};
|
|
415
|
+
const walletButtonRightStyle = {
|
|
416
|
+
display: "flex",
|
|
417
|
+
alignItems: "center",
|
|
418
|
+
gap: "8px",
|
|
419
|
+
color: theme.secondary
|
|
420
|
+
};
|
|
421
|
+
const footerStyle = {
|
|
422
|
+
display: "flex",
|
|
423
|
+
padding: "16px",
|
|
424
|
+
justifyContent: "center",
|
|
425
|
+
alignItems: "center",
|
|
426
|
+
gap: "4px",
|
|
427
|
+
borderTop: "1px solid rgba(152, 151, 156, 0.10)",
|
|
428
|
+
...theme.typography.caption,
|
|
429
|
+
color: theme.secondary
|
|
430
|
+
};
|
|
431
|
+
const contentWrapperStyle = {
|
|
432
|
+
display: "flex",
|
|
433
|
+
flexDirection: "column",
|
|
434
|
+
justifyContent: "space-between",
|
|
435
|
+
gap: "24px"
|
|
436
|
+
};
|
|
437
|
+
return /* @__PURE__ */ jsxs("div", { style: contentWrapperStyle, children: [
|
|
438
|
+
/* @__PURE__ */ jsx2("style", { children: `
|
|
439
|
+
@keyframes spin {
|
|
440
|
+
0% { transform: rotate(0deg); }
|
|
441
|
+
100% { transform: rotate(360deg); }
|
|
442
|
+
}
|
|
443
|
+
` }),
|
|
444
|
+
isLoading || baseConnect.isConnecting ? /* @__PURE__ */ jsxs("div", { style: loadingContainerStyle, children: [
|
|
445
|
+
/* @__PURE__ */ jsx2("div", { style: spinnerStyle }),
|
|
446
|
+
/* @__PURE__ */ jsx2(Text, { variant: "label", color: theme.secondary, children: "Loading..." })
|
|
447
|
+
] }) : showOtherWallets ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
448
|
+
/* @__PURE__ */ jsx2(
|
|
449
|
+
ModalHeader,
|
|
450
|
+
{
|
|
451
|
+
goBack: true,
|
|
452
|
+
onGoBack: () => {
|
|
453
|
+
setError(null);
|
|
454
|
+
setShowOtherWallets(false);
|
|
455
|
+
},
|
|
456
|
+
title: "Other Wallets",
|
|
457
|
+
onClose,
|
|
458
|
+
hideCloseButton
|
|
459
|
+
}
|
|
460
|
+
),
|
|
461
|
+
/* @__PURE__ */ jsxs("div", { style: otherWalletsContainerStyle, children: [
|
|
462
|
+
errorState && /* @__PURE__ */ jsx2("div", { style: errorStyle, children: errorState }),
|
|
463
|
+
discoveredWallets.map((wallet) => /* @__PURE__ */ jsx2(
|
|
464
|
+
Button,
|
|
465
|
+
{
|
|
466
|
+
onClick: () => connectWithWallet(wallet),
|
|
467
|
+
disabled: isConnectingState,
|
|
468
|
+
isLoading: isConnectingState && providerType === "injected" && selectedWalletId === wallet.id,
|
|
469
|
+
fullWidth: true,
|
|
470
|
+
children: /* @__PURE__ */ jsxs("span", { style: walletButtonContentStyle, children: [
|
|
471
|
+
/* @__PURE__ */ jsxs("span", { style: walletButtonLeftStyle, children: [
|
|
472
|
+
wallet.id === "phantom" ? /* @__PURE__ */ jsx2(BoundedIcon, { type: "phantom", size: 20, background: "#aba0f2", color: "white" }) : wallet.icon ? /* @__PURE__ */ jsx2("img", { src: wallet.icon, alt: wallet.name, style: walletIconStyle }) : /* @__PURE__ */ jsx2(BoundedIcon, { type: "wallet", size: 20, background: theme.aux, color: theme.text }),
|
|
473
|
+
/* @__PURE__ */ jsx2("span", { style: walletNameContainerStyle, children: /* @__PURE__ */ jsx2(Text, { variant: "captionBold", children: wallet.name }) })
|
|
474
|
+
] }),
|
|
475
|
+
/* @__PURE__ */ jsxs("span", { style: walletButtonRightStyle, children: [
|
|
476
|
+
wallet.addressTypes && wallet.addressTypes.length > 0 && /* @__PURE__ */ jsx2("span", { style: chainIndicatorsStyle, children: wallet.addressTypes.map((addressType) => /* @__PURE__ */ jsx2("span", { children: /* @__PURE__ */ jsx2(ChainIcon, { addressType, size: 8 }) }, `${wallet.id}-chain-${addressType}`)) }),
|
|
477
|
+
/* @__PURE__ */ jsx2(Icon2, { type: "chevron-right", size: 16, color: theme.secondary })
|
|
478
|
+
] })
|
|
479
|
+
] })
|
|
480
|
+
},
|
|
481
|
+
wallet.id
|
|
482
|
+
))
|
|
483
|
+
] })
|
|
484
|
+
] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
485
|
+
/* @__PURE__ */ jsx2(ModalHeader, { title: "Login or Sign Up", onClose, hideCloseButton }),
|
|
486
|
+
/* @__PURE__ */ jsxs("div", { style: connectContentContainerStyle, children: [
|
|
487
|
+
appIcon && /* @__PURE__ */ jsx2("img", { src: appIcon, alt: appName, style: appIconStyle }),
|
|
488
|
+
errorState && /* @__PURE__ */ jsx2("div", { style: errorStyle, children: errorState }),
|
|
489
|
+
isMobile && !isExtensionInstalled.isInstalled && allowedProviders.includes("deeplink") && /* @__PURE__ */ jsx2(
|
|
490
|
+
LoginWithPhantomButton,
|
|
491
|
+
{
|
|
492
|
+
testId: "deeplink-button",
|
|
493
|
+
onClick: connectWithDeeplink,
|
|
494
|
+
disabled: isConnectingState,
|
|
495
|
+
isLoading: isConnectingState && providerType === "deeplink",
|
|
496
|
+
fullWidth: true,
|
|
497
|
+
children: isConnecting && providerType === "deeplink" ? "Opening Phantom..." : "Open in Phantom App"
|
|
498
|
+
}
|
|
499
|
+
),
|
|
500
|
+
!isMobile && allowedProviders.includes("phantom") && isPhantomLoginAvailable2.isAvailable && /* @__PURE__ */ jsx2(
|
|
501
|
+
LoginWithPhantomButton,
|
|
502
|
+
{
|
|
503
|
+
testId: "login-with-phantom-button",
|
|
504
|
+
onClick: () => connectWithAuthProvider("phantom"),
|
|
505
|
+
disabled: isConnectingState,
|
|
506
|
+
isLoading: isConnectingState && providerType === "phantom"
|
|
507
|
+
}
|
|
508
|
+
),
|
|
509
|
+
allowedProviders.includes("google") && !(isMobile && isExtensionInstalled.isInstalled) && /* @__PURE__ */ jsx2(
|
|
510
|
+
Button,
|
|
511
|
+
{
|
|
512
|
+
onClick: () => connectWithAuthProvider("google"),
|
|
513
|
+
disabled: isConnectingState,
|
|
514
|
+
isLoading: isConnectingState && providerType === "google",
|
|
515
|
+
fullWidth: true,
|
|
516
|
+
children: /* @__PURE__ */ jsxs("span", { style: walletButtonContentStyle, children: [
|
|
517
|
+
/* @__PURE__ */ jsxs("span", { style: walletButtonLeftStyle, children: [
|
|
518
|
+
/* @__PURE__ */ jsx2(Icon2, { type: "google", size: 20 }),
|
|
519
|
+
/* @__PURE__ */ jsx2(Text, { variant: "captionBold", children: "Continue with Google" })
|
|
520
|
+
] }),
|
|
521
|
+
/* @__PURE__ */ jsx2("span", { style: walletButtonRightStyle, children: /* @__PURE__ */ jsx2(Icon2, { type: "chevron-right", size: 16, color: theme.secondary }) })
|
|
522
|
+
] })
|
|
523
|
+
}
|
|
524
|
+
),
|
|
525
|
+
allowedProviders.includes("apple") && /* @__PURE__ */ jsx2(
|
|
526
|
+
Button,
|
|
527
|
+
{
|
|
528
|
+
onClick: () => connectWithAuthProvider("apple"),
|
|
529
|
+
disabled: isConnectingState,
|
|
530
|
+
isLoading: isConnectingState && providerType === "apple",
|
|
531
|
+
fullWidth: true,
|
|
532
|
+
children: /* @__PURE__ */ jsxs("span", { style: walletButtonContentStyle, children: [
|
|
533
|
+
/* @__PURE__ */ jsxs("span", { style: walletButtonLeftStyle, children: [
|
|
534
|
+
/* @__PURE__ */ jsx2(Icon2, { type: "apple", size: 20 }),
|
|
535
|
+
/* @__PURE__ */ jsx2(Text, { variant: "captionBold", children: "Continue with Apple" })
|
|
536
|
+
] }),
|
|
537
|
+
/* @__PURE__ */ jsx2("span", { style: walletButtonRightStyle, children: /* @__PURE__ */ jsx2(Icon2, { type: "chevron-right", size: 16, color: theme.secondary }) })
|
|
538
|
+
] })
|
|
539
|
+
}
|
|
540
|
+
),
|
|
541
|
+
allowedProviders.includes("injected") && (isExtensionInstalled.isInstalled || discoveredWallets.length > 0) && (!isMobile || isExtensionInstalled.isInstalled) && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
542
|
+
showDivider && /* @__PURE__ */ jsxs("div", { style: dividerStyle, children: [
|
|
543
|
+
/* @__PURE__ */ jsx2("div", { style: dividerLineStyle }),
|
|
544
|
+
/* @__PURE__ */ jsx2("span", { style: dividerTextStyle, children: "OR" }),
|
|
545
|
+
/* @__PURE__ */ jsx2("div", { style: dividerLineStyle })
|
|
546
|
+
] }),
|
|
547
|
+
walletsToShowInline.map((wallet) => /* @__PURE__ */ jsx2(
|
|
548
|
+
Button,
|
|
549
|
+
{
|
|
550
|
+
onClick: () => connectWithWallet(wallet),
|
|
551
|
+
disabled: isConnectingState,
|
|
552
|
+
isLoading: isConnectingState && providerType === "injected" && selectedWalletId === wallet.id,
|
|
553
|
+
fullWidth: true,
|
|
554
|
+
children: /* @__PURE__ */ jsxs("span", { style: walletButtonContentStyle, children: [
|
|
555
|
+
/* @__PURE__ */ jsxs("span", { style: walletButtonLeftStyle, children: [
|
|
556
|
+
wallet.id === "phantom" ? /* @__PURE__ */ jsx2(BoundedIcon, { type: "phantom", size: 20, background: "#aba0f2", color: "white" }) : wallet.icon ? /* @__PURE__ */ jsx2("img", { src: wallet.icon, alt: wallet.name, style: walletIconStyle }) : /* @__PURE__ */ jsx2(BoundedIcon, { type: "wallet", size: 10, background: theme.aux, color: theme.text }),
|
|
557
|
+
/* @__PURE__ */ jsx2("span", { style: walletNameContainerStyle, children: /* @__PURE__ */ jsx2(Text, { variant: "captionBold", children: wallet.name }) })
|
|
558
|
+
] }),
|
|
559
|
+
/* @__PURE__ */ jsxs("span", { style: walletButtonRightStyle, children: [
|
|
560
|
+
wallet.addressTypes && wallet.addressTypes.length > 0 && /* @__PURE__ */ jsx2("span", { style: chainIndicatorsStyle, children: wallet.addressTypes.map((addressType) => /* @__PURE__ */ jsx2("span", { children: /* @__PURE__ */ jsx2(ChainIcon, { addressType, size: 8 }) }, `${wallet.id}-chain-${addressType}`)) }),
|
|
561
|
+
/* @__PURE__ */ jsx2(Icon2, { type: "chevron-right", size: 16, color: theme.secondary })
|
|
562
|
+
] })
|
|
563
|
+
] })
|
|
564
|
+
},
|
|
565
|
+
wallet.id
|
|
566
|
+
)),
|
|
567
|
+
shouldShowOtherWalletsButton && /* @__PURE__ */ jsx2(Button, { onClick: () => setShowOtherWallets(true), disabled: isConnectingState, fullWidth: true, children: /* @__PURE__ */ jsxs("span", { style: walletButtonContentStyle, children: [
|
|
568
|
+
/* @__PURE__ */ jsxs("span", { style: walletButtonLeftStyle, children: [
|
|
569
|
+
/* @__PURE__ */ jsx2(BoundedIcon, { type: "wallet", size: 20, background: theme.aux, color: theme.text }),
|
|
570
|
+
/* @__PURE__ */ jsx2(Text, { variant: "captionBold", children: "Other Wallets" })
|
|
571
|
+
] }),
|
|
572
|
+
/* @__PURE__ */ jsx2("span", { style: walletButtonRightStyle, children: /* @__PURE__ */ jsx2(Icon2, { type: "chevron-right", size: 16, color: theme.secondary }) })
|
|
573
|
+
] }) })
|
|
574
|
+
] })
|
|
575
|
+
] }),
|
|
576
|
+
/* @__PURE__ */ jsxs("div", { style: footerStyle, children: [
|
|
577
|
+
/* @__PURE__ */ jsx2(Text, { variant: "label", color: theme.secondary, children: "Powered by" }),
|
|
578
|
+
/* @__PURE__ */ jsx2(Icon2, { type: "phantom", size: 16 }),
|
|
579
|
+
/* @__PURE__ */ jsx2(Text, { variant: "label", color: theme.secondary, children: "Phantom" })
|
|
580
|
+
] })
|
|
581
|
+
] })
|
|
582
|
+
] });
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// src/components/ConnectedModalContent.tsx
|
|
586
|
+
import { useState as useState6, useEffect as useEffect4 } from "react";
|
|
587
|
+
import { Button as Button2, Text as Text2, useTheme as useTheme3, ModalHeader as ModalHeader2 } from "@phantom/wallet-sdk-ui";
|
|
588
|
+
|
|
589
|
+
// src/hooks/useDisconnect.ts
|
|
590
|
+
import { useCallback as useCallback4, useState as useState5 } from "react";
|
|
591
|
+
function useDisconnect() {
|
|
592
|
+
const { sdk } = usePhantom();
|
|
593
|
+
const [isDisconnecting, setIsDisconnecting] = useState5(false);
|
|
594
|
+
const [error, setError] = useState5(null);
|
|
595
|
+
const disconnect = useCallback4(async () => {
|
|
596
|
+
if (!sdk) {
|
|
597
|
+
throw new Error("SDK not initialized");
|
|
598
|
+
}
|
|
599
|
+
setIsDisconnecting(true);
|
|
600
|
+
setError(null);
|
|
601
|
+
try {
|
|
602
|
+
await sdk.disconnect();
|
|
603
|
+
} catch (err) {
|
|
604
|
+
const error2 = err instanceof Error ? err : new Error(String(err));
|
|
605
|
+
setError(error2);
|
|
606
|
+
throw err;
|
|
607
|
+
} finally {
|
|
608
|
+
setIsDisconnecting(false);
|
|
609
|
+
}
|
|
610
|
+
}, [sdk]);
|
|
611
|
+
return {
|
|
612
|
+
disconnect,
|
|
613
|
+
isDisconnecting,
|
|
614
|
+
error
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// src/components/ConnectedModalContent.tsx
|
|
619
|
+
import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
620
|
+
function ConnectedModalContent({ onClose, hideCloseButton = false }) {
|
|
621
|
+
const theme = useTheme3();
|
|
622
|
+
const { addresses } = usePhantom();
|
|
623
|
+
const { disconnect } = useDisconnect();
|
|
624
|
+
const [isDisconnecting, setIsDisconnecting] = useState6(false);
|
|
625
|
+
const [disconnectError, setDisconnectError] = useState6(null);
|
|
626
|
+
useEffect4(() => {
|
|
627
|
+
setDisconnectError(null);
|
|
628
|
+
}, []);
|
|
629
|
+
const handleDisconnect = async () => {
|
|
630
|
+
try {
|
|
631
|
+
setIsDisconnecting(true);
|
|
632
|
+
setDisconnectError(null);
|
|
633
|
+
await disconnect();
|
|
634
|
+
onClose();
|
|
635
|
+
} catch (err) {
|
|
636
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
637
|
+
setDisconnectError(error);
|
|
638
|
+
} finally {
|
|
639
|
+
setIsDisconnecting(false);
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
const accountListStyle = {
|
|
643
|
+
display: "flex",
|
|
644
|
+
flexDirection: "column",
|
|
645
|
+
gap: "16px",
|
|
646
|
+
width: "100%",
|
|
647
|
+
minWidth: 0,
|
|
648
|
+
boxSizing: "border-box"
|
|
649
|
+
};
|
|
650
|
+
const accountItemStyle = {
|
|
651
|
+
display: "flex",
|
|
652
|
+
flexDirection: "column",
|
|
653
|
+
gap: "8px",
|
|
654
|
+
width: "100%",
|
|
655
|
+
minWidth: 0,
|
|
656
|
+
boxSizing: "border-box"
|
|
657
|
+
};
|
|
658
|
+
const addressTextStyle = {
|
|
659
|
+
fontFamily: "monospace",
|
|
660
|
+
wordBreak: "break-all",
|
|
661
|
+
overflowWrap: "break-word",
|
|
662
|
+
minWidth: 0
|
|
663
|
+
};
|
|
664
|
+
const errorContainerStyle = {
|
|
665
|
+
padding: "12px",
|
|
666
|
+
backgroundColor: "rgba(220, 53, 69, 0.1)",
|
|
667
|
+
borderRadius: theme.borderRadius,
|
|
668
|
+
border: "1px solid rgba(220, 53, 69, 0.3)",
|
|
669
|
+
width: "100%",
|
|
670
|
+
boxSizing: "border-box",
|
|
671
|
+
minWidth: 0
|
|
672
|
+
};
|
|
673
|
+
const contentWrapperStyle = {
|
|
674
|
+
display: "flex",
|
|
675
|
+
flexDirection: "column",
|
|
676
|
+
gap: "24px"
|
|
677
|
+
};
|
|
678
|
+
const accountListContainerStyle = {
|
|
679
|
+
display: "flex",
|
|
680
|
+
flexDirection: "column",
|
|
681
|
+
alignItems: "center",
|
|
682
|
+
gap: "12px",
|
|
683
|
+
padding: "0 32px 24px 32px",
|
|
684
|
+
boxSizing: "border-box",
|
|
685
|
+
width: "100%",
|
|
686
|
+
minWidth: 0
|
|
687
|
+
};
|
|
688
|
+
const disconnectButtonContainerStyle = {
|
|
689
|
+
display: "flex",
|
|
690
|
+
flexDirection: "column",
|
|
691
|
+
alignItems: "center",
|
|
692
|
+
gap: "12px",
|
|
693
|
+
padding: "0 32px 24px 32px",
|
|
694
|
+
boxSizing: "border-box",
|
|
695
|
+
width: "100%",
|
|
696
|
+
minWidth: 0
|
|
697
|
+
};
|
|
698
|
+
return /* @__PURE__ */ jsxs2("div", { style: contentWrapperStyle, children: [
|
|
699
|
+
/* @__PURE__ */ jsx3(ModalHeader2, { title: "Wallet", onClose, hideCloseButton }),
|
|
700
|
+
/* @__PURE__ */ jsxs2("div", { style: accountListContainerStyle, children: [
|
|
701
|
+
disconnectError && /* @__PURE__ */ jsx3("div", { style: errorContainerStyle, children: /* @__PURE__ */ jsx3(Text2, { variant: "caption", color: theme.error, children: "Failed to disconnect" }) }),
|
|
702
|
+
addresses && addresses.length > 0 && /* @__PURE__ */ jsx3("div", { style: accountListStyle, children: addresses.map((account, index) => /* @__PURE__ */ jsxs2("div", { style: accountItemStyle, children: [
|
|
703
|
+
/* @__PURE__ */ jsx3(Text2, { variant: "label", color: theme.secondary, style: { textTransform: "uppercase" }, children: account.addressType }),
|
|
704
|
+
/* @__PURE__ */ jsx3("div", { style: addressTextStyle, children: /* @__PURE__ */ jsx3(Text2, { variant: "caption", children: account.address }) })
|
|
705
|
+
] }, index)) })
|
|
706
|
+
] }),
|
|
707
|
+
/* @__PURE__ */ jsx3("div", { style: disconnectButtonContainerStyle, children: /* @__PURE__ */ jsx3(Button2, { onClick: handleDisconnect, disabled: isDisconnecting, isLoading: isDisconnecting, fullWidth: true, children: /* @__PURE__ */ jsx3(Text2, { variant: "captionBold", children: isDisconnecting ? "Disconnecting..." : "Disconnect" }) }) })
|
|
708
|
+
] });
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
// src/components/SpendingLimitModalContent.tsx
|
|
712
|
+
import { Text as Text3, Button as Button3, useTheme as useTheme4 } from "@phantom/wallet-sdk-ui";
|
|
713
|
+
import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
714
|
+
function SpendingLimitModalContent({ onClose }) {
|
|
715
|
+
const theme = useTheme4();
|
|
716
|
+
return /* @__PURE__ */ jsxs3("div", { style: { display: "flex", flexDirection: "column", gap: 16, padding: 32 }, children: [
|
|
717
|
+
/* @__PURE__ */ jsx4(Text3, { variant: "caption", color: theme.secondary, children: "You've reached the maximum daily limit allowed to spend by this application." }),
|
|
718
|
+
/* @__PURE__ */ jsx4(Button3, { fullWidth: true, onClick: onClose, children: /* @__PURE__ */ jsx4(Text3, { variant: "captionBold", children: "Close" }) })
|
|
719
|
+
] });
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// src/ModalProvider.tsx
|
|
723
|
+
import { jsx as jsx5, jsxs as jsxs4 } from "react/jsx-runtime";
|
|
724
|
+
function ModalProvider({ children, appIcon, appName }) {
|
|
725
|
+
const { isConnected, errors, clearError } = usePhantom();
|
|
726
|
+
const [isModalOpen, setIsModalOpen] = useState7(false);
|
|
727
|
+
const isMobile = useMemo2(() => isMobileDevice2(), []);
|
|
728
|
+
const openModal = useCallback5(() => {
|
|
729
|
+
setIsModalOpen(true);
|
|
730
|
+
}, []);
|
|
731
|
+
const closeModal = useCallback5(() => {
|
|
732
|
+
setIsModalOpen(false);
|
|
733
|
+
clearError("spendingLimit");
|
|
734
|
+
}, [clearError]);
|
|
735
|
+
const isSpendingLimitOpen = !!errors.spendingLimit;
|
|
736
|
+
const modalContextValue = useMemo2(
|
|
737
|
+
() => ({
|
|
738
|
+
isModalOpen,
|
|
739
|
+
openModal,
|
|
740
|
+
closeModal
|
|
741
|
+
}),
|
|
742
|
+
[isModalOpen, openModal, closeModal]
|
|
743
|
+
);
|
|
744
|
+
return /* @__PURE__ */ jsxs4(ModalContext.Provider, { value: modalContextValue, children: [
|
|
745
|
+
children,
|
|
746
|
+
/* @__PURE__ */ jsx5(
|
|
747
|
+
Modal,
|
|
748
|
+
{
|
|
749
|
+
isVisible: isModalOpen || isSpendingLimitOpen,
|
|
750
|
+
onClose: closeModal,
|
|
751
|
+
appIcon,
|
|
752
|
+
appName,
|
|
753
|
+
isMobile,
|
|
754
|
+
children: isSpendingLimitOpen ? /* @__PURE__ */ jsx5(SpendingLimitModalContent, { onClose: closeModal }) : isConnected ? /* @__PURE__ */ jsx5(ConnectedModalContent, { onClose: closeModal }) : /* @__PURE__ */ jsx5(ConnectModalContent, { appIcon, appName, onClose: closeModal })
|
|
755
|
+
}
|
|
756
|
+
)
|
|
757
|
+
] });
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
// src/PhantomProvider.tsx
|
|
761
|
+
import { jsx as jsx6 } from "react/jsx-runtime";
|
|
762
|
+
function PhantomProvider({ children, config, debugConfig, theme, appIcon, appName }) {
|
|
763
|
+
const memoizedConfig = useMemo3(() => config, [config]);
|
|
764
|
+
const resolvedTheme = useMemo3(() => mergeTheme(theme || darkTheme), [theme]);
|
|
765
|
+
const [sdk, setSdk] = useState8(null);
|
|
766
|
+
const [isClient, setIsClient] = useState8(false);
|
|
767
|
+
const [isConnected, setIsConnected] = useState8(false);
|
|
768
|
+
const [isConnecting, setIsConnecting] = useState8(false);
|
|
769
|
+
const [isLoading, setIsLoading] = useState8(true);
|
|
770
|
+
const [errors, setErrors] = useState8({});
|
|
771
|
+
const [addresses, setAddresses] = useState8([]);
|
|
772
|
+
const [user, setUser] = useState8(null);
|
|
773
|
+
useEffect5(() => {
|
|
20
774
|
setIsClient(true);
|
|
21
775
|
}, []);
|
|
22
|
-
|
|
776
|
+
useEffect5(() => {
|
|
23
777
|
if (!isClient)
|
|
24
778
|
return;
|
|
25
779
|
const sdkInstance = new BrowserSDK(memoizedConfig);
|
|
26
780
|
setSdk(sdkInstance);
|
|
27
781
|
}, [isClient, memoizedConfig]);
|
|
28
|
-
|
|
782
|
+
useEffect5(() => {
|
|
29
783
|
if (!sdk)
|
|
30
784
|
return;
|
|
31
785
|
const handleConnectStart = () => {
|
|
32
786
|
setIsConnecting(true);
|
|
33
|
-
|
|
787
|
+
setErrors((prev) => ({ ...prev, connect: void 0 }));
|
|
34
788
|
};
|
|
35
|
-
const handleConnect = async () => {
|
|
789
|
+
const handleConnect = async (data) => {
|
|
36
790
|
try {
|
|
37
791
|
setIsConnected(true);
|
|
38
792
|
setIsConnecting(false);
|
|
39
|
-
|
|
40
|
-
setCurrentProviderType(providerInfo?.type || null);
|
|
793
|
+
setUser(data);
|
|
41
794
|
const addrs = await sdk.getAddresses();
|
|
42
795
|
setAddresses(addrs);
|
|
43
|
-
setWalletId(sdk.getWalletId());
|
|
44
796
|
} catch (err) {
|
|
45
797
|
console.error("Error connecting:", err);
|
|
46
798
|
try {
|
|
@@ -53,127 +805,91 @@ function PhantomProvider({ children, config, debugConfig }) {
|
|
|
53
805
|
const handleConnectError = (errorData) => {
|
|
54
806
|
setIsConnecting(false);
|
|
55
807
|
setIsConnected(false);
|
|
56
|
-
|
|
808
|
+
const isAutoConnectNoSession = errorData.source === "auto-connect" && (errorData.error === "No valid session found" || errorData.error === "No trusted connections available");
|
|
809
|
+
if (isAutoConnectNoSession) {
|
|
810
|
+
setErrors((prev) => ({ ...prev, connect: void 0 }));
|
|
811
|
+
} else {
|
|
812
|
+
setErrors((prev) => ({ ...prev, connect: new Error(errorData.error || "Connection failed") }));
|
|
813
|
+
}
|
|
814
|
+
setAddresses([]);
|
|
57
815
|
};
|
|
58
816
|
const handleDisconnect = () => {
|
|
59
817
|
setIsConnected(false);
|
|
60
818
|
setIsConnecting(false);
|
|
61
|
-
|
|
819
|
+
setErrors({});
|
|
62
820
|
setAddresses([]);
|
|
63
|
-
|
|
821
|
+
setUser(null);
|
|
822
|
+
};
|
|
823
|
+
const handleSpendingLimitReached = () => {
|
|
824
|
+
setErrors((prev) => ({ ...prev, spendingLimit: true }));
|
|
64
825
|
};
|
|
65
826
|
sdk.on("connect_start", handleConnectStart);
|
|
66
827
|
sdk.on("connect", handleConnect);
|
|
67
828
|
sdk.on("connect_error", handleConnectError);
|
|
68
829
|
sdk.on("disconnect", handleDisconnect);
|
|
830
|
+
sdk.on("spending_limit_reached", handleSpendingLimitReached);
|
|
69
831
|
return () => {
|
|
70
832
|
sdk.off("connect_start", handleConnectStart);
|
|
71
833
|
sdk.off("connect", handleConnect);
|
|
72
834
|
sdk.off("connect_error", handleConnectError);
|
|
73
835
|
sdk.off("disconnect", handleDisconnect);
|
|
836
|
+
sdk.off("spending_limit_reached", handleSpendingLimitReached);
|
|
74
837
|
};
|
|
75
838
|
}, [sdk]);
|
|
76
|
-
|
|
839
|
+
useEffect5(() => {
|
|
77
840
|
if (!debugConfig || !sdk)
|
|
78
841
|
return;
|
|
79
842
|
sdk.configureDebug(debugConfig);
|
|
80
843
|
}, [sdk, debugConfig]);
|
|
81
|
-
|
|
844
|
+
useEffect5(() => {
|
|
82
845
|
if (!isClient || !sdk)
|
|
83
846
|
return;
|
|
84
847
|
const initialize = async () => {
|
|
85
848
|
try {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
console.error("Error checking Phantom extension:", err);
|
|
90
|
-
setIsPhantomAvailable(false);
|
|
91
|
-
}
|
|
92
|
-
if (memoizedConfig.autoConnect !== false) {
|
|
93
|
-
sdk.autoConnect().catch(() => {
|
|
94
|
-
});
|
|
849
|
+
await sdk.autoConnect();
|
|
850
|
+
} catch (error) {
|
|
851
|
+
console.error("Auto-connect error:", error);
|
|
95
852
|
}
|
|
853
|
+
setIsLoading(false);
|
|
96
854
|
};
|
|
97
855
|
initialize();
|
|
98
|
-
}, [sdk,
|
|
99
|
-
const
|
|
856
|
+
}, [sdk, isClient]);
|
|
857
|
+
const clearError = useCallback6((key) => {
|
|
858
|
+
setErrors((prev) => {
|
|
859
|
+
const next = { ...prev };
|
|
860
|
+
delete next[key];
|
|
861
|
+
return next;
|
|
862
|
+
});
|
|
863
|
+
}, []);
|
|
864
|
+
const value = useMemo3(
|
|
100
865
|
() => ({
|
|
101
866
|
sdk,
|
|
102
867
|
isConnected,
|
|
103
868
|
isConnecting,
|
|
104
|
-
|
|
869
|
+
isLoading,
|
|
870
|
+
errors,
|
|
105
871
|
addresses,
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
872
|
+
isClient,
|
|
873
|
+
user,
|
|
874
|
+
theme: resolvedTheme,
|
|
875
|
+
allowedProviders: memoizedConfig.providers,
|
|
876
|
+
clearError
|
|
110
877
|
}),
|
|
111
|
-
[
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
import { useCallback } from "react";
|
|
125
|
-
function useConnect() {
|
|
126
|
-
const { sdk, isConnecting, connectError, currentProviderType, isPhantomAvailable } = usePhantom();
|
|
127
|
-
const connect = useCallback(
|
|
128
|
-
async (options) => {
|
|
129
|
-
if (!sdk) {
|
|
130
|
-
throw new Error("SDK not initialized");
|
|
131
|
-
}
|
|
132
|
-
try {
|
|
133
|
-
const result = await sdk.connect(options);
|
|
134
|
-
return result;
|
|
135
|
-
} catch (err) {
|
|
136
|
-
console.error("Error connecting to Phantom:", err);
|
|
137
|
-
throw err;
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
[sdk]
|
|
878
|
+
[
|
|
879
|
+
sdk,
|
|
880
|
+
isConnected,
|
|
881
|
+
isConnecting,
|
|
882
|
+
isLoading,
|
|
883
|
+
errors,
|
|
884
|
+
addresses,
|
|
885
|
+
isClient,
|
|
886
|
+
user,
|
|
887
|
+
resolvedTheme,
|
|
888
|
+
memoizedConfig.providers,
|
|
889
|
+
clearError
|
|
890
|
+
]
|
|
141
891
|
);
|
|
142
|
-
return {
|
|
143
|
-
connect,
|
|
144
|
-
isConnecting,
|
|
145
|
-
error: connectError,
|
|
146
|
-
currentProviderType,
|
|
147
|
-
isPhantomAvailable
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// src/hooks/useDisconnect.ts
|
|
152
|
-
import { useCallback as useCallback2, useState as useState2 } from "react";
|
|
153
|
-
function useDisconnect() {
|
|
154
|
-
const { sdk } = usePhantom();
|
|
155
|
-
const [isDisconnecting, setIsDisconnecting] = useState2(false);
|
|
156
|
-
const [error, setError] = useState2(null);
|
|
157
|
-
const disconnect = useCallback2(async () => {
|
|
158
|
-
if (!sdk) {
|
|
159
|
-
throw new Error("SDK not initialized");
|
|
160
|
-
}
|
|
161
|
-
setIsDisconnecting(true);
|
|
162
|
-
setError(null);
|
|
163
|
-
try {
|
|
164
|
-
await sdk.disconnect();
|
|
165
|
-
} catch (err) {
|
|
166
|
-
setError(err);
|
|
167
|
-
throw err;
|
|
168
|
-
} finally {
|
|
169
|
-
setIsDisconnecting(false);
|
|
170
|
-
}
|
|
171
|
-
}, [sdk]);
|
|
172
|
-
return {
|
|
173
|
-
disconnect,
|
|
174
|
-
isDisconnecting,
|
|
175
|
-
error
|
|
176
|
-
};
|
|
892
|
+
return /* @__PURE__ */ jsx6(ThemeProvider, { theme: resolvedTheme, children: /* @__PURE__ */ jsx6(PhantomContext.Provider, { value, children: /* @__PURE__ */ jsx6(ModalProvider, { appIcon, appName, children }) }) });
|
|
177
893
|
}
|
|
178
894
|
|
|
179
895
|
// src/hooks/useAccounts.ts
|
|
@@ -182,49 +898,16 @@ function useAccounts() {
|
|
|
182
898
|
return isConnected ? addresses : null;
|
|
183
899
|
}
|
|
184
900
|
|
|
185
|
-
// src/hooks/useIsExtensionInstalled.ts
|
|
186
|
-
import * as React from "react";
|
|
187
|
-
import { waitForPhantomExtension } from "@phantom/browser-sdk";
|
|
188
|
-
function useIsExtensionInstalled() {
|
|
189
|
-
const [isLoading, setIsLoading] = React.useState(true);
|
|
190
|
-
const [isInstalled, setIsInstalled] = React.useState(false);
|
|
191
|
-
React.useEffect(() => {
|
|
192
|
-
let isMounted = true;
|
|
193
|
-
const checkExtension = async () => {
|
|
194
|
-
try {
|
|
195
|
-
setIsLoading(true);
|
|
196
|
-
const result = await waitForPhantomExtension(3e3);
|
|
197
|
-
if (isMounted) {
|
|
198
|
-
setIsInstalled(result);
|
|
199
|
-
}
|
|
200
|
-
} catch (error) {
|
|
201
|
-
if (isMounted) {
|
|
202
|
-
setIsInstalled(false);
|
|
203
|
-
}
|
|
204
|
-
} finally {
|
|
205
|
-
if (isMounted) {
|
|
206
|
-
setIsLoading(false);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
};
|
|
210
|
-
checkExtension();
|
|
211
|
-
return () => {
|
|
212
|
-
isMounted = false;
|
|
213
|
-
};
|
|
214
|
-
}, []);
|
|
215
|
-
return { isLoading, isInstalled };
|
|
216
|
-
}
|
|
217
|
-
|
|
218
901
|
// src/hooks/useAutoConfirm.ts
|
|
219
|
-
import { useCallback as
|
|
902
|
+
import { useCallback as useCallback7, useState as useState9, useEffect as useEffect6 } from "react";
|
|
220
903
|
function useAutoConfirm() {
|
|
221
|
-
const { sdk,
|
|
222
|
-
const [status, setStatus] =
|
|
223
|
-
const [supportedChains, setSupportedChains] =
|
|
224
|
-
const [isLoading, setIsLoading] =
|
|
225
|
-
const [error, setError] =
|
|
226
|
-
const isInjected =
|
|
227
|
-
const enable =
|
|
904
|
+
const { sdk, user } = usePhantom();
|
|
905
|
+
const [status, setStatus] = useState9(null);
|
|
906
|
+
const [supportedChains, setSupportedChains] = useState9(null);
|
|
907
|
+
const [isLoading, setIsLoading] = useState9(false);
|
|
908
|
+
const [error, setError] = useState9(null);
|
|
909
|
+
const isInjected = user?.authProvider === "injected";
|
|
910
|
+
const enable = useCallback7(
|
|
228
911
|
async (params) => {
|
|
229
912
|
if (!sdk) {
|
|
230
913
|
throw new Error("SDK not initialized");
|
|
@@ -248,7 +931,7 @@ function useAutoConfirm() {
|
|
|
248
931
|
},
|
|
249
932
|
[sdk, isInjected]
|
|
250
933
|
);
|
|
251
|
-
const disable =
|
|
934
|
+
const disable = useCallback7(async () => {
|
|
252
935
|
if (!sdk) {
|
|
253
936
|
throw new Error("SDK not initialized");
|
|
254
937
|
}
|
|
@@ -269,7 +952,7 @@ function useAutoConfirm() {
|
|
|
269
952
|
setIsLoading(false);
|
|
270
953
|
}
|
|
271
954
|
}, [sdk, isInjected]);
|
|
272
|
-
const refetch =
|
|
955
|
+
const refetch = useCallback7(async () => {
|
|
273
956
|
if (!sdk || !isInjected) {
|
|
274
957
|
return;
|
|
275
958
|
}
|
|
@@ -289,7 +972,7 @@ function useAutoConfirm() {
|
|
|
289
972
|
setIsLoading(false);
|
|
290
973
|
}
|
|
291
974
|
}, [sdk, isInjected]);
|
|
292
|
-
|
|
975
|
+
useEffect6(() => {
|
|
293
976
|
if (sdk && isInjected) {
|
|
294
977
|
refetch();
|
|
295
978
|
} else {
|
|
@@ -310,55 +993,175 @@ function useAutoConfirm() {
|
|
|
310
993
|
}
|
|
311
994
|
|
|
312
995
|
// src/hooks/useSolana.ts
|
|
996
|
+
import { AddressType } from "@phantom/browser-sdk";
|
|
313
997
|
function useSolana() {
|
|
314
|
-
const { sdk,
|
|
315
|
-
if (!isClient || !sdk) {
|
|
998
|
+
const { sdk, isClient, isLoading } = usePhantom();
|
|
999
|
+
if (!isClient || !sdk || isLoading) {
|
|
1000
|
+
return {
|
|
1001
|
+
solana: {},
|
|
1002
|
+
isAvailable: false
|
|
1003
|
+
};
|
|
1004
|
+
}
|
|
1005
|
+
const enabledAddressTypes = sdk.getEnabledAddressTypes();
|
|
1006
|
+
const isAvailable = enabledAddressTypes.includes(AddressType.solana);
|
|
1007
|
+
if (!isAvailable) {
|
|
1008
|
+
return {
|
|
1009
|
+
solana: {},
|
|
1010
|
+
isAvailable: false
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
try {
|
|
1014
|
+
return {
|
|
1015
|
+
solana: sdk.solana,
|
|
1016
|
+
isAvailable: true
|
|
1017
|
+
};
|
|
1018
|
+
} catch (error) {
|
|
316
1019
|
return {
|
|
317
1020
|
solana: {},
|
|
318
|
-
// This will be replaced when SDK is ready
|
|
319
1021
|
isAvailable: false
|
|
320
1022
|
};
|
|
321
1023
|
}
|
|
322
|
-
return {
|
|
323
|
-
// Chain instance with connection enforcement for signing methods
|
|
324
|
-
solana: sdk.solana,
|
|
325
|
-
// State
|
|
326
|
-
isAvailable: !!isConnected
|
|
327
|
-
};
|
|
328
1024
|
}
|
|
329
1025
|
|
|
330
1026
|
// src/hooks/useEthereum.ts
|
|
1027
|
+
import { AddressType as AddressType2 } from "@phantom/browser-sdk";
|
|
331
1028
|
function useEthereum() {
|
|
332
|
-
const { sdk,
|
|
333
|
-
if (!isClient || !sdk) {
|
|
1029
|
+
const { sdk, isClient, isLoading } = usePhantom();
|
|
1030
|
+
if (!isClient || !sdk || isLoading) {
|
|
334
1031
|
return {
|
|
335
1032
|
ethereum: {},
|
|
336
|
-
// This will be replaced when SDK is ready
|
|
337
1033
|
isAvailable: false
|
|
338
1034
|
};
|
|
339
1035
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
1036
|
+
const enabledAddressTypes = sdk.getEnabledAddressTypes();
|
|
1037
|
+
const isAvailable = enabledAddressTypes.includes(AddressType2.ethereum);
|
|
1038
|
+
if (!isAvailable) {
|
|
1039
|
+
return {
|
|
1040
|
+
ethereum: {},
|
|
1041
|
+
isAvailable: false
|
|
1042
|
+
};
|
|
1043
|
+
}
|
|
1044
|
+
try {
|
|
1045
|
+
return {
|
|
1046
|
+
ethereum: sdk.ethereum,
|
|
1047
|
+
isAvailable: true
|
|
1048
|
+
};
|
|
1049
|
+
} catch (error) {
|
|
1050
|
+
return {
|
|
1051
|
+
ethereum: {},
|
|
1052
|
+
isAvailable: false
|
|
1053
|
+
};
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
// src/hooks/index.ts
|
|
1058
|
+
import { useTheme as useTheme5 } from "@phantom/wallet-sdk-ui";
|
|
1059
|
+
|
|
1060
|
+
// src/components/ConnectButton.tsx
|
|
1061
|
+
import { useMemo as useMemo4 } from "react";
|
|
1062
|
+
import { useTheme as useTheme6 } from "@phantom/wallet-sdk-ui";
|
|
1063
|
+
import { jsx as jsx7 } from "react/jsx-runtime";
|
|
1064
|
+
function ConnectButton({ addressType, fullWidth = false }) {
|
|
1065
|
+
const theme = useTheme6();
|
|
1066
|
+
const { open } = useModal();
|
|
1067
|
+
const { isConnected, addresses } = usePhantom();
|
|
1068
|
+
const displayAddress = useMemo4(() => {
|
|
1069
|
+
if (!addresses || addresses.length === 0)
|
|
1070
|
+
return null;
|
|
1071
|
+
if (addressType) {
|
|
1072
|
+
return addresses.find((addr) => addr.addressType === addressType);
|
|
1073
|
+
}
|
|
1074
|
+
return addresses[0];
|
|
1075
|
+
}, [addresses, addressType]);
|
|
1076
|
+
const truncatedAddress = useMemo4(() => {
|
|
1077
|
+
if (!displayAddress)
|
|
1078
|
+
return "";
|
|
1079
|
+
const addr = displayAddress.address;
|
|
1080
|
+
if (addr.length <= 12)
|
|
1081
|
+
return addr;
|
|
1082
|
+
return `${addr.slice(0, 6)}...${addr.slice(-4)}`;
|
|
1083
|
+
}, [displayAddress]);
|
|
1084
|
+
const buttonStyle = {
|
|
1085
|
+
width: fullWidth ? "100%" : "auto",
|
|
1086
|
+
padding: "12px 16px",
|
|
1087
|
+
border: "none",
|
|
1088
|
+
borderRadius: theme.borderRadius,
|
|
1089
|
+
fontFamily: theme.typography.captionBold.fontFamily,
|
|
1090
|
+
fontSize: theme.typography.captionBold.fontSize,
|
|
1091
|
+
fontStyle: theme.typography.captionBold.fontStyle,
|
|
1092
|
+
fontWeight: theme.typography.captionBold.fontWeight,
|
|
1093
|
+
lineHeight: theme.typography.captionBold.lineHeight,
|
|
1094
|
+
letterSpacing: theme.typography.captionBold.letterSpacing,
|
|
1095
|
+
cursor: "pointer",
|
|
1096
|
+
transition: "background-color 0.2s",
|
|
1097
|
+
display: "flex",
|
|
1098
|
+
alignItems: "center",
|
|
1099
|
+
justifyContent: "center",
|
|
1100
|
+
gap: "8px",
|
|
1101
|
+
background: theme.aux,
|
|
1102
|
+
color: theme.text
|
|
1103
|
+
};
|
|
1104
|
+
const connectedButtonStyle = {
|
|
1105
|
+
...buttonStyle,
|
|
1106
|
+
background: theme.aux,
|
|
1107
|
+
cursor: "pointer"
|
|
1108
|
+
};
|
|
1109
|
+
if (isConnected && displayAddress) {
|
|
1110
|
+
return /* @__PURE__ */ jsx7("button", { style: connectedButtonStyle, onClick: open, children: /* @__PURE__ */ jsx7("span", { style: { fontFamily: "monospace" }, children: truncatedAddress }) });
|
|
1111
|
+
}
|
|
1112
|
+
return /* @__PURE__ */ jsx7("button", { style: buttonStyle, onClick: open, children: "Connect Wallet" });
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
// src/components/ConnectBox.tsx
|
|
1116
|
+
import { useMemo as useMemo5 } from "react";
|
|
1117
|
+
import { useTheme as useTheme7 } from "@phantom/wallet-sdk-ui";
|
|
1118
|
+
import { jsx as jsx8 } from "react/jsx-runtime";
|
|
1119
|
+
function ConnectBox({ maxWidth = "350px", transparent = false, appIcon, appName }) {
|
|
1120
|
+
const theme = useTheme7();
|
|
1121
|
+
const { isConnected } = usePhantom();
|
|
1122
|
+
const boxStyle = useMemo5(() => {
|
|
1123
|
+
const style = {
|
|
1124
|
+
width: "100%",
|
|
1125
|
+
maxWidth: typeof maxWidth === "number" ? `${maxWidth}px` : maxWidth,
|
|
1126
|
+
position: "relative",
|
|
1127
|
+
overflow: "hidden"
|
|
1128
|
+
};
|
|
1129
|
+
if (!transparent) {
|
|
1130
|
+
style.backgroundColor = theme.background;
|
|
1131
|
+
style.borderRadius = theme.borderRadius;
|
|
1132
|
+
}
|
|
1133
|
+
return style;
|
|
1134
|
+
}, [maxWidth, transparent, theme.background, theme.borderRadius]);
|
|
1135
|
+
const noOp = () => {
|
|
345
1136
|
};
|
|
1137
|
+
return /* @__PURE__ */ jsx8("div", { style: boxStyle, children: isConnected ? /* @__PURE__ */ jsx8(ConnectedModalContent, { onClose: noOp, hideCloseButton: true }) : /* @__PURE__ */ jsx8(ConnectModalContent, { appIcon, appName, onClose: noOp, hideCloseButton: true }) });
|
|
346
1138
|
}
|
|
347
1139
|
|
|
348
1140
|
// src/index.ts
|
|
349
|
-
import {
|
|
1141
|
+
import { darkTheme as darkTheme2, lightTheme, mergeTheme as mergeTheme2 } from "@phantom/wallet-sdk-ui";
|
|
1142
|
+
import { NetworkId, AddressType as AddressType3, DebugLevel, debug, isMobileDevice as isMobileDevice3 } from "@phantom/browser-sdk";
|
|
350
1143
|
export {
|
|
351
|
-
AddressType,
|
|
1144
|
+
AddressType3 as AddressType,
|
|
1145
|
+
ConnectBox,
|
|
1146
|
+
ConnectButton,
|
|
352
1147
|
DebugLevel,
|
|
353
1148
|
NetworkId,
|
|
354
1149
|
PhantomProvider,
|
|
1150
|
+
darkTheme2 as darkTheme,
|
|
355
1151
|
debug,
|
|
1152
|
+
isMobileDevice3 as isMobileDevice,
|
|
1153
|
+
lightTheme,
|
|
1154
|
+
mergeTheme2 as mergeTheme,
|
|
356
1155
|
useAccounts,
|
|
357
1156
|
useAutoConfirm,
|
|
358
1157
|
useConnect,
|
|
359
1158
|
useDisconnect,
|
|
1159
|
+
useDiscoveredWallets,
|
|
360
1160
|
useEthereum,
|
|
361
1161
|
useIsExtensionInstalled,
|
|
1162
|
+
useIsPhantomLoginAvailable,
|
|
1163
|
+
useModal,
|
|
362
1164
|
usePhantom,
|
|
363
|
-
useSolana
|
|
1165
|
+
useSolana,
|
|
1166
|
+
useTheme5 as useTheme
|
|
364
1167
|
};
|