@phantom/browser-sdk 0.3.4 → 0.3.6
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 +158 -15
- package/dist/index.d.ts +55 -17
- package/dist/index.js +386 -59
- package/dist/index.mjs +386 -59
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -11,11 +11,12 @@ npm install @phantom/browser-sdk
|
|
|
11
11
|
### Injected Provider (Browser Extension)
|
|
12
12
|
|
|
13
13
|
```typescript
|
|
14
|
-
import { BrowserSDK, NetworkId } from "@phantom/browser-sdk";
|
|
14
|
+
import { BrowserSDK, NetworkId, 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
20
|
});
|
|
20
21
|
|
|
21
22
|
const { addresses } = await sdk.connect();
|
|
@@ -41,8 +42,7 @@ const sdk = new BrowserSDK({
|
|
|
41
42
|
organizationId: "your-org-id",
|
|
42
43
|
});
|
|
43
44
|
|
|
44
|
-
const {
|
|
45
|
-
console.log("Wallet ID:", walletId);
|
|
45
|
+
const { addresses } = await sdk.connect();
|
|
46
46
|
console.log("Addresses:", addresses);
|
|
47
47
|
|
|
48
48
|
const result = await sdk.signAndSendTransaction({
|
|
@@ -69,6 +69,7 @@ Uses the Phantom browser extension installed by the user. No additional configur
|
|
|
69
69
|
```typescript
|
|
70
70
|
const sdk = new BrowserSDK({
|
|
71
71
|
providerType: "injected",
|
|
72
|
+
addressTypes: [AddressType.solana],
|
|
72
73
|
});
|
|
73
74
|
```
|
|
74
75
|
|
|
@@ -89,10 +90,7 @@ const sdk = new BrowserSDK({
|
|
|
89
90
|
},
|
|
90
91
|
appName: "My DApp", // optional, for branding
|
|
91
92
|
appLogo: "https://myapp.com/logo.png", // optional, for branding
|
|
92
|
-
|
|
93
|
-
enabled: true, // optional, enable debug logging
|
|
94
|
-
level: "info", // optional, debug level
|
|
95
|
-
},
|
|
93
|
+
autoConnect: true, // optional, auto-connect to existing session (default: true for embedded)
|
|
96
94
|
});
|
|
97
95
|
```
|
|
98
96
|
|
|
@@ -162,6 +160,45 @@ const sdk = new BrowserSDK({
|
|
|
162
160
|
- **@solana/web3.js**: Better ecosystem compatibility, wider community support
|
|
163
161
|
- **@solana/kit**: Better TypeScript support, modern architecture, smaller bundle size
|
|
164
162
|
|
|
163
|
+
### Auto-Connect Feature
|
|
164
|
+
|
|
165
|
+
The SDK can automatically reconnect to existing sessions when instantiated, providing a seamless user experience.
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
const sdk = new BrowserSDK({
|
|
169
|
+
providerType: "embedded",
|
|
170
|
+
addressTypes: [AddressType.solana],
|
|
171
|
+
// ... other config
|
|
172
|
+
autoConnect: true, // Default: true for embedded, false for injected
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
// SDK will automatically check for existing valid session and connect in background
|
|
176
|
+
// No need to call connect() if user already has a session
|
|
177
|
+
|
|
178
|
+
// Check if already connected
|
|
179
|
+
if (sdk.isConnected()) {
|
|
180
|
+
console.log("Already connected!");
|
|
181
|
+
const addresses = await sdk.getAddresses();
|
|
182
|
+
} else {
|
|
183
|
+
// First time or session expired, need to connect manually
|
|
184
|
+
await sdk.connect();
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
**Disabling Auto-Connect:**
|
|
189
|
+
|
|
190
|
+
```typescript
|
|
191
|
+
const sdk = new BrowserSDK({
|
|
192
|
+
providerType: "embedded",
|
|
193
|
+
addressTypes: [AddressType.solana],
|
|
194
|
+
// ... other config
|
|
195
|
+
autoConnect: false, // Disable auto-connect
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// Now you must manually call connect() every time
|
|
199
|
+
await sdk.connect();
|
|
200
|
+
```
|
|
201
|
+
|
|
165
202
|
## API Reference
|
|
166
203
|
|
|
167
204
|
### Constructor
|
|
@@ -177,7 +214,7 @@ interface BrowserSDKConfig {
|
|
|
177
214
|
providerType: "injected" | "embedded";
|
|
178
215
|
appName?: string; // Optional app name for branding
|
|
179
216
|
appLogo?: string; // Optional app logo URL for branding
|
|
180
|
-
addressTypes?: AddressType[]; // Networks to enable (e.g., [AddressType.solana])
|
|
217
|
+
addressTypes?: [AddressType, ...AddressType[]]; // Networks to enable (e.g., [AddressType.solana])
|
|
181
218
|
|
|
182
219
|
// Required for embedded provider only
|
|
183
220
|
apiBaseUrl?: string; // Phantom API base URL
|
|
@@ -188,13 +225,7 @@ interface BrowserSDKConfig {
|
|
|
188
225
|
};
|
|
189
226
|
embeddedWalletType?: "app-wallet" | "user-wallet"; // Wallet type
|
|
190
227
|
solanaProvider?: "web3js" | "kit"; // Solana library choice (default: 'web3js')
|
|
191
|
-
|
|
192
|
-
// Debug options
|
|
193
|
-
debug?: {
|
|
194
|
-
enabled?: boolean; // Enable debug logging
|
|
195
|
-
level?: "info" | "warn" | "error"; // Debug level
|
|
196
|
-
callback?: (level: string, message: string, data?: any) => void; // Custom debug callback
|
|
197
|
-
};
|
|
228
|
+
autoConnect?: boolean; // Enable auto-connect to existing sessions (default: true)
|
|
198
229
|
}
|
|
199
230
|
```
|
|
200
231
|
|
|
@@ -316,6 +347,118 @@ Disconnect from wallet and clear session.
|
|
|
316
347
|
await sdk.disconnect();
|
|
317
348
|
```
|
|
318
349
|
|
|
350
|
+
#### autoConnect()
|
|
351
|
+
|
|
352
|
+
Attempt auto-connection using existing session. Should be called after setting up event listeners to avoid race conditions. Only works with embedded providers.
|
|
353
|
+
|
|
354
|
+
```typescript
|
|
355
|
+
await sdk.autoConnect();
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
## Debug Configuration
|
|
359
|
+
|
|
360
|
+
The BrowserSDK provides dynamic debug configuration that can be changed at runtime without reinstantiating the SDK. This provides better performance and cleaner architecture.
|
|
361
|
+
|
|
362
|
+
### Debug Methods
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
// Enable debug logging
|
|
366
|
+
sdk.enableDebug();
|
|
367
|
+
|
|
368
|
+
// Disable debug logging
|
|
369
|
+
sdk.disableDebug();
|
|
370
|
+
|
|
371
|
+
// Set debug level
|
|
372
|
+
sdk.setDebugLevel(DebugLevel.INFO);
|
|
373
|
+
|
|
374
|
+
// Set debug callback function
|
|
375
|
+
sdk.setDebugCallback((message) => {
|
|
376
|
+
console.log(`[${message.category}] ${message.message}`, message.data);
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
// Configure all debug settings at once
|
|
380
|
+
sdk.configureDebug({
|
|
381
|
+
enabled: true,
|
|
382
|
+
level: DebugLevel.DEBUG,
|
|
383
|
+
callback: (message) => {
|
|
384
|
+
// Handle debug messages
|
|
385
|
+
console.log(`[${message.level}] ${message.category}: ${message.message}`);
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
### Debug Levels
|
|
391
|
+
|
|
392
|
+
```typescript
|
|
393
|
+
import { DebugLevel } from "@phantom/browser-sdk";
|
|
394
|
+
|
|
395
|
+
// Available debug levels (in order of verbosity)
|
|
396
|
+
DebugLevel.ERROR // 0 - Only error messages
|
|
397
|
+
DebugLevel.WARN // 1 - Warning and error messages
|
|
398
|
+
DebugLevel.INFO // 2 - Info, warning, and error messages
|
|
399
|
+
DebugLevel.DEBUG // 3 - All debug messages (most verbose)
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
### Debug Message Structure
|
|
403
|
+
|
|
404
|
+
Debug callbacks receive a `DebugMessage` object:
|
|
405
|
+
|
|
406
|
+
```typescript
|
|
407
|
+
interface DebugMessage {
|
|
408
|
+
timestamp: number; // Unix timestamp
|
|
409
|
+
level: DebugLevel; // Message level
|
|
410
|
+
category: string; // Component category (e.g., "BrowserSDK", "ProviderManager")
|
|
411
|
+
message: string; // Debug message text
|
|
412
|
+
data?: any; // Additional debug data (optional)
|
|
413
|
+
}
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
### Example: Debug Console Implementation
|
|
417
|
+
|
|
418
|
+
```typescript
|
|
419
|
+
import { BrowserSDK, DebugLevel } from "@phantom/browser-sdk";
|
|
420
|
+
|
|
421
|
+
const sdk = new BrowserSDK({
|
|
422
|
+
providerType: "embedded",
|
|
423
|
+
// ... other config
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
// Store debug messages
|
|
427
|
+
const debugMessages: DebugMessage[] = [];
|
|
428
|
+
|
|
429
|
+
// Set up debug system
|
|
430
|
+
sdk.configureDebug({
|
|
431
|
+
enabled: true,
|
|
432
|
+
level: DebugLevel.INFO,
|
|
433
|
+
callback: (message) => {
|
|
434
|
+
debugMessages.push(message);
|
|
435
|
+
|
|
436
|
+
// Keep only last 100 messages
|
|
437
|
+
if (debugMessages.length > 100) {
|
|
438
|
+
debugMessages.shift();
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
// Update UI
|
|
442
|
+
updateDebugConsole();
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
// Dynamic debug level changing
|
|
447
|
+
function changeDebugLevel(newLevel: DebugLevel) {
|
|
448
|
+
sdk.setDebugLevel(newLevel);
|
|
449
|
+
console.log(`Debug level changed to: ${DebugLevel[newLevel]}`);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// Toggle debug on/off
|
|
453
|
+
function toggleDebug(enabled: boolean) {
|
|
454
|
+
if (enabled) {
|
|
455
|
+
sdk.enableDebug();
|
|
456
|
+
} else {
|
|
457
|
+
sdk.disableDebug();
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
```
|
|
461
|
+
|
|
319
462
|
## Transaction Examples
|
|
320
463
|
|
|
321
464
|
### Solana Transactions
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { EmbeddedProviderConfig, AuthOptions, ConnectResult, SignMessageParams, SignMessageResult, SignAndSendTransactionParams, SignedTransaction, WalletAddress, EmbeddedProviderEvent, EventCallback } from '@phantom/embedded-provider-core';
|
|
2
|
+
export { AuthOptions, ConnectResult, SignAndSendTransactionParams, SignMessageParams, SignMessageResult, SignedTransaction, WalletAddress } from '@phantom/embedded-provider-core';
|
|
1
3
|
import { AddressType } from '@phantom/client';
|
|
2
4
|
export { AddressType } from '@phantom/client';
|
|
3
|
-
import { AuthOptions, ConnectResult, SignMessageParams, SignMessageResult, SignAndSendTransactionParams, SignedTransaction, WalletAddress } from '@phantom/embedded-provider-core';
|
|
4
|
-
export { AuthOptions, ConnectResult, SignAndSendTransactionParams, SignMessageParams, SignMessageResult, SignedTransaction, WalletAddress } from '@phantom/embedded-provider-core';
|
|
5
5
|
export { NetworkId } from '@phantom/constants';
|
|
6
6
|
|
|
7
7
|
declare enum DebugLevel {
|
|
@@ -48,24 +48,18 @@ declare const DebugCategory: {
|
|
|
48
48
|
readonly SESSION: "Session";
|
|
49
49
|
};
|
|
50
50
|
|
|
51
|
-
interface
|
|
51
|
+
interface DebugConfig {
|
|
52
|
+
enabled?: boolean;
|
|
53
|
+
level?: DebugLevel;
|
|
54
|
+
callback?: DebugCallback;
|
|
55
|
+
}
|
|
56
|
+
interface BrowserSDKConfig extends Partial<EmbeddedProviderConfig> {
|
|
52
57
|
providerType: "injected" | "embedded" | (string & Record<never, never>);
|
|
53
|
-
|
|
54
|
-
appLogo?: string;
|
|
55
|
-
addressTypes?: AddressType[];
|
|
58
|
+
addressTypes: [AddressType, ...AddressType[]];
|
|
56
59
|
apiBaseUrl?: string;
|
|
57
60
|
organizationId?: string;
|
|
58
|
-
authOptions?: {
|
|
59
|
-
authUrl?: string;
|
|
60
|
-
redirectUrl?: string;
|
|
61
|
-
};
|
|
62
61
|
embeddedWalletType?: "app-wallet" | "user-wallet" | (string & Record<never, never>);
|
|
63
|
-
|
|
64
|
-
debug?: {
|
|
65
|
-
enabled?: boolean;
|
|
66
|
-
level?: DebugLevel;
|
|
67
|
-
callback?: DebugCallback;
|
|
68
|
-
};
|
|
62
|
+
autoConnect?: boolean;
|
|
69
63
|
}
|
|
70
64
|
|
|
71
65
|
interface Provider {
|
|
@@ -137,6 +131,50 @@ declare class BrowserSDK {
|
|
|
137
131
|
* Get the current wallet ID (for embedded wallets)
|
|
138
132
|
*/
|
|
139
133
|
getWalletId(): string | null;
|
|
134
|
+
/**
|
|
135
|
+
* Add event listener for provider events (connect, connect_start, connect_error, disconnect, error)
|
|
136
|
+
* Works with both embedded and injected providers
|
|
137
|
+
*/
|
|
138
|
+
on(event: EmbeddedProviderEvent, callback: EventCallback): void;
|
|
139
|
+
/**
|
|
140
|
+
* Remove event listener for provider events
|
|
141
|
+
* Works with both embedded and injected providers
|
|
142
|
+
*/
|
|
143
|
+
off(event: EmbeddedProviderEvent, callback: EventCallback): void;
|
|
144
|
+
/**
|
|
145
|
+
* Attempt auto-connection using existing session
|
|
146
|
+
* Should be called after setting up event listeners
|
|
147
|
+
* Only works with embedded providers
|
|
148
|
+
*/
|
|
149
|
+
autoConnect(): Promise<void>;
|
|
150
|
+
/**
|
|
151
|
+
* Debug configuration methods
|
|
152
|
+
* These allow dynamic debug configuration without SDK reinstantiation
|
|
153
|
+
*/
|
|
154
|
+
/**
|
|
155
|
+
* Enable debug logging
|
|
156
|
+
*/
|
|
157
|
+
enableDebug(): void;
|
|
158
|
+
/**
|
|
159
|
+
* Disable debug logging
|
|
160
|
+
*/
|
|
161
|
+
disableDebug(): void;
|
|
162
|
+
/**
|
|
163
|
+
* Set debug level
|
|
164
|
+
*/
|
|
165
|
+
setDebugLevel(level: DebugLevel): void;
|
|
166
|
+
/**
|
|
167
|
+
* Set debug callback function
|
|
168
|
+
*/
|
|
169
|
+
setDebugCallback(callback: DebugCallback): void;
|
|
170
|
+
/**
|
|
171
|
+
* Configure debug settings all at once
|
|
172
|
+
*/
|
|
173
|
+
configureDebug(config: {
|
|
174
|
+
enabled?: boolean;
|
|
175
|
+
level?: DebugLevel;
|
|
176
|
+
callback?: DebugCallback;
|
|
177
|
+
}): void;
|
|
140
178
|
}
|
|
141
179
|
|
|
142
180
|
/**
|
|
@@ -172,4 +210,4 @@ declare function getPlatformName(): string;
|
|
|
172
210
|
*/
|
|
173
211
|
declare function getBrowserDisplayName(): string;
|
|
174
212
|
|
|
175
|
-
export { BrowserInfo, BrowserSDK, BrowserSDKConfig, DEFAULT_AUTH_URL, DEFAULT_WALLET_API_URL, DebugCallback, DebugCategory, DebugLevel, DebugMessage, Provider, debug, detectBrowser, getBrowserDisplayName, getPlatformName, parseBrowserFromUserAgent };
|
|
213
|
+
export { BrowserInfo, BrowserSDK, BrowserSDKConfig, DEFAULT_AUTH_URL, DEFAULT_WALLET_API_URL, DebugCallback, DebugCategory, DebugConfig, DebugLevel, DebugMessage, Provider, debug, detectBrowser, getBrowserDisplayName, getPlatformName, parseBrowserFromUserAgent };
|