@phantom/browser-sdk 1.0.0-beta.2 → 1.0.0-beta.21

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
@@ -19,7 +19,7 @@ const sdk = new BrowserSDK({
19
19
  addressTypes: [AddressType.solana, AddressType.ethereum],
20
20
  });
21
21
 
22
- const { addresses } = await sdk.connect();
22
+ const { addresses } = await sdk.connect({ provider: "injected" });
23
23
  console.log("Connected addresses:", addresses);
24
24
 
25
25
  // Chain-specific operations
@@ -44,11 +44,10 @@ import { BrowserSDK, AddressType } from "@phantom/browser-sdk";
44
44
  const sdk = new BrowserSDK({
45
45
  providerType: "embedded",
46
46
  addressTypes: [AddressType.solana, AddressType.ethereum],
47
- apiBaseUrl: "https://api.phantom.app/v1/wallets",
48
- appId: "your-app-id",
47
+ appId: "your-app-id", // Get your app ID from phantom.com/portal
49
48
  });
50
49
 
51
- const { addresses } = await sdk.connect();
50
+ const { addresses } = await sdk.connect({ provider: "phantom" });
52
51
  console.log("Addresses:", addresses);
53
52
 
54
53
  // Use chain-specific APIs
@@ -79,8 +78,8 @@ const sdk = new BrowserSDK({
79
78
  addressTypes: [AddressType.solana, AddressType.ethereum],
80
79
  });
81
80
 
82
- // 2. Connect to wallet
83
- const { addresses } = await sdk.connect();
81
+ // 2. Connect to wallet (provider parameter is required)
82
+ const { addresses } = await sdk.connect({ provider: "injected" });
84
83
  console.log("Connected addresses:", addresses);
85
84
 
86
85
  // 3. Use chain-specific methods
@@ -94,24 +93,38 @@ const ethResult = await sdk.ethereum.sendTransaction({
94
93
 
95
94
  ### Connection Options
96
95
 
97
- For embedded user-wallets, you can specify authentication providers:
96
+ The `connect()` method requires a `provider` parameter and automatically switches between providers based on the authentication method you specify:
98
97
 
99
98
  ```typescript
100
- // Default: Show provider selection screen
101
- const result = await sdk.connect();
99
+ // Connect with injected provider (Phantom extension)
100
+ // Automatically switches to injected provider if not already using it
101
+ const result = await sdk.connect({
102
+ provider: "injected",
103
+ });
102
104
 
103
- // Google authentication (skips provider selection)
105
+ // Connect with Google authentication (embedded provider)
106
+ // Automatically switches to embedded provider if not already using it
104
107
  const result = await sdk.connect({
105
- authOptions: {
106
- provider: "google",
107
- },
108
+ provider: "google",
108
109
  });
109
110
 
110
- // Apple authentication (skips provider selection)
111
+ // Connect with Apple authentication (embedded provider)
112
+ // Automatically switches to embedded provider if not already using it
111
113
  const result = await sdk.connect({
112
- authOptions: {
113
- provider: "apple",
114
- },
114
+ provider: "apple",
115
+ });
116
+
117
+ // Connect with Phantom authentication (embedded provider)
118
+ // Uses Phantom extension or mobile app for authentication
119
+ // Automatically switches to embedded provider if not already using it
120
+ const result = await sdk.connect({
121
+ provider: "phantom",
122
+ });
123
+
124
+ // Connect with JWT authentication (embedded provider)
125
+ const result = await sdk.connect({
126
+ provider: "jwt",
127
+ jwtToken: "your-jwt-token",
115
128
  });
116
129
  ```
117
130
 
@@ -190,48 +203,29 @@ Creates a non-custodial wallet embedded in your application. Requires API config
190
203
  const sdk = new BrowserSDK({
191
204
  providerType: "embedded",
192
205
  addressTypes: [AddressType.solana, AddressType.ethereum],
193
- apiBaseUrl: "https://api.phantom.app/v1/wallets",
194
- appId: "your-app-id",
195
- embeddedWalletType: "app-wallet", // or 'user-wallet'
206
+ appId: "your-app-id", // Get your app ID from phantom.com/portal
196
207
  authOptions: {
197
- authUrl: "https://auth.phantom.app", // optional, defaults to "https://connect.phantom.app"
208
+ authUrl: "https://connect.phantom.app/login", // optional, defaults to "https://connect.phantom.app/login"
198
209
  redirectUrl: "https://yourapp.com/callback", // optional, defaults to current page
199
210
  },
200
211
  autoConnect: true, // optional, auto-connect to existing session (default: true for embedded)
201
212
  });
202
213
  ```
203
214
 
204
- ### Embedded Wallet Types
205
-
206
- #### App Wallet (`'app-wallet'`)
207
-
208
- - **New wallets** created per application
209
- - **Unfunded** by default - you need to fund them
210
- - **Independent** from user's existing Phantom wallet
211
- - **Perfect for**: Gaming, DeFi protocols, or apps that need fresh wallets
212
-
213
- ```typescript
214
- const sdk = new BrowserSDK({
215
- providerType: "embedded",
216
- embeddedWalletType: "app-wallet",
217
- addressTypes: [AddressType.solana],
218
- // ... other config
219
- });
220
- ```
215
+ ### Embedded Wallet Type
221
216
 
222
217
  #### User Wallet (`'user-wallet'`)
223
218
 
224
219
  - **Uses Phantom authentication** - user logs in with existing Phantom account
225
220
  - **Potentially funded** - brings in user's existing wallet balance
226
221
  - **Connected** to user's Phantom ecosystem
227
- - **Perfect for**: Trading platforms, NFT marketplaces, or apps needing funded wallets
222
+ - **Perfect for**: All embedded wallet use cases
228
223
 
229
224
  ```typescript
230
225
  const sdk = new BrowserSDK({
231
226
  providerType: "embedded",
232
- embeddedWalletType: "user-wallet",
227
+ appId: "your-app-id",
233
228
  addressTypes: [AddressType.solana, AddressType.ethereum],
234
- // ... other config
235
229
  });
236
230
  ```
237
231
 
@@ -242,28 +236,6 @@ const sdk = new BrowserSDK({
242
236
  | `AddressType.solana` | Solana Mainnet, Devnet, Testnet |
243
237
  | `AddressType.ethereum` | Ethereum, Polygon, Arbitrum, and more |
244
238
 
245
- ### Solana Provider Configuration
246
-
247
- When using `AddressType.solana`, you can choose between two Solana libraries:
248
-
249
- ```typescript
250
- const sdk = new BrowserSDK({
251
- providerType: "embedded",
252
- addressTypes: [AddressType.solana],
253
- solanaProvider: "web3js", // or 'kit'
254
- // ... other config
255
- });
256
- ```
257
-
258
- **Provider Options:**
259
-
260
- - `'web3js'` (default) - Uses `@solana/web3.js` library
261
- - `'kit'` - Uses `@solana/kit` library (modern, TypeScript-first)
262
-
263
- **When to use each:**
264
-
265
- - **@solana/web3.js**: Better ecosystem compatibility, wider community support
266
- - **@solana/kit**: Better TypeScript support, modern architecture, smaller bundle size
267
239
 
268
240
  ### Auto-Connect Feature
269
241
 
@@ -272,8 +244,8 @@ The SDK can automatically reconnect to existing sessions when instantiated, prov
272
244
  ```typescript
273
245
  const sdk = new BrowserSDK({
274
246
  providerType: "embedded",
247
+ appId: "your-app-id",
275
248
  addressTypes: [AddressType.solana],
276
- // ... other config
277
249
  autoConnect: true, // Default: true for embedded, false for injected
278
250
  });
279
251
 
@@ -295,8 +267,8 @@ if (sdk.isConnected()) {
295
267
  ```typescript
296
268
  const sdk = new BrowserSDK({
297
269
  providerType: "embedded",
270
+ appId: "your-app-id",
298
271
  addressTypes: [AddressType.solana],
299
- // ... other config
300
272
  autoConnect: false, // Disable auto-connect
301
273
  });
302
274
 
@@ -320,21 +292,24 @@ interface BrowserSDKConfig {
320
292
  addressTypes?: [AddressType, ...AddressType[]]; // Networks to enable (e.g., [AddressType.solana])
321
293
 
322
294
  // Required for embedded provider only
323
- apiBaseUrl?: string; // Phantom API base URL
324
- appId?: string; // Your app ID (required for embedded provider)
295
+ appId?: string; // Your app ID from phantom.com/portal (required for embedded provider)
296
+
297
+ // Optional configuration
298
+ apiBaseUrl?: string; // Phantom API base URL (optional, has default)
325
299
  authOptions?: {
326
- authUrl?: string; // Custom auth URL (default: "https://connect.phantom.app")
327
- redirectUrl?: string; // Custom redirect URL after authentication
300
+ authUrl?: string; // Custom auth URL (optional, defaults to "https://connect.phantom.app/login")
301
+ redirectUrl?: string; // Custom redirect URL after authentication (optional)
328
302
  };
329
- embeddedWalletType?: "app-wallet" | "user-wallet"; // Wallet type
330
- solanaProvider?: "web3js" | "kit"; // Solana library choice (default: 'web3js')
331
- autoConnect?: boolean; // Enable auto-connect to existing sessions (default: true)
303
+ embeddedWalletType?: "user-wallet"; // Wallet type (optional, defaults to "user-wallet", currently the only supported type)
304
+ autoConnect?: boolean; // Enable auto-connect to existing sessions (optional, defaults to true for embedded)
332
305
  }
333
306
  ```
334
307
 
335
308
  ### Extension Detection
336
309
 
337
- For injected provider usage, you can check if the Phantom extension is installed:
310
+ #### waitForPhantomExtension
311
+
312
+ Check if the Phantom extension is installed:
338
313
 
339
314
  ```typescript
340
315
  import { waitForPhantomExtension } from "@phantom/browser-sdk";
@@ -348,15 +323,46 @@ if (isAvailable) {
348
323
  }
349
324
  ```
350
325
 
326
+ #### isPhantomLoginAvailable
327
+
328
+ Check if Phantom Login is available (requires extension to be installed and support the `phantom_login` feature):
329
+
330
+ ```typescript
331
+ import { isPhantomLoginAvailable } from "@phantom/browser-sdk";
332
+
333
+ const isAvailable = await isPhantomLoginAvailable();
334
+
335
+ if (isAvailable) {
336
+ console.log("Phantom Login is available!");
337
+ // Can use provider: "phantom" in connect()
338
+ } else {
339
+ console.log("Phantom Login is not available");
340
+ }
341
+ ```
342
+
351
343
  ### Core Methods
352
344
 
353
- #### connect()
345
+ #### connect(options)
354
346
 
355
347
  Connect to wallet and get addresses for configured AddressTypes.
356
348
 
349
+ **Parameters:**
350
+ - `options: AuthOptions` (required) - Authentication options
351
+ - `provider: "google" | "apple" | "jwt" | "phantom" | "injected"` (required) - Authentication provider to use
352
+ - `jwtToken?: string` (optional) - JWT token (required when `provider` is "jwt")
353
+ - `customAuthData?: Record<string, any>` (optional) - Custom authentication data
354
+
357
355
  ```typescript
358
- const result = await sdk.connect();
359
- // Returns: { walletId?: string, addresses: WalletAddress[] }
356
+ // Connect with injected provider
357
+ const result = await sdk.connect({ provider: "injected" });
358
+
359
+ // Connect with Phantom authentication
360
+ const result = await sdk.connect({ provider: "phantom" });
361
+
362
+ // Connect with Google authentication
363
+ const result = await sdk.connect({ provider: "google" });
364
+
365
+ // Returns: { addresses: WalletAddress[], status: "pending" | "completed", providerType: "embedded" | "injected" }
360
366
  // addresses only includes types from addressTypes config
361
367
  ```
362
368
 
@@ -385,15 +391,6 @@ Check if SDK is connected to a wallet.
385
391
  const connected = sdk.isConnected();
386
392
  ```
387
393
 
388
- #### getWalletId()
389
-
390
- Get the wallet ID (embedded wallets only).
391
-
392
- ```typescript
393
- const walletId = sdk.getWalletId();
394
- // Returns string for embedded wallets, null for injected
395
- ```
396
-
397
394
  ### Solana Chain Methods
398
395
 
399
396
  #### signMessage(message)
@@ -547,7 +544,7 @@ const result = await sdk.ethereum.sendTransaction({
547
544
 
548
545
  #### switchChain(chainId)
549
546
 
550
- Switch to a different Ethereum chain.
547
+ Switch to a different Ethereum chain. Accepts a chain ID as a number or a hex string value.
551
548
 
552
549
  ```typescript
553
550
  await sdk.ethereum.switchChain(1); // Ethereum mainnet
@@ -555,6 +552,21 @@ await sdk.ethereum.switchChain(137); // Polygon
555
552
  await sdk.ethereum.switchChain(42161); // Arbitrum One
556
553
  ```
557
554
 
555
+ **Supported EVM Networks:**
556
+
557
+ | Network | Chain ID | Usage |
558
+ |---------|----------|-------|
559
+ | Ethereum Mainnet | `1` | `switchChain(1)` |
560
+ | Ethereum Sepolia | `11155111` | `switchChain(11155111)` |
561
+ | Polygon Mainnet | `137` | `switchChain(137)` |
562
+ | Polygon Amoy | `80002` | `switchChain(80002)` |
563
+ | Base Mainnet | `8453` | `switchChain(8453)` |
564
+ | Base Sepolia | `84532` | `switchChain(84532)` |
565
+ | Arbitrum One | `42161` | `switchChain(42161)` |
566
+ | Arbitrum Sepolia | `421614` | `switchChain(421614)` |
567
+ | Monad Mainnet | `143` | `switchChain(143)` |
568
+ | Monad Testnet | `10143` | `switchChain(10143)` |
569
+
558
570
  #### getChainId()
559
571
 
560
572
  Get the current chain ID.
@@ -590,6 +602,106 @@ Attempt auto-connection using existing session. Should be called after setting u
590
602
  await sdk.autoConnect();
591
603
  ```
592
604
 
605
+ ### Event Handlers
606
+
607
+ The SDK provides typed event handlers that allow you to listen for connection state changes. This is especially useful for `autoConnect()` flows where you need to track the connection result.
608
+
609
+ #### Available Events
610
+
611
+ ```typescript
612
+ import { BrowserSDK } from '@phantom/browser-sdk';
613
+ import type {
614
+ ConnectEventData,
615
+ ConnectStartEventData,
616
+ ConnectErrorEventData,
617
+ DisconnectEventData
618
+ } from '@phantom/browser-sdk';
619
+
620
+ const sdk = new BrowserSDK({
621
+ providerType: 'embedded',
622
+ appId: 'your-app-id',
623
+ addressTypes: [AddressType.solana],
624
+ });
625
+
626
+ // 1. connect_start - Fired when connection starts
627
+ sdk.on('connect_start', (data: ConnectStartEventData) => {
628
+ console.log('Connection starting:', data.source); // "auto-connect" | "manual-connect"
629
+ console.log('Auth options:', data.authOptions?.provider); // "google" | "apple" | etc.
630
+ });
631
+
632
+ // 2. connect - Fired when connection succeeds (includes full ConnectResult)
633
+ sdk.on('connect', (data: ConnectEventData) => {
634
+ console.log('Connected successfully!');
635
+ console.log('Provider type:', data.providerType); // "embedded" | "injected"
636
+ console.log('Wallet ID:', data.walletId); // only for embedded providers
637
+ console.log('Addresses:', data.addresses); // WalletAddress[]
638
+ console.log('Status:', data.status); // "pending" | "completed"
639
+ console.log('Source:', data.source); // "auto-connect" | "manual-connect" | "manual-existing" | "existing-session" | "manual"
640
+ });
641
+
642
+ // 3. connect_error - Fired when connection fails
643
+ sdk.on('connect_error', (data: ConnectErrorEventData) => {
644
+ console.error('Connection failed:', data.error);
645
+ console.log('Source:', data.source); // "auto-connect" | "manual-connect"
646
+ });
647
+
648
+ // 4. disconnect - Fired when disconnected
649
+ sdk.on('disconnect', (data: DisconnectEventData) => {
650
+ console.log('Disconnected from wallet');
651
+ console.log('Source:', data.source); // "manual"
652
+ });
653
+
654
+ // 5. error - General error handler
655
+ sdk.on('error', (error: unknown) => {
656
+ console.error('SDK error:', error);
657
+ });
658
+
659
+ // Don't forget to remove listeners when done
660
+ sdk.off('connect', handleConnect);
661
+ ```
662
+
663
+ #### Event Types
664
+
665
+ | Event | Payload Type | When Fired | Key Data |
666
+ |-------|-------------|------------|----------|
667
+ | `connect_start` | `ConnectStartEventData` | Connection initiated | `source`, `authOptions` |
668
+ | `connect` | `ConnectEventData` | Connection successful | `providerType`, `addresses`, `status`, `source`, `user`|
669
+ | `connect_error` | `ConnectErrorEventData` | Connection failed | `error`, `source` |
670
+ | `disconnect` | `DisconnectEventData` | Disconnected | `source` |
671
+ | `error` | `unknown` | General SDK errors | Error details |
672
+
673
+ #### Using Events with autoConnect()
674
+
675
+ Event handlers are especially useful with `autoConnect()` since it doesn't return a value:
676
+
677
+ ```typescript
678
+ const sdk = new BrowserSDK({
679
+ providerType: 'embedded',
680
+ appId: 'your-app-id',
681
+ addressTypes: [AddressType.solana],
682
+ autoConnect: true,
683
+ });
684
+
685
+ // Set up event listeners BEFORE autoConnect
686
+ sdk.on('connect', (data: ConnectEventData) => {
687
+ console.log('Auto-connected successfully!');
688
+ console.log('Provider type:', data.providerType);
689
+ console.log('Addresses:', data.addresses);
690
+
691
+ // Update your UI state here
692
+ updateUIWithAddresses(data.addresses);
693
+ });
694
+
695
+ sdk.on('connect_error', (data: ConnectErrorEventData) => {
696
+ console.log('Auto-connect failed:', data.error);
697
+ // Show connect button to user
698
+ showConnectButton();
699
+ });
700
+
701
+ // Auto-connect will trigger events
702
+ await sdk.autoConnect();
703
+ ```
704
+
593
705
  ### Auto-Confirm Methods (Injected Provider Only)
594
706
 
595
707
  The SDK provides auto-confirm functionality that allows automatic transaction confirmation for specified chains. This feature is only available when using the injected provider (Phantom browser extension).
@@ -647,33 +759,15 @@ console.log("Supported chains:", supportedChains.chains);
647
759
 
648
760
  #### Available NetworkId Values
649
761
 
762
+ For a complete list of supported networks including Solana, Ethereum, Polygon, Base, Arbitrum, Monad, and more, see the [Network Support section in the main README](../../README.md#network-support).
763
+
650
764
  ```typescript
651
765
  import { NetworkId } from "@phantom/browser-sdk";
652
766
 
653
- // Solana networks
654
- NetworkId.SOLANA_MAINNET; // "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"
655
- NetworkId.SOLANA_DEVNET; // "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1"
656
- NetworkId.SOLANA_TESTNET; // "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z"
657
-
658
- // Ethereum networks
659
- NetworkId.ETHEREUM_MAINNET; // "eip155:1"
660
- NetworkId.ETHEREUM_SEPOLIA; // "eip155:11155111"
661
-
662
- // Polygon networks
663
- NetworkId.POLYGON_MAINNET; // "eip155:137"
664
- NetworkId.POLYGON_AMOY; // "eip155:80002"
665
-
666
- // Arbitrum networks
667
- NetworkId.ARBITRUM_ONE; // "eip155:42161"
668
- NetworkId.ARBITRUM_SEPOLIA; // "eip155:421614"
669
-
670
- // Optimism networks
671
- NetworkId.OPTIMISM_MAINNET; // "eip155:10"
672
- NetworkId.OPTIMISM_GOERLI; // "eip155:420"
673
-
674
- // Base networks
675
- NetworkId.BASE_MAINNET; // "eip155:8453"
676
- NetworkId.BASE_SEPOLIA; // "eip155:84532"
767
+ // Example: Use NetworkId for auto-confirm
768
+ await sdk.enableAutoConfirm({
769
+ chains: [NetworkId.SOLANA_MAINNET, NetworkId.ETHEREUM_MAINNET],
770
+ });
677
771
  ```
678
772
 
679
773
  **Important Notes:**
@@ -748,7 +842,7 @@ import { BrowserSDK, DebugLevel } from "@phantom/browser-sdk";
748
842
 
749
843
  const sdk = new BrowserSDK({
750
844
  providerType: "embedded",
751
- // ... other config
845
+ appId: "your-app-id",
752
846
  });
753
847
 
754
848
  // Store debug messages
@@ -815,10 +909,9 @@ import { BrowserSDK, AddressType } from "@phantom/browser-sdk";
815
909
  const sdk = new BrowserSDK({
816
910
  providerType: "injected",
817
911
  addressTypes: [AddressType.solana],
818
- solanaProvider: "web3js",
819
912
  });
820
913
 
821
- await sdk.connect();
914
+ await sdk.connect({ provider: "injected" });
822
915
 
823
916
  // Get recent blockhash
824
917
  const connection = new Connection("https://api.mainnet-beta.solana.com");
@@ -869,10 +962,9 @@ import { BrowserSDK, AddressType } from "@phantom/browser-sdk";
869
962
  const sdk = new BrowserSDK({
870
963
  providerType: "injected",
871
964
  addressTypes: [AddressType.solana],
872
- solanaProvider: "kit",
873
965
  });
874
966
 
875
- await sdk.connect();
967
+ await sdk.connect({ provider: "injected" });
876
968
 
877
969
  // Create transaction with @solana/kit
878
970
  const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");
@@ -902,7 +994,7 @@ const sdk = new BrowserSDK({
902
994
  addressTypes: [AddressType.ethereum],
903
995
  });
904
996
 
905
- await sdk.connect();
997
+ await sdk.connect({ provider: "injected" });
906
998
 
907
999
  // Simple ETH transfer
908
1000
  const result = await sdk.ethereum.sendTransaction({