@phantom/react-sdk 0.0.10 → 0.1.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/index.d.ts CHANGED
@@ -1,18 +1,75 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import { CreatePhantomConfig, Phantom } from '@phantom/browser-sdk';
3
- import * as React from 'react';
4
-
5
- type PhantomContextType = {
6
- phantom: undefined;
7
- isReady: false;
8
- } | {
9
- phantom: Phantom;
10
- isReady: true;
2
+ import { ReactNode } from 'react';
3
+ import * as _phantom_browser_sdk from '@phantom/browser-sdk';
4
+ import { BrowserSDKConfig, BrowserSDK, WalletAddress, NetworkId, SignAndSendTransactionParams, SignedTransaction, CreateUserOrganizationParams, CreateUserOrganizationResult } from '@phantom/browser-sdk';
5
+ export { AddressType, CreateUserOrganizationParams, CreateUserOrganizationResult, NetworkId, SignAndSendTransactionParams, SignMessageParams, SignedTransaction, WalletAddress } from '@phantom/browser-sdk';
6
+
7
+ interface PhantomSDKConfig extends BrowserSDKConfig {
8
+ }
9
+ interface ConnectOptions {
10
+ providerType?: "injected" | "embedded";
11
+ embeddedWalletType?: "app-wallet" | "user-wallet";
12
+ }
13
+ interface PhantomContextValue {
14
+ sdk: BrowserSDK | null;
15
+ isConnected: boolean;
16
+ addresses: WalletAddress[];
17
+ walletId: string | null;
18
+ isReady: boolean;
19
+ error: Error | null;
20
+ currentProviderType: "injected" | "embedded" | null;
21
+ isPhantomAvailable: boolean;
22
+ updateConnectionState: () => Promise<void>;
23
+ }
24
+ interface PhantomProviderProps {
25
+ children: ReactNode;
26
+ config: PhantomSDKConfig;
27
+ }
28
+ declare function PhantomProvider({ children, config }: PhantomProviderProps): react_jsx_runtime.JSX.Element;
29
+ declare function usePhantom(): PhantomContextValue;
30
+
31
+ declare function useConnect(): {
32
+ connect: (options?: ConnectOptions) => Promise<_phantom_browser_sdk.ConnectResult>;
33
+ switchProvider: (type: "injected" | "embedded", options?: {
34
+ embeddedWalletType?: "app-wallet" | "user-wallet";
35
+ }) => Promise<void>;
36
+ isConnecting: boolean;
37
+ error: Error | null;
38
+ currentProviderType: "injected" | "embedded" | null;
39
+ isPhantomAvailable: boolean;
40
+ };
41
+
42
+ declare function useDisconnect(): {
43
+ disconnect: () => Promise<void>;
44
+ isDisconnecting: boolean;
45
+ error: Error | null;
46
+ };
47
+
48
+ declare function useSignMessage(): {
49
+ signMessage: (message: string, networkId: NetworkId) => Promise<string>;
50
+ isSigning: boolean;
51
+ error: Error | null;
52
+ };
53
+
54
+ declare function useSignAndSendTransaction(): {
55
+ signAndSendTransaction: (params: SignAndSendTransactionParams) => Promise<SignedTransaction>;
56
+ isSigning: boolean;
57
+ error: Error | null;
58
+ };
59
+
60
+ declare function useAccounts(): _phantom_browser_sdk.WalletAddress[] | null;
61
+
62
+ declare function useCreateUserOrganization(): {
63
+ createUserOrganization: (params: CreateUserOrganizationParams) => Promise<CreateUserOrganizationResult>;
64
+ isCreating: boolean;
65
+ error: Error | null;
11
66
  };
12
- declare const PhantomProvider: ({ children, config }: {
13
- children: React.ReactNode;
14
- config: CreatePhantomConfig;
15
- }) => react_jsx_runtime.JSX.Element;
16
- declare function usePhantom(): PhantomContextType;
17
67
 
18
- export { PhantomProvider, usePhantom };
68
+ declare function useIsExtensionInstalled(): {
69
+ isLoading: boolean;
70
+ isInstalled: boolean;
71
+ };
72
+
73
+ type ProviderType = "injected" | "embedded";
74
+
75
+ export { ConnectOptions, PhantomProvider, PhantomProviderProps, PhantomSDKConfig, ProviderType, useAccounts, useConnect, useCreateUserOrganization, useDisconnect, useIsExtensionInstalled, usePhantom, useSignAndSendTransaction, useSignMessage };
package/dist/index.js CHANGED
@@ -30,33 +30,327 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // src/index.ts
31
31
  var src_exports = {};
32
32
  __export(src_exports, {
33
+ AddressType: () => import_browser_sdk2.AddressType,
34
+ NetworkId: () => import_browser_sdk2.NetworkId,
33
35
  PhantomProvider: () => PhantomProvider,
34
- usePhantom: () => usePhantom
36
+ useAccounts: () => useAccounts,
37
+ useConnect: () => useConnect,
38
+ useCreateUserOrganization: () => useCreateUserOrganization,
39
+ useDisconnect: () => useDisconnect,
40
+ useIsExtensionInstalled: () => useIsExtensionInstalled,
41
+ usePhantom: () => usePhantom,
42
+ useSignAndSendTransaction: () => useSignAndSendTransaction,
43
+ useSignMessage: () => useSignMessage
35
44
  });
36
45
  module.exports = __toCommonJS(src_exports);
37
46
 
38
- // src/PhantomContext.tsx
47
+ // src/PhantomProvider.tsx
48
+ var import_react = require("react");
39
49
  var import_browser_sdk = require("@phantom/browser-sdk");
40
- var React = __toESM(require("react"));
41
50
  var import_jsx_runtime = require("react/jsx-runtime");
42
- var PhantomContext = React.createContext({ phantom: void 0, isReady: false });
43
- var PhantomProvider = ({ children, config }) => {
44
- const [context, setContext] = React.useState({ phantom: void 0, isReady: false });
45
- React.useEffect(() => {
46
- const phantom = (0, import_browser_sdk.createPhantom)(config);
47
- setContext({ phantom, isReady: true });
51
+ var PhantomContext = (0, import_react.createContext)(void 0);
52
+ function PhantomProvider({ children, config }) {
53
+ const [sdk, setSdk] = (0, import_react.useState)(null);
54
+ const [isConnected, setIsConnected] = (0, import_react.useState)(false);
55
+ const [addresses, setAddresses] = (0, import_react.useState)([]);
56
+ const [walletId, setWalletId] = (0, import_react.useState)(null);
57
+ const [isReady, setIsReady] = (0, import_react.useState)(false);
58
+ const [error, setError] = (0, import_react.useState)(null);
59
+ const [currentProviderType, setCurrentProviderType] = (0, import_react.useState)(null);
60
+ const [isPhantomAvailable, setIsPhantomAvailable] = (0, import_react.useState)(false);
61
+ (0, import_react.useEffect)(() => {
62
+ try {
63
+ const browserConfig = {
64
+ ...config,
65
+ // Use providerType if provided, default to embedded
66
+ providerType: config.providerType || "embedded"
67
+ };
68
+ const browserSDK = new import_browser_sdk.BrowserSDK(browserConfig);
69
+ setSdk(browserSDK);
70
+ const initialProviderInfo = browserSDK.getCurrentProviderInfo();
71
+ setCurrentProviderType(initialProviderInfo?.type || null);
72
+ const checkPhantom = async () => {
73
+ const available = await browserSDK.waitForPhantomExtension(1e3);
74
+ setIsPhantomAvailable(available);
75
+ };
76
+ checkPhantom();
77
+ setIsReady(true);
78
+ } catch (err) {
79
+ setError(err);
80
+ setIsReady(true);
81
+ }
48
82
  }, [config]);
49
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PhantomContext.Provider, { value: context, children });
50
- };
83
+ const updateConnectionState = (0, import_react.useCallback)(async () => {
84
+ if (sdk) {
85
+ try {
86
+ const connected = sdk.isConnected();
87
+ setIsConnected(connected);
88
+ const providerInfo = sdk.getCurrentProviderInfo();
89
+ setCurrentProviderType(providerInfo?.type || null);
90
+ if (connected) {
91
+ const addrs = await sdk.getAddresses();
92
+ setAddresses(addrs);
93
+ setWalletId(sdk.getWalletId());
94
+ } else {
95
+ setAddresses([]);
96
+ setWalletId(null);
97
+ }
98
+ } catch (err) {
99
+ console.error("Error updating connection state:", err);
100
+ }
101
+ }
102
+ }, [sdk]);
103
+ (0, import_react.useEffect)(() => {
104
+ updateConnectionState();
105
+ }, [updateConnectionState]);
106
+ (0, import_react.useEffect)(() => {
107
+ if (sdk) {
108
+ sdk._updateConnectionState = updateConnectionState;
109
+ }
110
+ }, [sdk, updateConnectionState]);
111
+ const value = {
112
+ sdk,
113
+ isConnected,
114
+ addresses,
115
+ updateConnectionState,
116
+ walletId,
117
+ isReady,
118
+ error,
119
+ currentProviderType,
120
+ isPhantomAvailable
121
+ };
122
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PhantomContext.Provider, { value, children });
123
+ }
51
124
  function usePhantom() {
52
- const context = React.useContext(PhantomContext);
125
+ const context = (0, import_react.useContext)(PhantomContext);
53
126
  if (!context) {
54
127
  throw new Error("usePhantom must be used within a PhantomProvider");
55
128
  }
56
129
  return context;
57
130
  }
58
- // Annotate the CommonJS export names for ESM import in node:
59
- 0 && (module.exports = {
60
- PhantomProvider,
61
- usePhantom
62
- });
131
+
132
+ // src/hooks/useConnect.ts
133
+ var import_react2 = require("react");
134
+ function useConnect() {
135
+ const context = usePhantom();
136
+ const [isConnecting, setIsConnecting] = (0, import_react2.useState)(false);
137
+ const [error, setError] = (0, import_react2.useState)(null);
138
+ const connect = (0, import_react2.useCallback)(
139
+ async (options) => {
140
+ if (!context.sdk || !context.isReady) {
141
+ throw new Error("SDK not initialized");
142
+ }
143
+ setIsConnecting(true);
144
+ setError(null);
145
+ try {
146
+ const result = await context.sdk.connect(options);
147
+ if (context.sdk._updateConnectionState) {
148
+ await context.sdk._updateConnectionState();
149
+ }
150
+ return result;
151
+ } catch (err) {
152
+ console.error("Error connecting to Phantom:", err);
153
+ setError(err);
154
+ throw err;
155
+ } finally {
156
+ setIsConnecting(false);
157
+ }
158
+ },
159
+ [context.sdk, context.isReady]
160
+ );
161
+ const switchProvider = (0, import_react2.useCallback)(
162
+ async (type, options) => {
163
+ if (!context.sdk || !context.isReady) {
164
+ throw new Error("SDK not initialized");
165
+ }
166
+ await context.sdk.switchProvider(type, options);
167
+ if (context.sdk._updateConnectionState) {
168
+ await context.sdk._updateConnectionState();
169
+ }
170
+ },
171
+ [context.sdk, context.isReady]
172
+ );
173
+ return {
174
+ connect,
175
+ switchProvider,
176
+ isConnecting,
177
+ error,
178
+ currentProviderType: context.currentProviderType,
179
+ isPhantomAvailable: context.isPhantomAvailable
180
+ };
181
+ }
182
+
183
+ // src/hooks/useDisconnect.ts
184
+ var import_react3 = require("react");
185
+ function useDisconnect() {
186
+ const { sdk, isReady, updateConnectionState } = usePhantom();
187
+ const [isDisconnecting, setIsDisconnecting] = (0, import_react3.useState)(false);
188
+ const [error, setError] = (0, import_react3.useState)(null);
189
+ const disconnect = (0, import_react3.useCallback)(async () => {
190
+ if (!sdk || !isReady) {
191
+ throw new Error("SDK not initialized");
192
+ }
193
+ setIsDisconnecting(true);
194
+ setError(null);
195
+ try {
196
+ await sdk.disconnect();
197
+ await updateConnectionState();
198
+ } catch (err) {
199
+ setError(err);
200
+ throw err;
201
+ } finally {
202
+ setIsDisconnecting(false);
203
+ }
204
+ }, [sdk, isReady, updateConnectionState]);
205
+ return {
206
+ disconnect,
207
+ isDisconnecting,
208
+ error
209
+ };
210
+ }
211
+
212
+ // src/hooks/useSignMessage.ts
213
+ var import_react4 = require("react");
214
+ function useSignMessage() {
215
+ const { sdk, isConnected } = usePhantom();
216
+ const [isSigning, setIsSigning] = (0, import_react4.useState)(false);
217
+ const [error, setError] = (0, import_react4.useState)(null);
218
+ const signMessage = (0, import_react4.useCallback)(
219
+ async (message, networkId) => {
220
+ if (!sdk) {
221
+ throw new Error("SDK not initialized");
222
+ }
223
+ if (!isConnected) {
224
+ throw new Error("Wallet not connected");
225
+ }
226
+ setIsSigning(true);
227
+ setError(null);
228
+ try {
229
+ const signature = await sdk.signMessage(message, networkId);
230
+ return signature;
231
+ } catch (err) {
232
+ setError(err);
233
+ throw err;
234
+ } finally {
235
+ setIsSigning(false);
236
+ }
237
+ },
238
+ [sdk, isConnected]
239
+ );
240
+ return {
241
+ signMessage,
242
+ isSigning,
243
+ error
244
+ };
245
+ }
246
+
247
+ // src/hooks/useSignAndSendTransaction.ts
248
+ var import_react5 = require("react");
249
+ function useSignAndSendTransaction() {
250
+ const { sdk, isConnected } = usePhantom();
251
+ const [isSigning, setIsSigning] = (0, import_react5.useState)(false);
252
+ const [error, setError] = (0, import_react5.useState)(null);
253
+ const signAndSendTransaction = (0, import_react5.useCallback)(
254
+ async (params) => {
255
+ if (!sdk) {
256
+ throw new Error("SDK not initialized");
257
+ }
258
+ if (!isConnected) {
259
+ throw new Error("Wallet not connected");
260
+ }
261
+ setIsSigning(true);
262
+ setError(null);
263
+ try {
264
+ const result = await sdk.signAndSendTransaction(params);
265
+ return result;
266
+ } catch (err) {
267
+ setError(err);
268
+ throw err;
269
+ } finally {
270
+ setIsSigning(false);
271
+ }
272
+ },
273
+ [sdk, isConnected]
274
+ );
275
+ return {
276
+ signAndSendTransaction,
277
+ isSigning,
278
+ error
279
+ };
280
+ }
281
+
282
+ // src/hooks/useAccounts.ts
283
+ function useAccounts() {
284
+ const { addresses, isConnected } = usePhantom();
285
+ return isConnected ? addresses : null;
286
+ }
287
+
288
+ // src/hooks/useCreateUserOrganization.ts
289
+ var import_react6 = require("react");
290
+ function useCreateUserOrganization() {
291
+ const { sdk } = usePhantom();
292
+ const [isCreating, setIsCreating] = (0, import_react6.useState)(false);
293
+ const [error, setError] = (0, import_react6.useState)(null);
294
+ const createUserOrganization = (0, import_react6.useCallback)(
295
+ async (params) => {
296
+ if (!sdk) {
297
+ throw new Error("SDK not initialized");
298
+ }
299
+ setIsCreating(true);
300
+ setError(null);
301
+ try {
302
+ const result = await sdk.createUserOrganization(params);
303
+ return result;
304
+ } catch (err) {
305
+ setError(err);
306
+ throw err;
307
+ } finally {
308
+ setIsCreating(false);
309
+ }
310
+ },
311
+ [sdk]
312
+ );
313
+ return {
314
+ createUserOrganization,
315
+ isCreating,
316
+ error
317
+ };
318
+ }
319
+
320
+ // src/hooks/useIsExtensionInstalled.ts
321
+ var React = __toESM(require("react"));
322
+ var cachedIsInstalled = null;
323
+ function useIsExtensionInstalled() {
324
+ const { sdk } = usePhantom();
325
+ const [isLoading, setIsLoading] = React.useState(cachedIsInstalled === null);
326
+ const [isInstalled, setIsInstalled] = React.useState(cachedIsInstalled ?? false);
327
+ React.useEffect(() => {
328
+ if (!sdk) {
329
+ setIsLoading(false);
330
+ return;
331
+ }
332
+ if (cachedIsInstalled !== null) {
333
+ setIsInstalled(cachedIsInstalled);
334
+ setIsLoading(false);
335
+ return;
336
+ }
337
+ const checkExtension = async () => {
338
+ try {
339
+ setIsLoading(true);
340
+ const result = await sdk.waitForPhantomExtension(1e3);
341
+ cachedIsInstalled = result;
342
+ setIsInstalled(result);
343
+ } catch (error) {
344
+ cachedIsInstalled = false;
345
+ setIsInstalled(false);
346
+ } finally {
347
+ setIsLoading(false);
348
+ }
349
+ };
350
+ checkExtension();
351
+ }, [sdk]);
352
+ return { isLoading, isInstalled };
353
+ }
354
+
355
+ // src/index.ts
356
+ var import_browser_sdk2 = require("@phantom/browser-sdk");