@phantom/browser-sdk 0.3.8 → 1.0.0-beta.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # @phantom/browser-sdk
2
2
 
3
- Browser SDK for Phantom Wallet supporting both injected and embedded non-custodial wallets.
3
+ Browser SDK for Phantom Wallet supporting both injected and embedded non-custodial wallets with chain-specific APIs.
4
4
 
5
5
  ## Quick Start
6
6
 
@@ -11,28 +11,34 @@ npm install @phantom/browser-sdk
11
11
  ### Injected Provider (Browser Extension)
12
12
 
13
13
  ```typescript
14
- import { BrowserSDK, NetworkId, AddressType } from "@phantom/browser-sdk";
14
+ import { BrowserSDK, AddressType } from "@phantom/browser-sdk";
15
15
 
16
16
  // Connect to Phantom browser extension
17
17
  const sdk = new BrowserSDK({
18
18
  providerType: "injected",
19
- addressTypes: [AddressType.solana],
19
+ addressTypes: [AddressType.solana, AddressType.ethereum],
20
20
  });
21
21
 
22
22
  const { addresses } = await sdk.connect();
23
23
  console.log("Connected addresses:", addresses);
24
24
 
25
+ // Chain-specific operations
26
+ const message = "Hello from Phantom!";
27
+ const solanaSignature = await sdk.solana.signMessage(message);
28
+
29
+ // Encode the message as hex for EVM
30
+ const encoded = "0x" + Buffer.from(message, "utf8").toString("hex");
31
+ const ethSignature = await sdk.ethereum.signPersonalMessage(encoded, addresses[1].address);
32
+
25
33
  // Sign and send transactions
26
- const result = await sdk.signAndSendTransaction({
27
- networkId: NetworkId.SOLANA_MAINNET,
28
- transaction: mySolanaTransaction,
29
- });
34
+ const solanaResult = await sdk.solana.signAndSendTransaction(mySolanaTransaction);
35
+ const ethResult = await sdk.ethereum.sendTransaction(myEthTransaction);
30
36
  ```
31
37
 
32
38
  ### Embedded Provider
33
39
 
34
40
  ```typescript
35
- import { BrowserSDK, AddressType, NetworkId } from "@phantom/browser-sdk";
41
+ import { BrowserSDK, AddressType } from "@phantom/browser-sdk";
36
42
 
37
43
  // Create embedded non-custodial wallet
38
44
  const sdk = new BrowserSDK({
@@ -45,21 +51,124 @@ const sdk = new BrowserSDK({
45
51
  const { addresses } = await sdk.connect();
46
52
  console.log("Addresses:", addresses);
47
53
 
48
- const result = await sdk.signAndSendTransaction({
49
- networkId: NetworkId.SOLANA_MAINNET,
50
- transaction: mySolanaTransaction,
51
- });
54
+ // Use chain-specific APIs
55
+ const solanaResult = await sdk.solana.signAndSendTransaction(mySolanaTransaction);
56
+ const ethResult = await sdk.ethereum.sendTransaction(myEthTransaction);
52
57
  ```
53
58
 
54
59
  ## Features
55
60
 
56
61
  - **🔒 Non-Custodial**: Full user control of private keys for both injected and embedded wallets
57
62
  - **🌐 Dual Provider Support**: Works with Phantom browser extension or creates embedded wallets
63
+ - **⛓️ Chain-Specific APIs**: Dedicated interfaces for Solana and Ethereum operations
58
64
  - **🛠️ Native Transactions**: Work with blockchain-native objects, not base64url strings
59
- - **🔗 Multi-Chain**: Solana, Ethereum, Bitcoin, Sui support
65
+ - **🔗 Multi-Chain**: Solana and Ethereum support with dedicated methods
60
66
  - **⚡ TypeScript**: Full type safety for all transaction formats
61
67
  - **🎯 Unified API**: Same interface for both injected and embedded providers
62
68
 
69
+ ## Connection Flow
70
+
71
+ After instantiating the SDK, use `sdk.connect()` to establish a connection to the wallet:
72
+
73
+ ```typescript
74
+ import { BrowserSDK, AddressType } from "@phantom/browser-sdk";
75
+
76
+ // 1. Create SDK instance
77
+ const sdk = new BrowserSDK({
78
+ providerType: "injected",
79
+ addressTypes: [AddressType.solana, AddressType.ethereum],
80
+ });
81
+
82
+ // 2. Connect to wallet
83
+ const { addresses } = await sdk.connect();
84
+ console.log("Connected addresses:", addresses);
85
+
86
+ // 3. Use chain-specific methods
87
+ const signature = await sdk.solana.signMessage("Hello!");
88
+ const ethResult = await sdk.ethereum.sendTransaction({
89
+ to: "0x...",
90
+ value: "1000000000000000000",
91
+ gas: "21000",
92
+ });
93
+ ```
94
+
95
+ ### Connection Options
96
+
97
+ For embedded user-wallets, you can specify authentication providers:
98
+
99
+ ```typescript
100
+ // Default: Show provider selection screen
101
+ const result = await sdk.connect();
102
+
103
+ // Google authentication (skips provider selection)
104
+ const result = await sdk.connect({
105
+ authOptions: {
106
+ provider: "google",
107
+ },
108
+ });
109
+
110
+ // Apple authentication (skips provider selection)
111
+ const result = await sdk.connect({
112
+ authOptions: {
113
+ provider: "apple",
114
+ },
115
+ });
116
+ ```
117
+
118
+ ## Chain-Specific APIs
119
+
120
+ The SDK provides separate interfaces for each blockchain with optimized methods:
121
+
122
+ ### Solana Chain (`sdk.solana`)
123
+
124
+ ```typescript
125
+ // Message signing
126
+ const signature = await sdk.solana.signMessage("Hello Solana!");
127
+
128
+ // Transaction signing (without sending)
129
+ const signedTx = await sdk.solana.signTransaction(transaction);
130
+
131
+ // Sign and send transaction
132
+ const result = await sdk.solana.signAndSendTransaction(transaction);
133
+
134
+ // Network switching
135
+ await sdk.solana.switchNetwork('devnet');
136
+
137
+ // Utilities
138
+ const publicKey = await sdk.solana.getPublicKey();
139
+ const isConnected = sdk.solana.isConnected();
140
+ ```
141
+
142
+ ### Ethereum Chain (`sdk.ethereum`)
143
+
144
+ ```typescript
145
+ // EIP-1193 requests
146
+ const accounts = await sdk.ethereum.request({ method: 'eth_accounts' });
147
+ const chainId = await sdk.ethereum.request({ method: 'eth_chainId' });
148
+
149
+ // Message signing
150
+ const signature = await sdk.ethereum.signPersonalMessage(message, address);
151
+
152
+ // EIP-712 typed data signing
153
+ const typedDataSignature = await sdk.ethereum.signTypedData(typedData, address);
154
+
155
+ // Transaction sending
156
+ const result = await sdk.ethereum.sendTransaction({
157
+ to: "0x...",
158
+ value: "1000000000000000000", // 1 ETH in wei
159
+ gas: "21000",
160
+ });
161
+
162
+ // Network switching
163
+ await sdk.ethereum.switchChain(1); // Ethereum mainnet
164
+ await sdk.ethereum.switchChain(137); // Polygon
165
+
166
+ // Utilities
167
+ const chainId = await sdk.ethereum.getChainId();
168
+ const accounts = await sdk.ethereum.getAccounts();
169
+ const isConnected = sdk.ethereum.isConnected();
170
+ ```
171
+
63
172
  ## Provider Types
64
173
 
65
174
  ### Injected Provider
@@ -69,7 +178,7 @@ Uses the Phantom browser extension installed by the user. No additional configur
69
178
  ```typescript
70
179
  const sdk = new BrowserSDK({
71
180
  providerType: "injected",
72
- addressTypes: [AddressType.solana],
181
+ addressTypes: [AddressType.solana, AddressType.ethereum],
73
182
  });
74
183
  ```
75
184
 
@@ -128,14 +237,12 @@ const sdk = new BrowserSDK({
128
237
  });
129
238
  ```
130
239
 
131
- ### Available AddressTypes (Embedded Only)
240
+ ### Available AddressTypes
132
241
 
133
- | AddressType | Networks Supported |
134
- | --------------------------- | ------------------------------------------- |
135
- | `AddressType.solana` | Solana Mainnet, Devnet, Testnet |
136
- | `AddressType.ethereum` | Ethereum, Polygon, Arbitrum, Optimism, Base |
137
- | `AddressType.bitcoinSegwit` | Bitcoin Mainnet, Testnet |
138
- | `AddressType.sui` | Sui Mainnet, Testnet, Devnet |
242
+ | AddressType | Supported Chains |
243
+ | --------------------------- | ----------------------------------- |
244
+ | `AddressType.solana` | Solana Mainnet, Devnet, Testnet |
245
+ | `AddressType.ethereum` | Ethereum, Polygon, Arbitrum, and more |
139
246
 
140
247
  ### Solana Provider Configuration
141
248
 
@@ -229,7 +336,7 @@ interface BrowserSDKConfig {
229
336
  }
230
337
  ```
231
338
 
232
- ### Methods
339
+ ### Core Methods
233
340
 
234
341
  #### connect()
235
342
 
@@ -237,114 +344,210 @@ Connect to wallet and get addresses for configured AddressTypes.
237
344
 
238
345
  ```typescript
239
346
  const result = await sdk.connect();
240
- // Returns: { walletId: string, addresses: WalletAddress[] }
347
+ // Returns: { walletId?: string, addresses: WalletAddress[] }
241
348
  // addresses only includes types from addressTypes config
242
349
  ```
243
350
 
244
- For embedded user-wallets, you can specify authentication options:
351
+ #### getAddresses()
352
+
353
+ Get connected wallet addresses.
245
354
 
246
355
  ```typescript
247
- // Phantom Connect with provider selection (default)
248
- const result = await sdk.connect();
356
+ const addresses = await sdk.getAddresses();
357
+ // Returns addresses matching configured AddressTypes
358
+ ```
249
359
 
250
- // Phantom Connect with Google authentication (skips provider selection)
251
- const result = await sdk.connect({
252
- authOptions: {
253
- provider: "google",
254
- },
255
- });
360
+ #### disconnect()
256
361
 
257
- // Phantom Connect with Apple authentication (skips provider selection)
258
- const result = await sdk.connect({
259
- authOptions: {
260
- provider: "apple",
261
- },
262
- });
362
+ Disconnect from wallet and clear session.
263
363
 
264
- // JWT authentication (direct API call)
265
- const result = await sdk.connect({
266
- authOptions: {
267
- provider: "jwt",
268
- jwtToken: "your-jwt-token",
269
- customAuthData: { userId: "user123" },
270
- },
271
- });
364
+ ```typescript
365
+ await sdk.disconnect();
272
366
  ```
273
367
 
274
- **Authentication Options:**
368
+ #### isConnected()
275
369
 
276
- - `provider` - Authentication method: `"google"`, `"apple"`, or `"jwt"`
277
- - If not specified: Shows provider selection screen on Phantom Connect
278
- - If `"google"` or `"apple"`: Skips provider selection and uses specified provider
279
- - If `"jwt"`: Uses JWT authentication flow via API call
280
- - `jwtToken` - Required when `provider` is `"jwt"`. Your JWT token for authentication
281
- - `customAuthData` - Additional data to pass to authentication service
370
+ Check if SDK is connected to a wallet.
282
371
 
283
- **Authentication Flow Types:**
372
+ ```typescript
373
+ const connected = sdk.isConnected();
374
+ ```
284
375
 
285
- 1. **Phantom Connect (Redirect-based)**: Used when `provider` is undefined, `"google"`, or `"apple"`
286
- - Redirects to `https://connect.phantom.app` (or custom `authOptions.authUrl` from config)
287
- - Handles OAuth flow with selected provider
288
- - Returns to your app with authentication result using `authOptions.redirectUrl` or current page
376
+ #### getWalletId()
289
377
 
290
- 2. **JWT Authentication (API-based)**: Used when `provider` is `"jwt"`
291
- - Makes direct API call to `/api/auth/jwt` endpoint
292
- - Validates JWT token server-side
293
- - Returns wallet immediately without redirect
378
+ Get the wallet ID (embedded wallets only).
379
+
380
+ ```typescript
381
+ const walletId = sdk.getWalletId();
382
+ // Returns string for embedded wallets, null for injected
383
+ ```
384
+
385
+ ### Solana Chain Methods
386
+
387
+ #### signMessage(message)
388
+
389
+ Sign a message with the Solana wallet.
390
+
391
+ ```typescript
392
+ const signature = await sdk.solana.signMessage("Hello Solana!");
393
+ // Returns: { signature: string, rawSignature: string }
394
+ ```
395
+
396
+ #### signTransaction(transaction)
397
+
398
+ Sign a transaction without sending it.
399
+
400
+ ```typescript
401
+ const signedTx = await sdk.solana.signTransaction(transaction);
402
+ // Returns the signed transaction object
403
+ ```
294
404
 
295
405
  #### signAndSendTransaction(transaction)
296
406
 
297
- Sign and send a native transaction object.
407
+ Sign and send a transaction to the Solana network.
408
+
409
+ ```typescript
410
+ const result = await sdk.solana.signAndSendTransaction(transaction);
411
+ // Returns: { hash: string, rawTransaction: string, blockExplorer?: string }
412
+ ```
413
+
414
+ #### switchNetwork(network)
415
+
416
+ Switch between Solana networks.
298
417
 
299
418
  ```typescript
300
- // Solana transaction
301
- const result = await sdk.signAndSendTransaction({
302
- networkId: NetworkId.SOLANA_MAINNET,
303
- transaction: solanaTransaction, // Native Transaction or VersionedTransaction
419
+ await sdk.solana.switchNetwork('mainnet');
420
+ await sdk.solana.switchNetwork('devnet');
421
+ ```
422
+
423
+ #### getPublicKey()
424
+
425
+ Get the current Solana public key.
426
+
427
+ ```typescript
428
+ const publicKey = await sdk.solana.getPublicKey();
429
+ // Returns: string | null
430
+ ```
431
+
432
+ #### isConnected()
433
+
434
+ Check if connected to Solana wallet.
435
+
436
+ ```typescript
437
+ const connected = sdk.solana.isConnected();
438
+ // Returns: boolean
439
+ ```
440
+
441
+ ### Ethereum Chain Methods
442
+
443
+ #### request(args)
444
+
445
+ Make an EIP-1193 compatible request.
446
+
447
+ ```typescript
448
+ const accounts = await sdk.ethereum.request({ method: 'eth_accounts' });
449
+ const chainId = await sdk.ethereum.request({ method: 'eth_chainId' });
450
+ const balance = await sdk.ethereum.request({
451
+ method: 'eth_getBalance',
452
+ params: [address, 'latest']
304
453
  });
454
+ ```
455
+
456
+ #### signPersonalMessage(message, address)
305
457
 
306
- // Ethereum transaction
307
- const result = await sdk.signAndSendTransaction({
308
- networkId: NetworkId.ETHEREUM_MAINNET,
309
- transaction: {
310
- to: "0x...",
311
- value: parseEther("1"), // 1 ETH
312
- gas: 21000n,
458
+ Sign a personal message.
459
+
460
+ ```typescript
461
+ const signature = await sdk.ethereum.signPersonalMessage("Hello Ethereum!", address);
462
+ // Returns: { signature: string, rawSignature: string }
463
+ ```
464
+
465
+ #### signTypedData(typedData, address)
466
+
467
+ Sign EIP-712 typed data.
468
+
469
+ ```typescript
470
+ const typedData = {
471
+ types: {
472
+ EIP712Domain: [
473
+ { name: "name", type: "string" },
474
+ { name: "version", type: "string" },
475
+ { name: "chainId", type: "uint256" },
476
+ { name: "verifyingContract", type: "address" }
477
+ ],
478
+ Mail: [
479
+ { name: "from", type: "string" },
480
+ { name: "to", type: "string" },
481
+ { name: "contents", type: "string" }
482
+ ]
313
483
  },
314
- });
484
+ primaryType: "Mail",
485
+ domain: {
486
+ name: "Ether Mail",
487
+ version: "1",
488
+ chainId: 1,
489
+ verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC"
490
+ },
491
+ message: {
492
+ from: "Alice",
493
+ to: "Bob",
494
+ contents: "Hello!"
495
+ }
496
+ };
497
+
498
+ const signature = await sdk.ethereum.signTypedData(typedData, address);
499
+ // Returns: { signature: string, rawSignature: string }
315
500
  ```
316
501
 
317
- #### signMessage(params)
502
+ #### sendTransaction(transaction)
318
503
 
319
- Sign a message string.
504
+ Send an Ethereum transaction.
320
505
 
321
506
  ```typescript
322
- const signature = await sdk.signMessage({
323
- message: "Hello from Phantom!",
324
- networkId: NetworkId.SOLANA_MAINNET,
507
+ const result = await sdk.ethereum.sendTransaction({
508
+ to: "0x742d35Cc6634C0532925a3b8D4C8db86fB5C4A7E",
509
+ value: "1000000000000000000", // 1 ETH in wei
510
+ gas: "21000",
511
+ gasPrice: "20000000000", // 20 gwei
325
512
  });
513
+ // Returns: { hash: string, rawTransaction: string, blockExplorer?: string }
326
514
  ```
327
515
 
328
- **Parameters:**
516
+ #### switchChain(chainId)
329
517
 
330
- - `params.message` (string) - Message to sign
331
- - `params.networkId` (NetworkId) - Network identifier
518
+ Switch to a different Ethereum chain.
332
519
 
333
- #### getAddresses()
520
+ ```typescript
521
+ await sdk.ethereum.switchChain(1); // Ethereum mainnet
522
+ await sdk.ethereum.switchChain(137); // Polygon
523
+ await sdk.ethereum.switchChain(42161); // Arbitrum One
524
+ ```
334
525
 
335
- Get connected wallet addresses.
526
+ #### getChainId()
527
+
528
+ Get the current chain ID.
336
529
 
337
530
  ```typescript
338
- const addresses = await sdk.getAddresses();
339
- // Returns addresses matching configured AddressTypes
531
+ const chainId = await sdk.ethereum.getChainId();
532
+ // Returns: number
340
533
  ```
341
534
 
342
- #### disconnect()
535
+ #### getAccounts()
343
536
 
344
- Disconnect from wallet and clear session.
537
+ Get connected Ethereum accounts.
345
538
 
346
539
  ```typescript
347
- await sdk.disconnect();
540
+ const accounts = await sdk.ethereum.getAccounts();
541
+ // Returns: string[]
542
+ ```
543
+
544
+ #### isConnected()
545
+
546
+ Check if connected to Ethereum wallet.
547
+
548
+ ```typescript
549
+ const connected = sdk.ethereum.isConnected();
550
+ // Returns: boolean
348
551
  ```
349
552
 
350
553
  #### autoConnect()
@@ -355,6 +558,99 @@ Attempt auto-connection using existing session. Should be called after setting u
355
558
  await sdk.autoConnect();
356
559
  ```
357
560
 
561
+ ### Auto-Confirm Methods (Injected Provider Only)
562
+
563
+ 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).
564
+
565
+ #### enableAutoConfirm(params?)
566
+
567
+ Enable auto-confirm for specific chains or all supported chains.
568
+
569
+ ```typescript
570
+ import { NetworkId } from "@phantom/browser-sdk";
571
+
572
+ // Enable auto-confirm for specific chains
573
+ const result = await sdk.enableAutoConfirm({
574
+ chains: [NetworkId.SOLANA_MAINNET, NetworkId.ETHEREUM_MAINNET]
575
+ });
576
+
577
+ // Enable auto-confirm for all supported chains
578
+ const result = await sdk.enableAutoConfirm();
579
+
580
+ console.log("Auto-confirm enabled:", result.enabled);
581
+ console.log("Enabled chains:", result.chains);
582
+ // Returns: { enabled: boolean, chains: NetworkId[] }
583
+ ```
584
+
585
+ #### disableAutoConfirm()
586
+
587
+ Disable auto-confirm for all chains.
588
+
589
+ ```typescript
590
+ const result = await sdk.disableAutoConfirm();
591
+ console.log("Auto-confirm disabled:", !result.enabled);
592
+ // Returns: { enabled: boolean, chains: NetworkId[] }
593
+ ```
594
+
595
+ #### getAutoConfirmStatus()
596
+
597
+ Get the current auto-confirm status and enabled chains.
598
+
599
+ ```typescript
600
+ const status = await sdk.getAutoConfirmStatus();
601
+ console.log("Auto-confirm enabled:", status.enabled);
602
+ console.log("Enabled chains:", status.chains);
603
+ // Returns: { enabled: boolean, chains: NetworkId[] }
604
+ ```
605
+
606
+ #### getSupportedAutoConfirmChains()
607
+
608
+ Get the list of chains that support auto-confirm functionality.
609
+
610
+ ```typescript
611
+ const supportedChains = await sdk.getSupportedAutoConfirmChains();
612
+ console.log("Supported chains:", supportedChains.chains);
613
+ // Returns: { chains: NetworkId[] }
614
+ ```
615
+
616
+ #### Available NetworkId Values
617
+
618
+ ```typescript
619
+ import { NetworkId } from "@phantom/browser-sdk";
620
+
621
+ // Solana networks
622
+ NetworkId.SOLANA_MAINNET // "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"
623
+ NetworkId.SOLANA_DEVNET // "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1"
624
+ NetworkId.SOLANA_TESTNET // "solana:4uhcVJyU9pJkvQyS88uRDiswHXSCkY3z"
625
+
626
+ // Ethereum networks
627
+ NetworkId.ETHEREUM_MAINNET // "eip155:1"
628
+ NetworkId.ETHEREUM_SEPOLIA // "eip155:11155111"
629
+
630
+ // Polygon networks
631
+ NetworkId.POLYGON_MAINNET // "eip155:137"
632
+ NetworkId.POLYGON_AMOY // "eip155:80002"
633
+
634
+ // Arbitrum networks
635
+ NetworkId.ARBITRUM_ONE // "eip155:42161"
636
+ NetworkId.ARBITRUM_SEPOLIA // "eip155:421614"
637
+
638
+ // Optimism networks
639
+ NetworkId.OPTIMISM_MAINNET // "eip155:10"
640
+ NetworkId.OPTIMISM_GOERLI // "eip155:420"
641
+
642
+ // Base networks
643
+ NetworkId.BASE_MAINNET // "eip155:8453"
644
+ NetworkId.BASE_SEPOLIA // "eip155:84532"
645
+ ```
646
+
647
+ **Important Notes:**
648
+
649
+ - Auto-confirm methods are **only available for injected providers** (Phantom browser extension)
650
+ - Calling these methods on embedded providers will throw an error
651
+ - Auto-confirm applies to transaction confirmations, not initial connection prompts
652
+ - Users can override auto-confirm settings directly in the Phantom extension UI
653
+
358
654
  ## Debug Configuration
359
655
 
360
656
  The BrowserSDK provides dynamic debug configuration that can be changed at runtime without reinstantiating the SDK. This provides better performance and cleaner architecture.
@@ -482,13 +778,22 @@ import {
482
778
  LAMPORTS_PER_SOL,
483
779
  Connection,
484
780
  } from "@solana/web3.js";
485
- import { BrowserSDK, NetworkId } from "@phantom/browser-sdk";
781
+ import { BrowserSDK, AddressType } from "@phantom/browser-sdk";
782
+
783
+ const sdk = new BrowserSDK({
784
+ providerType: "injected",
785
+ addressTypes: [AddressType.solana],
786
+ solanaProvider: "web3js",
787
+ });
788
+
789
+ await sdk.connect();
486
790
 
487
791
  // Get recent blockhash
488
792
  const connection = new Connection("https://api.mainnet-beta.solana.com");
489
793
  const { blockhash } = await connection.getLatestBlockhash();
490
794
 
491
795
  // Create transfer instruction
796
+ const fromAddress = await sdk.solana.getPublicKey();
492
797
  const transferInstruction = SystemProgram.transfer({
493
798
  fromPubkey: new PublicKey(fromAddress),
494
799
  toPubkey: new PublicKey(toAddress),
@@ -504,26 +809,9 @@ const messageV0 = new TransactionMessage({
504
809
 
505
810
  const transaction = new VersionedTransaction(messageV0);
506
811
 
507
- // Send native transaction object - no encoding needed!
508
- const result = await sdk.signAndSendTransaction({
509
- networkId: NetworkId.SOLANA_MAINNET,
510
- transaction: transaction,
511
- });
512
-
513
- console.log("Transaction signature:", result.rawTransaction);
514
- ```
515
-
516
- **VersionedTransaction with @solana/web3.js:**
517
-
518
- ```typescript
519
- import { VersionedTransaction } from "@solana/web3.js";
520
-
521
- const versionedTx = new VersionedTransaction(message);
522
-
523
- const result = await sdk.signAndSendTransaction({
524
- networkId: NetworkId.SOLANA_DEVNET,
525
- transaction: versionedTx,
526
- });
812
+ // Send transaction using chain-specific API
813
+ const result = await sdk.solana.signAndSendTransaction(transaction);
814
+ console.log("Transaction signature:", result.hash);
527
815
  ```
528
816
 
529
817
  #### Option 2: @solana/kit (Modern Library)
@@ -544,12 +832,21 @@ import {
544
832
  address,
545
833
  compileTransaction,
546
834
  } from "@solana/kit";
547
- import { BrowserSDK, NetworkId } from "@phantom/browser-sdk";
835
+ import { BrowserSDK, AddressType } from "@phantom/browser-sdk";
836
+
837
+ const sdk = new BrowserSDK({
838
+ providerType: "injected",
839
+ addressTypes: [AddressType.solana],
840
+ solanaProvider: "kit",
841
+ });
842
+
843
+ await sdk.connect();
548
844
 
549
845
  // Create transaction with @solana/kit
550
846
  const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");
551
847
  const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
552
848
 
849
+ const userPublicKey = await sdk.solana.getPublicKey();
553
850
  const transactionMessage = pipe(
554
851
  createTransactionMessage({ version: 0 }),
555
852
  tx => setTransactionMessageFeePayer(address(userPublicKey), tx),
@@ -558,345 +855,75 @@ const transactionMessage = pipe(
558
855
 
559
856
  const transaction = compileTransaction(transactionMessage);
560
857
 
561
- const result = await sdk.signAndSendTransaction({
562
- networkId: NetworkId.SOLANA_MAINNET,
563
- transaction: transaction,
564
- });
565
- ```
566
-
567
- ### Ethereum Transactions (with Viem)
568
-
569
- ```typescript
570
- import { parseEther, parseGwei, encodeFunctionData } from "viem";
571
- import { BrowserSDK, NetworkId } from "@phantom/browser-sdk";
572
-
573
- // Simple ETH transfer
574
- const result = await sdk.signAndSendTransaction({
575
- networkId: NetworkId.ETHEREUM_MAINNET,
576
- transaction: {
577
- to: "0x742d35Cc6634C0532925a3b8D4C8db86fB5C4A7E",
578
- value: parseEther("1"), // 1 ETH
579
- gas: 21000n,
580
- gasPrice: parseGwei("20"), // 20 gwei
581
- },
582
- });
583
-
584
- // EIP-1559 transaction with maxFeePerGas
585
- const result = await sdk.signAndSendTransaction({
586
- networkId: NetworkId.ETHEREUM_MAINNET,
587
- transaction: {
588
- to: "0x742d35Cc6634C0532925a3b8D4C8db86fB5C4A7E",
589
- value: parseEther("1"),
590
- data: encodeFunctionData({
591
- abi: tokenAbi,
592
- functionName: "transfer",
593
- args: ["0x742d35Cc6634C0532925a3b8D4C8db86fB5C4A7E", parseEther("1")],
594
- }),
595
- gas: 50000n,
596
- maxFeePerGas: parseGwei("30"), // 30 gwei
597
- maxPriorityFeePerGas: parseGwei("2"), // 2 gwei
598
- type: "eip1559",
599
- },
600
- });
601
- ```
602
-
603
- #### Other EVM Networks
604
-
605
- ```typescript
606
- // Polygon transaction
607
- const result = await sdk.signAndSendTransaction({
608
- networkId: NetworkId.POLYGON_MAINNET,
609
- transaction: {
610
- to: "0x742d35Cc6634C0532925a3b8D4C8db86fB5C4A7E",
611
- value: parseEther("1"), // 1 MATIC
612
- gas: 21000n,
613
- },
614
- });
615
-
616
- // Arbitrum transaction
617
- const result = await sdk.signAndSendTransaction({
618
- networkId: NetworkId.ARBITRUM_ONE,
619
- transaction: {
620
- to: "0x742d35Cc6634C0532925a3b8D4C8db86fB5C4A7E",
621
- value: parseEther("0.1"), // 0.1 ETH
622
- gas: 21000n,
623
- },
624
- });
858
+ // Send using chain-specific API
859
+ const result = await sdk.solana.signAndSendTransaction(transaction);
860
+ console.log("Transaction signature:", result.hash);
625
861
  ```
626
862
 
627
- ### Bitcoin Transactions
863
+ ### Ethereum Transactions
628
864
 
629
865
  ```typescript
630
- // Bitcoin transaction
631
- const result = await sdk.signAndSendTransaction({
632
- networkId: NetworkId.BITCOIN_MAINNET,
633
- transaction: {
634
- inputs: [
635
- {
636
- txid: "previous-transaction-id",
637
- vout: 0,
638
- scriptSig: "...",
639
- },
640
- ],
641
- outputs: [
642
- {
643
- value: 50000, // satoshis
644
- scriptPubKey: "76a914...88ac", // P2PKH script
645
- },
646
- ],
647
- version: 2,
648
- locktime: 0,
649
- },
650
- });
866
+ import { BrowserSDK, AddressType } from "@phantom/browser-sdk";
651
867
 
652
- // Bitcoin testnet
653
- const result = await sdk.signAndSendTransaction({
654
- networkId: NetworkId.BITCOIN_TESTNET,
655
- transaction: {
656
- // ... transaction details
657
- },
868
+ const sdk = new BrowserSDK({
869
+ providerType: "injected",
870
+ addressTypes: [AddressType.ethereum],
658
871
  });
659
- ```
660
872
 
661
- ### Sui Transactions
662
-
663
- ```typescript
664
- import { TransactionBlock } from "@mysten/sui.js/transactions";
665
-
666
- // Create Sui transaction block
667
- const txb = new TransactionBlock();
668
- txb.transferObjects([coin], recipientAddress);
873
+ await sdk.connect();
669
874
 
670
- const result = await sdk.signAndSendTransaction({
671
- networkId: NetworkId.SUI_MAINNET,
672
- transaction: {
673
- kind: "moveCall", // or 'transferObject', 'transferSui', 'pay'
674
- data: txb, // TransactionBlock from @mysten/sui.js
675
- },
875
+ // Simple ETH transfer
876
+ const result = await sdk.ethereum.sendTransaction({
877
+ to: "0x742d35Cc6634C0532925a3b8D4C8db86fB5C4A7E",
878
+ value: "1000000000000000000", // 1 ETH in wei
879
+ gas: "21000",
880
+ gasPrice: "20000000000", // 20 gwei
676
881
  });
677
882
 
678
- // Sui testnet
679
- const result = await sdk.signAndSendTransaction({
680
- networkId: NetworkId.SUI_TESTNET,
681
- transaction: {
682
- kind: "transferSui",
683
- data: suiTransactionData,
684
- },
883
+ // EIP-1559 transaction with maxFeePerGas
884
+ const result = await sdk.ethereum.sendTransaction({
885
+ to: "0x742d35Cc6634C0532925a3b8D4C8db86fB5C4A7E",
886
+ value: "1000000000000000000", // 1 ETH in wei
887
+ data: "0x...", // contract call data
888
+ gas: "50000",
889
+ maxFeePerGas: "30000000000", // 30 gwei
890
+ maxPriorityFeePerGas: "2000000000", // 2 gwei
685
891
  });
686
- ```
687
892
 
688
- ## Network IDs Reference
689
-
690
- Use the exported `NetworkId` enum for type safety:
691
-
692
- ```typescript
693
- import { NetworkId } from "@phantom/browser-sdk";
893
+ console.log("Transaction hash:", result.hash);
694
894
  ```
695
895
 
696
- ### Solana
697
-
698
- - `NetworkId.SOLANA_MAINNET` - Solana Mainnet Beta
699
- - `NetworkId.SOLANA_DEVNET` - Solana Devnet
700
- - `NetworkId.SOLANA_TESTNET` - Solana Testnet
701
-
702
- ### Ethereum/EVM
703
-
704
- - `NetworkId.ETHEREUM_MAINNET` - Ethereum Mainnet
705
- - `NetworkId.ETHEREUM_SEPOLIA` - Ethereum Sepolia Testnet
706
- - `NetworkId.POLYGON_MAINNET` - Polygon Mainnet
707
- - `NetworkId.ARBITRUM_ONE` - Arbitrum One
708
- - `NetworkId.OPTIMISM_MAINNET` - Optimism Mainnet
709
- - `NetworkId.BASE_MAINNET` - Base Mainnet
710
-
711
- ### Bitcoin
712
-
713
- - `NetworkId.BITCOIN_MAINNET` - Bitcoin Mainnet
714
- - `NetworkId.BITCOIN_TESTNET` - Bitcoin Testnet
715
-
716
- ### Sui
717
-
718
- - `NetworkId.SUI_MAINNET` - Sui Mainnet
719
- - `NetworkId.SUI_TESTNET` - Sui Testnet
720
- - `NetworkId.SUI_DEVNET` - Sui Devnet
721
-
722
- ## Advanced Usage
723
-
724
- ### Multi-Chain Application
896
+ #### Working with Viem
725
897
 
726
898
  ```typescript
899
+ import { parseEther, parseGwei, encodeFunctionData } from "viem";
727
900
  import { BrowserSDK, AddressType } from "@phantom/browser-sdk";
728
901
 
729
902
  const sdk = new BrowserSDK({
730
- addressTypes: [AddressType.solana, AddressType.ethereum, AddressType.sui],
731
- apiBaseUrl: "https://api.phantom.app/v1/wallets",
732
- organizationId: "your-org-id",
903
+ providerType: "embedded",
904
+ addressTypes: [AddressType.ethereum],
905
+ // ... config
733
906
  });
734
907
 
735
- class MultiChainWallet {
736
- async sendSolana(amount: number, recipient: string) {
737
- // Get recent blockhash
738
- const connection = new Connection("https://api.mainnet-beta.solana.com");
739
- const { blockhash } = await connection.getLatestBlockhash();
740
-
741
- // Create transfer instruction
742
- const transferInstruction = SystemProgram.transfer({
743
- fromPubkey: new PublicKey(this.solanaAddress),
744
- toPubkey: new PublicKey(recipient),
745
- lamports: amount * LAMPORTS_PER_SOL,
746
- });
747
-
748
- // Create VersionedTransaction
749
- const messageV0 = new TransactionMessage({
750
- payerKey: new PublicKey(this.solanaAddress),
751
- recentBlockhash: blockhash,
752
- instructions: [transferInstruction],
753
- }).compileToV0Message();
754
-
755
- const transaction = new VersionedTransaction(messageV0);
756
-
757
- return await sdk.signAndSendTransaction({
758
- networkId: NetworkId.SOLANA_MAINNET,
759
- transaction,
760
- });
761
- }
762
-
763
- async sendEthereum(amount: string, recipient: string) {
764
- return await sdk.signAndSendTransaction({
765
- networkId: NetworkId.ETHEREUM_MAINNET,
766
- transaction: {
767
- to: recipient,
768
- value: parseEther(amount),
769
- gas: 21000n,
770
- },
771
- });
772
- }
773
-
774
- async sendSui(coinId: string, recipient: string) {
775
- const txb = new TransactionBlock();
776
- txb.transferObjects([coinId], recipient);
777
-
778
- return await sdk.signAndSendTransaction({
779
- networkId: NetworkId.SUI_MAINNET,
780
- transaction: {
781
- kind: "transferObject",
782
- data: txb,
783
- },
784
- });
785
- }
786
- }
787
- ```
788
-
789
- ### Error Handling
908
+ // Simple transfer with viem utilities
909
+ const result = await sdk.ethereum.sendTransaction({
910
+ to: "0x742d35Cc6634C0532925a3b8D4C8db86fB5C4A7E",
911
+ value: parseEther("1").toString(), // 1 ETH
912
+ gas: "21000",
913
+ gasPrice: parseGwei("20").toString(), // 20 gwei
914
+ });
790
915
 
791
- ```typescript
792
- try {
793
- const result = await sdk.signAndSendTransaction({
794
- networkId: NetworkId.SOLANA_MAINNET,
795
- transaction: myTransaction,
796
- });
797
- console.log("Success:", result);
798
- } catch (error) {
799
- if (error.message.includes("User rejected")) {
800
- console.log("User cancelled the transaction");
801
- } else if (error.message.includes("insufficient funds")) {
802
- console.log("Not enough balance");
803
- } else {
804
- console.error("Transaction failed:", error);
805
- }
806
- }
916
+ // Contract interaction
917
+ const result = await sdk.ethereum.sendTransaction({
918
+ to: tokenContractAddress,
919
+ data: encodeFunctionData({
920
+ abi: tokenAbi,
921
+ functionName: "transfer",
922
+ args: [recipientAddress, parseEther("100")],
923
+ }),
924
+ gas: "50000",
925
+ maxFeePerGas: parseGwei("30").toString(),
926
+ maxPriorityFeePerGas: parseGwei("2").toString(),
927
+ });
807
928
  ```
808
929
 
809
- ### Bundle Optimization Tips
810
-
811
- 1. **Only include networks you need**:
812
-
813
- ```typescript
814
- // Good: Only Solana (~250KB)
815
- addressTypes: [AddressType.solana];
816
-
817
- // Avoid: All networks if not needed (~800KB+)
818
- addressTypes: [AddressType.solana, AddressType.ethereum, AddressType.sui, AddressType.bitcoinSegwit];
819
- ```
820
-
821
- 2. **Install dependencies based on enabled networks**:
822
-
823
- | AddressType | Required Dependencies |
824
- | --------------------------- | ---------------------------------- |
825
- | `AddressType.solana` | `@solana/web3.js` OR `@solana/kit` |
826
- | `AddressType.ethereum` | `viem` |
827
- | `AddressType.bitcoinSegwit` | `bitcoinjs-lib` |
828
- | `AddressType.sui` | `@mysten/sui.js` |
829
-
830
- **Example package.json for Solana + Ethereum (using @solana/web3.js)**:
831
-
832
- ```json
833
- {
834
- "dependencies": {
835
- "@phantom/browser-sdk": "^1.0.0",
836
- "@solana/web3.js": "^1.87.0",
837
- "viem": "^2.0.0"
838
- }
839
- }
840
- ```
841
-
842
- **Example package.json for Solana + Ethereum (using @solana/kit)**:
843
-
844
- ```json
845
- {
846
- "dependencies": {
847
- "@phantom/browser-sdk": "^1.0.0",
848
- "@solana/kit": "^2.0.0",
849
- "viem": "^2.0.0"
850
- }
851
- }
852
- ```
853
-
854
- **Example package.json for Solana only (using @solana/web3.js)**:
855
-
856
- ```json
857
- {
858
- "dependencies": {
859
- "@phantom/browser-sdk": "^1.0.0",
860
- "@solana/web3.js": "^1.87.0"
861
- }
862
- }
863
- ```
864
-
865
- **Example package.json for Solana only (using @solana/kit)**:
866
-
867
- ```json
868
- {
869
- "dependencies": {
870
- "@phantom/browser-sdk": "^1.0.0",
871
- "@solana/kit": "^2.0.0"
872
- }
873
- }
874
- ```
875
-
876
- **Example package.json for all networks (using @solana/web3.js)**:
877
-
878
- ```json
879
- {
880
- "dependencies": {
881
- "@phantom/browser-sdk": "^1.0.0",
882
- "@solana/web3.js": "^1.87.0",
883
- "viem": "^2.0.0",
884
- "bitcoinjs-lib": "^6.1.0",
885
- "@mysten/sui.js": "^0.50.0"
886
- }
887
- }
888
- ```
889
-
890
- **Example package.json for all networks (using @solana/kit)**:
891
-
892
- ```json
893
- {
894
- "dependencies": {
895
- "@phantom/browser-sdk": "^1.0.0",
896
- "@solana/kit": "^2.0.0",
897
- "viem": "^2.0.0",
898
- "bitcoinjs-lib": "^6.1.0",
899
- "@mysten/sui.js": "^0.50.0"
900
- }
901
- }
902
- ```