@phantom/browser-sdk 0.2.2 → 0.3.1
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 +109 -21
- package/dist/index.d.ts +97 -26
- package/dist/index.js +519 -124
- package/dist/index.mjs +508 -123
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -37,7 +37,7 @@ import { BrowserSDK, AddressType, NetworkId } from "@phantom/browser-sdk";
|
|
|
37
37
|
const sdk = new BrowserSDK({
|
|
38
38
|
providerType: "embedded",
|
|
39
39
|
addressTypes: [AddressType.solana, AddressType.ethereum],
|
|
40
|
-
apiBaseUrl: "https://api.phantom.
|
|
40
|
+
apiBaseUrl: "https://api.phantom.app/v1/wallets",
|
|
41
41
|
organizationId: "your-org-id",
|
|
42
42
|
});
|
|
43
43
|
|
|
@@ -80,9 +80,13 @@ Creates a non-custodial wallet embedded in your application. Requires API config
|
|
|
80
80
|
const sdk = new BrowserSDK({
|
|
81
81
|
providerType: "embedded",
|
|
82
82
|
addressTypes: [AddressType.solana, AddressType.ethereum],
|
|
83
|
-
apiBaseUrl: "https://api.phantom.
|
|
83
|
+
apiBaseUrl: "https://api.phantom.app/v1/wallets",
|
|
84
84
|
organizationId: "your-org-id",
|
|
85
85
|
embeddedWalletType: "app-wallet", // or 'user-wallet'
|
|
86
|
+
authOptions: {
|
|
87
|
+
authUrl: "https://auth.phantom.app", // optional, defaults to "https://connect.phantom.app"
|
|
88
|
+
redirectUrl: "https://yourapp.com/callback", // optional, defaults to current page
|
|
89
|
+
},
|
|
86
90
|
});
|
|
87
91
|
```
|
|
88
92
|
|
|
@@ -170,7 +174,10 @@ interface BrowserSDKConfig {
|
|
|
170
174
|
addressTypes?: AddressType[]; // Networks to enable
|
|
171
175
|
apiBaseUrl?: string; // Phantom API base URL
|
|
172
176
|
organizationId?: string; // Your organization ID
|
|
173
|
-
|
|
177
|
+
authOptions?: {
|
|
178
|
+
authUrl?: string; // Custom auth URL (default: "https://connect.phantom.app")
|
|
179
|
+
redirectUrl?: string; // Custom redirect URL after authentication
|
|
180
|
+
};
|
|
174
181
|
embeddedWalletType?: "app-wallet" | "user-wallet"; // Wallet type
|
|
175
182
|
solanaProvider?: "web3js" | "kit"; // Solana library choice (default: 'web3js')
|
|
176
183
|
}
|
|
@@ -188,6 +195,57 @@ const result = await sdk.connect();
|
|
|
188
195
|
// addresses only includes types from addressTypes config
|
|
189
196
|
```
|
|
190
197
|
|
|
198
|
+
For embedded user-wallets, you can specify authentication options:
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
// Phantom Connect with provider selection (default)
|
|
202
|
+
const result = await sdk.connect();
|
|
203
|
+
|
|
204
|
+
// Phantom Connect with Google authentication (skips provider selection)
|
|
205
|
+
const result = await sdk.connect({
|
|
206
|
+
authOptions: {
|
|
207
|
+
provider: "google",
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// Phantom Connect with Apple authentication (skips provider selection)
|
|
212
|
+
const result = await sdk.connect({
|
|
213
|
+
authOptions: {
|
|
214
|
+
provider: "apple",
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// JWT authentication (direct API call)
|
|
219
|
+
const result = await sdk.connect({
|
|
220
|
+
authOptions: {
|
|
221
|
+
provider: "jwt",
|
|
222
|
+
jwtToken: "your-jwt-token",
|
|
223
|
+
customAuthData: { userId: "user123" },
|
|
224
|
+
},
|
|
225
|
+
});
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
**Authentication Options:**
|
|
229
|
+
|
|
230
|
+
- `provider` - Authentication method: `"google"`, `"apple"`, or `"jwt"`
|
|
231
|
+
- If not specified: Shows provider selection screen on Phantom Connect
|
|
232
|
+
- If `"google"` or `"apple"`: Skips provider selection and uses specified provider
|
|
233
|
+
- If `"jwt"`: Uses JWT authentication flow via API call
|
|
234
|
+
- `jwtToken` - Required when `provider` is `"jwt"`. Your JWT token for authentication
|
|
235
|
+
- `customAuthData` - Additional data to pass to authentication service
|
|
236
|
+
|
|
237
|
+
**Authentication Flow Types:**
|
|
238
|
+
|
|
239
|
+
1. **Phantom Connect (Redirect-based)**: Used when `provider` is undefined, `"google"`, or `"apple"`
|
|
240
|
+
- Redirects to `https://connect.phantom.app` (or custom `authOptions.authUrl` from config)
|
|
241
|
+
- Handles OAuth flow with selected provider
|
|
242
|
+
- Returns to your app with authentication result using `authOptions.redirectUrl` or current page
|
|
243
|
+
|
|
244
|
+
2. **JWT Authentication (API-based)**: Used when `provider` is `"jwt"`
|
|
245
|
+
- Makes direct API call to `/api/auth/jwt` endpoint
|
|
246
|
+
- Validates JWT token server-side
|
|
247
|
+
- Returns wallet immediately without redirect
|
|
248
|
+
|
|
191
249
|
#### signAndSendTransaction(transaction)
|
|
192
250
|
|
|
193
251
|
Sign and send a native transaction object.
|
|
@@ -258,17 +316,35 @@ npm install @solana/web3.js
|
|
|
258
316
|
```
|
|
259
317
|
|
|
260
318
|
```typescript
|
|
261
|
-
import {
|
|
319
|
+
import {
|
|
320
|
+
VersionedTransaction,
|
|
321
|
+
TransactionMessage,
|
|
322
|
+
SystemProgram,
|
|
323
|
+
PublicKey,
|
|
324
|
+
LAMPORTS_PER_SOL,
|
|
325
|
+
Connection,
|
|
326
|
+
} from "@solana/web3.js";
|
|
262
327
|
import { BrowserSDK, NetworkId } from "@phantom/browser-sdk";
|
|
263
328
|
|
|
264
|
-
//
|
|
265
|
-
const
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
)
|
|
329
|
+
// Get recent blockhash
|
|
330
|
+
const connection = new Connection("https://api.mainnet-beta.solana.com");
|
|
331
|
+
const { blockhash } = await connection.getLatestBlockhash();
|
|
332
|
+
|
|
333
|
+
// Create transfer instruction
|
|
334
|
+
const transferInstruction = SystemProgram.transfer({
|
|
335
|
+
fromPubkey: new PublicKey(fromAddress),
|
|
336
|
+
toPubkey: new PublicKey(toAddress),
|
|
337
|
+
lamports: 0.001 * LAMPORTS_PER_SOL,
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
// Create VersionedTransaction
|
|
341
|
+
const messageV0 = new TransactionMessage({
|
|
342
|
+
payerKey: new PublicKey(fromAddress),
|
|
343
|
+
recentBlockhash: blockhash,
|
|
344
|
+
instructions: [transferInstruction],
|
|
345
|
+
}).compileToV0Message();
|
|
346
|
+
|
|
347
|
+
const transaction = new VersionedTransaction(messageV0);
|
|
272
348
|
|
|
273
349
|
// Send native transaction object - no encoding needed!
|
|
274
350
|
const result = await sdk.signAndSendTransaction({
|
|
@@ -494,19 +570,31 @@ import { BrowserSDK, AddressType } from "@phantom/browser-sdk";
|
|
|
494
570
|
|
|
495
571
|
const sdk = new BrowserSDK({
|
|
496
572
|
addressTypes: [AddressType.solana, AddressType.ethereum, AddressType.sui],
|
|
497
|
-
apiBaseUrl: "https://api.phantom.
|
|
573
|
+
apiBaseUrl: "https://api.phantom.app/v1/wallets",
|
|
498
574
|
organizationId: "your-org-id",
|
|
499
575
|
});
|
|
500
576
|
|
|
501
577
|
class MultiChainWallet {
|
|
502
578
|
async sendSolana(amount: number, recipient: string) {
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
579
|
+
// Get recent blockhash
|
|
580
|
+
const connection = new Connection("https://api.mainnet-beta.solana.com");
|
|
581
|
+
const { blockhash } = await connection.getLatestBlockhash();
|
|
582
|
+
|
|
583
|
+
// Create transfer instruction
|
|
584
|
+
const transferInstruction = SystemProgram.transfer({
|
|
585
|
+
fromPubkey: new PublicKey(this.solanaAddress),
|
|
586
|
+
toPubkey: new PublicKey(recipient),
|
|
587
|
+
lamports: amount * LAMPORTS_PER_SOL,
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
// Create VersionedTransaction
|
|
591
|
+
const messageV0 = new TransactionMessage({
|
|
592
|
+
payerKey: new PublicKey(this.solanaAddress),
|
|
593
|
+
recentBlockhash: blockhash,
|
|
594
|
+
instructions: [transferInstruction],
|
|
595
|
+
}).compileToV0Message();
|
|
596
|
+
|
|
597
|
+
const transaction = new VersionedTransaction(messageV0);
|
|
510
598
|
|
|
511
599
|
return await sdk.signAndSendTransaction({
|
|
512
600
|
networkId: NetworkId.SOLANA_MAINNET,
|
|
@@ -669,7 +757,7 @@ For embedded wallets, you need to set up a backend endpoint. Add the `serverUrl`
|
|
|
669
757
|
const sdk = new BrowserSDK({
|
|
670
758
|
providerType: "embedded",
|
|
671
759
|
addressTypes: [AddressType.solana],
|
|
672
|
-
apiBaseUrl: "https://api.phantom.
|
|
760
|
+
apiBaseUrl: "https://api.phantom.app/v1/wallets",
|
|
673
761
|
organizationId: "your-org-id",
|
|
674
762
|
serverUrl: "http://localhost:3000/api",
|
|
675
763
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,52 @@
|
|
|
1
|
-
import { AddressType
|
|
2
|
-
export { AddressType
|
|
1
|
+
import { AddressType } from '@phantom/client';
|
|
2
|
+
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
|
+
export { NetworkId } from '@phantom/constants';
|
|
6
|
+
|
|
7
|
+
declare enum DebugLevel {
|
|
8
|
+
ERROR = 0,
|
|
9
|
+
WARN = 1,
|
|
10
|
+
INFO = 2,
|
|
11
|
+
DEBUG = 3
|
|
12
|
+
}
|
|
13
|
+
interface DebugMessage {
|
|
14
|
+
timestamp: number;
|
|
15
|
+
level: DebugLevel;
|
|
16
|
+
category: string;
|
|
17
|
+
message: string;
|
|
18
|
+
data?: any;
|
|
19
|
+
}
|
|
20
|
+
type DebugCallback = (message: DebugMessage) => void;
|
|
21
|
+
declare class Debug {
|
|
22
|
+
private static instance;
|
|
23
|
+
private callback?;
|
|
24
|
+
private level;
|
|
25
|
+
private enabled;
|
|
26
|
+
private constructor();
|
|
27
|
+
static getInstance(): Debug;
|
|
28
|
+
setCallback(callback: DebugCallback): void;
|
|
29
|
+
setLevel(level: DebugLevel): void;
|
|
30
|
+
enable(): void;
|
|
31
|
+
disable(): void;
|
|
32
|
+
private writeLog;
|
|
33
|
+
error(category: string, message: string, data?: any): void;
|
|
34
|
+
warn(category: string, message: string, data?: any): void;
|
|
35
|
+
info(category: string, message: string, data?: any): void;
|
|
36
|
+
debug(category: string, message: string, data?: any): void;
|
|
37
|
+
log(category: string, message: string, data?: any): void;
|
|
38
|
+
}
|
|
39
|
+
declare const debug: Debug;
|
|
40
|
+
declare const DebugCategory: {
|
|
41
|
+
readonly BROWSER_SDK: "BrowserSDK";
|
|
42
|
+
readonly PROVIDER_MANAGER: "ProviderManager";
|
|
43
|
+
readonly EMBEDDED_PROVIDER: "EmbeddedProvider";
|
|
44
|
+
readonly INJECTED_PROVIDER: "InjectedProvider";
|
|
45
|
+
readonly PHANTOM_CONNECT_AUTH: "PhantomConnectAuth";
|
|
46
|
+
readonly JWT_AUTH: "JWTAuth";
|
|
47
|
+
readonly STORAGE: "Storage";
|
|
48
|
+
readonly SESSION: "Session";
|
|
49
|
+
};
|
|
3
50
|
|
|
4
51
|
interface BrowserSDKConfig {
|
|
5
52
|
providerType: "injected" | "embedded" | (string & Record<never, never>);
|
|
@@ -7,29 +54,18 @@ interface BrowserSDKConfig {
|
|
|
7
54
|
addressTypes?: AddressType[];
|
|
8
55
|
apiBaseUrl?: string;
|
|
9
56
|
organizationId?: string;
|
|
10
|
-
|
|
57
|
+
authOptions?: {
|
|
58
|
+
authUrl?: string;
|
|
59
|
+
redirectUrl?: string;
|
|
60
|
+
};
|
|
11
61
|
embeddedWalletType?: "app-wallet" | "user-wallet" | (string & Record<never, never>);
|
|
12
62
|
solanaProvider?: "web3js" | "kit";
|
|
13
63
|
serverUrl?: string;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
interface ConnectResult {
|
|
20
|
-
walletId?: string;
|
|
21
|
-
addresses: WalletAddress[];
|
|
22
|
-
}
|
|
23
|
-
interface SignMessageParams {
|
|
24
|
-
message: string;
|
|
25
|
-
networkId: NetworkId;
|
|
26
|
-
}
|
|
27
|
-
interface SignAndSendTransactionParams {
|
|
28
|
-
transaction: any;
|
|
29
|
-
networkId: NetworkId;
|
|
30
|
-
}
|
|
31
|
-
interface SignedTransaction {
|
|
32
|
-
rawTransaction: string;
|
|
64
|
+
debug?: {
|
|
65
|
+
enabled?: boolean;
|
|
66
|
+
level?: DebugLevel;
|
|
67
|
+
callback?: DebugCallback;
|
|
68
|
+
};
|
|
33
69
|
}
|
|
34
70
|
interface CreateUserOrganizationParams {
|
|
35
71
|
userId: string;
|
|
@@ -38,10 +74,11 @@ interface CreateUserOrganizationParams {
|
|
|
38
74
|
interface CreateUserOrganizationResult {
|
|
39
75
|
organizationId: string;
|
|
40
76
|
}
|
|
77
|
+
|
|
41
78
|
interface Provider {
|
|
42
|
-
connect(): Promise<ConnectResult>;
|
|
79
|
+
connect(authOptions?: AuthOptions): Promise<ConnectResult>;
|
|
43
80
|
disconnect(): Promise<void>;
|
|
44
|
-
signMessage(params: SignMessageParams): Promise<
|
|
81
|
+
signMessage(params: SignMessageParams): Promise<SignMessageResult>;
|
|
45
82
|
signAndSendTransaction(params: SignAndSendTransactionParams): Promise<SignedTransaction>;
|
|
46
83
|
getAddresses(): WalletAddress[];
|
|
47
84
|
isConnected(): boolean;
|
|
@@ -65,6 +102,7 @@ declare class BrowserSDK {
|
|
|
65
102
|
connect(options?: {
|
|
66
103
|
providerType?: "injected" | "embedded" | (string & Record<never, never>);
|
|
67
104
|
embeddedWalletType?: "app-wallet" | "user-wallet" | (string & Record<never, never>);
|
|
105
|
+
authOptions?: AuthOptions;
|
|
68
106
|
}): Promise<ConnectResult>;
|
|
69
107
|
/**
|
|
70
108
|
* Switch to a different provider type
|
|
@@ -88,7 +126,7 @@ declare class BrowserSDK {
|
|
|
88
126
|
* @param networkId - Network identifier
|
|
89
127
|
* @returns Signature string
|
|
90
128
|
*/
|
|
91
|
-
signMessage(params: SignMessageParams): Promise<
|
|
129
|
+
signMessage(params: SignMessageParams): Promise<SignMessageResult>;
|
|
92
130
|
/**
|
|
93
131
|
* Sign and send a transaction
|
|
94
132
|
* @param params - Transaction parameters with native transaction object
|
|
@@ -115,4 +153,37 @@ declare class BrowserSDK {
|
|
|
115
153
|
createUserOrganization(params: CreateUserOrganizationParams): Promise<CreateUserOrganizationResult>;
|
|
116
154
|
}
|
|
117
155
|
|
|
118
|
-
|
|
156
|
+
/**
|
|
157
|
+
* Constants used throughout the browser SDK
|
|
158
|
+
*/
|
|
159
|
+
declare const DEFAULT_AUTH_URL = "https://connect.phantom.app";
|
|
160
|
+
declare const DEFAULT_WALLET_API_URL = "https://api.phantom.app/v1/wallets";
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Browser detection utility to identify browser name and version
|
|
164
|
+
*/
|
|
165
|
+
interface BrowserInfo {
|
|
166
|
+
name: string;
|
|
167
|
+
version: string;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Parse browser information from a user agent string
|
|
171
|
+
* This is the core parsing logic that can be unit tested independently
|
|
172
|
+
*/
|
|
173
|
+
declare function parseBrowserFromUserAgent(userAgent: string, hasBraveAPI?: boolean): BrowserInfo;
|
|
174
|
+
/**
|
|
175
|
+
* Detect the current browser and version from the user agent string
|
|
176
|
+
*/
|
|
177
|
+
declare function detectBrowser(): BrowserInfo;
|
|
178
|
+
/**
|
|
179
|
+
* Get a formatted platform name for use in authenticator names
|
|
180
|
+
* Format: "browsername-v123" (e.g., "chrome-v120", "firefox-v119")
|
|
181
|
+
*/
|
|
182
|
+
declare function getPlatformName(): string;
|
|
183
|
+
/**
|
|
184
|
+
* Get detailed browser information as a string
|
|
185
|
+
* Format: "Chrome 120.0" or "Firefox 119.0"
|
|
186
|
+
*/
|
|
187
|
+
declare function getBrowserDisplayName(): string;
|
|
188
|
+
|
|
189
|
+
export { BrowserInfo, BrowserSDK, BrowserSDKConfig, CreateUserOrganizationParams, CreateUserOrganizationResult, DEFAULT_AUTH_URL, DEFAULT_WALLET_API_URL, DebugCallback, DebugCategory, DebugLevel, DebugMessage, Provider, debug, detectBrowser, getBrowserDisplayName, getPlatformName, parseBrowserFromUserAgent };
|