@phantom/react-native-sdk 0.1.4 → 0.1.5

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 CHANGED
@@ -202,6 +202,7 @@ interface PhantomSDKConfig {
202
202
  };
203
203
  appName?: string; // Optional app name for branding
204
204
  appLogo?: string; // Optional app logo URL for branding
205
+ autoConnect?: boolean; // Auto-connect to existing session on SDK instantiation (default: true)
205
206
  debug?: boolean; // Enable debug logging (optional)
206
207
  }
207
208
  ```
package/dist/index.d.ts CHANGED
@@ -11,6 +11,8 @@ interface PhantomSDKConfig extends EmbeddedProviderConfig {
11
11
  scheme: string;
12
12
  /** Enable debug logging */
13
13
  debug?: boolean;
14
+ /** Enable auto-connect to existing sessions (default: true) */
15
+ autoConnect?: boolean;
14
16
  }
15
17
  interface ConnectOptions {
16
18
  /** OAuth provider to use */
@@ -24,10 +26,10 @@ interface ConnectOptions {
24
26
  interface PhantomContextValue {
25
27
  sdk: EmbeddedProvider;
26
28
  isConnected: boolean;
29
+ isConnecting: boolean;
30
+ connectError: Error | null;
27
31
  addresses: WalletAddress[];
28
32
  walletId: string | null;
29
- error: Error | null;
30
- updateConnectionState: () => void;
31
33
  setWalletId: (walletId: string | null) => void;
32
34
  }
33
35
  interface PhantomProviderProps {
@@ -57,7 +59,6 @@ declare function useAccounts(): {
57
59
  addresses: _phantom_embedded_provider_core.WalletAddress[];
58
60
  isConnected: boolean;
59
61
  walletId: string | null;
60
- error: Error | null;
61
62
  };
62
63
 
63
64
  declare function useSignMessage(): {
package/dist/index.js CHANGED
@@ -422,18 +422,11 @@ function PhantomProvider({ children, config }) {
422
422
  const sdk = (0, import_react.useMemo)(() => {
423
423
  const redirectUrl = config.authOptions?.redirectUrl || `${config.scheme}://phantom-auth-callback`;
424
424
  const embeddedConfig = {
425
- apiBaseUrl: config.apiBaseUrl,
426
- organizationId: config.organizationId,
425
+ ...config,
427
426
  authOptions: {
428
- ...config.authOptions,
427
+ ...config.authOptions || {},
429
428
  redirectUrl
430
- },
431
- embeddedWalletType: config.embeddedWalletType,
432
- addressTypes: config.addressTypes,
433
- solanaProvider: config.solanaProvider || "web3js",
434
- appName: config.appName,
435
- appLogo: config.appLogo
436
- // Optional app logo URL
429
+ }
437
430
  };
438
431
  const storage = new ExpoSecureStorage();
439
432
  const authProvider = new ExpoAuthProvider();
@@ -450,56 +443,71 @@ function PhantomProvider({ children, config }) {
450
443
  stamper,
451
444
  name: `${import_react_native2.Platform.OS}-${import_react_native2.Platform.Version}`
452
445
  };
453
- return new import_embedded_provider_core.EmbeddedProvider(embeddedConfig, platform, logger);
446
+ const sdkInstance = new import_embedded_provider_core.EmbeddedProvider(embeddedConfig, platform, logger);
447
+ const handleConnectStart = () => {
448
+ setIsConnecting(true);
449
+ setConnectError(null);
450
+ };
451
+ const handleConnect = async () => {
452
+ try {
453
+ setIsConnected(true);
454
+ setIsConnecting(false);
455
+ const addrs = await sdkInstance.getAddresses();
456
+ setAddresses(addrs);
457
+ } catch (err) {
458
+ console.error("Error connecting:", err);
459
+ try {
460
+ await sdkInstance.disconnect();
461
+ } catch (err2) {
462
+ console.error("Error disconnecting:", err2);
463
+ }
464
+ }
465
+ };
466
+ const handleConnectError = (errorData) => {
467
+ setIsConnecting(false);
468
+ setConnectError(new Error(errorData.error || "Connection failed"));
469
+ };
470
+ const handleDisconnect = () => {
471
+ setIsConnected(false);
472
+ setIsConnecting(false);
473
+ setConnectError(null);
474
+ setAddresses([]);
475
+ setWalletId(null);
476
+ };
477
+ sdkInstance.on("connect_start", handleConnectStart);
478
+ sdkInstance.on("connect", handleConnect);
479
+ sdkInstance.on("connect_error", handleConnectError);
480
+ sdkInstance.on("disconnect", handleDisconnect);
481
+ return sdkInstance;
454
482
  }, [config]);
455
483
  const [isConnected, setIsConnected] = (0, import_react.useState)(false);
484
+ const [isConnecting, setIsConnecting] = (0, import_react.useState)(false);
485
+ const [connectError, setConnectError] = (0, import_react.useState)(null);
456
486
  const [addresses, setAddresses] = (0, import_react.useState)([]);
457
487
  const [walletId, setWalletId] = (0, import_react.useState)(null);
458
- const [error, setError] = (0, import_react.useState)(null);
459
- const updateConnectionState = (0, import_react.useCallback)(() => {
460
- try {
461
- const connected = sdk.isConnected();
462
- setIsConnected(connected);
463
- if (connected) {
464
- const addrs = sdk.getAddresses();
465
- setAddresses(addrs);
466
- } else {
467
- setAddresses([]);
468
- setWalletId(null);
469
- }
470
- } catch (err) {
471
- console.error("[PhantomProvider] Error updating connection state", err);
472
- setError(err);
473
- try {
474
- sdk.disconnect();
475
- setIsConnected(false);
476
- setAddresses([]);
477
- setWalletId(null);
478
- } catch (disconnectErr) {
479
- console.error("[PhantomProvider] Error disconnecting after error", disconnectErr);
480
- }
481
- }
482
- }, [sdk]);
483
488
  (0, import_react.useEffect)(() => {
484
- updateConnectionState();
485
- }, [updateConnectionState]);
489
+ if (config.autoConnect !== false) {
490
+ sdk.autoConnect().catch(() => {
491
+ });
492
+ }
493
+ }, [sdk, config.autoConnect]);
486
494
  const value = (0, import_react.useMemo)(
487
495
  () => ({
488
496
  sdk,
489
497
  isConnected,
498
+ isConnecting,
499
+ connectError,
490
500
  addresses,
491
501
  walletId,
492
- error,
493
- updateConnectionState,
494
502
  setWalletId
495
503
  }),
496
504
  [
497
505
  sdk,
498
506
  isConnected,
507
+ isConnecting,
508
+ connectError,
499
509
  addresses,
500
510
  walletId,
501
- error,
502
- updateConnectionState,
503
511
  setWalletId
504
512
  ]
505
513
  );
@@ -516,46 +524,36 @@ function usePhantom() {
516
524
  // src/hooks/useConnect.ts
517
525
  var import_react2 = require("react");
518
526
  function useConnect() {
519
- const { sdk, updateConnectionState, setWalletId } = usePhantom();
520
- const [isConnecting, setIsConnecting] = (0, import_react2.useState)(false);
521
- const [error, setError] = (0, import_react2.useState)(null);
527
+ const { sdk, isConnecting, connectError, setWalletId } = usePhantom();
522
528
  const connect = (0, import_react2.useCallback)(
523
529
  async (options) => {
524
530
  if (!sdk) {
525
531
  throw new Error("SDK not initialized");
526
532
  }
527
- setIsConnecting(true);
528
- setError(null);
529
533
  try {
530
534
  const result = await sdk.connect(options);
531
- if (result.status === "completed") {
532
- if (result.walletId) {
533
- setWalletId(result.walletId);
534
- }
535
- updateConnectionState();
535
+ if (result.status === "completed" && result.walletId) {
536
+ setWalletId(result.walletId);
536
537
  }
537
538
  return result;
538
539
  } catch (err) {
539
- const error2 = err;
540
- setError(error2);
541
- throw error2;
542
- } finally {
543
- setIsConnecting(false);
540
+ const error = err;
541
+ throw error;
544
542
  }
545
543
  },
546
- [sdk, updateConnectionState, setWalletId]
544
+ [sdk, setWalletId]
547
545
  );
548
546
  return {
549
547
  connect,
550
548
  isConnecting,
551
- error
549
+ error: connectError
552
550
  };
553
551
  }
554
552
 
555
553
  // src/hooks/useDisconnect.ts
556
554
  var import_react3 = require("react");
557
555
  function useDisconnect() {
558
- const { sdk, updateConnectionState } = usePhantom();
556
+ const { sdk } = usePhantom();
559
557
  const [isDisconnecting, setIsDisconnecting] = (0, import_react3.useState)(false);
560
558
  const [error, setError] = (0, import_react3.useState)(null);
561
559
  const disconnect = (0, import_react3.useCallback)(async () => {
@@ -566,7 +564,6 @@ function useDisconnect() {
566
564
  setError(null);
567
565
  try {
568
566
  await sdk.disconnect();
569
- updateConnectionState();
570
567
  } catch (err) {
571
568
  const error2 = err;
572
569
  setError(error2);
@@ -574,7 +571,7 @@ function useDisconnect() {
574
571
  } finally {
575
572
  setIsDisconnecting(false);
576
573
  }
577
- }, [sdk, updateConnectionState]);
574
+ }, [sdk]);
578
575
  return {
579
576
  disconnect,
580
577
  isDisconnecting,
@@ -584,12 +581,11 @@ function useDisconnect() {
584
581
 
585
582
  // src/hooks/useAccounts.ts
586
583
  function useAccounts() {
587
- const { addresses, isConnected, walletId, error } = usePhantom();
584
+ const { addresses, isConnected, walletId } = usePhantom();
588
585
  return {
589
586
  addresses,
590
587
  isConnected,
591
- walletId,
592
- error
588
+ walletId
593
589
  };
594
590
  }
595
591
 
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/PhantomProvider.tsx
2
- import { createContext, useContext, useState, useEffect, useCallback, useMemo } from "react";
2
+ import { createContext, useContext, useState, useEffect, useMemo } from "react";
3
3
  import { EmbeddedProvider } from "@phantom/embedded-provider-core";
4
4
 
5
5
  // src/providers/embedded/storage.ts
@@ -378,18 +378,11 @@ function PhantomProvider({ children, config }) {
378
378
  const sdk = useMemo(() => {
379
379
  const redirectUrl = config.authOptions?.redirectUrl || `${config.scheme}://phantom-auth-callback`;
380
380
  const embeddedConfig = {
381
- apiBaseUrl: config.apiBaseUrl,
382
- organizationId: config.organizationId,
381
+ ...config,
383
382
  authOptions: {
384
- ...config.authOptions,
383
+ ...config.authOptions || {},
385
384
  redirectUrl
386
- },
387
- embeddedWalletType: config.embeddedWalletType,
388
- addressTypes: config.addressTypes,
389
- solanaProvider: config.solanaProvider || "web3js",
390
- appName: config.appName,
391
- appLogo: config.appLogo
392
- // Optional app logo URL
385
+ }
393
386
  };
394
387
  const storage = new ExpoSecureStorage();
395
388
  const authProvider = new ExpoAuthProvider();
@@ -406,56 +399,71 @@ function PhantomProvider({ children, config }) {
406
399
  stamper,
407
400
  name: `${Platform.OS}-${Platform.Version}`
408
401
  };
409
- return new EmbeddedProvider(embeddedConfig, platform, logger);
402
+ const sdkInstance = new EmbeddedProvider(embeddedConfig, platform, logger);
403
+ const handleConnectStart = () => {
404
+ setIsConnecting(true);
405
+ setConnectError(null);
406
+ };
407
+ const handleConnect = async () => {
408
+ try {
409
+ setIsConnected(true);
410
+ setIsConnecting(false);
411
+ const addrs = await sdkInstance.getAddresses();
412
+ setAddresses(addrs);
413
+ } catch (err) {
414
+ console.error("Error connecting:", err);
415
+ try {
416
+ await sdkInstance.disconnect();
417
+ } catch (err2) {
418
+ console.error("Error disconnecting:", err2);
419
+ }
420
+ }
421
+ };
422
+ const handleConnectError = (errorData) => {
423
+ setIsConnecting(false);
424
+ setConnectError(new Error(errorData.error || "Connection failed"));
425
+ };
426
+ const handleDisconnect = () => {
427
+ setIsConnected(false);
428
+ setIsConnecting(false);
429
+ setConnectError(null);
430
+ setAddresses([]);
431
+ setWalletId(null);
432
+ };
433
+ sdkInstance.on("connect_start", handleConnectStart);
434
+ sdkInstance.on("connect", handleConnect);
435
+ sdkInstance.on("connect_error", handleConnectError);
436
+ sdkInstance.on("disconnect", handleDisconnect);
437
+ return sdkInstance;
410
438
  }, [config]);
411
439
  const [isConnected, setIsConnected] = useState(false);
440
+ const [isConnecting, setIsConnecting] = useState(false);
441
+ const [connectError, setConnectError] = useState(null);
412
442
  const [addresses, setAddresses] = useState([]);
413
443
  const [walletId, setWalletId] = useState(null);
414
- const [error, setError] = useState(null);
415
- const updateConnectionState = useCallback(() => {
416
- try {
417
- const connected = sdk.isConnected();
418
- setIsConnected(connected);
419
- if (connected) {
420
- const addrs = sdk.getAddresses();
421
- setAddresses(addrs);
422
- } else {
423
- setAddresses([]);
424
- setWalletId(null);
425
- }
426
- } catch (err) {
427
- console.error("[PhantomProvider] Error updating connection state", err);
428
- setError(err);
429
- try {
430
- sdk.disconnect();
431
- setIsConnected(false);
432
- setAddresses([]);
433
- setWalletId(null);
434
- } catch (disconnectErr) {
435
- console.error("[PhantomProvider] Error disconnecting after error", disconnectErr);
436
- }
437
- }
438
- }, [sdk]);
439
444
  useEffect(() => {
440
- updateConnectionState();
441
- }, [updateConnectionState]);
445
+ if (config.autoConnect !== false) {
446
+ sdk.autoConnect().catch(() => {
447
+ });
448
+ }
449
+ }, [sdk, config.autoConnect]);
442
450
  const value = useMemo(
443
451
  () => ({
444
452
  sdk,
445
453
  isConnected,
454
+ isConnecting,
455
+ connectError,
446
456
  addresses,
447
457
  walletId,
448
- error,
449
- updateConnectionState,
450
458
  setWalletId
451
459
  }),
452
460
  [
453
461
  sdk,
454
462
  isConnected,
463
+ isConnecting,
464
+ connectError,
455
465
  addresses,
456
466
  walletId,
457
- error,
458
- updateConnectionState,
459
467
  setWalletId
460
468
  ]
461
469
  );
@@ -470,51 +478,41 @@ function usePhantom() {
470
478
  }
471
479
 
472
480
  // src/hooks/useConnect.ts
473
- import { useState as useState2, useCallback as useCallback2 } from "react";
481
+ import { useCallback } from "react";
474
482
  function useConnect() {
475
- const { sdk, updateConnectionState, setWalletId } = usePhantom();
476
- const [isConnecting, setIsConnecting] = useState2(false);
477
- const [error, setError] = useState2(null);
478
- const connect = useCallback2(
483
+ const { sdk, isConnecting, connectError, setWalletId } = usePhantom();
484
+ const connect = useCallback(
479
485
  async (options) => {
480
486
  if (!sdk) {
481
487
  throw new Error("SDK not initialized");
482
488
  }
483
- setIsConnecting(true);
484
- setError(null);
485
489
  try {
486
490
  const result = await sdk.connect(options);
487
- if (result.status === "completed") {
488
- if (result.walletId) {
489
- setWalletId(result.walletId);
490
- }
491
- updateConnectionState();
491
+ if (result.status === "completed" && result.walletId) {
492
+ setWalletId(result.walletId);
492
493
  }
493
494
  return result;
494
495
  } catch (err) {
495
- const error2 = err;
496
- setError(error2);
497
- throw error2;
498
- } finally {
499
- setIsConnecting(false);
496
+ const error = err;
497
+ throw error;
500
498
  }
501
499
  },
502
- [sdk, updateConnectionState, setWalletId]
500
+ [sdk, setWalletId]
503
501
  );
504
502
  return {
505
503
  connect,
506
504
  isConnecting,
507
- error
505
+ error: connectError
508
506
  };
509
507
  }
510
508
 
511
509
  // src/hooks/useDisconnect.ts
512
- import { useState as useState3, useCallback as useCallback3 } from "react";
510
+ import { useState as useState2, useCallback as useCallback2 } from "react";
513
511
  function useDisconnect() {
514
- const { sdk, updateConnectionState } = usePhantom();
515
- const [isDisconnecting, setIsDisconnecting] = useState3(false);
516
- const [error, setError] = useState3(null);
517
- const disconnect = useCallback3(async () => {
512
+ const { sdk } = usePhantom();
513
+ const [isDisconnecting, setIsDisconnecting] = useState2(false);
514
+ const [error, setError] = useState2(null);
515
+ const disconnect = useCallback2(async () => {
518
516
  if (!sdk) {
519
517
  throw new Error("SDK not initialized");
520
518
  }
@@ -522,7 +520,6 @@ function useDisconnect() {
522
520
  setError(null);
523
521
  try {
524
522
  await sdk.disconnect();
525
- updateConnectionState();
526
523
  } catch (err) {
527
524
  const error2 = err;
528
525
  setError(error2);
@@ -530,7 +527,7 @@ function useDisconnect() {
530
527
  } finally {
531
528
  setIsDisconnecting(false);
532
529
  }
533
- }, [sdk, updateConnectionState]);
530
+ }, [sdk]);
534
531
  return {
535
532
  disconnect,
536
533
  isDisconnecting,
@@ -540,22 +537,21 @@ function useDisconnect() {
540
537
 
541
538
  // src/hooks/useAccounts.ts
542
539
  function useAccounts() {
543
- const { addresses, isConnected, walletId, error } = usePhantom();
540
+ const { addresses, isConnected, walletId } = usePhantom();
544
541
  return {
545
542
  addresses,
546
543
  isConnected,
547
- walletId,
548
- error
544
+ walletId
549
545
  };
550
546
  }
551
547
 
552
548
  // src/hooks/useSignMessage.ts
553
- import { useState as useState4, useCallback as useCallback4 } from "react";
549
+ import { useState as useState3, useCallback as useCallback3 } from "react";
554
550
  function useSignMessage() {
555
551
  const { sdk } = usePhantom();
556
- const [isSigning, setIsSigning] = useState4(false);
557
- const [error, setError] = useState4(null);
558
- const signMessage = useCallback4(
552
+ const [isSigning, setIsSigning] = useState3(false);
553
+ const [error, setError] = useState3(null);
554
+ const signMessage = useCallback3(
559
555
  async (params) => {
560
556
  if (!sdk) {
561
557
  throw new Error("SDK not initialized");
@@ -583,12 +579,12 @@ function useSignMessage() {
583
579
  }
584
580
 
585
581
  // src/hooks/useSignAndSendTransaction.ts
586
- import { useState as useState5, useCallback as useCallback5 } from "react";
582
+ import { useState as useState4, useCallback as useCallback4 } from "react";
587
583
  function useSignAndSendTransaction() {
588
584
  const { sdk } = usePhantom();
589
- const [isSigning, setIsSigning] = useState5(false);
590
- const [error, setError] = useState5(null);
591
- const signAndSendTransaction = useCallback5(
585
+ const [isSigning, setIsSigning] = useState4(false);
586
+ const [error, setError] = useState4(null);
587
+ const signAndSendTransaction = useCallback4(
592
588
  async (params) => {
593
589
  if (!sdk) {
594
590
  throw new Error("SDK not initialized");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phantom/react-native-sdk",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Phantom Wallet SDK for React Native and Expo applications",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -50,7 +50,7 @@
50
50
  "@phantom/client": "^0.1.8",
51
51
  "@phantom/constants": "^0.0.3",
52
52
  "@phantom/crypto": "^0.1.2",
53
- "@phantom/embedded-provider-core": "^0.1.5",
53
+ "@phantom/embedded-provider-core": "^0.1.6",
54
54
  "@phantom/sdk-types": "^0.1.4",
55
55
  "@types/bs58": "^5.0.0",
56
56
  "bs58": "^6.0.0",