@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/dist/index.js
CHANGED
|
@@ -30,7 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
|
-
AddressType: () =>
|
|
33
|
+
AddressType: () => import_client3.AddressType,
|
|
34
34
|
BrowserSDK: () => BrowserSDK,
|
|
35
35
|
DEFAULT_AUTH_URL: () => DEFAULT_AUTH_URL,
|
|
36
36
|
DEFAULT_WALLET_API_URL: () => DEFAULT_WALLET_API_URL,
|
|
@@ -133,8 +133,12 @@ var InjectedProvider = class {
|
|
|
133
133
|
constructor(config) {
|
|
134
134
|
this.connected = false;
|
|
135
135
|
this.addresses = [];
|
|
136
|
+
// Event management
|
|
137
|
+
this.eventListeners = /* @__PURE__ */ new Map();
|
|
138
|
+
this.browserInjectedCleanupFunctions = [];
|
|
139
|
+
this.eventsInitialized = false;
|
|
136
140
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Initializing InjectedProvider", { config });
|
|
137
|
-
this.addressTypes = config.addressTypes
|
|
141
|
+
this.addressTypes = config.addressTypes;
|
|
138
142
|
debug.log(DebugCategory.INJECTED_PROVIDER, "Address types configured", { addressTypes: this.addressTypes });
|
|
139
143
|
const plugins = [(0, import_browser_injected_sdk.createExtensionPlugin)()];
|
|
140
144
|
if (this.addressTypes.includes(import_client.AddressType.solana)) {
|
|
@@ -157,70 +161,108 @@ var InjectedProvider = class {
|
|
|
157
161
|
authOptionsIgnored: !!authOptions
|
|
158
162
|
// Note: authOptions are ignored for injected provider
|
|
159
163
|
});
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
164
|
+
this.emit("connect_start", {
|
|
165
|
+
source: "manual-connect",
|
|
166
|
+
providerType: "injected"
|
|
167
|
+
});
|
|
168
|
+
try {
|
|
169
|
+
if (!this.phantom.extension.isInstalled()) {
|
|
170
|
+
debug.error(DebugCategory.INJECTED_PROVIDER, "Phantom wallet extension not found");
|
|
171
|
+
const error = new Error("Phantom wallet not found");
|
|
172
|
+
this.emit("connect_error", {
|
|
173
|
+
error: error.message,
|
|
174
|
+
source: "manual-connect"
|
|
175
|
+
});
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
178
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Phantom extension detected");
|
|
179
|
+
const connectedAddresses = [];
|
|
180
|
+
if (this.addressTypes.includes(import_client.AddressType.solana)) {
|
|
181
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Attempting Solana connection");
|
|
182
|
+
try {
|
|
183
|
+
const publicKey = await this.phantom.solana.connect();
|
|
184
|
+
if (publicKey) {
|
|
185
|
+
connectedAddresses.push({
|
|
186
|
+
addressType: import_client.AddressType.solana,
|
|
187
|
+
address: publicKey
|
|
188
|
+
});
|
|
189
|
+
debug.info(DebugCategory.INJECTED_PROVIDER, "Solana connected successfully", { address: publicKey });
|
|
190
|
+
}
|
|
191
|
+
} catch (err) {
|
|
192
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to connect Solana", { error: err });
|
|
176
193
|
}
|
|
177
|
-
} catch (err) {
|
|
178
|
-
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to connect Solana", { error: err });
|
|
179
194
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
195
|
+
if (this.addressTypes.includes(import_client.AddressType.ethereum)) {
|
|
196
|
+
try {
|
|
197
|
+
const accounts = await this.phantom.ethereum.connect();
|
|
198
|
+
if (accounts && accounts.length > 0) {
|
|
199
|
+
connectedAddresses.push(
|
|
200
|
+
...accounts.map((address) => ({
|
|
201
|
+
addressType: import_client.AddressType.ethereum,
|
|
202
|
+
address
|
|
203
|
+
}))
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
} catch (err) {
|
|
207
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to connect Ethereum", { error: err });
|
|
191
208
|
}
|
|
192
|
-
} catch (err) {
|
|
193
|
-
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to connect Ethereum", { error: err });
|
|
194
209
|
}
|
|
210
|
+
if (connectedAddresses.length === 0) {
|
|
211
|
+
const error = new Error("Failed to connect to any supported wallet provider");
|
|
212
|
+
this.emit("connect_error", {
|
|
213
|
+
error: error.message,
|
|
214
|
+
source: "manual-connect"
|
|
215
|
+
});
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
this.addresses = connectedAddresses;
|
|
219
|
+
this.connected = true;
|
|
220
|
+
const result = {
|
|
221
|
+
addresses: this.addresses,
|
|
222
|
+
status: "completed"
|
|
223
|
+
// walletId is not applicable for injected providers
|
|
224
|
+
};
|
|
225
|
+
this.emit("connect", {
|
|
226
|
+
addresses: this.addresses,
|
|
227
|
+
source: "manual-connect"
|
|
228
|
+
});
|
|
229
|
+
return result;
|
|
230
|
+
} catch (error) {
|
|
231
|
+
if (error instanceof Error && !error.message.includes("Phantom wallet not found") && !error.message.includes("Failed to connect to any supported wallet provider")) {
|
|
232
|
+
this.emit("connect_error", {
|
|
233
|
+
error: error.message,
|
|
234
|
+
source: "manual-connect"
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
throw error;
|
|
195
238
|
}
|
|
196
|
-
if (connectedAddresses.length === 0) {
|
|
197
|
-
throw new Error("Failed to connect to any supported wallet provider");
|
|
198
|
-
}
|
|
199
|
-
this.addresses = connectedAddresses;
|
|
200
|
-
this.connected = true;
|
|
201
|
-
return {
|
|
202
|
-
addresses: this.addresses,
|
|
203
|
-
status: "completed"
|
|
204
|
-
// walletId is not applicable for injected providers
|
|
205
|
-
};
|
|
206
239
|
}
|
|
207
240
|
async disconnect() {
|
|
241
|
+
debug.info(DebugCategory.INJECTED_PROVIDER, "Starting injected provider disconnect");
|
|
208
242
|
if (this.addressTypes.includes(import_client.AddressType.solana)) {
|
|
209
243
|
try {
|
|
210
244
|
await this.phantom.solana.disconnect();
|
|
245
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Solana disconnected successfully");
|
|
211
246
|
} catch (err) {
|
|
212
|
-
|
|
247
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to disconnect Solana", { error: err });
|
|
213
248
|
}
|
|
214
249
|
}
|
|
215
250
|
if (this.addressTypes.includes(import_client.AddressType.ethereum)) {
|
|
216
251
|
try {
|
|
217
252
|
await this.phantom.ethereum.disconnect();
|
|
253
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Ethereum disconnected successfully");
|
|
218
254
|
} catch (err) {
|
|
219
|
-
|
|
255
|
+
debug.warn(DebugCategory.INJECTED_PROVIDER, "Failed to disconnect Ethereum", { error: err });
|
|
220
256
|
}
|
|
221
257
|
}
|
|
258
|
+
this.browserInjectedCleanupFunctions.forEach((cleanup) => cleanup());
|
|
259
|
+
this.browserInjectedCleanupFunctions = [];
|
|
222
260
|
this.connected = false;
|
|
223
261
|
this.addresses = [];
|
|
262
|
+
this.emit("disconnect", {
|
|
263
|
+
source: "manual-disconnect"
|
|
264
|
+
});
|
|
265
|
+
debug.info(DebugCategory.INJECTED_PROVIDER, "Injected provider disconnected successfully");
|
|
224
266
|
}
|
|
225
267
|
async signMessage(params) {
|
|
226
268
|
if (!this.connected) {
|
|
@@ -293,6 +335,139 @@ var InjectedProvider = class {
|
|
|
293
335
|
isConnected() {
|
|
294
336
|
return this.connected;
|
|
295
337
|
}
|
|
338
|
+
// Event management methods - implementing unified event interface
|
|
339
|
+
on(event, callback) {
|
|
340
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Adding event listener", { event });
|
|
341
|
+
if (!this.eventsInitialized) {
|
|
342
|
+
this.setupBrowserInjectedEvents();
|
|
343
|
+
this.eventsInitialized = true;
|
|
344
|
+
}
|
|
345
|
+
if (!this.eventListeners.has(event)) {
|
|
346
|
+
this.eventListeners.set(event, /* @__PURE__ */ new Set());
|
|
347
|
+
}
|
|
348
|
+
this.eventListeners.get(event).add(callback);
|
|
349
|
+
}
|
|
350
|
+
off(event, callback) {
|
|
351
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Removing event listener", { event });
|
|
352
|
+
if (this.eventListeners.has(event)) {
|
|
353
|
+
this.eventListeners.get(event).delete(callback);
|
|
354
|
+
if (this.eventListeners.get(event).size === 0) {
|
|
355
|
+
this.eventListeners.delete(event);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
emit(event, data) {
|
|
360
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Emitting event", {
|
|
361
|
+
event,
|
|
362
|
+
listenerCount: this.eventListeners.get(event)?.size || 0,
|
|
363
|
+
data
|
|
364
|
+
});
|
|
365
|
+
const listeners = this.eventListeners.get(event);
|
|
366
|
+
if (listeners && listeners.size > 0) {
|
|
367
|
+
listeners.forEach((callback) => {
|
|
368
|
+
try {
|
|
369
|
+
callback(data);
|
|
370
|
+
} catch (error) {
|
|
371
|
+
debug.error(DebugCategory.INJECTED_PROVIDER, "Event callback error", { event, error });
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
setupBrowserInjectedEvents() {
|
|
377
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Setting up browser-injected-sdk event listeners");
|
|
378
|
+
if (this.addressTypes.includes(import_client.AddressType.solana) && this.phantom.solana) {
|
|
379
|
+
this.setupSolanaEvents();
|
|
380
|
+
}
|
|
381
|
+
if (this.addressTypes.includes(import_client.AddressType.ethereum) && this.phantom.ethereum) {
|
|
382
|
+
this.setupEthereumEvents();
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
setupSolanaEvents() {
|
|
386
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Setting up Solana event listeners");
|
|
387
|
+
const solanaConnectCleanup = this.phantom.solana.addEventListener("connect", (publicKey) => {
|
|
388
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Solana connect event received", { publicKey });
|
|
389
|
+
const solanaAddress = { addressType: import_client.AddressType.solana, address: publicKey };
|
|
390
|
+
if (!this.addresses.find((addr) => addr.addressType === import_client.AddressType.solana)) {
|
|
391
|
+
this.addresses.push(solanaAddress);
|
|
392
|
+
}
|
|
393
|
+
this.connected = true;
|
|
394
|
+
this.emit("connect", {
|
|
395
|
+
addresses: this.addresses,
|
|
396
|
+
source: "injected-extension"
|
|
397
|
+
});
|
|
398
|
+
});
|
|
399
|
+
const solanaDisconnectCleanup = this.phantom.solana.addEventListener("disconnect", () => {
|
|
400
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Solana disconnect event received");
|
|
401
|
+
this.addresses = this.addresses.filter((addr) => addr.addressType !== import_client.AddressType.solana);
|
|
402
|
+
this.connected = this.addresses.length > 0;
|
|
403
|
+
this.emit("disconnect", {
|
|
404
|
+
source: "injected-extension"
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
const solanaAccountChangedCleanup = this.phantom.solana.addEventListener("accountChanged", (publicKey) => {
|
|
408
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Solana account changed event received", { publicKey });
|
|
409
|
+
const solanaIndex = this.addresses.findIndex((addr) => addr.addressType === import_client.AddressType.solana);
|
|
410
|
+
if (solanaIndex >= 0) {
|
|
411
|
+
this.addresses[solanaIndex] = { addressType: import_client.AddressType.solana, address: publicKey };
|
|
412
|
+
} else {
|
|
413
|
+
this.addresses.push({ addressType: import_client.AddressType.solana, address: publicKey });
|
|
414
|
+
}
|
|
415
|
+
this.emit("connect", {
|
|
416
|
+
addresses: this.addresses,
|
|
417
|
+
source: "injected-extension-account-change"
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
this.browserInjectedCleanupFunctions.push(
|
|
421
|
+
solanaConnectCleanup,
|
|
422
|
+
solanaDisconnectCleanup,
|
|
423
|
+
solanaAccountChangedCleanup
|
|
424
|
+
);
|
|
425
|
+
}
|
|
426
|
+
setupEthereumEvents() {
|
|
427
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Setting up Ethereum event listeners");
|
|
428
|
+
const ethConnectCleanup = this.phantom.ethereum.addEventListener("connect", (accounts) => {
|
|
429
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Ethereum connect event received", { accounts });
|
|
430
|
+
this.addresses = this.addresses.filter((addr) => addr.addressType !== import_client.AddressType.ethereum);
|
|
431
|
+
if (accounts && accounts.length > 0) {
|
|
432
|
+
this.addresses.push(...accounts.map((address) => ({
|
|
433
|
+
addressType: import_client.AddressType.ethereum,
|
|
434
|
+
address
|
|
435
|
+
})));
|
|
436
|
+
}
|
|
437
|
+
this.connected = this.addresses.length > 0;
|
|
438
|
+
this.emit("connect", {
|
|
439
|
+
addresses: this.addresses,
|
|
440
|
+
source: "injected-extension"
|
|
441
|
+
});
|
|
442
|
+
});
|
|
443
|
+
const ethDisconnectCleanup = this.phantom.ethereum.addEventListener("disconnect", () => {
|
|
444
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Ethereum disconnect event received");
|
|
445
|
+
this.addresses = this.addresses.filter((addr) => addr.addressType !== import_client.AddressType.ethereum);
|
|
446
|
+
this.connected = this.addresses.length > 0;
|
|
447
|
+
this.emit("disconnect", {
|
|
448
|
+
source: "injected-extension"
|
|
449
|
+
});
|
|
450
|
+
});
|
|
451
|
+
const ethAccountChangedCleanup = this.phantom.ethereum.addEventListener("accountChanged", (accounts) => {
|
|
452
|
+
debug.log(DebugCategory.INJECTED_PROVIDER, "Ethereum account changed event received", { accounts });
|
|
453
|
+
this.addresses = this.addresses.filter((addr) => addr.addressType !== import_client.AddressType.ethereum);
|
|
454
|
+
if (accounts && accounts.length > 0) {
|
|
455
|
+
this.addresses.push(...accounts.map((address) => ({
|
|
456
|
+
addressType: import_client.AddressType.ethereum,
|
|
457
|
+
address
|
|
458
|
+
})));
|
|
459
|
+
}
|
|
460
|
+
this.emit("connect", {
|
|
461
|
+
addresses: this.addresses,
|
|
462
|
+
source: "injected-extension-account-change"
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
this.browserInjectedCleanupFunctions.push(
|
|
466
|
+
ethConnectCleanup,
|
|
467
|
+
ethDisconnectCleanup,
|
|
468
|
+
ethAccountChangedCleanup
|
|
469
|
+
);
|
|
470
|
+
}
|
|
296
471
|
};
|
|
297
472
|
|
|
298
473
|
// src/providers/embedded/index.ts
|
|
@@ -662,11 +837,15 @@ var EmbeddedProvider = class extends import_embedded_provider_core.EmbeddedProvi
|
|
|
662
837
|
|
|
663
838
|
// src/ProviderManager.ts
|
|
664
839
|
var ProviderManager = class {
|
|
840
|
+
// Track which providers have forwarding set up
|
|
665
841
|
constructor(config) {
|
|
666
842
|
this.providers = /* @__PURE__ */ new Map();
|
|
667
843
|
this.currentProvider = null;
|
|
668
844
|
this.currentProviderKey = null;
|
|
669
845
|
this.walletId = null;
|
|
846
|
+
// Event management for forwarding provider events
|
|
847
|
+
this.eventListeners = /* @__PURE__ */ new Map();
|
|
848
|
+
this.providerForwardingSetup = /* @__PURE__ */ new WeakSet();
|
|
670
849
|
debug.log(DebugCategory.PROVIDER_MANAGER, "Initializing ProviderManager", { config });
|
|
671
850
|
this.config = config;
|
|
672
851
|
debug.log(DebugCategory.PROVIDER_MANAGER, "Setting default provider");
|
|
@@ -691,6 +870,7 @@ var ProviderManager = class {
|
|
|
691
870
|
this.currentProvider = this.providers.get(key);
|
|
692
871
|
this.currentProviderKey = key;
|
|
693
872
|
this.walletId = null;
|
|
873
|
+
this.ensureProviderEventForwarding();
|
|
694
874
|
return this.currentProvider;
|
|
695
875
|
}
|
|
696
876
|
/**
|
|
@@ -785,6 +965,79 @@ var ProviderManager = class {
|
|
|
785
965
|
getWalletId() {
|
|
786
966
|
return this.walletId;
|
|
787
967
|
}
|
|
968
|
+
/**
|
|
969
|
+
* Add event listener - stores callback and ensures current provider forwards events to ProviderManager
|
|
970
|
+
*/
|
|
971
|
+
on(event, callback) {
|
|
972
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Adding event listener", { event });
|
|
973
|
+
if (!this.eventListeners.has(event)) {
|
|
974
|
+
this.eventListeners.set(event, /* @__PURE__ */ new Set());
|
|
975
|
+
}
|
|
976
|
+
this.eventListeners.get(event).add(callback);
|
|
977
|
+
this.ensureProviderEventForwarding();
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Remove event listener
|
|
981
|
+
*/
|
|
982
|
+
off(event, callback) {
|
|
983
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Removing event listener", { event });
|
|
984
|
+
if (this.eventListeners.has(event)) {
|
|
985
|
+
this.eventListeners.get(event).delete(callback);
|
|
986
|
+
if (this.eventListeners.get(event).size === 0) {
|
|
987
|
+
this.eventListeners.delete(event);
|
|
988
|
+
}
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
/**
|
|
992
|
+
* Emit event to all registered callbacks
|
|
993
|
+
*/
|
|
994
|
+
emit(event, data) {
|
|
995
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Emitting event to stored callbacks", {
|
|
996
|
+
event,
|
|
997
|
+
listenerCount: this.eventListeners.get(event)?.size || 0,
|
|
998
|
+
data
|
|
999
|
+
});
|
|
1000
|
+
const listeners = this.eventListeners.get(event);
|
|
1001
|
+
if (listeners && listeners.size > 0) {
|
|
1002
|
+
listeners.forEach((callback) => {
|
|
1003
|
+
try {
|
|
1004
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Calling stored callback for event", { event });
|
|
1005
|
+
callback(data);
|
|
1006
|
+
} catch (error) {
|
|
1007
|
+
debug.error(DebugCategory.PROVIDER_MANAGER, "Event callback error", { event, error });
|
|
1008
|
+
}
|
|
1009
|
+
});
|
|
1010
|
+
} else {
|
|
1011
|
+
debug.warn(DebugCategory.PROVIDER_MANAGER, "No stored callbacks for event", { event });
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
/**
|
|
1015
|
+
* Ensure current provider forwards its events to this ProviderManager
|
|
1016
|
+
* Only sets up forwarding once per provider instance to avoid accumulation
|
|
1017
|
+
*/
|
|
1018
|
+
ensureProviderEventForwarding() {
|
|
1019
|
+
if (!this.currentProvider || !("on" in this.currentProvider)) {
|
|
1020
|
+
debug.warn(DebugCategory.PROVIDER_MANAGER, "Current provider does not support events", {
|
|
1021
|
+
providerType: this.getCurrentProviderInfo()?.type
|
|
1022
|
+
});
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
1025
|
+
if (this.providerForwardingSetup.has(this.currentProvider)) {
|
|
1026
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Event forwarding already set up for current provider");
|
|
1027
|
+
return;
|
|
1028
|
+
}
|
|
1029
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Setting up event forwarding from current provider");
|
|
1030
|
+
const eventsToForward = ["connect_start", "connect", "connect_error", "disconnect", "error"];
|
|
1031
|
+
for (const event of eventsToForward) {
|
|
1032
|
+
const forwardingCallback = (data) => {
|
|
1033
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Forwarding event from provider", { event, data });
|
|
1034
|
+
this.emit(event, data);
|
|
1035
|
+
};
|
|
1036
|
+
debug.log(DebugCategory.PROVIDER_MANAGER, "Attaching forwarding callback for event", { event });
|
|
1037
|
+
this.currentProvider.on(event, forwardingCallback);
|
|
1038
|
+
}
|
|
1039
|
+
this.providerForwardingSetup.add(this.currentProvider);
|
|
1040
|
+
}
|
|
788
1041
|
/**
|
|
789
1042
|
* Set default provider based on initial config
|
|
790
1043
|
*/
|
|
@@ -805,7 +1058,7 @@ var ProviderManager = class {
|
|
|
805
1058
|
if (type === "injected") {
|
|
806
1059
|
provider = new InjectedProvider({
|
|
807
1060
|
solanaProvider: this.config.solanaProvider || "web3js",
|
|
808
|
-
addressTypes: this.config.addressTypes
|
|
1061
|
+
addressTypes: this.config.addressTypes
|
|
809
1062
|
});
|
|
810
1063
|
} else {
|
|
811
1064
|
if (!this.config.apiBaseUrl || !this.config.organizationId) {
|
|
@@ -816,7 +1069,7 @@ var ProviderManager = class {
|
|
|
816
1069
|
organizationId: this.config.organizationId,
|
|
817
1070
|
authOptions: this.config.authOptions,
|
|
818
1071
|
embeddedWalletType: embeddedWalletType || "app-wallet",
|
|
819
|
-
addressTypes: this.config.addressTypes
|
|
1072
|
+
addressTypes: this.config.addressTypes,
|
|
820
1073
|
solanaProvider: this.config.solanaProvider || "web3js",
|
|
821
1074
|
appLogo: this.config.appLogo,
|
|
822
1075
|
// Optional app logo URL
|
|
@@ -871,20 +1124,10 @@ var ProviderManager = class {
|
|
|
871
1124
|
var import_browser_injected_sdk2 = require("@phantom/browser-injected-sdk");
|
|
872
1125
|
var BrowserSDK = class {
|
|
873
1126
|
constructor(config) {
|
|
874
|
-
if (config.debug?.enabled) {
|
|
875
|
-
debug.enable();
|
|
876
|
-
if (config.debug.level !== void 0) {
|
|
877
|
-
debug.setLevel(config.debug.level);
|
|
878
|
-
}
|
|
879
|
-
if (config.debug.callback) {
|
|
880
|
-
debug.setCallback(config.debug.callback);
|
|
881
|
-
}
|
|
882
|
-
}
|
|
883
1127
|
debug.info(DebugCategory.BROWSER_SDK, "Initializing BrowserSDK", {
|
|
884
1128
|
providerType: config.providerType,
|
|
885
1129
|
embeddedWalletType: config.embeddedWalletType,
|
|
886
|
-
addressTypes: config.addressTypes
|
|
887
|
-
debugEnabled: config.debug?.enabled
|
|
1130
|
+
addressTypes: config.addressTypes
|
|
888
1131
|
});
|
|
889
1132
|
if (!["injected", "embedded"].includes(config.providerType)) {
|
|
890
1133
|
debug.error(DebugCategory.BROWSER_SDK, "Invalid providerType", { providerType: config.providerType });
|
|
@@ -1038,8 +1281,92 @@ var BrowserSDK = class {
|
|
|
1038
1281
|
getWalletId() {
|
|
1039
1282
|
return this.providerManager.getWalletId();
|
|
1040
1283
|
}
|
|
1284
|
+
/**
|
|
1285
|
+
* Add event listener for provider events (connect, connect_start, connect_error, disconnect, error)
|
|
1286
|
+
* Works with both embedded and injected providers
|
|
1287
|
+
*/
|
|
1288
|
+
on(event, callback) {
|
|
1289
|
+
debug.log(DebugCategory.BROWSER_SDK, "Adding event listener", { event });
|
|
1290
|
+
this.providerManager.on(event, callback);
|
|
1291
|
+
}
|
|
1292
|
+
/**
|
|
1293
|
+
* Remove event listener for provider events
|
|
1294
|
+
* Works with both embedded and injected providers
|
|
1295
|
+
*/
|
|
1296
|
+
off(event, callback) {
|
|
1297
|
+
debug.log(DebugCategory.BROWSER_SDK, "Removing event listener", { event });
|
|
1298
|
+
this.providerManager.off(event, callback);
|
|
1299
|
+
}
|
|
1300
|
+
/**
|
|
1301
|
+
* Attempt auto-connection using existing session
|
|
1302
|
+
* Should be called after setting up event listeners
|
|
1303
|
+
* Only works with embedded providers
|
|
1304
|
+
*/
|
|
1305
|
+
async autoConnect() {
|
|
1306
|
+
debug.log(DebugCategory.BROWSER_SDK, "Attempting auto-connect");
|
|
1307
|
+
const currentProvider = this.providerManager.getCurrentProvider();
|
|
1308
|
+
if (currentProvider && "autoConnect" in currentProvider) {
|
|
1309
|
+
await currentProvider.autoConnect();
|
|
1310
|
+
} else {
|
|
1311
|
+
debug.warn(DebugCategory.BROWSER_SDK, "Current provider does not support auto-connect", {
|
|
1312
|
+
providerType: this.getCurrentProviderInfo()?.type
|
|
1313
|
+
});
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
/**
|
|
1317
|
+
* Debug configuration methods
|
|
1318
|
+
* These allow dynamic debug configuration without SDK reinstantiation
|
|
1319
|
+
*/
|
|
1320
|
+
/**
|
|
1321
|
+
* Enable debug logging
|
|
1322
|
+
*/
|
|
1323
|
+
enableDebug() {
|
|
1324
|
+
debug.enable();
|
|
1325
|
+
debug.info(DebugCategory.BROWSER_SDK, "Debug logging enabled");
|
|
1326
|
+
}
|
|
1327
|
+
/**
|
|
1328
|
+
* Disable debug logging
|
|
1329
|
+
*/
|
|
1330
|
+
disableDebug() {
|
|
1331
|
+
debug.disable();
|
|
1332
|
+
}
|
|
1333
|
+
/**
|
|
1334
|
+
* Set debug level
|
|
1335
|
+
*/
|
|
1336
|
+
setDebugLevel(level) {
|
|
1337
|
+
debug.setLevel(level);
|
|
1338
|
+
debug.info(DebugCategory.BROWSER_SDK, "Debug level updated", { level });
|
|
1339
|
+
}
|
|
1340
|
+
/**
|
|
1341
|
+
* Set debug callback function
|
|
1342
|
+
*/
|
|
1343
|
+
setDebugCallback(callback) {
|
|
1344
|
+
debug.setCallback(callback);
|
|
1345
|
+
debug.info(DebugCategory.BROWSER_SDK, "Debug callback updated");
|
|
1346
|
+
}
|
|
1347
|
+
/**
|
|
1348
|
+
* Configure debug settings all at once
|
|
1349
|
+
*/
|
|
1350
|
+
configureDebug(config) {
|
|
1351
|
+
if (config.enabled !== void 0) {
|
|
1352
|
+
if (config.enabled) {
|
|
1353
|
+
this.enableDebug();
|
|
1354
|
+
} else {
|
|
1355
|
+
this.disableDebug();
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
if (config.level !== void 0) {
|
|
1359
|
+
this.setDebugLevel(config.level);
|
|
1360
|
+
}
|
|
1361
|
+
if (config.callback !== void 0) {
|
|
1362
|
+
this.setDebugCallback(config.callback);
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1041
1365
|
};
|
|
1042
1366
|
|
|
1367
|
+
// src/types.ts
|
|
1368
|
+
var import_client2 = require("@phantom/client");
|
|
1369
|
+
|
|
1043
1370
|
// src/index.ts
|
|
1044
1371
|
var import_constants3 = require("@phantom/constants");
|
|
1045
|
-
var
|
|
1372
|
+
var import_client3 = require("@phantom/client");
|