@phantom/browser-sdk 1.0.0-beta.14 → 1.0.0-beta.16
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 +142 -11
- package/dist/index.d.ts +45 -10
- package/dist/index.js +385 -192
- package/dist/index.mjs +385 -192
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -93,24 +93,36 @@ const ethResult = await sdk.ethereum.sendTransaction({
|
|
|
93
93
|
|
|
94
94
|
### Connection Options
|
|
95
95
|
|
|
96
|
-
|
|
96
|
+
The `connect()` method automatically switches between providers based on the authentication method you specify:
|
|
97
97
|
|
|
98
98
|
```typescript
|
|
99
|
-
//
|
|
99
|
+
// Connect with current provider (no switching)
|
|
100
100
|
const result = await sdk.connect();
|
|
101
101
|
|
|
102
|
-
//
|
|
102
|
+
// Connect with injected provider (Phantom extension)
|
|
103
|
+
// Automatically switches to injected provider if not already using it
|
|
103
104
|
const result = await sdk.connect({
|
|
104
|
-
|
|
105
|
-
provider: "google",
|
|
106
|
-
},
|
|
105
|
+
provider: "injected",
|
|
107
106
|
});
|
|
108
107
|
|
|
109
|
-
//
|
|
108
|
+
// Connect with Google authentication (embedded provider)
|
|
109
|
+
// Automatically switches to embedded provider if not already using it
|
|
110
110
|
const result = await sdk.connect({
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
111
|
+
provider: "google",
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// Connect with Apple authentication (embedded provider)
|
|
115
|
+
// Automatically switches to embedded provider if not already using it
|
|
116
|
+
const result = await sdk.connect({
|
|
117
|
+
provider: "apple",
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
// Connect with Phantom authentication (embedded provider)
|
|
122
|
+
// Uses Phantom extension or mobile app for authentication
|
|
123
|
+
// Automatically switches to embedded provider if not already using it
|
|
124
|
+
const result = await sdk.connect({
|
|
125
|
+
provider: "phantom",
|
|
114
126
|
});
|
|
115
127
|
```
|
|
116
128
|
|
|
@@ -293,7 +305,9 @@ interface BrowserSDKConfig {
|
|
|
293
305
|
|
|
294
306
|
### Extension Detection
|
|
295
307
|
|
|
296
|
-
|
|
308
|
+
#### waitForPhantomExtension
|
|
309
|
+
|
|
310
|
+
Check if the Phantom extension is installed:
|
|
297
311
|
|
|
298
312
|
```typescript
|
|
299
313
|
import { waitForPhantomExtension } from "@phantom/browser-sdk";
|
|
@@ -307,6 +321,23 @@ if (isAvailable) {
|
|
|
307
321
|
}
|
|
308
322
|
```
|
|
309
323
|
|
|
324
|
+
#### isPhantomLoginAvailable
|
|
325
|
+
|
|
326
|
+
Check if Phantom Login is available (requires extension to be installed and support the `phantom_login` feature):
|
|
327
|
+
|
|
328
|
+
```typescript
|
|
329
|
+
import { isPhantomLoginAvailable } from "@phantom/browser-sdk";
|
|
330
|
+
|
|
331
|
+
const isAvailable = await isPhantomLoginAvailable();
|
|
332
|
+
|
|
333
|
+
if (isAvailable) {
|
|
334
|
+
console.log("Phantom Login is available!");
|
|
335
|
+
// Can use provider: "phantom" in connect()
|
|
336
|
+
} else {
|
|
337
|
+
console.log("Phantom Login is not available");
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
310
341
|
### Core Methods
|
|
311
342
|
|
|
312
343
|
#### connect()
|
|
@@ -540,6 +571,106 @@ Attempt auto-connection using existing session. Should be called after setting u
|
|
|
540
571
|
await sdk.autoConnect();
|
|
541
572
|
```
|
|
542
573
|
|
|
574
|
+
### Event Handlers
|
|
575
|
+
|
|
576
|
+
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.
|
|
577
|
+
|
|
578
|
+
#### Available Events
|
|
579
|
+
|
|
580
|
+
```typescript
|
|
581
|
+
import { BrowserSDK } from '@phantom/browser-sdk';
|
|
582
|
+
import type {
|
|
583
|
+
ConnectEventData,
|
|
584
|
+
ConnectStartEventData,
|
|
585
|
+
ConnectErrorEventData,
|
|
586
|
+
DisconnectEventData
|
|
587
|
+
} from '@phantom/browser-sdk';
|
|
588
|
+
|
|
589
|
+
const sdk = new BrowserSDK({
|
|
590
|
+
providerType: 'embedded',
|
|
591
|
+
appId: 'your-app-id',
|
|
592
|
+
addressTypes: [AddressType.solana],
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
// 1. connect_start - Fired when connection starts
|
|
596
|
+
sdk.on('connect_start', (data: ConnectStartEventData) => {
|
|
597
|
+
console.log('Connection starting:', data.source); // "auto-connect" | "manual-connect"
|
|
598
|
+
console.log('Auth options:', data.authOptions?.provider); // "google" | "apple" | etc.
|
|
599
|
+
});
|
|
600
|
+
|
|
601
|
+
// 2. connect - Fired when connection succeeds (includes full ConnectResult)
|
|
602
|
+
sdk.on('connect', (data: ConnectEventData) => {
|
|
603
|
+
console.log('Connected successfully!');
|
|
604
|
+
console.log('Provider type:', data.providerType); // "embedded" | "injected"
|
|
605
|
+
console.log('Wallet ID:', data.walletId); // only for embedded providers
|
|
606
|
+
console.log('Addresses:', data.addresses); // WalletAddress[]
|
|
607
|
+
console.log('Status:', data.status); // "pending" | "completed"
|
|
608
|
+
console.log('Source:', data.source); // "auto-connect" | "manual-connect" | "manual-existing" | "existing-session" | "manual"
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
// 3. connect_error - Fired when connection fails
|
|
612
|
+
sdk.on('connect_error', (data: ConnectErrorEventData) => {
|
|
613
|
+
console.error('Connection failed:', data.error);
|
|
614
|
+
console.log('Source:', data.source); // "auto-connect" | "manual-connect"
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
// 4. disconnect - Fired when disconnected
|
|
618
|
+
sdk.on('disconnect', (data: DisconnectEventData) => {
|
|
619
|
+
console.log('Disconnected from wallet');
|
|
620
|
+
console.log('Source:', data.source); // "manual"
|
|
621
|
+
});
|
|
622
|
+
|
|
623
|
+
// 5. error - General error handler
|
|
624
|
+
sdk.on('error', (error: unknown) => {
|
|
625
|
+
console.error('SDK error:', error);
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
// Don't forget to remove listeners when done
|
|
629
|
+
sdk.off('connect', handleConnect);
|
|
630
|
+
```
|
|
631
|
+
|
|
632
|
+
#### Event Types
|
|
633
|
+
|
|
634
|
+
| Event | Payload Type | When Fired | Key Data |
|
|
635
|
+
|-------|-------------|------------|----------|
|
|
636
|
+
| `connect_start` | `ConnectStartEventData` | Connection initiated | `source`, `authOptions` |
|
|
637
|
+
| `connect` | `ConnectEventData` | Connection successful | `providerType`, `addresses`, `status`, `source`, `user`|
|
|
638
|
+
| `connect_error` | `ConnectErrorEventData` | Connection failed | `error`, `source` |
|
|
639
|
+
| `disconnect` | `DisconnectEventData` | Disconnected | `source` |
|
|
640
|
+
| `error` | `unknown` | General SDK errors | Error details |
|
|
641
|
+
|
|
642
|
+
#### Using Events with autoConnect()
|
|
643
|
+
|
|
644
|
+
Event handlers are especially useful with `autoConnect()` since it doesn't return a value:
|
|
645
|
+
|
|
646
|
+
```typescript
|
|
647
|
+
const sdk = new BrowserSDK({
|
|
648
|
+
providerType: 'embedded',
|
|
649
|
+
appId: 'your-app-id',
|
|
650
|
+
addressTypes: [AddressType.solana],
|
|
651
|
+
autoConnect: true,
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
// Set up event listeners BEFORE autoConnect
|
|
655
|
+
sdk.on('connect', (data: ConnectEventData) => {
|
|
656
|
+
console.log('Auto-connected successfully!');
|
|
657
|
+
console.log('Provider type:', data.providerType);
|
|
658
|
+
console.log('Addresses:', data.addresses);
|
|
659
|
+
|
|
660
|
+
// Update your UI state here
|
|
661
|
+
updateUIWithAddresses(data.addresses);
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
sdk.on('connect_error', (data: ConnectErrorEventData) => {
|
|
665
|
+
console.log('Auto-connect failed:', data.error);
|
|
666
|
+
// Show connect button to user
|
|
667
|
+
showConnectButton();
|
|
668
|
+
});
|
|
669
|
+
|
|
670
|
+
// Auto-connect will trigger events
|
|
671
|
+
await sdk.autoConnect();
|
|
672
|
+
```
|
|
673
|
+
|
|
543
674
|
### Auto-Confirm Methods (Injected Provider Only)
|
|
544
675
|
|
|
545
676
|
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).
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EmbeddedProviderConfig, AuthOptions, ConnectResult, WalletAddress, EmbeddedProviderEvent, EventCallback } from '@phantom/embedded-provider-core';
|
|
2
|
-
export { AuthOptions, ConnectResult, SignAndSendTransactionParams, SignMessageParams, SignMessageResult, SignedTransaction, WalletAddress } from '@phantom/embedded-provider-core';
|
|
2
|
+
export { AuthOptions, ConnectErrorEventData, ConnectEventData, ConnectResult, ConnectStartEventData, DisconnectEventData, EmbeddedProviderEvent, EmbeddedProviderEventMap, EventCallback, SignAndSendTransactionParams, SignMessageParams, SignMessageResult, SignedTransaction, WalletAddress } from '@phantom/embedded-provider-core';
|
|
3
3
|
import { ISolanaChain, IEthereumChain } from '@phantom/chain-interfaces';
|
|
4
4
|
export { EthTransactionRequest, IEthereumChain, ISolanaChain } from '@phantom/chain-interfaces';
|
|
5
5
|
import { AddressType } from '@phantom/client';
|
|
@@ -52,14 +52,34 @@ declare const DebugCategory: {
|
|
|
52
52
|
readonly SESSION: "Session";
|
|
53
53
|
};
|
|
54
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Phantom extension app.login API types
|
|
57
|
+
*/
|
|
58
|
+
interface PhantomAppLoginOptions {
|
|
59
|
+
publicKey: string;
|
|
60
|
+
appId: string;
|
|
61
|
+
sessionId: string;
|
|
62
|
+
}
|
|
63
|
+
interface PhantomAppLoginResult {
|
|
64
|
+
walletId: string;
|
|
65
|
+
organizationId: string;
|
|
66
|
+
accountDerivationIndex?: number;
|
|
67
|
+
expiresInMs?: number;
|
|
68
|
+
}
|
|
69
|
+
interface PhantomApp {
|
|
70
|
+
login(options: PhantomAppLoginOptions): Promise<PhantomAppLoginResult>;
|
|
71
|
+
features(): Promise<string[]>;
|
|
72
|
+
}
|
|
55
73
|
declare global {
|
|
56
74
|
interface Window {
|
|
57
75
|
phantom?: {
|
|
58
76
|
solana?: unknown;
|
|
59
77
|
ethereum?: unknown;
|
|
60
|
-
|
|
78
|
+
app?: PhantomApp;
|
|
79
|
+
} | undefined;
|
|
61
80
|
}
|
|
62
81
|
}
|
|
82
|
+
|
|
63
83
|
interface InjectedProviderConfig {
|
|
64
84
|
addressTypes: AddressType[];
|
|
65
85
|
}
|
|
@@ -105,9 +125,6 @@ interface ProviderPreference {
|
|
|
105
125
|
type: "injected" | "embedded";
|
|
106
126
|
embeddedWalletType?: "app-wallet" | "user-wallet";
|
|
107
127
|
}
|
|
108
|
-
interface SwitchProviderOptions {
|
|
109
|
-
embeddedWalletType?: "app-wallet" | "user-wallet" | (string & Record<never, never>);
|
|
110
|
-
}
|
|
111
128
|
|
|
112
129
|
/**
|
|
113
130
|
* Browser SDK with chain-specific API
|
|
@@ -141,10 +158,6 @@ declare class BrowserSDK {
|
|
|
141
158
|
* Disconnect from the wallet
|
|
142
159
|
*/
|
|
143
160
|
disconnect(): Promise<void>;
|
|
144
|
-
/**
|
|
145
|
-
* Switch between provider types (injected vs embedded)
|
|
146
|
-
*/
|
|
147
|
-
switchProvider(type: "injected" | "embedded", options?: SwitchProviderOptions): Promise<void>;
|
|
148
161
|
/**
|
|
149
162
|
* Check if the SDK is connected to a wallet
|
|
150
163
|
*/
|
|
@@ -285,4 +298,26 @@ declare function getDeeplinkToPhantom(ref?: string): string;
|
|
|
285
298
|
*/
|
|
286
299
|
declare function waitForPhantomExtension(timeoutMs?: number): Promise<boolean>;
|
|
287
300
|
|
|
288
|
-
|
|
301
|
+
/**
|
|
302
|
+
* Check if Phantom Login is available
|
|
303
|
+
*
|
|
304
|
+
* This function checks if:
|
|
305
|
+
* 1. The Phantom extension is installed
|
|
306
|
+
* 2. The extension supports the phantom_login feature
|
|
307
|
+
*
|
|
308
|
+
* @param timeoutMs - Maximum time to wait for extension in milliseconds (default: 3000)
|
|
309
|
+
* @returns Promise<boolean> - true if Phantom Login is available, false otherwise
|
|
310
|
+
*
|
|
311
|
+
* Usage:
|
|
312
|
+
* ```typescript
|
|
313
|
+
* const isAvailable = await isPhantomLoginAvailable();
|
|
314
|
+
* if (isAvailable) {
|
|
315
|
+
* console.log("Phantom Login is available!");
|
|
316
|
+
* } else {
|
|
317
|
+
* console.log("Phantom Login is not available");
|
|
318
|
+
* }
|
|
319
|
+
* ```
|
|
320
|
+
*/
|
|
321
|
+
declare function isPhantomLoginAvailable(timeoutMs?: number): Promise<boolean>;
|
|
322
|
+
|
|
323
|
+
export { BrowserInfo, BrowserSDK, BrowserSDKConfig, DebugCallback, DebugCategory, DebugConfig, DebugLevel, DebugMessage, Provider, debug, detectBrowser, getBrowserDisplayName, getDeeplinkToPhantom, getPlatformName, isMobileDevice, isPhantomLoginAvailable, parseBrowserFromUserAgent, waitForPhantomExtension };
|