@phantom/browser-sdk 1.0.0-beta.19 → 1.0.0-beta.20
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 +31 -15
- package/dist/index.d.ts +4 -1
- package/dist/index.js +98 -40
- package/dist/index.mjs +98 -40
- package/package.json +3 -3
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
|
|
@@ -47,7 +47,7 @@ const sdk = new BrowserSDK({
|
|
|
47
47
|
appId: "your-app-id", // Get your app ID from phantom.com/portal
|
|
48
48
|
});
|
|
49
49
|
|
|
50
|
-
const { addresses } = await sdk.connect();
|
|
50
|
+
const { addresses } = await sdk.connect({ provider: "phantom" });
|
|
51
51
|
console.log("Addresses:", addresses);
|
|
52
52
|
|
|
53
53
|
// Use chain-specific APIs
|
|
@@ -78,8 +78,8 @@ const sdk = new BrowserSDK({
|
|
|
78
78
|
addressTypes: [AddressType.solana, AddressType.ethereum],
|
|
79
79
|
});
|
|
80
80
|
|
|
81
|
-
// 2. Connect to wallet
|
|
82
|
-
const { addresses } = await sdk.connect();
|
|
81
|
+
// 2. Connect to wallet (provider parameter is required)
|
|
82
|
+
const { addresses } = await sdk.connect({ provider: "injected" });
|
|
83
83
|
console.log("Connected addresses:", addresses);
|
|
84
84
|
|
|
85
85
|
// 3. Use chain-specific methods
|
|
@@ -93,12 +93,9 @@ const ethResult = await sdk.ethereum.sendTransaction({
|
|
|
93
93
|
|
|
94
94
|
### Connection Options
|
|
95
95
|
|
|
96
|
-
The `connect()` method automatically switches between providers based on the authentication method you specify:
|
|
96
|
+
The `connect()` method requires a `provider` parameter and automatically switches between providers based on the authentication method you specify:
|
|
97
97
|
|
|
98
98
|
```typescript
|
|
99
|
-
// Connect with current provider (no switching)
|
|
100
|
-
const result = await sdk.connect();
|
|
101
|
-
|
|
102
99
|
// Connect with injected provider (Phantom extension)
|
|
103
100
|
// Automatically switches to injected provider if not already using it
|
|
104
101
|
const result = await sdk.connect({
|
|
@@ -117,13 +114,18 @@ const result = await sdk.connect({
|
|
|
117
114
|
provider: "apple",
|
|
118
115
|
});
|
|
119
116
|
|
|
120
|
-
|
|
121
117
|
// Connect with Phantom authentication (embedded provider)
|
|
122
118
|
// Uses Phantom extension or mobile app for authentication
|
|
123
119
|
// Automatically switches to embedded provider if not already using it
|
|
124
120
|
const result = await sdk.connect({
|
|
125
121
|
provider: "phantom",
|
|
126
122
|
});
|
|
123
|
+
|
|
124
|
+
// Connect with JWT authentication (embedded provider)
|
|
125
|
+
const result = await sdk.connect({
|
|
126
|
+
provider: "jwt",
|
|
127
|
+
jwtToken: "your-jwt-token",
|
|
128
|
+
});
|
|
127
129
|
```
|
|
128
130
|
|
|
129
131
|
## Chain-Specific APIs
|
|
@@ -340,13 +342,27 @@ if (isAvailable) {
|
|
|
340
342
|
|
|
341
343
|
### Core Methods
|
|
342
344
|
|
|
343
|
-
#### connect()
|
|
345
|
+
#### connect(options)
|
|
344
346
|
|
|
345
347
|
Connect to wallet and get addresses for configured AddressTypes.
|
|
346
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
|
+
|
|
347
355
|
```typescript
|
|
348
|
-
|
|
349
|
-
|
|
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" }
|
|
350
366
|
// addresses only includes types from addressTypes config
|
|
351
367
|
```
|
|
352
368
|
|
|
@@ -898,7 +914,7 @@ const sdk = new BrowserSDK({
|
|
|
898
914
|
addressTypes: [AddressType.solana],
|
|
899
915
|
});
|
|
900
916
|
|
|
901
|
-
await sdk.connect();
|
|
917
|
+
await sdk.connect({ provider: "injected" });
|
|
902
918
|
|
|
903
919
|
// Get recent blockhash
|
|
904
920
|
const connection = new Connection("https://api.mainnet-beta.solana.com");
|
|
@@ -951,7 +967,7 @@ const sdk = new BrowserSDK({
|
|
|
951
967
|
addressTypes: [AddressType.solana],
|
|
952
968
|
});
|
|
953
969
|
|
|
954
|
-
await sdk.connect();
|
|
970
|
+
await sdk.connect({ provider: "injected" });
|
|
955
971
|
|
|
956
972
|
// Create transaction with @solana/kit
|
|
957
973
|
const rpc = createSolanaRpc("https://api.mainnet-beta.solana.com");
|
|
@@ -981,7 +997,7 @@ const sdk = new BrowserSDK({
|
|
|
981
997
|
addressTypes: [AddressType.ethereum],
|
|
982
998
|
});
|
|
983
999
|
|
|
984
|
-
await sdk.connect();
|
|
1000
|
+
await sdk.connect({ provider: "injected" });
|
|
985
1001
|
|
|
986
1002
|
// Simple ETH transfer
|
|
987
1003
|
const result = await sdk.ethereum.sendTransaction({
|
package/dist/index.d.ts
CHANGED
|
@@ -72,6 +72,9 @@ interface PhantomApp {
|
|
|
72
72
|
features(): Promise<{
|
|
73
73
|
features: string[];
|
|
74
74
|
}>;
|
|
75
|
+
getUser(): Promise<{
|
|
76
|
+
authUserId?: string;
|
|
77
|
+
} | undefined>;
|
|
75
78
|
}
|
|
76
79
|
declare global {
|
|
77
80
|
interface Window {
|
|
@@ -156,7 +159,7 @@ declare class BrowserSDK {
|
|
|
156
159
|
/**
|
|
157
160
|
* Connect to the wallet
|
|
158
161
|
*/
|
|
159
|
-
connect(options
|
|
162
|
+
connect(options: AuthOptions): Promise<ConnectResult>;
|
|
160
163
|
/**
|
|
161
164
|
* Disconnect from the wallet
|
|
162
165
|
*/
|
package/dist/index.js
CHANGED
|
@@ -384,6 +384,8 @@ var InjectedEthereumChain = class {
|
|
|
384
384
|
};
|
|
385
385
|
|
|
386
386
|
// src/providers/injected/index.ts
|
|
387
|
+
var MANUAL_DISCONNECT_KEY = "phantom-injected-manual-disconnect";
|
|
388
|
+
var MANUAL_DISCONNECT_VALUE = "true";
|
|
387
389
|
var InjectedProvider = class {
|
|
388
390
|
constructor(config) {
|
|
389
391
|
this.connected = false;
|
|
@@ -446,9 +448,11 @@ var InjectedProvider = class {
|
|
|
446
448
|
async connect(authOptions) {
|
|
447
449
|
debug.info(DebugCategory.INJECTED_PROVIDER, "Starting injected provider connect", {
|
|
448
450
|
addressTypes: this.addressTypes,
|
|
449
|
-
|
|
450
|
-
// Note: authOptions are ignored for injected provider
|
|
451
|
+
provider: authOptions.provider
|
|
451
452
|
});
|
|
453
|
+
if (authOptions.provider !== "injected") {
|
|
454
|
+
throw new Error(`Invalid provider for injected connection: ${authOptions.provider}. Must be "injected"`);
|
|
455
|
+
}
|
|
452
456
|
this.emit("connect_start", {
|
|
453
457
|
source: "manual-connect",
|
|
454
458
|
providerType: "injected"
|
|
@@ -516,13 +520,22 @@ var InjectedProvider = class {
|
|
|
516
520
|
}
|
|
517
521
|
this.addresses = connectedAddresses;
|
|
518
522
|
this.connected = true;
|
|
523
|
+
const authUserId = await this.getAuthUserId("manual-connect");
|
|
524
|
+
try {
|
|
525
|
+
localStorage.removeItem(MANUAL_DISCONNECT_KEY);
|
|
526
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Cleared manual disconnect flag - auto-reconnect enabled");
|
|
527
|
+
} catch (error) {
|
|
528
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to clear manual disconnect flag", { error });
|
|
529
|
+
}
|
|
519
530
|
const result = {
|
|
520
531
|
addresses: this.addresses,
|
|
521
|
-
status: "completed"
|
|
532
|
+
status: "completed",
|
|
533
|
+
authUserId
|
|
522
534
|
};
|
|
523
535
|
this.emit("connect", {
|
|
524
536
|
addresses: this.addresses,
|
|
525
|
-
source: "manual-connect"
|
|
537
|
+
source: "manual-connect",
|
|
538
|
+
authUserId
|
|
526
539
|
});
|
|
527
540
|
return result;
|
|
528
541
|
} catch (error) {
|
|
@@ -552,6 +565,12 @@ var InjectedProvider = class {
|
|
|
552
565
|
this.browserInjectedCleanupFunctions = [];
|
|
553
566
|
this.connected = false;
|
|
554
567
|
this.addresses = [];
|
|
568
|
+
try {
|
|
569
|
+
localStorage.setItem(MANUAL_DISCONNECT_KEY, MANUAL_DISCONNECT_VALUE);
|
|
570
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Set manual disconnect flag to prevent auto-reconnect");
|
|
571
|
+
} catch (error) {
|
|
572
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to set manual disconnect flag", { error });
|
|
573
|
+
}
|
|
555
574
|
this.emit("disconnect", {
|
|
556
575
|
source: "manual-disconnect"
|
|
557
576
|
});
|
|
@@ -564,6 +583,15 @@ var InjectedProvider = class {
|
|
|
564
583
|
*/
|
|
565
584
|
async autoConnect() {
|
|
566
585
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Attempting auto-connect with onlyIfTrusted=true");
|
|
586
|
+
try {
|
|
587
|
+
const manualDisconnect = localStorage.getItem(MANUAL_DISCONNECT_KEY);
|
|
588
|
+
if (manualDisconnect === MANUAL_DISCONNECT_VALUE) {
|
|
589
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Skipping auto-connect: user previously disconnected manually");
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
} catch (error) {
|
|
593
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to check manual disconnect flag", { error });
|
|
594
|
+
}
|
|
567
595
|
this.emit("connect_start", {
|
|
568
596
|
source: "auto-connect",
|
|
569
597
|
providerType: "injected"
|
|
@@ -620,13 +648,16 @@ var InjectedProvider = class {
|
|
|
620
648
|
}
|
|
621
649
|
this.addresses = connectedAddresses;
|
|
622
650
|
this.connected = true;
|
|
651
|
+
const authUserId = await this.getAuthUserId("auto-connect");
|
|
623
652
|
this.emit("connect", {
|
|
624
653
|
addresses: this.addresses,
|
|
625
|
-
source: "auto-connect"
|
|
654
|
+
source: "auto-connect",
|
|
655
|
+
authUserId
|
|
626
656
|
});
|
|
627
657
|
debug.info(DebugCategory.INJECTED_PROVIDER, "Auto-connect successful", {
|
|
628
658
|
addressCount: connectedAddresses.length,
|
|
629
|
-
addresses: connectedAddresses.map((addr) => ({ type: addr.addressType, address: addr.address.substring(0, 8) + "..." }))
|
|
659
|
+
addresses: connectedAddresses.map((addr) => ({ type: addr.addressType, address: addr.address.substring(0, 8) + "..." })),
|
|
660
|
+
authUserId
|
|
630
661
|
});
|
|
631
662
|
} catch (error) {
|
|
632
663
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Auto-connect failed with error", {
|
|
@@ -661,6 +692,27 @@ var InjectedProvider = class {
|
|
|
661
692
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Getting supported autoConfirm chains");
|
|
662
693
|
return await this.phantom.autoConfirm.autoConfirmSupportedChains();
|
|
663
694
|
}
|
|
695
|
+
/**
|
|
696
|
+
* Helper method to get authUserId from window.phantom.app.getUser()
|
|
697
|
+
* Returns undefined if the method is not available or fails
|
|
698
|
+
*/
|
|
699
|
+
async getAuthUserId(context) {
|
|
700
|
+
try {
|
|
701
|
+
if (window.phantom?.app?.getUser) {
|
|
702
|
+
const userInfo = await window.phantom.app.getUser();
|
|
703
|
+
const authUserId = userInfo?.authUserId;
|
|
704
|
+
if (authUserId) {
|
|
705
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, `Retrieved authUserId from window.phantom.app.getUser() during ${context}`, {
|
|
706
|
+
authUserId
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
return authUserId;
|
|
710
|
+
}
|
|
711
|
+
} catch (error) {
|
|
712
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, `Failed to get user info during ${context} (method may not be supported)`, { error });
|
|
713
|
+
}
|
|
714
|
+
return void 0;
|
|
715
|
+
}
|
|
664
716
|
// Event management methods - implementing unified event interface
|
|
665
717
|
on(event, callback) {
|
|
666
718
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Adding event listener", { event });
|
|
@@ -710,16 +762,18 @@ var InjectedProvider = class {
|
|
|
710
762
|
}
|
|
711
763
|
setupSolanaEvents() {
|
|
712
764
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Setting up Solana event listeners");
|
|
713
|
-
const handleSolanaConnect = (publicKey) => {
|
|
765
|
+
const handleSolanaConnect = async (publicKey) => {
|
|
714
766
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Solana connect event received", { publicKey });
|
|
715
767
|
const solanaAddress = { addressType: import_client4.AddressType.solana, address: publicKey };
|
|
716
768
|
if (!this.addresses.find((addr) => addr.addressType === import_client4.AddressType.solana)) {
|
|
717
769
|
this.addresses.push(solanaAddress);
|
|
718
770
|
}
|
|
719
771
|
this.connected = true;
|
|
772
|
+
const authUserId = await this.getAuthUserId("Solana connect event");
|
|
720
773
|
this.emit("connect", {
|
|
721
774
|
addresses: this.addresses,
|
|
722
|
-
source: "injected-extension"
|
|
775
|
+
source: "injected-extension",
|
|
776
|
+
authUserId
|
|
723
777
|
});
|
|
724
778
|
};
|
|
725
779
|
const handleSolanaDisconnect = () => {
|
|
@@ -730,7 +784,7 @@ var InjectedProvider = class {
|
|
|
730
784
|
source: "injected-extension"
|
|
731
785
|
});
|
|
732
786
|
};
|
|
733
|
-
const handleSolanaAccountChanged = (publicKey) => {
|
|
787
|
+
const handleSolanaAccountChanged = async (publicKey) => {
|
|
734
788
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Solana account changed event received", { publicKey });
|
|
735
789
|
const solanaIndex = this.addresses.findIndex((addr) => addr.addressType === import_client4.AddressType.solana);
|
|
736
790
|
if (solanaIndex >= 0) {
|
|
@@ -738,9 +792,11 @@ var InjectedProvider = class {
|
|
|
738
792
|
} else {
|
|
739
793
|
this.addresses.push({ addressType: import_client4.AddressType.solana, address: publicKey });
|
|
740
794
|
}
|
|
795
|
+
const authUserId = await this.getAuthUserId("Solana account changed event");
|
|
741
796
|
this.emit("connect", {
|
|
742
797
|
addresses: this.addresses,
|
|
743
|
-
source: "injected-extension-account-change"
|
|
798
|
+
source: "injected-extension-account-change",
|
|
799
|
+
authUserId
|
|
744
800
|
});
|
|
745
801
|
};
|
|
746
802
|
const cleanupConnect = this.phantom.solana.addEventListener("connect", handleSolanaConnect);
|
|
@@ -750,7 +806,7 @@ var InjectedProvider = class {
|
|
|
750
806
|
}
|
|
751
807
|
setupEthereumEvents() {
|
|
752
808
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Setting up Ethereum event listeners");
|
|
753
|
-
const handleEthereumConnect = (accounts) => {
|
|
809
|
+
const handleEthereumConnect = async (accounts) => {
|
|
754
810
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Ethereum connect event received", { accounts });
|
|
755
811
|
this.addresses = this.addresses.filter((addr) => addr.addressType !== import_client4.AddressType.ethereum);
|
|
756
812
|
if (accounts && accounts.length > 0) {
|
|
@@ -762,9 +818,11 @@ var InjectedProvider = class {
|
|
|
762
818
|
);
|
|
763
819
|
}
|
|
764
820
|
this.connected = this.addresses.length > 0;
|
|
821
|
+
const authUserId = await this.getAuthUserId("Ethereum connect event");
|
|
765
822
|
this.emit("connect", {
|
|
766
823
|
addresses: this.addresses,
|
|
767
|
-
source: "injected-extension"
|
|
824
|
+
source: "injected-extension",
|
|
825
|
+
authUserId
|
|
768
826
|
});
|
|
769
827
|
};
|
|
770
828
|
const handleEthereumDisconnect = () => {
|
|
@@ -775,7 +833,7 @@ var InjectedProvider = class {
|
|
|
775
833
|
source: "injected-extension"
|
|
776
834
|
});
|
|
777
835
|
};
|
|
778
|
-
const handleEthereumAccountsChanged = (accounts) => {
|
|
836
|
+
const handleEthereumAccountsChanged = async (accounts) => {
|
|
779
837
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Ethereum accounts changed event received", { accounts });
|
|
780
838
|
this.addresses = this.addresses.filter((addr) => addr.addressType !== import_client4.AddressType.ethereum);
|
|
781
839
|
if (accounts && accounts.length > 0) {
|
|
@@ -785,9 +843,11 @@ var InjectedProvider = class {
|
|
|
785
843
|
address
|
|
786
844
|
}))
|
|
787
845
|
);
|
|
846
|
+
const authUserId = await this.getAuthUserId("Ethereum accounts changed event");
|
|
788
847
|
this.emit("connect", {
|
|
789
848
|
addresses: this.addresses,
|
|
790
|
-
source: "injected-extension-account-change"
|
|
849
|
+
source: "injected-extension-account-change",
|
|
850
|
+
authUserId
|
|
791
851
|
});
|
|
792
852
|
} else {
|
|
793
853
|
this.connected = false;
|
|
@@ -807,7 +867,7 @@ var InjectedProvider = class {
|
|
|
807
867
|
createCallbacks() {
|
|
808
868
|
return {
|
|
809
869
|
connect: async () => {
|
|
810
|
-
const result = await this.connect();
|
|
870
|
+
const result = await this.connect({ provider: "injected" });
|
|
811
871
|
return result.addresses;
|
|
812
872
|
},
|
|
813
873
|
disconnect: async () => {
|
|
@@ -1140,7 +1200,7 @@ var BrowserAuthProvider = class {
|
|
|
1140
1200
|
// OAuth session management - defaults to allow refresh unless explicitly clearing after logout
|
|
1141
1201
|
clear_previous_session: (phantomOptions.clearPreviousSession ?? false).toString(),
|
|
1142
1202
|
allow_refresh: (phantomOptions.allowRefresh ?? true).toString(),
|
|
1143
|
-
sdk_version: "1.0.0-beta.
|
|
1203
|
+
sdk_version: "1.0.0-beta.20",
|
|
1144
1204
|
sdk_type: "browser",
|
|
1145
1205
|
platform: detectBrowser().name
|
|
1146
1206
|
});
|
|
@@ -1399,7 +1459,7 @@ var EmbeddedProvider = class extends import_embedded_provider_core.EmbeddedProvi
|
|
|
1399
1459
|
// Full user agent for more detailed info
|
|
1400
1460
|
[import_constants2.ANALYTICS_HEADERS.APP_ID]: config.appId,
|
|
1401
1461
|
[import_constants2.ANALYTICS_HEADERS.WALLET_TYPE]: config.embeddedWalletType,
|
|
1402
|
-
[import_constants2.ANALYTICS_HEADERS.SDK_VERSION]: "1.0.0-beta.
|
|
1462
|
+
[import_constants2.ANALYTICS_HEADERS.SDK_VERSION]: "1.0.0-beta.20"
|
|
1403
1463
|
// Replaced at build time
|
|
1404
1464
|
}
|
|
1405
1465
|
};
|
|
@@ -1496,35 +1556,33 @@ var ProviderManager = class {
|
|
|
1496
1556
|
}
|
|
1497
1557
|
/**
|
|
1498
1558
|
* Connect using the current provider
|
|
1499
|
-
* Automatically switches provider based on authOptions.provider
|
|
1559
|
+
* Automatically switches provider based on authOptions.provider
|
|
1500
1560
|
*/
|
|
1501
1561
|
async connect(authOptions) {
|
|
1502
1562
|
debug.info(DebugCategory.PROVIDER_MANAGER, "Starting connection", {
|
|
1503
1563
|
currentProviderKey: this.currentProviderKey,
|
|
1504
|
-
authOptions:
|
|
1564
|
+
authOptions: { provider: authOptions.provider, hasJwtToken: !!authOptions.jwtToken }
|
|
1505
1565
|
});
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
switchOptions.embeddedWalletType = currentInfo?.embeddedWalletType || this.config.embeddedWalletType;
|
|
1525
|
-
}
|
|
1526
|
-
this.switchProvider(targetProviderType, switchOptions);
|
|
1566
|
+
const requestedProvider = authOptions.provider;
|
|
1567
|
+
let targetProviderType = null;
|
|
1568
|
+
if (requestedProvider === "injected") {
|
|
1569
|
+
targetProviderType = "injected";
|
|
1570
|
+
} else if (["google", "apple", "jwt", "phantom"].includes(requestedProvider)) {
|
|
1571
|
+
targetProviderType = "embedded";
|
|
1572
|
+
}
|
|
1573
|
+
if (targetProviderType) {
|
|
1574
|
+
const currentInfo = this.getCurrentProviderInfo();
|
|
1575
|
+
if (currentInfo?.type !== targetProviderType) {
|
|
1576
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Auto-switching provider based on auth options", {
|
|
1577
|
+
from: currentInfo?.type,
|
|
1578
|
+
to: targetProviderType,
|
|
1579
|
+
requestedProvider
|
|
1580
|
+
});
|
|
1581
|
+
const switchOptions = {};
|
|
1582
|
+
if (targetProviderType === "embedded") {
|
|
1583
|
+
switchOptions.embeddedWalletType = currentInfo?.embeddedWalletType || this.config.embeddedWalletType;
|
|
1527
1584
|
}
|
|
1585
|
+
this.switchProvider(targetProviderType, switchOptions);
|
|
1528
1586
|
}
|
|
1529
1587
|
}
|
|
1530
1588
|
if (!this.currentProvider) {
|
package/dist/index.mjs
CHANGED
|
@@ -347,6 +347,8 @@ var InjectedEthereumChain = class {
|
|
|
347
347
|
};
|
|
348
348
|
|
|
349
349
|
// src/providers/injected/index.ts
|
|
350
|
+
var MANUAL_DISCONNECT_KEY = "phantom-injected-manual-disconnect";
|
|
351
|
+
var MANUAL_DISCONNECT_VALUE = "true";
|
|
350
352
|
var InjectedProvider = class {
|
|
351
353
|
constructor(config) {
|
|
352
354
|
this.connected = false;
|
|
@@ -409,9 +411,11 @@ var InjectedProvider = class {
|
|
|
409
411
|
async connect(authOptions) {
|
|
410
412
|
debug.info(DebugCategory.INJECTED_PROVIDER, "Starting injected provider connect", {
|
|
411
413
|
addressTypes: this.addressTypes,
|
|
412
|
-
|
|
413
|
-
// Note: authOptions are ignored for injected provider
|
|
414
|
+
provider: authOptions.provider
|
|
414
415
|
});
|
|
416
|
+
if (authOptions.provider !== "injected") {
|
|
417
|
+
throw new Error(`Invalid provider for injected connection: ${authOptions.provider}. Must be "injected"`);
|
|
418
|
+
}
|
|
415
419
|
this.emit("connect_start", {
|
|
416
420
|
source: "manual-connect",
|
|
417
421
|
providerType: "injected"
|
|
@@ -479,13 +483,22 @@ var InjectedProvider = class {
|
|
|
479
483
|
}
|
|
480
484
|
this.addresses = connectedAddresses;
|
|
481
485
|
this.connected = true;
|
|
486
|
+
const authUserId = await this.getAuthUserId("manual-connect");
|
|
487
|
+
try {
|
|
488
|
+
localStorage.removeItem(MANUAL_DISCONNECT_KEY);
|
|
489
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Cleared manual disconnect flag - auto-reconnect enabled");
|
|
490
|
+
} catch (error) {
|
|
491
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to clear manual disconnect flag", { error });
|
|
492
|
+
}
|
|
482
493
|
const result = {
|
|
483
494
|
addresses: this.addresses,
|
|
484
|
-
status: "completed"
|
|
495
|
+
status: "completed",
|
|
496
|
+
authUserId
|
|
485
497
|
};
|
|
486
498
|
this.emit("connect", {
|
|
487
499
|
addresses: this.addresses,
|
|
488
|
-
source: "manual-connect"
|
|
500
|
+
source: "manual-connect",
|
|
501
|
+
authUserId
|
|
489
502
|
});
|
|
490
503
|
return result;
|
|
491
504
|
} catch (error) {
|
|
@@ -515,6 +528,12 @@ var InjectedProvider = class {
|
|
|
515
528
|
this.browserInjectedCleanupFunctions = [];
|
|
516
529
|
this.connected = false;
|
|
517
530
|
this.addresses = [];
|
|
531
|
+
try {
|
|
532
|
+
localStorage.setItem(MANUAL_DISCONNECT_KEY, MANUAL_DISCONNECT_VALUE);
|
|
533
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Set manual disconnect flag to prevent auto-reconnect");
|
|
534
|
+
} catch (error) {
|
|
535
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to set manual disconnect flag", { error });
|
|
536
|
+
}
|
|
518
537
|
this.emit("disconnect", {
|
|
519
538
|
source: "manual-disconnect"
|
|
520
539
|
});
|
|
@@ -527,6 +546,15 @@ var InjectedProvider = class {
|
|
|
527
546
|
*/
|
|
528
547
|
async autoConnect() {
|
|
529
548
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Attempting auto-connect with onlyIfTrusted=true");
|
|
549
|
+
try {
|
|
550
|
+
const manualDisconnect = localStorage.getItem(MANUAL_DISCONNECT_KEY);
|
|
551
|
+
if (manualDisconnect === MANUAL_DISCONNECT_VALUE) {
|
|
552
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Skipping auto-connect: user previously disconnected manually");
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
} catch (error) {
|
|
556
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to check manual disconnect flag", { error });
|
|
557
|
+
}
|
|
530
558
|
this.emit("connect_start", {
|
|
531
559
|
source: "auto-connect",
|
|
532
560
|
providerType: "injected"
|
|
@@ -583,13 +611,16 @@ var InjectedProvider = class {
|
|
|
583
611
|
}
|
|
584
612
|
this.addresses = connectedAddresses;
|
|
585
613
|
this.connected = true;
|
|
614
|
+
const authUserId = await this.getAuthUserId("auto-connect");
|
|
586
615
|
this.emit("connect", {
|
|
587
616
|
addresses: this.addresses,
|
|
588
|
-
source: "auto-connect"
|
|
617
|
+
source: "auto-connect",
|
|
618
|
+
authUserId
|
|
589
619
|
});
|
|
590
620
|
debug.info(DebugCategory.INJECTED_PROVIDER, "Auto-connect successful", {
|
|
591
621
|
addressCount: connectedAddresses.length,
|
|
592
|
-
addresses: connectedAddresses.map((addr) => ({ type: addr.addressType, address: addr.address.substring(0, 8) + "..." }))
|
|
622
|
+
addresses: connectedAddresses.map((addr) => ({ type: addr.addressType, address: addr.address.substring(0, 8) + "..." })),
|
|
623
|
+
authUserId
|
|
593
624
|
});
|
|
594
625
|
} catch (error) {
|
|
595
626
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Auto-connect failed with error", {
|
|
@@ -624,6 +655,27 @@ var InjectedProvider = class {
|
|
|
624
655
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Getting supported autoConfirm chains");
|
|
625
656
|
return await this.phantom.autoConfirm.autoConfirmSupportedChains();
|
|
626
657
|
}
|
|
658
|
+
/**
|
|
659
|
+
* Helper method to get authUserId from window.phantom.app.getUser()
|
|
660
|
+
* Returns undefined if the method is not available or fails
|
|
661
|
+
*/
|
|
662
|
+
async getAuthUserId(context) {
|
|
663
|
+
try {
|
|
664
|
+
if (window.phantom?.app?.getUser) {
|
|
665
|
+
const userInfo = await window.phantom.app.getUser();
|
|
666
|
+
const authUserId = userInfo?.authUserId;
|
|
667
|
+
if (authUserId) {
|
|
668
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, `Retrieved authUserId from window.phantom.app.getUser() during ${context}`, {
|
|
669
|
+
authUserId
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
return authUserId;
|
|
673
|
+
}
|
|
674
|
+
} catch (error) {
|
|
675
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, `Failed to get user info during ${context} (method may not be supported)`, { error });
|
|
676
|
+
}
|
|
677
|
+
return void 0;
|
|
678
|
+
}
|
|
627
679
|
// Event management methods - implementing unified event interface
|
|
628
680
|
on(event, callback) {
|
|
629
681
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Adding event listener", { event });
|
|
@@ -673,16 +725,18 @@ var InjectedProvider = class {
|
|
|
673
725
|
}
|
|
674
726
|
setupSolanaEvents() {
|
|
675
727
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Setting up Solana event listeners");
|
|
676
|
-
const handleSolanaConnect = (publicKey) => {
|
|
728
|
+
const handleSolanaConnect = async (publicKey) => {
|
|
677
729
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Solana connect event received", { publicKey });
|
|
678
730
|
const solanaAddress = { addressType: AddressType4.solana, address: publicKey };
|
|
679
731
|
if (!this.addresses.find((addr) => addr.addressType === AddressType4.solana)) {
|
|
680
732
|
this.addresses.push(solanaAddress);
|
|
681
733
|
}
|
|
682
734
|
this.connected = true;
|
|
735
|
+
const authUserId = await this.getAuthUserId("Solana connect event");
|
|
683
736
|
this.emit("connect", {
|
|
684
737
|
addresses: this.addresses,
|
|
685
|
-
source: "injected-extension"
|
|
738
|
+
source: "injected-extension",
|
|
739
|
+
authUserId
|
|
686
740
|
});
|
|
687
741
|
};
|
|
688
742
|
const handleSolanaDisconnect = () => {
|
|
@@ -693,7 +747,7 @@ var InjectedProvider = class {
|
|
|
693
747
|
source: "injected-extension"
|
|
694
748
|
});
|
|
695
749
|
};
|
|
696
|
-
const handleSolanaAccountChanged = (publicKey) => {
|
|
750
|
+
const handleSolanaAccountChanged = async (publicKey) => {
|
|
697
751
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Solana account changed event received", { publicKey });
|
|
698
752
|
const solanaIndex = this.addresses.findIndex((addr) => addr.addressType === AddressType4.solana);
|
|
699
753
|
if (solanaIndex >= 0) {
|
|
@@ -701,9 +755,11 @@ var InjectedProvider = class {
|
|
|
701
755
|
} else {
|
|
702
756
|
this.addresses.push({ addressType: AddressType4.solana, address: publicKey });
|
|
703
757
|
}
|
|
758
|
+
const authUserId = await this.getAuthUserId("Solana account changed event");
|
|
704
759
|
this.emit("connect", {
|
|
705
760
|
addresses: this.addresses,
|
|
706
|
-
source: "injected-extension-account-change"
|
|
761
|
+
source: "injected-extension-account-change",
|
|
762
|
+
authUserId
|
|
707
763
|
});
|
|
708
764
|
};
|
|
709
765
|
const cleanupConnect = this.phantom.solana.addEventListener("connect", handleSolanaConnect);
|
|
@@ -713,7 +769,7 @@ var InjectedProvider = class {
|
|
|
713
769
|
}
|
|
714
770
|
setupEthereumEvents() {
|
|
715
771
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Setting up Ethereum event listeners");
|
|
716
|
-
const handleEthereumConnect = (accounts) => {
|
|
772
|
+
const handleEthereumConnect = async (accounts) => {
|
|
717
773
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Ethereum connect event received", { accounts });
|
|
718
774
|
this.addresses = this.addresses.filter((addr) => addr.addressType !== AddressType4.ethereum);
|
|
719
775
|
if (accounts && accounts.length > 0) {
|
|
@@ -725,9 +781,11 @@ var InjectedProvider = class {
|
|
|
725
781
|
);
|
|
726
782
|
}
|
|
727
783
|
this.connected = this.addresses.length > 0;
|
|
784
|
+
const authUserId = await this.getAuthUserId("Ethereum connect event");
|
|
728
785
|
this.emit("connect", {
|
|
729
786
|
addresses: this.addresses,
|
|
730
|
-
source: "injected-extension"
|
|
787
|
+
source: "injected-extension",
|
|
788
|
+
authUserId
|
|
731
789
|
});
|
|
732
790
|
};
|
|
733
791
|
const handleEthereumDisconnect = () => {
|
|
@@ -738,7 +796,7 @@ var InjectedProvider = class {
|
|
|
738
796
|
source: "injected-extension"
|
|
739
797
|
});
|
|
740
798
|
};
|
|
741
|
-
const handleEthereumAccountsChanged = (accounts) => {
|
|
799
|
+
const handleEthereumAccountsChanged = async (accounts) => {
|
|
742
800
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Ethereum accounts changed event received", { accounts });
|
|
743
801
|
this.addresses = this.addresses.filter((addr) => addr.addressType !== AddressType4.ethereum);
|
|
744
802
|
if (accounts && accounts.length > 0) {
|
|
@@ -748,9 +806,11 @@ var InjectedProvider = class {
|
|
|
748
806
|
address
|
|
749
807
|
}))
|
|
750
808
|
);
|
|
809
|
+
const authUserId = await this.getAuthUserId("Ethereum accounts changed event");
|
|
751
810
|
this.emit("connect", {
|
|
752
811
|
addresses: this.addresses,
|
|
753
|
-
source: "injected-extension-account-change"
|
|
812
|
+
source: "injected-extension-account-change",
|
|
813
|
+
authUserId
|
|
754
814
|
});
|
|
755
815
|
} else {
|
|
756
816
|
this.connected = false;
|
|
@@ -770,7 +830,7 @@ var InjectedProvider = class {
|
|
|
770
830
|
createCallbacks() {
|
|
771
831
|
return {
|
|
772
832
|
connect: async () => {
|
|
773
|
-
const result = await this.connect();
|
|
833
|
+
const result = await this.connect({ provider: "injected" });
|
|
774
834
|
return result.addresses;
|
|
775
835
|
},
|
|
776
836
|
disconnect: async () => {
|
|
@@ -1103,7 +1163,7 @@ var BrowserAuthProvider = class {
|
|
|
1103
1163
|
// OAuth session management - defaults to allow refresh unless explicitly clearing after logout
|
|
1104
1164
|
clear_previous_session: (phantomOptions.clearPreviousSession ?? false).toString(),
|
|
1105
1165
|
allow_refresh: (phantomOptions.allowRefresh ?? true).toString(),
|
|
1106
|
-
sdk_version: "1.0.0-beta.
|
|
1166
|
+
sdk_version: "1.0.0-beta.20",
|
|
1107
1167
|
sdk_type: "browser",
|
|
1108
1168
|
platform: detectBrowser().name
|
|
1109
1169
|
});
|
|
@@ -1362,7 +1422,7 @@ var EmbeddedProvider = class extends CoreEmbeddedProvider {
|
|
|
1362
1422
|
// Full user agent for more detailed info
|
|
1363
1423
|
[ANALYTICS_HEADERS.APP_ID]: config.appId,
|
|
1364
1424
|
[ANALYTICS_HEADERS.WALLET_TYPE]: config.embeddedWalletType,
|
|
1365
|
-
[ANALYTICS_HEADERS.SDK_VERSION]: "1.0.0-beta.
|
|
1425
|
+
[ANALYTICS_HEADERS.SDK_VERSION]: "1.0.0-beta.20"
|
|
1366
1426
|
// Replaced at build time
|
|
1367
1427
|
}
|
|
1368
1428
|
};
|
|
@@ -1459,35 +1519,33 @@ var ProviderManager = class {
|
|
|
1459
1519
|
}
|
|
1460
1520
|
/**
|
|
1461
1521
|
* Connect using the current provider
|
|
1462
|
-
* Automatically switches provider based on authOptions.provider
|
|
1522
|
+
* Automatically switches provider based on authOptions.provider
|
|
1463
1523
|
*/
|
|
1464
1524
|
async connect(authOptions) {
|
|
1465
1525
|
debug.info(DebugCategory.PROVIDER_MANAGER, "Starting connection", {
|
|
1466
1526
|
currentProviderKey: this.currentProviderKey,
|
|
1467
|
-
authOptions:
|
|
1527
|
+
authOptions: { provider: authOptions.provider, hasJwtToken: !!authOptions.jwtToken }
|
|
1468
1528
|
});
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
switchOptions.embeddedWalletType = currentInfo?.embeddedWalletType || this.config.embeddedWalletType;
|
|
1488
|
-
}
|
|
1489
|
-
this.switchProvider(targetProviderType, switchOptions);
|
|
1529
|
+
const requestedProvider = authOptions.provider;
|
|
1530
|
+
let targetProviderType = null;
|
|
1531
|
+
if (requestedProvider === "injected") {
|
|
1532
|
+
targetProviderType = "injected";
|
|
1533
|
+
} else if (["google", "apple", "jwt", "phantom"].includes(requestedProvider)) {
|
|
1534
|
+
targetProviderType = "embedded";
|
|
1535
|
+
}
|
|
1536
|
+
if (targetProviderType) {
|
|
1537
|
+
const currentInfo = this.getCurrentProviderInfo();
|
|
1538
|
+
if (currentInfo?.type !== targetProviderType) {
|
|
1539
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Auto-switching provider based on auth options", {
|
|
1540
|
+
from: currentInfo?.type,
|
|
1541
|
+
to: targetProviderType,
|
|
1542
|
+
requestedProvider
|
|
1543
|
+
});
|
|
1544
|
+
const switchOptions = {};
|
|
1545
|
+
if (targetProviderType === "embedded") {
|
|
1546
|
+
switchOptions.embeddedWalletType = currentInfo?.embeddedWalletType || this.config.embeddedWalletType;
|
|
1490
1547
|
}
|
|
1548
|
+
this.switchProvider(targetProviderType, switchOptions);
|
|
1491
1549
|
}
|
|
1492
1550
|
}
|
|
1493
1551
|
if (!this.currentProvider) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@phantom/browser-sdk",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.20",
|
|
4
4
|
"description": "Browser SDK for Phantom Wallet",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -31,9 +31,9 @@
|
|
|
31
31
|
"@phantom/base64url": "^1.0.0-beta.8",
|
|
32
32
|
"@phantom/browser-injected-sdk": "^1.0.0-beta.6",
|
|
33
33
|
"@phantom/chain-interfaces": "^1.0.0-beta.8",
|
|
34
|
-
"@phantom/client": "^1.0.0-beta.
|
|
34
|
+
"@phantom/client": "^1.0.0-beta.20",
|
|
35
35
|
"@phantom/constants": "^1.0.0-beta.8",
|
|
36
|
-
"@phantom/embedded-provider-core": "^1.0.0-beta.
|
|
36
|
+
"@phantom/embedded-provider-core": "^1.0.0-beta.20",
|
|
37
37
|
"@phantom/indexed-db-stamper": "^1.0.0-beta.2",
|
|
38
38
|
"@phantom/parsers": "^1.0.0-beta.8",
|
|
39
39
|
"@phantom/sdk-types": "^1.0.0-beta.8",
|