@phantom/react-native-sdk 0.1.3 → 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,48 +443,74 @@ 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]);
486
- const value = {
487
- sdk,
488
- isConnected,
489
- addresses,
490
- walletId,
491
- error,
492
- updateConnectionState,
493
- setWalletId
494
- };
489
+ if (config.autoConnect !== false) {
490
+ sdk.autoConnect().catch(() => {
491
+ });
492
+ }
493
+ }, [sdk, config.autoConnect]);
494
+ const value = (0, import_react.useMemo)(
495
+ () => ({
496
+ sdk,
497
+ isConnected,
498
+ isConnecting,
499
+ connectError,
500
+ addresses,
501
+ walletId,
502
+ setWalletId
503
+ }),
504
+ [
505
+ sdk,
506
+ isConnected,
507
+ isConnecting,
508
+ connectError,
509
+ addresses,
510
+ walletId,
511
+ setWalletId
512
+ ]
513
+ );
495
514
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PhantomContext.Provider, { value, children });
496
515
  }
497
516
  function usePhantom() {
@@ -505,46 +524,36 @@ function usePhantom() {
505
524
  // src/hooks/useConnect.ts
506
525
  var import_react2 = require("react");
507
526
  function useConnect() {
508
- const { sdk, updateConnectionState, setWalletId } = usePhantom();
509
- const [isConnecting, setIsConnecting] = (0, import_react2.useState)(false);
510
- const [error, setError] = (0, import_react2.useState)(null);
527
+ const { sdk, isConnecting, connectError, setWalletId } = usePhantom();
511
528
  const connect = (0, import_react2.useCallback)(
512
529
  async (options) => {
513
530
  if (!sdk) {
514
531
  throw new Error("SDK not initialized");
515
532
  }
516
- setIsConnecting(true);
517
- setError(null);
518
533
  try {
519
534
  const result = await sdk.connect(options);
520
- if (result.status === "completed") {
521
- if (result.walletId) {
522
- setWalletId(result.walletId);
523
- }
524
- updateConnectionState();
535
+ if (result.status === "completed" && result.walletId) {
536
+ setWalletId(result.walletId);
525
537
  }
526
538
  return result;
527
539
  } catch (err) {
528
- const error2 = err;
529
- setError(error2);
530
- throw error2;
531
- } finally {
532
- setIsConnecting(false);
540
+ const error = err;
541
+ throw error;
533
542
  }
534
543
  },
535
- [sdk, updateConnectionState, setWalletId]
544
+ [sdk, setWalletId]
536
545
  );
537
546
  return {
538
547
  connect,
539
548
  isConnecting,
540
- error
549
+ error: connectError
541
550
  };
542
551
  }
543
552
 
544
553
  // src/hooks/useDisconnect.ts
545
554
  var import_react3 = require("react");
546
555
  function useDisconnect() {
547
- const { sdk, updateConnectionState } = usePhantom();
556
+ const { sdk } = usePhantom();
548
557
  const [isDisconnecting, setIsDisconnecting] = (0, import_react3.useState)(false);
549
558
  const [error, setError] = (0, import_react3.useState)(null);
550
559
  const disconnect = (0, import_react3.useCallback)(async () => {
@@ -555,7 +564,6 @@ function useDisconnect() {
555
564
  setError(null);
556
565
  try {
557
566
  await sdk.disconnect();
558
- updateConnectionState();
559
567
  } catch (err) {
560
568
  const error2 = err;
561
569
  setError(error2);
@@ -563,7 +571,7 @@ function useDisconnect() {
563
571
  } finally {
564
572
  setIsDisconnecting(false);
565
573
  }
566
- }, [sdk, updateConnectionState]);
574
+ }, [sdk]);
567
575
  return {
568
576
  disconnect,
569
577
  isDisconnecting,
@@ -573,12 +581,11 @@ function useDisconnect() {
573
581
 
574
582
  // src/hooks/useAccounts.ts
575
583
  function useAccounts() {
576
- const { addresses, isConnected, walletId, error } = usePhantom();
584
+ const { addresses, isConnected, walletId } = usePhantom();
577
585
  return {
578
586
  addresses,
579
587
  isConnected,
580
- walletId,
581
- error
588
+ walletId
582
589
  };
583
590
  }
584
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,48 +399,74 @@ 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]);
442
- const value = {
443
- sdk,
444
- isConnected,
445
- addresses,
446
- walletId,
447
- error,
448
- updateConnectionState,
449
- setWalletId
450
- };
445
+ if (config.autoConnect !== false) {
446
+ sdk.autoConnect().catch(() => {
447
+ });
448
+ }
449
+ }, [sdk, config.autoConnect]);
450
+ const value = useMemo(
451
+ () => ({
452
+ sdk,
453
+ isConnected,
454
+ isConnecting,
455
+ connectError,
456
+ addresses,
457
+ walletId,
458
+ setWalletId
459
+ }),
460
+ [
461
+ sdk,
462
+ isConnected,
463
+ isConnecting,
464
+ connectError,
465
+ addresses,
466
+ walletId,
467
+ setWalletId
468
+ ]
469
+ );
451
470
  return /* @__PURE__ */ jsx(PhantomContext.Provider, { value, children });
452
471
  }
453
472
  function usePhantom() {
@@ -459,51 +478,41 @@ function usePhantom() {
459
478
  }
460
479
 
461
480
  // src/hooks/useConnect.ts
462
- import { useState as useState2, useCallback as useCallback2 } from "react";
481
+ import { useCallback } from "react";
463
482
  function useConnect() {
464
- const { sdk, updateConnectionState, setWalletId } = usePhantom();
465
- const [isConnecting, setIsConnecting] = useState2(false);
466
- const [error, setError] = useState2(null);
467
- const connect = useCallback2(
483
+ const { sdk, isConnecting, connectError, setWalletId } = usePhantom();
484
+ const connect = useCallback(
468
485
  async (options) => {
469
486
  if (!sdk) {
470
487
  throw new Error("SDK not initialized");
471
488
  }
472
- setIsConnecting(true);
473
- setError(null);
474
489
  try {
475
490
  const result = await sdk.connect(options);
476
- if (result.status === "completed") {
477
- if (result.walletId) {
478
- setWalletId(result.walletId);
479
- }
480
- updateConnectionState();
491
+ if (result.status === "completed" && result.walletId) {
492
+ setWalletId(result.walletId);
481
493
  }
482
494
  return result;
483
495
  } catch (err) {
484
- const error2 = err;
485
- setError(error2);
486
- throw error2;
487
- } finally {
488
- setIsConnecting(false);
496
+ const error = err;
497
+ throw error;
489
498
  }
490
499
  },
491
- [sdk, updateConnectionState, setWalletId]
500
+ [sdk, setWalletId]
492
501
  );
493
502
  return {
494
503
  connect,
495
504
  isConnecting,
496
- error
505
+ error: connectError
497
506
  };
498
507
  }
499
508
 
500
509
  // src/hooks/useDisconnect.ts
501
- import { useState as useState3, useCallback as useCallback3 } from "react";
510
+ import { useState as useState2, useCallback as useCallback2 } from "react";
502
511
  function useDisconnect() {
503
- const { sdk, updateConnectionState } = usePhantom();
504
- const [isDisconnecting, setIsDisconnecting] = useState3(false);
505
- const [error, setError] = useState3(null);
506
- 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 () => {
507
516
  if (!sdk) {
508
517
  throw new Error("SDK not initialized");
509
518
  }
@@ -511,7 +520,6 @@ function useDisconnect() {
511
520
  setError(null);
512
521
  try {
513
522
  await sdk.disconnect();
514
- updateConnectionState();
515
523
  } catch (err) {
516
524
  const error2 = err;
517
525
  setError(error2);
@@ -519,7 +527,7 @@ function useDisconnect() {
519
527
  } finally {
520
528
  setIsDisconnecting(false);
521
529
  }
522
- }, [sdk, updateConnectionState]);
530
+ }, [sdk]);
523
531
  return {
524
532
  disconnect,
525
533
  isDisconnecting,
@@ -529,22 +537,21 @@ function useDisconnect() {
529
537
 
530
538
  // src/hooks/useAccounts.ts
531
539
  function useAccounts() {
532
- const { addresses, isConnected, walletId, error } = usePhantom();
540
+ const { addresses, isConnected, walletId } = usePhantom();
533
541
  return {
534
542
  addresses,
535
543
  isConnected,
536
- walletId,
537
- error
544
+ walletId
538
545
  };
539
546
  }
540
547
 
541
548
  // src/hooks/useSignMessage.ts
542
- import { useState as useState4, useCallback as useCallback4 } from "react";
549
+ import { useState as useState3, useCallback as useCallback3 } from "react";
543
550
  function useSignMessage() {
544
551
  const { sdk } = usePhantom();
545
- const [isSigning, setIsSigning] = useState4(false);
546
- const [error, setError] = useState4(null);
547
- const signMessage = useCallback4(
552
+ const [isSigning, setIsSigning] = useState3(false);
553
+ const [error, setError] = useState3(null);
554
+ const signMessage = useCallback3(
548
555
  async (params) => {
549
556
  if (!sdk) {
550
557
  throw new Error("SDK not initialized");
@@ -572,12 +579,12 @@ function useSignMessage() {
572
579
  }
573
580
 
574
581
  // src/hooks/useSignAndSendTransaction.ts
575
- import { useState as useState5, useCallback as useCallback5 } from "react";
582
+ import { useState as useState4, useCallback as useCallback4 } from "react";
576
583
  function useSignAndSendTransaction() {
577
584
  const { sdk } = usePhantom();
578
- const [isSigning, setIsSigning] = useState5(false);
579
- const [error, setError] = useState5(null);
580
- const signAndSendTransaction = useCallback5(
585
+ const [isSigning, setIsSigning] = useState4(false);
586
+ const [error, setError] = useState4(null);
587
+ const signAndSendTransaction = useCallback4(
581
588
  async (params) => {
582
589
  if (!sdk) {
583
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.3",
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",
@@ -45,13 +45,13 @@
45
45
  "directory": "packages/react-native-sdk"
46
46
  },
47
47
  "dependencies": {
48
- "@phantom/api-key-stamper": "^0.1.4",
48
+ "@phantom/api-key-stamper": "^0.1.5",
49
49
  "@phantom/base64url": "^0.1.0",
50
- "@phantom/client": "^0.1.7",
51
- "@phantom/constants": "^0.0.2",
50
+ "@phantom/client": "^0.1.8",
51
+ "@phantom/constants": "^0.0.3",
52
52
  "@phantom/crypto": "^0.1.2",
53
- "@phantom/embedded-provider-core": "^0.1.4",
54
- "@phantom/sdk-types": "^0.1.3",
53
+ "@phantom/embedded-provider-core": "^0.1.6",
54
+ "@phantom/sdk-types": "^0.1.4",
55
55
  "@types/bs58": "^5.0.0",
56
56
  "bs58": "^6.0.0",
57
57
  "buffer": "^6.0.3"